Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame^] | 1 | /* |
| 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 "DisplayHardware/HWC2.h" |
| 25 | #include "Scheduler/RefreshRateConfigs.h" |
| 26 | #include "mock/DisplayHardware/MockDisplay.h" |
| 27 | |
| 28 | using namespace std::chrono_literals; |
| 29 | using testing::_; |
| 30 | |
| 31 | namespace android { |
| 32 | namespace scheduler { |
| 33 | |
| 34 | using RefreshRateType = RefreshRateConfigs::RefreshRateType; |
| 35 | using RefreshRate = RefreshRateConfigs::RefreshRate; |
| 36 | |
| 37 | class RefreshRateConfigsTest : public testing::Test { |
| 38 | protected: |
| 39 | static constexpr int CONFIG_ID_60 = 0; |
| 40 | static constexpr int CONFIG_ID_90 = 1; |
| 41 | static constexpr int64_t VSYNC_60 = 16666667; |
| 42 | static constexpr int64_t VSYNC_90 = 11111111; |
| 43 | |
| 44 | RefreshRateConfigsTest(); |
| 45 | ~RefreshRateConfigsTest(); |
| 46 | |
| 47 | void assertRatesEqual(const RefreshRate& left, const RefreshRate& right) { |
| 48 | ASSERT_EQ(left.configId, right.configId); |
| 49 | ASSERT_EQ(left.name, right.name); |
| 50 | ASSERT_EQ(left.fps, right.fps); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | RefreshRateConfigsTest::RefreshRateConfigsTest() { |
| 55 | const ::testing::TestInfo* const test_info = |
| 56 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 57 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 58 | } |
| 59 | |
| 60 | RefreshRateConfigsTest::~RefreshRateConfigsTest() { |
| 61 | const ::testing::TestInfo* const test_info = |
| 62 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 63 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 64 | } |
| 65 | |
| 66 | namespace { |
| 67 | /* ------------------------------------------------------------------------ |
| 68 | * Test cases |
| 69 | */ |
| 70 | TEST_F(RefreshRateConfigsTest, zeroDeviceConfigs_storesPowerSavingConfig) { |
| 71 | std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs; |
| 72 | RefreshRateConfigs configs(displayConfigs); |
| 73 | |
| 74 | // We always store a configuration for screen off. |
| 75 | const auto& rates = configs.getRefreshRates(); |
| 76 | ASSERT_EQ(1, rates.size()); |
| 77 | const auto& powerSavingRate = rates.find(RefreshRateType::POWER_SAVING); |
| 78 | ASSERT_NE(rates.end(), powerSavingRate); |
| 79 | ASSERT_EQ(rates.end(), rates.find(RefreshRateType::PERFORMANCE)); |
| 80 | ASSERT_EQ(rates.end(), rates.find(RefreshRateType::DEFAULT)); |
| 81 | |
| 82 | RefreshRate expectedConfig = RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0}; |
| 83 | assertRatesEqual(expectedConfig, *powerSavingRate->second); |
| 84 | |
| 85 | ASSERT_TRUE(configs.getRefreshRate(RefreshRateType::POWER_SAVING)); |
| 86 | assertRatesEqual(expectedConfig, *configs.getRefreshRate(RefreshRateType::POWER_SAVING)); |
| 87 | ASSERT_FALSE(configs.getRefreshRate(RefreshRateType::PERFORMANCE)); |
| 88 | ASSERT_FALSE(configs.getRefreshRate(RefreshRateType::DEFAULT)); |
| 89 | |
| 90 | // Sanity check that getRefreshRate() does not modify the underlying configs. |
| 91 | ASSERT_EQ(1, configs.getRefreshRates().size()); |
| 92 | } |
| 93 | |
| 94 | TEST_F(RefreshRateConfigsTest, oneDeviceConfig_storesDefaultConfig) { |
| 95 | auto display = new Hwc2::mock::Display(); |
| 96 | std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs; |
| 97 | auto config60 = HWC2::Display::Config::Builder(*display, CONFIG_ID_60); |
| 98 | config60.setVsyncPeriod(VSYNC_60); |
| 99 | displayConfigs.push_back(config60.build()); |
| 100 | RefreshRateConfigs configs(displayConfigs); |
| 101 | |
| 102 | const auto& rates = configs.getRefreshRates(); |
| 103 | ASSERT_EQ(2, rates.size()); |
| 104 | const auto& powerSavingRate = rates.find(RefreshRateType::POWER_SAVING); |
| 105 | const auto& defaultRate = rates.find(RefreshRateType::DEFAULT); |
| 106 | ASSERT_NE(rates.end(), powerSavingRate); |
| 107 | ASSERT_NE(rates.end(), defaultRate); |
| 108 | ASSERT_EQ(rates.end(), rates.find(RefreshRateType::PERFORMANCE)); |
| 109 | |
| 110 | RefreshRate expectedPowerSavingConfig = RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0}; |
| 111 | assertRatesEqual(expectedPowerSavingConfig, *powerSavingRate->second); |
| 112 | RefreshRate expectedDefaultConfig = RefreshRate{CONFIG_ID_60, "60fps", 60}; |
| 113 | assertRatesEqual(expectedDefaultConfig, *defaultRate->second); |
| 114 | |
| 115 | ASSERT_TRUE(configs.getRefreshRate(RefreshRateType::POWER_SAVING)); |
| 116 | assertRatesEqual(expectedPowerSavingConfig, |
| 117 | *configs.getRefreshRate(RefreshRateType::POWER_SAVING)); |
| 118 | ASSERT_TRUE(configs.getRefreshRate(RefreshRateType::DEFAULT)); |
| 119 | assertRatesEqual(expectedDefaultConfig, *configs.getRefreshRate(RefreshRateType::DEFAULT)); |
| 120 | ASSERT_FALSE(configs.getRefreshRate(RefreshRateType::PERFORMANCE)); |
| 121 | |
| 122 | // Sanity check that getRefreshRate() does not modify the underlying configs. |
| 123 | ASSERT_EQ(2, configs.getRefreshRates().size()); |
| 124 | } |
| 125 | |
| 126 | TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_storesPerformanceConfig) { |
| 127 | auto display = new Hwc2::mock::Display(); |
| 128 | std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs; |
| 129 | auto config60 = HWC2::Display::Config::Builder(*display, CONFIG_ID_60); |
| 130 | config60.setVsyncPeriod(VSYNC_60); |
| 131 | displayConfigs.push_back(config60.build()); |
| 132 | auto config90 = HWC2::Display::Config::Builder(*display, CONFIG_ID_90); |
| 133 | config90.setVsyncPeriod(VSYNC_90); |
| 134 | displayConfigs.push_back(config90.build()); |
| 135 | RefreshRateConfigs configs(displayConfigs); |
| 136 | |
| 137 | const auto& rates = configs.getRefreshRates(); |
| 138 | ASSERT_EQ(3, rates.size()); |
| 139 | const auto& powerSavingRate = rates.find(RefreshRateType::POWER_SAVING); |
| 140 | const auto& defaultRate = rates.find(RefreshRateType::DEFAULT); |
| 141 | const auto& performanceRate = rates.find(RefreshRateType::PERFORMANCE); |
| 142 | ASSERT_NE(rates.end(), powerSavingRate); |
| 143 | ASSERT_NE(rates.end(), defaultRate); |
| 144 | ASSERT_NE(rates.end(), performanceRate); |
| 145 | |
| 146 | RefreshRate expectedPowerSavingConfig = RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0}; |
| 147 | assertRatesEqual(expectedPowerSavingConfig, *powerSavingRate->second); |
| 148 | RefreshRate expectedDefaultConfig = RefreshRate{CONFIG_ID_60, "60fps", 60}; |
| 149 | assertRatesEqual(expectedDefaultConfig, *defaultRate->second); |
| 150 | RefreshRate expectedPerformanceConfig = RefreshRate{CONFIG_ID_90, "90fps", 90}; |
| 151 | assertRatesEqual(expectedPerformanceConfig, *performanceRate->second); |
| 152 | |
| 153 | ASSERT_TRUE(configs.getRefreshRate(RefreshRateType::POWER_SAVING)); |
| 154 | assertRatesEqual(expectedPowerSavingConfig, |
| 155 | *configs.getRefreshRate(RefreshRateType::POWER_SAVING)); |
| 156 | ASSERT_TRUE(configs.getRefreshRate(RefreshRateType::DEFAULT)); |
| 157 | assertRatesEqual(expectedDefaultConfig, *configs.getRefreshRate(RefreshRateType::DEFAULT)); |
| 158 | ASSERT_TRUE(configs.getRefreshRate(RefreshRateType::PERFORMANCE)); |
| 159 | assertRatesEqual(expectedPerformanceConfig, |
| 160 | *configs.getRefreshRate(RefreshRateType::PERFORMANCE)); |
| 161 | } |
| 162 | } // namespace |
| 163 | } // namespace scheduler |
| 164 | } // namespace android |