blob: dbd9b8403976f3585a97bd895968a33d77469566 [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,
Ady Abraham0f4a1b12019-06-04 16:04:04 -070058 ResyncCallback&& resyncCallback,
59 ISurfaceComposer::ConfigChanged configChanged)
60 : EventThreadConnection(eventThread, std::move(resyncCallback), configChanged) {}
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();
Ady Abraham0f4a1b12019-06-04 16:04:04 -070071 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
72 ISurfaceComposer::ConfigChanged configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -080073
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;
91 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
114 createThread();
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700115 mConnection = createConnection(mConnectionEventCallRecorder,
116 ISurfaceComposer::eConfigChangedDispatch);
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(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700144 ConnectionEventRecorder& recorder, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800145 sp<MockEventThreadConnection> connection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700146 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
147 configChanged);
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());
228 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
229 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
230}
231
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800232TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800233 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
234 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800235
236 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700237 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800238
239 // EventThread should not enable vsync callbacks.
240 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800241}
242
Lloyd Pique24b0a482018-03-09 18:52:26 -0800243TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
244 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700245 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800246
Ady Abraham8532d012019-05-08 14:50:56 -0700247 // EventThread should immediately request a resync.
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};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700274 sp<MockEventThreadConnection> firstConnection =
275 createConnection(firstConnectionEventRecorder,
276 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800277 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 =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700286 createConnection(secondConnectionEventRecorder,
287 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800288 mThread->setVsyncRate(1, secondConnection);
289
Dominik Laskowski029cc122019-01-23 19:52:06 -0800290 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800291 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800292
293 // Send a vsync event. EventThread should then make a call to the
294 // interceptor, and the second connection. The first connection should not
295 // get the event.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800296 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800297 expectInterceptCallReceived(123);
298 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
299 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
300 1u);
301}
302
303TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
304 mThread->setVsyncRate(1, mConnection);
305
Dominik Laskowski029cc122019-01-23 19:52:06 -0800306 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800307 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800308
309 // Send a vsync event. EventThread should then make a call to the
310 // interceptor, and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800311 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800312 expectInterceptCallReceived(123);
313 expectVsyncEventReceivedByConnection(123, 1u);
314
315 // A second event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800316 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800317 expectInterceptCallReceived(456);
318 expectVsyncEventReceivedByConnection(456, 2u);
319
320 // A third event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800321 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800322 expectInterceptCallReceived(789);
323 expectVsyncEventReceivedByConnection(789, 3u);
324}
325
326TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
327 mThread->setVsyncRate(2, mConnection);
328
Dominik Laskowski029cc122019-01-23 19:52:06 -0800329 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800330 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800331
332 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800333 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800334 expectInterceptCallReceived(123);
335 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
336
337 // The second event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800338 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800339 expectInterceptCallReceived(456);
340 expectVsyncEventReceivedByConnection(456, 2u);
341
342 // The third event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800343 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800344 expectInterceptCallReceived(789);
345 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
346
347 // The fourth event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800348 mCallback->onVSyncEvent(101112);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800349 expectInterceptCallReceived(101112);
350 expectVsyncEventReceivedByConnection(101112, 4u);
351}
352
353TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
354 mThread->setVsyncRate(1, mConnection);
355
Dominik Laskowski029cc122019-01-23 19:52:06 -0800356 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800357 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800358
359 // Destroy the only (strong) reference to the connection.
360 mConnection = nullptr;
361
362 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800363 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800364 expectInterceptCallReceived(123);
365 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
366
367 // EventThread should disable vsync callbacks
368 expectVSyncSetEnabledCallReceived(false);
369}
370
371TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
372 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700373 sp<MockEventThreadConnection> errorConnection =
374 createConnection(errorConnectionEventRecorder,
375 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800376 mThread->setVsyncRate(1, errorConnection);
377
Dominik Laskowski029cc122019-01-23 19:52:06 -0800378 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800379 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800380
381 // The first event will be seen by the interceptor, and by the connection,
382 // which then returns an error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800383 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800384 expectInterceptCallReceived(123);
385 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
386
387 // A subsequent event will be seen by the interceptor and not by the
388 // connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800389 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800390 expectInterceptCallReceived(456);
391 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
392
393 // EventThread should disable vsync callbacks with the second event
394 expectVSyncSetEnabledCallReceived(false);
395}
396
397TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
398 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700399 sp<MockEventThreadConnection> errorConnection =
400 createConnection(errorConnectionEventRecorder,
401 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800402 mThread->setVsyncRate(1, errorConnection);
403
Dominik Laskowski029cc122019-01-23 19:52:06 -0800404 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800405 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800406
407 // The first event will be seen by the interceptor, and by the connection,
408 // which then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800409 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800410 expectInterceptCallReceived(123);
411 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
412
413 // A subsequent event will be seen by the interceptor, and by the connection,
414 // which still then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800415 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800416 expectInterceptCallReceived(456);
417 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
418
419 // EventThread will not disable vsync callbacks as the errors are non-fatal.
420 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
421}
422
423TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
424 mThread->setPhaseOffset(321);
425 expectVSyncSetPhaseOffsetCallReceived(321);
426}
427
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800428TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
429 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
430 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800431}
432
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800433TEST_F(EventThreadTest, postHotplugInternalConnect) {
434 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
435 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800436}
437
438TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800439 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
440 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800441}
442
443TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800444 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
445 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800446}
447
Ady Abraham447052e2019-02-13 16:07:27 -0800448TEST_F(EventThreadTest, postConfigChangedPrimary) {
449 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 7);
450 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7);
451}
452
453TEST_F(EventThreadTest, postConfigChangedExternal) {
454 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, 5);
455 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5);
456}
457
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700458TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
459 mThread->onConfigChanged(DISPLAY_ID_64BIT, 7);
460 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7);
461}
462
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700463TEST_F(EventThreadTest, suppressConfigChanged) {
464 ConnectionEventRecorder suppressConnectionEventRecorder{0};
465 sp<MockEventThreadConnection> suppressConnection =
466 createConnection(suppressConnectionEventRecorder,
467 ISurfaceComposer::eConfigChangedSuppress);
468
469 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 9);
470 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9);
471
472 auto args = suppressConnectionEventRecorder.waitForCall();
473 ASSERT_FALSE(args.has_value());
474}
475
Lloyd Pique24b0a482018-03-09 18:52:26 -0800476} // namespace
477} // namespace android