1 /*
2  * Copyright (C) 2020 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/initial_display_state/initial_display_state_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/trace/android/initial_display_state.gen.h"
24 #include "protos/perfetto/trace/trace_packet.gen.h"
25 
26 using ::testing::AnyOf;
27 using ::testing::ElementsAre;
28 using ::testing::Return;
29 
30 namespace perfetto {
31 namespace {
32 
33 class TestInitialDisplayStateDataSource : public InitialDisplayStateDataSource {
34  public:
TestInitialDisplayStateDataSource(base::TaskRunner * task_runner,const DataSourceConfig & config,std::unique_ptr<TraceWriter> writer)35   TestInitialDisplayStateDataSource(base::TaskRunner* task_runner,
36                                     const DataSourceConfig& config,
37                                     std::unique_ptr<TraceWriter> writer)
38       : InitialDisplayStateDataSource(task_runner,
39                                       config,
40                                       /* session_id */ 0,
41                                       std::move(writer)) {}
42 
43   MOCK_METHOD1(ReadProperty,
44                const base::Optional<std::string>(const std::string));
45 };
46 
47 class InitialDisplayStateDataSourceTest : public ::testing::Test {
48  protected:
49   std::unique_ptr<TestInitialDisplayStateDataSource>
GetInitialDisplayStateDataSource(const DataSourceConfig & config)50   GetInitialDisplayStateDataSource(const DataSourceConfig& config) {
51     auto writer =
52         std::unique_ptr<TraceWriterForTesting>(new TraceWriterForTesting());
53     writer_raw_ = writer.get();
54     auto instance = std::unique_ptr<TestInitialDisplayStateDataSource>(
55         new TestInitialDisplayStateDataSource(&task_runner_, config,
56                                               std::move(writer)));
57     return instance;
58   }
59 
60   base::TestTaskRunner task_runner_;
61   TraceWriterForTesting* writer_raw_ = nullptr;
62 };
63 
TEST_F(InitialDisplayStateDataSourceTest,Success)64 TEST_F(InitialDisplayStateDataSourceTest, Success) {
65   ASSERT_TRUE(true);
66   auto data_source = GetInitialDisplayStateDataSource(DataSourceConfig());
67   EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_state"))
68       .WillOnce(Return(base::make_optional("2")));
69   EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_brightness"))
70       .WillOnce(Return(base::make_optional("0.123456")));
71   data_source->Start();
72 
73   protos::gen::TracePacket packet = writer_raw_->GetOnlyTracePacket();
74   ASSERT_TRUE(packet.has_initial_display_state());
75   auto state = packet.initial_display_state();
76   ASSERT_EQ(state.display_state(), 2);
77   ASSERT_EQ(state.brightness(), 0.123456);
78 }
79 
TEST_F(InitialDisplayStateDataSourceTest,Invalid)80 TEST_F(InitialDisplayStateDataSourceTest, Invalid) {
81   ASSERT_TRUE(true);
82   auto data_source = GetInitialDisplayStateDataSource(DataSourceConfig());
83   EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_state"))
84       .WillOnce(Return(base::make_optional("2")));
85   EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_brightness"))
86       .WillOnce(Return(base::make_optional("gotta wear shades")));
87   data_source->Start();
88 
89   protos::gen::TracePacket packet = writer_raw_->GetOnlyTracePacket();
90   ASSERT_TRUE(packet.has_initial_display_state());
91   auto state = packet.initial_display_state();
92   ASSERT_EQ(state.display_state(), 2);
93   ASSERT_FALSE(state.has_brightness());
94 }
95 
TEST_F(InitialDisplayStateDataSourceTest,Failure)96 TEST_F(InitialDisplayStateDataSourceTest, Failure) {
97   ASSERT_TRUE(true);
98   auto data_source = GetInitialDisplayStateDataSource(DataSourceConfig());
99   EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_state"))
100       .WillOnce(Return(base::nullopt));
101   EXPECT_CALL(*data_source, ReadProperty("debug.tracing.screen_brightness"))
102       .WillOnce(Return(base::nullopt));
103   data_source->Start();
104 
105   protos::gen::TracePacket packet = writer_raw_->GetOnlyTracePacket();
106   ASSERT_FALSE(packet.has_initial_display_state());
107 }
108 
109 }  // namespace
110 }  // namespace perfetto
111