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.
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"
17 using testing::AtLeast
;
18 using testing::DoDefault
;
19 using testing::Invoke
;
20 using testing::Return
;
24 class MockWlanApi
: public WifiDataProviderCommon::WlanApiInterface
{
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
));
36 WifiData::AccessPointDataSet data_out_
;
39 bool GetAccessPointDataInternal(WifiData::AccessPointDataSet
* data
) {
46 class MockPollingPolicy
: public WifiPollingPolicy
{
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
{
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
{
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_
;
100 ~WifiDataProviderCommonWithMock() override
{}
102 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommonWithMock
);
105 WifiDataProvider
* CreateWifiDataProviderCommonWithMock() {
106 return new WifiDataProviderCommonWithMock
;
110 class GeolocationWifiDataProviderCommonTest
: public testing::Test
{
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();
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(_
))
146 EXPECT_CALL(*polling_policy_
, PollingInterval())
148 provider_
->StartDataProvider();
149 main_message_loop_
.Run();
153 TEST_F(GeolocationWifiDataProviderCommonTest
, NoWifi
){
154 EXPECT_CALL(*polling_policy_
, NoWifiInterval())
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())
165 EXPECT_CALL(*polling_policy_
, NoWifiInterval())
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
188 #define MAYBE_DoAnEmptyScan DoAnEmptyScan
190 TEST_F(GeolocationWifiDataProviderCommonTest
, MAYBE_DoAnEmptyScan
) {
191 EXPECT_CALL(*wlan_api_
, GetAccessPointData(_
))
193 EXPECT_CALL(*polling_policy_
, PollingInterval())
195 provider_
->StartDataProvider();
196 main_message_loop_
.Run();
197 EXPECT_EQ(wlan_api_
->calls_
, 1);
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
206 #define MAYBE_DoScanWithResults DoScanWithResults
208 TEST_F(GeolocationWifiDataProviderCommonTest
, MAYBE_DoScanWithResults
) {
209 EXPECT_CALL(*wlan_api_
, GetAccessPointData(_
))
211 EXPECT_CALL(*polling_policy_
, PollingInterval())
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);
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