blob: 83a92c8691931e82afd7bcba77ca81a2ef385a6b [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;
Ady Abrahamaf0ec272019-03-28 11:38:31 -070041constexpr PhysicalDisplayId DISPLAY_ID_64BIT = 0xabcd12349876fedcULL;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080042
Lloyd Pique24b0a482018-03-09 18:52:26 -080043class MockVSyncSource : public VSyncSource {
44public:
45 MOCK_METHOD1(setVSyncEnabled, void(bool));
46 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
47 MOCK_METHOD1(setPhaseOffset, void(nsecs_t));
Ady Abrahamb838aed2019-02-12 15:30:16 -080048 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Lloyd Pique24b0a482018-03-09 18:52:26 -080049};
50
51} // namespace
52
53class EventThreadTest : public testing::Test {
54protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080055 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080056 public:
Dominik Laskowskif654d572018-12-20 11:03:06 -080057 MockEventThreadConnection(android::impl::EventThread* eventThread,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080058 ResyncCallback&& resyncCallback,
59 ResetIdleTimerCallback&& resetIdleTimerCallback)
60 : EventThreadConnection(eventThread, std::move(resyncCallback),
61 std::move(resetIdleTimerCallback)) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080062 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
63 };
64
65 using ConnectionEventRecorder =
66 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
67
68 EventThreadTest();
69 ~EventThreadTest() override;
70
71 void createThread();
72 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder);
73
74 void expectVSyncSetEnabledCallReceived(bool expectedState);
75 void expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset);
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 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;
90 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080091 AsyncCallRecorder<void (*)()> mResetIdleTimerCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -080092 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
93 ConnectionEventRecorder mConnectionEventCallRecorder{0};
94
95 MockVSyncSource mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -080096 VSyncSource::Callback* mCallback = nullptr;
Lloyd Pique24b0a482018-03-09 18:52:26 -080097 std::unique_ptr<android::impl::EventThread> mThread;
98 sp<MockEventThreadConnection> mConnection;
99};
100
101EventThreadTest::EventThreadTest() {
102 const ::testing::TestInfo* const test_info =
103 ::testing::UnitTest::GetInstance()->current_test_info();
104 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
105
106 EXPECT_CALL(mVSyncSource, setVSyncEnabled(_))
107 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
108
109 EXPECT_CALL(mVSyncSource, setCallback(_))
110 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
111
112 EXPECT_CALL(mVSyncSource, setPhaseOffset(_))
113 .WillRepeatedly(Invoke(mVSyncSetPhaseOffsetCallRecorder.getInvocable()));
114
115 createThread();
116 mConnection = createConnection(mConnectionEventCallRecorder);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800117
118 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800119 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
120 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800121}
122
123EventThreadTest::~EventThreadTest() {
124 const ::testing::TestInfo* const test_info =
125 ::testing::UnitTest::GetInstance()->current_test_info();
126 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800127
128 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800129 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800130}
131
132void EventThreadTest::createThread() {
133 mThread =
134 std::make_unique<android::impl::EventThread>(&mVSyncSource,
Lloyd Pique24b0a482018-03-09 18:52:26 -0800135 mInterceptVSyncCallRecorder.getInvocable(),
136 "unit-test-event-thread");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800137
138 // EventThread should register itself as VSyncSource callback.
139 mCallback = expectVSyncSetCallbackCallReceived();
140 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800141}
142
143sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
144 ConnectionEventRecorder& recorder) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800145 sp<MockEventThreadConnection> connection =
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800146 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
147 mResetIdleTimerCallRecorder.getInvocable());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800148 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
149 return connection;
150}
151
152void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
153 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
154 ASSERT_TRUE(args.has_value());
155 EXPECT_EQ(expectedState, std::get<0>(args.value()));
156}
157
158void EventThreadTest::expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset) {
159 auto args = mVSyncSetPhaseOffsetCallRecorder.waitForCall();
160 ASSERT_TRUE(args.has_value());
161 EXPECT_EQ(expectedPhaseOffset, std::get<0>(args.value()));
162}
163
164VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
165 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
166 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
167}
168
169void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
170 auto args = mInterceptVSyncCallRecorder.waitForCall();
171 ASSERT_TRUE(args.has_value());
172 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
173}
174
175void EventThreadTest::expectVsyncEventReceivedByConnection(
176 const char* name, ConnectionEventRecorder& connectionEventRecorder,
177 nsecs_t expectedTimestamp, unsigned expectedCount) {
178 auto args = connectionEventRecorder.waitForCall();
179 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
180 << expectedTimestamp;
181 const auto& event = std::get<0>(args.value());
182 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
183 << name << " did not get the correct event for timestamp " << expectedTimestamp;
184 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
185 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
186 EXPECT_EQ(expectedCount, event.vsync.count)
187 << name << " did not get the expected count for timestamp " << expectedTimestamp;
188}
189
190void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
191 unsigned expectedCount) {
192 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
193 mConnectionEventCallRecorder, expectedTimestamp,
194 expectedCount);
195}
196
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800197void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
198 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800199 auto args = mConnectionEventCallRecorder.waitForCall();
200 ASSERT_TRUE(args.has_value());
201 const auto& event = std::get<0>(args.value());
202 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800203 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800204 EXPECT_EQ(expectedConnected, event.hotplug.connected);
205}
206
Ady Abraham447052e2019-02-13 16:07:27 -0800207void EventThreadTest::expectConfigChangedEventReceivedByConnection(
208 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId) {
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_CONFIG_CHANGED, event.header.type);
213 EXPECT_EQ(expectedDisplayId, event.header.displayId);
214 EXPECT_EQ(expectedConfigId, event.config.configId);
215}
216
Lloyd Pique24b0a482018-03-09 18:52:26 -0800217namespace {
218
219/* ------------------------------------------------------------------------
220 * Test cases
221 */
222
223TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
224 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
225 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
226 EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value());
227 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800228 EXPECT_FALSE(mResetIdleTimerCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800229 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
230 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
231}
232
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800233TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800234 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
235 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800236
237 // Signal that we want the next vsync event to be posted to the connection.
238 mThread->requestNextVsync(mConnection, false);
239
240 // EventThread should not enable vsync callbacks.
241 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800242}
243
Lloyd Pique24b0a482018-03-09 18:52:26 -0800244TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
245 // Signal that we want the next vsync event to be posted to the connection
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800246 mThread->requestNextVsync(mConnection, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800247
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800248 // EventThread should immediately reset the idle timer and request a resync.
249 EXPECT_TRUE(mResetIdleTimerCallRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800250 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
251
Dominik Laskowski029cc122019-01-23 19:52:06 -0800252 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800253 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800254
255 // Use the received callback to signal a first vsync event.
256 // The interceptor should receive the event, as well as the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800257 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800258 expectInterceptCallReceived(123);
259 expectVsyncEventReceivedByConnection(123, 1u);
260
261 // Use the received callback to signal a second vsync event.
262 // The interceptor should receive the event, but the the connection should
263 // not as it was only interested in the first.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800264 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800265 expectInterceptCallReceived(456);
266 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
267
268 // EventThread should also detect that at this point that it does not need
269 // any more vsync events, and should disable their generation.
270 expectVSyncSetEnabledCallReceived(false);
271}
272
273TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
274 // Create a first connection, register it, and request a vsync rate of zero.
275 ConnectionEventRecorder firstConnectionEventRecorder{0};
276 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
277 mThread->setVsyncRate(0, firstConnection);
278
279 // By itself, this should not enable vsync events
280 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
281 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
282
283 // However if there is another connection which wants events at a nonzero rate.....
284 ConnectionEventRecorder secondConnectionEventRecorder{0};
285 sp<MockEventThreadConnection> secondConnection =
286 createConnection(secondConnectionEventRecorder);
287 mThread->setVsyncRate(1, secondConnection);
288
Dominik Laskowski029cc122019-01-23 19:52:06 -0800289 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800290 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800291
292 // Send a vsync event. EventThread should then make a call to the
293 // interceptor, and the second connection. The first connection should not
294 // get the event.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800295 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800296 expectInterceptCallReceived(123);
297 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
298 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
299 1u);
300}
301
302TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
303 mThread->setVsyncRate(1, mConnection);
304
Dominik Laskowski029cc122019-01-23 19:52:06 -0800305 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800306 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800307
308 // Send a vsync event. EventThread should then make a call to the
309 // interceptor, and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800310 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800311 expectInterceptCallReceived(123);
312 expectVsyncEventReceivedByConnection(123, 1u);
313
314 // A second event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800315 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800316 expectInterceptCallReceived(456);
317 expectVsyncEventReceivedByConnection(456, 2u);
318
319 // A third event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800320 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800321 expectInterceptCallReceived(789);
322 expectVsyncEventReceivedByConnection(789, 3u);
323}
324
325TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
326 mThread->setVsyncRate(2, mConnection);
327
Dominik Laskowski029cc122019-01-23 19:52:06 -0800328 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800329 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800330
331 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800332 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800333 expectInterceptCallReceived(123);
334 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
335
336 // The second event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800337 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800338 expectInterceptCallReceived(456);
339 expectVsyncEventReceivedByConnection(456, 2u);
340
341 // The third event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800342 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800343 expectInterceptCallReceived(789);
344 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
345
346 // The fourth event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800347 mCallback->onVSyncEvent(101112);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800348 expectInterceptCallReceived(101112);
349 expectVsyncEventReceivedByConnection(101112, 4u);
350}
351
352TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
353 mThread->setVsyncRate(1, mConnection);
354
Dominik Laskowski029cc122019-01-23 19:52:06 -0800355 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800356 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800357
358 // Destroy the only (strong) reference to the connection.
359 mConnection = nullptr;
360
361 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800362 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800363 expectInterceptCallReceived(123);
364 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
365
366 // EventThread should disable vsync callbacks
367 expectVSyncSetEnabledCallReceived(false);
368}
369
370TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
371 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
372 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
373 mThread->setVsyncRate(1, errorConnection);
374
Dominik Laskowski029cc122019-01-23 19:52:06 -0800375 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800376 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800377
378 // The first event will be seen by the interceptor, and by the connection,
379 // which then returns an error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800380 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800381 expectInterceptCallReceived(123);
382 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
383
384 // A subsequent event will be seen by the interceptor and not by the
385 // connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800386 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800387 expectInterceptCallReceived(456);
388 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
389
390 // EventThread should disable vsync callbacks with the second event
391 expectVSyncSetEnabledCallReceived(false);
392}
393
394TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
395 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
396 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
397 mThread->setVsyncRate(1, errorConnection);
398
Dominik Laskowski029cc122019-01-23 19:52:06 -0800399 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800400 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800401
402 // The first event will be seen by the interceptor, and by the connection,
403 // which then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800404 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800405 expectInterceptCallReceived(123);
406 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
407
408 // A subsequent event will be seen by the interceptor, and by the connection,
409 // which still then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800410 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800411 expectInterceptCallReceived(456);
412 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
413
414 // EventThread will not disable vsync callbacks as the errors are non-fatal.
415 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
416}
417
418TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
419 mThread->setPhaseOffset(321);
420 expectVSyncSetPhaseOffsetCallReceived(321);
421}
422
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800423TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
424 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
425 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800426}
427
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800428TEST_F(EventThreadTest, postHotplugInternalConnect) {
429 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
430 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800431}
432
433TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800434 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
435 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800436}
437
438TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800439 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
440 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800441}
442
Ady Abraham447052e2019-02-13 16:07:27 -0800443TEST_F(EventThreadTest, postConfigChangedPrimary) {
444 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 7);
445 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7);
446}
447
448TEST_F(EventThreadTest, postConfigChangedExternal) {
449 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, 5);
450 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5);
451}
452
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700453TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
454 mThread->onConfigChanged(DISPLAY_ID_64BIT, 7);
455 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7);
456}
457
Lloyd Pique24b0a482018-03-09 18:52:26 -0800458} // namespace
459} // namespace android