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()
18 : num_adapter_state_changed_(0),
19 num_discovery_started_(0),
20 num_discovery_stopped_(0) {
23 virtual ~BluetoothTaskObserver() {
26 virtual void AdapterStateChanged(
27 const device::BluetoothTaskManagerWin::AdapterState
& state
) override
{
28 num_adapter_state_changed_
++;
31 virtual void DiscoveryStarted(bool success
) override
{
32 num_discovery_started_
++;
35 virtual void DiscoveryStopped() override
{
36 num_discovery_stopped_
++;
39 int num_adapter_state_changed() const {
40 return num_adapter_state_changed_
;
43 int num_discovery_started() const {
44 return num_discovery_started_
;
47 int num_discovery_stopped() const {
48 return num_discovery_stopped_
;
52 int num_adapter_state_changed_
;
53 int num_discovery_started_
;
54 int num_discovery_stopped_
;
61 class BluetoothTaskManagerWinTest
: public testing::Test
{
63 BluetoothTaskManagerWinTest()
64 : ui_task_runner_(new base::TestSimpleTaskRunner()),
65 bluetooth_task_runner_(new base::TestSimpleTaskRunner()),
66 task_manager_(new BluetoothTaskManagerWin(ui_task_runner_
)),
67 has_bluetooth_stack_(device::bluetooth_init_win::HasBluetoothStack()) {
68 task_manager_
->InitializeWithBluetoothTaskRunner(bluetooth_task_runner_
);
71 virtual void SetUp() {
72 task_manager_
->AddObserver(&observer_
);
75 virtual void TearDown() {
76 task_manager_
->RemoveObserver(&observer_
);
79 int GetPollingIntervalMs() const {
80 return BluetoothTaskManagerWin::kPollIntervalMs
;
84 scoped_refptr
<base::TestSimpleTaskRunner
> ui_task_runner_
;
85 scoped_refptr
<base::TestSimpleTaskRunner
> bluetooth_task_runner_
;
86 scoped_refptr
<BluetoothTaskManagerWin
> task_manager_
;
87 BluetoothTaskObserver observer_
;
88 const bool has_bluetooth_stack_
;
91 TEST_F(BluetoothTaskManagerWinTest
, StartPolling
) {
92 EXPECT_EQ(1, bluetooth_task_runner_
->GetPendingTasks().size());
95 TEST_F(BluetoothTaskManagerWinTest
, PollAdapterIfBluetoothStackIsAvailable
) {
96 bluetooth_task_runner_
->RunPendingTasks();
97 int num_expected_pending_tasks
= has_bluetooth_stack_
? 1 : 0;
98 EXPECT_EQ(num_expected_pending_tasks
,
99 bluetooth_task_runner_
->GetPendingTasks().size());
102 TEST_F(BluetoothTaskManagerWinTest
, Polling
) {
103 if (!has_bluetooth_stack_
)
108 for (int i
= 0; i
< num_polls
; i
++) {
109 bluetooth_task_runner_
->RunPendingTasks();
112 ui_task_runner_
->RunPendingTasks();
113 EXPECT_EQ(num_polls
, observer_
.num_adapter_state_changed());
116 TEST_F(BluetoothTaskManagerWinTest
, SetPowered
) {
117 if (!has_bluetooth_stack_
)
120 bluetooth_task_runner_
->ClearPendingTasks();
121 base::Closure closure
;
122 task_manager_
->PostSetPoweredBluetoothTask(true, closure
, closure
);
124 EXPECT_EQ(1, bluetooth_task_runner_
->GetPendingTasks().size());
125 bluetooth_task_runner_
->RunPendingTasks();
126 EXPECT_TRUE(ui_task_runner_
->GetPendingTasks().size() >= 1);
129 TEST_F(BluetoothTaskManagerWinTest
, Discovery
) {
130 if (!has_bluetooth_stack_
)
133 bluetooth_task_runner_
->RunPendingTasks();
134 bluetooth_task_runner_
->ClearPendingTasks();
135 task_manager_
->PostStartDiscoveryTask();
136 bluetooth_task_runner_
->RunPendingTasks();
137 ui_task_runner_
->RunPendingTasks();
138 EXPECT_EQ(1, observer_
.num_discovery_started());
139 task_manager_
->PostStopDiscoveryTask();
140 bluetooth_task_runner_
->RunPendingTasks();
141 ui_task_runner_
->RunPendingTasks();
142 EXPECT_EQ(1, observer_
.num_discovery_stopped());
145 } // namespace device