File: tether_host_fetcher_impl_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (157 lines) | stat: -rw-r--r-- 5,381 bytes parent folder | download | duplicates (6)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/tether/tether_host_fetcher_impl.h"

#include <memory>
#include <optional>
#include <vector>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "chromeos/ash/components/multidevice/remote_device.h"
#include "chromeos/ash/components/multidevice/remote_device_ref.h"
#include "chromeos/ash/components/multidevice/remote_device_test_util.h"
#include "chromeos/ash/components/multidevice/software_feature.h"
#include "chromeos/ash/components/multidevice/software_feature_state.h"
#include "chromeos/ash/services/device_sync/public/cpp/fake_device_sync_client.h"
#include "chromeos/ash/services/multidevice_setup/public/cpp/fake_multidevice_setup_client.h"
#include "chromeos/ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::tether {

namespace {

class TestObserver : public TetherHostFetcher::Observer {
 public:
  TestObserver() = default;
  virtual ~TestObserver() = default;

  size_t num_updates() { return num_updates_; }

  // TetherHostFetcher::Observer:
  void OnTetherHostUpdated() override { ++num_updates_; }

 private:
  size_t num_updates_ = 0;
};

// This should be identical to TetherHostSource in tether_host_fetcher_impl.cc.
enum class TetherHostSource {
  UNKNOWN,
  MULTIDEVICE_SETUP_CLIENT,
  DEVICE_SYNC_CLIENT,
  REMOTE_DEVICE_PROVIDER
};

}  // namespace

class TetherHostFetcherImplTest : public testing::Test {
 public:
  TetherHostFetcherImplTest() {}

  TetherHostFetcherImplTest(const TetherHostFetcherImplTest&) = delete;
  TetherHostFetcherImplTest& operator=(const TetherHostFetcherImplTest&) =
      delete;

  void SetUp() override {
    fake_device_sync_client_ =
        std::make_unique<device_sync::FakeDeviceSyncClient>();
    fake_multidevice_setup_client_ =
        std::make_unique<multidevice_setup::FakeMultiDeviceSetupClient>();
  }

  void InitializeTest() {
    tether_host_fetcher_ = TetherHostFetcherImpl::Factory::Create(
        fake_device_sync_client_.get(), fake_multidevice_setup_client_.get());

    fake_device_sync_client_->NotifyReady();
    test_observer_ = std::make_unique<TestObserver>();
    tether_host_fetcher_->AddObserver(test_observer_.get());
  }

  void VerifyTetherHost(
      std::optional<multidevice::RemoteDeviceRef> expected_device) {
    std::optional<multidevice::RemoteDeviceRef> tether_host =
        tether_host_fetcher_->GetTetherHost();
    if (expected_device) {
      EXPECT_EQ(expected_device, tether_host);
    } else {
      EXPECT_EQ(std::nullopt, tether_host);
    }
  }

  void SetSyncedDevice(
      std::optional<multidevice::RemoteDeviceRef> remote_device) {
    if (!remote_device.has_value()) {
      fake_device_sync_client_->set_synced_devices(
          multidevice::RemoteDeviceRefList{});
      fake_multidevice_setup_client_->SetHostStatusWithDevice(
          std::make_pair(multidevice_setup::mojom::HostStatus::kNoEligibleHosts,
                         std::nullopt /* host_device */));
      fake_multidevice_setup_client_->SetFeatureState(
          multidevice_setup::mojom::Feature::kInstantTethering,
          multidevice_setup::mojom::FeatureState::
              kUnavailableNoVerifiedHost_NoEligibleHosts);
      return;
    }

    fake_device_sync_client_->set_synced_devices(
        multidevice::RemoteDeviceRefList{*remote_device});
    fake_multidevice_setup_client_->SetHostStatusWithDevice(std::make_pair(
        multidevice_setup::mojom::HostStatus::kHostVerified, remote_device));
    fake_multidevice_setup_client_->SetFeatureState(
        multidevice_setup::mojom::Feature::kInstantTethering,
        multidevice_setup::mojom::FeatureState::kEnabledByUser);
  }

  void NotifyNewDevicesSynced() {
    fake_device_sync_client_->NotifyNewDevicesSynced();
  }

  std::unique_ptr<TestObserver> test_observer_;

  std::unique_ptr<device_sync::FakeDeviceSyncClient> fake_device_sync_client_;
  std::unique_ptr<multidevice_setup::FakeMultiDeviceSetupClient>
      fake_multidevice_setup_client_;

  std::unique_ptr<TetherHostFetcher> tether_host_fetcher_;
};

TEST_F(TetherHostFetcherImplTest, TestHasSyncedTetherHosts) {
  multidevice::RemoteDeviceRef test_device =
      multidevice::CreateRemoteDeviceRefForTest();
  SetSyncedDevice(test_device);
  InitializeTest();
  VerifyTetherHost(test_device);

  EXPECT_EQ(0u, test_observer_->num_updates());

  // Update the list of devices to be empty.
  SetSyncedDevice(std::nullopt);
  NotifyNewDevicesSynced();
  VerifyTetherHost(std::nullopt);
  EXPECT_EQ(1u, test_observer_->num_updates());

  // Notify that the list has changed, even though it hasn't. There should be
  // no update.
  NotifyNewDevicesSynced();
  VerifyTetherHost(std::nullopt);
  EXPECT_EQ(1u, test_observer_->num_updates());

  // Update the list to include device 0 only.
  SetSyncedDevice(test_device);
  NotifyNewDevicesSynced();
  VerifyTetherHost(test_device);
  EXPECT_EQ(2u, test_observer_->num_updates());

  // Notify that the list has changed, even though it hasn't. There should be
  // no update.
  NotifyNewDevicesSynced();
  VerifyTetherHost(test_device);
  EXPECT_EQ(2u, test_observer_->num_updates());
}

}  // namespace ash::tether