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.
4
5#include "base/bind.h"
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"
12
13namespace {
14
15class BluetoothTaskObserver : public device::BluetoothTaskManagerWin::Observer {
16 public:
17  BluetoothTaskObserver()
18      : num_adapter_state_changed_(0),
19        num_discovery_started_(0),
20        num_discovery_stopped_(0) {
21  }
22
23  virtual ~BluetoothTaskObserver() {
24  }
25
26  virtual void AdapterStateChanged(
27    const device::BluetoothTaskManagerWin::AdapterState& state) OVERRIDE {
28    num_adapter_state_changed_++;
29  }
30
31  virtual void DiscoveryStarted(bool success) OVERRIDE {
32    num_discovery_started_++;
33  }
34
35  virtual void DiscoveryStopped() OVERRIDE {
36    num_discovery_stopped_++;
37  }
38
39  int num_adapter_state_changed() const {
40    return num_adapter_state_changed_;
41  }
42
43  int num_discovery_started() const {
44    return num_discovery_started_;
45  }
46
47  int num_discovery_stopped() const {
48    return num_discovery_stopped_;
49  }
50
51 private:
52   int num_adapter_state_changed_;
53   int num_discovery_started_;
54   int num_discovery_stopped_;
55};
56
57}  // namespace
58
59namespace device {
60
61class BluetoothTaskManagerWinTest : public testing::Test {
62 public:
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_);
69  }
70
71  virtual void SetUp() {
72    task_manager_->AddObserver(&observer_);
73  }
74
75  virtual void TearDown() {
76    task_manager_->RemoveObserver(&observer_);
77  }
78
79  int GetPollingIntervalMs() const {
80    return BluetoothTaskManagerWin::kPollIntervalMs;
81  }
82
83 protected:
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_;
89};
90
91TEST_F(BluetoothTaskManagerWinTest, StartPolling) {
92  EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size());
93}
94
95TEST_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());
100}
101
102TEST_F(BluetoothTaskManagerWinTest, Polling) {
103  if (!has_bluetooth_stack_)
104    return;
105
106  int num_polls = 5;
107
108  for (int i = 0; i < num_polls; i++) {
109    bluetooth_task_runner_->RunPendingTasks();
110  }
111
112  ui_task_runner_->RunPendingTasks();
113  EXPECT_EQ(num_polls, observer_.num_adapter_state_changed());
114}
115
116TEST_F(BluetoothTaskManagerWinTest, SetPowered) {
117  if (!has_bluetooth_stack_)
118    return;
119
120  bluetooth_task_runner_->ClearPendingTasks();
121  base::Closure closure;
122  task_manager_->PostSetPoweredBluetoothTask(true, closure, closure);
123
124  EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size());
125  bluetooth_task_runner_->RunPendingTasks();
126  EXPECT_TRUE(ui_task_runner_->GetPendingTasks().size() >= 1);
127}
128
129TEST_F(BluetoothTaskManagerWinTest, Discovery) {
130  if (!has_bluetooth_stack_)
131    return;
132
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());
143}
144
145}  // namespace device
146