blob: e499ff58f74eaa41ffc0de0c858b10d7236d2795 [file] [log] [blame]
Lloyd Pique24b0a482018-03-09 18:52:26 -08001/*
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 Krulecfefcb582018-08-07 14:22:37 -070028#include "Scheduler/EventThread.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080029
30using namespace std::chrono_literals;
31using namespace std::placeholders;
32
33using testing::_;
34using testing::Invoke;
35
36namespace android {
37namespace {
38
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080039constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = 111;
40constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = 222;
41
Lloyd Pique24b0a482018-03-09 18:52:26 -080042class MockVSyncSource : public VSyncSource {
43public:
44 MOCK_METHOD1(setVSyncEnabled, void(bool));
45 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
46 MOCK_METHOD1(setPhaseOffset, void(nsecs_t));
Ady Abrahamb838aed2019-02-12 15:30:16 -080047 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Lloyd Pique24b0a482018-03-09 18:52:26 -080048};
49
50} // namespace
51
52class EventThreadTest : public testing::Test {
53protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080054 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080055 public:
Dominik Laskowskif654d572018-12-20 11:03:06 -080056 MockEventThreadConnection(android::impl::EventThread* eventThread,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080057 ResyncCallback&& resyncCallback,
58 ResetIdleTimerCallback&& resetIdleTimerCallback)
59 : EventThreadConnection(eventThread, std::move(resyncCallback),
60 std::move(resetIdleTimerCallback)) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080061 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 Abrahamb838aed2019-02-12 15:30:16 -080075 void expectVSyncPauseVsyncCallbackCallReceived(bool expectedPause);
Lloyd Pique24b0a482018-03-09 18:52:26 -080076 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 Laskowskidcb38bb2019-01-25 02:35:50 -080082 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070083 bool expectedConnected);
Lloyd Pique24b0a482018-03-09 18:52:26 -080084
85 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
86 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
87 AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder;
Ady Abrahamb838aed2019-02-12 15:30:16 -080088 AsyncCallRecorder<void (*)(bool)> mVSyncPauseVsyncCallbackCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -080089 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080090 AsyncCallRecorder<void (*)()> mResetIdleTimerCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -080091 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
92 ConnectionEventRecorder mConnectionEventCallRecorder{0};
93
94 MockVSyncSource mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -080095 VSyncSource::Callback* mCallback = nullptr;
Lloyd Pique24b0a482018-03-09 18:52:26 -080096 std::unique_ptr<android::impl::EventThread> mThread;
97 sp<MockEventThreadConnection> mConnection;
98};
99
100EventThreadTest::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 Abrahamb838aed2019-02-12 15:30:16 -0800114 EXPECT_CALL(mVSyncSource, pauseVsyncCallback(_))
115 .WillRepeatedly(Invoke(mVSyncPauseVsyncCallbackCallRecorder.getInvocable()));
116
Lloyd Pique24b0a482018-03-09 18:52:26 -0800117 createThread();
118 mConnection = createConnection(mConnectionEventCallRecorder);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800119
120 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800121 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
122 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800123}
124
125EventThreadTest::~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 Laskowski029cc122019-01-23 19:52:06 -0800129
130 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800131 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800132}
133
134void EventThreadTest::createThread() {
135 mThread =
136 std::make_unique<android::impl::EventThread>(&mVSyncSource,
Lloyd Pique24b0a482018-03-09 18:52:26 -0800137 mInterceptVSyncCallRecorder.getInvocable(),
138 "unit-test-event-thread");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800139
140 // EventThread should register itself as VSyncSource callback.
141 mCallback = expectVSyncSetCallbackCallReceived();
142 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800143}
144
145sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
146 ConnectionEventRecorder& recorder) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800147 sp<MockEventThreadConnection> connection =
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800148 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
149 mResetIdleTimerCallRecorder.getInvocable());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800150 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
151 return connection;
152}
153
154void 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
160void 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 Abrahamb838aed2019-02-12 15:30:16 -0800166void 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 Pique24b0a482018-03-09 18:52:26 -0800172VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
173 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
174 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
175}
176
177void 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
183void 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
198void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
199 unsigned expectedCount) {
200 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
201 mConnectionEventCallRecorder, expectedTimestamp,
202 expectedCount);
203}
204
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800205void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
206 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800207 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 Laskowskidcb38bb2019-01-25 02:35:50 -0800211 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800212 EXPECT_EQ(expectedConnected, event.hotplug.connected);
213}
214
215namespace {
216
217/* ------------------------------------------------------------------------
218 * Test cases
219 */
220
221TEST_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 Laskowskiccf37d72019-02-01 16:47:58 -0800226 EXPECT_FALSE(mResetIdleTimerCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800227 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
228 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
229}
230
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800231TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800232 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
233 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800234
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 Laskowski1eba0202019-01-24 09:14:40 -0800240}
241
Lloyd Pique24b0a482018-03-09 18:52:26 -0800242TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
243 // Signal that we want the next vsync event to be posted to the connection
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800244 mThread->requestNextVsync(mConnection, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800245
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800246 // EventThread should immediately reset the idle timer and request a resync.
247 EXPECT_TRUE(mResetIdleTimerCallRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800248 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
249
Dominik Laskowski029cc122019-01-23 19:52:06 -0800250 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800251 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800252
253 // Use the received callback to signal a first vsync event.
254 // The interceptor should receive the event, as well as the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800255 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800256 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 Laskowski029cc122019-01-23 19:52:06 -0800262 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800263 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
271TEST_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 Laskowski029cc122019-01-23 19:52:06 -0800287 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800288 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800289
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 Laskowski029cc122019-01-23 19:52:06 -0800293 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800294 expectInterceptCallReceived(123);
295 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
296 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
297 1u);
298}
299
300TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
301 mThread->setVsyncRate(1, mConnection);
302
Dominik Laskowski029cc122019-01-23 19:52:06 -0800303 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800304 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800305
306 // Send a vsync event. EventThread should then make a call to the
307 // interceptor, and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800308 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800309 expectInterceptCallReceived(123);
310 expectVsyncEventReceivedByConnection(123, 1u);
311
312 // A second event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800313 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800314 expectInterceptCallReceived(456);
315 expectVsyncEventReceivedByConnection(456, 2u);
316
317 // A third event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800318 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800319 expectInterceptCallReceived(789);
320 expectVsyncEventReceivedByConnection(789, 3u);
321}
322
323TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
324 mThread->setVsyncRate(2, mConnection);
325
Dominik Laskowski029cc122019-01-23 19:52:06 -0800326 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800327 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800328
329 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800330 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800331 expectInterceptCallReceived(123);
332 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
333
334 // The second event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800335 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800336 expectInterceptCallReceived(456);
337 expectVsyncEventReceivedByConnection(456, 2u);
338
339 // The third event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800340 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800341 expectInterceptCallReceived(789);
342 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
343
344 // The fourth event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800345 mCallback->onVSyncEvent(101112);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800346 expectInterceptCallReceived(101112);
347 expectVsyncEventReceivedByConnection(101112, 4u);
348}
349
350TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
351 mThread->setVsyncRate(1, mConnection);
352
Dominik Laskowski029cc122019-01-23 19:52:06 -0800353 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800354 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800355
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 Laskowski029cc122019-01-23 19:52:06 -0800360 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800361 expectInterceptCallReceived(123);
362 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
363
364 // EventThread should disable vsync callbacks
365 expectVSyncSetEnabledCallReceived(false);
366}
367
368TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
369 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
370 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
371 mThread->setVsyncRate(1, errorConnection);
372
Dominik Laskowski029cc122019-01-23 19:52:06 -0800373 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800374 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800375
376 // The first event will be seen by the interceptor, and by the connection,
377 // which then returns an error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800378 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800379 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 Laskowski029cc122019-01-23 19:52:06 -0800384 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800385 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
392TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
393 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
394 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
395 mThread->setVsyncRate(1, errorConnection);
396
Dominik Laskowski029cc122019-01-23 19:52:06 -0800397 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800398 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800399
400 // The first event will be seen by the interceptor, and by the connection,
401 // which then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800402 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800403 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 Laskowski029cc122019-01-23 19:52:06 -0800408 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800409 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
416TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
417 mThread->setPhaseOffset(321);
418 expectVSyncSetPhaseOffsetCallReceived(321);
419}
420
Ady Abrahamb838aed2019-02-12 15:30:16 -0800421TEST_F(EventThreadTest, pauseVsyncCallbackForwardsToVSyncSource) {
422 mThread->pauseVsyncCallback(true);
423 expectVSyncPauseVsyncCallbackCallReceived(true);
424}
425
426TEST_F(EventThreadTest, resumeVsyncCallbackForwardsToVSyncSource) {
427 mThread->pauseVsyncCallback(false);
428 expectVSyncPauseVsyncCallbackCallReceived(false);
429}
430
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800431TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
432 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
433 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800434}
435
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800436TEST_F(EventThreadTest, postHotplugInternalConnect) {
437 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
438 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800439}
440
441TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800442 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
443 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800444}
445
446TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800447 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
448 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800449}
450
451} // namespace
452} // namespace android