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" |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 12 | #include "TestableScheduler.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 13 | #include "mock/MockEventThread.h" |
| 14 | |
| 15 | using testing::_; |
| 16 | using testing::Return; |
| 17 | |
| 18 | namespace android { |
| 19 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 20 | constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID = 999; |
| 21 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 22 | class SchedulerTest : public testing::Test { |
| 23 | protected: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 24 | class MockEventThreadConnection : public android::EventThreadConnection { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 25 | public: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 26 | explicit MockEventThreadConnection(EventThread* eventThread) |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 27 | : EventThreadConnection(eventThread, ResyncCallback(), |
| 28 | ISurfaceComposer::eConfigChangedSuppress) {} |
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 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 36 | SchedulerTest(); |
| 37 | ~SchedulerTest() override; |
| 38 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 39 | scheduler::RefreshRateConfigs mRefreshRateConfigs; |
| 40 | TestableScheduler mScheduler{mRefreshRateConfigs}; |
| 41 | |
| 42 | Scheduler::ConnectionHandle mConnectionHandle; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 43 | mock::EventThread* mEventThread; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 44 | sp<MockEventThreadConnection> mEventThreadConnection; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | SchedulerTest::SchedulerTest() { |
| 48 | const ::testing::TestInfo* const test_info = |
| 49 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 50 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 51 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 52 | auto eventThread = std::make_unique<mock::EventThread>(); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 53 | mEventThread = eventThread.get(); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 54 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); |
| 55 | |
| 56 | mEventThreadConnection = new MockEventThreadConnection(mEventThread); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 57 | |
| 58 | // createConnection call to scheduler makes a createEventConnection call to EventThread. Make |
| 59 | // sure that call gets executed and returns an EventThread::Connection object. |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 60 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 61 | .WillRepeatedly(Return(mEventThreadConnection)); |
| 62 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 63 | mConnectionHandle = mScheduler.createConnection(std::move(eventThread)); |
| 64 | EXPECT_TRUE(mConnectionHandle); |
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("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 71 | } |
| 72 | |
| 73 | namespace { |
| 74 | /* ------------------------------------------------------------------------ |
| 75 | * Test cases |
| 76 | */ |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 77 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 78 | TEST_F(SchedulerTest, invalidConnectionHandle) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 79 | Scheduler::ConnectionHandle handle; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 80 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 81 | sp<IDisplayEventConnection> connection; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 82 | ASSERT_NO_FATAL_FAILURE( |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 83 | connection = mScheduler.createDisplayEventConnection(handle, ResyncCallback(), |
| 84 | ISurfaceComposer:: |
| 85 | eConfigChangedSuppress)); |
| 86 | EXPECT_FALSE(connection); |
| 87 | EXPECT_FALSE(mScheduler.getEventThread(handle)); |
| 88 | EXPECT_FALSE(mScheduler.getEventConnection(handle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 89 | |
| 90 | // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads. |
| 91 | EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 92 | ASSERT_NO_FATAL_FAILURE(mScheduler.onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 93 | |
| 94 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 95 | ASSERT_NO_FATAL_FAILURE(mScheduler.onScreenAcquired(handle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 96 | |
| 97 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 98 | ASSERT_NO_FATAL_FAILURE(mScheduler.onScreenReleased(handle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 99 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 100 | std::string output; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 101 | EXPECT_CALL(*mEventThread, dump(_)).Times(0); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 102 | ASSERT_NO_FATAL_FAILURE(mScheduler.dump(handle, output)); |
| 103 | EXPECT_TRUE(output.empty()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 104 | |
| 105 | EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 106 | ASSERT_NO_FATAL_FAILURE(mScheduler.setPhaseOffset(handle, 10)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(SchedulerTest, validConnectionHandle) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 110 | sp<IDisplayEventConnection> connection; |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 111 | ASSERT_NO_FATAL_FAILURE( |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 112 | connection = |
| 113 | mScheduler.createDisplayEventConnection(mConnectionHandle, ResyncCallback(), |
| 114 | ISurfaceComposer:: |
| 115 | eConfigChangedSuppress)); |
| 116 | ASSERT_EQ(mEventThreadConnection, connection); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 117 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 118 | EXPECT_TRUE(mScheduler.getEventThread(mConnectionHandle)); |
| 119 | EXPECT_TRUE(mScheduler.getEventConnection(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 120 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 121 | EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1); |
| 122 | ASSERT_NO_FATAL_FAILURE( |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 123 | mScheduler.onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 124 | |
| 125 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 126 | ASSERT_NO_FATAL_FAILURE(mScheduler.onScreenAcquired(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 127 | |
| 128 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 129 | ASSERT_NO_FATAL_FAILURE(mScheduler.onScreenReleased(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 130 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 131 | std::string output("dump"); |
| 132 | EXPECT_CALL(*mEventThread, dump(output)).Times(1); |
| 133 | ASSERT_NO_FATAL_FAILURE(mScheduler.dump(mConnectionHandle, output)); |
| 134 | EXPECT_FALSE(output.empty()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 135 | |
| 136 | EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 137 | ASSERT_NO_FATAL_FAILURE(mScheduler.setPhaseOffset(mConnectionHandle, 10)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 138 | } |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame^] | 139 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 140 | } // namespace |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 141 | } // namespace android |