| 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" | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 22 | #include "Scheduler/RefreshRateConfigs.h" | 
| Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 23 | #include "Scheduler/Scheduler.h" | 
 | 24 |  | 
 | 25 | namespace android { | 
 | 26 |  | 
 | 27 | class TestableScheduler : public Scheduler { | 
 | 28 | public: | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 29 |     TestableScheduler(const scheduler::RefreshRateConfigs& refreshRateConfig) | 
 | 30 |           : Scheduler([](bool) {}, refreshRateConfig) {} | 
| Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 31 |  | 
 | 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 = | 
| Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 37 |                 new EventThreadConnection(eventThread.get(), ResyncCallback()); | 
| Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame] | 38 |         const int64_t id = sNextId++; | 
 | 39 |         mConnections.emplace(id, | 
 | 40 |                              std::make_unique<Scheduler::Connection>(new ConnectionHandle(id), | 
 | 41 |                                                                      eventThreadConnection, | 
 | 42 |                                                                      std::move(eventThread))); | 
 | 43 |         return mConnections[id]->handle; | 
 | 44 |     } | 
 | 45 |  | 
 | 46 |     /* ------------------------------------------------------------------------ | 
 | 47 |      * Read-write access to private data to set up preconditions and assert | 
 | 48 |      * post-conditions. | 
 | 49 |      */ | 
 | 50 |     auto& mutablePrimaryHWVsyncEnabled() { return mPrimaryHWVsyncEnabled; } | 
 | 51 |     auto& mutableEventControlThread() { return mEventControlThread; } | 
 | 52 |     auto& mutablePrimaryDispSync() { return mPrimaryDispSync; } | 
 | 53 |     auto& mutableHWVsyncAvailable() { return mHWVsyncAvailable; } | 
 | 54 |  | 
 | 55 |     ~TestableScheduler() { | 
 | 56 |         // All these pointer and container clears help ensure that GMock does | 
 | 57 |         // not report a leaked object, since the Scheduler instance may | 
 | 58 |         // still be referenced by something despite our best efforts to destroy | 
 | 59 |         // it after each test is done. | 
 | 60 |         mutableEventControlThread().reset(); | 
 | 61 |         mutablePrimaryDispSync().reset(); | 
 | 62 |         mConnections.clear(); | 
 | 63 |     }; | 
 | 64 | }; | 
 | 65 |  | 
 | 66 | } // namespace android |