| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // Most logic for the platform wifi provider is now factored into |
| 6 // WifiDataProviderCommon and covered by it's unit tests. |
| 7 |
| 5 #include "chrome/browser/geolocation/wifi_data_provider_win.h" | 8 #include "chrome/browser/geolocation/wifi_data_provider_win.h" |
| 6 | 9 |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/scoped_ptr.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "chrome/browser/geolocation/wifi_data_provider_common.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 11 |
| 14 namespace { | 12 TEST(GeolocationWin32WifiDataProviderTest, CreateDestroy) { |
| 15 class MockWlanApi : public Win32WifiDataProvider::WlanApiInterface { | 13 // WifiDataProviderCommon requires the client to have a message loop. |
| 16 public: | 14 MessageLoop dummy_loop; |
| 17 MockWlanApi() : calls_(0), bool_return_(true) { | 15 scoped_refptr<Win32WifiDataProvider> instance(new Win32WifiDataProvider); |
| 18 } | 16 instance = NULL; |
| 19 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) { | 17 SUCCEED(); |
| 20 ++calls_; | 18 // Can't actually call start provider on the Win32WifiDataProvider without |
| 21 *data = data_out_; | 19 // it accessing hardware and so risking making the test flaky. |
| 22 return bool_return_; | |
| 23 } | |
| 24 int calls_; | |
| 25 bool bool_return_; | |
| 26 WifiData::AccessPointDataSet data_out_; | |
| 27 }; | |
| 28 | |
| 29 class MockPollingPolicy : public PollingPolicyInterface { | |
| 30 public: | |
| 31 virtual void UpdatePollingInterval(bool scan_results_differ) { | |
| 32 results_differed_.push_back(scan_results_differ); | |
| 33 } | |
| 34 virtual int PollingInterval() { return 1; } | |
| 35 std::vector<bool> results_differed_; | |
| 36 }; | |
| 37 | |
| 38 // Stops the specified (nested) message loop when the listener is called back. | |
| 39 class MessageLoopQuitListener | |
| 40 : public Win32WifiDataProvider::ListenerInterface { | |
| 41 public: | |
| 42 explicit MessageLoopQuitListener(MessageLoop* message_loop) | |
| 43 : message_loop_to_quit_(message_loop) { | |
| 44 CHECK(message_loop_to_quit_ != NULL); | |
| 45 } | |
| 46 // ListenerInterface | |
| 47 virtual void DeviceDataUpdateAvailable( | |
| 48 DeviceDataProvider<WifiData>* provider) { | |
| 49 // Provider should call back on client's thread. | |
| 50 EXPECT_EQ(MessageLoop::current(), message_loop_to_quit_); | |
| 51 provider_ = provider; | |
| 52 message_loop_to_quit_->Quit(); | |
| 53 } | |
| 54 MessageLoop* message_loop_to_quit_; | |
| 55 DeviceDataProvider<WifiData>* provider_; | |
| 56 }; | |
| 57 | |
| 58 Win32WifiDataProvider* CreateWin32WifiDataProvider(MockWlanApi** wlan_api_out) { | |
| 59 Win32WifiDataProvider* provider = new Win32WifiDataProvider; | |
| 60 *wlan_api_out = new MockWlanApi; | |
| 61 provider->inject_mock_wlan_api(*wlan_api_out); // Takes ownership. | |
| 62 provider->inject_mock_polling_policy(new MockPollingPolicy); // ditto | |
| 63 return provider; | |
| 64 } | 20 } |
| 65 | |
| 66 WifiDataProviderImplBase* CreateWin32WifiDataProvider() { | |
| 67 MockWlanApi* wlan_api; | |
| 68 return CreateWin32WifiDataProvider(&wlan_api); | |
| 69 } | |
| 70 } // namespace | |
| 71 | |
| 72 // Main test fixture | |
| 73 class GeolocationWin32WifiDataProviderTest : public testing::Test { | |
| 74 public: | |
| 75 virtual void SetUp() { | |
| 76 provider_ = CreateWin32WifiDataProvider(&wlan_api_); | |
| 77 } | |
| 78 virtual void TearDown() { | |
| 79 provider_->StopDataProvider(); | |
| 80 provider_ = NULL; | |
| 81 } | |
| 82 | |
| 83 protected: | |
| 84 MessageLoop main_message_loop_; | |
| 85 scoped_refptr<Win32WifiDataProvider> provider_; | |
| 86 MockWlanApi* wlan_api_; | |
| 87 }; | |
| 88 | |
| 89 TEST_F(GeolocationWin32WifiDataProviderTest, CreateDestroy) { | |
| 90 // Test fixture members were SetUp correctly. | |
| 91 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); | |
| 92 EXPECT_TRUE(NULL != provider_.get()); | |
| 93 EXPECT_TRUE(NULL != wlan_api_); | |
| 94 } | |
| 95 | |
| 96 TEST_F(GeolocationWin32WifiDataProviderTest, StartThread) { | |
| 97 EXPECT_TRUE(provider_->StartDataProvider()); | |
| 98 provider_->StopDataProvider(); | |
| 99 SUCCEED(); | |
| 100 } | |
| 101 | |
| 102 TEST_F(GeolocationWin32WifiDataProviderTest, DoAnEmptyScan) { | |
| 103 MessageLoopQuitListener quit_listener(&main_message_loop_); | |
| 104 provider_->AddListener(&quit_listener); | |
| 105 EXPECT_TRUE(provider_->StartDataProvider()); | |
| 106 main_message_loop_.Run(); | |
| 107 // Check we had at least one call. The worker thread may have raced ahead | |
| 108 // and made multiple calls. | |
| 109 EXPECT_GT(wlan_api_->calls_, 0); | |
| 110 WifiData data; | |
| 111 EXPECT_TRUE(provider_->GetData(&data)); | |
| 112 EXPECT_EQ(0, data.access_point_data.size()); | |
| 113 provider_->RemoveListener(&quit_listener); | |
| 114 } | |
| 115 | |
| 116 TEST_F(GeolocationWin32WifiDataProviderTest, DoScanWithResults) { | |
| 117 MessageLoopQuitListener quit_listener(&main_message_loop_); | |
| 118 provider_->AddListener(&quit_listener); | |
| 119 AccessPointData single_access_point; | |
| 120 single_access_point.age = 1; | |
| 121 single_access_point.channel = 2; | |
| 122 single_access_point.mac_address = 3; | |
| 123 single_access_point.radio_signal_strength = 4; | |
| 124 single_access_point.signal_to_noise = 5; | |
| 125 single_access_point.ssid = L"foossid"; | |
| 126 wlan_api_->data_out_.insert(single_access_point); | |
| 127 | |
| 128 EXPECT_TRUE(provider_->StartDataProvider()); | |
| 129 main_message_loop_.Run(); | |
| 130 EXPECT_GT(wlan_api_->calls_, 0); | |
| 131 WifiData data; | |
| 132 EXPECT_TRUE(provider_->GetData(&data)); | |
| 133 EXPECT_EQ(1, data.access_point_data.size()); | |
| 134 EXPECT_EQ(single_access_point.age, data.access_point_data.begin()->age); | |
| 135 EXPECT_EQ(single_access_point.ssid, data.access_point_data.begin()->ssid); | |
| 136 provider_->RemoveListener(&quit_listener); | |
| 137 } | |
| 138 | |
| 139 TEST_F(GeolocationWin32WifiDataProviderTest, StartThreadViaDeviceDataProvider) { | |
| 140 MessageLoopQuitListener quit_listener(&main_message_loop_); | |
| 141 WifiDataProvider::SetFactory(CreateWin32WifiDataProvider); | |
| 142 DeviceDataProvider<WifiData>::Register(&quit_listener); | |
| 143 main_message_loop_.Run(); | |
| 144 DeviceDataProvider<WifiData>::Unregister(&quit_listener); | |
| 145 DeviceDataProvider<WifiData>::ResetFactory(); | |
| 146 } | |
| OLD | NEW |