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.
5 #include "base/message_loop/message_loop.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/shill_manager_client.h"
11 #include "chromeos/network/geolocation_handler.h"
12 #include "content/browser/geolocation/wifi_data_provider_chromeos.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
18 class GeolocationChromeOsWifiDataProviderTest
: public testing::Test
{
20 GeolocationChromeOsWifiDataProviderTest() {
23 void SetUp() override
{
24 chromeos::DBusThreadManager::Initialize();
25 chromeos::NetworkHandler::Initialize();
27 chromeos::DBusThreadManager::Get()->GetShillManagerClient();
28 manager_test_
= manager_client_
->GetTestInterface();
29 provider_
= new WifiDataProviderChromeOs();
30 message_loop_
.RunUntilIdle();
33 void TearDown() override
{
35 chromeos::NetworkHandler::Shutdown();
36 chromeos::DBusThreadManager::Shutdown();
39 bool GetAccessPointData() {
40 return provider_
->GetAccessPointData(&ap_data_
);
43 void AddAccessPoints(int ssids
, int aps_per_ssid
) {
44 for (int i
= 0; i
< ssids
; ++i
) {
45 for (int j
= 0; j
< aps_per_ssid
; ++j
) {
46 base::DictionaryValue properties
;
47 std::string mac_address
=
48 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X",
50 std::string channel
= base::IntToString(i
* 10 + j
);
51 std::string strength
= base::IntToString(i
* 100 + j
);
52 properties
.SetStringWithoutPathExpansion(
53 shill::kGeoMacAddressProperty
, mac_address
);
54 properties
.SetStringWithoutPathExpansion(
55 shill::kGeoChannelProperty
, channel
);
56 properties
.SetStringWithoutPathExpansion(
57 shill::kGeoSignalStrengthProperty
, strength
);
58 manager_test_
->AddGeoNetwork(shill::kTypeWifi
, properties
);
61 message_loop_
.RunUntilIdle();
64 base::MessageLoopForUI message_loop_
;
65 scoped_refptr
<WifiDataProviderChromeOs
> provider_
;
66 chromeos::ShillManagerClient
* manager_client_
;
67 chromeos::ShillManagerClient::TestInterface
* manager_test_
;
68 WifiData::AccessPointDataSet ap_data_
;
71 TEST_F(GeolocationChromeOsWifiDataProviderTest
, NoAccessPoints
) {
72 message_loop_
.RunUntilIdle();
73 // Initial call to GetAccessPointData requests data and will return false.
74 EXPECT_FALSE(GetAccessPointData());
75 message_loop_
.RunUntilIdle();
76 // Additional call to GetAccessPointData also returns false with no devices.
77 EXPECT_FALSE(GetAccessPointData());
78 EXPECT_EQ(0u, ap_data_
.size());
81 TEST_F(GeolocationChromeOsWifiDataProviderTest
, GetOneAccessPoint
) {
82 message_loop_
.RunUntilIdle();
83 EXPECT_FALSE(GetAccessPointData());
85 AddAccessPoints(1, 1);
86 EXPECT_TRUE(GetAccessPointData());
87 ASSERT_EQ(1u, ap_data_
.size());
88 EXPECT_EQ("00:00:03:04:05:06",
89 base::UTF16ToUTF8(ap_data_
.begin()->mac_address
));
92 TEST_F(GeolocationChromeOsWifiDataProviderTest
, GetManyAccessPoints
) {
93 message_loop_
.RunUntilIdle();
94 EXPECT_FALSE(GetAccessPointData());
96 AddAccessPoints(3, 4);
97 EXPECT_TRUE(GetAccessPointData());
98 ASSERT_EQ(12u, ap_data_
.size());
101 } // namespace content