Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/network/geolocation_handler.h" | 5 #include "chromeos/network/geolocation_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" | 15 #include "chromeos/dbus/dbus_thread_manager.h" |
| 15 #include "chromeos/dbus/shill_manager_client.h" | 16 #include "chromeos/dbus/shill_manager_client.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" | 18 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 | 19 |
| 19 namespace chromeos { | 20 namespace chromeos { |
| 20 | 21 |
| 21 class GeolocationHandlerTest : public testing::Test { | 22 class GeolocationHandlerTest : public testing::Test { |
| 22 public: | 23 public: |
| 23 GeolocationHandlerTest() : manager_test_(NULL) { | 24 GeolocationHandlerTest() : manager_test_(NULL) { |
| 24 } | 25 } |
| 25 | 26 |
| 26 ~GeolocationHandlerTest() override {} | 27 ~GeolocationHandlerTest() override {} |
| 27 | 28 |
| 28 void SetUp() override { | 29 void SetUp() override { |
| 29 // Initialize DBusThreadManager with a stub implementation. | 30 // Initialize DBusThreadManager with a stub implementation. |
| 30 DBusThreadManager::Initialize(); | 31 DBusThreadManager::Initialize(); |
| 31 // Get the test interface for manager / device. | 32 // Get the test interface for manager / device. |
| 32 manager_test_ = | 33 manager_test_ = |
| 33 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); | 34 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); |
| 34 ASSERT_TRUE(manager_test_); | 35 ASSERT_TRUE(manager_test_); |
| 35 geolocation_handler_.reset(new GeolocationHandler()); | 36 geolocation_handler_.reset(new GeolocationHandler()); |
| 36 geolocation_handler_->Init(); | 37 geolocation_handler_->Init(); |
| 37 message_loop_.RunUntilIdle(); | 38 base::RunLoop().RunUntilIdle(); |
| 38 } | 39 } |
| 39 | 40 |
| 40 void TearDown() override { | 41 void TearDown() override { |
| 41 geolocation_handler_.reset(); | 42 geolocation_handler_.reset(); |
| 42 DBusThreadManager::Shutdown(); | 43 DBusThreadManager::Shutdown(); |
| 43 } | 44 } |
| 44 | 45 |
| 45 bool GetWifiAccessPoints() { | 46 bool GetWifiAccessPoints() { |
| 46 return geolocation_handler_->GetWifiAccessPoints( | 47 return geolocation_handler_->GetWifiAccessPoints( |
| 47 &wifi_access_points_, NULL); | 48 &wifi_access_points_, NULL); |
| 48 } | 49 } |
| 49 | 50 |
| 50 void AddAccessPoint(int idx) { | 51 void AddAccessPoint(int idx) { |
| 51 base::DictionaryValue properties; | 52 base::DictionaryValue properties; |
| 52 std::string mac_address = | 53 std::string mac_address = |
| 53 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", | 54 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", |
| 54 idx, 0, 0, 0, 0, 0); | 55 idx, 0, 0, 0, 0, 0); |
| 55 std::string channel = base::IntToString(idx); | 56 std::string channel = base::IntToString(idx); |
| 56 std::string strength = base::IntToString(idx * 10); | 57 std::string strength = base::IntToString(idx * 10); |
| 57 properties.SetStringWithoutPathExpansion( | 58 properties.SetStringWithoutPathExpansion( |
| 58 shill::kGeoMacAddressProperty, mac_address); | 59 shill::kGeoMacAddressProperty, mac_address); |
| 59 properties.SetStringWithoutPathExpansion( | 60 properties.SetStringWithoutPathExpansion( |
| 60 shill::kGeoChannelProperty, channel); | 61 shill::kGeoChannelProperty, channel); |
| 61 properties.SetStringWithoutPathExpansion( | 62 properties.SetStringWithoutPathExpansion( |
| 62 shill::kGeoSignalStrengthProperty, strength); | 63 shill::kGeoSignalStrengthProperty, strength); |
| 63 manager_test_->AddGeoNetwork(shill::kTypeWifi, properties); | 64 manager_test_->AddGeoNetwork(shill::kTypeWifi, properties); |
| 64 message_loop_.RunUntilIdle(); | 65 base::RunLoop().RunUntilIdle(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 protected: | 68 protected: |
| 68 base::MessageLoopForUI message_loop_; | 69 base::MessageLoopForUI message_loop_; |
|
armansito
2016/09/06 22:04:32
Shouldn't this be removed as well?
fdoray
2016/09/07 13:03:48
No. A base::RunLoop must be instantiated within th
| |
| 69 std::unique_ptr<GeolocationHandler> geolocation_handler_; | 70 std::unique_ptr<GeolocationHandler> geolocation_handler_; |
| 70 ShillManagerClient::TestInterface* manager_test_; | 71 ShillManagerClient::TestInterface* manager_test_; |
| 71 WifiAccessPointVector wifi_access_points_; | 72 WifiAccessPointVector wifi_access_points_; |
| 72 | 73 |
| 73 private: | 74 private: |
| 74 DISALLOW_COPY_AND_ASSIGN(GeolocationHandlerTest); | 75 DISALLOW_COPY_AND_ASSIGN(GeolocationHandlerTest); |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 TEST_F(GeolocationHandlerTest, NoAccessPoints) { | 78 TEST_F(GeolocationHandlerTest, NoAccessPoints) { |
| 78 // Inititial call should return false. | 79 // Inititial call should return false. |
| 79 EXPECT_FALSE(GetWifiAccessPoints()); | 80 EXPECT_FALSE(GetWifiAccessPoints()); |
| 80 message_loop_.RunUntilIdle(); | 81 base::RunLoop().RunUntilIdle(); |
| 81 // Second call should return false since there are no devices. | 82 // Second call should return false since there are no devices. |
| 82 EXPECT_FALSE(GetWifiAccessPoints()); | 83 EXPECT_FALSE(GetWifiAccessPoints()); |
| 83 } | 84 } |
| 84 | 85 |
| 85 TEST_F(GeolocationHandlerTest, OneAccessPoint) { | 86 TEST_F(GeolocationHandlerTest, OneAccessPoint) { |
| 86 // Add an acces point. | 87 // Add an acces point. |
| 87 AddAccessPoint(1); | 88 AddAccessPoint(1); |
| 88 message_loop_.RunUntilIdle(); | 89 base::RunLoop().RunUntilIdle(); |
| 89 // Inititial call should return false and request access points. | 90 // Inititial call should return false and request access points. |
| 90 EXPECT_FALSE(GetWifiAccessPoints()); | 91 EXPECT_FALSE(GetWifiAccessPoints()); |
| 91 message_loop_.RunUntilIdle(); | 92 base::RunLoop().RunUntilIdle(); |
| 92 // Second call should return true since we have an access point. | 93 // Second call should return true since we have an access point. |
| 93 EXPECT_TRUE(GetWifiAccessPoints()); | 94 EXPECT_TRUE(GetWifiAccessPoints()); |
| 94 ASSERT_EQ(1u, wifi_access_points_.size()); | 95 ASSERT_EQ(1u, wifi_access_points_.size()); |
| 95 EXPECT_EQ("01:00:00:00:00:00", wifi_access_points_[0].mac_address); | 96 EXPECT_EQ("01:00:00:00:00:00", wifi_access_points_[0].mac_address); |
| 96 EXPECT_EQ(1, wifi_access_points_[0].channel); | 97 EXPECT_EQ(1, wifi_access_points_[0].channel); |
| 97 } | 98 } |
| 98 | 99 |
| 99 TEST_F(GeolocationHandlerTest, MultipleAccessPoints) { | 100 TEST_F(GeolocationHandlerTest, MultipleAccessPoints) { |
| 100 // Add several acces points. | 101 // Add several acces points. |
| 101 AddAccessPoint(1); | 102 AddAccessPoint(1); |
| 102 AddAccessPoint(2); | 103 AddAccessPoint(2); |
| 103 AddAccessPoint(3); | 104 AddAccessPoint(3); |
| 104 message_loop_.RunUntilIdle(); | 105 base::RunLoop().RunUntilIdle(); |
| 105 // Inititial call should return false and request access points. | 106 // Inititial call should return false and request access points. |
| 106 EXPECT_FALSE(GetWifiAccessPoints()); | 107 EXPECT_FALSE(GetWifiAccessPoints()); |
| 107 message_loop_.RunUntilIdle(); | 108 base::RunLoop().RunUntilIdle(); |
| 108 // Second call should return true since we have an access point. | 109 // Second call should return true since we have an access point. |
| 109 EXPECT_TRUE(GetWifiAccessPoints()); | 110 EXPECT_TRUE(GetWifiAccessPoints()); |
| 110 ASSERT_EQ(3u, wifi_access_points_.size()); | 111 ASSERT_EQ(3u, wifi_access_points_.size()); |
| 111 EXPECT_EQ("02:00:00:00:00:00", wifi_access_points_[1].mac_address); | 112 EXPECT_EQ("02:00:00:00:00:00", wifi_access_points_[1].mac_address); |
| 112 EXPECT_EQ(3, wifi_access_points_[2].channel); | 113 EXPECT_EQ(3, wifi_access_points_[2].channel); |
| 113 } | 114 } |
| 114 | 115 |
| 115 } // namespace chromeos | 116 } // namespace chromeos |
| OLD | NEW |