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