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