| Index: content/browser/geolocation/network_location_provider_unittest.cc
|
| ===================================================================
|
| --- content/browser/geolocation/network_location_provider_unittest.cc (revision 162123)
|
| +++ content/browser/geolocation/network_location_provider_unittest.cc (working copy)
|
| @@ -3,6 +3,7 @@
|
| // found in the LICENSE file.
|
|
|
| #include "base/json/json_reader.h"
|
| +#include "base/json/json_writer.h"
|
| #include "base/memory/scoped_ptr.h"
|
| #include "base/string_number_conversions.h"
|
| #include "base/string_util.h"
|
| @@ -10,6 +11,7 @@
|
| #include "base/utf_string_conversions.h"
|
| #include "base/values.h"
|
| #include "content/browser/geolocation/fake_access_token_store.h"
|
| +#include "content/browser/geolocation/location_arbitrator.h"
|
| #include "content/browser/geolocation/network_location_provider.h"
|
| #include "net/url_request/test_url_fetcher_factory.h"
|
| #include "net/url_request/url_request_status.h"
|
| @@ -22,16 +24,8 @@
|
|
|
| // Constants used in multiple tests.
|
| const char kTestServerUrl[] = "https://www.geolocation.test/service";
|
| +const char kAccessTokenString[] = "accessToken";
|
|
|
| -#if defined(GOOGLE_CHROME_BUILD)
|
| - const char kTestJson[] = "?browser=googlechrome&sensor=true";
|
| - const char kTestBrowser[] = "browser=googlechrome";
|
| -#else
|
| - const char kTestJson[] = "?browser=chromium&sensor=true";
|
| - const char kTestBrowser[] = "browser=chromium";
|
| -#endif
|
| -
|
| -const char kTestSensor[] = "sensor=true";
|
| // Using #define so we can easily paste this into various other strings.
|
| #define REFERENCE_ACCESS_TOKEN "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe"
|
|
|
| @@ -128,6 +122,7 @@
|
| class GeolocationNetworkProviderTest : public testing::Test {
|
| public:
|
| virtual void SetUp() {
|
| + test_server_url_ = GURL(kTestServerUrl);
|
| access_token_store_ = new FakeAccessTokenStore;
|
| wifi_data_provider_ =
|
| MockDeviceDataProviderImpl<WifiData>::CreateInstance();
|
| @@ -149,7 +144,7 @@
|
| }
|
|
|
| protected:
|
| - GeolocationNetworkProviderTest() : test_server_url_(kTestServerUrl) {
|
| + GeolocationNetworkProviderTest() {
|
| // TODO(joth): Really these should be in SetUp, not here, but they take no
|
| // effect on Mac OS Release builds if done there. I kid not. Figure out why.
|
| WifiDataProvider::SetFactory(
|
| @@ -185,21 +180,18 @@
|
| return data;
|
| }
|
|
|
| - static std::vector<std::string> CreateReferenceWifiScanDataJson(
|
| - int ap_count, int start_index) {
|
| + static void CreateReferenceWifiScanDataJson(
|
| + int ap_count, int start_index, base::ListValue* wifi_access_point_list) {
|
| std::vector<std::string> wifi_data;
|
| for (int i = 0; i < ap_count; ++i) {
|
| - std::string wifi_part;
|
| - wifi_part += "wifi=";
|
| - wifi_part += "mac%3A" + base::StringPrintf("%02d-34-56-78-54-32", i);
|
| - wifi_part += "%7Css%3A" + base::IntToString(start_index + ap_count - i);
|
| - wifi_part += "%7Cage%3A0";
|
| - wifi_part += "%7Cchan%3A" + base::IntToString(IndexToChannel(i));
|
| - wifi_part += "%7Csnr%3A" + base::IntToString(i + 42);
|
| - wifi_part += "%7Cssid%3ASome%20nice%2Bnetwork%5C%7Cname%5C%5C";
|
| - wifi_data.push_back(wifi_part);
|
| + base::DictionaryValue* ap = new base::DictionaryValue();
|
| + ap->SetString("macAddress", base::StringPrintf("%02d-34-56-78-54-32", i));
|
| + ap->SetInteger("signalStrength", start_index + ap_count - i);
|
| + ap->SetInteger("age", 0);
|
| + ap->SetInteger("channel", IndexToChannel(i));
|
| + ap->SetInteger("signalToNoiseRatio", i + 42);
|
| + wifi_access_point_list->Append(ap);
|
| }
|
| - return wifi_data;
|
| }
|
|
|
| static Geoposition CreateReferencePosition(int id) {
|
| @@ -211,67 +203,135 @@
|
| return pos;
|
| }
|
|
|
| - static void CheckRequestIsValid(const std::string& request_url,
|
| - int expected_routers,
|
| - int expected_wifi_aps,
|
| - int wifi_start_index,
|
| - const std::string& expected_access_token) {
|
| - std::vector<std::string> url_tokens;
|
| - EXPECT_EQ(size_t(2), Tokenize(request_url, "?", &url_tokens));
|
| - EXPECT_EQ(kTestServerUrl, url_tokens[0]);
|
| + static std::string PrettyJson(const base::Value& value) {
|
| + std::string pretty;
|
| + base::JSONWriter::WriteWithOptions(
|
| + &value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &pretty);
|
| + return pretty;
|
| + }
|
|
|
| - std::vector<std::string> json_tokens;
|
| - size_t expected_info_tokens = expected_access_token.empty() ? 2 : 3;
|
| - EXPECT_EQ(expected_info_tokens + expected_wifi_aps,
|
| - Tokenize(url_tokens[1], "&", &json_tokens));
|
| - EXPECT_EQ(kTestBrowser, json_tokens[0]);
|
| - EXPECT_EQ(kTestSensor, json_tokens[1]);
|
| - if (!expected_access_token.empty())
|
| - EXPECT_EQ("token=" + expected_access_token, json_tokens[2]);
|
| + static testing::AssertionResult JsonGetList(
|
| + const std::string& field,
|
| + const base::DictionaryValue& dict,
|
| + const base::ListValue** output_list) {
|
| + if (!dict.GetList(field, output_list))
|
| + return testing::AssertionFailure() << "Dictionary " << PrettyJson(dict)
|
| + << " is missing list field " << field;
|
| + return testing::AssertionSuccess();
|
| + }
|
|
|
| - std::vector<std::string> expected_json_tokens =
|
| - CreateReferenceWifiScanDataJson(expected_wifi_aps, wifi_start_index);
|
| - EXPECT_EQ(size_t(expected_wifi_aps), expected_json_tokens.size());
|
| - for (size_t i = 0; i < expected_json_tokens.size(); ++i ) {
|
| - std::vector<std::string> actual_wifi_tokens;
|
| - std::vector<std::string> expected_wifi_tokens;
|
| - ReplaceSubstringsAfterOffset(&json_tokens[i + expected_info_tokens],
|
| - 0, "%7C", "|");
|
| - ReplaceSubstringsAfterOffset(&expected_json_tokens[i],
|
| - 0, "%7C", "|");
|
| - Tokenize(json_tokens[i + expected_info_tokens],
|
| - "|", &actual_wifi_tokens);
|
| - Tokenize(expected_json_tokens[i], "|", &expected_wifi_tokens);
|
| + static testing::AssertionResult JsonFieldEquals(
|
| + const std::string& field,
|
| + const base::DictionaryValue& expected,
|
| + const base::DictionaryValue& actual) {
|
| + const base::Value* expected_value;
|
| + const base::Value* actual_value;
|
| + if (!expected.Get(field, &expected_value))
|
| + return testing::AssertionFailure()
|
| + << "Expected dictionary " << PrettyJson(expected)
|
| + << " is missing field " << field;
|
| + if (!expected.Get(field, &actual_value))
|
| + return testing::AssertionFailure()
|
| + << "Actual dictionary " << PrettyJson(actual)
|
| + << " is missing field " << field;
|
| + if (!expected_value->Equals(actual_value))
|
| + return testing::AssertionFailure()
|
| + << "Field " << field << " mismatch: " << PrettyJson(*expected_value)
|
| + << " != " << PrettyJson(*actual_value);
|
| + return testing::AssertionSuccess();
|
| + }
|
|
|
| - // MAC address.
|
| - EXPECT_EQ(expected_wifi_tokens[0], actual_wifi_tokens[0]);
|
| - // Signal Strength.
|
| - EXPECT_EQ(expected_wifi_tokens[1], actual_wifi_tokens[1]);
|
| - int age;
|
| - base::StringToInt(actual_wifi_tokens[2].substr(
|
| - 4, actual_wifi_tokens[2].size() - 4),
|
| - &age);
|
| - // Age.
|
| - EXPECT_LT(age, 20 * 1000); // Should not take longer than 20 seconds.
|
| - // Channel.
|
| - EXPECT_EQ(expected_wifi_tokens[3], actual_wifi_tokens[3]);
|
| - // Signal to noise ratio.
|
| - EXPECT_EQ(expected_wifi_tokens[4], actual_wifi_tokens[4]);
|
| - // SSID.
|
| - EXPECT_EQ(expected_wifi_tokens[5], actual_wifi_tokens[5]);
|
| - EXPECT_EQ(expected_wifi_tokens[6], actual_wifi_tokens[6]);
|
| + static GURL UrlWithoutQuery(const GURL& url) {
|
| + url_canon::Replacements<char> replacements;
|
| + replacements.ClearQuery();
|
| + return url.ReplaceComponents(replacements);
|
| + }
|
| +
|
| + testing::AssertionResult IsTestServerUrl(const GURL& request_url) {
|
| + const GURL a(UrlWithoutQuery(test_server_url_));
|
| + const GURL b(UrlWithoutQuery(request_url));
|
| + if (a == b)
|
| + return testing::AssertionSuccess();
|
| + return testing::AssertionFailure() << a << " != " << b;
|
| + }
|
| +
|
| + void CheckRequestIsValid(const net::TestURLFetcher& request,
|
| + int expected_routers,
|
| + int expected_wifi_aps,
|
| + int wifi_start_index,
|
| + const std::string& expected_access_token) {
|
| + const GURL& request_url = request.GetOriginalURL();
|
| +
|
| + EXPECT_TRUE(IsTestServerUrl(request_url));
|
| +
|
| + // Check to see that the api key is being appended for the default
|
| + // network provider url.
|
| + bool is_default_url = UrlWithoutQuery(request_url) ==
|
| + UrlWithoutQuery(GeolocationArbitrator::DefaultNetworkProviderURL());
|
| + EXPECT_EQ(is_default_url, !request_url.query().empty());
|
| +
|
| + const std::string& upload_data = request.upload_data();
|
| + ASSERT_FALSE(upload_data.empty());
|
| + std::string json_parse_error_msg;
|
| + scoped_ptr<base::Value> parsed_json(
|
| + base::JSONReader::ReadAndReturnError(
|
| + upload_data,
|
| + base::JSON_PARSE_RFC,
|
| + NULL,
|
| + &json_parse_error_msg));
|
| + EXPECT_TRUE(json_parse_error_msg.empty());
|
| + ASSERT_TRUE(parsed_json.get() != NULL);
|
| +
|
| + const base::DictionaryValue* request_json;
|
| + ASSERT_TRUE(parsed_json->GetAsDictionary(&request_json));
|
| +
|
| + if (!is_default_url) {
|
| + if (expected_access_token.empty())
|
| + ASSERT_FALSE(request_json->HasKey(kAccessTokenString));
|
| + else {
|
| + std::string access_token;
|
| + EXPECT_TRUE(request_json->GetString(kAccessTokenString, &access_token));
|
| + EXPECT_EQ(expected_access_token, access_token);
|
| + }
|
| }
|
| - EXPECT_TRUE(GURL(request_url).is_valid());
|
| +
|
| + if (expected_wifi_aps) {
|
| + base::ListValue expected_wifi_aps_json;
|
| + CreateReferenceWifiScanDataJson(
|
| + expected_wifi_aps,
|
| + wifi_start_index,
|
| + &expected_wifi_aps_json);
|
| + EXPECT_EQ(size_t(expected_wifi_aps), expected_wifi_aps_json.GetSize());
|
| +
|
| + const base::ListValue* wifi_aps_json;
|
| + ASSERT_TRUE(JsonGetList("wifiAccessPoints", *request_json,
|
| + &wifi_aps_json));
|
| + for (size_t i = 0; i < expected_wifi_aps_json.GetSize(); ++i ) {
|
| + const base::DictionaryValue* expected_json;
|
| + ASSERT_TRUE(expected_wifi_aps_json.GetDictionary(i, &expected_json));
|
| + const base::DictionaryValue* actual_json;
|
| + ASSERT_TRUE(wifi_aps_json->GetDictionary(i, &actual_json));
|
| + ASSERT_TRUE(JsonFieldEquals("macAddress", *expected_json,
|
| + *actual_json));
|
| + ASSERT_TRUE(JsonFieldEquals("signalStrength", *expected_json,
|
| + *actual_json));
|
| + ASSERT_TRUE(JsonFieldEquals("channel", *expected_json, *actual_json));
|
| + ASSERT_TRUE(JsonFieldEquals("signalToNoiseRatio", *expected_json,
|
| + *actual_json));
|
| + }
|
| + } else {
|
| + ASSERT_FALSE(request_json->HasKey("wifiAccessPoints"));
|
| + }
|
| + EXPECT_TRUE(request_url.is_valid());
|
| }
|
|
|
| - const GURL test_server_url_;
|
| + GURL test_server_url_;
|
| MessageLoop main_message_loop_;
|
| scoped_refptr<FakeAccessTokenStore> access_token_store_;
|
| net::TestURLFetcherFactory url_fetcher_factory_;
|
| scoped_refptr<MockDeviceDataProviderImpl<WifiData> > wifi_data_provider_;
|
| };
|
|
|
| -
|
| TEST_F(GeolocationNetworkProviderTest, CreateDestroy) {
|
| // Test fixture members were SetUp correctly.
|
| EXPECT_EQ(&main_message_loop_, MessageLoop::current());
|
| @@ -286,13 +346,19 @@
|
| EXPECT_TRUE(provider->StartProvider(false));
|
| net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id();
|
| ASSERT_TRUE(fetcher != NULL);
|
| + CheckRequestIsValid(*fetcher, 0, 0, 0, "");
|
| +}
|
|
|
| - EXPECT_EQ(test_server_url_.spec() + kTestJson,
|
| - fetcher->GetOriginalURL().spec());
|
| -
|
| - CheckRequestIsValid(fetcher->GetOriginalURL().spec(), 0, 0, 0, "");
|
| +TEST_F(GeolocationNetworkProviderTest, StartProviderDefaultUrl) {
|
| + test_server_url_ = GeolocationArbitrator::DefaultNetworkProviderURL();
|
| + scoped_ptr<LocationProviderBase> provider(CreateProvider(true));
|
| + EXPECT_TRUE(provider->StartProvider(false));
|
| + net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id();
|
| + ASSERT_TRUE(fetcher != NULL);
|
| + CheckRequestIsValid(*fetcher, 0, 0, 0, "");
|
| }
|
|
|
| +
|
| TEST_F(GeolocationNetworkProviderTest, StartProviderLongRequest) {
|
| scoped_ptr<LocationProviderBase> provider(CreateProvider(true));
|
| EXPECT_TRUE(provider->StartProvider(false));
|
| @@ -305,7 +371,7 @@
|
| // in length by not including access points with the lowest signal strength
|
| // in the request.
|
| EXPECT_LT(fetcher->GetOriginalURL().spec().size(), size_t(2048));
|
| - CheckRequestIsValid(fetcher->GetOriginalURL().spec(), 0, 16, 4, "");
|
| + CheckRequestIsValid(*fetcher, 0, 16, 4, "");
|
| }
|
|
|
| TEST_F(GeolocationNetworkProviderTest, MultiRegistrations) {
|
| @@ -330,8 +396,7 @@
|
|
|
| net::TestURLFetcher* fetcher = get_url_fetcher_and_advance_id();
|
| ASSERT_TRUE(fetcher != NULL);
|
| - EXPECT_EQ(test_server_url_.spec() + kTestJson,
|
| - fetcher->GetOriginalURL().spec());
|
| + EXPECT_TRUE(IsTestServerUrl(fetcher->GetOriginalURL()));
|
|
|
| // Complete the network request with bad position fix.
|
| const char* kNoFixNetworkResponse =
|
| @@ -355,14 +420,12 @@
|
| fetcher = get_url_fetcher_and_advance_id();
|
| ASSERT_TRUE(fetcher != NULL);
|
| // The request should have the wifi data.
|
| - CheckRequestIsValid(
|
| - fetcher->GetOriginalURL().spec(), 0, kFirstScanAps, 0, "");
|
| + CheckRequestIsValid(*fetcher, 0, kFirstScanAps, 0, "");
|
|
|
| // Send a reply with good position fix.
|
| const char* kReferenceNetworkResponse =
|
| "{"
|
| - " \"status\": \"OK\","
|
| - " \"access_token\": \"" REFERENCE_ACCESS_TOKEN "\","
|
| + " \"accessToken\": \"" REFERENCE_ACCESS_TOKEN "\","
|
| " \"accuracy\": 1200.4,"
|
| " \"location\": {"
|
| " \"lat\": 51.0,"
|
| @@ -405,9 +468,7 @@
|
| main_message_loop_.RunAllPending();
|
| fetcher = get_url_fetcher_and_advance_id();
|
| EXPECT_TRUE(fetcher);
|
| - CheckRequestIsValid(fetcher->GetOriginalURL().spec(), 0,
|
| - kThirdScanAps, 0,
|
| - REFERENCE_ACCESS_TOKEN);
|
| + CheckRequestIsValid(*fetcher, 0, kThirdScanAps, 0, REFERENCE_ACCESS_TOKEN);
|
| // ...reply with a network error.
|
|
|
| fetcher->set_url(test_server_url_);
|
| @@ -472,8 +533,7 @@
|
| fetcher = get_url_fetcher_and_advance_id();
|
| ASSERT_TRUE(fetcher != NULL);
|
|
|
| - EXPECT_EQ(test_server_url_.spec() + kTestJson,
|
| - fetcher->GetOriginalURL().spec());
|
| + EXPECT_TRUE(IsTestServerUrl(fetcher->GetOriginalURL()));
|
| }
|
|
|
| TEST_F(GeolocationNetworkProviderTest,
|
| @@ -497,8 +557,7 @@
|
| fetcher = get_url_fetcher_and_advance_id();
|
| ASSERT_TRUE(fetcher != NULL);
|
|
|
| - CheckRequestIsValid(fetcher->GetOriginalURL().spec(), 0,
|
| - kScanCount, 0, REFERENCE_ACCESS_TOKEN);
|
| + CheckRequestIsValid(*fetcher, 0, kScanCount, 0, REFERENCE_ACCESS_TOKEN);
|
| }
|
|
|
| TEST_F(GeolocationNetworkProviderTest, NetworkPositionCache) {
|
|
|