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> |
| 6 | |
| 7 | #include <log/log.h> |
| 8 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 9 | #include <mutex> |
| 10 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 11 | #include "Scheduler/EventControlThread.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 12 | #include "Scheduler/EventThread.h" |
| 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 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 37 | scheduler::RefreshRateConfigs mRefreshRateConfigs; |
| 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 */, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 51 | nsecs_t /* phaseOffsetNs */, |
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 | |
| 76 | std::unique_ptr<mock::EventThread> eventThread = std::make_unique<mock::EventThread>(); |
| 77 | mEventThread = eventThread.get(); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 78 | mScheduler = std::make_unique<MockScheduler>(mRefreshRateConfigs, std::move(eventThread)); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 79 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); |
| 80 | |
| 81 | mEventThreadConnection = new MockEventThreadConnection(mEventThread); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 82 | |
| 83 | // createConnection call to scheduler makes a createEventConnection call to EventThread. Make |
| 84 | // sure that call gets executed and returns an EventThread::Connection object. |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 85 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 86 | .WillRepeatedly(Return(mEventThreadConnection)); |
| 87 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 88 | mConnectionHandle = mScheduler->createConnection("appConnection", 16, ResyncCallback(), |
| 89 | impl::EventThread::InterceptVSyncsCallback()); |
| 90 | EXPECT_TRUE(mConnectionHandle != nullptr); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | SchedulerTest::~SchedulerTest() { |
| 94 | const ::testing::TestInfo* const test_info = |
| 95 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 96 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 97 | } |
| 98 | |
| 99 | namespace { |
| 100 | /* ------------------------------------------------------------------------ |
| 101 | * Test cases |
| 102 | */ |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 103 | |
| 104 | TEST_F(SchedulerTest, testNullPtr) { |
| 105 | // Passing a null pointer for ConnectionHandle is a valid argument. The code doesn't throw any |
| 106 | // exceptions, just gracefully continues. |
| 107 | sp<IDisplayEventConnection> returnedValue; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 108 | ASSERT_NO_FATAL_FAILURE( |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 109 | returnedValue = |
| 110 | mScheduler->createDisplayEventConnection(nullptr, ResyncCallback(), |
| 111 | ISurfaceComposer:: |
| 112 | eConfigChangedSuppress)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 113 | EXPECT_TRUE(returnedValue == nullptr); |
| 114 | EXPECT_TRUE(mScheduler->getEventThread(nullptr) == nullptr); |
| 115 | EXPECT_TRUE(mScheduler->getEventConnection(nullptr) == nullptr); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 116 | ASSERT_NO_FATAL_FAILURE(mScheduler->hotplugReceived(nullptr, PHYSICAL_DISPLAY_ID, false)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 117 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(nullptr)); |
| 118 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(nullptr)); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 119 | std::string testString; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 120 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(nullptr, testString)); |
| 121 | EXPECT_TRUE(testString == ""); |
| 122 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(nullptr, 10)); |
| 123 | } |
| 124 | |
| 125 | TEST_F(SchedulerTest, invalidConnectionHandle) { |
| 126 | // Passing an invalid ConnectionHandle is a valid argument. The code doesn't throw any |
| 127 | // exceptions, just gracefully continues. |
| 128 | sp<Scheduler::ConnectionHandle> connectionHandle = new Scheduler::ConnectionHandle(20); |
| 129 | |
| 130 | sp<IDisplayEventConnection> returnedValue; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 131 | ASSERT_NO_FATAL_FAILURE( |
| 132 | returnedValue = |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 133 | mScheduler->createDisplayEventConnection(connectionHandle, ResyncCallback(), |
| 134 | ISurfaceComposer:: |
| 135 | eConfigChangedSuppress)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 136 | EXPECT_TRUE(returnedValue == nullptr); |
| 137 | EXPECT_TRUE(mScheduler->getEventThread(connectionHandle) == nullptr); |
| 138 | EXPECT_TRUE(mScheduler->getEventConnection(connectionHandle) == nullptr); |
| 139 | |
| 140 | // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads. |
| 141 | EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 142 | ASSERT_NO_FATAL_FAILURE( |
| 143 | mScheduler->hotplugReceived(connectionHandle, PHYSICAL_DISPLAY_ID, false)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 144 | |
| 145 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0); |
| 146 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(connectionHandle)); |
| 147 | |
| 148 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0); |
| 149 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(connectionHandle)); |
| 150 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 151 | std::string testString; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 152 | EXPECT_CALL(*mEventThread, dump(_)).Times(0); |
| 153 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(connectionHandle, testString)); |
| 154 | EXPECT_TRUE(testString == ""); |
| 155 | |
| 156 | EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0); |
| 157 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(connectionHandle, 10)); |
| 158 | } |
| 159 | |
| 160 | TEST_F(SchedulerTest, validConnectionHandle) { |
| 161 | sp<IDisplayEventConnection> returnedValue; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 162 | ASSERT_NO_FATAL_FAILURE( |
| 163 | returnedValue = |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame^] | 164 | mScheduler->createDisplayEventConnection(mConnectionHandle, ResyncCallback(), |
| 165 | ISurfaceComposer:: |
| 166 | eConfigChangedSuppress)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 167 | EXPECT_TRUE(returnedValue != nullptr); |
| 168 | ASSERT_EQ(returnedValue, mEventThreadConnection); |
| 169 | |
| 170 | EXPECT_TRUE(mScheduler->getEventThread(mConnectionHandle) != nullptr); |
| 171 | EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle) != nullptr); |
| 172 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 173 | EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1); |
| 174 | ASSERT_NO_FATAL_FAILURE( |
| 175 | mScheduler->hotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 176 | |
| 177 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1); |
| 178 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(mConnectionHandle)); |
| 179 | |
| 180 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1); |
| 181 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(mConnectionHandle)); |
| 182 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 183 | std::string testString("dump"); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 184 | EXPECT_CALL(*mEventThread, dump(testString)).Times(1); |
| 185 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(mConnectionHandle, testString)); |
| 186 | EXPECT_TRUE(testString != ""); |
| 187 | |
| 188 | EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1); |
| 189 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(mConnectionHandle, 10)); |
| 190 | } |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 191 | } // namespace |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 192 | } // namespace android |