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/tracing/core/data_source_config.h"
22 #include "src/base/test/test_task_runner.h"
23 #include "test/android_test_utils.h"
24 #include "test/test_helper.h"
25 
26 #include "protos/perfetto/config/test_config.gen.h"
27 #include "protos/perfetto/trace/test_event.gen.h"
28 #include "protos/perfetto/trace/trace_packet.gen.h"
29 
30 namespace perfetto {
31 
32 class PerfettoCtsTest : public ::testing::Test {
33  protected:
TestMockProducer(const std::string & producer_name)34   void TestMockProducer(const std::string& producer_name) {
35     // Filter out watches; they do not have the required infrastructure to run
36     // these tests.
37     char chars[PROP_VALUE_MAX + 1];
38     int ret = __system_property_get("ro.build.characteristics", chars);
39     ASSERT_GE(ret, 0);
40     std::string characteristics(chars);
41     if (characteristics.find("watch") != std::string::npos) {
42       return;
43     }
44 
45     base::TestTaskRunner task_runner;
46 
47     const std::string app_name = "android.perfetto.producer";
48     const std::string activity = "ProducerActivity";
49     if (IsAppRunning(app_name)) {
50       StopApp(app_name, "old.app.stopped", &task_runner);
51       task_runner.RunUntilCheckpoint("old.app.stopped");
52     }
53     StartAppActivity(app_name, activity, "target.app.running", &task_runner,
54                      /*delay_ms=*/100);
55     task_runner.RunUntilCheckpoint("target.app.running");
56 
57     const std::string isolated_process_name =
58         "android.perfetto.producer:android.perfetto.producer."
59         "ProducerIsolatedService";
60     WaitForProcess(isolated_process_name, "isolated.service.running",
61                    &task_runner, 1000 /*ms*/);
62     task_runner.RunUntilCheckpoint("isolated.service.running");
63 
64     TestHelper helper(&task_runner);
65     helper.ConnectConsumer();
66     helper.WaitForConsumerConnect();
67 
68     TraceConfig trace_config;
69     trace_config.add_buffers()->set_size_kb(1024);
70     trace_config.set_duration_ms(200);
71 
72     auto* ds_config = trace_config.add_data_sources()->mutable_config();
73     ds_config->set_name(producer_name);
74     ds_config->set_target_buffer(0);
75 
76     static constexpr uint32_t kRandomSeed = 42;
77     static constexpr uint32_t kEventCount = 10;
78     static constexpr uint32_t kMessageSizeBytes = 1024;
79     ds_config->mutable_for_testing()->set_seed(kRandomSeed);
80     ds_config->mutable_for_testing()->set_message_count(kEventCount);
81     ds_config->mutable_for_testing()->set_message_size(kMessageSizeBytes);
82     ds_config->mutable_for_testing()->set_send_batch_on_register(true);
83 
84     helper.StartTracing(trace_config);
85     helper.WaitForTracingDisabled();
86 
87     helper.ReadData();
88     helper.WaitForReadData();
89 
90     const auto& packets = helper.trace();
91     ASSERT_EQ(packets.size(), kEventCount);
92 
93     std::minstd_rand0 rnd_engine(kRandomSeed);
94     for (const auto& packet : packets) {
95       ASSERT_TRUE(packet.has_for_testing());
96       ASSERT_EQ(packet.for_testing().seq_value(), rnd_engine());
97     }
98   }
99 };
100 
TEST_F(PerfettoCtsTest,TestProducerActivity)101 TEST_F(PerfettoCtsTest, TestProducerActivity) {
102   TestMockProducer("android.perfetto.cts.ProducerActivity");
103 }
104 
TEST_F(PerfettoCtsTest,TestProducerService)105 TEST_F(PerfettoCtsTest, TestProducerService) {
106   TestMockProducer("android.perfetto.cts.ProducerService");
107 }
108 
TEST_F(PerfettoCtsTest,TestProducerIsolatedService)109 TEST_F(PerfettoCtsTest, TestProducerIsolatedService) {
110   TestMockProducer("android.perfetto.cts.ProducerIsolatedService");
111 }
112 
113 }  // namespace perfetto
114