wifi_data_provider_chromeos_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1fb144a0716afe7ab8bf245f2391a9e53b3db3c89fschneider@chromium.org// Copyright (c) 2011 The Chromium Authors. All rights reserved.
29a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// Use of this source code is governed by a BSD-style license that can be
39a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com// found in the LICENSE file.
49a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
59a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "base/message_loop/message_loop.h"
69a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "base/strings/stringprintf.h"
79a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "base/strings/utf_string_conversions.h"
89a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "chromeos/dbus/dbus_thread_manager.h"
99a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "chromeos/dbus/shill_manager_client.h"
109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "chromeos/network/geolocation_handler.h"
119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "content/browser/geolocation/wifi_data_provider_chromeos.h"
129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "testing/gtest/include/gtest/gtest.h"
139a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com#include "third_party/cros_system_api/dbus/service_constants.h"
149a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
159a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comnamespace content {
169a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
179a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comclass GeolocationChromeOsWifiDataProviderTest : public testing::Test {
189a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com protected:
199a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  GeolocationChromeOsWifiDataProviderTest() {
209a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  }
219a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
229a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  virtual void SetUp() OVERRIDE {
239a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    chromeos::DBusThreadManager::Initialize();
249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    chromeos::NetworkHandler::Initialize();
259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    manager_client_ =
269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        chromeos::DBusThreadManager::Get()->GetShillManagerClient();
279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    manager_test_ = manager_client_->GetTestInterface();
285ad5acef6bd4ebc785f946d8bcc2a88b1e031827ricow@chromium.org    provider_ = new WifiDataProviderChromeOs();
295ad5acef6bd4ebc785f946d8bcc2a88b1e031827ricow@chromium.org    message_loop_.RunUntilIdle();
309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  }
319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
329a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  virtual void TearDown() OVERRIDE {
339a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    provider_ = NULL;
349a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    chromeos::NetworkHandler::Shutdown();
35a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org    chromeos::DBusThreadManager::Shutdown();
36b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org  }
379a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
38a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org  bool GetAccessPointData() {
399a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    return provider_->GetAccessPointData(&ap_data_);
409a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  }
41a55512615f5adc085d23bc8589d155c4b579fb7bkasperl@chromium.org
429a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  void AddAccessPoints(int ssids, int aps_per_ssid) {
43b912362e2b2e704d09faac4290e027fd744bf587kasperl@chromium.org    for (int i = 0; i < ssids; ++i) {
44b912362e2b2e704d09faac4290e027fd744bf587kasperl@chromium.org      for (int j = 0; j < aps_per_ssid; ++j) {
459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        base::DictionaryValue properties;
469a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        std::string mac_address =
479a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com            base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X",
489a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com                               i, j, 3, 4, 5, 6);
495a8ca6c70c6fc9716f18f6223c98d1fef5752cf6kasperl@chromium.org        std::string channel = base::StringPrintf("%d", i * 10 + j);
509a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        std::string strength = base::StringPrintf("%d", i * 100 + j);
519a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        properties.SetStringWithoutPathExpansion(
529a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com            shill::kGeoMacAddressProperty, mac_address);
533a37e9b96c768f6b5b6b09542e1cb1a1ece7a022ager@chromium.org        properties.SetStringWithoutPathExpansion(
543a37e9b96c768f6b5b6b09542e1cb1a1ece7a022ager@chromium.org            shill::kGeoChannelProperty, channel);
559a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        properties.SetStringWithoutPathExpansion(
569a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com            shill::kGeoSignalStrengthProperty, strength);
579a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        manager_test_->AddGeoNetwork(shill::kTypeWifi, properties);
589a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com      }
5965dad4b091d2925543c6326db635d0f7cf9e1edcager@chromium.org    }
60c4c927273ae2b690c4a015b4640a2a469c9a1a69ager@chromium.org    message_loop_.RunUntilIdle();
619a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  }
629a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
639a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  base::MessageLoopForUI message_loop_;
649a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  scoped_refptr<WifiDataProviderChromeOs> provider_;
659a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  chromeos::ShillManagerClient* manager_client_;
669a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  chromeos::ShillManagerClient::TestInterface* manager_test_;
679a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  WifiData::AccessPointDataSet ap_data_;
689a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com};
699a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
709a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comTEST_F(GeolocationChromeOsWifiDataProviderTest, NoAccessPoints) {
719a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  message_loop_.RunUntilIdle();
729a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  // Initial call to GetAccessPointData requests data and will return false.
739a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  EXPECT_FALSE(GetAccessPointData());
749a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  message_loop_.RunUntilIdle();
759a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  // Additional call to GetAccessPointData also returns false with no devices.
769a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  EXPECT_FALSE(GetAccessPointData());
779a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  EXPECT_EQ(0u, ap_data_.size());
789a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
799a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
809a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comTEST_F(GeolocationChromeOsWifiDataProviderTest, GetOneAccessPoint) {
819a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  message_loop_.RunUntilIdle();
829a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  EXPECT_FALSE(GetAccessPointData());
839a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
849a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  AddAccessPoints(1, 1);
859a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  EXPECT_TRUE(GetAccessPointData());
869a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  ASSERT_EQ(1u, ap_data_.size());
879a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  EXPECT_EQ("00:00:03:04:05:06",
889a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com            base::UTF16ToUTF8(ap_data_.begin()->mac_address));
899a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
909a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
919a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comTEST_F(GeolocationChromeOsWifiDataProviderTest, GetManyAccessPoints) {
929a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  message_loop_.RunUntilIdle();
939a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  EXPECT_FALSE(GetAccessPointData());
949a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
959a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  AddAccessPoints(3, 4);
969a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  EXPECT_TRUE(GetAccessPointData());
979a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com  ASSERT_EQ(12u, ap_data_.size());
989a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
999a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1009a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}  // namespace content
1019a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com