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 // ShutdownAndBlock() should be called. |
| 89 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return()); |
| 90 |
| 91 // Create the wlan API with the mock bus object injected. |
| 92 wifi_provider_linux_ = new WifiDataProviderLinux; |
| 93 wlan_api_.reset( |
| 94 wifi_provider_linux_->NewWlanApiForTesting(mock_bus_.get())); |
| 95 ASSERT_TRUE(wlan_api_.get()); |
| 96 } |
| 97 |
| 98 protected: |
| 99 // DeviceDataProviderImplBase, a super class of WifiDataProviderLinux, |
| 100 // requires a message loop to be present. message_loop_ is defined here, |
| 101 // as it should outlive wifi_provider_linux_. |
| 102 MessageLoop message_loop_; |
| 103 scoped_refptr<dbus::MockBus> mock_bus_; |
| 104 scoped_refptr<dbus::MockObjectProxy> mock_network_manager_proxy_; |
| 105 scoped_refptr<dbus::MockObjectProxy> mock_access_point_proxy_; |
| 106 scoped_refptr<dbus::MockObjectProxy> mock_device_proxy_; |
| 107 scoped_refptr<WifiDataProviderLinux> wifi_provider_linux_; |
| 108 scoped_ptr<WifiDataProviderCommon::WlanApiInterface> wlan_api_; |
| 109 |
| 110 private: |
| 111 // Creates a response for |mock_network_manager_proxy_|. |
| 112 dbus::Response* CreateNetowrkManagerProxyResponse( |
| 113 dbus::MethodCall* method_call, |
| 114 Unused) { |
| 115 if (method_call->GetInterface() == "org.freedesktop.NetworkManager" && |
| 116 method_call->GetMember() == "GetDevices") { |
| 117 // The list of devices is asked. Return the object path. |
| 118 std::vector<std::string> object_paths; |
| 119 object_paths.push_back("/org/freedesktop/NetworkManager/Devices/0"); |
| 120 |
| 121 dbus::Response* response = dbus::Response::CreateEmpty(); |
| 122 dbus::MessageWriter writer(response); |
| 123 writer.AppendArrayOfObjectPaths(object_paths); |
| 124 return response; |
| 125 } |
| 126 |
| 127 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 128 return NULL; |
| 129 } |
| 130 |
| 131 // Creates a response for |mock_device_proxy_|. |
| 132 dbus::Response* CreateDeviceProxyResponse(dbus::MethodCall* method_call, |
| 133 Unused) { |
| 134 if (method_call->GetInterface() == DBUS_INTERFACE_PROPERTIES && |
| 135 method_call->GetMember() == "Get") { |
| 136 dbus::MessageReader reader(method_call); |
| 137 std::string interface_name; |
| 138 std::string property_name; |
| 139 if (reader.PopString(&interface_name) && |
| 140 reader.PopString(&property_name)) { |
| 141 // The device type is asked. Respond that the device type is wifi. |
| 142 dbus::Response* response = dbus::Response::CreateEmpty(); |
| 143 dbus::MessageWriter writer(response); |
| 144 // This matches NM_DEVICE_TYPE_WIFI in wifi_data_provider_linux.cc. |
| 145 const int kDeviceTypeWifi = 2; |
| 146 writer.AppendVariantOfUint32(kDeviceTypeWifi); |
| 147 return response; |
| 148 } |
| 149 } else if (method_call->GetInterface() == |
| 150 "org.freedesktop.NetworkManager.Device.Wireless" && |
| 151 method_call->GetMember() == "GetAccessPoints") { |
| 152 // The list of access points is asked. Return the object path. |
| 153 dbus::Response* response = dbus::Response::CreateEmpty(); |
| 154 dbus::MessageWriter writer(response); |
| 155 std::vector<std::string> object_paths; |
| 156 object_paths.push_back("/org/freedesktop/NetworkManager/AccessPoint/0"); |
| 157 writer.AppendArrayOfObjectPaths(object_paths); |
| 158 return response; |
| 159 } |
| 160 |
| 161 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 162 return NULL; |
| 163 } |
| 164 |
| 165 |
| 166 // Creates a response for |mock_access_point_proxy_|. |
| 167 dbus::Response* CreateAccessPointProxyResponse(dbus::MethodCall* method_call, |
| 168 Unused) { |
| 169 if (method_call->GetInterface() == DBUS_INTERFACE_PROPERTIES && |
| 170 method_call->GetMember() == "Get") { |
| 171 dbus::MessageReader reader(method_call); |
| 172 |
| 173 std::string interface_name; |
| 174 std::string property_name; |
| 175 if (reader.PopString(&interface_name) && |
| 176 reader.PopString(&property_name)) { |
| 177 dbus::Response* response = dbus::Response::CreateEmpty(); |
| 178 dbus::MessageWriter writer(response); |
| 179 |
| 180 if (property_name == "Ssid") { |
| 181 const uint8 kSsid[] = {0x74, 0x65, 0x73, 0x74}; // "test" |
| 182 dbus::MessageWriter variant_writer(response); |
| 183 writer.OpenVariant("ay", &variant_writer); |
| 184 variant_writer.AppendArrayOfBytes(kSsid, arraysize(kSsid)); |
| 185 writer.CloseContainer(&variant_writer); |
| 186 } else if (property_name == "HwAddress") { |
| 187 // This will be converted to "00-11-22-33-44-55". |
| 188 const std::string kMacAddress = "00:11:22:33:44:55"; |
| 189 writer.AppendVariantOfString(kMacAddress); |
| 190 } else if (property_name == "Strength") { |
| 191 // This will be converted to -50. |
| 192 const uint8 kStrength = 100; |
| 193 writer.AppendVariantOfByte(kStrength); |
| 194 } else if (property_name == "Frequency") { |
| 195 // This will be converted to channel 4. |
| 196 const uint32 kFrequency = 2427; |
| 197 writer.AppendVariantOfUint32(kFrequency); |
| 198 } |
| 199 return response; |
| 200 } |
| 201 } |
| 202 |
| 203 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 204 return NULL; |
| 205 } |
| 206 }; |
| 207 |
| 208 TEST_F(GeolocationWifiDataProviderLinuxTest, GetAccessPointData) { |
| 209 WifiData::AccessPointDataSet access_point_data_set; |
| 210 ASSERT_TRUE(wlan_api_->GetAccessPointData(&access_point_data_set)); |
| 211 |
| 212 ASSERT_EQ(1U, access_point_data_set.size()); |
| 213 AccessPointData access_point_data = *access_point_data_set.begin(); |
| 214 |
| 215 // Check the contents of the access point data. |
| 216 // The expected values come from CreateAccessPointProxyResponse() above. |
| 217 EXPECT_EQ("test", UTF16ToUTF8(access_point_data.ssid)); |
| 218 EXPECT_EQ("00-11-22-33-44-55", UTF16ToUTF8(access_point_data.mac_address)); |
| 219 EXPECT_EQ(-50, access_point_data.radio_signal_strength); |
| 220 EXPECT_EQ(4, access_point_data.channel); |
| 221 } |
OLD | NEW |