Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: content/browser/geolocation/wifi_data_provider_common_unittest.cc

Issue 2192683002: Reland 2:Geolocation: move from content/browser to device/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Ignore size_t_to_int truncation warning Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "content/browser/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 "content/browser/geolocation/wifi_data_provider_manager.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 using testing::_;
21 using testing::AtLeast;
22 using testing::DoDefault;
23 using testing::Invoke;
24 using testing::Return;
25
26 namespace content {
27
28 class MockWlanApi : public WifiDataProviderCommon::WlanApiInterface {
29 public:
30 MockWlanApi() : calls_(0), bool_return_(true) {
31 ANNOTATE_BENIGN_RACE(&calls_, "This is a test-only data race on a counter");
32 ON_CALL(*this, GetAccessPointData(_))
33 .WillByDefault(Invoke(this, &MockWlanApi::GetAccessPointDataInternal));
34 }
35
36 MOCK_METHOD1(GetAccessPointData, bool(WifiData::AccessPointDataSet* data));
37
38 int calls_;
39 bool bool_return_;
40 WifiData::AccessPointDataSet data_out_;
41
42 private:
43 bool GetAccessPointDataInternal(WifiData::AccessPointDataSet* data) {
44 ++calls_;
45 *data = data_out_;
46 return bool_return_;
47 }
48 };
49
50 class MockPollingPolicy : public WifiPollingPolicy {
51 public:
52 MockPollingPolicy() {
53 ON_CALL(*this,PollingInterval())
54 .WillByDefault(Return(1));
55 ON_CALL(*this,NoWifiInterval())
56 .WillByDefault(Return(1));
57 }
58
59 MOCK_METHOD0(PollingInterval, int());
60 MOCK_METHOD0(NoWifiInterval, int());
61
62 virtual void UpdatePollingInterval(bool) {}
63 };
64
65 class WifiDataProviderCommonWithMock : public WifiDataProviderCommon {
66 public:
67 WifiDataProviderCommonWithMock()
68 : new_wlan_api_(new MockWlanApi),
69 new_polling_policy_(new MockPollingPolicy) {}
70
71 // WifiDataProviderCommon
72 WlanApiInterface* NewWlanApi() override {
73 CHECK(new_wlan_api_ != NULL);
74 return new_wlan_api_.release();
75 }
76 WifiPollingPolicy* NewPollingPolicy() override {
77 CHECK(new_polling_policy_ != NULL);
78 return new_polling_policy_.release();
79 }
80
81 std::unique_ptr<MockWlanApi> new_wlan_api_;
82 std::unique_ptr<MockPollingPolicy> new_polling_policy_;
83
84 private:
85 ~WifiDataProviderCommonWithMock() override {}
86
87 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommonWithMock);
88 };
89
90 WifiDataProvider* CreateWifiDataProviderCommonWithMock() {
91 return new WifiDataProviderCommonWithMock;
92 }
93
94 // Main test fixture
95 class GeolocationWifiDataProviderCommonTest : public testing::Test {
96 public:
97 GeolocationWifiDataProviderCommonTest()
98 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()),
99 wifi_data_callback_(
100 base::Bind(&GeolocationWifiDataProviderCommonTest::OnWifiDataUpdate,
101 base::Unretained(this))) {}
102
103 void SetUp() override {
104 provider_ = new WifiDataProviderCommonWithMock;
105 wlan_api_ = provider_->new_wlan_api_.get();
106 polling_policy_ = provider_->new_polling_policy_.get();
107 provider_->AddCallback(&wifi_data_callback_);
108 }
109
110 void TearDown() override {
111 provider_->RemoveCallback(&wifi_data_callback_);
112 provider_->StopDataProvider();
113 provider_ = NULL;
114 }
115
116 void OnWifiDataUpdate() {
117 // Callbacks must run on the originating thread.
118 EXPECT_TRUE(main_task_runner_->BelongsToCurrentThread());
119 run_loop_->Quit();
120 }
121
122 void RunLoop() {
123 run_loop_.reset(new base::RunLoop());
124 run_loop_->Run();
125 }
126
127 protected:
128 TestBrowserThreadBundle thread_bundle_;
129 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
130 std::unique_ptr<base::RunLoop> run_loop_;
131 WifiDataProviderManager::WifiDataUpdateCallback wifi_data_callback_;
132 scoped_refptr<WifiDataProviderCommonWithMock> provider_;
133 MockWlanApi* wlan_api_;
134 MockPollingPolicy* polling_policy_;
135 };
136
137 TEST_F(GeolocationWifiDataProviderCommonTest, CreateDestroy) {
138 // Test fixture members were SetUp correctly.
139 EXPECT_TRUE(main_task_runner_->BelongsToCurrentThread());
140 EXPECT_TRUE(NULL != provider_.get());
141 EXPECT_TRUE(NULL != wlan_api_);
142 }
143
144 TEST_F(GeolocationWifiDataProviderCommonTest, RunNormal) {
145 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
146 .Times(AtLeast(1));
147 EXPECT_CALL(*polling_policy_, PollingInterval())
148 .Times(AtLeast(1));
149 provider_->StartDataProvider();
150 RunLoop();
151 SUCCEED();
152 }
153
154 TEST_F(GeolocationWifiDataProviderCommonTest, NoWifi) {
155 EXPECT_CALL(*polling_policy_, NoWifiInterval())
156 .Times(AtLeast(1));
157 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
158 .WillRepeatedly(Return(false));
159 provider_->StartDataProvider();
160 RunLoop();
161 }
162
163 TEST_F(GeolocationWifiDataProviderCommonTest, IntermittentWifi) {
164 EXPECT_CALL(*polling_policy_, PollingInterval())
165 .Times(AtLeast(1));
166 EXPECT_CALL(*polling_policy_, NoWifiInterval())
167 .Times(1);
168 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
169 .WillOnce(Return(true))
170 .WillOnce(Return(false))
171 .WillRepeatedly(DoDefault());
172
173 AccessPointData single_access_point;
174 single_access_point.channel = 2;
175 single_access_point.mac_address = 3;
176 single_access_point.radio_signal_strength = 4;
177 single_access_point.signal_to_noise = 5;
178 single_access_point.ssid = base::ASCIIToUTF16("foossid");
179 wlan_api_->data_out_.insert(single_access_point);
180
181 provider_->StartDataProvider();
182 RunLoop();
183 RunLoop();
184 }
185
186 #if defined(OS_MACOSX)
187 #define MAYBE_DoAnEmptyScan DISABLED_DoAnEmptyScan
188 #else
189 #define MAYBE_DoAnEmptyScan DoAnEmptyScan
190 #endif
191 TEST_F(GeolocationWifiDataProviderCommonTest, MAYBE_DoAnEmptyScan) {
192 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
193 .Times(AtLeast(1));
194 EXPECT_CALL(*polling_policy_, PollingInterval())
195 .Times(AtLeast(1));
196 provider_->StartDataProvider();
197 RunLoop();
198 EXPECT_EQ(wlan_api_->calls_, 1);
199 WifiData data;
200 EXPECT_TRUE(provider_->GetData(&data));
201 EXPECT_EQ(0, static_cast<int>(data.access_point_data.size()));
202 }
203
204 #if defined(OS_MACOSX)
205 #define MAYBE_DoScanWithResults DISABLED_DoScanWithResults
206 #else
207 #define MAYBE_DoScanWithResults DoScanWithResults
208 #endif
209 TEST_F(GeolocationWifiDataProviderCommonTest, MAYBE_DoScanWithResults) {
210 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
211 .Times(AtLeast(1));
212 EXPECT_CALL(*polling_policy_, PollingInterval())
213 .Times(AtLeast(1));
214 AccessPointData single_access_point;
215 single_access_point.channel = 2;
216 single_access_point.mac_address = 3;
217 single_access_point.radio_signal_strength = 4;
218 single_access_point.signal_to_noise = 5;
219 single_access_point.ssid = base::ASCIIToUTF16("foossid");
220 wlan_api_->data_out_.insert(single_access_point);
221
222 provider_->StartDataProvider();
223 RunLoop();
224 EXPECT_EQ(wlan_api_->calls_, 1);
225 WifiData data;
226 EXPECT_TRUE(provider_->GetData(&data));
227 EXPECT_EQ(1, static_cast<int>(data.access_point_data.size()));
228 EXPECT_EQ(single_access_point.ssid, data.access_point_data.begin()->ssid);
229 }
230
231 TEST_F(GeolocationWifiDataProviderCommonTest, RegisterUnregister) {
232 WifiDataProviderManager::SetFactoryForTesting(
233 CreateWifiDataProviderCommonWithMock);
234 WifiDataProviderManager::Register(&wifi_data_callback_);
235 RunLoop();
236 WifiDataProviderManager::Unregister(&wifi_data_callback_);
237 WifiDataProviderManager::ResetFactoryForTesting();
238 }
239
240 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/geolocation/wifi_data_provider_common.cc ('k') | content/browser/geolocation/wifi_data_provider_common_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698