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/string_number_conversions.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/values.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/shill_manager_client.h"
12 #include "chromeos/network/geolocation_handler.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
18 class GeolocationHandlerTest
: public testing::Test
{
20 GeolocationHandlerTest() : manager_test_(NULL
) {
23 ~GeolocationHandlerTest() override
{}
25 void SetUp() override
{
26 // Initialize DBusThreadManager with a stub implementation.
27 DBusThreadManager::Initialize();
28 // Get the test interface for manager / device.
30 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
31 ASSERT_TRUE(manager_test_
);
32 geolocation_handler_
.reset(new GeolocationHandler());
33 geolocation_handler_
->Init();
34 message_loop_
.RunUntilIdle();
37 void TearDown() override
{
38 geolocation_handler_
.reset();
39 DBusThreadManager::Shutdown();
42 bool GetWifiAccessPoints() {
43 return geolocation_handler_
->GetWifiAccessPoints(
44 &wifi_access_points_
, NULL
);
47 void AddAccessPoint(int idx
) {
48 base::DictionaryValue properties
;
49 std::string mac_address
=
50 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X",
52 std::string channel
= base::IntToString(idx
);
53 std::string strength
= base::IntToString(idx
* 10);
54 properties
.SetStringWithoutPathExpansion(
55 shill::kGeoMacAddressProperty
, mac_address
);
56 properties
.SetStringWithoutPathExpansion(
57 shill::kGeoChannelProperty
, channel
);
58 properties
.SetStringWithoutPathExpansion(
59 shill::kGeoSignalStrengthProperty
, strength
);
60 manager_test_
->AddGeoNetwork(shill::kTypeWifi
, properties
);
61 message_loop_
.RunUntilIdle();
65 base::MessageLoopForUI message_loop_
;
66 scoped_ptr
<GeolocationHandler
> geolocation_handler_
;
67 ShillManagerClient::TestInterface
* manager_test_
;
68 WifiAccessPointVector wifi_access_points_
;
71 DISALLOW_COPY_AND_ASSIGN(GeolocationHandlerTest
);
74 TEST_F(GeolocationHandlerTest
, NoAccessPoints
) {
75 // Inititial call should return false.
76 EXPECT_FALSE(GetWifiAccessPoints());
77 message_loop_
.RunUntilIdle();
78 // Second call should return false since there are no devices.
79 EXPECT_FALSE(GetWifiAccessPoints());
82 TEST_F(GeolocationHandlerTest
, OneAccessPoint
) {
83 // Add an acces point.
85 message_loop_
.RunUntilIdle();
86 // Inititial call should return false and request access points.
87 EXPECT_FALSE(GetWifiAccessPoints());
88 message_loop_
.RunUntilIdle();
89 // Second call should return true since we have an access point.
90 EXPECT_TRUE(GetWifiAccessPoints());
91 ASSERT_EQ(1u, wifi_access_points_
.size());
92 EXPECT_EQ("01:00:00:00:00:00", wifi_access_points_
[0].mac_address
);
93 EXPECT_EQ(1, wifi_access_points_
[0].channel
);
96 TEST_F(GeolocationHandlerTest
, MultipleAccessPoints
) {
97 // Add several acces points.
101 message_loop_
.RunUntilIdle();
102 // Inititial call should return false and request access points.
103 EXPECT_FALSE(GetWifiAccessPoints());
104 message_loop_
.RunUntilIdle();
105 // Second call should return true since we have an access point.
106 EXPECT_TRUE(GetWifiAccessPoints());
107 ASSERT_EQ(3u, wifi_access_points_
.size());
108 EXPECT_EQ("02:00:00:00:00:00", wifi_access_points_
[1].mac_address
);
109 EXPECT_EQ(3, wifi_access_points_
[2].channel
);
112 } // namespace chromeos