Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 "FrameTimeline.h" |
| 24 | #include "Scheduler/MessageQueue.h" |
| 25 | #include "SurfaceFlinger.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | using namespace std::chrono_literals; |
| 30 | using namespace testing; |
| 31 | |
| 32 | using CallbackToken = scheduler::VSyncDispatch::CallbackToken; |
| 33 | |
| 34 | class TestableMessageQueue : public impl::MessageQueue { |
| 35 | public: |
| 36 | class MockHandler : public MessageQueue::Handler { |
| 37 | public: |
| 38 | explicit MockHandler(MessageQueue& queue) : MessageQueue::Handler(queue) {} |
| 39 | ~MockHandler() override = default; |
| 40 | MOCK_METHOD2(dispatchInvalidate, void(int64_t vsyncId, nsecs_t expectedVSyncTimestamp)); |
| 41 | }; |
| 42 | |
| 43 | TestableMessageQueue() = default; |
| 44 | ~TestableMessageQueue() override = default; |
| 45 | |
| 46 | void initHandler(const sp<MockHandler>& handler) { mHandler = handler; } |
| 47 | |
| 48 | void triggerVsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) { |
| 49 | vsyncCallback(vsyncTime, targetWakeupTime, readyTime); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | class MockVSyncDispatch : public scheduler::VSyncDispatch { |
| 54 | public: |
| 55 | MockVSyncDispatch() = default; |
| 56 | ~MockVSyncDispatch() override = default; |
| 57 | |
| 58 | MOCK_METHOD2(registerCallback, |
| 59 | CallbackToken(std::function<void(nsecs_t, nsecs_t, nsecs_t)> const&, std::string)); |
| 60 | MOCK_METHOD1(unregisterCallback, void(CallbackToken)); |
| 61 | MOCK_METHOD2(schedule, scheduler::ScheduleResult(CallbackToken, ScheduleTiming)); |
| 62 | MOCK_METHOD1(cancel, scheduler::CancelResult(CallbackToken token)); |
| 63 | MOCK_CONST_METHOD1(dump, void(std::string&)); |
| 64 | }; |
| 65 | |
| 66 | class MockTokenManager : public frametimeline::TokenManager { |
| 67 | public: |
| 68 | MockTokenManager() = default; |
| 69 | ~MockTokenManager() override = default; |
| 70 | |
| 71 | MOCK_METHOD1(generateTokenForPredictions, int64_t(frametimeline::TimelineItem&& prediction)); |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame^] | 72 | MOCK_CONST_METHOD1(getPredictionsForToken, std::optional<frametimeline::TimelineItem>(int64_t)); |
Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | class MessageQueueTest : public testing::Test { |
| 76 | public: |
| 77 | MessageQueueTest() = default; |
| 78 | ~MessageQueueTest() override = default; |
| 79 | |
| 80 | void SetUp() override { |
| 81 | EXPECT_NO_FATAL_FAILURE(mEventQueue.initHandler(mHandler)); |
| 82 | |
| 83 | EXPECT_CALL(mVSyncDispatch, registerCallback(_, "sf")).WillOnce(Return(mCallbackToken)); |
| 84 | EXPECT_NO_FATAL_FAILURE(mEventQueue.initVsync(mVSyncDispatch, mTokenManager, mDuration)); |
| 85 | EXPECT_CALL(mVSyncDispatch, unregisterCallback(mCallbackToken)).Times(1); |
| 86 | } |
| 87 | |
| 88 | sp<TestableMessageQueue::MockHandler> mHandler = |
| 89 | new TestableMessageQueue::MockHandler(mEventQueue); |
| 90 | MockVSyncDispatch mVSyncDispatch; |
| 91 | MockTokenManager mTokenManager; |
| 92 | TestableMessageQueue mEventQueue; |
| 93 | |
| 94 | const CallbackToken mCallbackToken{5}; |
| 95 | constexpr static auto mDuration = std::chrono::nanoseconds(100ms); |
| 96 | constexpr static auto mDifferentDuration = std::chrono::nanoseconds(250ms); |
| 97 | }; |
| 98 | |
| 99 | namespace { |
| 100 | /* ------------------------------------------------------------------------ |
| 101 | * Test cases |
| 102 | */ |
| 103 | TEST_F(MessageQueueTest, invalidate) { |
| 104 | const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(), |
| 105 | .readyDuration = 0, |
| 106 | .earliestVsync = 0}; |
| 107 | EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timing)).Times(1); |
| 108 | EXPECT_NO_FATAL_FAILURE(mEventQueue.invalidate()); |
| 109 | } |
| 110 | |
| 111 | TEST_F(MessageQueueTest, invalidateTwice) { |
| 112 | InSequence s; |
| 113 | const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(), |
| 114 | .readyDuration = 0, |
| 115 | .earliestVsync = 0}; |
| 116 | |
| 117 | EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timing)).Times(1); |
| 118 | EXPECT_NO_FATAL_FAILURE(mEventQueue.invalidate()); |
| 119 | |
| 120 | EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timing)).Times(1); |
| 121 | EXPECT_NO_FATAL_FAILURE(mEventQueue.invalidate()); |
| 122 | } |
| 123 | |
| 124 | TEST_F(MessageQueueTest, invalidateTwiceWithCallback) { |
| 125 | InSequence s; |
| 126 | const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(), |
| 127 | .readyDuration = 0, |
| 128 | .earliestVsync = 0}; |
| 129 | |
| 130 | EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timing)).Times(1); |
| 131 | EXPECT_NO_FATAL_FAILURE(mEventQueue.invalidate()); |
| 132 | |
| 133 | const auto startTime = 100; |
| 134 | const auto endTime = startTime + mDuration.count(); |
| 135 | const auto presentTime = 500; |
| 136 | const auto vsyncId = 42; |
| 137 | EXPECT_CALL(mTokenManager, |
| 138 | generateTokenForPredictions( |
| 139 | frametimeline::TimelineItem(startTime, endTime, presentTime))) |
| 140 | .WillOnce(Return(vsyncId)); |
| 141 | EXPECT_CALL(*mHandler, dispatchInvalidate(vsyncId, presentTime)).Times(1); |
| 142 | EXPECT_NO_FATAL_FAILURE(mEventQueue.triggerVsyncCallback(presentTime, startTime, endTime)); |
| 143 | |
| 144 | const auto timingAfterCallback = |
| 145 | scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(), |
| 146 | .readyDuration = 0, |
| 147 | .earliestVsync = presentTime}; |
| 148 | |
| 149 | EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timingAfterCallback)).Times(1); |
| 150 | EXPECT_NO_FATAL_FAILURE(mEventQueue.invalidate()); |
| 151 | } |
| 152 | |
| 153 | TEST_F(MessageQueueTest, invalidateWithDurationChange) { |
| 154 | EXPECT_NO_FATAL_FAILURE(mEventQueue.setDuration(mDifferentDuration)); |
| 155 | |
| 156 | const auto timing = |
| 157 | scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDifferentDuration.count(), |
| 158 | .readyDuration = 0, |
| 159 | .earliestVsync = 0}; |
| 160 | |
| 161 | EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timing)).Times(1); |
| 162 | EXPECT_NO_FATAL_FAILURE(mEventQueue.invalidate()); |
| 163 | } |
| 164 | |
| 165 | } // namespace |
| 166 | } // namespace android |