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