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 | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 11 | #include "AsyncCallRecorder.h" |
| 12 | #include "Scheduler/DispSync.h" |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 13 | #include "Scheduler/EventControlThread.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 14 | #include "Scheduler/EventThread.h" |
| 15 | #include "Scheduler/Scheduler.h" |
| 16 | #include "mock/MockDispSync.h" |
| 17 | #include "mock/MockEventThread.h" |
| 18 | |
| 19 | using testing::_; |
| 20 | using testing::Return; |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | class SchedulerTest : public testing::Test { |
| 25 | protected: |
| 26 | class MockEventThreadConnection : public BnDisplayEventConnection { |
| 27 | public: |
| 28 | MockEventThreadConnection() = default; |
| 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( |
Ana Krulec | 1f02791 | 2018-09-10 21:36:25 +0000 | [diff] [blame] | 46 | const std::string& /* connectionName */, DispSync* /* dispSync */, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 47 | nsecs_t /* phaseOffsetNs */, |
| 48 | impl::EventThread::ResyncWithRateLimitCallback /* resyncCallback */, |
| 49 | impl::EventThread::InterceptVSyncsCallback /* interceptCallback */) override { |
| 50 | return std::move(mEventThread); |
| 51 | } |
| 52 | |
| 53 | MockScheduler() = default; |
| 54 | ~MockScheduler() override = default; |
| 55 | |
| 56 | std::unique_ptr<EventThread> mEventThread; |
| 57 | }; |
| 58 | |
| 59 | SchedulerTest(); |
| 60 | ~SchedulerTest() override; |
| 61 | |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame^] | 62 | int64_t calculateMedian(std::vector<int64_t>* v); |
| 63 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 64 | sp<Scheduler::ConnectionHandle> mConnectionHandle; |
| 65 | mock::DispSync* mPrimaryDispSync = new mock::DispSync(); |
| 66 | mock::EventThread* mEventThread; |
| 67 | std::unique_ptr<MockScheduler> mScheduler; |
| 68 | sp<MockEventThreadConnection> mEventThreadConnection; |
| 69 | |
| 70 | AsyncCallRecorder<void (*)()> mResyncCallRecorder; |
| 71 | AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder; |
| 72 | }; |
| 73 | |
| 74 | SchedulerTest::SchedulerTest() { |
| 75 | const ::testing::TestInfo* const test_info = |
| 76 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 77 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 78 | |
| 79 | std::unique_ptr<mock::EventThread> eventThread = std::make_unique<mock::EventThread>(); |
| 80 | mEventThread = eventThread.get(); |
| 81 | mScheduler = std::make_unique<MockScheduler>(std::move(eventThread)); |
| 82 | mEventThreadConnection = new MockEventThreadConnection(); |
| 83 | |
| 84 | // createConnection call to scheduler makes a createEventConnection call to EventThread. Make |
| 85 | // sure that call gets executed and returns an EventThread::Connection object. |
| 86 | EXPECT_CALL(*mEventThread, createEventConnection()) |
| 87 | .WillRepeatedly(Return(mEventThreadConnection)); |
| 88 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 89 | mConnectionHandle = |
| 90 | mScheduler->createConnection("appConnection", 16, mResyncCallRecorder.getInvocable(), |
| 91 | mInterceptVSyncCallRecorder.getInvocable()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | SchedulerTest::~SchedulerTest() { |
| 95 | const ::testing::TestInfo* const test_info = |
| 96 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 97 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 98 | } |
| 99 | |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame^] | 100 | int64_t SchedulerTest::calculateMedian(std::vector<int64_t>* v) { |
| 101 | return mScheduler->calculateMedian(v); |
| 102 | } |
| 103 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 104 | namespace { |
| 105 | /* ------------------------------------------------------------------------ |
| 106 | * Test cases |
| 107 | */ |
| 108 | TEST_F(SchedulerTest, canCreateAndDestroyTest) { |
| 109 | EXPECT_FALSE(mResyncCallRecorder.waitForCall().has_value()); |
| 110 | EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall().has_value()); |
| 111 | EXPECT_EQ(0, mConnectionHandle->id); |
| 112 | } |
| 113 | |
| 114 | TEST_F(SchedulerTest, testNullPtr) { |
| 115 | // Passing a null pointer for ConnectionHandle is a valid argument. The code doesn't throw any |
| 116 | // exceptions, just gracefully continues. |
| 117 | sp<IDisplayEventConnection> returnedValue; |
| 118 | ASSERT_NO_FATAL_FAILURE(returnedValue = mScheduler->createDisplayEventConnection(nullptr)); |
| 119 | EXPECT_TRUE(returnedValue == nullptr); |
| 120 | EXPECT_TRUE(mScheduler->getEventThread(nullptr) == nullptr); |
| 121 | EXPECT_TRUE(mScheduler->getEventConnection(nullptr) == nullptr); |
| 122 | ASSERT_NO_FATAL_FAILURE( |
| 123 | mScheduler->hotplugReceived(nullptr, EventThread::DisplayType::Primary, false)); |
| 124 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(nullptr)); |
| 125 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(nullptr)); |
| 126 | String8 testString; |
| 127 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(nullptr, testString)); |
| 128 | EXPECT_TRUE(testString == ""); |
| 129 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(nullptr, 10)); |
| 130 | } |
| 131 | |
| 132 | TEST_F(SchedulerTest, invalidConnectionHandle) { |
| 133 | // Passing an invalid ConnectionHandle is a valid argument. The code doesn't throw any |
| 134 | // exceptions, just gracefully continues. |
| 135 | sp<Scheduler::ConnectionHandle> connectionHandle = new Scheduler::ConnectionHandle(20); |
| 136 | |
| 137 | sp<IDisplayEventConnection> returnedValue; |
| 138 | ASSERT_NO_FATAL_FAILURE(returnedValue = |
| 139 | mScheduler->createDisplayEventConnection(connectionHandle)); |
| 140 | EXPECT_TRUE(returnedValue == nullptr); |
| 141 | EXPECT_TRUE(mScheduler->getEventThread(connectionHandle) == nullptr); |
| 142 | EXPECT_TRUE(mScheduler->getEventConnection(connectionHandle) == nullptr); |
| 143 | |
| 144 | // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads. |
| 145 | EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0); |
| 146 | ASSERT_NO_FATAL_FAILURE(mScheduler->hotplugReceived(connectionHandle, |
| 147 | EventThread::DisplayType::Primary, false)); |
| 148 | |
| 149 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0); |
| 150 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(connectionHandle)); |
| 151 | |
| 152 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0); |
| 153 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(connectionHandle)); |
| 154 | |
| 155 | String8 testString; |
| 156 | EXPECT_CALL(*mEventThread, dump(_)).Times(0); |
| 157 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(connectionHandle, testString)); |
| 158 | EXPECT_TRUE(testString == ""); |
| 159 | |
| 160 | EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0); |
| 161 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(connectionHandle, 10)); |
| 162 | } |
| 163 | |
| 164 | TEST_F(SchedulerTest, validConnectionHandle) { |
| 165 | sp<IDisplayEventConnection> returnedValue; |
| 166 | ASSERT_NO_FATAL_FAILURE(returnedValue = |
| 167 | mScheduler->createDisplayEventConnection(mConnectionHandle)); |
| 168 | EXPECT_TRUE(returnedValue != nullptr); |
| 169 | ASSERT_EQ(returnedValue, mEventThreadConnection); |
| 170 | |
| 171 | EXPECT_TRUE(mScheduler->getEventThread(mConnectionHandle) != nullptr); |
| 172 | EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle) != nullptr); |
| 173 | |
| 174 | EXPECT_CALL(*mEventThread, onHotplugReceived(EventThread::DisplayType::Primary, false)) |
| 175 | .Times(1); |
| 176 | ASSERT_NO_FATAL_FAILURE(mScheduler->hotplugReceived(mConnectionHandle, |
| 177 | EventThread::DisplayType::Primary, false)); |
| 178 | |
| 179 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1); |
| 180 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(mConnectionHandle)); |
| 181 | |
| 182 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1); |
| 183 | ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(mConnectionHandle)); |
| 184 | |
| 185 | String8 testString("dump"); |
| 186 | EXPECT_CALL(*mEventThread, dump(testString)).Times(1); |
| 187 | ASSERT_NO_FATAL_FAILURE(mScheduler->dump(mConnectionHandle, testString)); |
| 188 | EXPECT_TRUE(testString != ""); |
| 189 | |
| 190 | EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1); |
| 191 | ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(mConnectionHandle, 10)); |
| 192 | } |
| 193 | |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame^] | 194 | TEST_F(SchedulerTest, calculateMedian) { |
| 195 | std::vector<int64_t> testVector; |
| 196 | // Calling the function on empty vector returns 0. |
| 197 | EXPECT_EQ(0, calculateMedian(&testVector)); |
| 198 | |
| 199 | testVector.push_back(33); |
| 200 | EXPECT_EQ(33, calculateMedian(&testVector)); |
| 201 | testVector.push_back(33); |
| 202 | testVector.push_back(33); |
| 203 | EXPECT_EQ(33, calculateMedian(&testVector)); |
| 204 | testVector.push_back(42); |
| 205 | EXPECT_EQ(33, calculateMedian(&testVector)); |
| 206 | testVector.push_back(33); |
| 207 | EXPECT_EQ(33, calculateMedian(&testVector)); |
| 208 | testVector.push_back(42); |
| 209 | EXPECT_EQ(33, calculateMedian(&testVector)); |
| 210 | testVector.push_back(42); |
| 211 | EXPECT_EQ(33, calculateMedian(&testVector)); |
| 212 | testVector.push_back(42); |
| 213 | EXPECT_EQ(42, calculateMedian(&testVector)); |
| 214 | testVector.push_back(60); |
| 215 | EXPECT_EQ(42, calculateMedian(&testVector)); |
| 216 | testVector.push_back(60); |
| 217 | EXPECT_EQ(42, calculateMedian(&testVector)); |
| 218 | testVector.push_back(33); |
| 219 | EXPECT_EQ(42, calculateMedian(&testVector)); |
| 220 | testVector.push_back(33); |
| 221 | EXPECT_EQ(42, calculateMedian(&testVector)); |
| 222 | testVector.push_back(33); |
| 223 | EXPECT_EQ(33, calculateMedian(&testVector)); |
| 224 | } |
| 225 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 226 | } // namespace |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame^] | 227 | } // namespace android |