Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: chromeos/components/tether/active_host_unittest.cc

Issue 2741253002: [CrOS Tether] Create KeepAliveScheduler, a class which schedules keep-alive tickles to be sent to a… (Closed)
Patch Set: Add missing dependency. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromeos/components/tether/active_host.h" 5 #include "chromeos/components/tether/active_host.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 22 matching lines...) Expand all
33 other.remote_device && *remote_device == *other.remote_device; 33 other.remote_device && *remote_device == *other.remote_device;
34 } else { 34 } else {
35 devices_equal = !other.remote_device; 35 devices_equal = !other.remote_device;
36 } 36 }
37 37
38 return active_host_status == other.active_host_status && devices_equal && 38 return active_host_status == other.active_host_status && devices_equal &&
39 wifi_network_id == other.wifi_network_id; 39 wifi_network_id == other.wifi_network_id;
40 } 40 }
41 }; 41 };
42 42
43 class TestObserver : public ActiveHost::Observer {
44 public:
45 void OnActiveHostChanged(
46 ActiveHost::ActiveHostStatus active_host_status,
47 std::unique_ptr<cryptauth::RemoteDevice> active_host_device,
48 const std::string& wifi_network_id) override {
49 host_changed_updates_.push_back(GetActiveHostResult{
50 active_host_status, std::move(active_host_device), wifi_network_id});
51 }
52
53 std::vector<GetActiveHostResult>& host_changed_updates() {
54 return host_changed_updates_;
55 }
56
57 private:
58 std::vector<GetActiveHostResult> host_changed_updates_;
59 };
60
43 } // namespace 61 } // namespace
44 62
45 class ActiveHostTest : public testing::Test { 63 class ActiveHostTest : public testing::Test {
46 public: 64 public:
47 ActiveHostTest() : test_devices_(cryptauth::GenerateTestRemoteDevices(4)) {} 65 ActiveHostTest() : test_devices_(cryptauth::GenerateTestRemoteDevices(4)) {}
48 66
49 void SetUp() override { 67 void SetUp() override {
50 get_active_host_results_.clear(); 68 get_active_host_results_.clear();
51 69
52 test_pref_service_ = base::MakeUnique<TestingPrefServiceSimple>(); 70 test_pref_service_ = base::MakeUnique<TestingPrefServiceSimple>();
53 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>( 71 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>(
54 test_devices_, false /* synchronously_reply_with_results */); 72 test_devices_, false /* synchronously_reply_with_results */);
55 73
56 ActiveHost::RegisterPrefs(test_pref_service_->registry()); 74 ActiveHost::RegisterPrefs(test_pref_service_->registry());
57 active_host_ = base::MakeUnique<ActiveHost>(fake_tether_host_fetcher_.get(), 75 active_host_ = base::MakeUnique<ActiveHost>(fake_tether_host_fetcher_.get(),
58 test_pref_service_.get()); 76 test_pref_service_.get());
77
78 test_observer_ = base::WrapUnique(new TestObserver);
79 active_host_->AddObserver(test_observer_.get());
59 } 80 }
60 81
61 void OnActiveHostFetched(ActiveHost::ActiveHostStatus active_host_status, 82 void OnActiveHostFetched(ActiveHost::ActiveHostStatus active_host_status,
62 std::unique_ptr<cryptauth::RemoteDevice> active_host, 83 std::unique_ptr<cryptauth::RemoteDevice> active_host,
63 const std::string& wifi_network_id) { 84 const std::string& wifi_network_id) {
64 get_active_host_results_.push_back(GetActiveHostResult{ 85 get_active_host_results_.push_back(GetActiveHostResult{
65 active_host_status, std::move(active_host), wifi_network_id}); 86 active_host_status, std::move(active_host), wifi_network_id});
66 } 87 }
67 88
68 void VerifyActiveHostDisconnected() { 89 void VerifyActiveHostDisconnected() {
69 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED, 90 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED,
70 active_host_->GetActiveHostStatus()); 91 active_host_->GetActiveHostStatus());
71 EXPECT_TRUE(active_host_->GetActiveHostDeviceId().empty()); 92 EXPECT_TRUE(active_host_->GetActiveHostDeviceId().empty());
72 EXPECT_TRUE(active_host_->GetWifiNetworkId().empty()); 93 EXPECT_TRUE(active_host_->GetWifiNetworkId().empty());
73 94
74 active_host_->GetActiveHost(base::Bind(&ActiveHostTest::OnActiveHostFetched, 95 active_host_->GetActiveHost(base::Bind(&ActiveHostTest::OnActiveHostFetched,
75 base::Unretained(this))); 96 base::Unretained(this)));
76 fake_tether_host_fetcher_->InvokePendingCallbacks(); 97 fake_tether_host_fetcher_->InvokePendingCallbacks();
77 ASSERT_EQ(1u, get_active_host_results_.size()); 98 ASSERT_EQ(1u, get_active_host_results_.size());
78 EXPECT_EQ((GetActiveHostResult{ActiveHost::ActiveHostStatus::DISCONNECTED, 99 EXPECT_EQ((GetActiveHostResult{ActiveHost::ActiveHostStatus::DISCONNECTED,
79 nullptr, std::string()}), 100 nullptr, std::string()}),
80 get_active_host_results_[0]); 101 get_active_host_results_[0]);
81 } 102 }
82 103
83 const std::vector<cryptauth::RemoteDevice> test_devices_; 104 const std::vector<cryptauth::RemoteDevice> test_devices_;
84 105
85 std::unique_ptr<TestingPrefServiceSimple> test_pref_service_; 106 std::unique_ptr<TestingPrefServiceSimple> test_pref_service_;
86 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; 107 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_;
108 std::unique_ptr<TestObserver> test_observer_;
87 109
88 std::vector<GetActiveHostResult> get_active_host_results_; 110 std::vector<GetActiveHostResult> get_active_host_results_;
89 111
90 std::unique_ptr<ActiveHost> active_host_; 112 std::unique_ptr<ActiveHost> active_host_;
91 113
92 private: 114 private:
93 DISALLOW_COPY_AND_ASSIGN(ActiveHostTest); 115 DISALLOW_COPY_AND_ASSIGN(ActiveHostTest);
94 }; 116 };
95 117
96 TEST_F(ActiveHostTest, TestDefaultValues) { 118 TEST_F(ActiveHostTest, TestDefaultValues) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 // Now, finish the fech. 187 // Now, finish the fech.
166 fake_tether_host_fetcher_->InvokePendingCallbacks(); 188 fake_tether_host_fetcher_->InvokePendingCallbacks();
167 189
168 // The resulting callback should indicate that there is no active host. 190 // The resulting callback should indicate that there is no active host.
169 ASSERT_EQ(1u, get_active_host_results_.size()); 191 ASSERT_EQ(1u, get_active_host_results_.size());
170 EXPECT_EQ((GetActiveHostResult{ActiveHost::ActiveHostStatus::DISCONNECTED, 192 EXPECT_EQ((GetActiveHostResult{ActiveHost::ActiveHostStatus::DISCONNECTED,
171 nullptr, ""}), 193 nullptr, ""}),
172 get_active_host_results_[0]); 194 get_active_host_results_[0]);
173 } 195 }
174 196
197 TEST_F(ActiveHostTest, TestObserverCalls) {
198 // Start as DISCONNECTED.
199 EXPECT_FALSE(test_observer_->host_changed_updates().size());
200
201 // Go to DISCONNECTED again. This should not cause an observer callback to be
202 // invoked.
203 active_host_->SetActiveHostDisconnected();
204 fake_tether_host_fetcher_->InvokePendingCallbacks();
205 EXPECT_FALSE(test_observer_->host_changed_updates().size());
206
207 // Transition to CONNECTING.
208 active_host_->SetActiveHostConnecting(test_devices_[0].GetDeviceId());
209 fake_tether_host_fetcher_->InvokePendingCallbacks();
210 EXPECT_EQ(1u, test_observer_->host_changed_updates().size());
211 EXPECT_EQ(
212 (GetActiveHostResult{ActiveHost::ActiveHostStatus::CONNECTING,
213 std::shared_ptr<cryptauth::RemoteDevice>(
214 new cryptauth::RemoteDevice(test_devices_[0])),
215 ""}),
216 test_observer_->host_changed_updates()[0]);
217
218 // Transition to CONNECTED.
219 active_host_->SetActiveHostConnected(test_devices_[0].GetDeviceId(),
220 "wifiNetworkId");
221 fake_tether_host_fetcher_->InvokePendingCallbacks();
222 EXPECT_EQ(2u, test_observer_->host_changed_updates().size());
223 EXPECT_EQ(
224 (GetActiveHostResult{ActiveHost::ActiveHostStatus::CONNECTED,
225 std::shared_ptr<cryptauth::RemoteDevice>(
226 new cryptauth::RemoteDevice(test_devices_[0])),
227 "wifiNetworkId"}),
228 test_observer_->host_changed_updates()[1]);
229
230 // Transition to DISCONNECTED.
231 active_host_->SetActiveHostDisconnected();
232 fake_tether_host_fetcher_->InvokePendingCallbacks();
233 EXPECT_EQ(3u, test_observer_->host_changed_updates().size());
234 EXPECT_EQ((GetActiveHostResult{ActiveHost::ActiveHostStatus::DISCONNECTED,
235 nullptr, ""}),
236 test_observer_->host_changed_updates()[2]);
237 }
238
175 } // namespace tether 239 } // namespace tether
176 240
177 } // namespace cryptauth 241 } // namespace cryptauth
OLDNEW
« no previous file with comments | « chromeos/components/tether/active_host.cc ('k') | chromeos/components/tether/fake_active_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698