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