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

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

Issue 604019: Refactor the state-machine & threading out of the windows wifi provider into ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 10 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
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/geolocation/wifi_data_provider_win.h" 5 #include "chrome/browser/geolocation/wifi_data_provider_common.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/scoped_ptr.h" 9 #include "base/scoped_ptr.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "chrome/browser/geolocation/wifi_data_provider_common.h"
12 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
13 12
14 namespace { 13 class MockWlanApi : public WifiDataProviderCommon::WlanApiInterface {
15 class MockWlanApi : public Win32WifiDataProvider::WlanApiInterface {
16 public: 14 public:
17 MockWlanApi() : calls_(0), bool_return_(true) { 15 MockWlanApi() : calls_(0), bool_return_(true) {
18 } 16 }
19 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) { 17 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data) {
20 ++calls_; 18 ++calls_;
21 *data = data_out_; 19 *data = data_out_;
22 return bool_return_; 20 return bool_return_;
23 } 21 }
24 int calls_; 22 int calls_;
25 bool bool_return_; 23 bool bool_return_;
26 WifiData::AccessPointDataSet data_out_; 24 WifiData::AccessPointDataSet data_out_;
27 }; 25 };
28 26
29 class MockPollingPolicy : public PollingPolicyInterface {
30 public:
31 virtual void UpdatePollingInterval(bool scan_results_differ) {
32 results_differed_.push_back(scan_results_differ);
33 }
34 virtual int PollingInterval() { return 1; }
35 std::vector<bool> results_differed_;
36 };
37
38 // Stops the specified (nested) message loop when the listener is called back. 27 // Stops the specified (nested) message loop when the listener is called back.
39 class MessageLoopQuitListener 28 class MessageLoopQuitListener
40 : public Win32WifiDataProvider::ListenerInterface { 29 : public WifiDataProviderCommon::ListenerInterface {
41 public: 30 public:
42 explicit MessageLoopQuitListener(MessageLoop* message_loop) 31 explicit MessageLoopQuitListener(MessageLoop* message_loop)
43 : message_loop_to_quit_(message_loop) { 32 : message_loop_to_quit_(message_loop) {
44 DCHECK(message_loop_to_quit_ != NULL); 33 CHECK(message_loop_to_quit_ != NULL);
45 } 34 }
46 // ListenerInterface 35 // ListenerInterface
47 virtual void DeviceDataUpdateAvailable( 36 virtual void DeviceDataUpdateAvailable(
48 DeviceDataProvider<WifiData>* provider) { 37 DeviceDataProvider<WifiData>* provider) {
49 // We expect the callback to come from the provider's internal worker 38 // Provider should call back on client's thread.
50 // thread. This is not a strict requirement, just a lot of the complexity 39 EXPECT_EQ(MessageLoop::current(), message_loop_to_quit_);
51 // here is predicated on the need to cope with this scenario!
52 EXPECT_NE(MessageLoop::current(), message_loop_to_quit_);
53 provider_ = provider; 40 provider_ = provider;
54 // Can't call Quit() directly on another thread's message loop. 41 message_loop_to_quit_->Quit();
55 message_loop_to_quit_->PostTask(FROM_HERE, new MessageLoop::QuitTask);
56 } 42 }
57 MessageLoop* message_loop_to_quit_; 43 MessageLoop* message_loop_to_quit_;
58 DeviceDataProvider<WifiData>* provider_; 44 DeviceDataProvider<WifiData>* provider_;
59 }; 45 };
60 46
47 class WifiDataProviderCommonWithMock : public WifiDataProviderCommon {
48 public:
49 WifiDataProviderCommonWithMock()
50 : new_wlan_api_(new MockWlanApi) {}
51
52 // WifiDataProviderCommon
53 virtual WlanApiInterface* NewWlanApi() {
54 CHECK(new_wlan_api_ != NULL);
55 return new_wlan_api_.release();
56 }
57 virtual PollingPolicyInterface* NewPolicyPolicy() {
58 return new GenericPollingPolicy<1, 2, 3>;
59 }
60
61 scoped_ptr<MockWlanApi> new_wlan_api_;
62
63 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderCommonWithMock);
64 };
65
66
67 WifiDataProviderImplBase* CreateWifiDataProviderCommonWithMock() {
68 return new WifiDataProviderCommonWithMock;
69 }
70
61 // Main test fixture 71 // Main test fixture
62 class Win32WifiDataProviderTest : public testing::Test { 72 class GeolocationWifiDataProviderCommonTest : public testing::Test {
63 public: 73 public:
64 static Win32WifiDataProvider* CreateWin32WifiDataProvider(
65 MockWlanApi** wlan_api_out) {
66 Win32WifiDataProvider* provider = new Win32WifiDataProvider;
67 *wlan_api_out = new MockWlanApi;
68 provider->inject_mock_wlan_api(*wlan_api_out); // Takes ownership.
69 provider->inject_mock_polling_policy(new MockPollingPolicy); // ditto
70 return provider;
71 }
72 virtual void SetUp() { 74 virtual void SetUp() {
73 provider_.reset(CreateWin32WifiDataProvider(&wlan_api_)); 75 provider_ = new WifiDataProviderCommonWithMock;
76 wlan_api_ = provider_->new_wlan_api_.get();
74 } 77 }
75 virtual void TearDown() { 78 virtual void TearDown() {
76 provider_.reset(); 79 provider_->StopDataProvider();
80 provider_ = NULL;
77 } 81 }
78 82
79 protected: 83 protected:
80 MessageLoop main_message_loop_; 84 MessageLoop main_message_loop_;
81 scoped_ptr<Win32WifiDataProvider> provider_; 85 scoped_refptr<WifiDataProviderCommonWithMock> provider_;
82 MockWlanApi* wlan_api_; 86 MockWlanApi* wlan_api_;
83 }; 87 };
84 88
85 WifiDataProviderImplBase* CreateWin32WifiDataProviderStatic() { 89 TEST_F(GeolocationWifiDataProviderCommonTest, CreateDestroy) {
86 MockWlanApi* wlan_api;
87 return Win32WifiDataProviderTest::CreateWin32WifiDataProvider(&wlan_api);
88 }
89 } // namespace
90
91 TEST_F(Win32WifiDataProviderTest, CreateDestroy) {
92 // Test fixture members were SetUp correctly. 90 // Test fixture members were SetUp correctly.
93 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); 91 EXPECT_EQ(&main_message_loop_, MessageLoop::current());
94 EXPECT_TRUE(NULL != provider_.get()); 92 EXPECT_TRUE(NULL != provider_.get());
95 EXPECT_TRUE(NULL != wlan_api_); 93 EXPECT_TRUE(NULL != wlan_api_);
96 } 94 }
97 95
98 TEST_F(Win32WifiDataProviderTest, StartThread) { 96 TEST_F(GeolocationWifiDataProviderCommonTest, StartThread) {
99 EXPECT_TRUE(provider_->StartDataProvider()); 97 EXPECT_TRUE(provider_->StartDataProvider());
100 provider_.reset(); // Stop()s the thread. 98 provider_->StopDataProvider();
101 SUCCEED(); 99 SUCCEED();
102 } 100 }
103 101
104 TEST_F(Win32WifiDataProviderTest, DoAnEmptyScan) { 102 TEST_F(GeolocationWifiDataProviderCommonTest, DoAnEmptyScan) {
105 MessageLoopQuitListener quit_listener(&main_message_loop_); 103 MessageLoopQuitListener quit_listener(&main_message_loop_);
106 provider_->AddListener(&quit_listener); 104 provider_->AddListener(&quit_listener);
107 EXPECT_TRUE(provider_->StartDataProvider()); 105 EXPECT_TRUE(provider_->StartDataProvider());
108 main_message_loop_.Run(); 106 main_message_loop_.Run();
109 // Check we had at least one call. The worker thread may have raced ahead 107 // Check we had at least one call. The worker thread may have raced ahead
110 // and made multiple calls. 108 // and made multiple calls.
111 EXPECT_GT(wlan_api_->calls_, 0); 109 EXPECT_GT(wlan_api_->calls_, 0);
112 WifiData data; 110 WifiData data;
113 EXPECT_TRUE(provider_->GetData(&data)); 111 EXPECT_TRUE(provider_->GetData(&data));
114 EXPECT_EQ(0, data.access_point_data.size()); 112 EXPECT_EQ(0, static_cast<int>(data.access_point_data.size()));
115 provider_->RemoveListener(&quit_listener); 113 provider_->RemoveListener(&quit_listener);
116 } 114 }
117 115
118 TEST_F(Win32WifiDataProviderTest, DoScanWithResults) { 116 TEST_F(GeolocationWifiDataProviderCommonTest, DoScanWithResults) {
119 MessageLoopQuitListener quit_listener(&main_message_loop_); 117 MessageLoopQuitListener quit_listener(&main_message_loop_);
120 provider_->AddListener(&quit_listener); 118 provider_->AddListener(&quit_listener);
121 AccessPointData single_access_point; 119 AccessPointData single_access_point;
122 single_access_point.age = 1; 120 single_access_point.age = 1;
123 single_access_point.channel = 2; 121 single_access_point.channel = 2;
124 single_access_point.mac_address = 3; 122 single_access_point.mac_address = 3;
125 single_access_point.radio_signal_strength = 4; 123 single_access_point.radio_signal_strength = 4;
126 single_access_point.signal_to_noise = 5; 124 single_access_point.signal_to_noise = 5;
127 single_access_point.ssid = L"foossid"; 125 single_access_point.ssid = ASCIIToUTF16("foossid");
128 wlan_api_->data_out_.insert(single_access_point); 126 wlan_api_->data_out_.insert(single_access_point);
129 127
130 EXPECT_TRUE(provider_->StartDataProvider()); 128 EXPECT_TRUE(provider_->StartDataProvider());
131 main_message_loop_.Run(); 129 main_message_loop_.Run();
132 EXPECT_GT(wlan_api_->calls_, 0); 130 EXPECT_GT(wlan_api_->calls_, 0);
133 WifiData data; 131 WifiData data;
134 EXPECT_TRUE(provider_->GetData(&data)); 132 EXPECT_TRUE(provider_->GetData(&data));
135 EXPECT_EQ(1, data.access_point_data.size()); 133 EXPECT_EQ(1, static_cast<int>(data.access_point_data.size()));
136 EXPECT_EQ(single_access_point.age, data.access_point_data.begin()->age); 134 EXPECT_EQ(single_access_point.age, data.access_point_data.begin()->age);
137 EXPECT_EQ(single_access_point.ssid, data.access_point_data.begin()->ssid); 135 EXPECT_EQ(single_access_point.ssid, data.access_point_data.begin()->ssid);
138 provider_->RemoveListener(&quit_listener); 136 provider_->RemoveListener(&quit_listener);
139 } 137 }
140 138
141 TEST_F(Win32WifiDataProviderTest, StartThreadViaDeviceDataProvider) { 139 TEST_F(GeolocationWifiDataProviderCommonTest,
140 StartThreadViaDeviceDataProvider) {
142 MessageLoopQuitListener quit_listener(&main_message_loop_); 141 MessageLoopQuitListener quit_listener(&main_message_loop_);
143 WifiDataProvider::SetFactory(CreateWin32WifiDataProviderStatic); 142 WifiDataProvider::SetFactory(CreateWifiDataProviderCommonWithMock);
144 DeviceDataProvider<WifiData>::Register(&quit_listener); 143 DeviceDataProvider<WifiData>::Register(&quit_listener);
145 main_message_loop_.Run(); 144 main_message_loop_.Run();
146 DeviceDataProvider<WifiData>::Unregister(&quit_listener); 145 DeviceDataProvider<WifiData>::Unregister(&quit_listener);
147 DeviceDataProvider<WifiData>::ResetFactory(); 146 DeviceDataProvider<WifiData>::ResetFactory();
148 } 147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698