• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_METHOD(const std::optional<std::string>,
48               ReadProperty,
49               (const std::string&),
50               (override));
51 };
52 
53 class AndroidSystemPropertyDataSourceTest : public ::testing::Test {
54  protected:
55   std::unique_ptr<TestAndroidSystemPropertyDataSource>
CreateAndroidSystemPropertyDataSource(const DataSourceConfig & config)56   CreateAndroidSystemPropertyDataSource(const DataSourceConfig& config) {
57     auto writer =
58         std::unique_ptr<TraceWriterForTesting>(new TraceWriterForTesting());
59     writer_raw_ = writer.get();
60     auto instance = std::unique_ptr<TestAndroidSystemPropertyDataSource>(
61         new TestAndroidSystemPropertyDataSource(&task_runner_, config,
62                                                 std::move(writer)));
63     return instance;
64   }
65 
66   base::TestTaskRunner task_runner_;
67   TraceWriterForTesting* writer_raw_ = nullptr;
68 };
69 
BuildConfig(const std::vector<std::string> & property_names)70 DataSourceConfig BuildConfig(const std::vector<std::string>& property_names) {
71   DataSourceConfig ds_config;
72   AndroidSystemPropertyConfig cfg;
73   for (auto name : property_names) {
74     cfg.add_property_name(name);
75   }
76   ds_config.set_android_system_property_config_raw(cfg.SerializeAsString());
77   return ds_config;
78 }
79 
TEST_F(AndroidSystemPropertyDataSourceTest,Success)80 TEST_F(AndroidSystemPropertyDataSourceTest, Success) {
81   auto data_source = CreateAndroidSystemPropertyDataSource(BuildConfig(
82       {"debug.tracing.screen_state", "debug.tracing.screen_brightness"}));
83   EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_state"))
84       .WillOnce(Return(std::make_optional("2")));
85   EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_brightness"))
86       .WillOnce(Return(std::make_optional("0.123456")));
87   data_source->Start();
88 
89   protos::gen::TracePacket packet = writer_raw_->GetOnlyTracePacket();
90   EXPECT_TRUE(packet.has_android_system_property());
91   auto properties = packet.android_system_property();
92   EXPECT_EQ(properties.values_size(), 2);
93 
94   EXPECT_EQ(properties.values()[0].name(), "debug.tracing.screen_state");
95   EXPECT_EQ(properties.values()[0].value(), "2");
96   EXPECT_EQ(properties.values()[1].name(), "debug.tracing.screen_brightness");
97   EXPECT_EQ(properties.values()[1].value(), "0.123456");
98 }
99 
TEST_F(AndroidSystemPropertyDataSourceTest,NotPermitted)100 TEST_F(AndroidSystemPropertyDataSourceTest, NotPermitted) {
101   auto data_source = CreateAndroidSystemPropertyDataSource(
102       BuildConfig({"something.with.wrong.prefix"}));
103   EXPECT_CALL(*data_source, ReadProperty("something.with.wrong.prefix"))
104       .Times(0);
105   data_source->Start();
106 
107   protos::gen::TracePacket packet = writer_raw_->GetOnlyTracePacket();
108   EXPECT_TRUE(packet.has_android_system_property());
109   auto properties = packet.android_system_property();
110   EXPECT_EQ(properties.values_size(), 0);
111 }
112 
TEST_F(AndroidSystemPropertyDataSourceTest,Failure)113 TEST_F(AndroidSystemPropertyDataSourceTest, Failure) {
114   auto data_source = CreateAndroidSystemPropertyDataSource(BuildConfig(
115       {"debug.tracing.screen_state", "debug.tracing.screen_brightness"}));
116   EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_state"))
117       .WillOnce(Return(std::nullopt));
118   EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_brightness"))
119       .WillOnce(Return(std::nullopt));
120   data_source->Start();
121 
122   protos::gen::TracePacket packet = writer_raw_->GetOnlyTracePacket();
123   auto properties = packet.android_system_property();
124   EXPECT_EQ(properties.values_size(), 0);
125 }
126 
127 // TODO(simonmacm) test poll_ms
128 }  // namespace
129 }  // namespace perfetto
130