blob: cb6980ed1ab3f9b1ede9a3e7de558885c2ce160d [file] [log] [blame]
Ana Krulecafb45842019-02-13 13:33:03 -08001/*
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#pragma once
18
19#include <gmock/gmock.h>
Ady Abraham0f4a1b12019-06-04 16:04:04 -070020#include <gui/ISurfaceComposer.h>
Ana Krulecafb45842019-02-13 13:33:03 -080021
22#include "Scheduler/EventThread.h"
Ady Abraham09bd3922019-04-08 10:44:56 -070023#include "Scheduler/RefreshRateConfigs.h"
Ana Krulecafb45842019-02-13 13:33:03 -080024#include "Scheduler/Scheduler.h"
25
26namespace android {
27
28class TestableScheduler : public Scheduler {
29public:
Ady Abraham09bd3922019-04-08 10:44:56 -070030 TestableScheduler(const scheduler::RefreshRateConfigs& refreshRateConfig)
31 : Scheduler([](bool) {}, refreshRateConfig) {}
Ana Krulecafb45842019-02-13 13:33:03 -080032
33 // Creates EventThreadConnection with the given eventThread. Creates Scheduler::Connection
34 // and adds it to the list of connectins. Returns the ConnectionHandle for the
35 // Scheduler::Connection. This allows plugging in mock::EventThread.
36 sp<Scheduler::ConnectionHandle> addConnection(std::unique_ptr<EventThread> eventThread) {
37 sp<EventThreadConnection> eventThreadConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -070038 new EventThreadConnection(eventThread.get(), ResyncCallback(),
39 ISurfaceComposer::eConfigChangedSuppress);
Ana Krulecafb45842019-02-13 13:33:03 -080040 const int64_t id = sNextId++;
41 mConnections.emplace(id,
42 std::make_unique<Scheduler::Connection>(new ConnectionHandle(id),
43 eventThreadConnection,
44 std::move(eventThread)));
45 return mConnections[id]->handle;
46 }
47
48 /* ------------------------------------------------------------------------
49 * Read-write access to private data to set up preconditions and assert
50 * post-conditions.
51 */
52 auto& mutablePrimaryHWVsyncEnabled() { return mPrimaryHWVsyncEnabled; }
53 auto& mutableEventControlThread() { return mEventControlThread; }
54 auto& mutablePrimaryDispSync() { return mPrimaryDispSync; }
55 auto& mutableHWVsyncAvailable() { return mHWVsyncAvailable; }
56
57 ~TestableScheduler() {
58 // All these pointer and container clears help ensure that GMock does
59 // not report a leaked object, since the Scheduler instance may
60 // still be referenced by something despite our best efforts to destroy
61 // it after each test is done.
62 mutableEventControlThread().reset();
63 mutablePrimaryDispSync().reset();
64 mConnections.clear();
65 };
66};
67
68} // namespace android