| OLD | NEW |
| 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 "content/browser/geolocation/wifi_data_provider_linux.h" | 5 #include "content/browser/geolocation/wifi_data_provider_linux.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 options.bus_type = dbus::Bus::SYSTEM; | 30 options.bus_type = dbus::Bus::SYSTEM; |
| 31 mock_bus_ = new dbus::MockBus(options); | 31 mock_bus_ = new dbus::MockBus(options); |
| 32 | 32 |
| 33 // Create a mock proxy that behaves as NetworkManager. | 33 // Create a mock proxy that behaves as NetworkManager. |
| 34 mock_network_manager_proxy_ = | 34 mock_network_manager_proxy_ = |
| 35 new dbus::MockObjectProxy( | 35 new dbus::MockObjectProxy( |
| 36 mock_bus_.get(), | 36 mock_bus_.get(), |
| 37 "org.freedesktop.NetworkManager", | 37 "org.freedesktop.NetworkManager", |
| 38 dbus::ObjectPath("/org/freedesktop/NetworkManager")); | 38 dbus::ObjectPath("/org/freedesktop/NetworkManager")); |
| 39 // Set an expectation so mock_network_manager_proxy_'s | 39 // Set an expectation so mock_network_manager_proxy_'s |
| 40 // CallMethodAndBlock() will use CreateNetowrkManagerProxyResponse() | 40 // CallMethodAndBlock() will use CreateNetworkManagerProxyResponse() |
| 41 // to return responses. | 41 // to return responses. |
| 42 EXPECT_CALL(*mock_network_manager_proxy_, | 42 EXPECT_CALL(*mock_network_manager_proxy_, |
| 43 CallMethodAndBlock(_, _)) | 43 MockCallMethodAndBlock(_, _)) |
| 44 .WillRepeatedly(Invoke( | 44 .WillRepeatedly(Invoke( |
| 45 this, | 45 this, |
| 46 &GeolocationWifiDataProviderLinuxTest:: | 46 &GeolocationWifiDataProviderLinuxTest:: |
| 47 CreateNetowrkManagerProxyResponse)); | 47 CreateNetworkManagerProxyResponse)); |
| 48 | 48 |
| 49 // Create a mock proxy that behaves as NetworkManager/Devices/0. | 49 // Create a mock proxy that behaves as NetworkManager/Devices/0. |
| 50 mock_device_proxy_ = | 50 mock_device_proxy_ = |
| 51 new dbus::MockObjectProxy( | 51 new dbus::MockObjectProxy( |
| 52 mock_bus_.get(), | 52 mock_bus_.get(), |
| 53 "org.freedesktop.NetworkManager", | 53 "org.freedesktop.NetworkManager", |
| 54 dbus::ObjectPath("/org/freedesktop/NetworkManager/Devices/0")); | 54 dbus::ObjectPath("/org/freedesktop/NetworkManager/Devices/0")); |
| 55 EXPECT_CALL(*mock_device_proxy_, | 55 EXPECT_CALL(*mock_device_proxy_, |
| 56 CallMethodAndBlock(_, _)) | 56 MockCallMethodAndBlock(_, _)) |
| 57 .WillRepeatedly(Invoke( | 57 .WillRepeatedly(Invoke( |
| 58 this, | 58 this, |
| 59 &GeolocationWifiDataProviderLinuxTest::CreateDeviceProxyResponse)); | 59 &GeolocationWifiDataProviderLinuxTest::CreateDeviceProxyResponse)); |
| 60 | 60 |
| 61 // Create a mock proxy that behaves as NetworkManager/AccessPoint/0. | 61 // Create a mock proxy that behaves as NetworkManager/AccessPoint/0. |
| 62 mock_access_point_proxy_ = | 62 mock_access_point_proxy_ = |
| 63 new dbus::MockObjectProxy( | 63 new dbus::MockObjectProxy( |
| 64 mock_bus_.get(), | 64 mock_bus_.get(), |
| 65 "org.freedesktop.NetworkManager", | 65 "org.freedesktop.NetworkManager", |
| 66 dbus::ObjectPath("/org/freedesktop/NetworkManager/AccessPoint/0")); | 66 dbus::ObjectPath("/org/freedesktop/NetworkManager/AccessPoint/0")); |
| 67 EXPECT_CALL(*mock_access_point_proxy_, | 67 EXPECT_CALL(*mock_access_point_proxy_, |
| 68 CallMethodAndBlock(_, _)) | 68 MockCallMethodAndBlock(_, _)) |
| 69 .WillRepeatedly(Invoke( | 69 .WillRepeatedly(Invoke( |
| 70 this, | 70 this, |
| 71 &GeolocationWifiDataProviderLinuxTest:: | 71 &GeolocationWifiDataProviderLinuxTest:: |
| 72 CreateAccessPointProxyResponse)); | 72 CreateAccessPointProxyResponse)); |
| 73 | 73 |
| 74 // Set an expectation so mock_bus_'s GetObjectProxy() for the given | 74 // Set an expectation so mock_bus_'s GetObjectProxy() for the given |
| 75 // service name and the object path will return | 75 // service name and the object path will return |
| 76 // mock_network_manager_proxy_. | 76 // mock_network_manager_proxy_. |
| 77 EXPECT_CALL(*mock_bus_, GetObjectProxy( | 77 EXPECT_CALL(*mock_bus_, GetObjectProxy( |
| 78 "org.freedesktop.NetworkManager", | 78 "org.freedesktop.NetworkManager", |
| (...skipping 28 matching lines...) Expand all Loading... |
| 107 MessageLoop message_loop_; | 107 MessageLoop message_loop_; |
| 108 scoped_refptr<dbus::MockBus> mock_bus_; | 108 scoped_refptr<dbus::MockBus> mock_bus_; |
| 109 scoped_refptr<dbus::MockObjectProxy> mock_network_manager_proxy_; | 109 scoped_refptr<dbus::MockObjectProxy> mock_network_manager_proxy_; |
| 110 scoped_refptr<dbus::MockObjectProxy> mock_access_point_proxy_; | 110 scoped_refptr<dbus::MockObjectProxy> mock_access_point_proxy_; |
| 111 scoped_refptr<dbus::MockObjectProxy> mock_device_proxy_; | 111 scoped_refptr<dbus::MockObjectProxy> mock_device_proxy_; |
| 112 scoped_refptr<WifiDataProviderLinux> wifi_provider_linux_; | 112 scoped_refptr<WifiDataProviderLinux> wifi_provider_linux_; |
| 113 scoped_ptr<WifiDataProviderCommon::WlanApiInterface> wlan_api_; | 113 scoped_ptr<WifiDataProviderCommon::WlanApiInterface> wlan_api_; |
| 114 | 114 |
| 115 private: | 115 private: |
| 116 // Creates a response for |mock_network_manager_proxy_|. | 116 // Creates a response for |mock_network_manager_proxy_|. |
| 117 dbus::Response* CreateNetowrkManagerProxyResponse( | 117 dbus::Response* CreateNetworkManagerProxyResponse( |
| 118 dbus::MethodCall* method_call, | 118 dbus::MethodCall* method_call, |
| 119 Unused) { | 119 Unused) { |
| 120 if (method_call->GetInterface() == "org.freedesktop.NetworkManager" && | 120 if (method_call->GetInterface() == "org.freedesktop.NetworkManager" && |
| 121 method_call->GetMember() == "GetDevices") { | 121 method_call->GetMember() == "GetDevices") { |
| 122 // The list of devices is asked. Return the object path. | 122 // The list of devices is asked. Return the object path. |
| 123 std::vector<dbus::ObjectPath> object_paths; | 123 std::vector<dbus::ObjectPath> object_paths; |
| 124 object_paths.push_back( | 124 object_paths.push_back( |
| 125 dbus::ObjectPath("/org/freedesktop/NetworkManager/Devices/0")); | 125 dbus::ObjectPath("/org/freedesktop/NetworkManager/Devices/0")); |
| 126 | 126 |
| 127 dbus::Response* response = dbus::Response::CreateEmpty(); | 127 scoped_ptr<dbus::Response> response = dbus::Response::CreateEmpty(); |
| 128 dbus::MessageWriter writer(response); | 128 dbus::MessageWriter writer(response.get()); |
| 129 writer.AppendArrayOfObjectPaths(object_paths); | 129 writer.AppendArrayOfObjectPaths(object_paths); |
| 130 return response; | 130 return response.release(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); | 133 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 134 return NULL; | 134 return NULL; |
| 135 } | 135 } |
| 136 | 136 |
| 137 // Creates a response for |mock_device_proxy_|. | 137 // Creates a response for |mock_device_proxy_|. |
| 138 dbus::Response* CreateDeviceProxyResponse(dbus::MethodCall* method_call, | 138 dbus::Response* CreateDeviceProxyResponse(dbus::MethodCall* method_call, |
| 139 Unused) { | 139 Unused) { |
| 140 if (method_call->GetInterface() == DBUS_INTERFACE_PROPERTIES && | 140 if (method_call->GetInterface() == DBUS_INTERFACE_PROPERTIES && |
| 141 method_call->GetMember() == "Get") { | 141 method_call->GetMember() == "Get") { |
| 142 dbus::MessageReader reader(method_call); | 142 dbus::MessageReader reader(method_call); |
| 143 std::string interface_name; | 143 std::string interface_name; |
| 144 std::string property_name; | 144 std::string property_name; |
| 145 if (reader.PopString(&interface_name) && | 145 if (reader.PopString(&interface_name) && |
| 146 reader.PopString(&property_name)) { | 146 reader.PopString(&property_name)) { |
| 147 // The device type is asked. Respond that the device type is wifi. | 147 // The device type is asked. Respond that the device type is wifi. |
| 148 dbus::Response* response = dbus::Response::CreateEmpty(); | 148 scoped_ptr<dbus::Response> response = dbus::Response::CreateEmpty(); |
| 149 dbus::MessageWriter writer(response); | 149 dbus::MessageWriter writer(response.get()); |
| 150 // This matches NM_DEVICE_TYPE_WIFI in wifi_data_provider_linux.cc. | 150 // This matches NM_DEVICE_TYPE_WIFI in wifi_data_provider_linux.cc. |
| 151 const int kDeviceTypeWifi = 2; | 151 const int kDeviceTypeWifi = 2; |
| 152 writer.AppendVariantOfUint32(kDeviceTypeWifi); | 152 writer.AppendVariantOfUint32(kDeviceTypeWifi); |
| 153 return response; | 153 return response.release(); |
| 154 } | 154 } |
| 155 } else if (method_call->GetInterface() == | 155 } else if (method_call->GetInterface() == |
| 156 "org.freedesktop.NetworkManager.Device.Wireless" && | 156 "org.freedesktop.NetworkManager.Device.Wireless" && |
| 157 method_call->GetMember() == "GetAccessPoints") { | 157 method_call->GetMember() == "GetAccessPoints") { |
| 158 // The list of access points is asked. Return the object path. | 158 // The list of access points is asked. Return the object path. |
| 159 dbus::Response* response = dbus::Response::CreateEmpty(); | 159 scoped_ptr<dbus::Response> response = dbus::Response::CreateEmpty(); |
| 160 dbus::MessageWriter writer(response); | 160 dbus::MessageWriter writer(response.get()); |
| 161 std::vector<dbus::ObjectPath> object_paths; | 161 std::vector<dbus::ObjectPath> object_paths; |
| 162 object_paths.push_back( | 162 object_paths.push_back( |
| 163 dbus::ObjectPath("/org/freedesktop/NetworkManager/AccessPoint/0")); | 163 dbus::ObjectPath("/org/freedesktop/NetworkManager/AccessPoint/0")); |
| 164 writer.AppendArrayOfObjectPaths(object_paths); | 164 writer.AppendArrayOfObjectPaths(object_paths); |
| 165 return response; | 165 return response.release(); |
| 166 } | 166 } |
| 167 | 167 |
| 168 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); | 168 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 169 return NULL; | 169 return NULL; |
| 170 } | 170 } |
| 171 | 171 |
| 172 | 172 |
| 173 // Creates a response for |mock_access_point_proxy_|. | 173 // Creates a response for |mock_access_point_proxy_|. |
| 174 dbus::Response* CreateAccessPointProxyResponse(dbus::MethodCall* method_call, | 174 dbus::Response* CreateAccessPointProxyResponse(dbus::MethodCall* method_call, |
| 175 Unused) { | 175 Unused) { |
| 176 if (method_call->GetInterface() == DBUS_INTERFACE_PROPERTIES && | 176 if (method_call->GetInterface() == DBUS_INTERFACE_PROPERTIES && |
| 177 method_call->GetMember() == "Get") { | 177 method_call->GetMember() == "Get") { |
| 178 dbus::MessageReader reader(method_call); | 178 dbus::MessageReader reader(method_call); |
| 179 | 179 |
| 180 std::string interface_name; | 180 std::string interface_name; |
| 181 std::string property_name; | 181 std::string property_name; |
| 182 if (reader.PopString(&interface_name) && | 182 if (reader.PopString(&interface_name) && |
| 183 reader.PopString(&property_name)) { | 183 reader.PopString(&property_name)) { |
| 184 dbus::Response* response = dbus::Response::CreateEmpty(); | 184 scoped_ptr<dbus::Response> response = dbus::Response::CreateEmpty(); |
| 185 dbus::MessageWriter writer(response); | 185 dbus::MessageWriter writer(response.get()); |
| 186 | 186 |
| 187 if (property_name == "Ssid") { | 187 if (property_name == "Ssid") { |
| 188 const uint8 kSsid[] = {0x74, 0x65, 0x73, 0x74}; // "test" | 188 const uint8 kSsid[] = {0x74, 0x65, 0x73, 0x74}; // "test" |
| 189 dbus::MessageWriter variant_writer(response); | 189 dbus::MessageWriter variant_writer(response.get()); |
| 190 writer.OpenVariant("ay", &variant_writer); | 190 writer.OpenVariant("ay", &variant_writer); |
| 191 variant_writer.AppendArrayOfBytes(kSsid, arraysize(kSsid)); | 191 variant_writer.AppendArrayOfBytes(kSsid, arraysize(kSsid)); |
| 192 writer.CloseContainer(&variant_writer); | 192 writer.CloseContainer(&variant_writer); |
| 193 } else if (property_name == "HwAddress") { | 193 } else if (property_name == "HwAddress") { |
| 194 // This will be converted to "00-11-22-33-44-55". | 194 // This will be converted to "00-11-22-33-44-55". |
| 195 const std::string kMacAddress = "00:11:22:33:44:55"; | 195 const std::string kMacAddress = "00:11:22:33:44:55"; |
| 196 writer.AppendVariantOfString(kMacAddress); | 196 writer.AppendVariantOfString(kMacAddress); |
| 197 } else if (property_name == "Strength") { | 197 } else if (property_name == "Strength") { |
| 198 // This will be converted to -50. | 198 // This will be converted to -50. |
| 199 const uint8 kStrength = 100; | 199 const uint8 kStrength = 100; |
| 200 writer.AppendVariantOfByte(kStrength); | 200 writer.AppendVariantOfByte(kStrength); |
| 201 } else if (property_name == "Frequency") { | 201 } else if (property_name == "Frequency") { |
| 202 // This will be converted to channel 4. | 202 // This will be converted to channel 4. |
| 203 const uint32 kFrequency = 2427; | 203 const uint32 kFrequency = 2427; |
| 204 writer.AppendVariantOfUint32(kFrequency); | 204 writer.AppendVariantOfUint32(kFrequency); |
| 205 } | 205 } |
| 206 return response; | 206 return response.release(); |
| 207 } | 207 } |
| 208 } | 208 } |
| 209 | 209 |
| 210 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); | 210 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 211 return NULL; | 211 return NULL; |
| 212 } | 212 } |
| 213 }; | 213 }; |
| 214 | 214 |
| 215 TEST_F(GeolocationWifiDataProviderLinuxTest, GetAccessPointData) { | 215 TEST_F(GeolocationWifiDataProviderLinuxTest, GetAccessPointData) { |
| 216 WifiData::AccessPointDataSet access_point_data_set; | 216 WifiData::AccessPointDataSet access_point_data_set; |
| 217 ASSERT_TRUE(wlan_api_->GetAccessPointData(&access_point_data_set)); | 217 ASSERT_TRUE(wlan_api_->GetAccessPointData(&access_point_data_set)); |
| 218 | 218 |
| 219 ASSERT_EQ(1U, access_point_data_set.size()); | 219 ASSERT_EQ(1U, access_point_data_set.size()); |
| 220 AccessPointData access_point_data = *access_point_data_set.begin(); | 220 AccessPointData access_point_data = *access_point_data_set.begin(); |
| 221 | 221 |
| 222 // Check the contents of the access point data. | 222 // Check the contents of the access point data. |
| 223 // The expected values come from CreateAccessPointProxyResponse() above. | 223 // The expected values come from CreateAccessPointProxyResponse() above. |
| 224 EXPECT_EQ("test", UTF16ToUTF8(access_point_data.ssid)); | 224 EXPECT_EQ("test", UTF16ToUTF8(access_point_data.ssid)); |
| 225 EXPECT_EQ("00-11-22-33-44-55", UTF16ToUTF8(access_point_data.mac_address)); | 225 EXPECT_EQ("00-11-22-33-44-55", UTF16ToUTF8(access_point_data.mac_address)); |
| 226 EXPECT_EQ(-50, access_point_data.radio_signal_strength); | 226 EXPECT_EQ(-50, access_point_data.radio_signal_strength); |
| 227 EXPECT_EQ(4, access_point_data.channel); | 227 EXPECT_EQ(4, access_point_data.channel); |
| 228 } | 228 } |
| 229 | 229 |
| 230 } // namespace content | 230 } // namespace content |
| OLD | NEW |