Set WebKeyboardEvent.domCode on Windows.
[chromium-blink-merge.git] / content / browser / geolocation / wifi_data_provider_common_unittest.cc
blob53d44bcc77018cce2ffc44f4ba9d1e3f64a2d962
1 // Copyright (c) 2012 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.
5 #include <vector>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
11 #include "content/browser/geolocation/wifi_data_provider_common.h"
12 #include "content/browser/geolocation/wifi_data_provider_manager.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using testing::_;
17 using testing::AtLeast;
18 using testing::DoDefault;
19 using testing::Invoke;
20 using testing::Return;
22 namespace content {
24 class MockWlanApi : public WifiDataProviderCommon::WlanApiInterface {
25 public:
26 MockWlanApi() : calls_(0), bool_return_(true) {
27 ANNOTATE_BENIGN_RACE(&calls_, "This is a test-only data race on a counter");
28 ON_CALL(*this, GetAccessPointData(_))
29 .WillByDefault(Invoke(this, &MockWlanApi::GetAccessPointDataInternal));
32 MOCK_METHOD1(GetAccessPointData, bool(WifiData::AccessPointDataSet* data));
34 int calls_;
35 bool bool_return_;
36 WifiData::AccessPointDataSet data_out_;
38 private:
39 bool GetAccessPointDataInternal(WifiData::AccessPointDataSet* data) {
40 ++calls_;
41 *data = data_out_;
42 return bool_return_;
46 class MockPollingPolicy : public WifiPollingPolicy {
47 public:
48 MockPollingPolicy() {
49 ON_CALL(*this,PollingInterval())
50 .WillByDefault(Return(1));
51 ON_CALL(*this,NoWifiInterval())
52 .WillByDefault(Return(1));
55 MOCK_METHOD0(PollingInterval, int());
56 MOCK_METHOD0(NoWifiInterval, int());
58 virtual void UpdatePollingInterval(bool) {}
61 // Stops the specified (nested) message loop when the callback is called.
62 class MessageLoopQuitter {
63 public:
64 explicit MessageLoopQuitter(base::MessageLoop* message_loop)
65 : message_loop_to_quit_(message_loop),
66 callback_(base::Bind(&MessageLoopQuitter::OnWifiDataUpdate,
67 base::Unretained(this))) {
68 CHECK(message_loop_to_quit_ != NULL);
71 void OnWifiDataUpdate() {
72 // Provider should call back on client's thread.
73 EXPECT_EQ(base::MessageLoop::current(), message_loop_to_quit_);
74 message_loop_to_quit_->QuitNow();
76 base::MessageLoop* message_loop_to_quit_;
77 WifiDataProviderManager::WifiDataUpdateCallback callback_;
80 class WifiDataProviderCommonWithMock : public WifiDataProviderCommon {
81 public:
82 WifiDataProviderCommonWithMock()
83 : new_wlan_api_(new MockWlanApi),
84 new_polling_policy_(new MockPollingPolicy) {}
86 // WifiDataProviderCommon
87 WlanApiInterface* NewWlanApi() override {
88 CHECK(new_wlan_api_ != NULL);
89 return new_wlan_api_.release();
91 WifiPollingPolicy* NewPollingPolicy() override {
92 CHECK(new_polling_policy_ != NULL);
93 return new_polling_policy_.release();
96 scoped_ptr<MockWlanApi> new_wlan_api_;
97 scoped_ptr<MockPollingPolicy> new_polling_policy_;
99 private:
100 ~WifiDataProviderCommonWithMock() override {}
102 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommonWithMock);
105 WifiDataProvider* CreateWifiDataProviderCommonWithMock() {
106 return new WifiDataProviderCommonWithMock;
109 // Main test fixture
110 class GeolocationWifiDataProviderCommonTest : public testing::Test {
111 public:
112 GeolocationWifiDataProviderCommonTest()
113 : loop_quitter_(&main_message_loop_) {
116 void SetUp() override {
117 provider_ = new WifiDataProviderCommonWithMock;
118 wlan_api_ = provider_->new_wlan_api_.get();
119 polling_policy_ = provider_->new_polling_policy_.get();
120 provider_->AddCallback(&loop_quitter_.callback_);
122 void TearDown() override {
123 provider_->RemoveCallback(&loop_quitter_.callback_);
124 provider_->StopDataProvider();
125 provider_ = NULL;
128 protected:
129 base::MessageLoop main_message_loop_;
130 MessageLoopQuitter loop_quitter_;
131 scoped_refptr<WifiDataProviderCommonWithMock> provider_;
132 MockWlanApi* wlan_api_;
133 MockPollingPolicy* polling_policy_;
136 TEST_F(GeolocationWifiDataProviderCommonTest, CreateDestroy) {
137 // Test fixture members were SetUp correctly.
138 EXPECT_EQ(&main_message_loop_, base::MessageLoop::current());
139 EXPECT_TRUE(NULL != provider_.get());
140 EXPECT_TRUE(NULL != wlan_api_);
143 TEST_F(GeolocationWifiDataProviderCommonTest, RunNormal) {
144 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
145 .Times(AtLeast(1));
146 EXPECT_CALL(*polling_policy_, PollingInterval())
147 .Times(AtLeast(1));
148 provider_->StartDataProvider();
149 main_message_loop_.Run();
150 SUCCEED();
153 TEST_F(GeolocationWifiDataProviderCommonTest, NoWifi){
154 EXPECT_CALL(*polling_policy_, NoWifiInterval())
155 .Times(AtLeast(1));
156 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
157 .WillRepeatedly(Return(false));
158 provider_->StartDataProvider();
159 main_message_loop_.Run();
162 TEST_F(GeolocationWifiDataProviderCommonTest, IntermittentWifi){
163 EXPECT_CALL(*polling_policy_, PollingInterval())
164 .Times(AtLeast(1));
165 EXPECT_CALL(*polling_policy_, NoWifiInterval())
166 .Times(1);
167 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
168 .WillOnce(Return(true))
169 .WillOnce(Return(false))
170 .WillRepeatedly(DoDefault());
172 AccessPointData single_access_point;
173 single_access_point.channel = 2;
174 single_access_point.mac_address = 3;
175 single_access_point.radio_signal_strength = 4;
176 single_access_point.signal_to_noise = 5;
177 single_access_point.ssid = base::ASCIIToUTF16("foossid");
178 wlan_api_->data_out_.insert(single_access_point);
180 provider_->StartDataProvider();
181 main_message_loop_.Run();
182 main_message_loop_.Run();
185 #if defined(OS_MACOSX)
186 #define MAYBE_DoAnEmptyScan DISABLED_DoAnEmptyScan
187 #else
188 #define MAYBE_DoAnEmptyScan DoAnEmptyScan
189 #endif
190 TEST_F(GeolocationWifiDataProviderCommonTest, MAYBE_DoAnEmptyScan) {
191 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
192 .Times(AtLeast(1));
193 EXPECT_CALL(*polling_policy_, PollingInterval())
194 .Times(AtLeast(1));
195 provider_->StartDataProvider();
196 main_message_loop_.Run();
197 EXPECT_EQ(wlan_api_->calls_, 1);
198 WifiData data;
199 EXPECT_TRUE(provider_->GetData(&data));
200 EXPECT_EQ(0, static_cast<int>(data.access_point_data.size()));
203 #if defined(OS_MACOSX)
204 #define MAYBE_DoScanWithResults DISABLED_DoScanWithResults
205 #else
206 #define MAYBE_DoScanWithResults DoScanWithResults
207 #endif
208 TEST_F(GeolocationWifiDataProviderCommonTest, MAYBE_DoScanWithResults) {
209 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
210 .Times(AtLeast(1));
211 EXPECT_CALL(*polling_policy_, PollingInterval())
212 .Times(AtLeast(1));
213 AccessPointData single_access_point;
214 single_access_point.channel = 2;
215 single_access_point.mac_address = 3;
216 single_access_point.radio_signal_strength = 4;
217 single_access_point.signal_to_noise = 5;
218 single_access_point.ssid = base::ASCIIToUTF16("foossid");
219 wlan_api_->data_out_.insert(single_access_point);
221 provider_->StartDataProvider();
222 main_message_loop_.Run();
223 EXPECT_EQ(wlan_api_->calls_, 1);
224 WifiData data;
225 EXPECT_TRUE(provider_->GetData(&data));
226 EXPECT_EQ(1, static_cast<int>(data.access_point_data.size()));
227 EXPECT_EQ(single_access_point.ssid, data.access_point_data.begin()->ssid);
230 TEST_F(GeolocationWifiDataProviderCommonTest, RegisterUnregister) {
231 MessageLoopQuitter loop_quitter(&main_message_loop_);
232 WifiDataProviderManager::SetFactoryForTesting(
233 CreateWifiDataProviderCommonWithMock);
234 WifiDataProviderManager::Register(&loop_quitter.callback_);
235 main_message_loop_.Run();
236 WifiDataProviderManager::Unregister(&loop_quitter.callback_);
237 WifiDataProviderManager::ResetFactoryForTesting();
240 } // namespace content