OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chromeos/components/tether/active_host.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/macros.h" | |
11 #include "base/memory/ptr_util.h" | |
12 #include "chromeos/components/tether/fake_tether_host_fetcher.h" | |
13 #include "components/cryptauth/remote_device.h" | |
14 #include "components/cryptauth/remote_device_test_util.h" | |
15 #include "components/prefs/testing_pref_service.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace chromeos { | |
19 | |
20 namespace tether { | |
21 | |
22 namespace { | |
23 | |
24 struct GetActiveHostResult { | |
25 ActiveHost::ActiveHostStatus active_host_status; | |
26 std::shared_ptr<cryptauth::RemoteDevice> remote_device; | |
27 std::string tether_network_id; | |
28 | |
29 bool operator==(const GetActiveHostResult& other) const { | |
30 bool devices_equal; | |
31 if (remote_device) { | |
32 devices_equal = | |
33 other.remote_device && *remote_device == *other.remote_device; | |
34 } else { | |
35 devices_equal = !other.remote_device; | |
36 } | |
37 | |
38 return active_host_status == other.active_host_status && devices_equal && | |
39 tether_network_id == other.tether_network_id; | |
40 } | |
41 }; | |
42 | |
43 } // namespace | |
44 | |
45 class ActiveHostTest : public testing::Test { | |
46 public: | |
47 ActiveHostTest() : test_devices_(cryptauth::GenerateTestRemoteDevices(4)) {} | |
48 | |
49 void SetUp() override { | |
50 get_active_host_results_.clear(); | |
51 | |
52 test_pref_service_ = base::MakeUnique<TestingPrefServiceSimple>(); | |
53 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>( | |
54 test_devices_, false /* synchronously_reply_with_results */); | |
55 | |
56 ActiveHost::RegisterPrefs(test_pref_service_->registry()); | |
57 active_host_ = base::MakeUnique<ActiveHost>(fake_tether_host_fetcher_.get(), | |
58 test_pref_service_.get()); | |
59 } | |
60 | |
61 void OnActiveHostFetched(ActiveHost::ActiveHostStatus active_host_status, | |
62 std::unique_ptr<cryptauth::RemoteDevice> active_host, | |
63 const std::string& tether_network_id) { | |
64 get_active_host_results_.push_back(GetActiveHostResult{ | |
65 active_host_status, std::move(active_host), tether_network_id}); | |
66 } | |
67 | |
68 void VerifyActiveHostDisconnected() { | |
69 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED, | |
70 active_host_->GetActiveHostStatus()); | |
71 EXPECT_TRUE(active_host_->GetActiveHostDeviceId().empty()); | |
72 EXPECT_TRUE(active_host_->GetTetherNetworkId().empty()); | |
73 | |
74 active_host_->GetActiveHost(base::Bind(&ActiveHostTest::OnActiveHostFetched, | |
75 base::Unretained(this))); | |
76 fake_tether_host_fetcher_->InvokePendingCallbacks(); | |
77 ASSERT_EQ(1u, get_active_host_results_.size()); | |
78 EXPECT_EQ((GetActiveHostResult{ActiveHost::ActiveHostStatus::DISCONNECTED, | |
79 nullptr, std::string()}), | |
80 get_active_host_results_[0]); | |
81 } | |
82 | |
83 const std::vector<cryptauth::RemoteDevice> test_devices_; | |
84 | |
85 std::unique_ptr<TestingPrefServiceSimple> test_pref_service_; | |
86 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; | |
87 | |
88 std::vector<GetActiveHostResult> get_active_host_results_; | |
89 | |
90 std::unique_ptr<ActiveHost> active_host_; | |
91 | |
92 private: | |
93 DISALLOW_COPY_AND_ASSIGN(ActiveHostTest); | |
94 }; | |
95 | |
96 TEST_F(ActiveHostTest, TestDefaultValues) { | |
97 VerifyActiveHostDisconnected(); | |
98 } | |
99 | |
100 TEST_F(ActiveHostTest, TestDisconnected) { | |
101 active_host_->SetActiveHostDisconnected(); | |
102 VerifyActiveHostDisconnected(); | |
103 } | |
104 | |
105 TEST_F(ActiveHostTest, TestConnecting) { | |
106 active_host_->SetActiveHostConnecting(test_devices_[0].GetDeviceId()); | |
107 | |
108 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, | |
109 active_host_->GetActiveHostStatus()); | |
110 EXPECT_EQ(test_devices_[0].GetDeviceId(), | |
111 active_host_->GetActiveHostDeviceId()); | |
112 EXPECT_TRUE(active_host_->GetTetherNetworkId().empty()); | |
113 | |
114 active_host_->GetActiveHost( | |
115 base::Bind(&ActiveHostTest::OnActiveHostFetched, base::Unretained(this))); | |
116 fake_tether_host_fetcher_->InvokePendingCallbacks(); | |
117 ASSERT_EQ(1u, get_active_host_results_.size()); | |
118 EXPECT_EQ( | |
119 (GetActiveHostResult{ActiveHost::ActiveHostStatus::CONNECTING, | |
120 std::shared_ptr<cryptauth::RemoteDevice>( | |
121 new cryptauth::RemoteDevice(test_devices_[0])), | |
122 std::string()}), | |
123 get_active_host_results_[0]); | |
124 } | |
125 | |
126 TEST_F(ActiveHostTest, TestConnected) { | |
127 active_host_->SetActiveHostConnected(test_devices_[0].GetDeviceId(), | |
128 "tetherNetworkId"); | |
129 | |
130 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTED, | |
131 active_host_->GetActiveHostStatus()); | |
132 EXPECT_EQ(test_devices_[0].GetDeviceId(), | |
133 active_host_->GetActiveHostDeviceId()); | |
134 EXPECT_EQ("tetherNetworkId", active_host_->GetTetherNetworkId()); | |
135 | |
136 active_host_->GetActiveHost( | |
137 base::Bind(&ActiveHostTest::OnActiveHostFetched, base::Unretained(this))); | |
138 fake_tether_host_fetcher_->InvokePendingCallbacks(); | |
139 ASSERT_EQ(1u, get_active_host_results_.size()); | |
140 EXPECT_EQ( | |
141 (GetActiveHostResult{ActiveHost::ActiveHostStatus::CONNECTED, | |
142 std::shared_ptr<cryptauth::RemoteDevice>( | |
143 new cryptauth::RemoteDevice(test_devices_[0])), | |
144 "tetherNetworkId"}), | |
145 get_active_host_results_[0]); | |
146 } | |
147 | |
148 TEST_F(ActiveHostTest, TestActiveHostChangesDuringFetch) { | |
149 active_host_->SetActiveHostConnected(test_devices_[0].GetDeviceId(), | |
150 "tetherNetworkId"); | |
151 | |
152 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTED, | |
153 active_host_->GetActiveHostStatus()); | |
154 EXPECT_EQ(test_devices_[0].GetDeviceId(), | |
155 active_host_->GetActiveHostDeviceId()); | |
156 EXPECT_EQ("tetherNetworkId", active_host_->GetTetherNetworkId()); | |
157 | |
158 // Start a fetch for the active host. | |
159 active_host_->GetActiveHost( | |
160 base::Bind(&ActiveHostTest::OnActiveHostFetched, base::Unretained(this))); | |
161 | |
162 // While the fetch is in progress, simulate a disconnection occurring. | |
163 active_host_->SetActiveHostDisconnected(); | |
Ryan Hansberry
2017/03/09 19:18:20
I don't doubt that this could happen, but when do
Kyle Horimoto
2017/03/09 19:29:28
0) Connected to a device.
1) Someone calls GetActi
| |
164 | |
165 // Now, finish the fech. | |
166 fake_tether_host_fetcher_->InvokePendingCallbacks(); | |
167 | |
168 // The resulting callback should indicate that there is no active host. | |
169 ASSERT_EQ(1u, get_active_host_results_.size()); | |
170 EXPECT_EQ((GetActiveHostResult{ActiveHost::ActiveHostStatus::DISCONNECTED, | |
171 nullptr, ""}), | |
172 get_active_host_results_[0]); | |
173 } | |
174 | |
175 } // namespace tether | |
176 | |
177 } // namespace cryptauth | |
OLD | NEW |