1 // Copyright 2019 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/task/sequence_manager/test/mock_time_message_pump.h" 6 7 #include <algorithm> 8 #include <ostream> 9 10 #include "base/auto_reset.h" 11 #include "base/notreached.h" 12 #include "base/test/simple_test_tick_clock.h" 13 14 namespace base { 15 namespace sequence_manager { 16 MockTimeMessagePump(SimpleTestTickClock * clock)17MockTimeMessagePump::MockTimeMessagePump(SimpleTestTickClock* clock) 18 : clock_(clock) {} 19 ~MockTimeMessagePump()20MockTimeMessagePump::~MockTimeMessagePump() {} 21 MaybeAdvanceTime(TimeTicks target_time)22bool MockTimeMessagePump::MaybeAdvanceTime(TimeTicks target_time) { 23 auto now = clock_->NowTicks(); 24 25 if (target_time <= now) 26 return true; 27 28 TimeTicks next_now; 29 30 if (!target_time.is_max()) { 31 next_now = std::min(allow_advance_until_, target_time); 32 } else if (allow_advance_until_ == TimeTicks::Max()) { 33 next_now = now; 34 } else { 35 next_now = allow_advance_until_; 36 } 37 38 if (now < next_now) { 39 clock_->SetNowTicks(next_now); 40 return true; 41 } 42 return false; 43 } 44 Run(Delegate * delegate)45void MockTimeMessagePump::Run(Delegate* delegate) { 46 AutoReset<bool> auto_reset_keep_running(&keep_running_, true); 47 48 for (;;) { 49 Delegate::NextWorkInfo info = delegate->DoWork(); 50 51 if (!keep_running_ || quit_after_do_some_work_) 52 break; 53 54 if (info.is_immediate()) 55 continue; 56 57 bool have_immediate_work = delegate->DoIdleWork(); 58 59 if (!keep_running_) 60 break; 61 62 if (have_immediate_work) 63 continue; 64 65 if (MaybeAdvanceTime(info.delayed_run_time)) 66 continue; 67 68 next_wake_up_time_ = info.delayed_run_time; 69 70 if (stop_when_message_pump_is_idle_) 71 return; 72 73 NOTREACHED() << "Pump would go to sleep. Probably not what you wanted, " 74 "consider rewriting your test."; 75 } 76 } 77 Quit()78void MockTimeMessagePump::Quit() { 79 keep_running_ = false; 80 } 81 ScheduleWork()82void MockTimeMessagePump::ScheduleWork() {} 83 ScheduleDelayedWork(const Delegate::NextWorkInfo & next_work_info)84void MockTimeMessagePump::ScheduleDelayedWork( 85 const Delegate::NextWorkInfo& next_work_info) { 86 next_wake_up_time_ = next_work_info.delayed_run_time; 87 } 88 89 } // namespace sequence_manager 90 } // namespace base 91