Ady Abraham | 50c202a | 2019-03-14 11:44:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | #define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include <inttypes.h> |
| 22 | |
| 23 | #include <gmock/gmock.h> |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include <log/log.h> |
| 27 | |
| 28 | #include "AsyncCallRecorder.h" |
| 29 | #include "Scheduler/DispSyncSource.h" |
| 30 | #include "mock/MockDispSync.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace { |
| 34 | |
| 35 | using namespace std::chrono_literals; |
| 36 | using testing::Return; |
| 37 | |
| 38 | class DispSyncSourceTest : public testing::Test, private VSyncSource::Callback { |
| 39 | protected: |
| 40 | DispSyncSourceTest(); |
| 41 | ~DispSyncSourceTest() override; |
| 42 | |
| 43 | void createDispSync(); |
| 44 | void createDispSyncSource(); |
| 45 | |
| 46 | void onVSyncEvent(nsecs_t when) override; |
| 47 | |
| 48 | std::unique_ptr<mock::DispSync> mDispSync; |
| 49 | std::unique_ptr<DispSyncSource> mDispSyncSource; |
| 50 | |
| 51 | AsyncCallRecorder<void (*)(nsecs_t)> mVSyncEventCallRecorder; |
| 52 | |
| 53 | static constexpr std::chrono::nanoseconds mPhaseOffset = 6ms; |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 54 | static constexpr std::chrono::nanoseconds mOffsetThresholdForNextVsync = 16ms; |
Ady Abraham | 50c202a | 2019-03-14 11:44:38 -0700 | [diff] [blame] | 55 | static constexpr int mIterations = 100; |
| 56 | }; |
| 57 | |
| 58 | DispSyncSourceTest::DispSyncSourceTest() { |
| 59 | const ::testing::TestInfo* const test_info = |
| 60 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 61 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 62 | } |
| 63 | |
| 64 | DispSyncSourceTest::~DispSyncSourceTest() { |
| 65 | const ::testing::TestInfo* const test_info = |
| 66 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 67 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 68 | } |
| 69 | |
| 70 | void DispSyncSourceTest::onVSyncEvent(nsecs_t when) { |
| 71 | ALOGD("onVSyncEvent: %" PRId64, when); |
| 72 | |
| 73 | mVSyncEventCallRecorder.recordCall(when); |
| 74 | } |
| 75 | |
| 76 | void DispSyncSourceTest::createDispSync() { |
| 77 | mDispSync = std::make_unique<mock::DispSync>(); |
| 78 | } |
| 79 | |
| 80 | void DispSyncSourceTest::createDispSyncSource() { |
| 81 | createDispSync(); |
Ady Abraham | 45e4e36 | 2019-06-07 18:20:51 -0700 | [diff] [blame] | 82 | mDispSyncSource = std::make_unique<DispSyncSource>(mDispSync.get(), mPhaseOffset.count(), |
| 83 | mOffsetThresholdForNextVsync.count(), true, |
Ady Abraham | 50c202a | 2019-03-14 11:44:38 -0700 | [diff] [blame] | 84 | "DispSyncSourceTest"); |
| 85 | mDispSyncSource->setCallback(this); |
| 86 | } |
| 87 | |
| 88 | /* ------------------------------------------------------------------------ |
| 89 | * Test cases |
| 90 | */ |
| 91 | |
| 92 | TEST_F(DispSyncSourceTest, createDispSync) { |
| 93 | createDispSync(); |
| 94 | EXPECT_TRUE(mDispSync); |
| 95 | } |
| 96 | |
| 97 | TEST_F(DispSyncSourceTest, createDispSyncSource) { |
| 98 | createDispSyncSource(); |
| 99 | EXPECT_TRUE(mDispSyncSource); |
| 100 | } |
| 101 | |
| 102 | TEST_F(DispSyncSourceTest, noCallbackAfterInit) { |
| 103 | createDispSyncSource(); |
| 104 | EXPECT_TRUE(mDispSyncSource); |
| 105 | |
| 106 | // DispSyncSource starts with Vsync disabled |
| 107 | mDispSync->triggerCallback(); |
| 108 | EXPECT_FALSE(mVSyncEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 109 | } |
| 110 | |
| 111 | TEST_F(DispSyncSourceTest, waitForCallbacks) { |
| 112 | createDispSyncSource(); |
| 113 | EXPECT_TRUE(mDispSyncSource); |
| 114 | |
| 115 | mDispSyncSource->setVSyncEnabled(true); |
| 116 | EXPECT_EQ(mDispSync->getCallbackPhase(), mPhaseOffset.count()); |
| 117 | |
| 118 | for (int i = 0; i < mIterations; i++) { |
| 119 | mDispSync->triggerCallback(); |
| 120 | EXPECT_TRUE(mVSyncEventCallRecorder.waitForCall().has_value()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | TEST_F(DispSyncSourceTest, waitForCallbacksWithPhaseChange) { |
| 125 | createDispSyncSource(); |
| 126 | EXPECT_TRUE(mDispSyncSource); |
| 127 | |
| 128 | mDispSyncSource->setVSyncEnabled(true); |
| 129 | EXPECT_EQ(mDispSync->getCallbackPhase(), mPhaseOffset.count()); |
| 130 | |
| 131 | for (int i = 0; i < mIterations; i++) { |
| 132 | mDispSync->triggerCallback(); |
| 133 | EXPECT_TRUE(mVSyncEventCallRecorder.waitForCall().has_value()); |
| 134 | } |
| 135 | |
| 136 | EXPECT_CALL(*mDispSync, getPeriod()).Times(1).WillOnce(Return(16666666)); |
| 137 | mDispSyncSource->setPhaseOffset((mPhaseOffset / 2).count()); |
| 138 | |
| 139 | EXPECT_EQ(mDispSync->getCallbackPhase(), (mPhaseOffset / 2).count()); |
| 140 | |
| 141 | for (int i = 0; i < mIterations; i++) { |
| 142 | mDispSync->triggerCallback(); |
| 143 | EXPECT_TRUE(mVSyncEventCallRecorder.waitForCall().has_value()); |
| 144 | } |
| 145 | } |
| 146 | |
Ady Abraham | 50c202a | 2019-03-14 11:44:38 -0700 | [diff] [blame] | 147 | } // namespace |
| 148 | } // namespace android |