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

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

Issue 2781263004: HostScanner: Add scanned hosts to NetworkStateHandler and NotificationPresenter. (Closed)
Patch Set: Address nits. Created 3 years, 8 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
« no previous file with comments | « chromeos/components/tether/host_scanner.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/host_scanner.h" 5 #include "chromeos/components/tether/host_scanner.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <memory> 8 #include <memory>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/run_loop.h"
13 #include "base/test/scoped_task_environment.h"
12 #include "chromeos/components/tether/fake_ble_connection_manager.h" 14 #include "chromeos/components/tether/fake_ble_connection_manager.h"
15 #include "chromeos/components/tether/fake_notification_presenter.h"
13 #include "chromeos/components/tether/fake_tether_host_fetcher.h" 16 #include "chromeos/components/tether/fake_tether_host_fetcher.h"
14 #include "chromeos/components/tether/host_scan_device_prioritizer.h" 17 #include "chromeos/components/tether/host_scan_device_prioritizer.h"
18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "chromeos/network/network_state.h"
20 #include "chromeos/network/network_state_handler.h"
21 #include "chromeos/network/network_state_test.h"
15 #include "components/cryptauth/remote_device_test_util.h" 22 #include "components/cryptauth/remote_device_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
17 24
18 namespace chromeos { 25 namespace chromeos {
19 26
20 namespace tether { 27 namespace tether {
21 28
22 namespace { 29 namespace {
23 30
24 class FakeHostScanDevicePrioritizer : public HostScanDevicePrioritizer { 31 class FakeHostScanDevicePrioritizer : public HostScanDevicePrioritizer {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 // Require set-up for odd-numbered device indices. 119 // Require set-up for odd-numbered device indices.
113 bool set_up_required = i % 2 == 0; 120 bool set_up_required = i % 2 == 0;
114 scanned_device_infos.push_back(HostScannerOperation::ScannedDeviceInfo( 121 scanned_device_infos.push_back(HostScannerOperation::ScannedDeviceInfo(
115 remote_devices[i], device_status, set_up_required)); 122 remote_devices[i], device_status, set_up_required));
116 } 123 }
117 return scanned_device_infos; 124 return scanned_device_infos;
118 } 125 }
119 126
120 } // namespace 127 } // namespace
121 128
122 class HostScannerTest : public testing::Test { 129 class HostScannerTest : public NetworkStateTest {
123 protected: 130 protected:
124 HostScannerTest() 131 HostScannerTest()
125 : test_devices_(cryptauth::GenerateTestRemoteDevices(4)), 132 : test_devices_(cryptauth::GenerateTestRemoteDevices(4)),
126 test_scanned_device_infos(CreateFakeScannedDeviceInfos(test_devices_)) { 133 test_scanned_device_infos(CreateFakeScannedDeviceInfos(test_devices_)) {
127 } 134 }
128 135
129 void SetUp() override { 136 void SetUp() override {
137 DBusThreadManager::Initialize();
138 NetworkStateTest::SetUp();
139
130 scanned_device_infos_so_far_.clear(); 140 scanned_device_infos_so_far_.clear();
131 141
132 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>( 142 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>(
133 test_devices_, false /* synchronously_reply_with_results */); 143 test_devices_, false /* synchronously_reply_with_results */);
134 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); 144 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>();
135 fake_host_scan_device_prioritizer_ = 145 fake_host_scan_device_prioritizer_ =
136 base::MakeUnique<FakeHostScanDevicePrioritizer>(); 146 base::MakeUnique<FakeHostScanDevicePrioritizer>();
137 147
138 fake_host_scanner_operation_factory_ = 148 fake_host_scanner_operation_factory_ =
139 base::WrapUnique(new FakeHostScannerOperationFactory(test_devices_)); 149 base::WrapUnique(new FakeHostScannerOperationFactory(test_devices_));
140 HostScannerOperation::Factory::SetInstanceForTesting( 150 HostScannerOperation::Factory::SetInstanceForTesting(
141 fake_host_scanner_operation_factory_.get()); 151 fake_host_scanner_operation_factory_.get());
142 152
153 fake_notification_presenter_ =
154 base::MakeUnique<FakeNotificationPresenter>();
155
143 host_scanner_ = base::MakeUnique<HostScanner>( 156 host_scanner_ = base::MakeUnique<HostScanner>(
144 fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(), 157 fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(),
145 fake_host_scan_device_prioritizer_.get()); 158 fake_host_scan_device_prioritizer_.get(), network_state_handler(),
159 fake_notification_presenter_.get());
160 }
161
162 void TearDown() override {
163 ShutdownNetworkState();
164 NetworkStateTest::TearDown();
165 DBusThreadManager::Shutdown();
146 } 166 }
147 167
148 // Causes |fake_operation| to receive the scan result in 168 // Causes |fake_operation| to receive the scan result in
149 // |test_scanned_device_infos| vector at the index |test_device_index| with 169 // |test_scanned_device_infos| vector at the index |test_device_index| with
150 // the "final result" value of |is_final_scan_result|. 170 // the "final result" value of |is_final_scan_result|.
151 void ReceiveScanResultAndVerifySuccess( 171 void ReceiveScanResultAndVerifySuccess(
152 FakeHostScannerOperation* fake_operation, 172 FakeHostScannerOperation* fake_operation,
153 size_t test_device_index, 173 size_t test_device_index,
154 bool is_final_scan_result) { 174 bool is_final_scan_result) {
155 scanned_device_infos_so_far_.push_back( 175 scanned_device_infos_so_far_.push_back(
156 test_scanned_device_infos[test_device_index]); 176 test_scanned_device_infos[test_device_index]);
157 fake_operation->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, 177 fake_operation->SendScannedDeviceListUpdate(scanned_device_infos_so_far_,
158 is_final_scan_result); 178 is_final_scan_result);
159 EXPECT_EQ(scanned_device_infos_so_far_, 179 EXPECT_EQ(scanned_device_infos_so_far_,
160 host_scanner_->most_recent_scan_results()); 180 host_scanner_->most_recent_scan_results());
181
182 NetworkStateHandler::NetworkStateList tether_networks;
183 network_state_handler()->GetTetherNetworkList(0 /* no limit */,
184 &tether_networks);
185 EXPECT_EQ(scanned_device_infos_so_far_.size(), tether_networks.size());
186 for (auto& scanned_device_info : scanned_device_infos_so_far_) {
187 cryptauth::RemoteDevice remote_device = scanned_device_info.remote_device;
188 const NetworkState* tether_network =
189 network_state_handler()->GetNetworkStateFromGuid(
190 remote_device.GetDeviceId());
191 ASSERT_TRUE(tether_network);
192 EXPECT_EQ(remote_device.name, tether_network->name());
193 }
194
195 if (scanned_device_infos_so_far_.size() == 1) {
196 EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState::
197 SINGLE_HOTSPOT_NEARBY_SHOWN,
198 fake_notification_presenter_->potential_hotspot_state());
199 } else {
200 EXPECT_EQ(FakeNotificationPresenter::PotentialHotspotNotificationState::
201 MULTIPLE_HOTSPOTS_NEARBY_SHOWN,
202 fake_notification_presenter_->potential_hotspot_state());
203 }
161 } 204 }
162 205
206 base::test::ScopedTaskEnvironment scoped_task_environment_;
207
163 const std::vector<cryptauth::RemoteDevice> test_devices_; 208 const std::vector<cryptauth::RemoteDevice> test_devices_;
164 const std::vector<HostScannerOperation::ScannedDeviceInfo> 209 const std::vector<HostScannerOperation::ScannedDeviceInfo>
165 test_scanned_device_infos; 210 test_scanned_device_infos;
166 211
167 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; 212 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_;
168 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; 213 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
169 std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_; 214 std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_;
170 215
171 std::unique_ptr<FakeHostScannerOperationFactory> 216 std::unique_ptr<FakeHostScannerOperationFactory>
172 fake_host_scanner_operation_factory_; 217 fake_host_scanner_operation_factory_;
173 218
174 std::vector<HostScannerOperation::ScannedDeviceInfo> 219 std::vector<HostScannerOperation::ScannedDeviceInfo>
175 scanned_device_infos_so_far_; 220 scanned_device_infos_so_far_;
176 221
177 std::unique_ptr<HostScanner> host_scanner_; 222 std::unique_ptr<HostScanner> host_scanner_;
178 223
224 std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_;
225
179 private: 226 private:
180 DISALLOW_COPY_AND_ASSIGN(HostScannerTest); 227 DISALLOW_COPY_AND_ASSIGN(HostScannerTest);
181 }; 228 };
182 229
183 TEST_F(HostScannerTest, TestScan_ResultsFromAllDevices) { 230 TEST_F(HostScannerTest, TestScan_ResultsFromAllDevices) {
184 EXPECT_FALSE(host_scanner_->IsScanActive()); 231 EXPECT_FALSE(host_scanner_->IsScanActive());
185 host_scanner_->StartScan(); 232 host_scanner_->StartScan();
186 EXPECT_TRUE(host_scanner_->IsScanActive()); 233 EXPECT_TRUE(host_scanner_->IsScanActive());
187 234
188 fake_tether_host_fetcher_->InvokePendingCallbacks(); 235 fake_tether_host_fetcher_->InvokePendingCallbacks();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_, 349 ->SendScannedDeviceListUpdate(scanned_device_infos_so_far_,
303 true /* is_final_scan_result */); 350 true /* is_final_scan_result */);
304 EXPECT_EQ(scanned_device_infos_so_far_, 351 EXPECT_EQ(scanned_device_infos_so_far_,
305 host_scanner_->most_recent_scan_results()); 352 host_scanner_->most_recent_scan_results());
306 EXPECT_FALSE(host_scanner_->IsScanActive()); 353 EXPECT_FALSE(host_scanner_->IsScanActive());
307 } 354 }
308 355
309 } // namespace tether 356 } // namespace tether
310 357
311 } // namespace cryptauth 358 } // namespace cryptauth
OLDNEW
« no previous file with comments | « chromeos/components/tether/host_scanner.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698