Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "LibSurfaceFlingerUnittests" |
| 19 | |
| 20 | #include <gmock/gmock.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | #include <log/log.h> |
| 24 | |
| 25 | #include <utils/Errors.h> |
| 26 | |
| 27 | #include "AsyncCallRecorder.h" |
Ana Krulec | fefcb58 | 2018-08-07 14:22:37 -0700 | [diff] [blame] | 28 | #include "Scheduler/EventThread.h" |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace std::chrono_literals; |
| 31 | using namespace std::placeholders; |
| 32 | |
| 33 | using testing::_; |
| 34 | using testing::Invoke; |
| 35 | |
| 36 | namespace android { |
| 37 | namespace { |
| 38 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 39 | constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = 111; |
| 40 | constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = 222; |
| 41 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 42 | class MockVSyncSource : public VSyncSource { |
| 43 | public: |
| 44 | MOCK_METHOD1(setVSyncEnabled, void(bool)); |
| 45 | MOCK_METHOD1(setCallback, void(VSyncSource::Callback*)); |
| 46 | MOCK_METHOD1(setPhaseOffset, void(nsecs_t)); |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame^] | 47 | MOCK_METHOD1(pauseVsyncCallback, void(bool)); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | class EventThreadTest : public testing::Test { |
| 53 | protected: |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 54 | class MockEventThreadConnection : public EventThreadConnection { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 55 | public: |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 56 | MockEventThreadConnection(android::impl::EventThread* eventThread, |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 57 | ResyncCallback&& resyncCallback, |
| 58 | ResetIdleTimerCallback&& resetIdleTimerCallback) |
| 59 | : EventThreadConnection(eventThread, std::move(resyncCallback), |
| 60 | std::move(resetIdleTimerCallback)) {} |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 61 | MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event)); |
| 62 | }; |
| 63 | |
| 64 | using ConnectionEventRecorder = |
| 65 | AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>; |
| 66 | |
| 67 | EventThreadTest(); |
| 68 | ~EventThreadTest() override; |
| 69 | |
| 70 | void createThread(); |
| 71 | sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder); |
| 72 | |
| 73 | void expectVSyncSetEnabledCallReceived(bool expectedState); |
| 74 | void expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset); |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame^] | 75 | void expectVSyncPauseVsyncCallbackCallReceived(bool expectedPause); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 76 | VSyncSource::Callback* expectVSyncSetCallbackCallReceived(); |
| 77 | void expectInterceptCallReceived(nsecs_t expectedTimestamp); |
| 78 | void expectVsyncEventReceivedByConnection(const char* name, |
| 79 | ConnectionEventRecorder& connectionEventRecorder, |
| 80 | nsecs_t expectedTimestamp, unsigned expectedCount); |
| 81 | void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 82 | void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 83 | bool expectedConnected); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 84 | |
| 85 | AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder; |
| 86 | AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder; |
| 87 | AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder; |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame^] | 88 | AsyncCallRecorder<void (*)(bool)> mVSyncPauseVsyncCallbackCallRecorder; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 89 | AsyncCallRecorder<void (*)()> mResyncCallRecorder; |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 90 | AsyncCallRecorder<void (*)()> mResetIdleTimerCallRecorder; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 91 | AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder; |
| 92 | ConnectionEventRecorder mConnectionEventCallRecorder{0}; |
| 93 | |
| 94 | MockVSyncSource mVSyncSource; |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 95 | VSyncSource::Callback* mCallback = nullptr; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 96 | std::unique_ptr<android::impl::EventThread> mThread; |
| 97 | sp<MockEventThreadConnection> mConnection; |
| 98 | }; |
| 99 | |
| 100 | EventThreadTest::EventThreadTest() { |
| 101 | const ::testing::TestInfo* const test_info = |
| 102 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 103 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 104 | |
| 105 | EXPECT_CALL(mVSyncSource, setVSyncEnabled(_)) |
| 106 | .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable())); |
| 107 | |
| 108 | EXPECT_CALL(mVSyncSource, setCallback(_)) |
| 109 | .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable())); |
| 110 | |
| 111 | EXPECT_CALL(mVSyncSource, setPhaseOffset(_)) |
| 112 | .WillRepeatedly(Invoke(mVSyncSetPhaseOffsetCallRecorder.getInvocable())); |
| 113 | |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame^] | 114 | EXPECT_CALL(mVSyncSource, pauseVsyncCallback(_)) |
| 115 | .WillRepeatedly(Invoke(mVSyncPauseVsyncCallbackCallRecorder.getInvocable())); |
| 116 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 117 | createThread(); |
| 118 | mConnection = createConnection(mConnectionEventCallRecorder); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 119 | |
| 120 | // A display must be connected for VSYNC events to be delivered. |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 121 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 122 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | EventThreadTest::~EventThreadTest() { |
| 126 | const ::testing::TestInfo* const test_info = |
| 127 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 128 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 129 | |
| 130 | // EventThread should unregister itself as VSyncSource callback. |
Lloyd Pique | 0655b8e | 2019-01-31 18:20:27 -0800 | [diff] [blame] | 131 | EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void EventThreadTest::createThread() { |
| 135 | mThread = |
| 136 | std::make_unique<android::impl::EventThread>(&mVSyncSource, |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 137 | mInterceptVSyncCallRecorder.getInvocable(), |
| 138 | "unit-test-event-thread"); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 139 | |
| 140 | // EventThread should register itself as VSyncSource callback. |
| 141 | mCallback = expectVSyncSetCallbackCallReceived(); |
| 142 | ASSERT_TRUE(mCallback); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection( |
| 146 | ConnectionEventRecorder& recorder) { |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 147 | sp<MockEventThreadConnection> connection = |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 148 | new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(), |
| 149 | mResetIdleTimerCallRecorder.getInvocable()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 150 | EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable())); |
| 151 | return connection; |
| 152 | } |
| 153 | |
| 154 | void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) { |
| 155 | auto args = mVSyncSetEnabledCallRecorder.waitForCall(); |
| 156 | ASSERT_TRUE(args.has_value()); |
| 157 | EXPECT_EQ(expectedState, std::get<0>(args.value())); |
| 158 | } |
| 159 | |
| 160 | void EventThreadTest::expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset) { |
| 161 | auto args = mVSyncSetPhaseOffsetCallRecorder.waitForCall(); |
| 162 | ASSERT_TRUE(args.has_value()); |
| 163 | EXPECT_EQ(expectedPhaseOffset, std::get<0>(args.value())); |
| 164 | } |
| 165 | |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame^] | 166 | void EventThreadTest::expectVSyncPauseVsyncCallbackCallReceived(bool expectedPause) { |
| 167 | auto args = mVSyncPauseVsyncCallbackCallRecorder.waitForCall(); |
| 168 | ASSERT_TRUE(args.has_value()); |
| 169 | EXPECT_EQ(expectedPause, std::get<0>(args.value())); |
| 170 | } |
| 171 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 172 | VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() { |
| 173 | auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall(); |
| 174 | return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr; |
| 175 | } |
| 176 | |
| 177 | void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) { |
| 178 | auto args = mInterceptVSyncCallRecorder.waitForCall(); |
| 179 | ASSERT_TRUE(args.has_value()); |
| 180 | EXPECT_EQ(expectedTimestamp, std::get<0>(args.value())); |
| 181 | } |
| 182 | |
| 183 | void EventThreadTest::expectVsyncEventReceivedByConnection( |
| 184 | const char* name, ConnectionEventRecorder& connectionEventRecorder, |
| 185 | nsecs_t expectedTimestamp, unsigned expectedCount) { |
| 186 | auto args = connectionEventRecorder.waitForCall(); |
| 187 | ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp " |
| 188 | << expectedTimestamp; |
| 189 | const auto& event = std::get<0>(args.value()); |
| 190 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type) |
| 191 | << name << " did not get the correct event for timestamp " << expectedTimestamp; |
| 192 | EXPECT_EQ(expectedTimestamp, event.header.timestamp) |
| 193 | << name << " did not get the expected timestamp for timestamp " << expectedTimestamp; |
| 194 | EXPECT_EQ(expectedCount, event.vsync.count) |
| 195 | << name << " did not get the expected count for timestamp " << expectedTimestamp; |
| 196 | } |
| 197 | |
| 198 | void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, |
| 199 | unsigned expectedCount) { |
| 200 | expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder", |
| 201 | mConnectionEventCallRecorder, expectedTimestamp, |
| 202 | expectedCount); |
| 203 | } |
| 204 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 205 | void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 206 | bool expectedConnected) { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 207 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 208 | ASSERT_TRUE(args.has_value()); |
| 209 | const auto& event = std::get<0>(args.value()); |
| 210 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 211 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 212 | EXPECT_EQ(expectedConnected, event.hotplug.connected); |
| 213 | } |
| 214 | |
| 215 | namespace { |
| 216 | |
| 217 | /* ------------------------------------------------------------------------ |
| 218 | * Test cases |
| 219 | */ |
| 220 | |
| 221 | TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) { |
| 222 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 223 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
| 224 | EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value()); |
| 225 | EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value()); |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 226 | EXPECT_FALSE(mResetIdleTimerCallRecorder.waitForCall(0us).has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 227 | EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value()); |
| 228 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value()); |
| 229 | } |
| 230 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 231 | TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 232 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 233 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 234 | |
| 235 | // Signal that we want the next vsync event to be posted to the connection. |
| 236 | mThread->requestNextVsync(mConnection, false); |
| 237 | |
| 238 | // EventThread should not enable vsync callbacks. |
| 239 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 242 | TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) { |
| 243 | // Signal that we want the next vsync event to be posted to the connection |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 244 | mThread->requestNextVsync(mConnection, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 245 | |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 246 | // EventThread should immediately reset the idle timer and request a resync. |
| 247 | EXPECT_TRUE(mResetIdleTimerCallRecorder.waitForCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 248 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 249 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 250 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 251 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 252 | |
| 253 | // Use the received callback to signal a first vsync event. |
| 254 | // The interceptor should receive the event, as well as the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 255 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 256 | expectInterceptCallReceived(123); |
| 257 | expectVsyncEventReceivedByConnection(123, 1u); |
| 258 | |
| 259 | // Use the received callback to signal a second vsync event. |
| 260 | // The interceptor should receive the event, but the the connection should |
| 261 | // not as it was only interested in the first. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 262 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 263 | expectInterceptCallReceived(456); |
| 264 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 265 | |
| 266 | // EventThread should also detect that at this point that it does not need |
| 267 | // any more vsync events, and should disable their generation. |
| 268 | expectVSyncSetEnabledCallReceived(false); |
| 269 | } |
| 270 | |
| 271 | TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) { |
| 272 | // Create a first connection, register it, and request a vsync rate of zero. |
| 273 | ConnectionEventRecorder firstConnectionEventRecorder{0}; |
| 274 | sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder); |
| 275 | mThread->setVsyncRate(0, firstConnection); |
| 276 | |
| 277 | // By itself, this should not enable vsync events |
| 278 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 279 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
| 280 | |
| 281 | // However if there is another connection which wants events at a nonzero rate..... |
| 282 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 283 | sp<MockEventThreadConnection> secondConnection = |
| 284 | createConnection(secondConnectionEventRecorder); |
| 285 | mThread->setVsyncRate(1, secondConnection); |
| 286 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 287 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 288 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 289 | |
| 290 | // Send a vsync event. EventThread should then make a call to the |
| 291 | // interceptor, and the second connection. The first connection should not |
| 292 | // get the event. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 293 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 294 | expectInterceptCallReceived(123); |
| 295 | EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 296 | expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123, |
| 297 | 1u); |
| 298 | } |
| 299 | |
| 300 | TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) { |
| 301 | mThread->setVsyncRate(1, mConnection); |
| 302 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 303 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 304 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 305 | |
| 306 | // Send a vsync event. EventThread should then make a call to the |
| 307 | // interceptor, and the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 308 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 309 | expectInterceptCallReceived(123); |
| 310 | expectVsyncEventReceivedByConnection(123, 1u); |
| 311 | |
| 312 | // A second event should go to the same places. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 313 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 314 | expectInterceptCallReceived(456); |
| 315 | expectVsyncEventReceivedByConnection(456, 2u); |
| 316 | |
| 317 | // A third event should go to the same places. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 318 | mCallback->onVSyncEvent(789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 319 | expectInterceptCallReceived(789); |
| 320 | expectVsyncEventReceivedByConnection(789, 3u); |
| 321 | } |
| 322 | |
| 323 | TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) { |
| 324 | mThread->setVsyncRate(2, mConnection); |
| 325 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 326 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 327 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 328 | |
| 329 | // The first event will be seen by the interceptor, and not the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 330 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 331 | expectInterceptCallReceived(123); |
| 332 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 333 | |
| 334 | // The second event will be seen by the interceptor and the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 335 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 336 | expectInterceptCallReceived(456); |
| 337 | expectVsyncEventReceivedByConnection(456, 2u); |
| 338 | |
| 339 | // The third event will be seen by the interceptor, and not the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 340 | mCallback->onVSyncEvent(789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 341 | expectInterceptCallReceived(789); |
| 342 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 343 | |
| 344 | // The fourth event will be seen by the interceptor and the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 345 | mCallback->onVSyncEvent(101112); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 346 | expectInterceptCallReceived(101112); |
| 347 | expectVsyncEventReceivedByConnection(101112, 4u); |
| 348 | } |
| 349 | |
| 350 | TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) { |
| 351 | mThread->setVsyncRate(1, mConnection); |
| 352 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 353 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 354 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 355 | |
| 356 | // Destroy the only (strong) reference to the connection. |
| 357 | mConnection = nullptr; |
| 358 | |
| 359 | // The first event will be seen by the interceptor, and not the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 360 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 361 | expectInterceptCallReceived(123); |
| 362 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 363 | |
| 364 | // EventThread should disable vsync callbacks |
| 365 | expectVSyncSetEnabledCallReceived(false); |
| 366 | } |
| 367 | |
| 368 | TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) { |
| 369 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
| 370 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
| 371 | mThread->setVsyncRate(1, errorConnection); |
| 372 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 373 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 374 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 375 | |
| 376 | // The first event will be seen by the interceptor, and by the connection, |
| 377 | // which then returns an error. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 378 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 379 | expectInterceptCallReceived(123); |
| 380 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 381 | |
| 382 | // A subsequent event will be seen by the interceptor and not by the |
| 383 | // connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 384 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 385 | expectInterceptCallReceived(456); |
| 386 | EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 387 | |
| 388 | // EventThread should disable vsync callbacks with the second event |
| 389 | expectVSyncSetEnabledCallReceived(false); |
| 390 | } |
| 391 | |
| 392 | TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) { |
| 393 | ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK}; |
| 394 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
| 395 | mThread->setVsyncRate(1, errorConnection); |
| 396 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 397 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 398 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 399 | |
| 400 | // The first event will be seen by the interceptor, and by the connection, |
| 401 | // which then returns an non-fatal error. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 402 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 403 | expectInterceptCallReceived(123); |
| 404 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 405 | |
| 406 | // A subsequent event will be seen by the interceptor, and by the connection, |
| 407 | // which still then returns an non-fatal error. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 408 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 409 | expectInterceptCallReceived(456); |
| 410 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u); |
| 411 | |
| 412 | // EventThread will not disable vsync callbacks as the errors are non-fatal. |
| 413 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 414 | } |
| 415 | |
| 416 | TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) { |
| 417 | mThread->setPhaseOffset(321); |
| 418 | expectVSyncSetPhaseOffsetCallReceived(321); |
| 419 | } |
| 420 | |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame^] | 421 | TEST_F(EventThreadTest, pauseVsyncCallbackForwardsToVSyncSource) { |
| 422 | mThread->pauseVsyncCallback(true); |
| 423 | expectVSyncPauseVsyncCallbackCallReceived(true); |
| 424 | } |
| 425 | |
| 426 | TEST_F(EventThreadTest, resumeVsyncCallbackForwardsToVSyncSource) { |
| 427 | mThread->pauseVsyncCallback(false); |
| 428 | expectVSyncPauseVsyncCallbackCallReceived(false); |
| 429 | } |
| 430 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 431 | TEST_F(EventThreadTest, postHotplugInternalDisconnect) { |
| 432 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 433 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 436 | TEST_F(EventThreadTest, postHotplugInternalConnect) { |
| 437 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 438 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | TEST_F(EventThreadTest, postHotplugExternalDisconnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 442 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false); |
| 443 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | TEST_F(EventThreadTest, postHotplugExternalConnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 447 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true); |
| 448 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | } // namespace |
| 452 | } // namespace android |