blob: 406ec81ce4ee2c56dd8a617da2f996ceb328c300 [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);
Ady Abraham447052e2019-02-13 16:07:27 -080084 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
85 int32_t expectedConfigId);
Lloyd Pique24b0a482018-03-09 18:52:26 -080086
87 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
88 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
89 AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder;
Ady Abrahamb838aed2019-02-12 15:30:16 -080090 AsyncCallRecorder<void (*)(bool)> mVSyncPauseVsyncCallbackCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -080091 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080092 AsyncCallRecorder<void (*)()> mResetIdleTimerCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -080093 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
94 ConnectionEventRecorder mConnectionEventCallRecorder{0};
95
96 MockVSyncSource mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -080097 VSyncSource::Callback* mCallback = nullptr;
Lloyd Pique24b0a482018-03-09 18:52:26 -080098 std::unique_ptr<android::impl::EventThread> mThread;
99 sp<MockEventThreadConnection> mConnection;
100};
101
102EventThreadTest::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 Abrahamb838aed2019-02-12 15:30:16 -0800116 EXPECT_CALL(mVSyncSource, pauseVsyncCallback(_))
117 .WillRepeatedly(Invoke(mVSyncPauseVsyncCallbackCallRecorder.getInvocable()));
118
Lloyd Pique24b0a482018-03-09 18:52:26 -0800119 createThread();
120 mConnection = createConnection(mConnectionEventCallRecorder);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800121
122 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800123 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
124 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800125}
126
127EventThreadTest::~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 Laskowski029cc122019-01-23 19:52:06 -0800131
132 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800133 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800134}
135
136void EventThreadTest::createThread() {
137 mThread =
138 std::make_unique<android::impl::EventThread>(&mVSyncSource,
Lloyd Pique24b0a482018-03-09 18:52:26 -0800139 mInterceptVSyncCallRecorder.getInvocable(),
140 "unit-test-event-thread");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800141
142 // EventThread should register itself as VSyncSource callback.
143 mCallback = expectVSyncSetCallbackCallReceived();
144 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800145}
146
147sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
148 ConnectionEventRecorder& recorder) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800149 sp<MockEventThreadConnection> connection =
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800150 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
151 mResetIdleTimerCallRecorder.getInvocable());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800152 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
153 return connection;
154}
155
156void 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
162void 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 Abrahamb838aed2019-02-12 15:30:16 -0800168void 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 Pique24b0a482018-03-09 18:52:26 -0800174VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
175 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
176 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
177}
178
179void 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
185void 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
200void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
201 unsigned expectedCount) {
202 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
203 mConnectionEventCallRecorder, expectedTimestamp,
204 expectedCount);
205}
206
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800207void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
208 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800209 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 Laskowskidcb38bb2019-01-25 02:35:50 -0800213 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800214 EXPECT_EQ(expectedConnected, event.hotplug.connected);
215}
216
Ady Abraham447052e2019-02-13 16:07:27 -0800217void 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 Pique24b0a482018-03-09 18:52:26 -0800227namespace {
228
229/* ------------------------------------------------------------------------
230 * Test cases
231 */
232
233TEST_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 Laskowskiccf37d72019-02-01 16:47:58 -0800238 EXPECT_FALSE(mResetIdleTimerCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800239 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
240 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
241}
242
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800243TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800244 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
245 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800246
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 Laskowski1eba0202019-01-24 09:14:40 -0800252}
253
Lloyd Pique24b0a482018-03-09 18:52:26 -0800254TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
255 // Signal that we want the next vsync event to be posted to the connection
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800256 mThread->requestNextVsync(mConnection, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800257
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800258 // EventThread should immediately reset the idle timer and request a resync.
259 EXPECT_TRUE(mResetIdleTimerCallRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800260 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
261
Dominik Laskowski029cc122019-01-23 19:52:06 -0800262 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800263 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800264
265 // Use the received callback to signal a first vsync event.
266 // The interceptor should receive the event, as well as the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800267 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800268 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 Laskowski029cc122019-01-23 19:52:06 -0800274 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800275 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
283TEST_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 Laskowski029cc122019-01-23 19:52:06 -0800299 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800300 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800301
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 Laskowski029cc122019-01-23 19:52:06 -0800305 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800306 expectInterceptCallReceived(123);
307 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
308 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
309 1u);
310}
311
312TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
313 mThread->setVsyncRate(1, mConnection);
314
Dominik Laskowski029cc122019-01-23 19:52:06 -0800315 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800316 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800317
318 // Send a vsync event. EventThread should then make a call to the
319 // interceptor, and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800320 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800321 expectInterceptCallReceived(123);
322 expectVsyncEventReceivedByConnection(123, 1u);
323
324 // A second event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800325 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800326 expectInterceptCallReceived(456);
327 expectVsyncEventReceivedByConnection(456, 2u);
328
329 // A third event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800330 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800331 expectInterceptCallReceived(789);
332 expectVsyncEventReceivedByConnection(789, 3u);
333}
334
335TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
336 mThread->setVsyncRate(2, mConnection);
337
Dominik Laskowski029cc122019-01-23 19:52:06 -0800338 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800339 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800340
341 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800342 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800343 expectInterceptCallReceived(123);
344 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
345
346 // The second event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800347 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800348 expectInterceptCallReceived(456);
349 expectVsyncEventReceivedByConnection(456, 2u);
350
351 // The third event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800352 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800353 expectInterceptCallReceived(789);
354 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
355
356 // The fourth event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800357 mCallback->onVSyncEvent(101112);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800358 expectInterceptCallReceived(101112);
359 expectVsyncEventReceivedByConnection(101112, 4u);
360}
361
362TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
363 mThread->setVsyncRate(1, mConnection);
364
Dominik Laskowski029cc122019-01-23 19:52:06 -0800365 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800366 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800367
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 Laskowski029cc122019-01-23 19:52:06 -0800372 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800373 expectInterceptCallReceived(123);
374 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
375
376 // EventThread should disable vsync callbacks
377 expectVSyncSetEnabledCallReceived(false);
378}
379
380TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
381 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
382 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
383 mThread->setVsyncRate(1, errorConnection);
384
Dominik Laskowski029cc122019-01-23 19:52:06 -0800385 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800386 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800387
388 // The first event will be seen by the interceptor, and by the connection,
389 // which then returns an error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800390 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800391 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 Laskowski029cc122019-01-23 19:52:06 -0800396 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800397 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
404TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
405 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
406 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
407 mThread->setVsyncRate(1, errorConnection);
408
Dominik Laskowski029cc122019-01-23 19:52:06 -0800409 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800410 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800411
412 // The first event will be seen by the interceptor, and by the connection,
413 // which then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800414 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800415 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 Laskowski029cc122019-01-23 19:52:06 -0800420 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800421 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
428TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
429 mThread->setPhaseOffset(321);
430 expectVSyncSetPhaseOffsetCallReceived(321);
431}
432
Ady Abrahamb838aed2019-02-12 15:30:16 -0800433TEST_F(EventThreadTest, pauseVsyncCallbackForwardsToVSyncSource) {
434 mThread->pauseVsyncCallback(true);
435 expectVSyncPauseVsyncCallbackCallReceived(true);
436}
437
438TEST_F(EventThreadTest, resumeVsyncCallbackForwardsToVSyncSource) {
439 mThread->pauseVsyncCallback(false);
440 expectVSyncPauseVsyncCallbackCallReceived(false);
441}
442
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800443TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
444 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
445 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800446}
447
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800448TEST_F(EventThreadTest, postHotplugInternalConnect) {
449 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
450 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800451}
452
453TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800454 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
455 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800456}
457
458TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800459 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
460 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800461}
462
Ady Abraham447052e2019-02-13 16:07:27 -0800463TEST_F(EventThreadTest, postConfigChangedPrimary) {
464 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 7);
465 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7);
466}
467
468TEST_F(EventThreadTest, postConfigChangedExternal) {
469 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, 5);
470 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5);
471}
472
Lloyd Pique24b0a482018-03-09 18:52:26 -0800473} // namespace
474} // namespace android