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