Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chromeos / network / geolocation_handler_unittest.cc
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.
4
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"
14
15 namespace chromeos {
16
17 class GeolocationHandlerTest : public testing::Test {
18  public:
19   GeolocationHandlerTest() : manager_test_(NULL) {
20   }
21
22   virtual ~GeolocationHandlerTest() {
23   }
24
25   virtual void SetUp() OVERRIDE {
26     // Initialize DBusThreadManager with a stub implementation.
27     DBusThreadManager::Initialize();
28     // Get the test interface for manager / device.
29     manager_test_ =
30         DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
31     ASSERT_TRUE(manager_test_);
32     geolocation_handler_.reset(new GeolocationHandler());
33     geolocation_handler_->Init();
34     message_loop_.RunUntilIdle();
35   }
36
37   virtual void TearDown() OVERRIDE {
38     geolocation_handler_.reset();
39     DBusThreadManager::Shutdown();
40   }
41
42   bool GetWifiAccessPoints() {
43     return geolocation_handler_->GetWifiAccessPoints(
44         &wifi_access_points_, NULL);
45   }
46
47   void AddAccessPoint(int idx) {
48     base::DictionaryValue properties;
49     std::string mac_address =
50         base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X",
51                            idx, 0, 0, 0, 0, 0);
52     std::string channel = base::StringPrintf("%d", idx);
53     std::string strength = base::StringPrintf("%d", 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();
62   }
63
64  protected:
65   base::MessageLoopForUI message_loop_;
66   scoped_ptr<GeolocationHandler> geolocation_handler_;
67   ShillManagerClient::TestInterface* manager_test_;
68   WifiAccessPointVector wifi_access_points_;
69
70  private:
71   DISALLOW_COPY_AND_ASSIGN(GeolocationHandlerTest);
72 };
73
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());
80 }
81
82 TEST_F(GeolocationHandlerTest, OneAccessPoint) {
83   // Add an acces point.
84   AddAccessPoint(1);
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);
94 }
95
96 TEST_F(GeolocationHandlerTest, MultipleAccessPoints) {
97   // Add several acces points.
98   AddAccessPoint(1);
99   AddAccessPoint(2);
100   AddAccessPoint(3);
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);
110 }
111
112 }  // namespace chromeos