blob: 0384d9dd9a024d24f3484a3bf4d6c1d9603140fa [file] [log] [blame]
Ana Krulec4593b692019-01-11 22:07:25 -08001/*
2 * Copyright 2019 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#undef LOG_TAG
18#define LOG_TAG "SchedulerUnittests"
19
20#include <gmock/gmock.h>
21#include <log/log.h>
22#include <thread>
23
24#include "Scheduler/RefreshRateStats.h"
25#include "mock/DisplayHardware/MockDisplay.h"
26
27using namespace std::chrono_literals;
28
29namespace android {
30namespace scheduler {
31
32class RefreshRateStatsTest : public testing::Test {
33protected:
34 static constexpr int CONFIG_ID_90 = 0;
35 static constexpr int CONFIG_ID_60 = 1;
36 static constexpr int64_t VSYNC_90 = 11111111;
37 static constexpr int64_t VSYNC_60 = 16666667;
38
39 RefreshRateStatsTest();
40 ~RefreshRateStatsTest();
41
42 void init(std::vector<std::shared_ptr<const HWC2::Display::Config>> configs);
43
44 std::unique_ptr<RefreshRateStats> mRefreshRateStats;
45};
46
47RefreshRateStatsTest::RefreshRateStatsTest() {
48 const ::testing::TestInfo* const test_info =
49 ::testing::UnitTest::GetInstance()->current_test_info();
50 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
51}
52
53RefreshRateStatsTest::~RefreshRateStatsTest() {
54 const ::testing::TestInfo* const test_info =
55 ::testing::UnitTest::GetInstance()->current_test_info();
56 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
57}
58
59void RefreshRateStatsTest::init(std::vector<std::shared_ptr<const HWC2::Display::Config>> configs) {
60 mRefreshRateStats = std::make_unique<RefreshRateStats>(configs);
61}
62
63namespace {
64/* ------------------------------------------------------------------------
65 * Test cases
66 */
67TEST_F(RefreshRateStatsTest, canCreateAndDestroyTest) {
68 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs;
69 init(configs);
70
71 // There is one default config, so the refresh rates should have one item.
72 ASSERT_EQ(1, mRefreshRateStats->getTotalTimes().size());
73}
74
75TEST_F(RefreshRateStatsTest, oneConfigTest) {
76 auto display = new Hwc2::mock::Display();
77
78 auto config = HWC2::Display::Config::Builder(*display, CONFIG_ID_90);
79 config.setVsyncPeriod(VSYNC_90);
80 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs;
81 configs.push_back(config.build());
82
83 init(configs);
84
85 std::unordered_map<std::string, int64_t> times = mRefreshRateStats->getTotalTimes();
86 ASSERT_EQ(2, times.size());
87 ASSERT_EQ(0, times["ScreenOff"]);
88 ASSERT_EQ(0, times["90fps"]);
89 // Setting up tests on mobile harness can be flaky with time passing, so testing for
90 // exact time changes can result in flaxy numbers. To avoid that remember old
91 // numbers to make sure the correct values are increasing in the next test.
92 int screenOff = times["ScreenOff"];
93 int ninety = times["90fps"];
94
95 // Screen is off by default.
96 std::this_thread::sleep_for(std::chrono::milliseconds(2));
97 times = mRefreshRateStats->getTotalTimes();
98 ASSERT_LT(screenOff, times["ScreenOff"]);
99 ASSERT_EQ(0, times["90fps"]);
100 screenOff = times["ScreenOff"];
101
102 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_NORMAL);
103 std::this_thread::sleep_for(std::chrono::milliseconds(2));
104 times = mRefreshRateStats->getTotalTimes();
105 ASSERT_EQ(screenOff, times["ScreenOff"]);
106 ASSERT_LT(ninety, times["90fps"]);
107 ninety = times["90fps"];
108
109 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_DOZE);
110 std::this_thread::sleep_for(std::chrono::milliseconds(2));
111 times = mRefreshRateStats->getTotalTimes();
112 ASSERT_LT(screenOff, times["ScreenOff"]);
113 ASSERT_EQ(ninety, times["90fps"]);
114 screenOff = times["ScreenOff"];
115
116 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
117 std::this_thread::sleep_for(std::chrono::milliseconds(2));
118 times = mRefreshRateStats->getTotalTimes();
119 // Because the power mode is not HWC_POWER_MODE_NORMAL, switching the config
120 // does not update refresh rates that come from the config.
121 ASSERT_LT(screenOff, times["ScreenOff"]);
122 ASSERT_EQ(ninety, times["90fps"]);
123}
124
125TEST_F(RefreshRateStatsTest, twoConfigsTest) {
126 auto display = new Hwc2::mock::Display();
127
128 auto config90 = HWC2::Display::Config::Builder(*display, CONFIG_ID_90);
129 config90.setVsyncPeriod(VSYNC_90);
130 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs;
131 configs.push_back(config90.build());
132
133 auto config60 = HWC2::Display::Config::Builder(*display, CONFIG_ID_60);
134 config60.setVsyncPeriod(VSYNC_60);
135 configs.push_back(config60.build());
136
137 init(configs);
138
139 std::unordered_map<std::string, int64_t> times = mRefreshRateStats->getTotalTimes();
140 ASSERT_EQ(3, times.size());
141 ASSERT_EQ(0, times["ScreenOff"]);
142 ASSERT_EQ(0, times["60fps"]);
143 ASSERT_EQ(0, times["90fps"]);
144 // Setting up tests on mobile harness can be flaky with time passing, so testing for
145 // exact time changes can result in flaxy numbers. To avoid that remember old
146 // numbers to make sure the correct values are increasing in the next test.
147 int screenOff = times["ScreenOff"];
148 int sixty = times["60fps"];
149 int ninety = times["90fps"];
150
151 // Screen is off by default.
152 std::this_thread::sleep_for(std::chrono::milliseconds(2));
153 times = mRefreshRateStats->getTotalTimes();
154 ASSERT_LT(screenOff, times["ScreenOff"]);
155 ASSERT_EQ(sixty, times["60fps"]);
156 ASSERT_EQ(ninety, times["90fps"]);
157 screenOff = times["ScreenOff"];
158
159 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_NORMAL);
160 std::this_thread::sleep_for(std::chrono::milliseconds(2));
161 times = mRefreshRateStats->getTotalTimes();
162 ASSERT_EQ(screenOff, times["ScreenOff"]);
163 ASSERT_EQ(sixty, times["60fps"]);
164 ASSERT_LT(ninety, times["90fps"]);
165 ninety = times["90fps"];
166
167 // When power mode is normal, time for configs updates.
168 mRefreshRateStats->setConfigMode(CONFIG_ID_60);
169 std::this_thread::sleep_for(std::chrono::milliseconds(2));
170 times = mRefreshRateStats->getTotalTimes();
171 ASSERT_EQ(screenOff, times["ScreenOff"]);
172 ASSERT_EQ(ninety, times["90fps"]);
173 ASSERT_LT(sixty, times["60fps"]);
174 sixty = times["60fps"];
175
176 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
177 std::this_thread::sleep_for(std::chrono::milliseconds(2));
178 times = mRefreshRateStats->getTotalTimes();
179 ASSERT_EQ(screenOff, times["ScreenOff"]);
180 ASSERT_LT(ninety, times["90fps"]);
181 ASSERT_EQ(sixty, times["60fps"]);
182 ninety = times["90fps"];
183
184 mRefreshRateStats->setConfigMode(CONFIG_ID_60);
185 std::this_thread::sleep_for(std::chrono::milliseconds(2));
186 times = mRefreshRateStats->getTotalTimes();
187 ASSERT_EQ(screenOff, times["ScreenOff"]);
188 ASSERT_EQ(ninety, times["90fps"]);
189 ASSERT_LT(sixty, times["60fps"]);
190 sixty = times["60fps"];
191
192 // Because the power mode is not HWC_POWER_MODE_NORMAL, switching the config
193 // does not update refresh rates that come from the config.
194 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_DOZE);
195 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
196 std::this_thread::sleep_for(std::chrono::milliseconds(2));
197 times = mRefreshRateStats->getTotalTimes();
198 ASSERT_LT(screenOff, times["ScreenOff"]);
199 ASSERT_EQ(ninety, times["90fps"]);
200 ASSERT_EQ(sixty, times["60fps"]);
201 screenOff = times["ScreenOff"];
202
203 mRefreshRateStats->setConfigMode(CONFIG_ID_60);
204 std::this_thread::sleep_for(std::chrono::milliseconds(2));
205 times = mRefreshRateStats->getTotalTimes();
206 ASSERT_LT(screenOff, times["ScreenOff"]);
207 ASSERT_EQ(ninety, times["90fps"]);
208 ASSERT_EQ(sixty, times["60fps"]);
209}
210} // namespace
211} // namespace scheduler
212} // namespace android