third_party: Add OWNERS for re2 library.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_task_manager_win_unittest.cc
blob0a82c7de4526318a785db146ee9636bae52a8913
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 "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"
13 namespace {
15 class 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) {
23 ~BluetoothTaskObserver() override {}
25 void AdapterStateChanged(
26 const device::BluetoothTaskManagerWin::AdapterState& state) override {
27 num_adapter_state_changed_++;
30 void DiscoveryStarted(bool success) override { num_discovery_started_++; }
32 void DiscoveryStopped() override { num_discovery_stopped_++; }
34 int num_adapter_state_changed() const {
35 return num_adapter_state_changed_;
38 int num_discovery_started() const {
39 return num_discovery_started_;
42 int num_discovery_stopped() const {
43 return num_discovery_stopped_;
46 private:
47 int num_adapter_state_changed_;
48 int num_discovery_started_;
49 int num_discovery_stopped_;
52 } // namespace
54 namespace device {
56 class BluetoothTaskManagerWinTest : public testing::Test {
57 public:
58 BluetoothTaskManagerWinTest()
59 : ui_task_runner_(new base::TestSimpleTaskRunner()),
60 bluetooth_task_runner_(new base::TestSimpleTaskRunner()),
61 task_manager_(new BluetoothTaskManagerWin(ui_task_runner_)),
62 has_bluetooth_stack_(device::bluetooth_init_win::HasBluetoothStack()) {
63 task_manager_->InitializeWithBluetoothTaskRunner(bluetooth_task_runner_);
66 void SetUp() override { task_manager_->AddObserver(&observer_); }
68 void TearDown() override { task_manager_->RemoveObserver(&observer_); }
70 int GetPollingIntervalMs() const {
71 return BluetoothTaskManagerWin::kPollIntervalMs;
74 protected:
75 scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_;
76 scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_;
77 scoped_refptr<BluetoothTaskManagerWin> task_manager_;
78 BluetoothTaskObserver observer_;
79 const bool has_bluetooth_stack_;
82 TEST_F(BluetoothTaskManagerWinTest, StartPolling) {
83 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size());
86 TEST_F(BluetoothTaskManagerWinTest, PollAdapterIfBluetoothStackIsAvailable) {
87 bluetooth_task_runner_->RunPendingTasks();
88 int num_expected_pending_tasks = has_bluetooth_stack_ ? 1 : 0;
89 EXPECT_EQ(num_expected_pending_tasks,
90 bluetooth_task_runner_->GetPendingTasks().size());
93 TEST_F(BluetoothTaskManagerWinTest, Polling) {
94 if (!has_bluetooth_stack_)
95 return;
97 int num_polls = 5;
99 for (int i = 0; i < num_polls; i++) {
100 bluetooth_task_runner_->RunPendingTasks();
103 ui_task_runner_->RunPendingTasks();
104 EXPECT_EQ(num_polls, observer_.num_adapter_state_changed());
107 TEST_F(BluetoothTaskManagerWinTest, SetPowered) {
108 if (!has_bluetooth_stack_)
109 return;
111 bluetooth_task_runner_->ClearPendingTasks();
112 base::Closure closure;
113 task_manager_->PostSetPoweredBluetoothTask(true, closure, closure);
115 EXPECT_EQ(1, bluetooth_task_runner_->GetPendingTasks().size());
116 bluetooth_task_runner_->RunPendingTasks();
117 EXPECT_TRUE(ui_task_runner_->GetPendingTasks().size() >= 1);
120 TEST_F(BluetoothTaskManagerWinTest, Discovery) {
121 if (!has_bluetooth_stack_)
122 return;
124 bluetooth_task_runner_->RunPendingTasks();
125 bluetooth_task_runner_->ClearPendingTasks();
126 task_manager_->PostStartDiscoveryTask();
127 bluetooth_task_runner_->RunPendingTasks();
128 ui_task_runner_->RunPendingTasks();
129 EXPECT_EQ(1, observer_.num_discovery_started());
130 task_manager_->PostStopDiscoveryTask();
131 bluetooth_task_runner_->RunPendingTasks();
132 ui_task_runner_->RunPendingTasks();
133 EXPECT_EQ(1, observer_.num_discovery_stopped());
136 } // namespace device