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