wifi_data_provider_chromeos_unittest.cc 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include "services/device/geolocation/wifi_data_provider_chromeos.h"
  5. #include "base/run_loop.h"
  6. #include "base/strings/string_number_conversions.h"
  7. #include "base/strings/stringprintf.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "base/test/task_environment.h"
  10. #include "chromeos/ash/components/dbus/shill/shill_clients.h"
  11. #include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
  12. #include "chromeos/ash/components/network/geolocation_handler.h"
  13. #include "chromeos/ash/components/network/network_handler_test_helper.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. #include "third_party/cros_system_api/dbus/service_constants.h"
  16. namespace device {
  17. class GeolocationChromeOsWifiDataProviderTest : public testing::Test {
  18. protected:
  19. GeolocationChromeOsWifiDataProviderTest()
  20. : task_environment_(
  21. base::test::SingleThreadTaskEnvironment::MainThreadType::UI) {}
  22. void SetUp() override {
  23. provider_ = new WifiDataProviderChromeOs();
  24. base::RunLoop().RunUntilIdle();
  25. }
  26. void TearDown() override {
  27. provider_.reset();
  28. }
  29. bool GetAccessPointData() { return provider_->GetAccessPointData(&ap_data_); }
  30. void AddAccessPoints(int ssids, int aps_per_ssid) {
  31. for (int i = 0; i < ssids; ++i) {
  32. for (int j = 0; j < aps_per_ssid; ++j) {
  33. base::DictionaryValue properties;
  34. std::string mac_address = base::StringPrintf(
  35. "%02X:%02X:%02X:%02X:%02X:%02X", i, j, 3, 4, 5, 6);
  36. std::string channel = base::NumberToString(i * 10 + j);
  37. std::string strength = base::NumberToString(i * 100 + j);
  38. properties.SetKey(shill::kGeoMacAddressProperty,
  39. base::Value(mac_address));
  40. properties.SetKey(shill::kGeoChannelProperty, base::Value(channel));
  41. properties.SetKey(shill::kGeoSignalStrengthProperty,
  42. base::Value(strength));
  43. network_handler_test_helper_.manager_test()->AddGeoNetwork(
  44. shill::kGeoWifiAccessPointsProperty, properties);
  45. }
  46. }
  47. base::RunLoop().RunUntilIdle();
  48. }
  49. base::test::SingleThreadTaskEnvironment task_environment_;
  50. ash::NetworkHandlerTestHelper network_handler_test_helper_;
  51. scoped_refptr<WifiDataProviderChromeOs> provider_;
  52. ash::ShillManagerClient::TestInterface* manager_test_;
  53. WifiData::AccessPointDataSet ap_data_;
  54. };
  55. TEST_F(GeolocationChromeOsWifiDataProviderTest, NoAccessPoints) {
  56. base::RunLoop().RunUntilIdle();
  57. // Initial call to GetAccessPointData requests data and will return false.
  58. EXPECT_FALSE(GetAccessPointData());
  59. base::RunLoop().RunUntilIdle();
  60. // Additional call to GetAccessPointData also returns false with no devices.
  61. EXPECT_FALSE(GetAccessPointData());
  62. EXPECT_EQ(0u, ap_data_.size());
  63. }
  64. TEST_F(GeolocationChromeOsWifiDataProviderTest, GetOneAccessPoint) {
  65. base::RunLoop().RunUntilIdle();
  66. EXPECT_FALSE(GetAccessPointData());
  67. AddAccessPoints(1, 1);
  68. EXPECT_TRUE(GetAccessPointData());
  69. ASSERT_EQ(1u, ap_data_.size());
  70. EXPECT_EQ("00:00:03:04:05:06",
  71. base::UTF16ToUTF8(ap_data_.begin()->mac_address));
  72. }
  73. TEST_F(GeolocationChromeOsWifiDataProviderTest, GetManyAccessPoints) {
  74. base::RunLoop().RunUntilIdle();
  75. EXPECT_FALSE(GetAccessPointData());
  76. AddAccessPoints(3, 4);
  77. EXPECT_TRUE(GetAccessPointData());
  78. ASSERT_EQ(12u, ap_data_.size());
  79. }
  80. } // namespace device