12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
59ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chromeos/dbus/dbus_thread_manager.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chromeos/dbus/shill_manager_client.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chromeos/network/geolocation_handler.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/browser/geolocation/wifi_data_provider_chromeos.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "third_party/cros_system_api/dbus/service_constants.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace content {
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class GeolocationChromeOsWifiDataProviderTest : public testing::Test {
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GeolocationChromeOsWifiDataProviderTest() {
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetUp() OVERRIDE {
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    chromeos::DBusThreadManager::Initialize();
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    chromeos::NetworkHandler::Initialize();
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    manager_client_ =
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        chromeos::DBusThreadManager::Get()->GetShillManagerClient();
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    manager_test_ = manager_client_->GetTestInterface();
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    provider_ = new WifiDataProviderChromeOs();
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    message_loop_.RunUntilIdle();
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void TearDown() OVERRIDE {
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    provider_ = NULL;
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    chromeos::NetworkHandler::Shutdown();
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    chromeos::DBusThreadManager::Shutdown();
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool GetAccessPointData() {
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return provider_->GetAccessPointData(&ap_data_);
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void AddAccessPoints(int ssids, int aps_per_ssid) {
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (int i = 0; i < ssids; ++i) {
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (int j = 0; j < aps_per_ssid; ++j) {
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::DictionaryValue properties;
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        std::string mac_address =
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X",
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               i, j, 3, 4, 5, 6);
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        std::string channel = base::StringPrintf("%d", i * 10 + j);
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        std::string strength = base::StringPrintf("%d", i * 100 + j);
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        properties.SetStringWithoutPathExpansion(
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            shill::kGeoMacAddressProperty, mac_address);
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        properties.SetStringWithoutPathExpansion(
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            shill::kGeoChannelProperty, channel);
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        properties.SetStringWithoutPathExpansion(
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            shill::kGeoSignalStrengthProperty, strength);
5768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        manager_test_->AddGeoNetwork(shill::kTypeWifi, properties);
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    message_loop_.RunUntilIdle();
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MessageLoopForUI message_loop_;
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<WifiDataProviderChromeOs> provider_;
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  chromeos::ShillManagerClient* manager_client_;
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  chromeos::ShillManagerClient::TestInterface* manager_test_;
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WifiData::AccessPointDataSet ap_data_;
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(GeolocationChromeOsWifiDataProviderTest, NoAccessPoints) {
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  message_loop_.RunUntilIdle();
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Initial call to GetAccessPointData requests data and will return false.
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(GetAccessPointData());
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  message_loop_.RunUntilIdle();
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Additional call to GetAccessPointData also returns false with no devices.
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(GetAccessPointData());
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(0u, ap_data_.size());
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(GeolocationChromeOsWifiDataProviderTest, GetOneAccessPoint) {
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  message_loop_.RunUntilIdle();
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(GetAccessPointData());
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AddAccessPoints(1, 1);
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(GetAccessPointData());
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_EQ(1u, ap_data_.size());
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ("00:00:03:04:05:06",
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            base::UTF16ToUTF8(ap_data_.begin()->mac_address));
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(GeolocationChromeOsWifiDataProviderTest, GetManyAccessPoints) {
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  message_loop_.RunUntilIdle();
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(GetAccessPointData());
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AddAccessPoints(3, 4);
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(GetAccessPointData());
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_EQ(12u, ap_data_.size());
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace content
101