blob: 4d9aec6b66620c7830abe598701b510b5cfa4aa4 [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 Krulece588e312018-09-18 12:32:24 -070011#include "Scheduler/EventControlThread.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070012#include "Scheduler/EventThread.h"
13#include "Scheduler/Scheduler.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070014#include "mock/MockEventThread.h"
15
16using testing::_;
17using testing::Return;
18
19namespace android {
20
21class SchedulerTest : public testing::Test {
22protected:
Ana Krulec85c39af2018-12-26 17:29:57 -080023 class MockEventThreadConnection : public android::EventThreadConnection {
Ana Krulec0c8cd522018-08-31 12:27:28 -070024 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080025 explicit MockEventThreadConnection(EventThread* eventThread)
Dominik Laskowskif654d572018-12-20 11:03:06 -080026 : EventThreadConnection(eventThread, ResyncCallback()) {}
Ana Krulec0c8cd522018-08-31 12:27:28 -070027 ~MockEventThreadConnection() = default;
28
29 MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel));
30 MOCK_METHOD1(setVsyncRate, status_t(uint32_t count));
31 MOCK_METHOD0(requestNextVsync, void());
32 };
33
34 /**
35 * This mock Scheduler class uses implementation of mock::EventThread but keeps everything else
36 * the same.
37 */
38 class MockScheduler : public android::Scheduler {
39 public:
40 MockScheduler(std::unique_ptr<EventThread> eventThread)
Ana Krulece588e312018-09-18 12:32:24 -070041 : Scheduler([](bool) {}), mEventThread(std::move(eventThread)) {}
Ana Krulec0c8cd522018-08-31 12:27:28 -070042
43 std::unique_ptr<EventThread> makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -080044 const char* /* connectionName */, DispSync* /* dispSync */,
Ana Krulec0c8cd522018-08-31 12:27:28 -070045 nsecs_t /* phaseOffsetNs */,
Ana Krulec0c8cd522018-08-31 12:27:28 -070046 impl::EventThread::InterceptVSyncsCallback /* interceptCallback */) override {
47 return std::move(mEventThread);
48 }
49
50 MockScheduler() = default;
51 ~MockScheduler() override = default;
52
53 std::unique_ptr<EventThread> mEventThread;
54 };
55
56 SchedulerTest();
57 ~SchedulerTest() override;
58
59 sp<Scheduler::ConnectionHandle> mConnectionHandle;
Ana Krulec0c8cd522018-08-31 12:27:28 -070060 mock::EventThread* mEventThread;
61 std::unique_ptr<MockScheduler> mScheduler;
62 sp<MockEventThreadConnection> mEventThreadConnection;
Ana Krulec0c8cd522018-08-31 12:27:28 -070063};
64
65SchedulerTest::SchedulerTest() {
66 const ::testing::TestInfo* const test_info =
67 ::testing::UnitTest::GetInstance()->current_test_info();
68 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
69
70 std::unique_ptr<mock::EventThread> eventThread = std::make_unique<mock::EventThread>();
71 mEventThread = eventThread.get();
72 mScheduler = std::make_unique<MockScheduler>(std::move(eventThread));
Ana Krulec85c39af2018-12-26 17:29:57 -080073 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
74
75 mEventThreadConnection = new MockEventThreadConnection(mEventThread);
Ana Krulec0c8cd522018-08-31 12:27:28 -070076
77 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
78 // sure that call gets executed and returns an EventThread::Connection object.
Dominik Laskowskif654d572018-12-20 11:03:06 -080079 EXPECT_CALL(*mEventThread, createEventConnection(_))
Ana Krulec0c8cd522018-08-31 12:27:28 -070080 .WillRepeatedly(Return(mEventThreadConnection));
81
Dominik Laskowskif654d572018-12-20 11:03:06 -080082 mConnectionHandle = mScheduler->createConnection("appConnection", 16, ResyncCallback(),
83 impl::EventThread::InterceptVSyncsCallback());
84 EXPECT_TRUE(mConnectionHandle != nullptr);
Ana Krulec0c8cd522018-08-31 12:27:28 -070085}
86
87SchedulerTest::~SchedulerTest() {
88 const ::testing::TestInfo* const test_info =
89 ::testing::UnitTest::GetInstance()->current_test_info();
90 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
91}
92
93namespace {
94/* ------------------------------------------------------------------------
95 * Test cases
96 */
Ana Krulec0c8cd522018-08-31 12:27:28 -070097
98TEST_F(SchedulerTest, testNullPtr) {
99 // Passing a null pointer for ConnectionHandle is a valid argument. The code doesn't throw any
100 // exceptions, just gracefully continues.
101 sp<IDisplayEventConnection> returnedValue;
Dominik Laskowskif654d572018-12-20 11:03:06 -0800102 ASSERT_NO_FATAL_FAILURE(
103 returnedValue = mScheduler->createDisplayEventConnection(nullptr, ResyncCallback()));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700104 EXPECT_TRUE(returnedValue == nullptr);
105 EXPECT_TRUE(mScheduler->getEventThread(nullptr) == nullptr);
106 EXPECT_TRUE(mScheduler->getEventConnection(nullptr) == nullptr);
107 ASSERT_NO_FATAL_FAILURE(
108 mScheduler->hotplugReceived(nullptr, EventThread::DisplayType::Primary, false));
109 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(nullptr));
110 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(nullptr));
Yiwei Zhang5434a782018-12-05 18:06:32 -0800111 std::string testString;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700112 ASSERT_NO_FATAL_FAILURE(mScheduler->dump(nullptr, testString));
113 EXPECT_TRUE(testString == "");
114 ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(nullptr, 10));
115}
116
117TEST_F(SchedulerTest, invalidConnectionHandle) {
118 // Passing an invalid ConnectionHandle is a valid argument. The code doesn't throw any
119 // exceptions, just gracefully continues.
120 sp<Scheduler::ConnectionHandle> connectionHandle = new Scheduler::ConnectionHandle(20);
121
122 sp<IDisplayEventConnection> returnedValue;
Dominik Laskowskif654d572018-12-20 11:03:06 -0800123 ASSERT_NO_FATAL_FAILURE(
124 returnedValue =
125 mScheduler->createDisplayEventConnection(connectionHandle, ResyncCallback()));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700126 EXPECT_TRUE(returnedValue == nullptr);
127 EXPECT_TRUE(mScheduler->getEventThread(connectionHandle) == nullptr);
128 EXPECT_TRUE(mScheduler->getEventConnection(connectionHandle) == nullptr);
129
130 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
131 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
132 ASSERT_NO_FATAL_FAILURE(mScheduler->hotplugReceived(connectionHandle,
133 EventThread::DisplayType::Primary, false));
134
135 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
136 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(connectionHandle));
137
138 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
139 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(connectionHandle));
140
Yiwei Zhang5434a782018-12-05 18:06:32 -0800141 std::string testString;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700142 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
143 ASSERT_NO_FATAL_FAILURE(mScheduler->dump(connectionHandle, testString));
144 EXPECT_TRUE(testString == "");
145
146 EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0);
147 ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(connectionHandle, 10));
148}
149
150TEST_F(SchedulerTest, validConnectionHandle) {
151 sp<IDisplayEventConnection> returnedValue;
Dominik Laskowskif654d572018-12-20 11:03:06 -0800152 ASSERT_NO_FATAL_FAILURE(
153 returnedValue =
154 mScheduler->createDisplayEventConnection(mConnectionHandle, ResyncCallback()));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700155 EXPECT_TRUE(returnedValue != nullptr);
156 ASSERT_EQ(returnedValue, mEventThreadConnection);
157
158 EXPECT_TRUE(mScheduler->getEventThread(mConnectionHandle) != nullptr);
159 EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle) != nullptr);
160
161 EXPECT_CALL(*mEventThread, onHotplugReceived(EventThread::DisplayType::Primary, false))
162 .Times(1);
163 ASSERT_NO_FATAL_FAILURE(mScheduler->hotplugReceived(mConnectionHandle,
164 EventThread::DisplayType::Primary, false));
165
166 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
167 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(mConnectionHandle));
168
169 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
170 ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(mConnectionHandle));
171
Yiwei Zhang5434a782018-12-05 18:06:32 -0800172 std::string testString("dump");
Ana Krulec0c8cd522018-08-31 12:27:28 -0700173 EXPECT_CALL(*mEventThread, dump(testString)).Times(1);
174 ASSERT_NO_FATAL_FAILURE(mScheduler->dump(mConnectionHandle, testString));
175 EXPECT_TRUE(testString != "");
176
177 EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1);
178 ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(mConnectionHandle, 10));
179}
Ana Krulec0c8cd522018-08-31 12:27:28 -0700180} // namespace
Ana Krulec3084c052018-11-21 20:27:17 +0100181} // namespace android