1 // Copyright (c) 2013 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/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/values.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 "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
17 class GeolocationHandlerTest
: public testing::Test
{
19 GeolocationHandlerTest() : manager_test_(NULL
) {
22 ~GeolocationHandlerTest() override
{}
24 void SetUp() override
{
25 // Initialize DBusThreadManager with a stub implementation.
26 DBusThreadManager::Initialize();
27 // Get the test interface for manager / device.
29 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
30 ASSERT_TRUE(manager_test_
);
31 geolocation_handler_
.reset(new GeolocationHandler());
32 geolocation_handler_
->Init();
33 message_loop_
.RunUntilIdle();
36 void TearDown() override
{
37 geolocation_handler_
.reset();
38 DBusThreadManager::Shutdown();
41 bool GetWifiAccessPoints() {
42 return geolocation_handler_
->GetWifiAccessPoints(
43 &wifi_access_points_
, NULL
);
46 void AddAccessPoint(int idx
) {
47 base::DictionaryValue properties
;
48 std::string mac_address
=
49 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X",
51 std::string channel
= base::StringPrintf("%d", idx
);
52 std::string strength
= base::StringPrintf("%d", idx
* 10);
53 properties
.SetStringWithoutPathExpansion(
54 shill::kGeoMacAddressProperty
, mac_address
);
55 properties
.SetStringWithoutPathExpansion(
56 shill::kGeoChannelProperty
, channel
);
57 properties
.SetStringWithoutPathExpansion(
58 shill::kGeoSignalStrengthProperty
, strength
);
59 manager_test_
->AddGeoNetwork(shill::kTypeWifi
, properties
);
60 message_loop_
.RunUntilIdle();
64 base::MessageLoopForUI message_loop_
;
65 scoped_ptr
<GeolocationHandler
> geolocation_handler_
;
66 ShillManagerClient::TestInterface
* manager_test_
;
67 WifiAccessPointVector wifi_access_points_
;
70 DISALLOW_COPY_AND_ASSIGN(GeolocationHandlerTest
);
73 TEST_F(GeolocationHandlerTest
, NoAccessPoints
) {
74 // Inititial call should return false.
75 EXPECT_FALSE(GetWifiAccessPoints());
76 message_loop_
.RunUntilIdle();
77 // Second call should return false since there are no devices.
78 EXPECT_FALSE(GetWifiAccessPoints());
81 TEST_F(GeolocationHandlerTest
, OneAccessPoint
) {
82 // Add an acces point.
84 message_loop_
.RunUntilIdle();
85 // Inititial call should return false and request access points.
86 EXPECT_FALSE(GetWifiAccessPoints());
87 message_loop_
.RunUntilIdle();
88 // Second call should return true since we have an access point.
89 EXPECT_TRUE(GetWifiAccessPoints());
90 ASSERT_EQ(1u, wifi_access_points_
.size());
91 EXPECT_EQ("01:00:00:00:00:00", wifi_access_points_
[0].mac_address
);
92 EXPECT_EQ(1, wifi_access_points_
[0].channel
);
95 TEST_F(GeolocationHandlerTest
, MultipleAccessPoints
) {
96 // Add several acces points.
100 message_loop_
.RunUntilIdle();
101 // Inititial call should return false and request access points.
102 EXPECT_FALSE(GetWifiAccessPoints());
103 message_loop_
.RunUntilIdle();
104 // Second call should return true since we have an access point.
105 EXPECT_TRUE(GetWifiAccessPoints());
106 ASSERT_EQ(3u, wifi_access_points_
.size());
107 EXPECT_EQ("02:00:00:00:00:00", wifi_access_points_
[1].mac_address
);
108 EXPECT_EQ(3, wifi_access_points_
[2].channel
);
111 } // namespace chromeos