1 // Copyright 2016 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma once 16 17 #include "aemu/base/async/DefaultLooper.h" 18 19 namespace android { 20 namespace base { 21 22 // A TestLooper is a Looper implementation that allows its user to inspect all 23 // internals. 24 // 25 class TestLooper : public DefaultLooper { 26 public: 27 using DefaultLooper::DefaultLooper; 28 29 // Override nowMs/nowNs to allow overriding virtual time. 30 Duration nowMs(ClockType clockType = ClockType::kHost) override; 31 DurationNs nowNs(ClockType clockType = ClockType::kHost) override; 32 33 void setVirtualTimeNs(DurationNs timeNs); 34 35 // Add some functions to uncover the internal state 36 const TimerSet& timers() const; 37 const TimerList& activeTimers() const; 38 const TimerList& pendingTimers() const; 39 40 const FdWatchSet& fdWatches() const; 41 const FdWatchList& pendingFdWatches() const; 42 43 bool exitRequested() const; 44 45 using DefaultLooper::runOneIterationWithDeadlineMs; 46 47 private: 48 DurationNs mVirtualTimeNs = 0; 49 }; 50 nowMs(ClockType clockType)51inline Looper::Duration TestLooper::nowMs(ClockType clockType) { 52 if (clockType == ClockType::kVirtual) { 53 return mVirtualTimeNs / 1000LL; 54 } else { 55 return DefaultLooper::nowMs(clockType); 56 } 57 } 58 nowNs(ClockType clockType)59inline Looper::DurationNs TestLooper::nowNs(ClockType clockType) { 60 if (clockType == ClockType::kVirtual) { 61 return mVirtualTimeNs; 62 } else { 63 return DefaultLooper::nowNs(clockType); 64 } 65 } 66 setVirtualTimeNs(DurationNs timeNs)67inline void TestLooper::setVirtualTimeNs(DurationNs timeNs) { 68 mVirtualTimeNs = timeNs; 69 } 70 timers()71inline const DefaultLooper::TimerSet& TestLooper::timers() const { 72 return mTimers; 73 } 74 activeTimers()75inline const DefaultLooper::TimerList& TestLooper::activeTimers() const { 76 return mActiveTimers; 77 } 78 pendingTimers()79inline const DefaultLooper::TimerList& TestLooper::pendingTimers() const { 80 return mPendingTimers; 81 } 82 fdWatches()83inline const DefaultLooper::FdWatchSet& TestLooper::fdWatches() const { 84 return mFdWatches; 85 } 86 pendingFdWatches()87inline const DefaultLooper::FdWatchList& TestLooper::pendingFdWatches() const { 88 return mPendingFdWatches; 89 } 90 exitRequested()91inline bool TestLooper::exitRequested() const { 92 return mForcedExit; 93 } 94 95 } // namespace base 96 } // namespace android 97