1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sys/system_properties.h>
18 #include <random>
19 #include "test/gtest_and_gmock.h"
20
21 #include "perfetto/ext/base/android_utils.h"
22 #include "perfetto/tracing/core/data_source_config.h"
23 #include "src/base/test/test_task_runner.h"
24 #include "test/android_test_utils.h"
25 #include "test/test_helper.h"
26
27 #include "protos/perfetto/config/test_config.gen.h"
28 #include "protos/perfetto/trace/test_event.gen.h"
29 #include "protos/perfetto/trace/trace_packet.gen.h"
30
31 namespace perfetto {
32
33 class PerfettoCtsTest : public ::testing::Test {
34 protected:
TestMockProducer(const std::string & producer_name)35 void TestMockProducer(const std::string& producer_name) {
36 // Filter out watches; they do not have the required infrastructure to run
37 // these tests.
38 std::string characteristics =
39 base::GetAndroidProp("ro.build.characteristics");
40 if (characteristics.find("watch") != std::string::npos) {
41 return;
42 }
43
44 base::TestTaskRunner task_runner;
45
46 const std::string app_name = "android.perfetto.producer";
47 const std::string activity = "ProducerActivity";
48 if (IsAppRunning(app_name)) {
49 StopApp(app_name, "old.app.stopped", &task_runner);
50 task_runner.RunUntilCheckpoint("old.app.stopped");
51 }
52 StartAppActivity(app_name, activity, "target.app.running", &task_runner,
53 /*delay_ms=*/100);
54 task_runner.RunUntilCheckpoint("target.app.running");
55
56 const std::string isolated_process_name =
57 "android.perfetto.producer:android.perfetto.producer."
58 "ProducerIsolatedService";
59 WaitForProcess(isolated_process_name, "isolated.service.running",
60 &task_runner, 1000 /*ms*/);
61 task_runner.RunUntilCheckpoint("isolated.service.running");
62
63 TestHelper helper(&task_runner);
64 helper.ConnectConsumer();
65 helper.WaitForConsumerConnect();
66
67 TraceConfig trace_config;
68 trace_config.add_buffers()->set_size_kb(1024);
69 trace_config.set_duration_ms(200);
70
71 auto* ds_config = trace_config.add_data_sources()->mutable_config();
72 ds_config->set_name(producer_name);
73 ds_config->set_target_buffer(0);
74
75 static constexpr uint32_t kRandomSeed = 42;
76 static constexpr uint32_t kEventCount = 10;
77 static constexpr uint32_t kMessageSizeBytes = 1024;
78 ds_config->mutable_for_testing()->set_seed(kRandomSeed);
79 ds_config->mutable_for_testing()->set_message_count(kEventCount);
80 ds_config->mutable_for_testing()->set_message_size(kMessageSizeBytes);
81 ds_config->mutable_for_testing()->set_send_batch_on_register(true);
82
83 helper.StartTracing(trace_config);
84 helper.WaitForTracingDisabled();
85
86 helper.ReadData();
87 helper.WaitForReadData();
88
89 const auto& packets = helper.trace();
90 ASSERT_EQ(packets.size(), kEventCount);
91
92 std::minstd_rand0 rnd_engine(kRandomSeed);
93 for (const auto& packet : packets) {
94 ASSERT_TRUE(packet.has_for_testing());
95 ASSERT_EQ(packet.for_testing().seq_value(), rnd_engine());
96 }
97 }
98 };
99
TEST_F(PerfettoCtsTest,TestProducerActivity)100 TEST_F(PerfettoCtsTest, TestProducerActivity) {
101 TestMockProducer("android.perfetto.cts.ProducerActivity");
102 }
103
TEST_F(PerfettoCtsTest,TestProducerService)104 TEST_F(PerfettoCtsTest, TestProducerService) {
105 TestMockProducer("android.perfetto.cts.ProducerService");
106 }
107
TEST_F(PerfettoCtsTest,TestProducerIsolatedService)108 TEST_F(PerfettoCtsTest, TestProducerIsolatedService) {
109 TestMockProducer("android.perfetto.cts.ProducerIsolatedService");
110 }
111
112 } // namespace perfetto
113