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

Side by Side Diff: chromeos/network/geolocation_handler_unittest.cc

Issue 2624843003: Add support for cellular geolocation (Closed)
Patch Set: Add SEND_ALL_NETWORK_INFO option for timezone detection Created 3 years, 10 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) 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 30 matching lines...) Expand all
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(
48 &wifi_access_points_, NULL); 48 &wifi_access_points_, NULL);
49 } 49 }
50 50
51 bool GetCellTowers() {
52 return geolocation_handler_->GetCellTowers(&cell_towers_, nullptr);
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());
123 EXPECT_FALSE(GetCellTowers());
92 base::RunLoop().RunUntilIdle(); 124 base::RunLoop().RunUntilIdle();
93 // Second call should return true since we have an access point. 125 // Second call should return true since we have an access point.
94 EXPECT_TRUE(GetWifiAccessPoints()); 126 EXPECT_TRUE(GetWifiAccessPoints());
127 EXPECT_FALSE(GetCellTowers());
95 ASSERT_EQ(1u, wifi_access_points_.size()); 128 ASSERT_EQ(1u, wifi_access_points_.size());
96 EXPECT_EQ("01:00:00:00:00:00", wifi_access_points_[0].mac_address); 129 EXPECT_EQ("01:00:00:00:00:00", wifi_access_points_[0].mac_address);
97 EXPECT_EQ(1, wifi_access_points_[0].channel); 130 EXPECT_EQ(1, wifi_access_points_[0].channel);
98 } 131 }
99 132
100 TEST_F(GeolocationHandlerTest, MultipleAccessPoints) { 133 TEST_F(GeolocationHandlerTest, MultipleAccessPoints) {
101 // Add several acces points. 134 // Add several access points.
102 AddAccessPoint(1); 135 AddAccessPoint(1);
103 AddAccessPoint(2); 136 AddAccessPoint(2);
104 AddAccessPoint(3); 137 AddAccessPoint(3);
105 base::RunLoop().RunUntilIdle(); 138 base::RunLoop().RunUntilIdle();
106 // Inititial call should return false and request access points. 139 // Inititial call should return false and request access points.
107 EXPECT_FALSE(GetWifiAccessPoints()); 140 EXPECT_FALSE(GetWifiAccessPoints());
141 EXPECT_FALSE(GetCellTowers());
108 base::RunLoop().RunUntilIdle(); 142 base::RunLoop().RunUntilIdle();
109 // Second call should return true since we have an access point. 143 // Second call should return true since we have an access point.
110 EXPECT_TRUE(GetWifiAccessPoints()); 144 EXPECT_TRUE(GetWifiAccessPoints());
145 EXPECT_FALSE(GetCellTowers());
111 ASSERT_EQ(3u, wifi_access_points_.size()); 146 ASSERT_EQ(3u, wifi_access_points_.size());
112 EXPECT_EQ("02:00:00:00:00:00", wifi_access_points_[1].mac_address); 147 EXPECT_EQ("02:00:00:00:00:00", wifi_access_points_[1].mac_address);
113 EXPECT_EQ(3, wifi_access_points_[2].channel); 148 EXPECT_EQ(3, wifi_access_points_[2].channel);
114 } 149 }
115 150
151 TEST_F(GeolocationHandlerTest, OneCellTower) {
152 // Add a cell tower.
153 AddCellTower(1);
154 base::RunLoop().RunUntilIdle();
155 // Inititial call should return false and request towers.
156 EXPECT_FALSE(GetCellTowers());
157 EXPECT_FALSE(GetWifiAccessPoints());
158 base::RunLoop().RunUntilIdle();
159 // Second call should return true since we have a cell tower.
160 EXPECT_TRUE(GetCellTowers());
161 EXPECT_FALSE(GetWifiAccessPoints());
162 ASSERT_EQ(1u, cell_towers_.size());
163 EXPECT_EQ("1", cell_towers_[0].ci);
164 EXPECT_EQ("10", cell_towers_[0].lac);
165 EXPECT_EQ("100", cell_towers_[0].mcc);
166 EXPECT_EQ("101", cell_towers_[0].mnc);
167 }
168
169 TEST_F(GeolocationHandlerTest, MultipleCellTowers) {
170 // Add several cell towers.
171 AddCellTower(1);
172 AddCellTower(2);
173 AddCellTower(3);
174 base::RunLoop().RunUntilIdle();
175 // Inititial call should return false and request cell towers.
176 EXPECT_FALSE(GetWifiAccessPoints());
177 EXPECT_FALSE(GetCellTowers());
178 base::RunLoop().RunUntilIdle();
179 // Second call should return true since we have a cell tower.
180 EXPECT_FALSE(GetWifiAccessPoints());
181 EXPECT_TRUE(GetCellTowers());
182 ASSERT_EQ(3u, cell_towers_.size());
183 EXPECT_EQ("20", cell_towers_[1].lac);
184 EXPECT_EQ("301", cell_towers_[2].mnc);
185 }
186
187 TEST_F(GeolocationHandlerTest, MultipleGeolocations) {
188 // Add both a cell tower and wifi AP.
189 AddCellTower(1);
190 AddCellTower(2);
191 AddAccessPoint(1);
192 AddAccessPoint(2);
193 base::RunLoop().RunUntilIdle();
194 // Inititial call should return false and request towers.
195 EXPECT_FALSE(GetCellTowers());
196 EXPECT_FALSE(GetWifiAccessPoints());
197 base::RunLoop().RunUntilIdle();
198 // Second call should return true since we have a cell tower.
199 EXPECT_TRUE(GetCellTowers());
200 EXPECT_TRUE(GetWifiAccessPoints());
201 ASSERT_EQ(2u, wifi_access_points_.size());
202 EXPECT_EQ("02:00:00:00:00:00", wifi_access_points_[1].mac_address);
203 EXPECT_EQ(1, wifi_access_points_[0].channel);
204
205 ASSERT_EQ(2u, cell_towers_.size());
206 EXPECT_EQ("2", cell_towers_[1].ci);
207 EXPECT_EQ("10", cell_towers_[0].lac);
208 EXPECT_EQ("200", cell_towers_[1].mcc);
209 EXPECT_EQ("101", cell_towers_[0].mnc);
210 }
211
116 } // namespace chromeos 212 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698