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); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame^] | 84 | void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 85 | int32_t expectedConfigId); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 86 | |
| 87 | AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder; |
| 88 | AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder; |
| 89 | AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder; |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame] | 90 | AsyncCallRecorder<void (*)(bool)> mVSyncPauseVsyncCallbackCallRecorder; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 91 | AsyncCallRecorder<void (*)()> mResyncCallRecorder; |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 92 | AsyncCallRecorder<void (*)()> mResetIdleTimerCallRecorder; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 93 | AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder; |
| 94 | ConnectionEventRecorder mConnectionEventCallRecorder{0}; |
| 95 | |
| 96 | MockVSyncSource mVSyncSource; |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 97 | VSyncSource::Callback* mCallback = nullptr; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 98 | std::unique_ptr<android::impl::EventThread> mThread; |
| 99 | sp<MockEventThreadConnection> mConnection; |
| 100 | }; |
| 101 | |
| 102 | EventThreadTest::EventThreadTest() { |
| 103 | const ::testing::TestInfo* const test_info = |
| 104 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 105 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 106 | |
| 107 | EXPECT_CALL(mVSyncSource, setVSyncEnabled(_)) |
| 108 | .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable())); |
| 109 | |
| 110 | EXPECT_CALL(mVSyncSource, setCallback(_)) |
| 111 | .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable())); |
| 112 | |
| 113 | EXPECT_CALL(mVSyncSource, setPhaseOffset(_)) |
| 114 | .WillRepeatedly(Invoke(mVSyncSetPhaseOffsetCallRecorder.getInvocable())); |
| 115 | |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame] | 116 | EXPECT_CALL(mVSyncSource, pauseVsyncCallback(_)) |
| 117 | .WillRepeatedly(Invoke(mVSyncPauseVsyncCallbackCallRecorder.getInvocable())); |
| 118 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 119 | createThread(); |
| 120 | mConnection = createConnection(mConnectionEventCallRecorder); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 121 | |
| 122 | // A display must be connected for VSYNC events to be delivered. |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 123 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 124 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | EventThreadTest::~EventThreadTest() { |
| 128 | const ::testing::TestInfo* const test_info = |
| 129 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 130 | 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] | 131 | |
| 132 | // EventThread should unregister itself as VSyncSource callback. |
Lloyd Pique | 0655b8e | 2019-01-31 18:20:27 -0800 | [diff] [blame] | 133 | EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void EventThreadTest::createThread() { |
| 137 | mThread = |
| 138 | std::make_unique<android::impl::EventThread>(&mVSyncSource, |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 139 | mInterceptVSyncCallRecorder.getInvocable(), |
| 140 | "unit-test-event-thread"); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 141 | |
| 142 | // EventThread should register itself as VSyncSource callback. |
| 143 | mCallback = expectVSyncSetCallbackCallReceived(); |
| 144 | ASSERT_TRUE(mCallback); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection( |
| 148 | ConnectionEventRecorder& recorder) { |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 149 | sp<MockEventThreadConnection> connection = |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 150 | new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(), |
| 151 | mResetIdleTimerCallRecorder.getInvocable()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 152 | EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable())); |
| 153 | return connection; |
| 154 | } |
| 155 | |
| 156 | void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) { |
| 157 | auto args = mVSyncSetEnabledCallRecorder.waitForCall(); |
| 158 | ASSERT_TRUE(args.has_value()); |
| 159 | EXPECT_EQ(expectedState, std::get<0>(args.value())); |
| 160 | } |
| 161 | |
| 162 | void EventThreadTest::expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset) { |
| 163 | auto args = mVSyncSetPhaseOffsetCallRecorder.waitForCall(); |
| 164 | ASSERT_TRUE(args.has_value()); |
| 165 | EXPECT_EQ(expectedPhaseOffset, std::get<0>(args.value())); |
| 166 | } |
| 167 | |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame] | 168 | void EventThreadTest::expectVSyncPauseVsyncCallbackCallReceived(bool expectedPause) { |
| 169 | auto args = mVSyncPauseVsyncCallbackCallRecorder.waitForCall(); |
| 170 | ASSERT_TRUE(args.has_value()); |
| 171 | EXPECT_EQ(expectedPause, std::get<0>(args.value())); |
| 172 | } |
| 173 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 174 | VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() { |
| 175 | auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall(); |
| 176 | return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr; |
| 177 | } |
| 178 | |
| 179 | void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) { |
| 180 | auto args = mInterceptVSyncCallRecorder.waitForCall(); |
| 181 | ASSERT_TRUE(args.has_value()); |
| 182 | EXPECT_EQ(expectedTimestamp, std::get<0>(args.value())); |
| 183 | } |
| 184 | |
| 185 | void EventThreadTest::expectVsyncEventReceivedByConnection( |
| 186 | const char* name, ConnectionEventRecorder& connectionEventRecorder, |
| 187 | nsecs_t expectedTimestamp, unsigned expectedCount) { |
| 188 | auto args = connectionEventRecorder.waitForCall(); |
| 189 | ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp " |
| 190 | << expectedTimestamp; |
| 191 | const auto& event = std::get<0>(args.value()); |
| 192 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type) |
| 193 | << name << " did not get the correct event for timestamp " << expectedTimestamp; |
| 194 | EXPECT_EQ(expectedTimestamp, event.header.timestamp) |
| 195 | << name << " did not get the expected timestamp for timestamp " << expectedTimestamp; |
| 196 | EXPECT_EQ(expectedCount, event.vsync.count) |
| 197 | << name << " did not get the expected count for timestamp " << expectedTimestamp; |
| 198 | } |
| 199 | |
| 200 | void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, |
| 201 | unsigned expectedCount) { |
| 202 | expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder", |
| 203 | mConnectionEventCallRecorder, expectedTimestamp, |
| 204 | expectedCount); |
| 205 | } |
| 206 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 207 | void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 208 | bool expectedConnected) { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 209 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 210 | ASSERT_TRUE(args.has_value()); |
| 211 | const auto& event = std::get<0>(args.value()); |
| 212 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 213 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 214 | EXPECT_EQ(expectedConnected, event.hotplug.connected); |
| 215 | } |
| 216 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame^] | 217 | void EventThreadTest::expectConfigChangedEventReceivedByConnection( |
| 218 | PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId) { |
| 219 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 220 | ASSERT_TRUE(args.has_value()); |
| 221 | const auto& event = std::get<0>(args.value()); |
| 222 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type); |
| 223 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 224 | EXPECT_EQ(expectedConfigId, event.config.configId); |
| 225 | } |
| 226 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 227 | namespace { |
| 228 | |
| 229 | /* ------------------------------------------------------------------------ |
| 230 | * Test cases |
| 231 | */ |
| 232 | |
| 233 | TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) { |
| 234 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 235 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
| 236 | EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value()); |
| 237 | EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value()); |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 238 | EXPECT_FALSE(mResetIdleTimerCallRecorder.waitForCall(0us).has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 239 | EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value()); |
| 240 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value()); |
| 241 | } |
| 242 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 243 | TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 244 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 245 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 246 | |
| 247 | // Signal that we want the next vsync event to be posted to the connection. |
| 248 | mThread->requestNextVsync(mConnection, false); |
| 249 | |
| 250 | // EventThread should not enable vsync callbacks. |
| 251 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 254 | TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) { |
| 255 | // 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] | 256 | mThread->requestNextVsync(mConnection, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 257 | |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 258 | // EventThread should immediately reset the idle timer and request a resync. |
| 259 | EXPECT_TRUE(mResetIdleTimerCallRecorder.waitForCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 260 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 261 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 262 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 263 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 264 | |
| 265 | // Use the received callback to signal a first vsync event. |
| 266 | // The interceptor should receive the event, as well as the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 267 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 268 | expectInterceptCallReceived(123); |
| 269 | expectVsyncEventReceivedByConnection(123, 1u); |
| 270 | |
| 271 | // Use the received callback to signal a second vsync event. |
| 272 | // The interceptor should receive the event, but the the connection should |
| 273 | // not as it was only interested in the first. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 274 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 275 | expectInterceptCallReceived(456); |
| 276 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 277 | |
| 278 | // EventThread should also detect that at this point that it does not need |
| 279 | // any more vsync events, and should disable their generation. |
| 280 | expectVSyncSetEnabledCallReceived(false); |
| 281 | } |
| 282 | |
| 283 | TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) { |
| 284 | // Create a first connection, register it, and request a vsync rate of zero. |
| 285 | ConnectionEventRecorder firstConnectionEventRecorder{0}; |
| 286 | sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder); |
| 287 | mThread->setVsyncRate(0, firstConnection); |
| 288 | |
| 289 | // By itself, this should not enable vsync events |
| 290 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 291 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
| 292 | |
| 293 | // However if there is another connection which wants events at a nonzero rate..... |
| 294 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 295 | sp<MockEventThreadConnection> secondConnection = |
| 296 | createConnection(secondConnectionEventRecorder); |
| 297 | mThread->setVsyncRate(1, secondConnection); |
| 298 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 299 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 300 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 301 | |
| 302 | // Send a vsync event. EventThread should then make a call to the |
| 303 | // interceptor, and the second connection. The first connection should not |
| 304 | // get the event. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 305 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 306 | expectInterceptCallReceived(123); |
| 307 | EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 308 | expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123, |
| 309 | 1u); |
| 310 | } |
| 311 | |
| 312 | TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) { |
| 313 | mThread->setVsyncRate(1, mConnection); |
| 314 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 315 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 316 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 317 | |
| 318 | // Send a vsync event. EventThread should then make a call to the |
| 319 | // interceptor, and the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 320 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 321 | expectInterceptCallReceived(123); |
| 322 | expectVsyncEventReceivedByConnection(123, 1u); |
| 323 | |
| 324 | // A second event should go to the same places. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 325 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 326 | expectInterceptCallReceived(456); |
| 327 | expectVsyncEventReceivedByConnection(456, 2u); |
| 328 | |
| 329 | // A third event should go to the same places. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 330 | mCallback->onVSyncEvent(789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 331 | expectInterceptCallReceived(789); |
| 332 | expectVsyncEventReceivedByConnection(789, 3u); |
| 333 | } |
| 334 | |
| 335 | TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) { |
| 336 | mThread->setVsyncRate(2, mConnection); |
| 337 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 338 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 339 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 340 | |
| 341 | // 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] | 342 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 343 | expectInterceptCallReceived(123); |
| 344 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 345 | |
| 346 | // The second event will be seen by the interceptor and the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 347 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 348 | expectInterceptCallReceived(456); |
| 349 | expectVsyncEventReceivedByConnection(456, 2u); |
| 350 | |
| 351 | // 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] | 352 | mCallback->onVSyncEvent(789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 353 | expectInterceptCallReceived(789); |
| 354 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 355 | |
| 356 | // The fourth event will be seen by the interceptor and the connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 357 | mCallback->onVSyncEvent(101112); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 358 | expectInterceptCallReceived(101112); |
| 359 | expectVsyncEventReceivedByConnection(101112, 4u); |
| 360 | } |
| 361 | |
| 362 | TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) { |
| 363 | mThread->setVsyncRate(1, mConnection); |
| 364 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 365 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 366 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 367 | |
| 368 | // Destroy the only (strong) reference to the connection. |
| 369 | mConnection = nullptr; |
| 370 | |
| 371 | // 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] | 372 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 373 | expectInterceptCallReceived(123); |
| 374 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 375 | |
| 376 | // EventThread should disable vsync callbacks |
| 377 | expectVSyncSetEnabledCallReceived(false); |
| 378 | } |
| 379 | |
| 380 | TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) { |
| 381 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
| 382 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
| 383 | mThread->setVsyncRate(1, errorConnection); |
| 384 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 385 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 386 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 387 | |
| 388 | // The first event will be seen by the interceptor, and by the connection, |
| 389 | // which then returns an error. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 390 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 391 | expectInterceptCallReceived(123); |
| 392 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 393 | |
| 394 | // A subsequent event will be seen by the interceptor and not by the |
| 395 | // connection. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 396 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 397 | expectInterceptCallReceived(456); |
| 398 | EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 399 | |
| 400 | // EventThread should disable vsync callbacks with the second event |
| 401 | expectVSyncSetEnabledCallReceived(false); |
| 402 | } |
| 403 | |
| 404 | TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) { |
| 405 | ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK}; |
| 406 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
| 407 | mThread->setVsyncRate(1, errorConnection); |
| 408 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 409 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 410 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 411 | |
| 412 | // The first event will be seen by the interceptor, and by the connection, |
| 413 | // which then returns an non-fatal error. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 414 | mCallback->onVSyncEvent(123); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 415 | expectInterceptCallReceived(123); |
| 416 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 417 | |
| 418 | // A subsequent event will be seen by the interceptor, and by the connection, |
| 419 | // which still then returns an non-fatal error. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 420 | mCallback->onVSyncEvent(456); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 421 | expectInterceptCallReceived(456); |
| 422 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u); |
| 423 | |
| 424 | // EventThread will not disable vsync callbacks as the errors are non-fatal. |
| 425 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 426 | } |
| 427 | |
| 428 | TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) { |
| 429 | mThread->setPhaseOffset(321); |
| 430 | expectVSyncSetPhaseOffsetCallReceived(321); |
| 431 | } |
| 432 | |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame] | 433 | TEST_F(EventThreadTest, pauseVsyncCallbackForwardsToVSyncSource) { |
| 434 | mThread->pauseVsyncCallback(true); |
| 435 | expectVSyncPauseVsyncCallbackCallReceived(true); |
| 436 | } |
| 437 | |
| 438 | TEST_F(EventThreadTest, resumeVsyncCallbackForwardsToVSyncSource) { |
| 439 | mThread->pauseVsyncCallback(false); |
| 440 | expectVSyncPauseVsyncCallbackCallReceived(false); |
| 441 | } |
| 442 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 443 | TEST_F(EventThreadTest, postHotplugInternalDisconnect) { |
| 444 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 445 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 448 | TEST_F(EventThreadTest, postHotplugInternalConnect) { |
| 449 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 450 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | TEST_F(EventThreadTest, postHotplugExternalDisconnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 454 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false); |
| 455 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | TEST_F(EventThreadTest, postHotplugExternalConnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 459 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true); |
| 460 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame^] | 463 | TEST_F(EventThreadTest, postConfigChangedPrimary) { |
| 464 | mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 7); |
| 465 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7); |
| 466 | } |
| 467 | |
| 468 | TEST_F(EventThreadTest, postConfigChangedExternal) { |
| 469 | mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, 5); |
| 470 | expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5); |
| 471 | } |
| 472 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 473 | } // namespace |
| 474 | } // namespace android |