blob: 249c78f09ca367e06ada89fdcdd5274db7838481 [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);
Ady Abrahamb838aed2019-02-12 15:30:16 -080076 void expectVSyncPauseVsyncCallbackCallReceived(bool expectedPause);
Lloyd Pique24b0a482018-03-09 18:52:26 -080077 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
78 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
79 void expectVsyncEventReceivedByConnection(const char* name,
80 ConnectionEventRecorder& connectionEventRecorder,
81 nsecs_t expectedTimestamp, unsigned expectedCount);
82 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080083 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070084 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080085 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
86 int32_t expectedConfigId);
Lloyd Pique24b0a482018-03-09 18:52:26 -080087
88 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
89 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
90 AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder;
Ady Abrahamb838aed2019-02-12 15:30:16 -080091 AsyncCallRecorder<void (*)(bool)> mVSyncPauseVsyncCallbackCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -080092 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080093 AsyncCallRecorder<void (*)()> mResetIdleTimerCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -080094 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
95 ConnectionEventRecorder mConnectionEventCallRecorder{0};
96
97 MockVSyncSource mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -080098 VSyncSource::Callback* mCallback = nullptr;
Lloyd Pique24b0a482018-03-09 18:52:26 -080099 std::unique_ptr<android::impl::EventThread> mThread;
100 sp<MockEventThreadConnection> mConnection;
101};
102
103EventThreadTest::EventThreadTest() {
104 const ::testing::TestInfo* const test_info =
105 ::testing::UnitTest::GetInstance()->current_test_info();
106 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
107
108 EXPECT_CALL(mVSyncSource, setVSyncEnabled(_))
109 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
110
111 EXPECT_CALL(mVSyncSource, setCallback(_))
112 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
113
114 EXPECT_CALL(mVSyncSource, setPhaseOffset(_))
115 .WillRepeatedly(Invoke(mVSyncSetPhaseOffsetCallRecorder.getInvocable()));
116
Ady Abrahamb838aed2019-02-12 15:30:16 -0800117 EXPECT_CALL(mVSyncSource, pauseVsyncCallback(_))
118 .WillRepeatedly(Invoke(mVSyncPauseVsyncCallbackCallRecorder.getInvocable()));
119
Lloyd Pique24b0a482018-03-09 18:52:26 -0800120 createThread();
121 mConnection = createConnection(mConnectionEventCallRecorder);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800122
123 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800124 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
125 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800126}
127
128EventThreadTest::~EventThreadTest() {
129 const ::testing::TestInfo* const test_info =
130 ::testing::UnitTest::GetInstance()->current_test_info();
131 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800132
133 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800134 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800135}
136
137void EventThreadTest::createThread() {
138 mThread =
139 std::make_unique<android::impl::EventThread>(&mVSyncSource,
Lloyd Pique24b0a482018-03-09 18:52:26 -0800140 mInterceptVSyncCallRecorder.getInvocable(),
141 "unit-test-event-thread");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800142
143 // EventThread should register itself as VSyncSource callback.
144 mCallback = expectVSyncSetCallbackCallReceived();
145 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800146}
147
148sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
149 ConnectionEventRecorder& recorder) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800150 sp<MockEventThreadConnection> connection =
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800151 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
152 mResetIdleTimerCallRecorder.getInvocable());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800153 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
154 return connection;
155}
156
157void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
158 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
159 ASSERT_TRUE(args.has_value());
160 EXPECT_EQ(expectedState, std::get<0>(args.value()));
161}
162
163void EventThreadTest::expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset) {
164 auto args = mVSyncSetPhaseOffsetCallRecorder.waitForCall();
165 ASSERT_TRUE(args.has_value());
166 EXPECT_EQ(expectedPhaseOffset, std::get<0>(args.value()));
167}
168
Ady Abrahamb838aed2019-02-12 15:30:16 -0800169void EventThreadTest::expectVSyncPauseVsyncCallbackCallReceived(bool expectedPause) {
170 auto args = mVSyncPauseVsyncCallbackCallRecorder.waitForCall();
171 ASSERT_TRUE(args.has_value());
172 EXPECT_EQ(expectedPause, std::get<0>(args.value()));
173}
174
Lloyd Pique24b0a482018-03-09 18:52:26 -0800175VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
176 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
177 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
178}
179
180void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
181 auto args = mInterceptVSyncCallRecorder.waitForCall();
182 ASSERT_TRUE(args.has_value());
183 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
184}
185
186void EventThreadTest::expectVsyncEventReceivedByConnection(
187 const char* name, ConnectionEventRecorder& connectionEventRecorder,
188 nsecs_t expectedTimestamp, unsigned expectedCount) {
189 auto args = connectionEventRecorder.waitForCall();
190 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
191 << expectedTimestamp;
192 const auto& event = std::get<0>(args.value());
193 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
194 << name << " did not get the correct event for timestamp " << expectedTimestamp;
195 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
196 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
197 EXPECT_EQ(expectedCount, event.vsync.count)
198 << name << " did not get the expected count for timestamp " << expectedTimestamp;
199}
200
201void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
202 unsigned expectedCount) {
203 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
204 mConnectionEventCallRecorder, expectedTimestamp,
205 expectedCount);
206}
207
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800208void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
209 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800210 auto args = mConnectionEventCallRecorder.waitForCall();
211 ASSERT_TRUE(args.has_value());
212 const auto& event = std::get<0>(args.value());
213 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800214 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800215 EXPECT_EQ(expectedConnected, event.hotplug.connected);
216}
217
Ady Abraham447052e2019-02-13 16:07:27 -0800218void EventThreadTest::expectConfigChangedEventReceivedByConnection(
219 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId) {
220 auto args = mConnectionEventCallRecorder.waitForCall();
221 ASSERT_TRUE(args.has_value());
222 const auto& event = std::get<0>(args.value());
223 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type);
224 EXPECT_EQ(expectedDisplayId, event.header.displayId);
225 EXPECT_EQ(expectedConfigId, event.config.configId);
226}
227
Lloyd Pique24b0a482018-03-09 18:52:26 -0800228namespace {
229
230/* ------------------------------------------------------------------------
231 * Test cases
232 */
233
234TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
235 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
236 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
237 EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value());
238 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800239 EXPECT_FALSE(mResetIdleTimerCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800240 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
241 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
242}
243
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800244TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800245 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
246 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800247
248 // Signal that we want the next vsync event to be posted to the connection.
249 mThread->requestNextVsync(mConnection, false);
250
251 // EventThread should not enable vsync callbacks.
252 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800253}
254
Lloyd Pique24b0a482018-03-09 18:52:26 -0800255TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
256 // Signal that we want the next vsync event to be posted to the connection
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800257 mThread->requestNextVsync(mConnection, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800258
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800259 // EventThread should immediately reset the idle timer and request a resync.
260 EXPECT_TRUE(mResetIdleTimerCallRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800261 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
262
Dominik Laskowski029cc122019-01-23 19:52:06 -0800263 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800264 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800265
266 // Use the received callback to signal a first vsync event.
267 // The interceptor should receive the event, as well as the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800268 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800269 expectInterceptCallReceived(123);
270 expectVsyncEventReceivedByConnection(123, 1u);
271
272 // Use the received callback to signal a second vsync event.
273 // The interceptor should receive the event, but the the connection should
274 // not as it was only interested in the first.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800275 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800276 expectInterceptCallReceived(456);
277 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
278
279 // EventThread should also detect that at this point that it does not need
280 // any more vsync events, and should disable their generation.
281 expectVSyncSetEnabledCallReceived(false);
282}
283
284TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
285 // Create a first connection, register it, and request a vsync rate of zero.
286 ConnectionEventRecorder firstConnectionEventRecorder{0};
287 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
288 mThread->setVsyncRate(0, firstConnection);
289
290 // By itself, this should not enable vsync events
291 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
292 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
293
294 // However if there is another connection which wants events at a nonzero rate.....
295 ConnectionEventRecorder secondConnectionEventRecorder{0};
296 sp<MockEventThreadConnection> secondConnection =
297 createConnection(secondConnectionEventRecorder);
298 mThread->setVsyncRate(1, secondConnection);
299
Dominik Laskowski029cc122019-01-23 19:52:06 -0800300 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800301 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800302
303 // Send a vsync event. EventThread should then make a call to the
304 // interceptor, and the second connection. The first connection should not
305 // get the event.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800306 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800307 expectInterceptCallReceived(123);
308 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
309 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
310 1u);
311}
312
313TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
314 mThread->setVsyncRate(1, mConnection);
315
Dominik Laskowski029cc122019-01-23 19:52:06 -0800316 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800317 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800318
319 // Send a vsync event. EventThread should then make a call to the
320 // interceptor, and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800321 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800322 expectInterceptCallReceived(123);
323 expectVsyncEventReceivedByConnection(123, 1u);
324
325 // A second event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800326 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800327 expectInterceptCallReceived(456);
328 expectVsyncEventReceivedByConnection(456, 2u);
329
330 // A third event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800331 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800332 expectInterceptCallReceived(789);
333 expectVsyncEventReceivedByConnection(789, 3u);
334}
335
336TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
337 mThread->setVsyncRate(2, mConnection);
338
Dominik Laskowski029cc122019-01-23 19:52:06 -0800339 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800340 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800341
342 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800343 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800344 expectInterceptCallReceived(123);
345 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
346
347 // The second event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800348 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800349 expectInterceptCallReceived(456);
350 expectVsyncEventReceivedByConnection(456, 2u);
351
352 // The third event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800353 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800354 expectInterceptCallReceived(789);
355 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
356
357 // The fourth event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800358 mCallback->onVSyncEvent(101112);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800359 expectInterceptCallReceived(101112);
360 expectVsyncEventReceivedByConnection(101112, 4u);
361}
362
363TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
364 mThread->setVsyncRate(1, mConnection);
365
Dominik Laskowski029cc122019-01-23 19:52:06 -0800366 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800367 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800368
369 // Destroy the only (strong) reference to the connection.
370 mConnection = nullptr;
371
372 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800373 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800374 expectInterceptCallReceived(123);
375 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
376
377 // EventThread should disable vsync callbacks
378 expectVSyncSetEnabledCallReceived(false);
379}
380
381TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
382 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
383 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
384 mThread->setVsyncRate(1, errorConnection);
385
Dominik Laskowski029cc122019-01-23 19:52:06 -0800386 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800387 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800388
389 // The first event will be seen by the interceptor, and by the connection,
390 // which then returns an error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800391 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800392 expectInterceptCallReceived(123);
393 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
394
395 // A subsequent event will be seen by the interceptor and not by the
396 // connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800397 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800398 expectInterceptCallReceived(456);
399 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
400
401 // EventThread should disable vsync callbacks with the second event
402 expectVSyncSetEnabledCallReceived(false);
403}
404
405TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
406 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
407 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
408 mThread->setVsyncRate(1, errorConnection);
409
Dominik Laskowski029cc122019-01-23 19:52:06 -0800410 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800411 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800412
413 // The first event will be seen by the interceptor, and by the connection,
414 // which then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800415 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800416 expectInterceptCallReceived(123);
417 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
418
419 // A subsequent event will be seen by the interceptor, and by the connection,
420 // which still then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800421 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800422 expectInterceptCallReceived(456);
423 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
424
425 // EventThread will not disable vsync callbacks as the errors are non-fatal.
426 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
427}
428
429TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
430 mThread->setPhaseOffset(321);
431 expectVSyncSetPhaseOffsetCallReceived(321);
432}
433
Ady Abrahamb838aed2019-02-12 15:30:16 -0800434TEST_F(EventThreadTest, pauseVsyncCallbackForwardsToVSyncSource) {
435 mThread->pauseVsyncCallback(true);
436 expectVSyncPauseVsyncCallbackCallReceived(true);
437}
438
439TEST_F(EventThreadTest, resumeVsyncCallbackForwardsToVSyncSource) {
440 mThread->pauseVsyncCallback(false);
441 expectVSyncPauseVsyncCallbackCallReceived(false);
442}
443
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800444TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
445 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
446 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800447}
448
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800449TEST_F(EventThreadTest, postHotplugInternalConnect) {
450 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
451 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800452}
453
454TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800455 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
456 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800457}
458
459TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800460 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
461 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800462}
463
Ady Abraham447052e2019-02-13 16:07:27 -0800464TEST_F(EventThreadTest, postConfigChangedPrimary) {
465 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 7);
466 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7);
467}
468
469TEST_F(EventThreadTest, postConfigChangedExternal) {
470 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, 5);
471 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5);
472}
473
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700474TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
475 mThread->onConfigChanged(DISPLAY_ID_64BIT, 7);
476 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7);
477}
478
Lloyd Pique24b0a482018-03-09 18:52:26 -0800479} // namespace
480} // namespace android