• 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 #ifndef NET_TEST_TEST_CONNECTION_COST_OBSERVER_H_
6 #define NET_TEST_TEST_CONNECTION_COST_OBSERVER_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/run_loop.h"
12 #include "base/sequence_checker.h"
13 #include "net/base/network_change_notifier.h"
14 
15 namespace net {
16 
17 class TestConnectionCostObserver final
18     : public NetworkChangeNotifier::ConnectionCostObserver {
19  public:
20   TestConnectionCostObserver();
21   ~TestConnectionCostObserver() final;
22 
23   void OnConnectionCostChanged(
24       NetworkChangeNotifier::ConnectionCost cost) final;
25 
26   void WaitForConnectionCostChanged();
27 
28   size_t cost_changed_calls() const;
29   std::vector<NetworkChangeNotifier::ConnectionCost> cost_changed_inputs()
30       const;
31   NetworkChangeNotifier::ConnectionCost last_cost_changed_input() const;
32 
33   TestConnectionCostObserver(const TestConnectionCostObserver&) = delete;
34   TestConnectionCostObserver& operator=(const TestConnectionCostObserver&) =
35       delete;
36 
37  private:
38   SEQUENCE_CHECKER(sequence_checker_);
39 
40   // Set and used to block in `WaitForConnectionCostChanged()` until the next
41   // cost changed event occurs.
42   std::unique_ptr<base::RunLoop> run_loop_;
43 
44   // Record each `OnConnectionCostChanged()` call.
45   std::vector<NetworkChangeNotifier::ConnectionCost> cost_changed_inputs_;
46 };
47 
48 }  // namespace net
49 
50 #endif  // NET_TEST_TEST_CONNECTION_COST_OBSERVER_H_
51