Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/geolocation/wifi_data_provider_linux.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "dbus/message.h" | |
| 12 #include "dbus/mock_bus.h" | |
| 13 #include "dbus/mock_object_proxy.h" | |
| 14 #include "dbus/object_proxy.h" | |
| 15 #include "testing/gmock/include/gmock/gmock.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 using ::testing::_; | |
| 19 using ::testing::Invoke; | |
| 20 using ::testing::Return; | |
| 21 using ::testing::Unused; | |
| 22 | |
| 23 class GeolocationWifiDataProviderLinuxTest : public testing::Test { | |
| 24 void SetUp() { | |
| 25 // Create a mock bus. | |
| 26 dbus::Bus::Options options; | |
| 27 options.bus_type = dbus::Bus::SYSTEM; | |
| 28 mock_bus_ = new dbus::MockBus(options); | |
| 29 | |
| 30 // Create a mock proxy that behaves as NetworkManager. | |
| 31 mock_network_manager_proxy_ = | |
| 32 new dbus::MockObjectProxy(mock_bus_.get(), | |
| 33 "org.freedesktop.NetworkManager", | |
| 34 "/org/freedesktop/NetworkManager"); | |
| 35 // Set an expectation so mock_network_manager_proxy_'s | |
| 36 // CallMethodAndBlock() will use CreateNetowrkManagerProxyResponse() | |
| 37 // to return responses. | |
| 38 EXPECT_CALL(*mock_network_manager_proxy_, | |
| 39 CallMethodAndBlock(_, _)) | |
| 40 .WillRepeatedly(Invoke( | |
| 41 this, | |
| 42 &GeolocationWifiDataProviderLinuxTest:: | |
| 43 CreateNetowrkManagerProxyResponse)); | |
| 44 | |
| 45 // Create a mock proxy that behaves as NetworkManager/Devices/0. | |
| 46 mock_device_proxy_ = | |
| 47 new dbus::MockObjectProxy(mock_bus_.get(), | |
| 48 "org.freedesktop.NetworkManager", | |
| 49 "/org/freedesktop/NetworkManager/Devices/0"); | |
| 50 EXPECT_CALL(*mock_device_proxy_, | |
| 51 CallMethodAndBlock(_, _)) | |
| 52 .WillRepeatedly(Invoke( | |
| 53 this, | |
| 54 &GeolocationWifiDataProviderLinuxTest::CreateDeviceProxyResponse)); | |
| 55 | |
| 56 // Create a mock proxy that behaves as NetworkManager/AccessPoint/0. | |
| 57 mock_access_point_proxy_ = | |
| 58 new dbus::MockObjectProxy( | |
| 59 mock_bus_.get(), | |
| 60 "org.freedesktop.NetworkManager", | |
| 61 "/org/freedesktop/NetworkManager/AccessPoint/0"); | |
| 62 EXPECT_CALL(*mock_access_point_proxy_, | |
| 63 CallMethodAndBlock(_, _)) | |
| 64 .WillRepeatedly(Invoke( | |
| 65 this, | |
| 66 &GeolocationWifiDataProviderLinuxTest:: | |
| 67 CreateAccessPointProxyResponse)); | |
| 68 | |
| 69 // Set an expectation so mock_bus_'s GetObjectProxy() for the given | |
| 70 // service name and the object path will return | |
| 71 // mock_network_manager_proxy_. | |
| 72 EXPECT_CALL(*mock_bus_, GetObjectProxy( | |
| 73 "org.freedesktop.NetworkManager", | |
| 74 "/org/freedesktop/NetworkManager")) | |
| 75 .WillOnce(Return(mock_network_manager_proxy_.get())); | |
| 76 // Likewise, set an expectation for mock_device_proxy_. | |
| 77 EXPECT_CALL(*mock_bus_, GetObjectProxy( | |
| 78 "org.freedesktop.NetworkManager", | |
| 79 "/org/freedesktop/NetworkManager/Devices/0")) | |
| 80 .WillOnce(Return(mock_device_proxy_.get())) | |
| 81 .WillOnce(Return(mock_device_proxy_.get())); | |
| 82 // Likewise, set an expectation for mock_access_point_proxy_. | |
| 83 EXPECT_CALL(*mock_bus_, GetObjectProxy( | |
| 84 "org.freedesktop.NetworkManager", | |
| 85 "/org/freedesktop/NetworkManager/AccessPoint/0")) | |
| 86 .WillOnce(Return(mock_access_point_proxy_.get())); | |
| 87 | |
| 88 // Create the wlan API with the mock bus object injected. | |
| 89 wifi_provider_linux_ = new WifiDataProviderLinux; | |
| 90 wlan_api_.reset( | |
| 91 wifi_provider_linux_->NewWlanApiForTesting(mock_bus_.get())); | |
| 92 ASSERT_TRUE(wlan_api_.get()); | |
| 93 } | |
| 94 | |
| 95 protected: | |
| 96 // DeviceDataProviderImplBase, a super class of WifiDataProviderLinux, | |
| 97 // requires a message loop to be present. message_loop_ is defined here, | |
| 98 // as it should outlive wifi_provider_linux_. | |
| 99 MessageLoop message_loop_; | |
| 100 scoped_refptr<dbus::MockBus> mock_bus_; | |
| 101 scoped_refptr<dbus::MockObjectProxy> mock_network_manager_proxy_; | |
| 102 scoped_refptr<dbus::MockObjectProxy> mock_access_point_proxy_; | |
| 103 scoped_refptr<dbus::MockObjectProxy> mock_device_proxy_; | |
| 104 scoped_refptr<WifiDataProviderLinux> wifi_provider_linux_; | |
| 105 scoped_ptr<WifiDataProviderCommon::WlanApiInterface> wlan_api_; | |
| 106 | |
| 107 private: | |
| 108 // Creates a response for |mock_network_manager_proxy_|. | |
| 109 dbus::Response* CreateNetowrkManagerProxyResponse( | |
| 110 dbus::MethodCall* method_call, | |
| 111 Unused) { | |
| 112 if (method_call->GetInterface() == "org.freedesktop.NetworkManager" && | |
| 113 method_call->GetMember() == "GetDevices") { | |
| 114 // The list of devices is asked. Return the object path. | |
| 115 std::vector<std::string> object_paths; | |
| 116 object_paths.push_back("/org/freedesktop/NetworkManager/Devices/0"); | |
| 117 | |
| 118 dbus::Response* response = dbus::Response::CreateEmpty(); | |
| 119 dbus::MessageWriter writer(response); | |
| 120 writer.AppendArrayOfObjectPaths(object_paths); | |
| 121 return response; | |
| 122 } | |
| 123 | |
| 124 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); | |
| 125 return NULL; | |
| 126 } | |
| 127 | |
| 128 // Creates a response for |mock_device_proxy_|. | |
| 129 dbus::Response* CreateDeviceProxyResponse(dbus::MethodCall* method_call, | |
| 130 Unused) { | |
| 131 if (method_call->GetInterface() == DBUS_INTERFACE_PROPERTIES && | |
| 132 method_call->GetMember() == "Get") { | |
| 133 dbus::MessageReader reader(method_call); | |
| 134 std::string interface_name; | |
| 135 std::string property_name; | |
| 136 if (reader.PopString(&interface_name) && | |
| 137 reader.PopString(&property_name)) { | |
| 138 // The device type is asked. Respond that the device type is wifi. | |
| 139 dbus::Response* response = dbus::Response::CreateEmpty(); | |
| 140 dbus::MessageWriter writer(response); | |
| 141 const int kDeviceTypeWifi = 2; | |
|
stevenjb
2011/08/24 23:32:04
Is the '2' to match TYPE_WIFI in network_library.h
satorux1
2011/08/25 06:03:17
This matches NM_DEVICE_TYPE_WIFI in wifi_data_prov
| |
| 142 writer.AppendVariantOfUint32(kDeviceTypeWifi); | |
| 143 return response; | |
| 144 } | |
| 145 } else if (method_call->GetInterface() == | |
| 146 "org.freedesktop.NetworkManager.Device.Wireless" && | |
| 147 method_call->GetMember() == "GetAccessPoints") { | |
| 148 // The list of access points is asked. Return the object path. | |
| 149 dbus::Response* response = dbus::Response::CreateEmpty(); | |
| 150 dbus::MessageWriter writer(response); | |
| 151 std::vector<std::string> object_paths; | |
| 152 object_paths.push_back("/org/freedesktop/NetworkManager/AccessPoint/0"); | |
| 153 writer.AppendArrayOfObjectPaths(object_paths); | |
| 154 return response; | |
| 155 } | |
| 156 | |
| 157 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); | |
| 158 return NULL; | |
| 159 } | |
| 160 | |
| 161 | |
| 162 // Creates a response for |mock_access_point_proxy_|. | |
| 163 dbus::Response* CreateAccessPointProxyResponse(dbus::MethodCall* method_call, | |
| 164 Unused) { | |
| 165 if (method_call->GetInterface() == DBUS_INTERFACE_PROPERTIES && | |
| 166 method_call->GetMember() == "Get") { | |
| 167 dbus::MessageReader reader(method_call); | |
| 168 | |
| 169 std::string interface_name; | |
| 170 std::string property_name; | |
| 171 if (reader.PopString(&interface_name) && | |
| 172 reader.PopString(&property_name)) { | |
| 173 dbus::Response* response = dbus::Response::CreateEmpty(); | |
| 174 dbus::MessageWriter writer(response); | |
| 175 | |
| 176 if (property_name == "Ssid") { | |
| 177 uint8 ssid[] = {0x74, 0x65, 0x73, 0x74}; // "test" | |
|
stevenjb
2011/08/24 23:32:04
nit: Use consts for all of these, hear and below,
satorux1
2011/08/25 06:03:17
Done. Most values are converted to some other form
| |
| 178 dbus::MessageWriter variant_writer(response); | |
| 179 writer.OpenVariant("ay", &variant_writer); | |
| 180 variant_writer.AppendArrayOfBytes(ssid, arraysize(ssid)); | |
| 181 writer.CloseContainer(&variant_writer); | |
| 182 } else if (property_name == "HwAddress") { | |
| 183 // This will be converted to "00-11-22-33-44-55". | |
| 184 std::string mac_address = "00:11:22:33:44:55"; | |
| 185 writer.AppendVariantOfString(mac_address); | |
| 186 } else if (property_name == "Strength") { | |
| 187 // This will be converted to -50. | |
| 188 writer.AppendVariantOfByte(100); | |
| 189 } else if (property_name == "Frequency") { | |
| 190 // This will be converted to channel 4. | |
| 191 writer.AppendVariantOfUint32(2427); | |
| 192 } | |
| 193 return response; | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); | |
| 198 return NULL; | |
| 199 } | |
| 200 }; | |
| 201 | |
| 202 TEST_F(GeolocationWifiDataProviderLinuxTest, GetAccessPointData) { | |
| 203 WifiData::AccessPointDataSet access_point_data_set; | |
| 204 ASSERT_TRUE(wlan_api_->GetAccessPointData(&access_point_data_set)); | |
| 205 | |
| 206 ASSERT_EQ(1U, access_point_data_set.size()); | |
| 207 AccessPointData access_point_data = *access_point_data_set.begin(); | |
| 208 | |
| 209 // Check the contents of the access point data. | |
| 210 // The expected values come from CreateAccessPointProxyResponse() above. | |
| 211 EXPECT_EQ("test", UTF16ToUTF8(access_point_data.ssid)); | |
| 212 EXPECT_EQ("00-11-22-33-44-55", UTF16ToUTF8(access_point_data.mac_address)); | |
| 213 EXPECT_EQ(-50, access_point_data.radio_signal_strength); | |
| 214 EXPECT_EQ(4, access_point_data.channel); | |
| 215 } | |
| OLD | NEW |