OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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) { | |
Jeremy Klein
2017/03/10 23:58:08
Can we also add a test which goes through the earl
Kyle Horimoto
2017/03/11 00:03:09
Done.
| |
198 // Start as DISCONNECTED. | |
199 EXPECT_FALSE(test_observer_->host_changed_updates().size()); | |
Jeremy Klein
2017/03/10 23:58:08
Is EXPECT_FALSE the standard chrome practice for "
Kyle Horimoto
2017/03/11 00:03:09
Yeah - I don't really like it either :(
| |
200 | |
201 // Transition to CONNECTING. | |
202 active_host_->SetActiveHostConnecting(test_devices_[0].GetDeviceId()); | |
203 fake_tether_host_fetcher_->InvokePendingCallbacks(); | |
204 EXPECT_EQ(1u, test_observer_->host_changed_updates().size()); | |
205 EXPECT_EQ( | |
206 (GetActiveHostResult{ActiveHost::ActiveHostStatus::CONNECTING, | |
207 std::shared_ptr<cryptauth::RemoteDevice>( | |
208 new cryptauth::RemoteDevice(test_devices_[0])), | |
209 ""}), | |
210 test_observer_->host_changed_updates()[0]); | |
211 | |
212 // Transition to CONNECTED. | |
213 active_host_->SetActiveHostConnected(test_devices_[0].GetDeviceId(), | |
214 "wifiNetworkId"); | |
215 fake_tether_host_fetcher_->InvokePendingCallbacks(); | |
216 EXPECT_EQ(2u, test_observer_->host_changed_updates().size()); | |
217 EXPECT_EQ( | |
218 (GetActiveHostResult{ActiveHost::ActiveHostStatus::CONNECTED, | |
219 std::shared_ptr<cryptauth::RemoteDevice>( | |
220 new cryptauth::RemoteDevice(test_devices_[0])), | |
221 "wifiNetworkId"}), | |
222 test_observer_->host_changed_updates()[1]); | |
223 | |
224 // Transition to DISCONNECTED. | |
225 active_host_->SetActiveHostDisconnected(); | |
226 fake_tether_host_fetcher_->InvokePendingCallbacks(); | |
227 EXPECT_EQ(3u, test_observer_->host_changed_updates().size()); | |
228 EXPECT_EQ((GetActiveHostResult{ActiveHost::ActiveHostStatus::DISCONNECTED, | |
229 nullptr, ""}), | |
230 test_observer_->host_changed_updates()[2]); | |
231 } | |
232 | |
175 } // namespace tether | 233 } // namespace tether |
176 | 234 |
177 } // namespace cryptauth | 235 } // namespace cryptauth |
OLD | NEW |