Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 1 | #undef LOG_TAG |
| 2 | #define LOG_TAG "SchedulerUnittests" |
| 3 | |
| 4 | #include <gmock/gmock.h> |
| 5 | #include <gtest/gtest.h> |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 6 | #include <log/log.h> |
| 7 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 8 | #include <mutex> |
| 9 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 10 | #include "Scheduler/EventControlThread.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 11 | #include "Scheduler/EventThread.h" |
Steven Thomas | e9eb183 | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 12 | #include "Scheduler/RefreshRateConfigs.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 13 | #include "Scheduler/Scheduler.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 14 | #include "mock/MockEventThread.h" |
| 15 | |
| 16 | using testing::_; |
| 17 | using testing::Return; |
| 18 | |
| 19 | namespace android { |
| 20 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 21 | constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID = 999; |
| 22 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 23 | class SchedulerTest : public testing::Test { |
| 24 | protected: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 25 | class MockEventThreadConnection : public android::EventThreadConnection { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 26 | public: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 27 | explicit MockEventThreadConnection(EventThread* eventThread) |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 28 | : EventThreadConnection(eventThread, ResyncCallback(), |
| 29 | ISurfaceComposer::eConfigChangedSuppress) {} |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 30 | ~MockEventThreadConnection() = default; |
| 31 | |
| 32 | MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel)); |
| 33 | MOCK_METHOD1(setVsyncRate, status_t(uint32_t count)); |
| 34 | MOCK_METHOD0(requestNextVsync, void()); |
| 35 | }; |
| 36 | |
Steven Thomas | e9eb183 | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 37 | std::unique_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 38 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 39 | /** |
| 40 | * This mock Scheduler class uses implementation of mock::EventThread but keeps everything else |
| 41 | * the same. |
| 42 | */ |
| 43 | class MockScheduler : public android::Scheduler { |
| 44 | public: |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 45 | MockScheduler(const scheduler::RefreshRateConfigs& refreshRateConfigs, |
| 46 | std::unique_ptr<EventThread> eventThread) |
| 47 | : Scheduler([](bool) {}, refreshRateConfigs), mEventThread(std::move(eventThread)) {} |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 48 | |
| 49 | std::unique_ptr<EventThread> makeEventThread( |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 50 | const char* /* connectionName */, DispSync* /* dispSync */, |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 51 | nsecs_t /* phaseOffsetNs */, nsecs_t /* offsetThresholdForNextVsync */, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 52 | impl::EventThread::InterceptVSyncsCallback /* interceptCallback */) override { |
| 53 | return std::move(mEventThread); |
| 54 | } |
| 55 | |
| 56 | MockScheduler() = default; |
| 57 | ~MockScheduler() override = default; |
| 58 | |
| 59 | std::unique_ptr<EventThread> mEventThread; |
| 60 | }; |
| 61 | |
| 62 | SchedulerTest(); |
| 63 | ~SchedulerTest() override; |
| 64 | |
| 65 | sp<Scheduler::ConnectionHandle> mConnectionHandle; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 66 | mock::EventThread* mEventThread; |
| 67 | std::unique_ptr<MockScheduler> mScheduler; |
| 68 | sp<MockEventThreadConnection> mEventThreadConnection; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | SchedulerTest::SchedulerTest() { |
| 72 | const ::testing::TestInfo* const test_info = |
| 73 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 74 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 75 | |
Steven Thomas | e9eb183 | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 76 | std::vector<scheduler::RefreshRateConfigs::InputConfig> configs{{/*hwcId=*/0, 16666667}}; |
| 77 | mRefreshRateConfigs = |
| 78 | std::make_unique<scheduler::RefreshRateConfigs>(/*refreshRateSwitching=*/false, configs, |
| 79 | /*currentConfig=*/0); |
| 80 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 81 | std::unique_ptr<mock::EventThread> eventThread = std::make_unique<mock::EventThread>(); |
| 82 | mEventThread = eventThread.get(); |
Steven Thomas | e9eb183 | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 83 | mScheduler = std::make_unique<MockScheduler>(*mRefreshRateConfigs, std::move(eventThread)); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 84 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); |
| 85 | |
| 86 | mEventThreadConnection = new MockEventThreadConnection(mEventThread); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 87 | |
| 88 | // createConnection call to scheduler makes a createEventConnection call to EventThread. Make |
| 89 | // sure that call gets executed and returns an EventThread::Connection object. |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 90 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 91 | .WillRepeatedly(Return(mEventThreadConnection)); |
| 92 | |
Steven Thomas | e9eb183 | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 93 | mConnectionHandle = mScheduler->createConnection("appConnection", 16, 16, |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 94 | impl::EventThread::InterceptVSyncsCallback()); |
| 95 | EXPECT_TRUE(mConnectionHandle != nullptr); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | SchedulerTest::~SchedulerTest() { |
| 99 | const ::testing::TestInfo* const test_info = |
| 100 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 101 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 102 | } |
| 103 | |
| 104 | namespace { |
| 105 | /* ------------------------------------------------------------------------ |
| 106 | * Test cases |
| 107 | */ |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 108 | |
| 109 | TEST_F(SchedulerTest, testNullPtr) { |
| 110 | // Passing a null pointer for ConnectionHandle is a valid argument. The code doesn't throw any |
| 111 | // exceptions, just gracefully continues. |
| 112 | sp<IDisplayEventConnection> returnedValue; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 113 | ASSERT_NO_FATAL_FAILURE( |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 114 | returnedValue = |
Steven Thomas | e9eb183 | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 115 | mScheduler->createDisplayEventConnection(nullptr, |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 116 | ISurfaceComposer:: |
| 117 | eConfigChangedSuppress)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 118 | EXPECT_TRUE(returnedValue == nullptr); |
| 119 | EXPECT_TRUE(mScheduler->getEventThread(nullptr) == nullptr); |
| 120 | EXPECT_TRUE(mScheduler->getEventConnection(nullptr) == nullptr); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 121 | ASSERT_NO_FATAL_FAILURE(mScheduler->hotplugReceived(nullptr, PHYSICAL_DISPLAY_ID, false)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 122 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(nullptr)); |
| 123 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(nullptr)); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 124 | std::string testString; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 125 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(nullptr, testString)); |
| 126 | EXPECT_TRUE(testString == ""); |
| 127 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(nullptr, 10)); |
| 128 | } |
| 129 | |
| 130 | TEST_F(SchedulerTest, invalidConnectionHandle) { |
| 131 | // Passing an invalid ConnectionHandle is a valid argument. The code doesn't throw any |
| 132 | // exceptions, just gracefully continues. |
| 133 | sp<Scheduler::ConnectionHandle> connectionHandle = new Scheduler::ConnectionHandle(20); |
| 134 | |
| 135 | sp<IDisplayEventConnection> returnedValue; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 136 | ASSERT_NO_FATAL_FAILURE( |
| 137 | returnedValue = |
Steven Thomas | e9eb183 | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 138 | mScheduler->createDisplayEventConnection(connectionHandle, |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 139 | ISurfaceComposer:: |
| 140 | eConfigChangedSuppress)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 141 | EXPECT_TRUE(returnedValue == nullptr); |
| 142 | EXPECT_TRUE(mScheduler->getEventThread(connectionHandle) == nullptr); |
| 143 | EXPECT_TRUE(mScheduler->getEventConnection(connectionHandle) == nullptr); |
| 144 | |
| 145 | // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads. |
| 146 | EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 147 | ASSERT_NO_FATAL_FAILURE( |
| 148 | mScheduler->hotplugReceived(connectionHandle, PHYSICAL_DISPLAY_ID, false)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 149 | |
| 150 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0); |
| 151 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(connectionHandle)); |
| 152 | |
| 153 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0); |
| 154 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(connectionHandle)); |
| 155 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 156 | std::string testString; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 157 | EXPECT_CALL(*mEventThread, dump(_)).Times(0); |
| 158 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(connectionHandle, testString)); |
| 159 | EXPECT_TRUE(testString == ""); |
| 160 | |
| 161 | EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0); |
| 162 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(connectionHandle, 10)); |
| 163 | } |
| 164 | |
| 165 | TEST_F(SchedulerTest, validConnectionHandle) { |
| 166 | sp<IDisplayEventConnection> returnedValue; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 167 | ASSERT_NO_FATAL_FAILURE( |
| 168 | returnedValue = |
Steven Thomas | e9eb183 | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 169 | mScheduler->createDisplayEventConnection(mConnectionHandle, |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 170 | ISurfaceComposer:: |
| 171 | eConfigChangedSuppress)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 172 | EXPECT_TRUE(returnedValue != nullptr); |
| 173 | ASSERT_EQ(returnedValue, mEventThreadConnection); |
| 174 | |
| 175 | EXPECT_TRUE(mScheduler->getEventThread(mConnectionHandle) != nullptr); |
| 176 | EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle) != nullptr); |
| 177 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 178 | EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1); |
| 179 | ASSERT_NO_FATAL_FAILURE( |
| 180 | mScheduler->hotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 181 | |
| 182 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1); |
| 183 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(mConnectionHandle)); |
| 184 | |
| 185 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1); |
| 186 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(mConnectionHandle)); |
| 187 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 188 | std::string testString("dump"); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 189 | EXPECT_CALL(*mEventThread, dump(testString)).Times(1); |
| 190 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(mConnectionHandle, testString)); |
| 191 | EXPECT_TRUE(testString != ""); |
| 192 | |
| 193 | EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1); |
| 194 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(mConnectionHandle, 10)); |
| 195 | } |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 196 | } // namespace |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 197 | } // namespace android |