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.
6 #include "base/memory/ref_counted.h"
7 #include "base/test/test_pending_task.h"
8 #include "base/test/test_simple_task_runner.h"
9 #include "device/bluetooth/bluetooth_init_win.h"
10 #include "device/bluetooth/bluetooth_task_manager_win.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class BluetoothTaskObserver
: public device::BluetoothTaskManagerWin::Observer
{
17 BluetoothTaskObserver() {
21 virtual ~BluetoothTaskObserver() {
24 virtual void AdapterStateChanged(
25 const device::BluetoothTaskManagerWin::AdapterState
& state
) OVERRIDE
{
26 num_adapter_state_changed_
++;
29 virtual void DiscoveryStarted(bool success
) OVERRIDE
{
30 num_discovery_started_
++;
33 virtual void DiscoveryStopped() OVERRIDE
{
34 num_discovery_stopped_
++;
37 virtual void DevicesDiscovered(
38 const ScopedVector
<device::BluetoothTaskManagerWin::DeviceState
>&
40 num_devices_discovered_
++;
44 num_adapter_state_changed_
= 0;
45 num_discovery_started_
= 0;
46 num_discovery_stopped_
= 0;
47 num_devices_discovered_
= 0;
50 int num_adapter_state_changed() const {
51 return num_adapter_state_changed_
;
54 int num_discovery_started() const {
55 return num_discovery_started_
;
58 int num_discovery_stopped() const {
59 return num_discovery_stopped_
;
62 int num_devices_discovered() const {
63 return num_devices_discovered_
;
67 int num_adapter_state_changed_
;
68 int num_discovery_started_
;
69 int num_discovery_stopped_
;
70 int num_devices_discovered_
;
77 class BluetoothTaskManagerWinTest
: public testing::Test
{
79 BluetoothTaskManagerWinTest()
80 : ui_task_runner_(new base::TestSimpleTaskRunner()),
81 bluetooth_task_runner_(new base::TestSimpleTaskRunner()),
82 task_manager_(new BluetoothTaskManagerWin(ui_task_runner_
)),
83 has_bluetooth_stack_(device::bluetooth_init_win::HasBluetoothStack()) {
84 task_manager_
->InitializeWithBluetoothTaskRunner(bluetooth_task_runner_
);
87 virtual void SetUp() {
88 task_manager_
->AddObserver(&observer_
);
91 virtual void TearDown() {
92 task_manager_
->RemoveObserver(&observer_
);
95 int GetPollingIntervalMs() const {
96 return BluetoothTaskManagerWin::kPollIntervalMs
;
100 scoped_refptr
<base::TestSimpleTaskRunner
> ui_task_runner_
;
101 scoped_refptr
<base::TestSimpleTaskRunner
> bluetooth_task_runner_
;
102 scoped_refptr
<BluetoothTaskManagerWin
> task_manager_
;
103 BluetoothTaskObserver observer_
;
104 const bool has_bluetooth_stack_
;
107 TEST_F(BluetoothTaskManagerWinTest
, StartPolling
) {
108 EXPECT_EQ(1, bluetooth_task_runner_
->GetPendingTasks().size());
111 TEST_F(BluetoothTaskManagerWinTest
, PollAdapterIfBluetoothStackIsAvailable
) {
112 bluetooth_task_runner_
->RunPendingTasks();
113 int num_expected_pending_tasks
= has_bluetooth_stack_
? 1 : 0;
114 EXPECT_EQ(num_expected_pending_tasks
,
115 bluetooth_task_runner_
->GetPendingTasks().size());
118 TEST_F(BluetoothTaskManagerWinTest
, Polling
) {
119 if (!has_bluetooth_stack_
)
124 for (int i
= 0; i
< num_polls
; i
++) {
125 bluetooth_task_runner_
->RunPendingTasks();
128 ui_task_runner_
->RunPendingTasks();
129 EXPECT_EQ(num_polls
, observer_
.num_adapter_state_changed());
132 TEST_F(BluetoothTaskManagerWinTest
, SetPowered
) {
133 if (!has_bluetooth_stack_
)
136 bluetooth_task_runner_
->ClearPendingTasks();
137 base::Closure closure
;
138 task_manager_
->PostSetPoweredBluetoothTask(true, closure
, closure
);
140 EXPECT_EQ(1, bluetooth_task_runner_
->GetPendingTasks().size());
141 bluetooth_task_runner_
->RunPendingTasks();
142 EXPECT_TRUE(ui_task_runner_
->GetPendingTasks().size() >= 1);
145 TEST_F(BluetoothTaskManagerWinTest
, Discovery
) {
146 if (!has_bluetooth_stack_
)
149 bluetooth_task_runner_
->RunPendingTasks();
150 bluetooth_task_runner_
->ClearPendingTasks();
151 task_manager_
->PostStartDiscoveryTask();
152 bluetooth_task_runner_
->RunPendingTasks();
153 ui_task_runner_
->RunPendingTasks();
154 EXPECT_EQ(1, observer_
.num_discovery_started());
155 task_manager_
->PostStopDiscoveryTask();
156 bluetooth_task_runner_
->RunPendingTasks();
157 ui_task_runner_
->RunPendingTasks();
158 EXPECT_EQ(1, observer_
.num_discovery_stopped());
161 } // namespace device