blob: 0aa8cf565d4f591b86d01cf710703990aa7d23b2 [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
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 Abraham45e4e362019-06-07 18:20:51 -070054 static constexpr std::chrono::nanoseconds mOffsetThresholdForNextVsync = 16ms;
Ady Abraham50c202a2019-03-14 11:44:38 -070055 static constexpr int mIterations = 100;
56};
57
58DispSyncSourceTest::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
64DispSyncSourceTest::~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
70void DispSyncSourceTest::onVSyncEvent(nsecs_t when) {
71 ALOGD("onVSyncEvent: %" PRId64, when);
72
73 mVSyncEventCallRecorder.recordCall(when);
74}
75
76void DispSyncSourceTest::createDispSync() {
77 mDispSync = std::make_unique<mock::DispSync>();
78}
79
80void DispSyncSourceTest::createDispSyncSource() {
81 createDispSync();
Ady Abraham45e4e362019-06-07 18:20:51 -070082 mDispSyncSource = std::make_unique<DispSyncSource>(mDispSync.get(), mPhaseOffset.count(),
83 mOffsetThresholdForNextVsync.count(), true,
Ady Abraham50c202a2019-03-14 11:44:38 -070084 "DispSyncSourceTest");
85 mDispSyncSource->setCallback(this);
86}
87
88/* ------------------------------------------------------------------------
89 * Test cases
90 */
91
92TEST_F(DispSyncSourceTest, createDispSync) {
93 createDispSync();
94 EXPECT_TRUE(mDispSync);
95}
96
97TEST_F(DispSyncSourceTest, createDispSyncSource) {
98 createDispSyncSource();
99 EXPECT_TRUE(mDispSyncSource);
100}
101
102TEST_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
111TEST_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
124TEST_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 Abraham50c202a2019-03-14 11:44:38 -0700147} // namespace
148} // namespace android