Chromium Code Reviews| Index: chromeos/geolocation/simple_geolocation_unittest.cc |
| diff --git a/chromeos/geolocation/simple_geolocation_unittest.cc b/chromeos/geolocation/simple_geolocation_unittest.cc |
| index d18502ec4195f43f9ecde93677f0404f0354c449..e0b0dfff90e2bebddf51600db0acbb4c1f4ecdf3 100644 |
| --- a/chromeos/geolocation/simple_geolocation_unittest.cc |
| +++ b/chromeos/geolocation/simple_geolocation_unittest.cc |
| @@ -53,11 +53,24 @@ const char kOneWiFiAPRequestBody[] = |
| "}" |
| "]" |
| "}"; |
| +const char kOneCellTowerRequestBody[] = |
| + "{" |
| + "\"cellTowers\":[" |
| + "{" |
| + "\"cellId\":\"1\"," |
| + "\"locationAreaCode\":\"10\"," |
| + "\"mobileCountryCode\":\"100\"," |
| + "\"mobileNetworkCode\":\"101\"" |
| + "}" |
| + "]," |
| + "\"considerIp\":true" |
| + "}"; |
| const char kExpectedPosition[] = |
| "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, " |
| "error_code=0, error_message='', status=1 (OK)"; |
| const char kWiFiAP1MacAddress[] = "01:00:00:00:00:00"; |
| +const char kCellTower1MNC[] = "101"; |
|
stevenjb
2017/02/03 02:12:02
constexpr
can Skylar cook
2017/02/03 21:47:06
Done.
|
| } // anonymous namespace |
| namespace chromeos { |
| @@ -189,9 +202,9 @@ class GeolocationReceiver { |
| std::unique_ptr<base::RunLoop> message_loop_runner_; |
| }; |
| -class WiFiTestMonitor : public SimpleGeolocationRequestTestMonitor { |
| +class WirelessTestMonitor : public SimpleGeolocationRequestTestMonitor { |
| public: |
| - WiFiTestMonitor() {} |
| + WirelessTestMonitor() {} |
| void OnRequestCreated(SimpleGeolocationRequest* request) override {} |
| void OnStart(SimpleGeolocationRequest* request) override { |
| @@ -203,7 +216,7 @@ class WiFiTestMonitor : public SimpleGeolocationRequestTestMonitor { |
| private: |
| std::string last_request_body_; |
| - DISALLOW_COPY_AND_ASSIGN(WiFiTestMonitor); |
| + DISALLOW_COPY_AND_ASSIGN(WirelessTestMonitor); |
| }; |
| class SimpleGeolocationTest : public testing::Test { |
| @@ -221,7 +234,7 @@ TEST_F(SimpleGeolocationTest, ResponseOK) { |
| &provider); |
| GeolocationReceiver receiver; |
| - provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, |
| + provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, false, |
| base::Bind(&GeolocationReceiver::OnRequestDone, |
| base::Unretained(&receiver))); |
| receiver.WaitUntilRequestDone(); |
| @@ -241,7 +254,7 @@ TEST_F(SimpleGeolocationTest, ResponseOKWithRetries) { |
| &provider); |
| GeolocationReceiver receiver; |
| - provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, |
| + provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, false, |
| base::Bind(&GeolocationReceiver::OnRequestDone, |
| base::Unretained(&receiver))); |
| receiver.WaitUntilRequestDone(); |
| @@ -267,7 +280,7 @@ TEST_F(SimpleGeolocationTest, InvalidResponse) { |
| ASSERT_GE(expected_retries, 2U); |
| provider.RequestGeolocation(base::TimeDelta::FromSeconds(timeout_seconds), |
| - false, |
| + false, false, |
| base::Bind(&GeolocationReceiver::OnRequestDone, |
| base::Unretained(&receiver))); |
| receiver.WaitUntilRequestDone(); |
| @@ -299,7 +312,7 @@ TEST_F(SimpleGeolocationTest, NoWiFi) { |
| DBusThreadManager::GetSetterForTesting(); |
| NetworkHandler::Initialize(); |
| - WiFiTestMonitor requests_monitor; |
| + WirelessTestMonitor requests_monitor; |
| SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); |
| SimpleGeolocationProvider provider(nullptr, |
| @@ -310,7 +323,7 @@ TEST_F(SimpleGeolocationTest, NoWiFi) { |
| 0 /* require_retries */, &provider); |
| GeolocationReceiver receiver; |
| - provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), true, |
| + provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), true, false, |
| base::Bind(&GeolocationReceiver::OnRequestDone, |
| base::Unretained(&receiver))); |
| receiver.WaitUntilRequestDone(); |
| @@ -324,13 +337,13 @@ TEST_F(SimpleGeolocationTest, NoWiFi) { |
| DBusThreadManager::Shutdown(); |
| } |
| -// Test sending of WiFi Access points. |
| +// Test sending of WiFi Access points and Cell Towers. |
| // (This is mostly derived from GeolocationHandlerTest.) |
| -class SimpleGeolocationWiFiTest : public ::testing::TestWithParam<bool> { |
| +class SimpleGeolocationWirelessTest : public ::testing::TestWithParam<bool> { |
| public: |
| - SimpleGeolocationWiFiTest() : manager_test_(nullptr) {} |
| + SimpleGeolocationWirelessTest() : manager_test_(nullptr) {} |
| - ~SimpleGeolocationWiFiTest() override {} |
| + ~SimpleGeolocationWirelessTest() override {} |
| void SetUp() override { |
| // This initializes DBusThreadManager and markes it "for tests only". |
| @@ -354,6 +367,11 @@ class SimpleGeolocationWiFiTest : public ::testing::TestWithParam<bool> { |
| nullptr); |
| } |
| + bool GetCellTowers() { |
| + return geolocation_handler_->GetCellTowers(&cell_towers_, NULL); |
| + } |
| + |
| + // This should remain in sync with the format of shill (chromeos) dict entries |
| void AddAccessPoint(int idx) { |
| base::DictionaryValue properties; |
| std::string mac_address = |
| @@ -366,7 +384,28 @@ class SimpleGeolocationWiFiTest : public ::testing::TestWithParam<bool> { |
| channel); |
| properties.SetStringWithoutPathExpansion(shill::kGeoSignalStrengthProperty, |
| strength); |
| - manager_test_->AddGeoNetwork(shill::kTypeWifi, properties); |
| + manager_test_->AddGeoNetwork(shill::kGeoWifiAccessPointsProperty, |
| + properties); |
| + base::RunLoop().RunUntilIdle(); |
| + } |
| + |
| + // This should remain in sync with the format of shill (chromeos) dict entries |
| + void AddCellTower(int idx) { |
| + base::DictionaryValue properties; |
| + std::string ci = base::IntToString(idx); |
| + std::string lac = base::IntToString(idx * 10); |
| + std::string mcc = base::IntToString(idx * 100); |
| + std::string mnc = base::IntToString(idx * 100 + 1); |
| + |
| + properties.SetStringWithoutPathExpansion(shill::kGeoCellIdProperty, ci); |
| + properties.SetStringWithoutPathExpansion( |
| + shill::kGeoLocationAreaCodeProperty, lac); |
| + properties.SetStringWithoutPathExpansion( |
| + shill::kGeoMobileCountryCodeProperty, mcc); |
| + properties.SetStringWithoutPathExpansion( |
| + shill::kGeoMobileNetworkCodeProperty, mnc); |
| + |
| + manager_test_->AddGeoNetwork(shill::kGeoCellTowersProperty, properties); |
| base::RunLoop().RunUntilIdle(); |
| } |
| @@ -375,16 +414,17 @@ class SimpleGeolocationWiFiTest : public ::testing::TestWithParam<bool> { |
| std::unique_ptr<GeolocationHandler> geolocation_handler_; |
| ShillManagerClient::TestInterface* manager_test_; |
| WifiAccessPointVector wifi_access_points_; |
| + CellTowerVector cell_towers_; |
| private: |
| - DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationWiFiTest); |
| + DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationWirelessTest); |
| }; |
| // Parameter is enable/disable sending of WiFi data. |
| -TEST_P(SimpleGeolocationWiFiTest, WiFiExists) { |
| +TEST_P(SimpleGeolocationWirelessTest, WiFiExists) { |
| NetworkHandler::Initialize(); |
| - WiFiTestMonitor requests_monitor; |
| + WirelessTestMonitor requests_monitor; |
| SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); |
| SimpleGeolocationProvider provider(nullptr, |
| @@ -396,6 +436,7 @@ TEST_P(SimpleGeolocationWiFiTest, WiFiExists) { |
| { |
| GeolocationReceiver receiver; |
| provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(), |
| + false, |
| base::Bind(&GeolocationReceiver::OnRequestDone, |
| base::Unretained(&receiver))); |
| receiver.WaitUntilRequestDone(); |
| @@ -406,10 +447,10 @@ TEST_P(SimpleGeolocationWiFiTest, WiFiExists) { |
| EXPECT_EQ(1U, url_factory.attempts()); |
| } |
| - // Add an acces point. |
| + // Add an access point. |
| AddAccessPoint(1); |
| base::RunLoop().RunUntilIdle(); |
| - // Inititial call should return false and request access points. |
| + // Initial call should return false and request access points. |
| EXPECT_FALSE(GetWifiAccessPoints()); |
| base::RunLoop().RunUntilIdle(); |
| // Second call should return true since we have an access point. |
| @@ -421,6 +462,7 @@ TEST_P(SimpleGeolocationWiFiTest, WiFiExists) { |
| { |
| GeolocationReceiver receiver; |
| provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(), |
| + false, |
| base::Bind(&GeolocationReceiver::OnRequestDone, |
| base::Unretained(&receiver))); |
| receiver.WaitUntilRequestDone(); |
| @@ -442,7 +484,68 @@ TEST_P(SimpleGeolocationWiFiTest, WiFiExists) { |
| // This test verifies that WiFi data is sent only if sending was requested. |
| INSTANTIATE_TEST_CASE_P(EnableDisableSendingWifiData, |
| - SimpleGeolocationWiFiTest, |
| + SimpleGeolocationWirelessTest, |
| testing::Bool()); |
| +TEST_P(SimpleGeolocationWirelessTest, CellularExists) { |
| + NetworkHandler::Initialize(); |
| + |
| + WirelessTestMonitor requests_monitor; |
| + SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); |
| + |
| + SimpleGeolocationProvider provider(nullptr, |
| + GURL(kTestGeolocationProviderUrl)); |
| + |
| + GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| + std::string(kSimpleResponseBody), |
| + 0 /* require_retries */, &provider); |
| + { |
| + GeolocationReceiver receiver; |
| + provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, |
| + GetParam(), |
| + base::Bind(&GeolocationReceiver::OnRequestDone, |
| + base::Unretained(&receiver))); |
| + receiver.WaitUntilRequestDone(); |
| + EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); |
| + |
| + EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| + EXPECT_FALSE(receiver.server_error()); |
| + EXPECT_EQ(1U, url_factory.attempts()); |
| + } |
| + |
| + // Add a cell tower. |
| + AddCellTower(1); |
| + base::RunLoop().RunUntilIdle(); |
| + // Initial call should return false and request cell towers. |
| + EXPECT_FALSE(GetCellTowers()); |
| + base::RunLoop().RunUntilIdle(); |
| + // Second call should return true since we have a tower. |
| + EXPECT_TRUE(GetCellTowers()); |
| + ASSERT_EQ(1u, cell_towers_.size()); |
| + EXPECT_EQ(kCellTower1MNC, cell_towers_[0].mnc); |
| + EXPECT_EQ(base::IntToString(1), cell_towers_[0].ci); |
| + |
| + { |
| + GeolocationReceiver receiver; |
| + provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, |
| + GetParam(), |
| + base::Bind(&GeolocationReceiver::OnRequestDone, |
| + base::Unretained(&receiver))); |
| + receiver.WaitUntilRequestDone(); |
| + if (GetParam()) { |
| + // Sending Cellular data is enabled. |
| + EXPECT_EQ(kOneCellTowerRequestBody, requests_monitor.last_request_body()); |
| + } else { |
| + // Sending Cellular data is disabled. |
| + EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); |
| + } |
| + |
| + EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| + EXPECT_FALSE(receiver.server_error()); |
| + // This is total. |
| + EXPECT_EQ(2U, url_factory.attempts()); |
| + } |
| + NetworkHandler::Shutdown(); |
| +} |
| + |
| } // namespace chromeos |