Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [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 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "SchedulerUnittests" |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 26 | #include <log/log.h> |
| 27 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 28 | #include <mutex> |
| 29 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 30 | #include "Scheduler/EventControlThread.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 31 | #include "Scheduler/EventThread.h" |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 32 | #include "Scheduler/RefreshRateConfigs.h" |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 33 | #include "TestableScheduler.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 34 | #include "mock/MockEventThread.h" |
| 35 | |
| 36 | using testing::_; |
| 37 | using testing::Return; |
| 38 | |
| 39 | namespace android { |
| 40 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 41 | constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID = 999; |
| 42 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 43 | class SchedulerTest : public testing::Test { |
| 44 | protected: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 45 | class MockEventThreadConnection : public android::EventThreadConnection { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 46 | public: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 47 | explicit MockEventThreadConnection(EventThread* eventThread) |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 48 | : EventThreadConnection(eventThread, ResyncCallback(), |
| 49 | ISurfaceComposer::eConfigChangedSuppress) {} |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 50 | ~MockEventThreadConnection() = default; |
| 51 | |
| 52 | MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel)); |
| 53 | MOCK_METHOD1(setVsyncRate, status_t(uint32_t count)); |
| 54 | MOCK_METHOD0(requestNextVsync, void()); |
| 55 | }; |
| 56 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 57 | SchedulerTest(); |
| 58 | ~SchedulerTest() override; |
| 59 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 60 | std::unique_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs; |
| 61 | std::unique_ptr<TestableScheduler> mScheduler; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 62 | |
| 63 | Scheduler::ConnectionHandle mConnectionHandle; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 64 | mock::EventThread* mEventThread; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 65 | sp<MockEventThreadConnection> mEventThreadConnection; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | SchedulerTest::SchedulerTest() { |
| 69 | const ::testing::TestInfo* const test_info = |
| 70 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 71 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 72 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 73 | std::vector<scheduler::RefreshRateConfigs::InputConfig> configs{ |
| 74 | {{HwcConfigIndexType(0), HwcConfigGroupType(0), 16666667}}}; |
| 75 | mRefreshRateConfigs = std::make_unique< |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 76 | scheduler::RefreshRateConfigs>(configs, /*currentConfig=*/HwcConfigIndexType(0)); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 77 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 78 | mScheduler = std::make_unique<TestableScheduler>(*mRefreshRateConfigs, false); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 79 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 80 | auto eventThread = std::make_unique<mock::EventThread>(); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 81 | mEventThread = eventThread.get(); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 82 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); |
| 83 | |
| 84 | mEventThreadConnection = new MockEventThreadConnection(mEventThread); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 85 | |
| 86 | // createConnection call to scheduler makes a createEventConnection call to EventThread. Make |
| 87 | // sure that call gets executed and returns an EventThread::Connection object. |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 88 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 89 | .WillRepeatedly(Return(mEventThreadConnection)); |
| 90 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 91 | mConnectionHandle = mScheduler->createConnection(std::move(eventThread)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(mConnectionHandle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | SchedulerTest::~SchedulerTest() { |
| 96 | const ::testing::TestInfo* const test_info = |
| 97 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 98 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 99 | } |
| 100 | |
| 101 | namespace { |
| 102 | /* ------------------------------------------------------------------------ |
| 103 | * Test cases |
| 104 | */ |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 105 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 106 | TEST_F(SchedulerTest, invalidConnectionHandle) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 107 | Scheduler::ConnectionHandle handle; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 108 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 109 | sp<IDisplayEventConnection> connection; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 110 | ASSERT_NO_FATAL_FAILURE( |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 111 | connection = mScheduler->createDisplayEventConnection(handle, |
| 112 | ISurfaceComposer:: |
| 113 | eConfigChangedSuppress)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 114 | EXPECT_FALSE(connection); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 115 | EXPECT_FALSE(mScheduler->getEventConnection(handle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 116 | |
| 117 | // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads. |
| 118 | EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 119 | ASSERT_NO_FATAL_FAILURE(mScheduler->onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 120 | |
| 121 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 122 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(handle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 123 | |
| 124 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 125 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(handle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 126 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 127 | std::string output; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 128 | EXPECT_CALL(*mEventThread, dump(_)).Times(0); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 129 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(handle, output)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 130 | EXPECT_TRUE(output.empty()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 131 | |
| 132 | EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 133 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(handle, 10)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | TEST_F(SchedulerTest, validConnectionHandle) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 137 | sp<IDisplayEventConnection> connection; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 138 | ASSERT_NO_FATAL_FAILURE( |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 139 | connection = mScheduler->createDisplayEventConnection(mConnectionHandle, |
| 140 | ISurfaceComposer:: |
| 141 | eConfigChangedSuppress)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 142 | ASSERT_EQ(mEventThreadConnection, connection); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 143 | EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 144 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 145 | EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1); |
| 146 | ASSERT_NO_FATAL_FAILURE( |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 147 | mScheduler->onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 148 | |
| 149 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 150 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 151 | |
| 152 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 153 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 154 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 155 | std::string output("dump"); |
| 156 | EXPECT_CALL(*mEventThread, dump(output)).Times(1); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 157 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(mConnectionHandle, output)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 158 | EXPECT_FALSE(output.empty()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 159 | |
| 160 | EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 161 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(mConnectionHandle, 10)); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame^] | 162 | |
| 163 | static constexpr size_t kEventConnections = 5; |
| 164 | ON_CALL(*mEventThread, getEventThreadConnectionCount()) |
| 165 | .WillByDefault(Return(kEventConnections)); |
| 166 | EXPECT_EQ(kEventConnections, mScheduler->getEventThreadConnectionCount(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 167 | } |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 168 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 169 | } // namespace |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 170 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 171 | |
| 172 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 173 | #pragma clang diagnostic pop // ignored "-Wconversion" |