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

Side by Side Diff: device/geolocation/network_location_provider_unittest.cc

Issue 2200483002: Geolocation cleanup: run clang-format (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "device/geolocation/network_location_provider.h" 5 #include "device/geolocation/network_location_provider.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 // TODO(joth): Really these should be in SetUp, not here, but they take no 138 // TODO(joth): Really these should be in SetUp, not here, but they take no
139 // effect on Mac OS Release builds if done there. I kid not. Figure out why. 139 // effect on Mac OS Release builds if done there. I kid not. Figure out why.
140 WifiDataProviderManager::SetFactoryForTesting( 140 WifiDataProviderManager::SetFactoryForTesting(
141 MockWifiDataProvider::GetInstance); 141 MockWifiDataProvider::GetInstance);
142 } 142 }
143 143
144 // Returns the current url fetcher (if any) and advances the id ready for the 144 // Returns the current url fetcher (if any) and advances the id ready for the
145 // next test step. 145 // next test step.
146 net::TestURLFetcher* get_url_fetcher_and_advance_id() { 146 net::TestURLFetcher* get_url_fetcher_and_advance_id() {
147 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID( 147 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(
148 NetworkLocationRequest::url_fetcher_id_for_tests); 148 NetworkLocationRequest::url_fetcher_id_for_tests);
149 if (fetcher) 149 if (fetcher)
150 ++NetworkLocationRequest::url_fetcher_id_for_tests; 150 ++NetworkLocationRequest::url_fetcher_id_for_tests;
151 return fetcher; 151 return fetcher;
152 } 152 }
153 153
154 static int IndexToChannel(int index) { return index + 4; } 154 static int IndexToChannel(int index) { return index + 4; }
155 155
156 // Creates wifi data containing the specified number of access points, with 156 // Creates wifi data containing the specified number of access points, with
157 // some differentiating charactistics in each. 157 // some differentiating charactistics in each.
158 static WifiData CreateReferenceWifiScanData(int ap_count) { 158 static WifiData CreateReferenceWifiScanData(int ap_count) {
159 WifiData data; 159 WifiData data;
160 for (int i = 0; i < ap_count; ++i) { 160 for (int i = 0; i < ap_count; ++i) {
161 AccessPointData ap; 161 AccessPointData ap;
162 ap.mac_address = 162 ap.mac_address =
163 base::ASCIIToUTF16(base::StringPrintf("%02d-34-56-78-54-32", i)); 163 base::ASCIIToUTF16(base::StringPrintf("%02d-34-56-78-54-32", i));
164 ap.radio_signal_strength = ap_count - i; 164 ap.radio_signal_strength = ap_count - i;
165 ap.channel = IndexToChannel(i); 165 ap.channel = IndexToChannel(i);
166 ap.signal_to_noise = i + 42; 166 ap.signal_to_noise = i + 42;
167 ap.ssid = base::ASCIIToUTF16("Some nice+network|name\\"); 167 ap.ssid = base::ASCIIToUTF16("Some nice+network|name\\");
168 data.access_point_data.insert(ap); 168 data.access_point_data.insert(ap);
169 } 169 }
170 return data; 170 return data;
171 } 171 }
172 172
173 static void CreateReferenceWifiScanDataJson( 173 static void CreateReferenceWifiScanDataJson(
174 int ap_count, int start_index, base::ListValue* wifi_access_point_list) { 174 int ap_count,
175 int start_index,
176 base::ListValue* wifi_access_point_list) {
175 std::vector<std::string> wifi_data; 177 std::vector<std::string> wifi_data;
176 for (int i = 0; i < ap_count; ++i) { 178 for (int i = 0; i < ap_count; ++i) {
177 std::unique_ptr<base::DictionaryValue> ap(new base::DictionaryValue()); 179 std::unique_ptr<base::DictionaryValue> ap(new base::DictionaryValue());
178 ap->SetString("macAddress", base::StringPrintf("%02d-34-56-78-54-32", i)); 180 ap->SetString("macAddress", base::StringPrintf("%02d-34-56-78-54-32", i));
179 ap->SetInteger("signalStrength", start_index + ap_count - i); 181 ap->SetInteger("signalStrength", start_index + ap_count - i);
180 ap->SetInteger("age", 0); 182 ap->SetInteger("age", 0);
181 ap->SetInteger("channel", IndexToChannel(i)); 183 ap->SetInteger("channel", IndexToChannel(i));
182 ap->SetInteger("signalToNoiseRatio", i + 42); 184 ap->SetInteger("signalToNoiseRatio", i + 42);
183 wifi_access_point_list->Append(std::move(ap)); 185 wifi_access_point_list->Append(std::move(ap));
184 } 186 }
(...skipping 25 matching lines...) Expand all
210 return testing::AssertionSuccess(); 212 return testing::AssertionSuccess();
211 } 213 }
212 214
213 static testing::AssertionResult JsonFieldEquals( 215 static testing::AssertionResult JsonFieldEquals(
214 const std::string& field, 216 const std::string& field,
215 const base::DictionaryValue& expected, 217 const base::DictionaryValue& expected,
216 const base::DictionaryValue& actual) { 218 const base::DictionaryValue& actual) {
217 const base::Value* expected_value; 219 const base::Value* expected_value;
218 const base::Value* actual_value; 220 const base::Value* actual_value;
219 if (!expected.Get(field, &expected_value)) 221 if (!expected.Get(field, &expected_value))
220 return testing::AssertionFailure() 222 return testing::AssertionFailure() << "Expected dictionary "
221 << "Expected dictionary " << PrettyJson(expected) 223 << PrettyJson(expected)
222 << " is missing field " << field; 224 << " is missing field " << field;
223 if (!expected.Get(field, &actual_value)) 225 if (!expected.Get(field, &actual_value))
224 return testing::AssertionFailure() 226 return testing::AssertionFailure() << "Actual dictionary "
225 << "Actual dictionary " << PrettyJson(actual) 227 << PrettyJson(actual)
226 << " is missing field " << field; 228 << " is missing field " << field;
227 if (!expected_value->Equals(actual_value)) 229 if (!expected_value->Equals(actual_value))
228 return testing::AssertionFailure() 230 return testing::AssertionFailure()
229 << "Field " << field << " mismatch: " << PrettyJson(*expected_value) 231 << "Field " << field
230 << " != " << PrettyJson(*actual_value); 232 << " mismatch: " << PrettyJson(*expected_value)
233 << " != " << PrettyJson(*actual_value);
231 return testing::AssertionSuccess(); 234 return testing::AssertionSuccess();
232 } 235 }
233 236
234 static GURL UrlWithoutQuery(const GURL& url) { 237 static GURL UrlWithoutQuery(const GURL& url) {
235 url::Replacements<char> replacements; 238 url::Replacements<char> replacements;
236 replacements.ClearQuery(); 239 replacements.ClearQuery();
237 return url.ReplaceComponents(replacements); 240 return url.ReplaceComponents(replacements);
238 } 241 }
239 242
240 testing::AssertionResult IsTestServerUrl(const GURL& request_url) { 243 testing::AssertionResult IsTestServerUrl(const GURL& request_url) {
241 const GURL a(UrlWithoutQuery(test_server_url_)); 244 const GURL a(UrlWithoutQuery(test_server_url_));
242 const GURL b(UrlWithoutQuery(request_url)); 245 const GURL b(UrlWithoutQuery(request_url));
243 if (a == b) 246 if (a == b)
244 return testing::AssertionSuccess(); 247 return testing::AssertionSuccess();
245 return testing::AssertionFailure() << a << " != " << b; 248 return testing::AssertionFailure() << a << " != " << b;
246 } 249 }
247 250
248 void CheckRequestIsValid(const net::TestURLFetcher& request, 251 void CheckRequestIsValid(const net::TestURLFetcher& request,
249 int expected_routers, 252 int expected_routers,
250 int expected_wifi_aps, 253 int expected_wifi_aps,
251 int wifi_start_index, 254 int wifi_start_index,
252 const std::string& expected_access_token) { 255 const std::string& expected_access_token) {
253 const GURL& request_url = request.GetOriginalURL(); 256 const GURL& request_url = request.GetOriginalURL();
254 257
255 EXPECT_TRUE(IsTestServerUrl(request_url)); 258 EXPECT_TRUE(IsTestServerUrl(request_url));
256 259
257 // Check to see that the api key is being appended for the default 260 // Check to see that the api key is being appended for the default
258 // network provider url. 261 // network provider url.
259 bool is_default_url = UrlWithoutQuery(request_url) == 262 bool is_default_url =
263 UrlWithoutQuery(request_url) ==
260 UrlWithoutQuery(LocationArbitratorImpl::DefaultNetworkProviderURL()); 264 UrlWithoutQuery(LocationArbitratorImpl::DefaultNetworkProviderURL());
261 EXPECT_EQ(is_default_url, !request_url.query().empty()); 265 EXPECT_EQ(is_default_url, !request_url.query().empty());
262 266
263 const std::string& upload_data = request.upload_data(); 267 const std::string& upload_data = request.upload_data();
264 ASSERT_FALSE(upload_data.empty()); 268 ASSERT_FALSE(upload_data.empty());
265 std::string json_parse_error_msg; 269 std::string json_parse_error_msg;
266 std::unique_ptr<base::Value> parsed_json = 270 std::unique_ptr<base::Value> parsed_json =
267 base::JSONReader::ReadAndReturnError(upload_data, base::JSON_PARSE_RFC, 271 base::JSONReader::ReadAndReturnError(upload_data, base::JSON_PARSE_RFC,
268 nullptr, &json_parse_error_msg); 272 nullptr, &json_parse_error_msg);
269 EXPECT_TRUE(json_parse_error_msg.empty()); 273 EXPECT_TRUE(json_parse_error_msg.empty());
270 ASSERT_TRUE(parsed_json); 274 ASSERT_TRUE(parsed_json);
271 275
272 const base::DictionaryValue* request_json; 276 const base::DictionaryValue* request_json;
273 ASSERT_TRUE(parsed_json->GetAsDictionary(&request_json)); 277 ASSERT_TRUE(parsed_json->GetAsDictionary(&request_json));
274 278
275 if (!is_default_url) { 279 if (!is_default_url) {
276 if (expected_access_token.empty()) 280 if (expected_access_token.empty())
277 ASSERT_FALSE(request_json->HasKey(kAccessTokenString)); 281 ASSERT_FALSE(request_json->HasKey(kAccessTokenString));
278 else { 282 else {
279 std::string access_token; 283 std::string access_token;
280 EXPECT_TRUE(request_json->GetString(kAccessTokenString, &access_token)); 284 EXPECT_TRUE(request_json->GetString(kAccessTokenString, &access_token));
281 EXPECT_EQ(expected_access_token, access_token); 285 EXPECT_EQ(expected_access_token, access_token);
282 } 286 }
283 } 287 }
284 288
285 if (expected_wifi_aps) { 289 if (expected_wifi_aps) {
286 base::ListValue expected_wifi_aps_json; 290 base::ListValue expected_wifi_aps_json;
287 CreateReferenceWifiScanDataJson( 291 CreateReferenceWifiScanDataJson(expected_wifi_aps, wifi_start_index,
288 expected_wifi_aps, 292 &expected_wifi_aps_json);
289 wifi_start_index,
290 &expected_wifi_aps_json);
291 EXPECT_EQ(size_t(expected_wifi_aps), expected_wifi_aps_json.GetSize()); 293 EXPECT_EQ(size_t(expected_wifi_aps), expected_wifi_aps_json.GetSize());
292 294
293 const base::ListValue* wifi_aps_json; 295 const base::ListValue* wifi_aps_json;
294 ASSERT_TRUE(JsonGetList("wifiAccessPoints", *request_json, 296 ASSERT_TRUE(
295 &wifi_aps_json)); 297 JsonGetList("wifiAccessPoints", *request_json, &wifi_aps_json));
296 for (size_t i = 0; i < expected_wifi_aps_json.GetSize(); ++i ) { 298 for (size_t i = 0; i < expected_wifi_aps_json.GetSize(); ++i) {
297 const base::DictionaryValue* expected_json; 299 const base::DictionaryValue* expected_json;
298 ASSERT_TRUE(expected_wifi_aps_json.GetDictionary(i, &expected_json)); 300 ASSERT_TRUE(expected_wifi_aps_json.GetDictionary(i, &expected_json));
299 const base::DictionaryValue* actual_json; 301 const base::DictionaryValue* actual_json;
300 ASSERT_TRUE(wifi_aps_json->GetDictionary(i, &actual_json)); 302 ASSERT_TRUE(wifi_aps_json->GetDictionary(i, &actual_json));
301 ASSERT_TRUE(JsonFieldEquals("macAddress", *expected_json, 303 ASSERT_TRUE(
302 *actual_json)); 304 JsonFieldEquals("macAddress", *expected_json, *actual_json));
303 ASSERT_TRUE(JsonFieldEquals("signalStrength", *expected_json, 305 ASSERT_TRUE(
304 *actual_json)); 306 JsonFieldEquals("signalStrength", *expected_json, *actual_json));
305 ASSERT_TRUE(JsonFieldEquals("channel", *expected_json, *actual_json)); 307 ASSERT_TRUE(JsonFieldEquals("channel", *expected_json, *actual_json));
306 ASSERT_TRUE(JsonFieldEquals("signalToNoiseRatio", *expected_json, 308 ASSERT_TRUE(JsonFieldEquals("signalToNoiseRatio", *expected_json,
307 *actual_json)); 309 *actual_json));
308 } 310 }
309 } else { 311 } else {
310 ASSERT_FALSE(request_json->HasKey("wifiAccessPoints")); 312 ASSERT_FALSE(request_json->HasKey("wifiAccessPoints"));
311 } 313 }
312 EXPECT_TRUE(request_url.is_valid()); 314 EXPECT_TRUE(request_url.is_valid());
313 } 315 }
314 316
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); 390 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps));
389 base::RunLoop().RunUntilIdle(); 391 base::RunLoop().RunUntilIdle();
390 fetcher = get_url_fetcher_and_advance_id(); 392 fetcher = get_url_fetcher_and_advance_id();
391 ASSERT_TRUE(fetcher); 393 ASSERT_TRUE(fetcher);
392 // The request should have the wifi data. 394 // The request should have the wifi data.
393 CheckRequestIsValid(*fetcher, 0, kFirstScanAps, 0, std::string()); 395 CheckRequestIsValid(*fetcher, 0, kFirstScanAps, 0, std::string());
394 396
395 // Send a reply with good position fix. 397 // Send a reply with good position fix.
396 const char* kReferenceNetworkResponse = 398 const char* kReferenceNetworkResponse =
397 "{" 399 "{"
398 " \"accessToken\": \"" REFERENCE_ACCESS_TOKEN "\"," 400 " \"accessToken\": \"" REFERENCE_ACCESS_TOKEN
401 "\","
399 " \"accuracy\": 1200.4," 402 " \"accuracy\": 1200.4,"
400 " \"location\": {" 403 " \"location\": {"
401 " \"lat\": 51.0," 404 " \"lat\": 51.0,"
402 " \"lng\": -0.1" 405 " \"lng\": -0.1"
403 " }" 406 " }"
404 "}"; 407 "}";
405 fetcher->set_url(test_server_url_); 408 fetcher->set_url(test_server_url_);
406 fetcher->set_status(net::URLRequestStatus()); 409 fetcher->set_status(net::URLRequestStatus());
407 fetcher->set_response_code(200); // OK 410 fetcher->set_response_code(200); // OK
408 fetcher->SetResponseString(kReferenceNetworkResponse); 411 fetcher->SetResponseString(kReferenceNetworkResponse);
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 CheckRequestIsValid(*fetcher, 0, kScanCount, 0, REFERENCE_ACCESS_TOKEN); 535 CheckRequestIsValid(*fetcher, 0, kScanCount, 0, REFERENCE_ACCESS_TOKEN);
533 } 536 }
534 537
535 TEST_F(GeolocationNetworkProviderTest, NetworkPositionCache) { 538 TEST_F(GeolocationNetworkProviderTest, NetworkPositionCache) {
536 NetworkLocationProvider::PositionCache cache; 539 NetworkLocationProvider::PositionCache cache;
537 540
538 const int kCacheSize = NetworkLocationProvider::PositionCache::kMaximumSize; 541 const int kCacheSize = NetworkLocationProvider::PositionCache::kMaximumSize;
539 for (int i = 1; i < kCacheSize * 2 + 1; ++i) { 542 for (int i = 1; i < kCacheSize * 2 + 1; ++i) {
540 Geoposition pos = CreateReferencePosition(i); 543 Geoposition pos = CreateReferencePosition(i);
541 bool ret = cache.CachePosition(CreateReferenceWifiScanData(i), pos); 544 bool ret = cache.CachePosition(CreateReferenceWifiScanData(i), pos);
542 EXPECT_TRUE(ret) << i; 545 EXPECT_TRUE(ret) << i;
543 const Geoposition* item = 546 const Geoposition* item =
544 cache.FindPosition(CreateReferenceWifiScanData(i)); 547 cache.FindPosition(CreateReferenceWifiScanData(i));
545 ASSERT_TRUE(item) << i; 548 ASSERT_TRUE(item) << i;
546 EXPECT_EQ(pos.latitude, item->latitude) << i; 549 EXPECT_EQ(pos.latitude, item->latitude) << i;
547 EXPECT_EQ(pos.longitude, item->longitude) << i; 550 EXPECT_EQ(pos.longitude, item->longitude) << i;
548 if (i <= kCacheSize) { 551 if (i <= kCacheSize) {
549 // Nothing should have spilled yet; check oldest item is still there. 552 // Nothing should have spilled yet; check oldest item is still there.
550 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); 553 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1)));
551 } else { 554 } else {
552 const int evicted = i - kCacheSize; 555 const int evicted = i - kCacheSize;
553 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); 556 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted)));
554 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); 557 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1)));
555 } 558 }
556 } 559 }
557 } 560 }
558 561
559 } // namespace device 562 } // namespace device
OLDNEW
« no previous file with comments | « device/geolocation/network_location_provider.cc ('k') | device/geolocation/network_location_request.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698