blob: e967742f8597b344b4a8d4f625f1bf1cc221e3fe [file] [log] [blame]
Ana Krulec0c8cd522018-08-31 12:27:28 -07001#undef LOG_TAG
2#define LOG_TAG "SchedulerUnittests"
3
4#include <gmock/gmock.h>
5#include <gtest/gtest.h>
6
7#include <log/log.h>
8
Ana Krulece588e312018-09-18 12:32:24 -07009#include <mutex>
10
Ana Krulec0c8cd522018-08-31 12:27:28 -070011#include "AsyncCallRecorder.h"
12#include "Scheduler/DispSync.h"
Ana Krulece588e312018-09-18 12:32:24 -070013#include "Scheduler/EventControlThread.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070014#include "Scheduler/EventThread.h"
15#include "Scheduler/Scheduler.h"
16#include "mock/MockDispSync.h"
17#include "mock/MockEventThread.h"
18
19using testing::_;
20using testing::Return;
21
22namespace android {
23
24class SchedulerTest : public testing::Test {
25protected:
26 class MockEventThreadConnection : public BnDisplayEventConnection {
27 public:
28 MockEventThreadConnection() = default;
29 ~MockEventThreadConnection() = default;
30
31 MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel));
32 MOCK_METHOD1(setVsyncRate, status_t(uint32_t count));
33 MOCK_METHOD0(requestNextVsync, void());
34 };
35
36 /**
37 * This mock Scheduler class uses implementation of mock::EventThread but keeps everything else
38 * the same.
39 */
40 class MockScheduler : public android::Scheduler {
41 public:
42 MockScheduler(std::unique_ptr<EventThread> eventThread)
Ana Krulece588e312018-09-18 12:32:24 -070043 : Scheduler([](bool) {}), mEventThread(std::move(eventThread)) {}
Ana Krulec0c8cd522018-08-31 12:27:28 -070044
45 std::unique_ptr<EventThread> makeEventThread(
Ana Krulec1f027912018-09-10 21:36:25 +000046 const std::string& /* connectionName */, DispSync* /* dispSync */,
Ana Krulec0c8cd522018-08-31 12:27:28 -070047 nsecs_t /* phaseOffsetNs */,
48 impl::EventThread::ResyncWithRateLimitCallback /* resyncCallback */,
49 impl::EventThread::InterceptVSyncsCallback /* interceptCallback */) override {
50 return std::move(mEventThread);
51 }
52
53 MockScheduler() = default;
54 ~MockScheduler() override = default;
55
56 std::unique_ptr<EventThread> mEventThread;
57 };
58
59 SchedulerTest();
60 ~SchedulerTest() override;
61
Ana Krulec3084c052018-11-21 20:27:17 +010062 int64_t calculateMedian(std::vector<int64_t>* v);
63
Ana Krulec0c8cd522018-08-31 12:27:28 -070064 sp<Scheduler::ConnectionHandle> mConnectionHandle;
65 mock::DispSync* mPrimaryDispSync = new mock::DispSync();
66 mock::EventThread* mEventThread;
67 std::unique_ptr<MockScheduler> mScheduler;
68 sp<MockEventThreadConnection> mEventThreadConnection;
69
70 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
71 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
72};
73
74SchedulerTest::SchedulerTest() {
75 const ::testing::TestInfo* const test_info =
76 ::testing::UnitTest::GetInstance()->current_test_info();
77 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
78
79 std::unique_ptr<mock::EventThread> eventThread = std::make_unique<mock::EventThread>();
80 mEventThread = eventThread.get();
81 mScheduler = std::make_unique<MockScheduler>(std::move(eventThread));
82 mEventThreadConnection = new MockEventThreadConnection();
83
84 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
85 // sure that call gets executed and returns an EventThread::Connection object.
86 EXPECT_CALL(*mEventThread, createEventConnection())
87 .WillRepeatedly(Return(mEventThreadConnection));
88
Ana Krulece588e312018-09-18 12:32:24 -070089 mConnectionHandle =
90 mScheduler->createConnection("appConnection", 16, mResyncCallRecorder.getInvocable(),
91 mInterceptVSyncCallRecorder.getInvocable());
Ana Krulec0c8cd522018-08-31 12:27:28 -070092}
93
94SchedulerTest::~SchedulerTest() {
95 const ::testing::TestInfo* const test_info =
96 ::testing::UnitTest::GetInstance()->current_test_info();
97 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
98}
99
Ana Krulec3084c052018-11-21 20:27:17 +0100100int64_t SchedulerTest::calculateMedian(std::vector<int64_t>* v) {
101 return mScheduler->calculateMedian(v);
102}
103
Ana Krulec0c8cd522018-08-31 12:27:28 -0700104namespace {
105/* ------------------------------------------------------------------------
106 * Test cases
107 */
108TEST_F(SchedulerTest, canCreateAndDestroyTest) {
109 EXPECT_FALSE(mResyncCallRecorder.waitForCall().has_value());
110 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall().has_value());
111 EXPECT_EQ(0, mConnectionHandle->id);
112}
113
114TEST_F(SchedulerTest, testNullPtr) {
115 // Passing a null pointer for ConnectionHandle is a valid argument. The code doesn't throw any
116 // exceptions, just gracefully continues.
117 sp<IDisplayEventConnection> returnedValue;
118 ASSERT_NO_FATAL_FAILURE(returnedValue = mScheduler->createDisplayEventConnection(nullptr));
119 EXPECT_TRUE(returnedValue == nullptr);
120 EXPECT_TRUE(mScheduler->getEventThread(nullptr) == nullptr);
121 EXPECT_TRUE(mScheduler->getEventConnection(nullptr) == nullptr);
122 ASSERT_NO_FATAL_FAILURE(
123 mScheduler->hotplugReceived(nullptr, EventThread::DisplayType::Primary, false));
124 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(nullptr));
125 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(nullptr));
126 String8 testString;
127 ASSERT_NO_FATAL_FAILURE(mScheduler->dump(nullptr, testString));
128 EXPECT_TRUE(testString == "");
129 ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(nullptr, 10));
130}
131
132TEST_F(SchedulerTest, invalidConnectionHandle) {
133 // Passing an invalid ConnectionHandle is a valid argument. The code doesn't throw any
134 // exceptions, just gracefully continues.
135 sp<Scheduler::ConnectionHandle> connectionHandle = new Scheduler::ConnectionHandle(20);
136
137 sp<IDisplayEventConnection> returnedValue;
138 ASSERT_NO_FATAL_FAILURE(returnedValue =
139 mScheduler->createDisplayEventConnection(connectionHandle));
140 EXPECT_TRUE(returnedValue == nullptr);
141 EXPECT_TRUE(mScheduler->getEventThread(connectionHandle) == nullptr);
142 EXPECT_TRUE(mScheduler->getEventConnection(connectionHandle) == nullptr);
143
144 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
145 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
146 ASSERT_NO_FATAL_FAILURE(mScheduler->hotplugReceived(connectionHandle,
147 EventThread::DisplayType::Primary, false));
148
149 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
150 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(connectionHandle));
151
152 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
153 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(connectionHandle));
154
155 String8 testString;
156 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
157 ASSERT_NO_FATAL_FAILURE(mScheduler->dump(connectionHandle, testString));
158 EXPECT_TRUE(testString == "");
159
160 EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0);
161 ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(connectionHandle, 10));
162}
163
164TEST_F(SchedulerTest, validConnectionHandle) {
165 sp<IDisplayEventConnection> returnedValue;
166 ASSERT_NO_FATAL_FAILURE(returnedValue =
167 mScheduler->createDisplayEventConnection(mConnectionHandle));
168 EXPECT_TRUE(returnedValue != nullptr);
169 ASSERT_EQ(returnedValue, mEventThreadConnection);
170
171 EXPECT_TRUE(mScheduler->getEventThread(mConnectionHandle) != nullptr);
172 EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle) != nullptr);
173
174 EXPECT_CALL(*mEventThread, onHotplugReceived(EventThread::DisplayType::Primary, false))
175 .Times(1);
176 ASSERT_NO_FATAL_FAILURE(mScheduler->hotplugReceived(mConnectionHandle,
177 EventThread::DisplayType::Primary, false));
178
179 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
180 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(mConnectionHandle));
181
182 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
183 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(mConnectionHandle));
184
185 String8 testString("dump");
186 EXPECT_CALL(*mEventThread, dump(testString)).Times(1);
187 ASSERT_NO_FATAL_FAILURE(mScheduler->dump(mConnectionHandle, testString));
188 EXPECT_TRUE(testString != "");
189
190 EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1);
191 ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(mConnectionHandle, 10));
192}
193
Ana Krulec3084c052018-11-21 20:27:17 +0100194TEST_F(SchedulerTest, calculateMedian) {
195 std::vector<int64_t> testVector;
196 // Calling the function on empty vector returns 0.
197 EXPECT_EQ(0, calculateMedian(&testVector));
198
199 testVector.push_back(33);
200 EXPECT_EQ(33, calculateMedian(&testVector));
201 testVector.push_back(33);
202 testVector.push_back(33);
203 EXPECT_EQ(33, calculateMedian(&testVector));
204 testVector.push_back(42);
205 EXPECT_EQ(33, calculateMedian(&testVector));
206 testVector.push_back(33);
207 EXPECT_EQ(33, calculateMedian(&testVector));
208 testVector.push_back(42);
209 EXPECT_EQ(33, calculateMedian(&testVector));
210 testVector.push_back(42);
211 EXPECT_EQ(33, calculateMedian(&testVector));
212 testVector.push_back(42);
213 EXPECT_EQ(42, calculateMedian(&testVector));
214 testVector.push_back(60);
215 EXPECT_EQ(42, calculateMedian(&testVector));
216 testVector.push_back(60);
217 EXPECT_EQ(42, calculateMedian(&testVector));
218 testVector.push_back(33);
219 EXPECT_EQ(42, calculateMedian(&testVector));
220 testVector.push_back(33);
221 EXPECT_EQ(42, calculateMedian(&testVector));
222 testVector.push_back(33);
223 EXPECT_EQ(33, calculateMedian(&testVector));
224}
225
Ana Krulec0c8cd522018-08-31 12:27:28 -0700226} // namespace
Ana Krulec3084c052018-11-21 20:27:17 +0100227} // namespace android