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

Side by Side Diff: device/bluetooth/bluetooth_adapter_win_unittest.cc

Issue 424093004: Improve processing of Bluetooth device discovery on Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address code review feedback (nits and memory leak). Created 6 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
« no previous file with comments | « device/bluetooth/bluetooth_adapter_win.cc ('k') | device/bluetooth/bluetooth_device_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 <string> 5 #include <string>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/strings/string_number_conversions.h"
9 #include "base/test/test_simple_task_runner.h" 10 #include "base/test/test_simple_task_runner.h"
10 #include "device/bluetooth/bluetooth_adapter.h" 11 #include "device/bluetooth/bluetooth_adapter.h"
11 #include "device/bluetooth/bluetooth_adapter_win.h" 12 #include "device/bluetooth/bluetooth_adapter_win.h"
12 #include "device/bluetooth/bluetooth_device.h" 13 #include "device/bluetooth/bluetooth_device.h"
13 #include "device/bluetooth/bluetooth_task_manager_win.h" 14 #include "device/bluetooth/bluetooth_task_manager_win.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 namespace { 17 namespace {
17 18
18 const char kAdapterAddress[] = "A1:B2:C3:D4:E5:F6"; 19 const char kAdapterAddress[] = "A1:B2:C3:D4:E5:F6";
19 const char kAdapterName[] = "Bluetooth Adapter Name"; 20 const char kAdapterName[] = "Bluetooth Adapter Name";
20 21
22 const char kTestAudioSdpName[] = "Audio";
23 const char kTestAudioSdpName2[] = "Audio2";
24 const char kTestAudioSdpBytes[] =
25 "35510900000a00010001090001350319110a09000435103506190100090019350619001909"
26 "010209000535031910020900093508350619110d090102090100250c417564696f20536f75"
27 "726365090311090001";
28 const device::BluetoothUUID kTestAudioSdpUuid("110a");
21 29
22 void MakeDeviceState(const std::string& name, 30 void MakeDeviceState(const std::string& name,
23 const std::string& address, 31 const std::string& address,
24 device::BluetoothTaskManagerWin::DeviceState* state) { 32 device::BluetoothTaskManagerWin::DeviceState* state) {
25 state->name = name; 33 state->name = name;
26 state->address = address; 34 state->address = address;
27 state->bluetooth_class = 0; 35 state->bluetooth_class = 0;
28 state->authenticated = false; 36 state->authenticated = false;
29 state->connected = false; 37 state->connected = false;
30 } 38 }
31 39
32 class AdapterObserver : public device::BluetoothAdapter::Observer { 40 class AdapterObserver : public device::BluetoothAdapter::Observer {
33 public: 41 public:
34 AdapterObserver() 42 AdapterObserver() { ResetCounters(); }
35 : num_present_changed_(0), 43
36 num_powered_changed_(0), 44 void ResetCounters() {
37 num_discovering_changed_(0), 45 num_present_changed_ = 0;
38 num_device_added_(0) { 46 num_powered_changed_ = 0;
47 num_discovering_changed_ = 0;
48 num_device_added_ = 0;
49 num_device_removed_ = 0;
50 num_device_changed_ = 0;
39 } 51 }
40 52
41 virtual void AdapterPresentChanged( 53 virtual void AdapterPresentChanged(
42 device::BluetoothAdapter* adapter, bool present) OVERRIDE { 54 device::BluetoothAdapter* adapter, bool present) OVERRIDE {
43 num_present_changed_++; 55 num_present_changed_++;
44 } 56 }
45 57
46 virtual void AdapterPoweredChanged( 58 virtual void AdapterPoweredChanged(
47 device::BluetoothAdapter* adapter, bool powered) OVERRIDE { 59 device::BluetoothAdapter* adapter, bool powered) OVERRIDE {
48 num_powered_changed_++; 60 num_powered_changed_++;
49 } 61 }
50 62
51 virtual void AdapterDiscoveringChanged( 63 virtual void AdapterDiscoveringChanged(
52 device::BluetoothAdapter* adapter, bool discovering) OVERRIDE { 64 device::BluetoothAdapter* adapter, bool discovering) OVERRIDE {
53 num_discovering_changed_++; 65 num_discovering_changed_++;
54 } 66 }
55 67
56 virtual void DeviceAdded( 68 virtual void DeviceAdded(
57 device::BluetoothAdapter* adapter, 69 device::BluetoothAdapter* adapter,
58 device::BluetoothDevice* device) OVERRIDE { 70 device::BluetoothDevice* device) OVERRIDE {
59 num_device_added_++; 71 num_device_added_++;
60 } 72 }
61 73
62 int num_present_changed() const { 74 virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
63 return num_present_changed_; 75 device::BluetoothDevice* device) OVERRIDE {
76 num_device_removed_++;
64 } 77 }
65 78
66 int num_powered_changed() const { 79 virtual void DeviceChanged(device::BluetoothAdapter* adapter,
67 return num_powered_changed_; 80 device::BluetoothDevice* device) OVERRIDE {
81 num_device_changed_++;
68 } 82 }
69 83
70 int num_discovering_changed() const { 84 int num_present_changed() const { return num_present_changed_; }
71 return num_discovering_changed_;
72 }
73 85
74 int num_device_added() const { 86 int num_powered_changed() const { return num_powered_changed_; }
75 return num_device_added_; 87
76 } 88 int num_discovering_changed() const { return num_discovering_changed_; }
89
90 int num_device_added() const { return num_device_added_; }
91
92 int num_device_removed() const { return num_device_removed_; }
93
94 int num_device_changed() const { return num_device_changed_; }
77 95
78 private: 96 private:
79 int num_present_changed_; 97 int num_present_changed_;
80 int num_powered_changed_; 98 int num_powered_changed_;
81 int num_discovering_changed_; 99 int num_discovering_changed_;
82 int num_device_added_; 100 int num_device_added_;
101 int num_device_removed_;
102 int num_device_changed_;
83 }; 103 };
84 104
85 } // namespace 105 } // namespace
86 106
87 namespace device { 107 namespace device {
88 108
89 class BluetoothAdapterWinTest : public testing::Test { 109 class BluetoothAdapterWinTest : public testing::Test {
90 public: 110 public:
91 BluetoothAdapterWinTest() 111 BluetoothAdapterWinTest()
92 : ui_task_runner_(new base::TestSimpleTaskRunner()), 112 : ui_task_runner_(new base::TestSimpleTaskRunner()),
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 base::Unretained(this)), 500 base::Unretained(this)),
481 BluetoothAdapter::ErrorCallback()); 501 BluetoothAdapter::ErrorCallback());
482 ui_task_runner_->ClearPendingTasks(); 502 ui_task_runner_->ClearPendingTasks();
483 adapter_win_->DiscoveryStarted(false); 503 adapter_win_->DiscoveryStarted(false);
484 ui_task_runner_->RunPendingTasks(); 504 ui_task_runner_->RunPendingTasks();
485 EXPECT_EQ(1, num_start_discovery_error_callbacks_); 505 EXPECT_EQ(1, num_start_discovery_error_callbacks_);
486 EXPECT_EQ(1, num_stop_discovery_callbacks_); 506 EXPECT_EQ(1, num_stop_discovery_callbacks_);
487 EXPECT_EQ(0, adapter_observer_.num_discovering_changed()); 507 EXPECT_EQ(0, adapter_observer_.num_discovering_changed());
488 } 508 }
489 509
490 TEST_F(BluetoothAdapterWinTest, DevicesDiscovered) { 510 TEST_F(BluetoothAdapterWinTest, DevicesPolled) {
491 BluetoothTaskManagerWin::DeviceState* android_phone_state = 511 BluetoothTaskManagerWin::DeviceState* android_phone_state =
492 new BluetoothTaskManagerWin::DeviceState(); 512 new BluetoothTaskManagerWin::DeviceState();
493 MakeDeviceState("phone", "android phone address", android_phone_state); 513 MakeDeviceState("phone", "A1:B2:C3:D4:E5:E0", android_phone_state);
494 BluetoothTaskManagerWin::DeviceState* laptop_state = 514 BluetoothTaskManagerWin::DeviceState* laptop_state =
495 new BluetoothTaskManagerWin::DeviceState(); 515 new BluetoothTaskManagerWin::DeviceState();
496 MakeDeviceState("laptop", "laptop address", laptop_state); 516 MakeDeviceState("laptop", "A1:B2:C3:D4:E5:E1", laptop_state);
497 BluetoothTaskManagerWin::DeviceState* iphone_state = 517 BluetoothTaskManagerWin::DeviceState* iphone_state =
498 new BluetoothTaskManagerWin::DeviceState(); 518 new BluetoothTaskManagerWin::DeviceState();
499 MakeDeviceState("phone", "iphone address", iphone_state); 519 MakeDeviceState("phone", "A1:B2:C3:D4:E5:E2", iphone_state);
500 ScopedVector<BluetoothTaskManagerWin::DeviceState> devices; 520 ScopedVector<BluetoothTaskManagerWin::DeviceState> devices;
501 devices.push_back(android_phone_state); 521 devices.push_back(android_phone_state);
502 devices.push_back(laptop_state); 522 devices.push_back(laptop_state);
503 devices.push_back(iphone_state); 523 devices.push_back(iphone_state);
504 524
505 adapter_win_->DevicesDiscovered(devices); 525 // Add 3 devices
526 adapter_observer_.ResetCounters();
527 adapter_win_->DevicesPolled(devices);
506 EXPECT_EQ(3, adapter_observer_.num_device_added()); 528 EXPECT_EQ(3, adapter_observer_.num_device_added());
529 EXPECT_EQ(0, adapter_observer_.num_device_removed());
530 EXPECT_EQ(0, adapter_observer_.num_device_changed());
531
532 // Change a device name
533 android_phone_state->name = "phone2";
534 adapter_observer_.ResetCounters();
535 adapter_win_->DevicesPolled(devices);
536 EXPECT_EQ(0, adapter_observer_.num_device_added());
537 EXPECT_EQ(0, adapter_observer_.num_device_removed());
538 EXPECT_EQ(1, adapter_observer_.num_device_changed());
539
540 // Change a device address
541 android_phone_state->address = "A1:B2:C3:D4:E5:E6";
542 adapter_observer_.ResetCounters();
543 adapter_win_->DevicesPolled(devices);
544 EXPECT_EQ(1, adapter_observer_.num_device_added());
545 EXPECT_EQ(1, adapter_observer_.num_device_removed());
546 EXPECT_EQ(0, adapter_observer_.num_device_changed());
547
548 // Remove a device
549 devices.erase(devices.begin());
550 adapter_observer_.ResetCounters();
551 adapter_win_->DevicesPolled(devices);
552 EXPECT_EQ(0, adapter_observer_.num_device_added());
553 EXPECT_EQ(1, adapter_observer_.num_device_removed());
554 EXPECT_EQ(0, adapter_observer_.num_device_changed());
555
556 // Add a service
557 BluetoothTaskManagerWin::ServiceRecordState* audio_state =
558 new BluetoothTaskManagerWin::ServiceRecordState();
559 audio_state->name = kTestAudioSdpName;
560 base::HexStringToBytes(kTestAudioSdpBytes, &audio_state->sdp_bytes);
561 laptop_state->service_record_states.push_back(audio_state);
562 adapter_observer_.ResetCounters();
563 adapter_win_->DevicesPolled(devices);
564 EXPECT_EQ(0, adapter_observer_.num_device_added());
565 EXPECT_EQ(0, adapter_observer_.num_device_removed());
566 EXPECT_EQ(1, adapter_observer_.num_device_changed());
567
568 // Change a service
569 audio_state->name = kTestAudioSdpName2;
570 adapter_observer_.ResetCounters();
571 adapter_win_->DevicesPolled(devices);
572 EXPECT_EQ(0, adapter_observer_.num_device_added());
573 EXPECT_EQ(0, adapter_observer_.num_device_removed());
574 EXPECT_EQ(1, adapter_observer_.num_device_changed());
575
576 // Remove a service
577 laptop_state->service_record_states.clear();
578 adapter_observer_.ResetCounters();
579 adapter_win_->DevicesPolled(devices);
580 EXPECT_EQ(0, adapter_observer_.num_device_added());
581 EXPECT_EQ(0, adapter_observer_.num_device_removed());
582 EXPECT_EQ(1, adapter_observer_.num_device_changed());
507 } 583 }
508 584
509 } // namespace device 585 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_win.cc ('k') | device/bluetooth/bluetooth_device_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698