diff options
author | allanwoj@chromium.org <allanwoj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-19 13:36:22 +0000 |
---|---|---|
committer | allanwoj@chromium.org <allanwoj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-19 13:36:22 +0000 |
commit | cd2e14a3b1acd5e672bb9cfd56da5567bd1071d7 (patch) | |
tree | f60c61ef0bb40d1efa757f12cd0142a86276ab00 /chrome | |
parent | 4a44bc89e1783eba04c4c4de898ee62d05607ff1 (diff) | |
download | chromium_src-cd2e14a3b1acd5e672bb9cfd56da5567bd1071d7.zip chromium_src-cd2e14a3b1acd5e672bb9cfd56da5567bd1071d7.tar.gz chromium_src-cd2e14a3b1acd5e672bb9cfd56da5567bd1071d7.tar.bz2 |
Patched from issue 2825039
BUG=44011
TEST=NoWifi and IntermittentWifi test fixtures in wifi_data_provider_common_unittest.cc
Review URL: http://codereview.chromium.org/2971006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52901 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
7 files changed, 118 insertions, 25 deletions
diff --git a/chrome/browser/geolocation/wifi_data_provider_chromeos.cc b/chrome/browser/geolocation/wifi_data_provider_chromeos.cc index 1a0343c..bb1d936 100644 --- a/chrome/browser/geolocation/wifi_data_provider_chromeos.cc +++ b/chrome/browser/geolocation/wifi_data_provider_chromeos.cc @@ -14,6 +14,7 @@ namespace { const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins +const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s } namespace chromeos { @@ -93,5 +94,6 @@ WifiDataProviderCommon::WlanApiInterface* PollingPolicyInterface* WifiDataProviderChromeOs::NewPollingPolicy() { return new GenericPollingPolicy<kDefaultPollingIntervalMilliseconds, kNoChangePollingIntervalMilliseconds, - kTwoNoChangePollingIntervalMilliseconds>; + kTwoNoChangePollingIntervalMilliseconds, + kNoWifiPollingIntervalMilliseconds>; } diff --git a/chrome/browser/geolocation/wifi_data_provider_common.cc b/chrome/browser/geolocation/wifi_data_provider_common.cc index a4c6a69..39df876 100644 --- a/chrome/browser/geolocation/wifi_data_provider_common.cc +++ b/chrome/browser/geolocation/wifi_data_provider_common.cc @@ -76,19 +76,21 @@ void WifiDataProviderCommon::CleanUp() { void WifiDataProviderCommon::DoWifiScanTask() { bool update_available = false; WifiData new_data; - if (wlan_api_->GetAccessPointData(&new_data.access_point_data)) { + if (!wlan_api_->GetAccessPointData(&new_data.access_point_data)) { + ScheduleNextScan(polling_policy_->NoWifiInterval()); + } else { { AutoLock lock(data_mutex_); update_available = wifi_data_.DiffersSignificantly(new_data); wifi_data_ = new_data; } - if (update_available || !is_first_scan_complete_) { - is_first_scan_complete_ = true; - NotifyListeners(); - } + polling_policy_->UpdatePollingInterval(update_available); + ScheduleNextScan(polling_policy_->PollingInterval()); + } + if (update_available || !is_first_scan_complete_) { + is_first_scan_complete_ = true; + NotifyListeners(); } - polling_policy_->UpdatePollingInterval(update_available); - ScheduleNextScan(polling_policy_->PollingInterval()); } void WifiDataProviderCommon::ScheduleNextScan(int interval) { diff --git a/chrome/browser/geolocation/wifi_data_provider_common.h b/chrome/browser/geolocation/wifi_data_provider_common.h index 086c149..77289f0 100644 --- a/chrome/browser/geolocation/wifi_data_provider_common.h +++ b/chrome/browser/geolocation/wifi_data_provider_common.h @@ -24,13 +24,15 @@ class PollingPolicyInterface { // interval and whether the last scan produced new results. virtual void UpdatePollingInterval(bool scan_results_differ) = 0; virtual int PollingInterval() = 0; + virtual int NoWifiInterval() = 0; }; // Generic polling policy, constants are compile-time parameterized to allow // tuning on a per-platform basis. template<int DEFAULT_INTERVAL, int NO_CHANGE_INTERVAL, - int TWO_NO_CHANGE_INTERVAL> + int TWO_NO_CHANGE_INTERVAL, + int NO_WIFI_INTERVAL> class GenericPollingPolicy : public PollingPolicyInterface { public: GenericPollingPolicy() : polling_interval_(DEFAULT_INTERVAL) {} @@ -47,6 +49,7 @@ class GenericPollingPolicy : public PollingPolicyInterface { } } virtual int PollingInterval() { return polling_interval_; } + virtual int NoWifiInterval() { return NO_WIFI_INTERVAL; } private: int polling_interval_; diff --git a/chrome/browser/geolocation/wifi_data_provider_common_unittest.cc b/chrome/browser/geolocation/wifi_data_provider_common_unittest.cc index be165a7..6947d41 100644 --- a/chrome/browser/geolocation/wifi_data_provider_common_unittest.cc +++ b/chrome/browser/geolocation/wifi_data_provider_common_unittest.cc @@ -9,21 +9,50 @@ #include "base/scoped_ptr.h" #include "base/string_util.h" #include "base/third_party/dynamic_annotations/dynamic_annotations.h" +#include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" +using testing::_; +using testing::AtLeast; +using testing::DoDefault; +using testing::Invoke; +using testing::Return; + class MockWlanApi : public WifiDataProviderCommon::WlanApiInterface { public: MockWlanApi() : calls_(0), bool_return_(true) { ANNOTATE_BENIGN_RACE(&calls_, "This is a test-only data race on a counter"); + ON_CALL(*this, GetAccessPointData(_)) + .WillByDefault(Invoke(this, &MockWlanApi::GetAccessPointDataInternal)); } - virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) { + + MOCK_METHOD1(GetAccessPointData, bool(WifiData::AccessPointDataSet* data)); + + int calls_; + bool bool_return_; + WifiData::AccessPointDataSet data_out_; + + private: + bool GetAccessPointDataInternal(WifiData::AccessPointDataSet* data) { ++calls_; *data = data_out_; return bool_return_; } - int calls_; - bool bool_return_; - WifiData::AccessPointDataSet data_out_; +}; + +class MockPollingPolicy :public PollingPolicyInterface { + public: + MockPollingPolicy() { + ON_CALL(*this,PollingInterval()) + .WillByDefault(Return(1)); + ON_CALL(*this,NoWifiInterval()) + .WillByDefault(Return(1)); + } + + MOCK_METHOD0(PollingInterval, int()); + MOCK_METHOD0(NoWifiInterval, int()); + + virtual void UpdatePollingInterval(bool) {} }; // Stops the specified (nested) message loop when the listener is called back. @@ -46,10 +75,12 @@ class MessageLoopQuitListener DeviceDataProvider<WifiData>* provider_; }; + class WifiDataProviderCommonWithMock : public WifiDataProviderCommon { public: WifiDataProviderCommonWithMock() - : new_wlan_api_(new MockWlanApi) {} + : new_wlan_api_(new MockWlanApi), + new_polling_policy_(new MockPollingPolicy) {} // WifiDataProviderCommon virtual WlanApiInterface* NewWlanApi() { @@ -57,15 +88,16 @@ class WifiDataProviderCommonWithMock : public WifiDataProviderCommon { return new_wlan_api_.release(); } virtual PollingPolicyInterface* NewPollingPolicy() { - return new GenericPollingPolicy<1, 2, 3>; + CHECK(new_polling_policy_ != NULL); + return new_polling_policy_.release(); } scoped_ptr<MockWlanApi> new_wlan_api_; + scoped_ptr<MockPollingPolicy> new_polling_policy_; DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommonWithMock); }; - WifiDataProviderImplBase* CreateWifiDataProviderCommonWithMock() { return new WifiDataProviderCommonWithMock; } @@ -73,19 +105,28 @@ WifiDataProviderImplBase* CreateWifiDataProviderCommonWithMock() { // Main test fixture class GeolocationWifiDataProviderCommonTest : public testing::Test { public: + GeolocationWifiDataProviderCommonTest() + : quit_listener_(&main_message_loop_) { + } + virtual void SetUp() { provider_ = new WifiDataProviderCommonWithMock; wlan_api_ = provider_->new_wlan_api_.get(); + polling_policy_ = provider_->new_polling_policy_.get(); + provider_->AddListener(&quit_listener_); } virtual void TearDown() { + provider_->RemoveListener(&quit_listener_); provider_->StopDataProvider(); provider_ = NULL; } protected: MessageLoop main_message_loop_; + MessageLoopQuitListener quit_listener_; scoped_refptr<WifiDataProviderCommonWithMock> provider_; MockWlanApi* wlan_api_; + MockPollingPolicy* polling_policy_; }; TEST_F(GeolocationWifiDataProviderCommonTest, CreateDestroy) { @@ -96,14 +137,52 @@ TEST_F(GeolocationWifiDataProviderCommonTest, CreateDestroy) { } TEST_F(GeolocationWifiDataProviderCommonTest, StartThread) { + EXPECT_CALL(*wlan_api_, GetAccessPointData(_)) + .Times(AtLeast(1)); + EXPECT_CALL(*polling_policy_, PollingInterval()) + .Times(AtLeast(1)); EXPECT_TRUE(provider_->StartDataProvider()); provider_->StopDataProvider(); SUCCEED(); } +TEST_F(GeolocationWifiDataProviderCommonTest, NoWifi){ + EXPECT_CALL(*polling_policy_, NoWifiInterval()) + .Times(AtLeast(1)); + EXPECT_CALL(*wlan_api_, GetAccessPointData(_)) + .WillRepeatedly(Return(false)); + provider_->StartDataProvider(); + main_message_loop_.Run(); +} + +TEST_F(GeolocationWifiDataProviderCommonTest, IntermittentWifi){ + EXPECT_CALL(*polling_policy_, PollingInterval()) + .Times(AtLeast(1)); + EXPECT_CALL(*polling_policy_, NoWifiInterval()) + .Times(1); + EXPECT_CALL(*wlan_api_, GetAccessPointData(_)) + .WillOnce(Return(true)) + .WillOnce(Return(false)) + .WillRepeatedly(DoDefault()); + + AccessPointData single_access_point; + single_access_point.channel = 2; + single_access_point.mac_address = 3; + single_access_point.radio_signal_strength = 4; + single_access_point.signal_to_noise = 5; + single_access_point.ssid = ASCIIToUTF16("foossid"); + wlan_api_->data_out_.insert(single_access_point); + + provider_->StartDataProvider(); + main_message_loop_.Run(); + main_message_loop_.Run(); +} + TEST_F(GeolocationWifiDataProviderCommonTest, DoAnEmptyScan) { - MessageLoopQuitListener quit_listener(&main_message_loop_); - provider_->AddListener(&quit_listener); + EXPECT_CALL(*wlan_api_, GetAccessPointData(_)) + .Times(AtLeast(1)); + EXPECT_CALL(*polling_policy_, PollingInterval()) + .Times(AtLeast(1)); EXPECT_TRUE(provider_->StartDataProvider()); main_message_loop_.Run(); // Check we had at least one call. The worker thread may have raced ahead @@ -112,12 +191,13 @@ TEST_F(GeolocationWifiDataProviderCommonTest, DoAnEmptyScan) { WifiData data; EXPECT_TRUE(provider_->GetData(&data)); EXPECT_EQ(0, static_cast<int>(data.access_point_data.size())); - provider_->RemoveListener(&quit_listener); } TEST_F(GeolocationWifiDataProviderCommonTest, DoScanWithResults) { - MessageLoopQuitListener quit_listener(&main_message_loop_); - provider_->AddListener(&quit_listener); + EXPECT_CALL(*wlan_api_, GetAccessPointData(_)) + .Times(AtLeast(1)); + EXPECT_CALL(*polling_policy_, PollingInterval()) + .Times(AtLeast(1)); AccessPointData single_access_point; single_access_point.channel = 2; single_access_point.mac_address = 3; @@ -133,9 +213,9 @@ TEST_F(GeolocationWifiDataProviderCommonTest, DoScanWithResults) { EXPECT_TRUE(provider_->GetData(&data)); EXPECT_EQ(1, static_cast<int>(data.access_point_data.size())); EXPECT_EQ(single_access_point.ssid, data.access_point_data.begin()->ssid); - provider_->RemoveListener(&quit_listener); } +// TODO(Joth) Convert to gmock style TEST_F(GeolocationWifiDataProviderCommonTest, StartThreadViaDeviceDataProvider) { MessageLoopQuitListener quit_listener(&main_message_loop_); diff --git a/chrome/browser/geolocation/wifi_data_provider_linux.cc b/chrome/browser/geolocation/wifi_data_provider_linux.cc index 14b265b..4bc3441 100644 --- a/chrome/browser/geolocation/wifi_data_provider_linux.cc +++ b/chrome/browser/geolocation/wifi_data_provider_linux.cc @@ -21,6 +21,7 @@ namespace { const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins +const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager"; const char kNetworkManagerPath[] = "/org/freedesktop/NetworkManager"; @@ -371,6 +372,7 @@ WifiDataProviderLinux::NewWlanApi() { PollingPolicyInterface* WifiDataProviderLinux::NewPollingPolicy() { return new GenericPollingPolicy<kDefaultPollingIntervalMilliseconds, kNoChangePollingIntervalMilliseconds, - kTwoNoChangePollingIntervalMilliseconds>; + kTwoNoChangePollingIntervalMilliseconds, + kNoWifiPollingIntervalMilliseconds>; } diff --git a/chrome/browser/geolocation/wifi_data_provider_mac.cc b/chrome/browser/geolocation/wifi_data_provider_mac.cc index 1e7b521..f99b45f 100644 --- a/chrome/browser/geolocation/wifi_data_provider_mac.cc +++ b/chrome/browser/geolocation/wifi_data_provider_mac.cc @@ -20,6 +20,7 @@ namespace { const int kDefaultPollingInterval = 120000; // 2 mins const int kNoChangePollingInterval = 300000; // 5 mins const int kTwoNoChangePollingInterval = 600000; // 10 mins +const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s // Provides the wifi API binding for use when running on OSX 10.5 machines using // the Apple80211 framework. @@ -178,5 +179,6 @@ MacWifiDataProvider::WlanApiInterface* MacWifiDataProvider::NewWlanApi() { PollingPolicyInterface* MacWifiDataProvider::NewPollingPolicy() { return new GenericPollingPolicy<kDefaultPollingInterval, kNoChangePollingInterval, - kTwoNoChangePollingInterval>; + kTwoNoChangePollingInterval, + kNoWifiPollingIntervalMilliseconds>; } diff --git a/chrome/browser/geolocation/wifi_data_provider_win.cc b/chrome/browser/geolocation/wifi_data_provider_win.cc index 2134d22..cfc2636 100644 --- a/chrome/browser/geolocation/wifi_data_provider_win.cc +++ b/chrome/browser/geolocation/wifi_data_provider_win.cc @@ -47,6 +47,7 @@ const int kStringLength = 512; const int kDefaultPollingInterval = 10000; // 10s const int kNoChangePollingInterval = 120000; // 2 mins const int kTwoNoChangePollingInterval = 600000; // 10 mins +const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s // WlanOpenHandle typedef DWORD (WINAPI* WlanOpenHandleFunction)(DWORD dwClientVersion, @@ -175,7 +176,8 @@ WifiDataProviderCommon::WlanApiInterface* Win32WifiDataProvider::NewWlanApi() { PollingPolicyInterface* Win32WifiDataProvider::NewPollingPolicy() { return new GenericPollingPolicy<kDefaultPollingInterval, kNoChangePollingInterval, - kTwoNoChangePollingInterval>; + kTwoNoChangePollingInterval, + kNoWifiPollingIntervalMilliseconds>; } // Local classes and functions |