• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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 "net/test/test_connection_cost_observer.h"
6 
7 namespace net {
8 
9 TestConnectionCostObserver::TestConnectionCostObserver() = default;
10 
~TestConnectionCostObserver()11 TestConnectionCostObserver::~TestConnectionCostObserver() {
12   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
13 }
14 
OnConnectionCostChanged(NetworkChangeNotifier::ConnectionCost cost)15 void TestConnectionCostObserver::OnConnectionCostChanged(
16     NetworkChangeNotifier::ConnectionCost cost) {
17   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
18 
19   cost_changed_inputs_.push_back(cost);
20 
21   if (run_loop_) {
22     run_loop_->Quit();
23   }
24 }
25 
WaitForConnectionCostChanged()26 void TestConnectionCostObserver::WaitForConnectionCostChanged() {
27   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
28   run_loop_ = std::make_unique<base::RunLoop>();
29   run_loop_->Run();
30   run_loop_.reset();
31 }
32 
cost_changed_calls() const33 size_t TestConnectionCostObserver::cost_changed_calls() const {
34   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
35   return cost_changed_inputs_.size();
36 }
37 
38 std::vector<NetworkChangeNotifier::ConnectionCost>
cost_changed_inputs() const39 TestConnectionCostObserver::cost_changed_inputs() const {
40   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
41   return cost_changed_inputs_;
42 }
43 
44 NetworkChangeNotifier::ConnectionCost
last_cost_changed_input() const45 TestConnectionCostObserver::last_cost_changed_input() const {
46   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
47   CHECK_GT(cost_changed_inputs_.size(), 0u);
48   return cost_changed_inputs_.back();
49 }
50 
51 }  // namespace net
52