| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 1 | /* | 
 | 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 Krulec | fefcb58 | 2018-08-07 14:22:37 -0700 | [diff] [blame] | 28 | #include "Scheduler/EventThread.h" | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 29 |  | 
 | 30 | using namespace std::chrono_literals; | 
 | 31 | using namespace std::placeholders; | 
 | 32 |  | 
 | 33 | using testing::_; | 
 | 34 | using testing::Invoke; | 
 | 35 |  | 
 | 36 | namespace android { | 
 | 37 | namespace { | 
 | 38 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 39 | constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = 111; | 
 | 40 | constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = 222; | 
| Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 41 | constexpr PhysicalDisplayId DISPLAY_ID_64BIT = 0xabcd12349876fedcULL; | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 42 |  | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 43 | class MockVSyncSource : public VSyncSource { | 
 | 44 | public: | 
 | 45 |     MOCK_METHOD1(setVSyncEnabled, void(bool)); | 
 | 46 |     MOCK_METHOD1(setCallback, void(VSyncSource::Callback*)); | 
 | 47 |     MOCK_METHOD1(setPhaseOffset, void(nsecs_t)); | 
| Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame] | 48 |     MOCK_METHOD1(pauseVsyncCallback, void(bool)); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 49 | }; | 
 | 50 |  | 
 | 51 | } // namespace | 
 | 52 |  | 
 | 53 | class EventThreadTest : public testing::Test { | 
 | 54 | protected: | 
| Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 55 |     class MockEventThreadConnection : public EventThreadConnection { | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 56 |     public: | 
| Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 57 |         MockEventThreadConnection(android::impl::EventThread* eventThread, | 
| Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 58 |                                   ResyncCallback&& resyncCallback, | 
 | 59 |                                   ResetIdleTimerCallback&& resetIdleTimerCallback) | 
 | 60 |               : EventThreadConnection(eventThread, std::move(resyncCallback), | 
 | 61 |                                       std::move(resetIdleTimerCallback)) {} | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 62 |         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 Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 82 |     void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, | 
| Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 83 |                                                 bool expectedConnected); | 
| Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 84 |     void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, | 
 | 85 |                                                       int32_t expectedConfigId); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 86 |  | 
 | 87 |     AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder; | 
 | 88 |     AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder; | 
 | 89 |     AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder; | 
 | 90 |     AsyncCallRecorder<void (*)()> mResyncCallRecorder; | 
| Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 91 |     AsyncCallRecorder<void (*)()> mResetIdleTimerCallRecorder; | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 92 |     AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder; | 
 | 93 |     ConnectionEventRecorder mConnectionEventCallRecorder{0}; | 
 | 94 |  | 
 | 95 |     MockVSyncSource mVSyncSource; | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 96 |     VSyncSource::Callback* mCallback = nullptr; | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 97 |     std::unique_ptr<android::impl::EventThread> mThread; | 
 | 98 |     sp<MockEventThreadConnection> mConnection; | 
 | 99 | }; | 
 | 100 |  | 
 | 101 | EventThreadTest::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 Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 117 |  | 
 | 118 |     // A display must be connected for VSYNC events to be delivered. | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 119 |     mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); | 
 | 120 |     expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 121 | } | 
 | 122 |  | 
 | 123 | EventThreadTest::~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 Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 127 |  | 
 | 128 |     // EventThread should unregister itself as VSyncSource callback. | 
| Lloyd Pique | 0655b8e | 2019-01-31 18:20:27 -0800 | [diff] [blame] | 129 |     EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value()); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 130 | } | 
 | 131 |  | 
 | 132 | void EventThreadTest::createThread() { | 
 | 133 |     mThread = | 
 | 134 |             std::make_unique<android::impl::EventThread>(&mVSyncSource, | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 135 |                                                          mInterceptVSyncCallRecorder.getInvocable(), | 
 | 136 |                                                          "unit-test-event-thread"); | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 137 |  | 
 | 138 |     // EventThread should register itself as VSyncSource callback. | 
 | 139 |     mCallback = expectVSyncSetCallbackCallReceived(); | 
 | 140 |     ASSERT_TRUE(mCallback); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 141 | } | 
 | 142 |  | 
 | 143 | sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection( | 
 | 144 |         ConnectionEventRecorder& recorder) { | 
| Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 145 |     sp<MockEventThreadConnection> connection = | 
| Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 146 |             new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(), | 
 | 147 |                                           mResetIdleTimerCallRecorder.getInvocable()); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 148 |     EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable())); | 
 | 149 |     return connection; | 
 | 150 | } | 
 | 151 |  | 
 | 152 | void 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 |  | 
 | 158 | void 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 |  | 
 | 164 | VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() { | 
 | 165 |     auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall(); | 
 | 166 |     return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr; | 
 | 167 | } | 
 | 168 |  | 
 | 169 | void 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 |  | 
 | 175 | void 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 |  | 
 | 190 | void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, | 
 | 191 |                                                            unsigned expectedCount) { | 
 | 192 |     expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder", | 
 | 193 |                                          mConnectionEventCallRecorder, expectedTimestamp, | 
 | 194 |                                          expectedCount); | 
 | 195 | } | 
 | 196 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 197 | void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, | 
 | 198 |                                                              bool expectedConnected) { | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 199 |     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 Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 203 |     EXPECT_EQ(expectedDisplayId, event.header.displayId); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 204 |     EXPECT_EQ(expectedConnected, event.hotplug.connected); | 
 | 205 | } | 
 | 206 |  | 
| Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 207 | void 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 Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 217 | namespace { | 
 | 218 |  | 
 | 219 | /* ------------------------------------------------------------------------ | 
 | 220 |  * Test cases | 
 | 221 |  */ | 
 | 222 |  | 
 | 223 | TEST_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 Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 228 |     EXPECT_FALSE(mResetIdleTimerCallRecorder.waitForCall(0us).has_value()); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 229 |     EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value()); | 
 | 230 |     EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value()); | 
 | 231 | } | 
 | 232 |  | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 233 | TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) { | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 234 |     mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); | 
 | 235 |     expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 236 |  | 
 | 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 Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 242 | } | 
 | 243 |  | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 244 | TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) { | 
 | 245 |     // Signal that we want the next vsync event to be posted to the connection | 
| Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 246 |     mThread->requestNextVsync(mConnection, true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 247 |  | 
| Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 248 |     // EventThread should immediately reset the idle timer and request a resync. | 
 | 249 |     EXPECT_TRUE(mResetIdleTimerCallRecorder.waitForCall().has_value()); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 250 |     EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); | 
 | 251 |  | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 252 |     // EventThread should enable vsync callbacks. | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 253 |     expectVSyncSetEnabledCallReceived(true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 254 |  | 
 | 255 |     // Use the received callback to signal a first vsync event. | 
 | 256 |     // The interceptor should receive the event, as well as the connection. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 257 |     mCallback->onVSyncEvent(123); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 258 |     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 Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 264 |     mCallback->onVSyncEvent(456); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 265 |     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 |  | 
 | 273 | TEST_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 Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 289 |     // EventThread should enable vsync callbacks. | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 290 |     expectVSyncSetEnabledCallReceived(true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 291 |  | 
 | 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 Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 295 |     mCallback->onVSyncEvent(123); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 296 |     expectInterceptCallReceived(123); | 
 | 297 |     EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value()); | 
 | 298 |     expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123, | 
 | 299 |                                          1u); | 
 | 300 | } | 
 | 301 |  | 
 | 302 | TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) { | 
 | 303 |     mThread->setVsyncRate(1, mConnection); | 
 | 304 |  | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 305 |     // EventThread should enable vsync callbacks. | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 306 |     expectVSyncSetEnabledCallReceived(true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 307 |  | 
 | 308 |     // Send a vsync event. EventThread should then make a call to the | 
 | 309 |     // interceptor, and the connection. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 310 |     mCallback->onVSyncEvent(123); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 311 |     expectInterceptCallReceived(123); | 
 | 312 |     expectVsyncEventReceivedByConnection(123, 1u); | 
 | 313 |  | 
 | 314 |     // A second event should go to the same places. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 315 |     mCallback->onVSyncEvent(456); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 316 |     expectInterceptCallReceived(456); | 
 | 317 |     expectVsyncEventReceivedByConnection(456, 2u); | 
 | 318 |  | 
 | 319 |     // A third event should go to the same places. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 320 |     mCallback->onVSyncEvent(789); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 321 |     expectInterceptCallReceived(789); | 
 | 322 |     expectVsyncEventReceivedByConnection(789, 3u); | 
 | 323 | } | 
 | 324 |  | 
 | 325 | TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) { | 
 | 326 |     mThread->setVsyncRate(2, mConnection); | 
 | 327 |  | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 328 |     // EventThread should enable vsync callbacks. | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 329 |     expectVSyncSetEnabledCallReceived(true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 330 |  | 
 | 331 |     // The first event will be seen by the interceptor, and not the connection. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 332 |     mCallback->onVSyncEvent(123); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 333 |     expectInterceptCallReceived(123); | 
 | 334 |     EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); | 
 | 335 |  | 
 | 336 |     // The second event will be seen by the interceptor and the connection. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 337 |     mCallback->onVSyncEvent(456); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 338 |     expectInterceptCallReceived(456); | 
 | 339 |     expectVsyncEventReceivedByConnection(456, 2u); | 
 | 340 |  | 
 | 341 |     // The third event will be seen by the interceptor, and not the connection. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 342 |     mCallback->onVSyncEvent(789); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 343 |     expectInterceptCallReceived(789); | 
 | 344 |     EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); | 
 | 345 |  | 
 | 346 |     // The fourth event will be seen by the interceptor and the connection. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 347 |     mCallback->onVSyncEvent(101112); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 348 |     expectInterceptCallReceived(101112); | 
 | 349 |     expectVsyncEventReceivedByConnection(101112, 4u); | 
 | 350 | } | 
 | 351 |  | 
 | 352 | TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) { | 
 | 353 |     mThread->setVsyncRate(1, mConnection); | 
 | 354 |  | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 355 |     // EventThread should enable vsync callbacks. | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 356 |     expectVSyncSetEnabledCallReceived(true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 357 |  | 
 | 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 Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 362 |     mCallback->onVSyncEvent(123); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 363 |     expectInterceptCallReceived(123); | 
 | 364 |     EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); | 
 | 365 |  | 
 | 366 |     // EventThread should disable vsync callbacks | 
 | 367 |     expectVSyncSetEnabledCallReceived(false); | 
 | 368 | } | 
 | 369 |  | 
 | 370 | TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) { | 
 | 371 |     ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; | 
 | 372 |     sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); | 
 | 373 |     mThread->setVsyncRate(1, errorConnection); | 
 | 374 |  | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 375 |     // EventThread should enable vsync callbacks. | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 376 |     expectVSyncSetEnabledCallReceived(true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 377 |  | 
 | 378 |     // The first event will be seen by the interceptor, and by the connection, | 
 | 379 |     // which then returns an error. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 380 |     mCallback->onVSyncEvent(123); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 381 |     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 Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 386 |     mCallback->onVSyncEvent(456); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 387 |     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 |  | 
 | 394 | TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) { | 
 | 395 |     ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK}; | 
 | 396 |     sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); | 
 | 397 |     mThread->setVsyncRate(1, errorConnection); | 
 | 398 |  | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 399 |     // EventThread should enable vsync callbacks. | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 400 |     expectVSyncSetEnabledCallReceived(true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 401 |  | 
 | 402 |     // The first event will be seen by the interceptor, and by the connection, | 
 | 403 |     // which then returns an non-fatal error. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 404 |     mCallback->onVSyncEvent(123); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 405 |     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 Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 410 |     mCallback->onVSyncEvent(456); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 411 |     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 |  | 
 | 418 | TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) { | 
 | 419 |     mThread->setPhaseOffset(321); | 
 | 420 |     expectVSyncSetPhaseOffsetCallReceived(321); | 
 | 421 | } | 
 | 422 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 423 | TEST_F(EventThreadTest, postHotplugInternalDisconnect) { | 
 | 424 |     mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); | 
 | 425 |     expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 426 | } | 
 | 427 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 428 | TEST_F(EventThreadTest, postHotplugInternalConnect) { | 
 | 429 |     mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); | 
 | 430 |     expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 431 | } | 
 | 432 |  | 
 | 433 | TEST_F(EventThreadTest, postHotplugExternalDisconnect) { | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 434 |     mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false); | 
 | 435 |     expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 436 | } | 
 | 437 |  | 
 | 438 | TEST_F(EventThreadTest, postHotplugExternalConnect) { | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 439 |     mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true); | 
 | 440 |     expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 441 | } | 
 | 442 |  | 
| Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 443 | TEST_F(EventThreadTest, postConfigChangedPrimary) { | 
 | 444 |     mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 7); | 
 | 445 |     expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7); | 
 | 446 | } | 
 | 447 |  | 
 | 448 | TEST_F(EventThreadTest, postConfigChangedExternal) { | 
 | 449 |     mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, 5); | 
 | 450 |     expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5); | 
 | 451 | } | 
 | 452 |  | 
| Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 453 | TEST_F(EventThreadTest, postConfigChangedPrimary64bit) { | 
 | 454 |     mThread->onConfigChanged(DISPLAY_ID_64BIT, 7); | 
 | 455 |     expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7); | 
 | 456 | } | 
 | 457 |  | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 458 | } // namespace | 
 | 459 | } // namespace android |