Add some instrumentation for jank in URLRequest::Start.
[chromium-blink-merge.git] / chromeos / network / geolocation_handler_unittest.cc
blob97c6ac721572b1294ee09e10563c0cc3a83c4247
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"
15 namespace chromeos {
17 class GeolocationHandlerTest : public testing::Test {
18 public:
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.
28 manager_test_ =
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",
50 idx, 0, 0, 0, 0, 0);
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();
63 protected:
64 base::MessageLoopForUI message_loop_;
65 scoped_ptr<GeolocationHandler> geolocation_handler_;
66 ShillManagerClient::TestInterface* manager_test_;
67 WifiAccessPointVector wifi_access_points_;
69 private:
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.
83 AddAccessPoint(1);
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.
97 AddAccessPoint(1);
98 AddAccessPoint(2);
99 AddAccessPoint(3);
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