1 /*
2 * Copyright (C) 2022 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 "src/traced/probes/android_system_property/android_system_property_data_source.h"
18 #include "perfetto/tracing/core/data_source_config.h"
19 #include "src/base/test/test_task_runner.h"
20 #include "src/tracing/core/trace_writer_for_testing.h"
21 #include "test/gtest_and_gmock.h"
22
23 #include "protos/perfetto/config/android/android_system_property_config.gen.h"
24 #include "protos/perfetto/trace/android/android_system_property.gen.h"
25 #include "protos/perfetto/trace/trace_packet.gen.h"
26
27 using ::perfetto::protos::gen::AndroidSystemPropertyConfig;
28
29 using ::testing::AnyOf;
30 using ::testing::ElementsAre;
31 using ::testing::Return;
32
33 namespace perfetto {
34 namespace {
35
36 class TestAndroidSystemPropertyDataSource
37 : public AndroidSystemPropertyDataSource {
38 public:
TestAndroidSystemPropertyDataSource(base::TaskRunner * task_runner,const DataSourceConfig & config,std::unique_ptr<TraceWriter> writer)39 TestAndroidSystemPropertyDataSource(base::TaskRunner* task_runner,
40 const DataSourceConfig& config,
41 std::unique_ptr<TraceWriter> writer)
42 : AndroidSystemPropertyDataSource(task_runner,
43 config,
44 /* session_id */ 0,
45 std::move(writer)) {}
46
47 MOCK_METHOD1(ReadProperty,
48 const base::Optional<std::string>(const std::string&));
49 };
50
51 class AndroidSystemPropertyDataSourceTest : public ::testing::Test {
52 protected:
53 std::unique_ptr<TestAndroidSystemPropertyDataSource>
CreateAndroidSystemPropertyDataSource(const DataSourceConfig & config)54 CreateAndroidSystemPropertyDataSource(const DataSourceConfig& config) {
55 auto writer =
56 std::unique_ptr<TraceWriterForTesting>(new TraceWriterForTesting());
57 writer_raw_ = writer.get();
58 auto instance = std::unique_ptr<TestAndroidSystemPropertyDataSource>(
59 new TestAndroidSystemPropertyDataSource(&task_runner_, config,
60 std::move(writer)));
61 return instance;
62 }
63
64 base::TestTaskRunner task_runner_;
65 TraceWriterForTesting* writer_raw_ = nullptr;
66 };
67
BuildConfig(const std::vector<std::string> & property_names)68 DataSourceConfig BuildConfig(const std::vector<std::string>& property_names) {
69 DataSourceConfig ds_config;
70 AndroidSystemPropertyConfig cfg;
71 for (auto name : property_names) {
72 cfg.add_property_name(name);
73 }
74 ds_config.set_android_system_property_config_raw(cfg.SerializeAsString());
75 return ds_config;
76 }
77
TEST_F(AndroidSystemPropertyDataSourceTest,Success)78 TEST_F(AndroidSystemPropertyDataSourceTest, Success) {
79 auto data_source = CreateAndroidSystemPropertyDataSource(BuildConfig(
80 {"debug.tracing.screen_state", "debug.tracing.screen_brightness"}));
81 EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_state"))
82 .WillOnce(Return(base::make_optional("2")));
83 EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_brightness"))
84 .WillOnce(Return(base::make_optional("0.123456")));
85 data_source->Start();
86
87 protos::gen::TracePacket packet = writer_raw_->GetOnlyTracePacket();
88 EXPECT_TRUE(packet.has_android_system_property());
89 auto properties = packet.android_system_property();
90 EXPECT_EQ(properties.values_size(), 2);
91
92 EXPECT_EQ(properties.values()[0].name(), "debug.tracing.screen_state");
93 EXPECT_EQ(properties.values()[0].value(), "2");
94 EXPECT_EQ(properties.values()[1].name(), "debug.tracing.screen_brightness");
95 EXPECT_EQ(properties.values()[1].value(), "0.123456");
96 }
97
TEST_F(AndroidSystemPropertyDataSourceTest,NotPermitted)98 TEST_F(AndroidSystemPropertyDataSourceTest, NotPermitted) {
99 auto data_source = CreateAndroidSystemPropertyDataSource(
100 BuildConfig({"something.with.wrong.prefix"}));
101 EXPECT_CALL(*data_source, ReadProperty("something.with.wrong.prefix"))
102 .Times(0);
103 data_source->Start();
104
105 protos::gen::TracePacket packet = writer_raw_->GetOnlyTracePacket();
106 EXPECT_TRUE(packet.has_android_system_property());
107 auto properties = packet.android_system_property();
108 EXPECT_EQ(properties.values_size(), 0);
109 }
110
TEST_F(AndroidSystemPropertyDataSourceTest,Failure)111 TEST_F(AndroidSystemPropertyDataSourceTest, Failure) {
112 auto data_source = CreateAndroidSystemPropertyDataSource(BuildConfig(
113 {"debug.tracing.screen_state", "debug.tracing.screen_brightness"}));
114 EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_state"))
115 .WillOnce(Return(base::nullopt));
116 EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_brightness"))
117 .WillOnce(Return(base::nullopt));
118 data_source->Start();
119
120 protos::gen::TracePacket packet = writer_raw_->GetOnlyTracePacket();
121 auto properties = packet.android_system_property();
122 EXPECT_EQ(properties.values_size(), 0);
123 }
124
125 // TODO(simonmacm) test poll_ms
126 } // namespace
127 } // namespace perfetto
128