Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / geolocation / wifi_data_provider_common_unittest.cc
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 <vector>
6
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 "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using testing::_;
16 using testing::AtLeast;
17 using testing::DoDefault;
18 using testing::Invoke;
19 using testing::Return;
20
21 namespace content {
22
23 class MockWlanApi : public WifiDataProviderCommon::WlanApiInterface {
24  public:
25   MockWlanApi() : calls_(0), bool_return_(true) {
26     ANNOTATE_BENIGN_RACE(&calls_, "This is a test-only data race on a counter");
27     ON_CALL(*this, GetAccessPointData(_))
28         .WillByDefault(Invoke(this, &MockWlanApi::GetAccessPointDataInternal));
29   }
30
31   MOCK_METHOD1(GetAccessPointData, bool(WifiData::AccessPointDataSet* data));
32
33   int calls_;
34   bool bool_return_;
35   WifiData::AccessPointDataSet data_out_;
36
37  private:
38   bool GetAccessPointDataInternal(WifiData::AccessPointDataSet* data) {
39     ++calls_;
40     *data = data_out_;
41     return bool_return_;
42   }
43 };
44
45 class MockPollingPolicy : public WifiPollingPolicy {
46  public:
47   MockPollingPolicy() {
48     ON_CALL(*this,PollingInterval())
49         .WillByDefault(Return(1));
50     ON_CALL(*this,NoWifiInterval())
51         .WillByDefault(Return(1));
52   }
53
54   MOCK_METHOD0(PollingInterval, int());
55   MOCK_METHOD0(NoWifiInterval, int());
56
57   virtual void UpdatePollingInterval(bool) {}
58 };
59
60 // Stops the specified (nested) message loop when the callback is called.
61 class MessageLoopQuitter {
62  public:
63   explicit MessageLoopQuitter(base::MessageLoop* message_loop)
64       : message_loop_to_quit_(message_loop),
65         callback_(base::Bind(&MessageLoopQuitter::OnWifiDataUpdate,
66                              base::Unretained(this))) {
67     CHECK(message_loop_to_quit_ != NULL);
68   }
69
70   void OnWifiDataUpdate(WifiDataProvider* provider) {
71     // Provider should call back on client's thread.
72     EXPECT_EQ(base::MessageLoop::current(), message_loop_to_quit_);
73     message_loop_to_quit_->QuitNow();
74   }
75   base::MessageLoop* message_loop_to_quit_;
76   WifiDataProvider::WifiDataUpdateCallback callback_;
77 };
78
79 class WifiDataProviderCommonWithMock : public WifiDataProviderCommon {
80  public:
81   WifiDataProviderCommonWithMock()
82       : new_wlan_api_(new MockWlanApi),
83         new_polling_policy_(new MockPollingPolicy) {}
84
85   // WifiDataProviderCommon
86   virtual WlanApiInterface* NewWlanApi() OVERRIDE {
87     CHECK(new_wlan_api_ != NULL);
88     return new_wlan_api_.release();
89   }
90   virtual WifiPollingPolicy* NewPollingPolicy() OVERRIDE {
91     CHECK(new_polling_policy_ != NULL);
92     return new_polling_policy_.release();
93   }
94
95   scoped_ptr<MockWlanApi> new_wlan_api_;
96   scoped_ptr<MockPollingPolicy> new_polling_policy_;
97
98  private:
99   virtual ~WifiDataProviderCommonWithMock() {}
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       : loop_quitter_(&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_->AddCallback(&loop_quitter_.callback_);
120   }
121   virtual void TearDown() {
122     provider_->RemoveCallback(&loop_quitter_.callback_);
123     provider_->StopDataProvider();
124     provider_ = NULL;
125   }
126
127  protected:
128   base::MessageLoop main_message_loop_;
129   MessageLoopQuitter loop_quitter_;
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_, base::MessageLoop::current());
138   EXPECT_TRUE(NULL != provider_.get());
139   EXPECT_TRUE(NULL != wlan_api_);
140 }
141
142 TEST_F(GeolocationWifiDataProviderCommonTest, RunNormal) {
143   EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
144       .Times(AtLeast(1));
145   EXPECT_CALL(*polling_policy_, PollingInterval())
146       .Times(AtLeast(1));
147   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 = base::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   provider_->StartDataProvider();
190   main_message_loop_.Run();
191   EXPECT_EQ(wlan_api_->calls_, 1);
192   WifiData data;
193   EXPECT_TRUE(provider_->GetData(&data));
194   EXPECT_EQ(0, static_cast<int>(data.access_point_data.size()));
195 }
196
197 TEST_F(GeolocationWifiDataProviderCommonTest, DoScanWithResults) {
198   EXPECT_CALL(*wlan_api_, GetAccessPointData(_))
199       .Times(AtLeast(1));
200   EXPECT_CALL(*polling_policy_, PollingInterval())
201       .Times(AtLeast(1));
202   AccessPointData single_access_point;
203   single_access_point.channel = 2;
204   single_access_point.mac_address = 3;
205   single_access_point.radio_signal_strength = 4;
206   single_access_point.signal_to_noise = 5;
207   single_access_point.ssid = base::ASCIIToUTF16("foossid");
208   wlan_api_->data_out_.insert(single_access_point);
209
210   provider_->StartDataProvider();
211   main_message_loop_.Run();
212   EXPECT_EQ(wlan_api_->calls_, 1);
213   WifiData data;
214   EXPECT_TRUE(provider_->GetData(&data));
215   EXPECT_EQ(1, static_cast<int>(data.access_point_data.size()));
216   EXPECT_EQ(single_access_point.ssid, data.access_point_data.begin()->ssid);
217 }
218
219 TEST_F(GeolocationWifiDataProviderCommonTest, RegisterUnregister) {
220   MessageLoopQuitter loop_quitter(&main_message_loop_);
221   WifiDataProvider::SetFactory(CreateWifiDataProviderCommonWithMock);
222   WifiDataProvider::Register(&loop_quitter.callback_);
223   main_message_loop_.Run();
224   WifiDataProvider::Unregister(&loop_quitter.callback_);
225   WifiDataProvider::ResetFactory();
226 }
227
228 }  // namespace content