1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/ref_counted.h"
#include "base/test/test_pending_task.h"
#include "base/test/test_simple_task_runner.h"
#include "device/bluetooth/bluetooth_classic_win.h"
#include "device/bluetooth/bluetooth_init_win.h"
#include "device/bluetooth/bluetooth_task_manager_win.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class BluetoothTaskObserver : public device::BluetoothTaskManagerWin::Observer {
public:
BluetoothTaskObserver()
: num_adapter_state_changed_(0),
num_discovery_started_(0),
num_discovery_stopped_(0) {
}
~BluetoothTaskObserver() override {}
void AdapterStateChanged(
const device::BluetoothTaskManagerWin::AdapterState& state) override {
num_adapter_state_changed_++;
}
void DiscoveryStarted(bool success) override { num_discovery_started_++; }
void DiscoveryStopped() override { num_discovery_stopped_++; }
int num_adapter_state_changed() const {
return num_adapter_state_changed_;
}
int num_discovery_started() const {
return num_discovery_started_;
}
int num_discovery_stopped() const {
return num_discovery_stopped_;
}
private:
int num_adapter_state_changed_;
int num_discovery_started_;
int num_discovery_stopped_;
};
} // namespace
namespace device {
class BluetoothTaskManagerWinTest : public testing::Test {
public:
BluetoothTaskManagerWinTest()
: ui_task_runner_(new base::TestSimpleTaskRunner()),
bluetooth_task_runner_(new base::TestSimpleTaskRunner()),
task_manager_(new BluetoothTaskManagerWin(ui_task_runner_)),
has_bluetooth_stack_(device::bluetooth_init_win::HasBluetoothStack()) {
task_manager_->InitializeWithBluetoothTaskRunner(bluetooth_task_runner_);
}
void SetUp() override { task_manager_->AddObserver(&observer_); }
void TearDown() override { task_manager_->RemoveObserver(&observer_); }
int GetPollingIntervalMs() const {
return BluetoothTaskManagerWin::kPollIntervalMs;
}
protected:
scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_;
scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_;
scoped_refptr<BluetoothTaskManagerWin> task_manager_;
BluetoothTaskObserver observer_;
const bool has_bluetooth_stack_;
};
TEST_F(BluetoothTaskManagerWinTest, StartPolling) {
EXPECT_EQ(1u, bluetooth_task_runner_->NumPendingTasks());
}
TEST_F(BluetoothTaskManagerWinTest, PollAdapterIfBluetoothStackIsAvailable) {
bluetooth_task_runner_->RunPendingTasks();
size_t num_expected_pending_tasks = has_bluetooth_stack_ ? 1 : 0;
EXPECT_EQ(num_expected_pending_tasks,
bluetooth_task_runner_->NumPendingTasks());
}
TEST_F(BluetoothTaskManagerWinTest, Polling) {
if (!has_bluetooth_stack_)
return;
int num_polls = 5;
for (int i = 0; i < num_polls; i++) {
bluetooth_task_runner_->RunPendingTasks();
}
ui_task_runner_->RunPendingTasks();
EXPECT_EQ(num_polls, observer_.num_adapter_state_changed());
}
TEST_F(BluetoothTaskManagerWinTest, SetPowered) {
if (!has_bluetooth_stack_)
return;
bluetooth_task_runner_->ClearPendingTasks();
task_manager_->PostSetPoweredBluetoothTask(true, base::OnceClosure(),
base::OnceClosure());
EXPECT_EQ(1u, bluetooth_task_runner_->NumPendingTasks());
bluetooth_task_runner_->RunPendingTasks();
EXPECT_TRUE(ui_task_runner_->NumPendingTasks() >= 1);
}
TEST_F(BluetoothTaskManagerWinTest, Discovery) {
if (!has_bluetooth_stack_)
return;
bluetooth_task_runner_->RunPendingTasks();
bluetooth_task_runner_->ClearPendingTasks();
task_manager_->PostStartDiscoveryTask();
bluetooth_task_runner_->RunPendingTasks();
ui_task_runner_->RunPendingTasks();
EXPECT_EQ(1, observer_.num_discovery_started());
task_manager_->PostStopDiscoveryTask();
bluetooth_task_runner_->RunPendingTasks();
ui_task_runner_->RunPendingTasks();
EXPECT_EQ(1, observer_.num_discovery_stopped());
}
} // namespace device
|