OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "device/geolocation/wifi_data_provider_common.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/macros.h" | |
10 #include "base/run_loop.h" | |
11 #include "base/single_thread_task_runner.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "device/geolocation/wifi_data_provider_manager.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using testing::_; | |
20 using testing::AtLeast; | |
21 using testing::DoDefault; | |
22 using testing::Invoke; | |
23 using testing::Return; | |
24 | |
25 namespace device { | |
26 | |
27 class MockWlanApi : public WifiDataProviderCommon::WlanApiInterface { | |
28 public: | |
29 MockWlanApi() : calls_(0), bool_return_(true) { | |
30 ANNOTATE_BENIGN_RACE(&calls_, "This is a test-only data race on a counter"); | |
31 ON_CALL(*this, GetAccessPointData(_)) | |
32 .WillByDefault(Invoke(this, &MockWlanApi::GetAccessPointDataInternal)); | |
33 } | |
34 | |
35 MOCK_METHOD1(GetAccessPointData, bool(WifiData::AccessPointDataSet* data)); | |
36 | |
37 int calls_; | |
38 bool bool_return_; | |
39 WifiData::AccessPointDataSet data_out_; | |
40 | |
41 private: | |
42 bool GetAccessPointDataInternal(WifiData::AccessPointDataSet* data) { | |
43 ++calls_; | |
44 *data = data_out_; | |
45 return bool_return_; | |
46 } | |
47 }; | |
48 | |
49 class MockPollingPolicy : public WifiPollingPolicy { | |
50 public: | |
51 MockPollingPolicy() { | |
52 ON_CALL(*this,PollingInterval()) | |
53 .WillByDefault(Return(1)); | |
54 ON_CALL(*this,NoWifiInterval()) | |
55 .WillByDefault(Return(1)); | |
56 } | |
57 | |
58 MOCK_METHOD0(PollingInterval, int()); | |
59 MOCK_METHOD0(NoWifiInterval, int()); | |
60 | |
61 virtual void UpdatePollingInterval(bool) {} | |
62 }; | |
63 | |
64 class WifiDataProviderCommonWithMock : public WifiDataProviderCommon { | |
65 public: | |
66 WifiDataProviderCommonWithMock() | |
67 : new_wlan_api_(new MockWlanApi), | |
68 new_polling_policy_(new MockPollingPolicy) {} | |
69 | |
70 // WifiDataProviderCommon | |
71 WlanApiInterface* NewWlanApi() override { | |
72 CHECK(new_wlan_api_ != NULL); | |
73 return new_wlan_api_.release(); | |
74 } | |
75 WifiPollingPolicy* NewPollingPolicy() override { | |
76 CHECK(new_polling_policy_ != NULL); | |
77 return new_polling_policy_.release(); | |
78 } | |
79 | |
80 std::unique_ptr<MockWlanApi> new_wlan_api_; | |
81 std::unique_ptr<MockPollingPolicy> new_polling_policy_; | |
82 | |
83 private: | |
84 ~WifiDataProviderCommonWithMock() override {} | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommonWithMock); | |
87 }; | |
88 | |
89 WifiDataProvider* CreateWifiDataProviderCommonWithMock() { | |
90 return new WifiDataProviderCommonWithMock; | |
91 } | |
92 | |
93 // Main test fixture | |
94 class GeolocationWifiDataProviderCommonTest : public testing::Test { | |
95 public: | |
96 GeolocationWifiDataProviderCommonTest() | |
97 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
98 wifi_data_callback_( | |
99 base::Bind(&GeolocationWifiDataProviderCommonTest::OnWifiDataUpdate, | |
100 base::Unretained(this))) {} | |
101 | |
102 void SetUp() override { | |
103 provider_ = new WifiDataProviderCommonWithMock; | |
104 wlan_api_ = provider_->new_wlan_api_.get(); | |
105 polling_policy_ = provider_->new_polling_policy_.get(); | |
106 provider_->AddCallback(&wifi_data_callback_); | |
107 } | |
108 | |
109 void TearDown() override { | |
110 provider_->RemoveCallback(&wifi_data_callback_); | |
111 provider_->StopDataProvider(); | |
112 provider_ = NULL; | |
113 } | |
114 | |
115 void OnWifiDataUpdate() { | |
116 // Callbacks must run on the originating thread. | |
117 EXPECT_TRUE(main_task_runner_->BelongsToCurrentThread()); | |
118 run_loop_->Quit(); | |
119 } | |
120 | |
121 void RunLoop() { | |
122 run_loop_.reset(new base::RunLoop()); | |
123 run_loop_->Run(); | |
124 } | |
125 | |
126 protected: | |
127 base::MessageLoopForUI message_loop_; | |
128 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
129 std::unique_ptr<base::RunLoop> run_loop_; | |
130 WifiDataProviderManager::WifiDataUpdateCallback wifi_data_callback_; | |
131 scoped_refptr<WifiDataProviderCommonWithMock> provider_; | |
132 MockWlanApi* wlan_api_; | |
133 MockPollingPolicy* polling_policy_; | |
134 }; | |
135 | |
136 TEST_F(GeolocationWifiDataProviderCommonTest, CreateDestroy) { | |
137 // Test fixture members were SetUp correctly. | |
138 EXPECT_TRUE(main_task_runner_->BelongsToCurrentThread()); | |
139 EXPECT_TRUE(NULL != provider_.get()); | |
140 EXPECT_TRUE(NULL != wlan_api_); | |
141 } | |
142 | |
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 RunLoop(); | |
150 SUCCEED(); | |
151 } | |
152 | |
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 RunLoop(); | |
160 } | |
161 | |
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()); | |
171 | |
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); | |
179 | |
180 provider_->StartDataProvider(); | |
181 RunLoop(); | |
182 RunLoop(); | |
183 } | |
184 | |
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 RunLoop(); | |
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())); | |
201 } | |
202 | |
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); | |
220 | |
221 provider_->StartDataProvider(); | |
222 RunLoop(); | |
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); | |
228 } | |
229 | |
230 TEST_F(GeolocationWifiDataProviderCommonTest, RegisterUnregister) { | |
231 WifiDataProviderManager::SetFactoryForTesting( | |
232 CreateWifiDataProviderCommonWithMock); | |
233 WifiDataProviderManager::Register(&wifi_data_callback_); | |
234 RunLoop(); | |
235 WifiDataProviderManager::Unregister(&wifi_data_callback_); | |
236 WifiDataProviderManager::ResetFactoryForTesting(); | |
237 } | |
238 | |
239 } // namespace device | |
OLD | NEW |