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