blob: afebc40aa96b01a1e6e55e3f68b0eaf3cbe7f48e [file] [log] [blame]
Ady Abraham50c202a2019-03-14 11:44:38 -07001/*
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
32namespace android {
33namespace {
34
35using namespace std::chrono_literals;
36using testing::Return;
37
38class DispSyncSourceTest : public testing::Test, private VSyncSource::Callback {
39protected:
40 DispSyncSourceTest();
41 ~DispSyncSourceTest() override;
42
43 void createDispSync();
44 void createDispSyncSource();
45
Ady Abraham5facfb12020-04-22 15:18:31 -070046 void onVSyncEvent(nsecs_t when, nsecs_t expectedVSyncTimestamp) override;
Ady Abraham50c202a2019-03-14 11:44:38 -070047
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;
54 static constexpr int mIterations = 100;
55};
56
57DispSyncSourceTest::DispSyncSourceTest() {
58 const ::testing::TestInfo* const test_info =
59 ::testing::UnitTest::GetInstance()->current_test_info();
60 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
61}
62
63DispSyncSourceTest::~DispSyncSourceTest() {
64 const ::testing::TestInfo* const test_info =
65 ::testing::UnitTest::GetInstance()->current_test_info();
66 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
67}
68
Ady Abraham5facfb12020-04-22 15:18:31 -070069void DispSyncSourceTest::onVSyncEvent(nsecs_t when, nsecs_t /*expectedVSyncTimestamp*/) {
Ady Abraham50c202a2019-03-14 11:44:38 -070070 ALOGD("onVSyncEvent: %" PRId64, when);
71
72 mVSyncEventCallRecorder.recordCall(when);
73}
74
75void DispSyncSourceTest::createDispSync() {
76 mDispSync = std::make_unique<mock::DispSync>();
77}
78
79void DispSyncSourceTest::createDispSyncSource() {
80 createDispSync();
Ady Abraham9e16a482019-12-03 17:19:41 -080081 mDispSyncSource = std::make_unique<DispSyncSource>(mDispSync.get(), mPhaseOffset.count(), true,
Ady Abraham50c202a2019-03-14 11:44:38 -070082 "DispSyncSourceTest");
83 mDispSyncSource->setCallback(this);
84}
85
86/* ------------------------------------------------------------------------
87 * Test cases
88 */
89
90TEST_F(DispSyncSourceTest, createDispSync) {
91 createDispSync();
92 EXPECT_TRUE(mDispSync);
93}
94
95TEST_F(DispSyncSourceTest, createDispSyncSource) {
96 createDispSyncSource();
97 EXPECT_TRUE(mDispSyncSource);
98}
99
100TEST_F(DispSyncSourceTest, noCallbackAfterInit) {
101 createDispSyncSource();
102 EXPECT_TRUE(mDispSyncSource);
103
104 // DispSyncSource starts with Vsync disabled
105 mDispSync->triggerCallback();
106 EXPECT_FALSE(mVSyncEventCallRecorder.waitForUnexpectedCall().has_value());
107}
108
109TEST_F(DispSyncSourceTest, waitForCallbacks) {
110 createDispSyncSource();
111 EXPECT_TRUE(mDispSyncSource);
112
113 mDispSyncSource->setVSyncEnabled(true);
114 EXPECT_EQ(mDispSync->getCallbackPhase(), mPhaseOffset.count());
115
116 for (int i = 0; i < mIterations; i++) {
117 mDispSync->triggerCallback();
118 EXPECT_TRUE(mVSyncEventCallRecorder.waitForCall().has_value());
119 }
120}
121
122TEST_F(DispSyncSourceTest, waitForCallbacksWithPhaseChange) {
123 createDispSyncSource();
124 EXPECT_TRUE(mDispSyncSource);
125
126 mDispSyncSource->setVSyncEnabled(true);
127 EXPECT_EQ(mDispSync->getCallbackPhase(), mPhaseOffset.count());
128
129 for (int i = 0; i < mIterations; i++) {
130 mDispSync->triggerCallback();
131 EXPECT_TRUE(mVSyncEventCallRecorder.waitForCall().has_value());
132 }
133
134 EXPECT_CALL(*mDispSync, getPeriod()).Times(1).WillOnce(Return(16666666));
135 mDispSyncSource->setPhaseOffset((mPhaseOffset / 2).count());
136
137 EXPECT_EQ(mDispSync->getCallbackPhase(), (mPhaseOffset / 2).count());
138
139 for (int i = 0; i < mIterations; i++) {
140 mDispSync->triggerCallback();
141 EXPECT_TRUE(mVSyncEventCallRecorder.waitForCall().has_value());
142 }
143}
144
Ady Abraham50c202a2019-03-14 11:44:38 -0700145} // namespace
146} // namespace android