Use new WebView zoom API.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_win_unittest.cc
blob4ea7a1e6f3d0360fcdab197165cadfbdf9e59b8e
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.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/test/test_simple_task_runner.h"
10 #include "device/bluetooth/bluetooth_adapter.h"
11 #include "device/bluetooth/bluetooth_adapter_win.h"
12 #include "device/bluetooth/bluetooth_device.h"
13 #include "device/bluetooth/bluetooth_task_manager_win.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace {
18 const char kAdapterAddress[] = "Bluetooth Adapter Address";
19 const char kAdapterName[] = "Bluetooth Adapter Name";
22 void MakeDeviceState(const std::string& name,
23 const std::string& address,
24 device::BluetoothTaskManagerWin::DeviceState* state) {
25 state->name = name;
26 state->address = address;
27 state->bluetooth_class = 0;
28 state->authenticated = false;
29 state->connected = false;
32 class AdapterObserver : public device::BluetoothAdapter::Observer {
33 public:
34 AdapterObserver()
35 : num_present_changed_(0),
36 num_powered_changed_(0),
37 num_discovering_changed_(0),
38 num_device_added_(0) {
41 virtual void AdapterPresentChanged(
42 device::BluetoothAdapter* adapter, bool present) OVERRIDE {
43 num_present_changed_++;
46 virtual void AdapterPoweredChanged(
47 device::BluetoothAdapter* adapter, bool powered) OVERRIDE {
48 num_powered_changed_++;
51 virtual void AdapterDiscoveringChanged(
52 device::BluetoothAdapter* adapter, bool discovering) OVERRIDE {
53 num_discovering_changed_++;
56 virtual void DeviceAdded(
57 device::BluetoothAdapter* adapter,
58 device::BluetoothDevice* device) OVERRIDE {
59 num_device_added_++;
62 int num_present_changed() const {
63 return num_present_changed_;
66 int num_powered_changed() const {
67 return num_powered_changed_;
70 int num_discovering_changed() const {
71 return num_discovering_changed_;
74 int num_device_added() const {
75 return num_device_added_;
78 private:
79 int num_present_changed_;
80 int num_powered_changed_;
81 int num_discovering_changed_;
82 int num_device_added_;
85 } // namespace
87 namespace device {
89 class BluetoothAdapterWinTest : public testing::Test {
90 public:
91 BluetoothAdapterWinTest()
92 : ui_task_runner_(new base::TestSimpleTaskRunner()),
93 bluetooth_task_runner_(new base::TestSimpleTaskRunner()),
94 adapter_(new BluetoothAdapterWin(
95 base::Bind(&BluetoothAdapterWinTest::RunInitCallback,
96 base::Unretained(this)))),
97 adapter_win_(static_cast<BluetoothAdapterWin*>(adapter_.get())),
98 init_callback_called_(false) {
99 adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_);
102 virtual void SetUp() OVERRIDE {
103 adapter_win_->AddObserver(&adapter_observer_);
104 num_start_discovery_callbacks_ = 0;
105 num_start_discovery_error_callbacks_ = 0;
106 num_stop_discovery_callbacks_ = 0;
107 num_stop_discovery_error_callbacks_ = 0;
110 virtual void TearDown() OVERRIDE {
111 adapter_win_->RemoveObserver(&adapter_observer_);
114 void RunInitCallback() {
115 init_callback_called_ = true;
118 void IncrementNumStartDiscoveryCallbacks() {
119 num_start_discovery_callbacks_++;
122 void IncrementNumStartDiscoveryErrorCallbacks() {
123 num_start_discovery_error_callbacks_++;
126 void IncrementNumStopDiscoveryCallbacks() {
127 num_stop_discovery_callbacks_++;
130 void IncrementNumStopDiscoveryErrorCallbacks() {
131 num_stop_discovery_error_callbacks_++;
134 protected:
135 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_;
136 scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_;
137 scoped_refptr<BluetoothAdapter> adapter_;
138 BluetoothAdapterWin* adapter_win_;
139 AdapterObserver adapter_observer_;
140 bool init_callback_called_;
141 int num_start_discovery_callbacks_;
142 int num_start_discovery_error_callbacks_;
143 int num_stop_discovery_callbacks_;
144 int num_stop_discovery_error_callbacks_;
147 TEST_F(BluetoothAdapterWinTest, AdapterNotPresent) {
148 BluetoothTaskManagerWin::AdapterState state;
149 adapter_win_->AdapterStateChanged(state);
150 EXPECT_FALSE(adapter_win_->IsPresent());
153 TEST_F(BluetoothAdapterWinTest, AdapterPresent) {
154 BluetoothTaskManagerWin::AdapterState state;
155 state.address = kAdapterAddress;
156 state.name = kAdapterName;
157 adapter_win_->AdapterStateChanged(state);
158 EXPECT_TRUE(adapter_win_->IsPresent());
161 TEST_F(BluetoothAdapterWinTest, AdapterPresentChanged) {
162 BluetoothTaskManagerWin::AdapterState state;
163 state.address = kAdapterAddress;
164 state.name = kAdapterName;
165 adapter_win_->AdapterStateChanged(state);
166 EXPECT_EQ(1, adapter_observer_.num_present_changed());
167 adapter_win_->AdapterStateChanged(state);
168 EXPECT_EQ(1, adapter_observer_.num_present_changed());
169 BluetoothTaskManagerWin::AdapterState empty_state;
170 adapter_win_->AdapterStateChanged(empty_state);
171 EXPECT_EQ(2, adapter_observer_.num_present_changed());
174 TEST_F(BluetoothAdapterWinTest, AdapterPoweredChanged) {
175 BluetoothTaskManagerWin::AdapterState state;
176 state.powered = true;
177 adapter_win_->AdapterStateChanged(state);
178 EXPECT_EQ(1, adapter_observer_.num_powered_changed());
179 adapter_win_->AdapterStateChanged(state);
180 EXPECT_EQ(1, adapter_observer_.num_powered_changed());
181 state.powered = false;
182 adapter_win_->AdapterStateChanged(state);
183 EXPECT_EQ(2, adapter_observer_.num_powered_changed());
186 TEST_F(BluetoothAdapterWinTest, AdapterInitialized) {
187 EXPECT_FALSE(adapter_win_->IsInitialized());
188 EXPECT_FALSE(init_callback_called_);
189 BluetoothTaskManagerWin::AdapterState state;
190 adapter_win_->AdapterStateChanged(state);
191 EXPECT_TRUE(adapter_win_->IsInitialized());
192 EXPECT_TRUE(init_callback_called_);
195 TEST_F(BluetoothAdapterWinTest, SingleStartDiscovery) {
196 bluetooth_task_runner_->ClearPendingTasks();
197 adapter_win_->StartDiscovering(
198 base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks,
199 base::Unretained(this)),
200 BluetoothAdapter::ErrorCallback());
201 EXPECT_TRUE(ui_task_runner_->GetPendingTasks().empty());
202 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size());
203 EXPECT_FALSE(adapter_->IsDiscovering());
204 EXPECT_EQ(0, num_start_discovery_callbacks_);
205 adapter_win_->DiscoveryStarted(true);
206 ui_task_runner_->RunPendingTasks();
207 EXPECT_TRUE(adapter_->IsDiscovering());
208 EXPECT_EQ(1, num_start_discovery_callbacks_);
209 EXPECT_EQ(1, adapter_observer_.num_discovering_changed());
212 TEST_F(BluetoothAdapterWinTest, SingleStartDiscoveryFailure) {
213 adapter_win_->StartDiscovering(
214 base::Closure(),
215 base::Bind(
216 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks,
217 base::Unretained(this)));
218 adapter_win_->DiscoveryStarted(false);
219 ui_task_runner_->RunPendingTasks();
220 EXPECT_FALSE(adapter_->IsDiscovering());
221 EXPECT_EQ(1, num_start_discovery_error_callbacks_);
222 EXPECT_EQ(0, adapter_observer_.num_discovering_changed());
225 TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveries) {
226 bluetooth_task_runner_->ClearPendingTasks();
227 int num_discoveries = 5;
228 for (int i = 0; i < num_discoveries; i++) {
229 adapter_win_->StartDiscovering(
230 base::Bind(
231 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks,
232 base::Unretained(this)),
233 BluetoothAdapter::ErrorCallback());
234 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size());
236 EXPECT_TRUE(ui_task_runner_->GetPendingTasks().empty());
237 EXPECT_FALSE(adapter_->IsDiscovering());
238 EXPECT_EQ(0, num_start_discovery_callbacks_);
239 adapter_win_->DiscoveryStarted(true);
240 ui_task_runner_->RunPendingTasks();
241 EXPECT_TRUE(adapter_->IsDiscovering());
242 EXPECT_EQ(num_discoveries, num_start_discovery_callbacks_);
243 EXPECT_EQ(1, adapter_observer_.num_discovering_changed());
246 TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesFailure) {
247 int num_discoveries = 5;
248 for (int i = 0; i < num_discoveries; i++) {
249 adapter_win_->StartDiscovering(
250 base::Closure(),
251 base::Bind(
252 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks,
253 base::Unretained(this)));
255 adapter_win_->DiscoveryStarted(false);
256 ui_task_runner_->RunPendingTasks();
257 EXPECT_FALSE(adapter_->IsDiscovering());
258 EXPECT_EQ(num_discoveries, num_start_discovery_error_callbacks_);
259 EXPECT_EQ(0, adapter_observer_.num_discovering_changed());
262 TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesAfterDiscovering) {
263 adapter_win_->StartDiscovering(
264 base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks,
265 base::Unretained(this)),
266 BluetoothAdapter::ErrorCallback());
267 adapter_win_->DiscoveryStarted(true);
268 ui_task_runner_->RunPendingTasks();
269 EXPECT_TRUE(adapter_->IsDiscovering());
270 EXPECT_EQ(1, num_start_discovery_callbacks_);
272 bluetooth_task_runner_->ClearPendingTasks();
273 for (int i = 0; i < 5; i++) {
274 int num_start_discovery_callbacks = num_start_discovery_callbacks_;
275 adapter_win_->StartDiscovering(
276 base::Bind(
277 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks,
278 base::Unretained(this)),
279 BluetoothAdapter::ErrorCallback());
280 EXPECT_TRUE(adapter_->IsDiscovering());
281 EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty());
282 EXPECT_TRUE(ui_task_runner_->GetPendingTasks().empty());
283 EXPECT_EQ(num_start_discovery_callbacks + 1,
284 num_start_discovery_callbacks_);
286 EXPECT_EQ(1, adapter_observer_.num_discovering_changed());
289 TEST_F(BluetoothAdapterWinTest, StartDiscoveryAfterDiscoveringFailure) {
290 adapter_win_->StartDiscovering(
291 base::Closure(),
292 base::Bind(
293 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks,
294 base::Unretained(this)));
295 adapter_win_->DiscoveryStarted(false);
296 ui_task_runner_->RunPendingTasks();
297 EXPECT_FALSE(adapter_->IsDiscovering());
298 EXPECT_EQ(1, num_start_discovery_error_callbacks_);
300 adapter_win_->StartDiscovering(
301 base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks,
302 base::Unretained(this)),
303 BluetoothAdapter::ErrorCallback());
304 adapter_win_->DiscoveryStarted(true);
305 ui_task_runner_->RunPendingTasks();
306 EXPECT_TRUE(adapter_->IsDiscovering());
307 EXPECT_EQ(1, num_start_discovery_callbacks_);
310 TEST_F(BluetoothAdapterWinTest, SingleStopDiscovery) {
311 adapter_win_->StartDiscovering(
312 base::Closure(), BluetoothAdapter::ErrorCallback());
313 adapter_win_->DiscoveryStarted(true);
314 ui_task_runner_->ClearPendingTasks();
315 adapter_win_->StopDiscovering(
316 base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks,
317 base::Unretained(this)),
318 BluetoothAdapter::ErrorCallback());
319 EXPECT_TRUE(adapter_->IsDiscovering());
320 EXPECT_EQ(0, num_stop_discovery_callbacks_);
321 bluetooth_task_runner_->ClearPendingTasks();
322 adapter_win_->DiscoveryStopped();
323 ui_task_runner_->RunPendingTasks();
324 EXPECT_FALSE(adapter_->IsDiscovering());
325 EXPECT_EQ(1, num_stop_discovery_callbacks_);
326 EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty());
327 EXPECT_EQ(2, adapter_observer_.num_discovering_changed());
330 TEST_F(BluetoothAdapterWinTest, MultipleStopDiscoveries) {
331 int num_discoveries = 5;
332 for (int i = 0; i < num_discoveries; i++) {
333 adapter_win_->StartDiscovering(
334 base::Closure(), BluetoothAdapter::ErrorCallback());
336 adapter_win_->DiscoveryStarted(true);
337 ui_task_runner_->ClearPendingTasks();
338 bluetooth_task_runner_->ClearPendingTasks();
339 for (int i = 0; i < num_discoveries - 1; i++) {
340 adapter_win_->StopDiscovering(
341 base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks,
342 base::Unretained(this)),
343 BluetoothAdapter::ErrorCallback());
344 EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty());
345 ui_task_runner_->RunPendingTasks();
346 EXPECT_EQ(i + 1, num_stop_discovery_callbacks_);
348 adapter_win_->StopDiscovering(
349 base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks,
350 base::Unretained(this)),
351 BluetoothAdapter::ErrorCallback());
352 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size());
353 EXPECT_TRUE(adapter_->IsDiscovering());
354 adapter_win_->DiscoveryStopped();
355 ui_task_runner_->RunPendingTasks();
356 EXPECT_FALSE(adapter_->IsDiscovering());
357 EXPECT_EQ(num_discoveries, num_stop_discovery_callbacks_);
358 EXPECT_EQ(2, adapter_observer_.num_discovering_changed());
361 TEST_F(BluetoothAdapterWinTest,
362 StartDiscoveryAndStartDiscoveryAndStopDiscoveries) {
363 adapter_win_->StartDiscovering(
364 base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks,
365 base::Unretained(this)),
366 BluetoothAdapter::ErrorCallback());
367 adapter_win_->DiscoveryStarted(true);
368 adapter_win_->StartDiscovering(
369 base::Bind(&BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks,
370 base::Unretained(this)),
371 BluetoothAdapter::ErrorCallback());
372 ui_task_runner_->ClearPendingTasks();
373 bluetooth_task_runner_->ClearPendingTasks();
374 adapter_win_->StopDiscovering(
375 base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks,
376 base::Unretained(this)),
377 BluetoothAdapter::ErrorCallback());
378 EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty());
379 adapter_win_->StopDiscovering(
380 base::Bind(&BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks,
381 base::Unretained(this)),
382 BluetoothAdapter::ErrorCallback());
383 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size());
386 TEST_F(BluetoothAdapterWinTest,
387 StartDiscoveryAndStopDiscoveryAndStartDiscovery) {
388 adapter_win_->StartDiscovering(
389 base::Closure(), BluetoothAdapter::ErrorCallback());
390 adapter_win_->DiscoveryStarted(true);
391 EXPECT_TRUE(adapter_->IsDiscovering());
392 adapter_win_->StopDiscovering(
393 base::Closure(), BluetoothAdapter::ErrorCallback());
394 adapter_win_->DiscoveryStopped();
395 EXPECT_FALSE(adapter_->IsDiscovering());
396 adapter_win_->StartDiscovering(
397 base::Closure(), BluetoothAdapter::ErrorCallback());
398 adapter_win_->DiscoveryStarted(true);
399 EXPECT_TRUE(adapter_->IsDiscovering());
402 TEST_F(BluetoothAdapterWinTest, StartDiscoveryBeforeDiscoveryStopped) {
403 adapter_win_->StartDiscovering(
404 base::Closure(), BluetoothAdapter::ErrorCallback());
405 adapter_win_->DiscoveryStarted(true);
406 adapter_win_->StopDiscovering(
407 base::Closure(), BluetoothAdapter::ErrorCallback());
408 adapter_win_->StartDiscovering(
409 base::Closure(), BluetoothAdapter::ErrorCallback());
410 bluetooth_task_runner_->ClearPendingTasks();
411 adapter_win_->DiscoveryStopped();
412 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size());
415 TEST_F(BluetoothAdapterWinTest, StopDiscoveryWithoutStartDiscovery) {
416 adapter_win_->StopDiscovering(
417 base::Closure(),
418 base::Bind(
419 &BluetoothAdapterWinTest::IncrementNumStopDiscoveryErrorCallbacks,
420 base::Unretained(this)));
421 EXPECT_EQ(1, num_stop_discovery_error_callbacks_);
424 TEST_F(BluetoothAdapterWinTest, StopDiscoveryBeforeDiscoveryStarted) {
425 adapter_win_->StartDiscovering(
426 base::Closure(), BluetoothAdapter::ErrorCallback());
427 adapter_win_->StopDiscovering(
428 base::Closure(), BluetoothAdapter::ErrorCallback());
429 bluetooth_task_runner_->ClearPendingTasks();
430 adapter_win_->DiscoveryStarted(true);
431 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size());
434 TEST_F(BluetoothAdapterWinTest, StartAndStopBeforeDiscoveryStarted) {
435 int num_expected_start_discoveries = 3;
436 int num_expected_stop_discoveries = 2;
437 for (int i = 0; i < num_expected_start_discoveries; i++) {
438 adapter_win_->StartDiscovering(
439 base::Bind(
440 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryCallbacks,
441 base::Unretained(this)),
442 BluetoothAdapter::ErrorCallback());
444 for (int i = 0; i < num_expected_stop_discoveries; i++) {
445 adapter_win_->StopDiscovering(
446 base::Bind(
447 &BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks,
448 base::Unretained(this)),
449 BluetoothAdapter::ErrorCallback());
451 bluetooth_task_runner_->ClearPendingTasks();
452 adapter_win_->DiscoveryStarted(true);
453 EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty());
454 ui_task_runner_->RunPendingTasks();
455 EXPECT_EQ(num_expected_start_discoveries, num_start_discovery_callbacks_);
456 EXPECT_EQ(num_expected_stop_discoveries, num_stop_discovery_callbacks_);
459 TEST_F(BluetoothAdapterWinTest, StopDiscoveryBeforeDiscoveryStartedAndFailed) {
460 adapter_win_->StartDiscovering(
461 base::Closure(),
462 base::Bind(
463 &BluetoothAdapterWinTest::IncrementNumStartDiscoveryErrorCallbacks,
464 base::Unretained(this)));
465 adapter_win_->StopDiscovering(
466 base::Bind(
467 &BluetoothAdapterWinTest::IncrementNumStopDiscoveryCallbacks,
468 base::Unretained(this)),
469 BluetoothAdapter::ErrorCallback());
470 ui_task_runner_->ClearPendingTasks();
471 adapter_win_->DiscoveryStarted(false);
472 ui_task_runner_->RunPendingTasks();
473 EXPECT_EQ(1, num_start_discovery_error_callbacks_);
474 EXPECT_EQ(1, num_stop_discovery_callbacks_);
475 EXPECT_EQ(0, adapter_observer_.num_discovering_changed());
478 TEST_F(BluetoothAdapterWinTest, DevicesDiscovered) {
479 BluetoothTaskManagerWin::DeviceState* android_phone_state =
480 new BluetoothTaskManagerWin::DeviceState();
481 MakeDeviceState("phone", "android phone address", android_phone_state);
482 BluetoothTaskManagerWin::DeviceState* laptop_state =
483 new BluetoothTaskManagerWin::DeviceState();
484 MakeDeviceState("laptop", "laptop address", laptop_state);
485 BluetoothTaskManagerWin::DeviceState* iphone_state =
486 new BluetoothTaskManagerWin::DeviceState();
487 MakeDeviceState("phone", "iphone address", iphone_state);
488 ScopedVector<BluetoothTaskManagerWin::DeviceState> devices;
489 devices.push_back(android_phone_state);
490 devices.push_back(laptop_state);
491 devices.push_back(iphone_state);
493 adapter_win_->DevicesDiscovered(devices);
494 EXPECT_EQ(3, adapter_observer_.num_device_added());
497 } // namespace device