blob: c18068f2ec494f31a6ea4b1137bb703f62aeb2f4 [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
39class MockVSyncSource : public VSyncSource {
40public:
41 MOCK_METHOD1(setVSyncEnabled, void(bool));
42 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
43 MOCK_METHOD1(setPhaseOffset, void(nsecs_t));
44};
45
46} // namespace
47
48class EventThreadTest : public testing::Test {
49protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080050 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080051 public:
Dominik Laskowskif654d572018-12-20 11:03:06 -080052 MockEventThreadConnection(android::impl::EventThread* eventThread,
53 ResyncCallback&& resyncCallback)
54 : EventThreadConnection(eventThread, std::move(resyncCallback)) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080055 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
56 };
57
58 using ConnectionEventRecorder =
59 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
60
61 EventThreadTest();
62 ~EventThreadTest() override;
63
64 void createThread();
65 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder);
66
67 void expectVSyncSetEnabledCallReceived(bool expectedState);
68 void expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset);
69 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
70 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
71 void expectVsyncEventReceivedByConnection(const char* name,
72 ConnectionEventRecorder& connectionEventRecorder,
73 nsecs_t expectedTimestamp, unsigned expectedCount);
74 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070075 void expectHotplugEventReceivedByConnection(EventThread::DisplayType expectedDisplayType,
76 bool expectedConnected);
Lloyd Pique24b0a482018-03-09 18:52:26 -080077
78 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
79 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
80 AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder;
81 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
82 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
83 ConnectionEventRecorder mConnectionEventCallRecorder{0};
84
85 MockVSyncSource mVSyncSource;
86 std::unique_ptr<android::impl::EventThread> mThread;
87 sp<MockEventThreadConnection> mConnection;
88};
89
90EventThreadTest::EventThreadTest() {
91 const ::testing::TestInfo* const test_info =
92 ::testing::UnitTest::GetInstance()->current_test_info();
93 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
94
95 EXPECT_CALL(mVSyncSource, setVSyncEnabled(_))
96 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
97
98 EXPECT_CALL(mVSyncSource, setCallback(_))
99 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
100
101 EXPECT_CALL(mVSyncSource, setPhaseOffset(_))
102 .WillRepeatedly(Invoke(mVSyncSetPhaseOffsetCallRecorder.getInvocable()));
103
104 createThread();
105 mConnection = createConnection(mConnectionEventCallRecorder);
106}
107
108EventThreadTest::~EventThreadTest() {
109 const ::testing::TestInfo* const test_info =
110 ::testing::UnitTest::GetInstance()->current_test_info();
111 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
112}
113
114void EventThreadTest::createThread() {
115 mThread =
116 std::make_unique<android::impl::EventThread>(&mVSyncSource,
Lloyd Pique24b0a482018-03-09 18:52:26 -0800117 mInterceptVSyncCallRecorder.getInvocable(),
118 "unit-test-event-thread");
119}
120
121sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
122 ConnectionEventRecorder& recorder) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800123 sp<MockEventThreadConnection> connection =
124 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800125 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
126 return connection;
127}
128
129void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
130 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
131 ASSERT_TRUE(args.has_value());
132 EXPECT_EQ(expectedState, std::get<0>(args.value()));
133}
134
135void EventThreadTest::expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset) {
136 auto args = mVSyncSetPhaseOffsetCallRecorder.waitForCall();
137 ASSERT_TRUE(args.has_value());
138 EXPECT_EQ(expectedPhaseOffset, std::get<0>(args.value()));
139}
140
141VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
142 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
143 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
144}
145
146void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
147 auto args = mInterceptVSyncCallRecorder.waitForCall();
148 ASSERT_TRUE(args.has_value());
149 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
150}
151
152void EventThreadTest::expectVsyncEventReceivedByConnection(
153 const char* name, ConnectionEventRecorder& connectionEventRecorder,
154 nsecs_t expectedTimestamp, unsigned expectedCount) {
155 auto args = connectionEventRecorder.waitForCall();
156 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
157 << expectedTimestamp;
158 const auto& event = std::get<0>(args.value());
159 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
160 << name << " did not get the correct event for timestamp " << expectedTimestamp;
161 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
162 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
163 EXPECT_EQ(expectedCount, event.vsync.count)
164 << name << " did not get the expected count for timestamp " << expectedTimestamp;
165}
166
167void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
168 unsigned expectedCount) {
169 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
170 mConnectionEventCallRecorder, expectedTimestamp,
171 expectedCount);
172}
173
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700174void EventThreadTest::expectHotplugEventReceivedByConnection(
175 EventThread::DisplayType expectedDisplayType, bool expectedConnected) {
176 const uint32_t expectedDisplayId =
177 expectedDisplayType == EventThread::DisplayType::Primary ? 0 : 1;
178
Lloyd Pique24b0a482018-03-09 18:52:26 -0800179 auto args = mConnectionEventCallRecorder.waitForCall();
180 ASSERT_TRUE(args.has_value());
181 const auto& event = std::get<0>(args.value());
182 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700183 EXPECT_EQ(expectedDisplayId, event.header.id);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800184 EXPECT_EQ(expectedConnected, event.hotplug.connected);
185}
186
187namespace {
188
189/* ------------------------------------------------------------------------
190 * Test cases
191 */
192
193TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
194 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
195 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
196 EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value());
197 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
198 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
199 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
200}
201
202TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
203 // Signal that we want the next vsync event to be posted to the connection
Ana Krulec7d1d6832018-12-27 11:10:09 -0800204 mThread->requestNextVsync(mConnection, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800205
206 // EventThread should immediately request a resync.
207 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
208
209 // EventThread should enable vsync callbacks, and set a callback interface
210 // pointer to use them with the VSync source.
211 expectVSyncSetEnabledCallReceived(true);
212 auto callback = expectVSyncSetCallbackCallReceived();
213 ASSERT_TRUE(callback);
214
215 // Use the received callback to signal a first vsync event.
216 // The interceptor should receive the event, as well as the connection.
217 callback->onVSyncEvent(123);
218 expectInterceptCallReceived(123);
219 expectVsyncEventReceivedByConnection(123, 1u);
220
221 // Use the received callback to signal a second vsync event.
222 // The interceptor should receive the event, but the the connection should
223 // not as it was only interested in the first.
224 callback->onVSyncEvent(456);
225 expectInterceptCallReceived(456);
226 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
227
228 // EventThread should also detect that at this point that it does not need
229 // any more vsync events, and should disable their generation.
230 expectVSyncSetEnabledCallReceived(false);
231}
232
233TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
234 // Create a first connection, register it, and request a vsync rate of zero.
235 ConnectionEventRecorder firstConnectionEventRecorder{0};
236 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
237 mThread->setVsyncRate(0, firstConnection);
238
239 // By itself, this should not enable vsync events
240 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
241 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
242
243 // However if there is another connection which wants events at a nonzero rate.....
244 ConnectionEventRecorder secondConnectionEventRecorder{0};
245 sp<MockEventThreadConnection> secondConnection =
246 createConnection(secondConnectionEventRecorder);
247 mThread->setVsyncRate(1, secondConnection);
248
249 // EventThread should enable vsync callbacks, and set a callback interface
250 // pointer to use them with the VSync source.
251 expectVSyncSetEnabledCallReceived(true);
252 auto callback = expectVSyncSetCallbackCallReceived();
253 ASSERT_TRUE(callback);
254
255 // Send a vsync event. EventThread should then make a call to the
256 // interceptor, and the second connection. The first connection should not
257 // get the event.
258 callback->onVSyncEvent(123);
259 expectInterceptCallReceived(123);
260 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
261 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
262 1u);
263}
264
265TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
266 mThread->setVsyncRate(1, mConnection);
267
268 // EventThread should enable vsync callbacks, and set a callback interface
269 // pointer to use them with the VSync source.
270 expectVSyncSetEnabledCallReceived(true);
271 auto callback = expectVSyncSetCallbackCallReceived();
272 ASSERT_TRUE(callback);
273
274 // Send a vsync event. EventThread should then make a call to the
275 // interceptor, and the connection.
276 callback->onVSyncEvent(123);
277 expectInterceptCallReceived(123);
278 expectVsyncEventReceivedByConnection(123, 1u);
279
280 // A second event should go to the same places.
281 callback->onVSyncEvent(456);
282 expectInterceptCallReceived(456);
283 expectVsyncEventReceivedByConnection(456, 2u);
284
285 // A third event should go to the same places.
286 callback->onVSyncEvent(789);
287 expectInterceptCallReceived(789);
288 expectVsyncEventReceivedByConnection(789, 3u);
289}
290
291TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
292 mThread->setVsyncRate(2, mConnection);
293
294 // EventThread should enable vsync callbacks, and set a callback interface
295 // pointer to use them with the VSync source.
296 expectVSyncSetEnabledCallReceived(true);
297 auto callback = expectVSyncSetCallbackCallReceived();
298 ASSERT_TRUE(callback);
299
300 // The first event will be seen by the interceptor, and not the connection.
301 callback->onVSyncEvent(123);
302 expectInterceptCallReceived(123);
303 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
304
305 // The second event will be seen by the interceptor and the connection.
306 callback->onVSyncEvent(456);
307 expectInterceptCallReceived(456);
308 expectVsyncEventReceivedByConnection(456, 2u);
309
310 // The third event will be seen by the interceptor, and not the connection.
311 callback->onVSyncEvent(789);
312 expectInterceptCallReceived(789);
313 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
314
315 // The fourth event will be seen by the interceptor and the connection.
316 callback->onVSyncEvent(101112);
317 expectInterceptCallReceived(101112);
318 expectVsyncEventReceivedByConnection(101112, 4u);
319}
320
321TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
322 mThread->setVsyncRate(1, mConnection);
323
324 // EventThread should enable vsync callbacks, and set a callback interface
325 // pointer to use them with the VSync source.
326 expectVSyncSetEnabledCallReceived(true);
327 auto callback = expectVSyncSetCallbackCallReceived();
328 ASSERT_TRUE(callback);
329
330 // Destroy the only (strong) reference to the connection.
331 mConnection = nullptr;
332
333 // The first event will be seen by the interceptor, and not the connection.
334 callback->onVSyncEvent(123);
335 expectInterceptCallReceived(123);
336 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
337
338 // EventThread should disable vsync callbacks
339 expectVSyncSetEnabledCallReceived(false);
340}
341
342TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
343 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
344 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
345 mThread->setVsyncRate(1, errorConnection);
346
347 // EventThread should enable vsync callbacks, and set a callback interface
348 // pointer to use them with the VSync source.
349 expectVSyncSetEnabledCallReceived(true);
350 auto callback = expectVSyncSetCallbackCallReceived();
351 ASSERT_TRUE(callback);
352
353 // The first event will be seen by the interceptor, and by the connection,
354 // which then returns an error.
355 callback->onVSyncEvent(123);
356 expectInterceptCallReceived(123);
357 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
358
359 // A subsequent event will be seen by the interceptor and not by the
360 // connection.
361 callback->onVSyncEvent(456);
362 expectInterceptCallReceived(456);
363 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
364
365 // EventThread should disable vsync callbacks with the second event
366 expectVSyncSetEnabledCallReceived(false);
367}
368
369TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
370 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
371 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
372 mThread->setVsyncRate(1, errorConnection);
373
374 // EventThread should enable vsync callbacks, and set a callback interface
375 // pointer to use them with the VSync source.
376 expectVSyncSetEnabledCallReceived(true);
377 auto callback = expectVSyncSetCallbackCallReceived();
378 ASSERT_TRUE(callback);
379
380 // The first event will be seen by the interceptor, and by the connection,
381 // which then returns an non-fatal error.
382 callback->onVSyncEvent(123);
383 expectInterceptCallReceived(123);
384 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
385
386 // A subsequent event will be seen by the interceptor, and by the connection,
387 // which still then returns an non-fatal error.
388 callback->onVSyncEvent(456);
389 expectInterceptCallReceived(456);
390 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
391
392 // EventThread will not disable vsync callbacks as the errors are non-fatal.
393 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
394}
395
396TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
397 mThread->setPhaseOffset(321);
398 expectVSyncSetPhaseOffsetCallReceived(321);
399}
400
401TEST_F(EventThreadTest, postHotplugPrimaryDisconnect) {
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700402 mThread->onHotplugReceived(EventThread::DisplayType::Primary, false);
403 expectHotplugEventReceivedByConnection(EventThread::DisplayType::Primary, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800404}
405
406TEST_F(EventThreadTest, postHotplugPrimaryConnect) {
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700407 mThread->onHotplugReceived(EventThread::DisplayType::Primary, true);
408 expectHotplugEventReceivedByConnection(EventThread::DisplayType::Primary, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800409}
410
411TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700412 mThread->onHotplugReceived(EventThread::DisplayType::External, false);
413 expectHotplugEventReceivedByConnection(EventThread::DisplayType::External, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800414}
415
416TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700417 mThread->onHotplugReceived(EventThread::DisplayType::External, true);
418 expectHotplugEventReceivedByConnection(EventThread::DisplayType::External, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800419}
420
421} // namespace
422} // namespace android