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