1 #undef LOG_TAG
2 #define LOG_TAG "SchedulerUnittests"
3 
4 #include <gmock/gmock.h>
5 #include <gtest/gtest.h>
6 #include <log/log.h>
7 
8 #include <mutex>
9 
10 #include "Scheduler/EventControlThread.h"
11 #include "Scheduler/EventThread.h"
12 #include "Scheduler/RefreshRateConfigs.h"
13 #include "Scheduler/Scheduler.h"
14 #include "mock/MockEventThread.h"
15 
16 using testing::_;
17 using testing::Return;
18 
19 namespace android {
20 
21 constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID = 999;
22 
23 class SchedulerTest : public testing::Test {
24 protected:
25     class MockEventThreadConnection : public android::EventThreadConnection {
26     public:
MockEventThreadConnection(EventThread * eventThread)27         explicit MockEventThreadConnection(EventThread* eventThread)
28               : EventThreadConnection(eventThread, ResyncCallback(),
29                                       ISurfaceComposer::eConfigChangedSuppress) {}
30         ~MockEventThreadConnection() = default;
31 
32         MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel));
33         MOCK_METHOD1(setVsyncRate, status_t(uint32_t count));
34         MOCK_METHOD0(requestNextVsync, void());
35     };
36 
37     std::unique_ptr<scheduler::RefreshRateConfigs> mRefreshRateConfigs;
38 
39     /**
40      * This mock Scheduler class uses implementation of mock::EventThread but keeps everything else
41      * the same.
42      */
43     class MockScheduler : public android::Scheduler {
44     public:
MockScheduler(const scheduler::RefreshRateConfigs & refreshRateConfigs,std::unique_ptr<EventThread> eventThread)45         MockScheduler(const scheduler::RefreshRateConfigs& refreshRateConfigs,
46                       std::unique_ptr<EventThread> eventThread)
47               : Scheduler([](bool) {}, refreshRateConfigs), mEventThread(std::move(eventThread)) {}
48 
makeEventThread(const char *,DispSync *,nsecs_t,nsecs_t,impl::EventThread::InterceptVSyncsCallback)49         std::unique_ptr<EventThread> makeEventThread(
50                 const char* /* connectionName */, DispSync* /* dispSync */,
51                 nsecs_t /* phaseOffsetNs */, nsecs_t /* offsetThresholdForNextVsync */,
52                 impl::EventThread::InterceptVSyncsCallback /* interceptCallback */) override {
53             return std::move(mEventThread);
54         }
55 
56         MockScheduler() = default;
57         ~MockScheduler() override = default;
58 
59         std::unique_ptr<EventThread> mEventThread;
60     };
61 
62     SchedulerTest();
63     ~SchedulerTest() override;
64 
65     sp<Scheduler::ConnectionHandle> mConnectionHandle;
66     mock::EventThread* mEventThread;
67     std::unique_ptr<MockScheduler> mScheduler;
68     sp<MockEventThreadConnection> mEventThreadConnection;
69 };
70 
SchedulerTest()71 SchedulerTest::SchedulerTest() {
72     const ::testing::TestInfo* const test_info =
73             ::testing::UnitTest::GetInstance()->current_test_info();
74     ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
75 
76     std::vector<scheduler::RefreshRateConfigs::InputConfig> configs{{/*hwcId=*/0, 16666667}};
77     mRefreshRateConfigs =
78             std::make_unique<scheduler::RefreshRateConfigs>(/*refreshRateSwitching=*/false, configs,
79                                                             /*currentConfig=*/0);
80 
81     std::unique_ptr<mock::EventThread> eventThread = std::make_unique<mock::EventThread>();
82     mEventThread = eventThread.get();
83     mScheduler = std::make_unique<MockScheduler>(*mRefreshRateConfigs, std::move(eventThread));
84     EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
85 
86     mEventThreadConnection = new MockEventThreadConnection(mEventThread);
87 
88     // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
89     // sure that call gets executed and returns an EventThread::Connection object.
90     EXPECT_CALL(*mEventThread, createEventConnection(_, _))
91             .WillRepeatedly(Return(mEventThreadConnection));
92 
93     mConnectionHandle = mScheduler->createConnection("appConnection", 16, 16,
94                                                      impl::EventThread::InterceptVSyncsCallback());
95     EXPECT_TRUE(mConnectionHandle != nullptr);
96 }
97 
~SchedulerTest()98 SchedulerTest::~SchedulerTest() {
99     const ::testing::TestInfo* const test_info =
100             ::testing::UnitTest::GetInstance()->current_test_info();
101     ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
102 }
103 
104 namespace {
105 /* ------------------------------------------------------------------------
106  * Test cases
107  */
108 
TEST_F(SchedulerTest,testNullPtr)109 TEST_F(SchedulerTest, testNullPtr) {
110     // Passing a null pointer for ConnectionHandle is a valid argument. The code doesn't throw any
111     // exceptions, just gracefully continues.
112     sp<IDisplayEventConnection> returnedValue;
113     ASSERT_NO_FATAL_FAILURE(
114             returnedValue =
115                     mScheduler->createDisplayEventConnection(nullptr,
116                                                              ISurfaceComposer::
117                                                                      eConfigChangedSuppress));
118     EXPECT_TRUE(returnedValue == nullptr);
119     EXPECT_TRUE(mScheduler->getEventThread(nullptr) == nullptr);
120     EXPECT_TRUE(mScheduler->getEventConnection(nullptr) == nullptr);
121     ASSERT_NO_FATAL_FAILURE(mScheduler->hotplugReceived(nullptr, PHYSICAL_DISPLAY_ID, false));
122     ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(nullptr));
123     ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(nullptr));
124     std::string testString;
125     ASSERT_NO_FATAL_FAILURE(mScheduler->dump(nullptr, testString));
126     EXPECT_TRUE(testString == "");
127     ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(nullptr, 10));
128 }
129 
TEST_F(SchedulerTest,invalidConnectionHandle)130 TEST_F(SchedulerTest, invalidConnectionHandle) {
131     // Passing an invalid ConnectionHandle is a valid argument. The code doesn't throw any
132     // exceptions, just gracefully continues.
133     sp<Scheduler::ConnectionHandle> connectionHandle = new Scheduler::ConnectionHandle(20);
134 
135     sp<IDisplayEventConnection> returnedValue;
136     ASSERT_NO_FATAL_FAILURE(
137             returnedValue =
138                     mScheduler->createDisplayEventConnection(connectionHandle,
139                                                              ISurfaceComposer::
140                                                                      eConfigChangedSuppress));
141     EXPECT_TRUE(returnedValue == nullptr);
142     EXPECT_TRUE(mScheduler->getEventThread(connectionHandle) == nullptr);
143     EXPECT_TRUE(mScheduler->getEventConnection(connectionHandle) == nullptr);
144 
145     // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
146     EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
147     ASSERT_NO_FATAL_FAILURE(
148             mScheduler->hotplugReceived(connectionHandle, PHYSICAL_DISPLAY_ID, false));
149 
150     EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
151     ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(connectionHandle));
152 
153     EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
154     ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(connectionHandle));
155 
156     std::string testString;
157     EXPECT_CALL(*mEventThread, dump(_)).Times(0);
158     ASSERT_NO_FATAL_FAILURE(mScheduler->dump(connectionHandle, testString));
159     EXPECT_TRUE(testString == "");
160 
161     EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0);
162     ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(connectionHandle, 10));
163 }
164 
TEST_F(SchedulerTest,validConnectionHandle)165 TEST_F(SchedulerTest, validConnectionHandle) {
166     sp<IDisplayEventConnection> returnedValue;
167     ASSERT_NO_FATAL_FAILURE(
168             returnedValue =
169                     mScheduler->createDisplayEventConnection(mConnectionHandle,
170                                                              ISurfaceComposer::
171                                                                      eConfigChangedSuppress));
172     EXPECT_TRUE(returnedValue != nullptr);
173     ASSERT_EQ(returnedValue, mEventThreadConnection);
174 
175     EXPECT_TRUE(mScheduler->getEventThread(mConnectionHandle) != nullptr);
176     EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle) != nullptr);
177 
178     EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1);
179     ASSERT_NO_FATAL_FAILURE(
180             mScheduler->hotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false));
181 
182     EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
183     ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenAcquired(mConnectionHandle));
184 
185     EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
186     ASSERT_NO_FATAL_FAILURE(mScheduler->onScreenReleased(mConnectionHandle));
187 
188     std::string testString("dump");
189     EXPECT_CALL(*mEventThread, dump(testString)).Times(1);
190     ASSERT_NO_FATAL_FAILURE(mScheduler->dump(mConnectionHandle, testString));
191     EXPECT_TRUE(testString != "");
192 
193     EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1);
194     ASSERT_NO_FATAL_FAILURE(mScheduler->setPhaseOffset(mConnectionHandle, 10));
195 }
196 } // namespace
197 } // namespace android
198