1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92

base / task / sequence_manager / test / mock_time_message_pump.cc [blame]

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/task/sequence_manager/test/mock_time_message_pump.h"

#include <algorithm>
#include <ostream>

#include "base/auto_reset.h"
#include "base/notreached.h"
#include "base/test/simple_test_tick_clock.h"

namespace base {
namespace sequence_manager {

MockTimeMessagePump::MockTimeMessagePump(SimpleTestTickClock* clock)
    : clock_(clock) {}

MockTimeMessagePump::~MockTimeMessagePump() = default;

bool MockTimeMessagePump::MaybeAdvanceTime(TimeTicks target_time) {
  auto now = clock_->NowTicks();

  if (target_time <= now) {
    return true;
  }

  TimeTicks next_now;

  if (!target_time.is_max()) {
    next_now = std::min(allow_advance_until_, target_time);
  } else if (allow_advance_until_ == TimeTicks::Max()) {
    next_now = now;
  } else {
    next_now = allow_advance_until_;
  }

  if (now < next_now) {
    clock_->SetNowTicks(next_now);
    return true;
  }
  return false;
}

void MockTimeMessagePump::Run(Delegate* delegate) {
  AutoReset<bool> auto_reset_keep_running(&keep_running_, true);

  for (;;) {
    Delegate::NextWorkInfo info = delegate->DoWork();

    if (!keep_running_ || quit_after_do_some_work_) {
      break;
    }

    if (info.is_immediate()) {
      continue;
    }

    delegate->DoIdleWork();
    if (!keep_running_) {
      break;
    }

    if (MaybeAdvanceTime(info.delayed_run_time)) {
      continue;
    }

    next_wake_up_time_ = info.delayed_run_time;

    if (stop_when_message_pump_is_idle_) {
      return;
    }

    NOTREACHED() << "Pump would go to sleep. Probably not what you wanted, "
                    "consider rewriting your test.";
  }
}

void MockTimeMessagePump::Quit() {
  keep_running_ = false;
}

void MockTimeMessagePump::ScheduleWork() {}

void MockTimeMessagePump::ScheduleDelayedWork(
    const Delegate::NextWorkInfo& next_work_info) {
  next_wake_up_time_ = next_work_info.delayed_run_time;
}

}  // namespace sequence_manager
}  // namespace base