| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/network/geolocation_handler.h" | 5 #include "chromeos/network/geolocation_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 geolocation_handler_->Init(); | 37 geolocation_handler_->Init(); |
| 38 base::RunLoop().RunUntilIdle(); | 38 base::RunLoop().RunUntilIdle(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 void TearDown() override { | 41 void TearDown() override { |
| 42 geolocation_handler_.reset(); | 42 geolocation_handler_.reset(); |
| 43 DBusThreadManager::Shutdown(); | 43 DBusThreadManager::Shutdown(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 bool GetWifiAccessPoints() { | 46 bool GetWifiAccessPoints() { |
| 47 return geolocation_handler_->GetWifiAccessPoints( | 47 return geolocation_handler_->GetWifiAccessPoints(&wifi_access_points_, |
| 48 &wifi_access_points_, NULL); | 48 nullptr); |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool GetCellTowers() { |
| 52 return geolocation_handler_->GetNetworkInformation(nullptr, &cell_towers_); |
| 53 } |
| 54 |
| 55 // This should remain in sync with the format of shill (chromeos) dict entries |
| 51 void AddAccessPoint(int idx) { | 56 void AddAccessPoint(int idx) { |
| 52 base::DictionaryValue properties; | 57 base::DictionaryValue properties; |
| 53 std::string mac_address = | 58 std::string mac_address = |
| 54 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", | 59 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", |
| 55 idx, 0, 0, 0, 0, 0); | 60 idx, 0, 0, 0, 0, 0); |
| 56 std::string channel = base::IntToString(idx); | 61 std::string channel = base::IntToString(idx); |
| 57 std::string strength = base::IntToString(idx * 10); | 62 std::string strength = base::IntToString(idx * 10); |
| 58 properties.SetStringWithoutPathExpansion( | 63 properties.SetStringWithoutPathExpansion(shill::kGeoMacAddressProperty, |
| 59 shill::kGeoMacAddressProperty, mac_address); | 64 mac_address); |
| 60 properties.SetStringWithoutPathExpansion( | 65 properties.SetStringWithoutPathExpansion(shill::kGeoChannelProperty, |
| 61 shill::kGeoChannelProperty, channel); | 66 channel); |
| 62 properties.SetStringWithoutPathExpansion( | 67 properties.SetStringWithoutPathExpansion(shill::kGeoSignalStrengthProperty, |
| 63 shill::kGeoSignalStrengthProperty, strength); | 68 strength); |
| 64 manager_test_->AddGeoNetwork(shill::kTypeWifi, properties); | 69 manager_test_->AddGeoNetwork(shill::kGeoWifiAccessPointsProperty, |
| 70 properties); |
| 65 base::RunLoop().RunUntilIdle(); | 71 base::RunLoop().RunUntilIdle(); |
| 66 } | 72 } |
| 67 | 73 |
| 74 // This should remain in sync with the format of shill (chromeos) dict entries |
| 75 void AddCellTower(int idx) { |
| 76 base::DictionaryValue properties; |
| 77 // Multiplications are intended solely to differentiate the various fields |
| 78 // in a predictable way, while preserving 3 digits for MCC and MNC. |
| 79 std::string ci = base::IntToString(idx); |
| 80 std::string lac = base::IntToString(idx * 10); |
| 81 std::string mcc = base::IntToString(idx * 100); |
| 82 std::string mnc = base::IntToString(idx * 100 + 1); |
| 83 |
| 84 properties.SetStringWithoutPathExpansion(shill::kGeoCellIdProperty, ci); |
| 85 properties.SetStringWithoutPathExpansion( |
| 86 shill::kGeoLocationAreaCodeProperty, lac); |
| 87 properties.SetStringWithoutPathExpansion( |
| 88 shill::kGeoMobileCountryCodeProperty, mcc); |
| 89 properties.SetStringWithoutPathExpansion( |
| 90 shill::kGeoMobileNetworkCodeProperty, mnc); |
| 91 |
| 92 manager_test_->AddGeoNetwork(shill::kGeoCellTowersProperty, properties); |
| 93 base::RunLoop().RunUntilIdle(); |
| 94 } |
| 95 |
| 68 protected: | 96 protected: |
| 69 base::MessageLoopForUI message_loop_; | 97 base::MessageLoopForUI message_loop_; |
| 70 std::unique_ptr<GeolocationHandler> geolocation_handler_; | 98 std::unique_ptr<GeolocationHandler> geolocation_handler_; |
| 71 ShillManagerClient::TestInterface* manager_test_; | 99 ShillManagerClient::TestInterface* manager_test_; |
| 72 WifiAccessPointVector wifi_access_points_; | 100 WifiAccessPointVector wifi_access_points_; |
| 101 CellTowerVector cell_towers_; |
| 73 | 102 |
| 74 private: | 103 private: |
| 75 DISALLOW_COPY_AND_ASSIGN(GeolocationHandlerTest); | 104 DISALLOW_COPY_AND_ASSIGN(GeolocationHandlerTest); |
| 76 }; | 105 }; |
| 77 | 106 |
| 78 TEST_F(GeolocationHandlerTest, NoAccessPoints) { | 107 TEST_F(GeolocationHandlerTest, NoAccessPoints) { |
| 79 // Inititial call should return false. | 108 // Inititial call should return false. |
| 80 EXPECT_FALSE(GetWifiAccessPoints()); | 109 EXPECT_FALSE(GetWifiAccessPoints()); |
| 110 EXPECT_FALSE(GetCellTowers()); |
| 81 base::RunLoop().RunUntilIdle(); | 111 base::RunLoop().RunUntilIdle(); |
| 82 // Second call should return false since there are no devices. | 112 // Second call should return false since there are no devices. |
| 83 EXPECT_FALSE(GetWifiAccessPoints()); | 113 EXPECT_FALSE(GetWifiAccessPoints()); |
| 114 EXPECT_FALSE(GetCellTowers()); |
| 84 } | 115 } |
| 85 | 116 |
| 86 TEST_F(GeolocationHandlerTest, OneAccessPoint) { | 117 TEST_F(GeolocationHandlerTest, OneAccessPoint) { |
| 87 // Add an acces point. | 118 // Add an access point. |
| 88 AddAccessPoint(1); | 119 AddAccessPoint(1); |
| 89 base::RunLoop().RunUntilIdle(); | 120 base::RunLoop().RunUntilIdle(); |
| 90 // Inititial call should return false and request access points. | 121 // Inititial call should return false and request access points. |
| 91 EXPECT_FALSE(GetWifiAccessPoints()); | 122 EXPECT_FALSE(GetWifiAccessPoints()); |
| 92 base::RunLoop().RunUntilIdle(); | 123 base::RunLoop().RunUntilIdle(); |
| 93 // Second call should return true since we have an access point. | 124 // Second call should return true since we have an access point. |
| 94 EXPECT_TRUE(GetWifiAccessPoints()); | 125 EXPECT_TRUE(GetWifiAccessPoints()); |
| 95 ASSERT_EQ(1u, wifi_access_points_.size()); | 126 ASSERT_EQ(1u, wifi_access_points_.size()); |
| 96 EXPECT_EQ("01:00:00:00:00:00", wifi_access_points_[0].mac_address); | 127 EXPECT_EQ("01:00:00:00:00:00", wifi_access_points_[0].mac_address); |
| 97 EXPECT_EQ(1, wifi_access_points_[0].channel); | 128 EXPECT_EQ(1, wifi_access_points_[0].channel); |
| 98 } | 129 } |
| 99 | 130 |
| 100 TEST_F(GeolocationHandlerTest, MultipleAccessPoints) { | 131 TEST_F(GeolocationHandlerTest, MultipleAccessPoints) { |
| 101 // Add several acces points. | 132 // Add several access points. |
| 102 AddAccessPoint(1); | 133 AddAccessPoint(1); |
| 103 AddAccessPoint(2); | 134 AddAccessPoint(2); |
| 104 AddAccessPoint(3); | 135 AddAccessPoint(3); |
| 105 base::RunLoop().RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
| 106 // Inititial call should return false and request access points. | 137 // Inititial call should return false and request access points. |
| 107 EXPECT_FALSE(GetWifiAccessPoints()); | 138 EXPECT_FALSE(GetWifiAccessPoints()); |
| 139 EXPECT_FALSE(GetCellTowers()); |
| 108 base::RunLoop().RunUntilIdle(); | 140 base::RunLoop().RunUntilIdle(); |
| 109 // Second call should return true since we have an access point. | 141 // Second call should return true since we have an access point. |
| 110 EXPECT_TRUE(GetWifiAccessPoints()); | 142 EXPECT_TRUE(GetWifiAccessPoints()); |
| 111 ASSERT_EQ(3u, wifi_access_points_.size()); | 143 ASSERT_EQ(3u, wifi_access_points_.size()); |
| 112 EXPECT_EQ("02:00:00:00:00:00", wifi_access_points_[1].mac_address); | 144 EXPECT_EQ("02:00:00:00:00:00", wifi_access_points_[1].mac_address); |
| 113 EXPECT_EQ(3, wifi_access_points_[2].channel); | 145 EXPECT_EQ(3, wifi_access_points_[2].channel); |
| 114 } | 146 } |
| 115 | 147 |
| 148 TEST_F(GeolocationHandlerTest, OneCellTower) { |
| 149 // Add a cell tower. |
| 150 AddCellTower(1); |
| 151 base::RunLoop().RunUntilIdle(); |
| 152 // Inititial call should return false and request towers. |
| 153 EXPECT_FALSE(GetCellTowers()); |
| 154 EXPECT_FALSE(GetWifiAccessPoints()); |
| 155 base::RunLoop().RunUntilIdle(); |
| 156 // Second call should return true since we have a cell tower. |
| 157 EXPECT_TRUE(GetCellTowers()); |
| 158 EXPECT_FALSE(GetWifiAccessPoints()); |
| 159 ASSERT_EQ(1u, cell_towers_.size()); |
| 160 EXPECT_EQ("1", cell_towers_[0].ci); |
| 161 EXPECT_EQ("10", cell_towers_[0].lac); |
| 162 EXPECT_EQ("100", cell_towers_[0].mcc); |
| 163 EXPECT_EQ("101", cell_towers_[0].mnc); |
| 164 } |
| 165 |
| 166 TEST_F(GeolocationHandlerTest, MultipleCellTowers) { |
| 167 // Add several cell towers. |
| 168 AddCellTower(1); |
| 169 AddCellTower(2); |
| 170 AddCellTower(3); |
| 171 base::RunLoop().RunUntilIdle(); |
| 172 // Inititial call should return false and request cell towers. |
| 173 EXPECT_FALSE(GetWifiAccessPoints()); |
| 174 EXPECT_FALSE(GetCellTowers()); |
| 175 base::RunLoop().RunUntilIdle(); |
| 176 // Second call should return true since we have a cell tower. |
| 177 EXPECT_FALSE(GetWifiAccessPoints()); |
| 178 EXPECT_TRUE(GetCellTowers()); |
| 179 ASSERT_EQ(3u, cell_towers_.size()); |
| 180 EXPECT_EQ("20", cell_towers_[1].lac); |
| 181 EXPECT_EQ("301", cell_towers_[2].mnc); |
| 182 } |
| 183 |
| 184 TEST_F(GeolocationHandlerTest, MultipleGeolocations) { |
| 185 // Add both a cell tower and wifi AP. |
| 186 AddCellTower(1); |
| 187 AddCellTower(2); |
| 188 AddAccessPoint(1); |
| 189 AddAccessPoint(2); |
| 190 base::RunLoop().RunUntilIdle(); |
| 191 // Inititial call should return false and request towers. |
| 192 EXPECT_FALSE(GetCellTowers()); |
| 193 EXPECT_FALSE(GetWifiAccessPoints()); |
| 194 base::RunLoop().RunUntilIdle(); |
| 195 // Second call should return true since we have a cell tower. |
| 196 EXPECT_TRUE(GetCellTowers()); |
| 197 EXPECT_TRUE(GetWifiAccessPoints()); |
| 198 ASSERT_EQ(2u, wifi_access_points_.size()); |
| 199 EXPECT_EQ("02:00:00:00:00:00", wifi_access_points_[1].mac_address); |
| 200 EXPECT_EQ(1, wifi_access_points_[0].channel); |
| 201 |
| 202 ASSERT_EQ(2u, cell_towers_.size()); |
| 203 EXPECT_EQ("2", cell_towers_[1].ci); |
| 204 EXPECT_EQ("10", cell_towers_[0].lac); |
| 205 EXPECT_EQ("200", cell_towers_[1].mcc); |
| 206 EXPECT_EQ("101", cell_towers_[0].mnc); |
| 207 } |
| 208 |
| 116 } // namespace chromeos | 209 } // namespace chromeos |
| OLD | NEW |