| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <vector> | |
| 6 | |
| 7 #include "base/scoped_ptr.h" | |
| 8 #include "base/string_util.h" | |
| 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "content/browser/geolocation/wifi_data_provider_common.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 using testing::_; | |
| 16 using testing::AtLeast; | |
| 17 using testing::DoDefault; | |
| 18 using testing::Invoke; | |
| 19 using testing::Return; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class MockWlanApi : public WifiDataProviderCommon::WlanApiInterface { | |
| 24 public: | |
| 25 MockWlanApi() : calls_(0), bool_return_(true) { | |
| 26 ANNOTATE_BENIGN_RACE(&calls_, "This is a test-only data race on a counter"); | |
| 27 ON_CALL(*this, GetAccessPointData(_)) | |
| 28 .WillByDefault(Invoke(this, &MockWlanApi::GetAccessPointDataInternal)); | |
| 29 } | |
| 30 | |
| 31 MOCK_METHOD1(GetAccessPointData, bool(WifiData::AccessPointDataSet* data)); | |
| 32 | |
| 33 int calls_; | |
| 34 bool bool_return_; | |
| 35 WifiData::AccessPointDataSet data_out_; | |
| 36 | |
| 37 private: | |
| 38 bool GetAccessPointDataInternal(WifiData::AccessPointDataSet* data) { | |
| 39 ++calls_; | |
| 40 *data = data_out_; | |
| 41 return bool_return_; | |
| 42 } | |
| 43 }; | |
| 44 | |
| 45 class MockPollingPolicy :public PollingPolicyInterface { | |
| 46 public: | |
| 47 MockPollingPolicy() { | |
| 48 ON_CALL(*this,PollingInterval()) | |
| 49 .WillByDefault(Return(1)); | |
| 50 ON_CALL(*this,NoWifiInterval()) | |
| 51 .WillByDefault(Return(1)); | |
| 52 } | |
| 53 | |
| 54 MOCK_METHOD0(PollingInterval, int()); | |
| 55 MOCK_METHOD0(NoWifiInterval, int()); | |
| 56 | |
| 57 virtual void UpdatePollingInterval(bool) {} | |
| 58 }; | |
| 59 | |
| 60 // Stops the specified (nested) message loop when the listener is called back. | |
| 61 class MessageLoopQuitListener | |
| 62 : public WifiDataProviderCommon::ListenerInterface { | |
| 63 public: | |
| 64 explicit MessageLoopQuitListener(MessageLoop* message_loop) | |
| 65 : message_loop_to_quit_(message_loop) { | |
| 66 CHECK(message_loop_to_quit_ != NULL); | |
| 67 } | |
| 68 // ListenerInterface | |
| 69 virtual void DeviceDataUpdateAvailable( | |
| 70 DeviceDataProvider<WifiData>* provider) { | |
| 71 // Provider should call back on client's thread. | |
| 72 EXPECT_EQ(MessageLoop::current(), message_loop_to_quit_); | |
| 73 provider_ = provider; | |
| 74 message_loop_to_quit_->QuitNow(); | |
| 75 } | |
| 76 MessageLoop* message_loop_to_quit_; | |
| 77 DeviceDataProvider<WifiData>* provider_; | |
| 78 }; | |
| 79 | |
| 80 | |
| 81 class WifiDataProviderCommonWithMock : public WifiDataProviderCommon { | |
| 82 public: | |
| 83 WifiDataProviderCommonWithMock() | |
| 84 : new_wlan_api_(new MockWlanApi), | |
| 85 new_polling_policy_(new MockPollingPolicy) {} | |
| 86 | |
| 87 // WifiDataProviderCommon | |
| 88 virtual WlanApiInterface* NewWlanApi() { | |
| 89 CHECK(new_wlan_api_ != NULL); | |
| 90 return new_wlan_api_.release(); | |
| 91 } | |
| 92 virtual PollingPolicyInterface* NewPollingPolicy() { | |
| 93 CHECK(new_polling_policy_ != NULL); | |
| 94 return new_polling_policy_.release(); | |
| 95 } | |
| 96 | |
| 97 scoped_ptr<MockWlanApi> new_wlan_api_; | |
| 98 scoped_ptr<MockPollingPolicy> new_polling_policy_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommonWithMock); | |
| 101 }; | |
| 102 | |
| 103 WifiDataProviderImplBase* CreateWifiDataProviderCommonWithMock() { | |
| 104 return new WifiDataProviderCommonWithMock; | |
| 105 } | |
| 106 | |
| 107 // Main test fixture | |
| 108 class GeolocationWifiDataProviderCommonTest : public testing::Test { | |
| 109 public: | |
| 110 GeolocationWifiDataProviderCommonTest() | |
| 111 : quit_listener_(&main_message_loop_) { | |
| 112 } | |
| 113 | |
| 114 virtual void SetUp() { | |
| 115 provider_ = new WifiDataProviderCommonWithMock; | |
| 116 wlan_api_ = provider_->new_wlan_api_.get(); | |
| 117 polling_policy_ = provider_->new_polling_policy_.get(); | |
| 118 provider_->AddListener(&quit_listener_); | |
| 119 } | |
| 120 virtual void TearDown() { | |
| 121 provider_->RemoveListener(&quit_listener_); | |
| 122 provider_->StopDataProvider(); | |
| 123 provider_ = NULL; | |
| 124 } | |
| 125 | |
| 126 protected: | |
| 127 MessageLoop main_message_loop_; | |
| 128 MessageLoopQuitListener quit_listener_; | |
| 129 scoped_refptr<WifiDataProviderCommonWithMock> provider_; | |
| 130 MockWlanApi* wlan_api_; | |
| 131 MockPollingPolicy* polling_policy_; | |
| 132 }; | |
| 133 | |
| 134 TEST_F(GeolocationWifiDataProviderCommonTest, CreateDestroy) { | |
| 135 // Test fixture members were SetUp correctly. | |
| 136 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); | |
| 137 EXPECT_TRUE(NULL != provider_.get()); | |
| 138 EXPECT_TRUE(NULL != wlan_api_); | |
| 139 } | |
| 140 | |
| 141 TEST_F(GeolocationWifiDataProviderCommonTest, StartThread) { | |
| 142 EXPECT_CALL(*wlan_api_, GetAccessPointData(_)) | |
| 143 .Times(AtLeast(1)); | |
| 144 EXPECT_CALL(*polling_policy_, PollingInterval()) | |
| 145 .Times(AtLeast(1)); | |
| 146 EXPECT_TRUE(provider_->StartDataProvider()); | |
| 147 main_message_loop_.Run(); | |
| 148 SUCCEED(); | |
| 149 } | |
| 150 | |
| 151 TEST_F(GeolocationWifiDataProviderCommonTest, NoWifi){ | |
| 152 EXPECT_CALL(*polling_policy_, NoWifiInterval()) | |
| 153 .Times(AtLeast(1)); | |
| 154 EXPECT_CALL(*wlan_api_, GetAccessPointData(_)) | |
| 155 .WillRepeatedly(Return(false)); | |
| 156 provider_->StartDataProvider(); | |
| 157 main_message_loop_.Run(); | |
| 158 } | |
| 159 | |
| 160 TEST_F(GeolocationWifiDataProviderCommonTest, IntermittentWifi){ | |
| 161 EXPECT_CALL(*polling_policy_, PollingInterval()) | |
| 162 .Times(AtLeast(1)); | |
| 163 EXPECT_CALL(*polling_policy_, NoWifiInterval()) | |
| 164 .Times(1); | |
| 165 EXPECT_CALL(*wlan_api_, GetAccessPointData(_)) | |
| 166 .WillOnce(Return(true)) | |
| 167 .WillOnce(Return(false)) | |
| 168 .WillRepeatedly(DoDefault()); | |
| 169 | |
| 170 AccessPointData single_access_point; | |
| 171 single_access_point.channel = 2; | |
| 172 single_access_point.mac_address = 3; | |
| 173 single_access_point.radio_signal_strength = 4; | |
| 174 single_access_point.signal_to_noise = 5; | |
| 175 single_access_point.ssid = ASCIIToUTF16("foossid"); | |
| 176 wlan_api_->data_out_.insert(single_access_point); | |
| 177 | |
| 178 provider_->StartDataProvider(); | |
| 179 main_message_loop_.Run(); | |
| 180 main_message_loop_.Run(); | |
| 181 } | |
| 182 | |
| 183 TEST_F(GeolocationWifiDataProviderCommonTest, DoAnEmptyScan) { | |
| 184 EXPECT_CALL(*wlan_api_, GetAccessPointData(_)) | |
| 185 .Times(AtLeast(1)); | |
| 186 EXPECT_CALL(*polling_policy_, PollingInterval()) | |
| 187 .Times(AtLeast(1)); | |
| 188 EXPECT_TRUE(provider_->StartDataProvider()); | |
| 189 main_message_loop_.Run(); | |
| 190 // Check we had at least one call. The worker thread may have raced ahead | |
| 191 // and made multiple calls. | |
| 192 EXPECT_GT(wlan_api_->calls_, 0); | |
| 193 WifiData data; | |
| 194 EXPECT_TRUE(provider_->GetData(&data)); | |
| 195 EXPECT_EQ(0, static_cast<int>(data.access_point_data.size())); | |
| 196 } | |
| 197 | |
| 198 TEST_F(GeolocationWifiDataProviderCommonTest, DoScanWithResults) { | |
| 199 EXPECT_CALL(*wlan_api_, GetAccessPointData(_)) | |
| 200 .Times(AtLeast(1)); | |
| 201 EXPECT_CALL(*polling_policy_, PollingInterval()) | |
| 202 .Times(AtLeast(1)); | |
| 203 AccessPointData single_access_point; | |
| 204 single_access_point.channel = 2; | |
| 205 single_access_point.mac_address = 3; | |
| 206 single_access_point.radio_signal_strength = 4; | |
| 207 single_access_point.signal_to_noise = 5; | |
| 208 single_access_point.ssid = ASCIIToUTF16("foossid"); | |
| 209 wlan_api_->data_out_.insert(single_access_point); | |
| 210 | |
| 211 EXPECT_TRUE(provider_->StartDataProvider()); | |
| 212 main_message_loop_.Run(); | |
| 213 EXPECT_GT(wlan_api_->calls_, 0); | |
| 214 WifiData data; | |
| 215 EXPECT_TRUE(provider_->GetData(&data)); | |
| 216 EXPECT_EQ(1, static_cast<int>(data.access_point_data.size())); | |
| 217 EXPECT_EQ(single_access_point.ssid, data.access_point_data.begin()->ssid); | |
| 218 } | |
| 219 | |
| 220 TEST_F(GeolocationWifiDataProviderCommonTest, | |
| 221 StartThreadViaDeviceDataProvider) { | |
| 222 MessageLoopQuitListener quit_listener(&main_message_loop_); | |
| 223 WifiDataProvider::SetFactory(CreateWifiDataProviderCommonWithMock); | |
| 224 DeviceDataProvider<WifiData>::Register(&quit_listener); | |
| 225 main_message_loop_.Run(); | |
| 226 DeviceDataProvider<WifiData>::Unregister(&quit_listener); | |
| 227 DeviceDataProvider<WifiData>::ResetFactory(); | |
| 228 } | |
| 229 | |
| 230 } // namespace | |
| 231 | |
| OLD | NEW |