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

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

Issue 6591034: Move core pieces of geolocation from chrome to content.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix Linux build Created 9 years, 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 "chrome/browser/geolocation/wifi_data_provider_common.h"
6
7 #include <vector>
8
9 #include "base/scoped_ptr.h"
10 #include "base/string_util.h"
11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
12 #include "base/utf_string_conversions.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using testing::_;
17 using testing::AtLeast;
18 using testing::DoDefault;
19 using testing::Invoke;
20 using testing::Return;
21
22 namespace {
23
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));
30 }
31
32 MOCK_METHOD1(GetAccessPointData, bool(WifiData::AccessPointDataSet* data));
33
34 int calls_;
35 bool bool_return_;
36 WifiData::AccessPointDataSet data_out_;
37
38 private:
39 bool GetAccessPointDataInternal(WifiData::AccessPointDataSet* data) {
40 ++calls_;
41 *data = data_out_;
42 return bool_return_;
43 }
44 };
45
46 class MockPollingPolicy :public PollingPolicyInterface {
47 public:
48 MockPollingPolicy() {
49 ON_CALL(*this,PollingInterval())
50 .WillByDefault(Return(1));
51 ON_CALL(*this,NoWifiInterval())
52 .WillByDefault(Return(1));
53 }
54
55 MOCK_METHOD0(PollingInterval, int());
56 MOCK_METHOD0(NoWifiInterval, int());
57
58 virtual void UpdatePollingInterval(bool) {}
59 };
60
61 // Stops the specified (nested) message loop when the listener is called back.
62 class MessageLoopQuitListener
63 : public WifiDataProviderCommon::ListenerInterface {
64 public:
65 explicit MessageLoopQuitListener(MessageLoop* message_loop)
66 : message_loop_to_quit_(message_loop) {
67 CHECK(message_loop_to_quit_ != NULL);
68 }
69 // ListenerInterface
70 virtual void DeviceDataUpdateAvailable(
71 DeviceDataProvider<WifiData>* provider) {
72 // Provider should call back on client's thread.
73 EXPECT_EQ(MessageLoop::current(), message_loop_to_quit_);
74 provider_ = provider;
75 message_loop_to_quit_->QuitNow();
76 }
77 MessageLoop* message_loop_to_quit_;
78 DeviceDataProvider<WifiData>* provider_;
79 };
80
81
82 class WifiDataProviderCommonWithMock : public WifiDataProviderCommon {
83 public:
84 WifiDataProviderCommonWithMock()
85 : new_wlan_api_(new MockWlanApi),
86 new_polling_policy_(new MockPollingPolicy) {}
87
88 // WifiDataProviderCommon
89 virtual WlanApiInterface* NewWlanApi() {
90 CHECK(new_wlan_api_ != NULL);
91 return new_wlan_api_.release();
92 }
93 virtual PollingPolicyInterface* NewPollingPolicy() {
94 CHECK(new_polling_policy_ != NULL);
95 return new_polling_policy_.release();
96 }
97
98 scoped_ptr<MockWlanApi> new_wlan_api_;
99 scoped_ptr<MockPollingPolicy> new_polling_policy_;
100
101 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommonWithMock);
102 };
103
104 WifiDataProviderImplBase* CreateWifiDataProviderCommonWithMock() {
105 return new WifiDataProviderCommonWithMock;
106 }
107
108 // Main test fixture
109 class GeolocationWifiDataProviderCommonTest : public testing::Test {
110 public:
111 GeolocationWifiDataProviderCommonTest()
112 : quit_listener_(&main_message_loop_) {
113 }
114
115 virtual void SetUp() {
116 provider_ = new WifiDataProviderCommonWithMock;
117 wlan_api_ = provider_->new_wlan_api_.get();
118 polling_policy_ = provider_->new_polling_policy_.get();
119 provider_->AddListener(&quit_listener_);
120 }
121 virtual void TearDown() {
122 provider_->RemoveListener(&quit_listener_);
123 provider_->StopDataProvider();
124 provider_ = NULL;
125 }
126
127 protected:
128 MessageLoop main_message_loop_;
129 MessageLoopQuitListener quit_listener_;
130 scoped_refptr<WifiDataProviderCommonWithMock> provider_;
131 MockWlanApi* wlan_api_;
132 MockPollingPolicy* polling_policy_;
133 };
134
135 TEST_F(GeolocationWifiDataProviderCommonTest, CreateDestroy) {
136 // Test fixture members were SetUp correctly.
137 EXPECT_EQ(&main_message_loop_, MessageLoop::current());
138 EXPECT_TRUE(NULL != provider_.get());
139 EXPECT_TRUE(NULL != wlan_api_);
140 }
141
142 TEST_F(GeolocationWifiDataProviderCommonTest, StartThread) {
143 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
144 .Times(AtLeast(1));
145 EXPECT_CALL(*polling_policy_, PollingInterval())
146 .Times(AtLeast(1));
147 EXPECT_TRUE(provider_->StartDataProvider());
148 main_message_loop_.Run();
149 SUCCEED();
150 }
151
152 TEST_F(GeolocationWifiDataProviderCommonTest, NoWifi){
153 EXPECT_CALL(*polling_policy_, NoWifiInterval())
154 .Times(AtLeast(1));
155 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
156 .WillRepeatedly(Return(false));
157 provider_->StartDataProvider();
158 main_message_loop_.Run();
159 }
160
161 TEST_F(GeolocationWifiDataProviderCommonTest, IntermittentWifi){
162 EXPECT_CALL(*polling_policy_, PollingInterval())
163 .Times(AtLeast(1));
164 EXPECT_CALL(*polling_policy_, NoWifiInterval())
165 .Times(1);
166 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
167 .WillOnce(Return(true))
168 .WillOnce(Return(false))
169 .WillRepeatedly(DoDefault());
170
171 AccessPointData single_access_point;
172 single_access_point.channel = 2;
173 single_access_point.mac_address = 3;
174 single_access_point.radio_signal_strength = 4;
175 single_access_point.signal_to_noise = 5;
176 single_access_point.ssid = ASCIIToUTF16("foossid");
177 wlan_api_->data_out_.insert(single_access_point);
178
179 provider_->StartDataProvider();
180 main_message_loop_.Run();
181 main_message_loop_.Run();
182 }
183
184 TEST_F(GeolocationWifiDataProviderCommonTest, DoAnEmptyScan) {
185 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
186 .Times(AtLeast(1));
187 EXPECT_CALL(*polling_policy_, PollingInterval())
188 .Times(AtLeast(1));
189 EXPECT_TRUE(provider_->StartDataProvider());
190 main_message_loop_.Run();
191 // Check we had at least one call. The worker thread may have raced ahead
192 // and made multiple calls.
193 EXPECT_GT(wlan_api_->calls_, 0);
194 WifiData data;
195 EXPECT_TRUE(provider_->GetData(&data));
196 EXPECT_EQ(0, static_cast<int>(data.access_point_data.size()));
197 }
198
199 TEST_F(GeolocationWifiDataProviderCommonTest, DoScanWithResults) {
200 EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
201 .Times(AtLeast(1));
202 EXPECT_CALL(*polling_policy_, PollingInterval())
203 .Times(AtLeast(1));
204 AccessPointData single_access_point;
205 single_access_point.channel = 2;
206 single_access_point.mac_address = 3;
207 single_access_point.radio_signal_strength = 4;
208 single_access_point.signal_to_noise = 5;
209 single_access_point.ssid = ASCIIToUTF16("foossid");
210 wlan_api_->data_out_.insert(single_access_point);
211
212 EXPECT_TRUE(provider_->StartDataProvider());
213 main_message_loop_.Run();
214 EXPECT_GT(wlan_api_->calls_, 0);
215 WifiData data;
216 EXPECT_TRUE(provider_->GetData(&data));
217 EXPECT_EQ(1, static_cast<int>(data.access_point_data.size()));
218 EXPECT_EQ(single_access_point.ssid, data.access_point_data.begin()->ssid);
219 }
220
221 TEST_F(GeolocationWifiDataProviderCommonTest,
222 StartThreadViaDeviceDataProvider) {
223 MessageLoopQuitListener quit_listener(&main_message_loop_);
224 WifiDataProvider::SetFactory(CreateWifiDataProviderCommonWithMock);
225 DeviceDataProvider<WifiData>::Register(&quit_listener);
226 main_message_loop_.Run();
227 DeviceDataProvider<WifiData>::Unregister(&quit_listener);
228 DeviceDataProvider<WifiData>::ResetFactory();
229 }
230
231 } // namespace
232
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698