OLD | NEW |
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" | 12 #include "base/run_loop.h" |
13 #include "base/test/scoped_task_environment.h" | 13 #include "base/test/scoped_task_environment.h" |
14 #include "base/test/simple_test_clock.h" | 14 #include "base/test/simple_test_clock.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" | 16 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
17 #include "chromeos/components/tether/fake_ble_connection_manager.h" | 17 #include "chromeos/components/tether/fake_ble_connection_manager.h" |
18 #include "chromeos/components/tether/fake_host_scan_cache.h" | 18 #include "chromeos/components/tether/fake_host_scan_cache.h" |
19 #include "chromeos/components/tether/fake_notification_presenter.h" | 19 #include "chromeos/components/tether/fake_notification_presenter.h" |
20 #include "chromeos/components/tether/fake_tether_host_fetcher.h" | 20 #include "chromeos/components/tether/fake_tether_host_fetcher.h" |
21 #include "chromeos/components/tether/host_scan_device_prioritizer.h" | 21 #include "chromeos/components/tether/host_scan_device_prioritizer.h" |
22 #include "chromeos/components/tether/host_scanner.h" | 22 #include "chromeos/components/tether/host_scanner.h" |
23 #include "chromeos/components/tether/mock_tether_host_response_recorder.h" | 23 #include "chromeos/components/tether/mock_tether_host_response_recorder.h" |
| 24 #include "chromeos/components/tether/proto_test_util.h" |
24 #include "chromeos/dbus/dbus_thread_manager.h" | 25 #include "chromeos/dbus/dbus_thread_manager.h" |
25 #include "components/cryptauth/remote_device_test_util.h" | 26 #include "components/cryptauth/remote_device_test_util.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
27 | 28 |
28 namespace chromeos { | 29 namespace chromeos { |
29 | 30 |
30 namespace tether { | 31 namespace tether { |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 const std::vector<cryptauth::RemoteDevice>& expected_devices_; | 105 const std::vector<cryptauth::RemoteDevice>& expected_devices_; |
105 std::vector<FakeHostScannerOperation*> created_operations_; | 106 std::vector<FakeHostScannerOperation*> created_operations_; |
106 }; | 107 }; |
107 | 108 |
108 std::string GenerateCellProviderForDevice( | 109 std::string GenerateCellProviderForDevice( |
109 const cryptauth::RemoteDevice& remote_device) { | 110 const cryptauth::RemoteDevice& remote_device) { |
110 // Return a string unique to |remote_device|. | 111 // Return a string unique to |remote_device|. |
111 return "cellProvider" + remote_device.GetTruncatedDeviceIdForLogs(); | 112 return "cellProvider" + remote_device.GetTruncatedDeviceIdForLogs(); |
112 } | 113 } |
113 | 114 |
114 const char kDoNotSetStringField[] = "doNotSetField"; | |
115 const int kDoNotSetIntField = -100; | |
116 | |
117 // Creates a DeviceStatus object using the parameters provided. If | |
118 // |kDoNotSetStringField| or |kDoNotSetIntField| are passed, these fields will | |
119 // not be set in the output. | |
120 DeviceStatus CreateFakeDeviceStatus(const std::string& cell_provider_name, | |
121 int battery_percentage, | |
122 int connection_strength) { | |
123 // TODO(khorimoto): Once a ConnectedWifiSsid field is added as a property of | |
124 // Tether networks, give an option to pass a parameter for that field as well. | |
125 WifiStatus wifi_status; | |
126 wifi_status.set_status_code( | |
127 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); | |
128 wifi_status.set_ssid("Google A"); | |
129 | |
130 DeviceStatus device_status; | |
131 if (battery_percentage != kDoNotSetIntField) { | |
132 device_status.set_battery_percentage(battery_percentage); | |
133 } | |
134 if (cell_provider_name != kDoNotSetStringField) { | |
135 device_status.set_cell_provider(cell_provider_name); | |
136 } | |
137 if (connection_strength != kDoNotSetIntField) { | |
138 device_status.set_connection_strength(connection_strength); | |
139 } | |
140 | |
141 device_status.mutable_wifi_status()->CopyFrom(wifi_status); | |
142 | |
143 return device_status; | |
144 } | |
145 | |
146 std::vector<HostScannerOperation::ScannedDeviceInfo> | 115 std::vector<HostScannerOperation::ScannedDeviceInfo> |
147 CreateFakeScannedDeviceInfos( | 116 CreateFakeScannedDeviceInfos( |
148 const std::vector<cryptauth::RemoteDevice>& remote_devices) { | 117 const std::vector<cryptauth::RemoteDevice>& remote_devices) { |
149 // At least 4 ScannedDeviceInfos should be created to ensure that all 4 cases | 118 // At least 4 ScannedDeviceInfos should be created to ensure that all 4 cases |
150 // described below are tested. | 119 // described below are tested. |
151 EXPECT_GT(remote_devices.size(), 3u); | 120 EXPECT_GT(remote_devices.size(), 3u); |
152 | 121 |
153 std::vector<HostScannerOperation::ScannedDeviceInfo> scanned_device_infos; | 122 std::vector<HostScannerOperation::ScannedDeviceInfo> scanned_device_infos; |
154 | 123 |
155 for (size_t i = 0; i < remote_devices.size(); ++i) { | 124 for (size_t i = 0; i < remote_devices.size(); ++i) { |
156 // Four field possibilities: | 125 // Four field possibilities: |
157 // i % 4 == 0: Field is not supplied. | 126 // i % 4 == 0: Field is not supplied. |
158 // i % 4 == 1: Field is below the minimum value (int fields only). | 127 // i % 4 == 1: Field is below the minimum value (int fields only). |
159 // i % 4 == 2: Field is within the valid range (int fields only). | 128 // i % 4 == 2: Field is within the valid range (int fields only). |
160 // i % 4 == 3: Field is above the maximium value (int fields only). | 129 // i % 4 == 3: Field is above the maximium value (int fields only). |
161 std::string cell_provider_name; | 130 std::string cell_provider_name; |
162 int battery_percentage; | 131 int battery_percentage; |
163 int connection_strength; | 132 int connection_strength; |
164 switch (i % 4) { | 133 switch (i % 4) { |
165 case 0: | 134 case 0: |
166 cell_provider_name = kDoNotSetStringField; | 135 cell_provider_name = proto_test_util::kDoNotSetStringField; |
167 battery_percentage = kDoNotSetIntField; | 136 battery_percentage = proto_test_util::kDoNotSetIntField; |
168 connection_strength = kDoNotSetIntField; | 137 connection_strength = proto_test_util::kDoNotSetIntField; |
169 break; | 138 break; |
170 case 1: | 139 case 1: |
171 cell_provider_name = GenerateCellProviderForDevice(remote_devices[i]); | 140 cell_provider_name = GenerateCellProviderForDevice(remote_devices[i]); |
172 battery_percentage = -1 - i; | 141 battery_percentage = -1 - i; |
173 connection_strength = -1 - i; | 142 connection_strength = -1 - i; |
174 break; | 143 break; |
175 case 2: | 144 case 2: |
176 cell_provider_name = GenerateCellProviderForDevice(remote_devices[i]); | 145 cell_provider_name = GenerateCellProviderForDevice(remote_devices[i]); |
177 battery_percentage = (50 + i) % 100; // Valid range is [0, 100]. | 146 battery_percentage = (50 + i) % 100; // Valid range is [0, 100]. |
178 connection_strength = (1 + i) % 4; // Valid range is [0, 4]. | 147 connection_strength = (1 + i) % 4; // Valid range is [0, 4]. |
179 break; | 148 break; |
180 case 3: | 149 case 3: |
181 cell_provider_name = GenerateCellProviderForDevice(remote_devices[i]); | 150 cell_provider_name = GenerateCellProviderForDevice(remote_devices[i]); |
182 battery_percentage = 101 + i; | 151 battery_percentage = 101 + i; |
183 connection_strength = 101 + i; | 152 connection_strength = 101 + i; |
184 break; | 153 break; |
185 default: | 154 default: |
186 NOTREACHED(); | 155 NOTREACHED(); |
187 // Set values for |battery_percentage| and |connection_strength| here to | 156 // Set values for |battery_percentage| and |connection_strength| here to |
188 // prevent a compiler warning which says that they may be unset at this | 157 // prevent a compiler warning which says that they may be unset at this |
189 // point. | 158 // point. |
190 battery_percentage = 0; | 159 battery_percentage = 0; |
191 connection_strength = 0; | 160 connection_strength = 0; |
192 break; | 161 break; |
193 } | 162 } |
194 | 163 |
195 DeviceStatus device_status = CreateFakeDeviceStatus( | 164 DeviceStatus device_status = CreateTestDeviceStatus( |
196 cell_provider_name, battery_percentage, connection_strength); | 165 cell_provider_name, battery_percentage, connection_strength); |
197 | 166 |
198 // Require set-up for odd-numbered device indices. | 167 // Require set-up for odd-numbered device indices. |
199 bool setup_required = i % 2 == 0; | 168 bool setup_required = i % 2 == 0; |
200 | 169 |
201 scanned_device_infos.push_back(HostScannerOperation::ScannedDeviceInfo( | 170 scanned_device_infos.push_back(HostScannerOperation::ScannedDeviceInfo( |
202 remote_devices[i], device_status, setup_required)); | 171 remote_devices[i], device_status, setup_required)); |
203 } | 172 } |
204 | 173 |
205 return scanned_device_infos; | 174 return scanned_device_infos; |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 EXPECT_TRUE(host_scanner_->HasRecentlyScanned()); | 566 EXPECT_TRUE(host_scanner_->HasRecentlyScanned()); |
598 | 567 |
599 // Move past the limit. | 568 // Move past the limit. |
600 test_clock_->Advance(base::TimeDelta::FromSeconds(1)); | 569 test_clock_->Advance(base::TimeDelta::FromSeconds(1)); |
601 EXPECT_FALSE(host_scanner_->HasRecentlyScanned()); | 570 EXPECT_FALSE(host_scanner_->HasRecentlyScanned()); |
602 } | 571 } |
603 | 572 |
604 } // namespace tether | 573 } // namespace tether |
605 | 574 |
606 } // namespace chromeos | 575 } // namespace chromeos |
OLD | NEW |