diff options
author | scheib <scheib@chromium.org> | 2015-04-29 14:46:57 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-29 21:48:15 +0000 |
commit | 42e6b49d98c2e3bcac098bdf8e7a2139d22bad0d (patch) | |
tree | 153fb1e19edea2d932a619f58fb08a62eddf48a1 /device | |
parent | 870867909e7ea656d43405e396c12e088c9e787f (diff) | |
download | chromium_src-42e6b49d98c2e3bcac098bdf8e7a2139d22bad0d.zip chromium_src-42e6b49d98c2e3bcac098bdf8e7a2139d22bad0d.tar.gz chromium_src-42e6b49d98c2e3bcac098bdf8e7a2139d22bad0d.tar.bz2 |
bluetooth: Refactor multiple test observers to TestBluetoothAdapterObserver.
In preparation for BluetoothAdapterAndroid, reduce code duplication by
combining multiple test subclasses of BluetoothAdapter::Observer into
a single TestBluetoothAdapterObserver.
BUG=471536
Review URL: https://codereview.chromium.org/1082573003
Cr-Commit-Position: refs/heads/master@{#327579}
Diffstat (limited to 'device')
-rw-r--r-- | device/BUILD.gn | 2 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_win_unittest.cc | 159 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_chromeos_unittest.cc | 624 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_gatt_chromeos_unittest.cc | 496 | ||||
-rw-r--r-- | device/bluetooth/test/test_bluetooth_adapter_observer.cc | 296 | ||||
-rw-r--r-- | device/bluetooth/test/test_bluetooth_adapter_observer.h | 164 | ||||
-rw-r--r-- | device/device_tests.gyp | 2 |
7 files changed, 914 insertions, 829 deletions
diff --git a/device/BUILD.gn b/device/BUILD.gn index aadefeb..8523b5f 100644 --- a/device/BUILD.gn +++ b/device/BUILD.gn @@ -33,6 +33,8 @@ test("device_unittests") { "bluetooth/bluetooth_socket_chromeos_unittest.cc", "bluetooth/bluetooth_task_manager_win_unittest.cc", "bluetooth/bluetooth_uuid_unittest.cc", + "bluetooth/test/test_bluetooth_adapter_observer.cc", + "bluetooth/test/test_bluetooth_adapter_observer.h", "nfc/nfc_chromeos_unittest.cc", "nfc/nfc_ndef_record_unittest.cc", "test/run_all_unittests.cc", diff --git a/device/bluetooth/bluetooth_adapter_win_unittest.cc b/device/bluetooth/bluetooth_adapter_win_unittest.cc index df445f8..b852282 100644 --- a/device/bluetooth/bluetooth_adapter_win_unittest.cc +++ b/device/bluetooth/bluetooth_adapter_win_unittest.cc @@ -12,6 +12,7 @@ #include "device/bluetooth/bluetooth_adapter_win.h" #include "device/bluetooth/bluetooth_device.h" #include "device/bluetooth/bluetooth_task_manager_win.h" +#include "device/bluetooth/test/test_bluetooth_adapter_observer.h" #include "testing/gtest/include/gtest/gtest.h" namespace { @@ -37,70 +38,6 @@ void MakeDeviceState(const std::string& name, state->connected = false; } -class AdapterObserver : public device::BluetoothAdapter::Observer { - public: - AdapterObserver() { ResetCounters(); } - - void ResetCounters() { - num_present_changed_ = 0; - num_powered_changed_ = 0; - num_discovering_changed_ = 0; - num_device_added_ = 0; - num_device_removed_ = 0; - num_device_changed_ = 0; - } - - void AdapterPresentChanged(device::BluetoothAdapter* adapter, - bool present) override { - num_present_changed_++; - } - - void AdapterPoweredChanged(device::BluetoothAdapter* adapter, - bool powered) override { - num_powered_changed_++; - } - - void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter, - bool discovering) override { - num_discovering_changed_++; - } - - void DeviceAdded(device::BluetoothAdapter* adapter, - device::BluetoothDevice* device) override { - num_device_added_++; - } - - void DeviceRemoved(device::BluetoothAdapter* adapter, - device::BluetoothDevice* device) override { - num_device_removed_++; - } - - void DeviceChanged(device::BluetoothAdapter* adapter, - device::BluetoothDevice* device) override { - num_device_changed_++; - } - - int num_present_changed() const { return num_present_changed_; } - - int num_powered_changed() const { return num_powered_changed_; } - - int num_discovering_changed() const { return num_discovering_changed_; } - - int num_device_added() const { return num_device_added_; } - - int num_device_removed() const { return num_device_removed_; } - - int num_device_changed() const { return num_device_changed_; } - - private: - int num_present_changed_; - int num_powered_changed_; - int num_discovering_changed_; - int num_device_added_; - int num_device_removed_; - int num_device_changed_; -}; - } // namespace namespace device { @@ -111,23 +48,21 @@ class BluetoothAdapterWinTest : public testing::Test { : ui_task_runner_(new base::TestSimpleTaskRunner()), bluetooth_task_runner_(new base::TestSimpleTaskRunner()), adapter_(new BluetoothAdapterWin( - base::Bind(&BluetoothAdapterWinTest::RunInitCallback, - base::Unretained(this)))), + base::Bind(&BluetoothAdapterWinTest::RunInitCallback, + base::Unretained(this)))), adapter_win_(static_cast<BluetoothAdapterWin*>(adapter_.get())), + observer_(adapter_), init_callback_called_(false) { adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); } void SetUp() override { - adapter_win_->AddObserver(&adapter_observer_); num_start_discovery_callbacks_ = 0; num_start_discovery_error_callbacks_ = 0; num_stop_discovery_callbacks_ = 0; num_stop_discovery_error_callbacks_ = 0; } - void TearDown() override { adapter_win_->RemoveObserver(&adapter_observer_); } - void RunInitCallback() { init_callback_called_ = true; } @@ -165,7 +100,7 @@ class BluetoothAdapterWinTest : public testing::Test { scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_; scoped_refptr<BluetoothAdapter> adapter_; BluetoothAdapterWin* adapter_win_; - AdapterObserver adapter_observer_; + TestBluetoothAdapterObserver observer_; bool init_callback_called_; int num_start_discovery_callbacks_; int num_start_discovery_error_callbacks_; @@ -192,24 +127,24 @@ TEST_F(BluetoothAdapterWinTest, AdapterPresentChanged) { state.address = kAdapterAddress; state.name = kAdapterName; adapter_win_->AdapterStateChanged(state); - EXPECT_EQ(1, adapter_observer_.num_present_changed()); + EXPECT_EQ(1, observer_.present_changed_count()); adapter_win_->AdapterStateChanged(state); - EXPECT_EQ(1, adapter_observer_.num_present_changed()); + EXPECT_EQ(1, observer_.present_changed_count()); BluetoothTaskManagerWin::AdapterState empty_state; adapter_win_->AdapterStateChanged(empty_state); - EXPECT_EQ(2, adapter_observer_.num_present_changed()); + EXPECT_EQ(2, observer_.present_changed_count()); } TEST_F(BluetoothAdapterWinTest, AdapterPoweredChanged) { BluetoothTaskManagerWin::AdapterState state; state.powered = true; adapter_win_->AdapterStateChanged(state); - EXPECT_EQ(1, adapter_observer_.num_powered_changed()); + EXPECT_EQ(1, observer_.powered_changed_count()); adapter_win_->AdapterStateChanged(state); - EXPECT_EQ(1, adapter_observer_.num_powered_changed()); + EXPECT_EQ(1, observer_.powered_changed_count()); state.powered = false; adapter_win_->AdapterStateChanged(state); - EXPECT_EQ(2, adapter_observer_.num_powered_changed()); + EXPECT_EQ(2, observer_.powered_changed_count()); } TEST_F(BluetoothAdapterWinTest, AdapterInitialized) { @@ -235,7 +170,7 @@ TEST_F(BluetoothAdapterWinTest, SingleStartDiscovery) { ui_task_runner_->RunPendingTasks(); EXPECT_TRUE(adapter_->IsDiscovering()); EXPECT_EQ(1, num_start_discovery_callbacks_); - EXPECT_EQ(1, adapter_observer_.num_discovering_changed()); + EXPECT_EQ(1, observer_.discovering_changed_count()); } TEST_F(BluetoothAdapterWinTest, SingleStartDiscoveryFailure) { @@ -248,7 +183,7 @@ TEST_F(BluetoothAdapterWinTest, SingleStartDiscoveryFailure) { ui_task_runner_->RunPendingTasks(); EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_EQ(1, num_start_discovery_error_callbacks_); - EXPECT_EQ(0, adapter_observer_.num_discovering_changed()); + EXPECT_EQ(0, observer_.discovering_changed_count()); } TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveries) { @@ -269,7 +204,7 @@ TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveries) { ui_task_runner_->RunPendingTasks(); EXPECT_TRUE(adapter_->IsDiscovering()); EXPECT_EQ(num_discoveries, num_start_discovery_callbacks_); - EXPECT_EQ(1, adapter_observer_.num_discovering_changed()); + EXPECT_EQ(1, observer_.discovering_changed_count()); } TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesFailure) { @@ -285,7 +220,7 @@ TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesFailure) { ui_task_runner_->RunPendingTasks(); EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_EQ(num_discoveries, num_start_discovery_error_callbacks_); - EXPECT_EQ(0, adapter_observer_.num_discovering_changed()); + EXPECT_EQ(0, observer_.discovering_changed_count()); } TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesAfterDiscovering) { @@ -312,7 +247,7 @@ TEST_F(BluetoothAdapterWinTest, MultipleStartDiscoveriesAfterDiscovering) { EXPECT_EQ(num_start_discovery_callbacks + 1, num_start_discovery_callbacks_); } - EXPECT_EQ(1, adapter_observer_.num_discovering_changed()); + EXPECT_EQ(1, observer_.discovering_changed_count()); } TEST_F(BluetoothAdapterWinTest, StartDiscoveryAfterDiscoveringFailure) { @@ -353,7 +288,7 @@ TEST_F(BluetoothAdapterWinTest, SingleStopDiscovery) { EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_EQ(1, num_stop_discovery_callbacks_); EXPECT_TRUE(bluetooth_task_runner_->GetPendingTasks().empty()); - EXPECT_EQ(2, adapter_observer_.num_discovering_changed()); + EXPECT_EQ(2, observer_.discovering_changed_count()); } TEST_F(BluetoothAdapterWinTest, MultipleStopDiscoveries) { @@ -384,7 +319,7 @@ TEST_F(BluetoothAdapterWinTest, MultipleStopDiscoveries) { ui_task_runner_->RunPendingTasks(); EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_EQ(num_discoveries, num_stop_discovery_callbacks_); - EXPECT_EQ(2, adapter_observer_.num_discovering_changed()); + EXPECT_EQ(2, observer_.discovering_changed_count()); } TEST_F(BluetoothAdapterWinTest, @@ -501,7 +436,7 @@ TEST_F(BluetoothAdapterWinTest, StopDiscoveryBeforeDiscoveryStartedAndFailed) { ui_task_runner_->RunPendingTasks(); EXPECT_EQ(1, num_start_discovery_error_callbacks_); EXPECT_EQ(1, num_stop_discovery_callbacks_); - EXPECT_EQ(0, adapter_observer_.num_discovering_changed()); + EXPECT_EQ(0, observer_.discovering_changed_count()); } TEST_F(BluetoothAdapterWinTest, DevicesPolled) { @@ -520,35 +455,35 @@ TEST_F(BluetoothAdapterWinTest, DevicesPolled) { devices.push_back(iphone_state); // Add 3 devices - adapter_observer_.ResetCounters(); + observer_.Reset(); adapter_win_->DevicesPolled(devices); - EXPECT_EQ(3, adapter_observer_.num_device_added()); - EXPECT_EQ(0, adapter_observer_.num_device_removed()); - EXPECT_EQ(0, adapter_observer_.num_device_changed()); + EXPECT_EQ(3, observer_.device_added_count()); + EXPECT_EQ(0, observer_.device_removed_count()); + EXPECT_EQ(0, observer_.device_changed_count()); // Change a device name android_phone_state->name = "phone2"; - adapter_observer_.ResetCounters(); + observer_.Reset(); adapter_win_->DevicesPolled(devices); - EXPECT_EQ(0, adapter_observer_.num_device_added()); - EXPECT_EQ(0, adapter_observer_.num_device_removed()); - EXPECT_EQ(1, adapter_observer_.num_device_changed()); + EXPECT_EQ(0, observer_.device_added_count()); + EXPECT_EQ(0, observer_.device_removed_count()); + EXPECT_EQ(1, observer_.device_changed_count()); // Change a device address android_phone_state->address = "A1:B2:C3:D4:E5:E6"; - adapter_observer_.ResetCounters(); + observer_.Reset(); adapter_win_->DevicesPolled(devices); - EXPECT_EQ(1, adapter_observer_.num_device_added()); - EXPECT_EQ(1, adapter_observer_.num_device_removed()); - EXPECT_EQ(0, adapter_observer_.num_device_changed()); + EXPECT_EQ(1, observer_.device_added_count()); + EXPECT_EQ(1, observer_.device_removed_count()); + EXPECT_EQ(0, observer_.device_changed_count()); // Remove a device devices.erase(devices.begin()); - adapter_observer_.ResetCounters(); + observer_.Reset(); adapter_win_->DevicesPolled(devices); - EXPECT_EQ(0, adapter_observer_.num_device_added()); - EXPECT_EQ(1, adapter_observer_.num_device_removed()); - EXPECT_EQ(0, adapter_observer_.num_device_changed()); + EXPECT_EQ(0, observer_.device_added_count()); + EXPECT_EQ(1, observer_.device_removed_count()); + EXPECT_EQ(0, observer_.device_changed_count()); // Add a service BluetoothTaskManagerWin::ServiceRecordState* audio_state = @@ -556,27 +491,27 @@ TEST_F(BluetoothAdapterWinTest, DevicesPolled) { audio_state->name = kTestAudioSdpName; base::HexStringToBytes(kTestAudioSdpBytes, &audio_state->sdp_bytes); laptop_state->service_record_states.push_back(audio_state); - adapter_observer_.ResetCounters(); + observer_.Reset(); adapter_win_->DevicesPolled(devices); - EXPECT_EQ(0, adapter_observer_.num_device_added()); - EXPECT_EQ(0, adapter_observer_.num_device_removed()); - EXPECT_EQ(1, adapter_observer_.num_device_changed()); + EXPECT_EQ(0, observer_.device_added_count()); + EXPECT_EQ(0, observer_.device_removed_count()); + EXPECT_EQ(1, observer_.device_changed_count()); // Change a service audio_state->name = kTestAudioSdpName2; - adapter_observer_.ResetCounters(); + observer_.Reset(); adapter_win_->DevicesPolled(devices); - EXPECT_EQ(0, adapter_observer_.num_device_added()); - EXPECT_EQ(0, adapter_observer_.num_device_removed()); - EXPECT_EQ(1, adapter_observer_.num_device_changed()); + EXPECT_EQ(0, observer_.device_added_count()); + EXPECT_EQ(0, observer_.device_removed_count()); + EXPECT_EQ(1, observer_.device_changed_count()); // Remove a service laptop_state->service_record_states.clear(); - adapter_observer_.ResetCounters(); + observer_.Reset(); adapter_win_->DevicesPolled(devices); - EXPECT_EQ(0, adapter_observer_.num_device_added()); - EXPECT_EQ(0, adapter_observer_.num_device_removed()); - EXPECT_EQ(1, adapter_observer_.num_device_changed()); + EXPECT_EQ(0, observer_.device_added_count()); + EXPECT_EQ(0, observer_.device_removed_count()); + EXPECT_EQ(1, observer_.device_changed_count()); } } // namespace device diff --git a/device/bluetooth/bluetooth_chromeos_unittest.cc b/device/bluetooth/bluetooth_chromeos_unittest.cc index 1a71c5b..a4bcb8a 100644 --- a/device/bluetooth/bluetooth_chromeos_unittest.cc +++ b/device/bluetooth/bluetooth_chromeos_unittest.cc @@ -20,6 +20,7 @@ #include "device/bluetooth/bluetooth_device_chromeos.h" #include "device/bluetooth/bluetooth_discovery_session.h" #include "device/bluetooth/bluetooth_pairing_chromeos.h" +#include "device/bluetooth/test/test_bluetooth_adapter_observer.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/cros_system_api/dbus/service_constants.h" @@ -30,118 +31,12 @@ using device::BluetoothDevice; using device::BluetoothDiscoveryFilter; using device::BluetoothDiscoverySession; using device::BluetoothUUID; +using device::TestBluetoothAdapterObserver; namespace chromeos { namespace { -class TestObserver : public BluetoothAdapter::Observer { - public: - TestObserver(scoped_refptr<BluetoothAdapter> adapter) - : present_changed_count_(0), - powered_changed_count_(0), - discoverable_changed_count_(0), - discovering_changed_count_(0), - last_present_(false), - last_powered_(false), - last_discovering_(false), - device_added_count_(0), - device_changed_count_(0), - device_removed_count_(0), - last_device_(NULL), - adapter_(adapter) { - adapter_->AddObserver(this); - } - - ~TestObserver() override { adapter_->RemoveObserver(this); } - - void AdapterPresentChanged(BluetoothAdapter* adapter, bool present) override { - EXPECT_EQ(adapter_.get(), adapter); - - ++present_changed_count_; - last_present_ = present; - } - - void AdapterPoweredChanged(BluetoothAdapter* adapter, bool powered) override { - EXPECT_EQ(adapter_.get(), adapter); - - ++powered_changed_count_; - last_powered_ = powered; - } - - void AdapterDiscoverableChanged(BluetoothAdapter* adapter, - bool discoverable) override { - EXPECT_EQ(adapter_.get(), adapter); - - ++discoverable_changed_count_; - } - - void AdapterDiscoveringChanged(BluetoothAdapter* adapter, - bool discovering) override { - EXPECT_EQ(adapter_.get(), adapter); - - ++discovering_changed_count_; - last_discovering_ = discovering; - } - - void DeviceAdded(BluetoothAdapter* adapter, - BluetoothDevice* device) override { - EXPECT_EQ(adapter_.get(), adapter); - - ++device_added_count_; - last_device_ = device; - last_device_address_ = device->GetAddress(); - - QuitMessageLoop(); - } - - void DeviceChanged(BluetoothAdapter* adapter, - BluetoothDevice* device) override { - EXPECT_EQ(adapter_.get(), adapter); - - ++device_changed_count_; - last_device_ = device; - last_device_address_ = device->GetAddress(); - - QuitMessageLoop(); - } - - void DeviceRemoved(BluetoothAdapter* adapter, - BluetoothDevice* device) override { - EXPECT_EQ(adapter_.get(), adapter); - - ++device_removed_count_; - // Can't save device, it may be freed - last_device_address_ = device->GetAddress(); - - QuitMessageLoop(); - } - - int present_changed_count_; - int powered_changed_count_; - int discoverable_changed_count_; - int discovering_changed_count_; - bool last_present_; - bool last_powered_; - bool last_discovering_; - int device_added_count_; - int device_changed_count_; - int device_removed_count_; - BluetoothDevice* last_device_; - std::string last_device_address_; - - private: - // Some tests use a message loop since background processing is simulated; - // break out of those loops. - void QuitMessageLoop() { - if (base::MessageLoop::current() && - base::MessageLoop::current()->is_running()) - base::MessageLoop::current()->Quit(); - } - - scoped_refptr<BluetoothAdapter> adapter_; -}; - // Callback for BluetoothDevice::GetConnectionInfo() that simply saves the // connection info to the bound argument. void SaveConnectionInfo(BluetoothDevice::ConnectionInfo* out, @@ -380,7 +275,7 @@ class BluetoothChromeOSTest : public testing::Test { ASSERT_TRUE(base::MessageLoop::current() != NULL); fake_bluetooth_device_client_->SetSimulationIntervalMs(10); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); adapter_->StartDiscoverySession( @@ -397,8 +292,8 @@ class BluetoothChromeOSTest : public testing::Test { ASSERT_TRUE(adapter_->IsPowered()); ASSERT_TRUE(adapter_->IsDiscovering()); - while (!observer.device_removed_count_ && - observer.last_device_address_ != address) + while (!observer.device_removed_count() && + observer.last_device_address() != address) base::MessageLoop::current()->Run(); discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); @@ -467,23 +362,23 @@ TEST_F(BluetoothChromeOSTest, BecomePresent) { // Install an observer; expect the AdapterPresentChanged to be called // with true, and IsPresent() to return true. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_adapter_client_->SetVisible(true); - EXPECT_EQ(1, observer.present_changed_count_); - EXPECT_TRUE(observer.last_present_); + EXPECT_EQ(1, observer.present_changed_count()); + EXPECT_TRUE(observer.last_present()); EXPECT_TRUE(adapter_->IsPresent()); // We should have had a device announced. - EXPECT_EQ(2, observer.device_added_count_); + EXPECT_EQ(2, observer.device_added_count()); EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress, - observer.last_device_address_); + observer.last_device_address()); // Other callbacks shouldn't be called if the values are false. - EXPECT_EQ(0, observer.powered_changed_count_); - EXPECT_EQ(0, observer.discovering_changed_count_); + EXPECT_EQ(0, observer.powered_changed_count()); + EXPECT_EQ(0, observer.discovering_changed_count()); EXPECT_FALSE(adapter_->IsPowered()); EXPECT_FALSE(adapter_->IsDiscovering()); } @@ -494,23 +389,23 @@ TEST_F(BluetoothChromeOSTest, BecomeNotPresent) { // Install an observer; expect the AdapterPresentChanged to be called // with false, and IsPresent() to return false. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_adapter_client_->SetVisible(false); - EXPECT_EQ(1, observer.present_changed_count_); - EXPECT_FALSE(observer.last_present_); + EXPECT_EQ(1, observer.present_changed_count()); + EXPECT_FALSE(observer.last_present()); EXPECT_FALSE(adapter_->IsPresent()); // We should have had a device removed. - EXPECT_EQ(2, observer.device_removed_count_); + EXPECT_EQ(2, observer.device_removed_count()); EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress, - observer.last_device_address_); + observer.last_device_address()); // Other callbacks shouldn't be called since the values are false. - EXPECT_EQ(0, observer.powered_changed_count_); - EXPECT_EQ(0, observer.discovering_changed_count_); + EXPECT_EQ(0, observer.powered_changed_count()); + EXPECT_EQ(0, observer.discovering_changed_count()); EXPECT_FALSE(adapter_->IsPowered()); EXPECT_FALSE(adapter_->IsDiscovering()); } @@ -521,11 +416,11 @@ TEST_F(BluetoothChromeOSTest, SecondAdapter) { // Install an observer, then add a second adapter. Nothing should change, // we ignore the second adapter. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_adapter_client_->SetSecondVisible(true); - EXPECT_EQ(0, observer.present_changed_count_); + EXPECT_EQ(0, observer.present_changed_count()); EXPECT_TRUE(adapter_->IsPresent()); EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress, @@ -535,30 +430,30 @@ TEST_F(BluetoothChromeOSTest, SecondAdapter) { // is no longer present rather than fall back to the second. fake_bluetooth_adapter_client_->SetVisible(false); - EXPECT_EQ(1, observer.present_changed_count_); - EXPECT_FALSE(observer.last_present_); + EXPECT_EQ(1, observer.present_changed_count()); + EXPECT_FALSE(observer.last_present()); EXPECT_FALSE(adapter_->IsPresent()); // We should have had a device removed. - EXPECT_EQ(2, observer.device_removed_count_); + EXPECT_EQ(2, observer.device_removed_count()); EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress, - observer.last_device_address_); + observer.last_device_address()); // Other callbacks shouldn't be called since the values are false. - EXPECT_EQ(0, observer.powered_changed_count_); - EXPECT_EQ(0, observer.discovering_changed_count_); + EXPECT_EQ(0, observer.powered_changed_count()); + EXPECT_EQ(0, observer.discovering_changed_count()); EXPECT_FALSE(adapter_->IsPowered()); EXPECT_FALSE(adapter_->IsDiscovering()); - observer.device_removed_count_ = 0; + observer.Reset(); // Removing the second adapter shouldn't set anything either. fake_bluetooth_adapter_client_->SetSecondVisible(false); - EXPECT_EQ(0, observer.device_removed_count_); - EXPECT_EQ(0, observer.powered_changed_count_); - EXPECT_EQ(0, observer.discovering_changed_count_); + EXPECT_EQ(0, observer.device_removed_count()); + EXPECT_EQ(0, observer.powered_changed_count()); + EXPECT_EQ(0, observer.discovering_changed_count()); } TEST_F(BluetoothChromeOSTest, BecomePowered) { @@ -567,14 +462,14 @@ TEST_F(BluetoothChromeOSTest, BecomePowered) { // Install an observer; expect the AdapterPoweredChanged to be called // with true, and IsPowered() to return true. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); EXPECT_EQ(1, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.powered_changed_count_); - EXPECT_TRUE(observer.last_powered_); + EXPECT_EQ(1, observer.powered_changed_count()); + EXPECT_TRUE(observer.last_powered()); EXPECT_TRUE(adapter_->IsPowered()); } @@ -590,14 +485,14 @@ TEST_F(BluetoothChromeOSTest, BecomeNotPowered) { // Install an observer; expect the AdapterPoweredChanged to be called // with false, and IsPowered() to return false. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); adapter_->SetPowered(false, GetCallback(), GetErrorCallback()); EXPECT_EQ(1, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.powered_changed_count_); - EXPECT_FALSE(observer.last_powered_); + EXPECT_EQ(1, observer.powered_changed_count()); + EXPECT_FALSE(observer.last_powered()); EXPECT_FALSE(adapter_->IsPowered()); } @@ -608,12 +503,12 @@ TEST_F(BluetoothChromeOSTest, SetPoweredWhenNotPresent) { // Install an observer; expect the AdapterPresentChanged to be called // with false, and IsPresent() to return false. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_adapter_client_->SetVisible(false); - EXPECT_EQ(1, observer.present_changed_count_); - EXPECT_FALSE(observer.last_present_); + EXPECT_EQ(1, observer.present_changed_count()); + EXPECT_FALSE(observer.last_present()); EXPECT_FALSE(adapter_->IsPresent()); EXPECT_FALSE(adapter_->IsPowered()); @@ -622,8 +517,8 @@ TEST_F(BluetoothChromeOSTest, SetPoweredWhenNotPresent) { EXPECT_EQ(0, callback_count_); EXPECT_EQ(1, error_callback_count_); - EXPECT_EQ(0, observer.powered_changed_count_); - EXPECT_FALSE(observer.last_powered_); + EXPECT_EQ(0, observer.powered_changed_count()); + EXPECT_FALSE(observer.last_powered()); EXPECT_FALSE(adapter_->IsPowered()); } @@ -646,12 +541,12 @@ TEST_F(BluetoothChromeOSTest, ChangeAdapterNameWhenNotPresent) { // Install an observer; expect the AdapterPresentChanged to be called // with false, and IsPresent() to return false. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_adapter_client_->SetVisible(false); - EXPECT_EQ(1, observer.present_changed_count_); - EXPECT_FALSE(observer.last_present_); + EXPECT_EQ(1, observer.present_changed_count()); + EXPECT_FALSE(observer.last_present()); EXPECT_FALSE(adapter_->IsPresent()); EXPECT_FALSE(adapter_->IsPowered()); @@ -669,13 +564,13 @@ TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) { // Install an observer; expect the AdapterDiscoverableChanged to be called // with true, and IsDiscoverable() to return true. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback()); EXPECT_EQ(1, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.discoverable_changed_count_); + EXPECT_EQ(1, observer.discoverable_changed_count()); EXPECT_TRUE(adapter_->IsDiscoverable()); } @@ -691,13 +586,13 @@ TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) { // Install an observer; expect the AdapterDiscoverableChanged to be called // with false, and IsDiscoverable() to return false. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); adapter_->SetDiscoverable(false, GetCallback(), GetErrorCallback()); EXPECT_EQ(1, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.discoverable_changed_count_); + EXPECT_EQ(1, observer.discoverable_changed_count()); EXPECT_FALSE(adapter_->IsDiscoverable()); } @@ -709,12 +604,12 @@ TEST_F(BluetoothChromeOSTest, SetDiscoverableWhenNotPresent) { // Install an observer; expect the AdapterDiscoverableChanged to be called // with true, and IsDiscoverable() to return true. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_adapter_client_->SetVisible(false); - EXPECT_EQ(1, observer.present_changed_count_); - EXPECT_FALSE(observer.last_present_); + EXPECT_EQ(1, observer.present_changed_count()); + EXPECT_FALSE(observer.last_present()); EXPECT_FALSE(adapter_->IsPresent()); EXPECT_FALSE(adapter_->IsDiscoverable()); @@ -723,7 +618,7 @@ TEST_F(BluetoothChromeOSTest, SetDiscoverableWhenNotPresent) { EXPECT_EQ(0, callback_count_); EXPECT_EQ(1, error_callback_count_); - EXPECT_EQ(0, observer.discoverable_changed_count_); + EXPECT_EQ(0, observer.discoverable_changed_count()); EXPECT_FALSE(adapter_->IsDiscoverable()); } @@ -749,15 +644,15 @@ TEST_F(BluetoothChromeOSTest, StopDiscovery) { // Install an observer; aside from the callback, expect the // AdapterDiscoveringChanged method to be called and no longer to be // discovering, - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); message_loop_.Run(); EXPECT_EQ(1, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_EQ(1, observer.discovering_changed_count()); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); } @@ -767,7 +662,7 @@ TEST_F(BluetoothChromeOSTest, Discovery) { fake_bluetooth_device_client_->SetSimulationIntervalMs(10); GetAdapter(); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); adapter_->StartDiscoverySession( @@ -787,21 +682,21 @@ TEST_F(BluetoothChromeOSTest, Discovery) { // First two devices to appear. message_loop_.Run(); - EXPECT_EQ(2, observer.device_added_count_); + EXPECT_EQ(2, observer.device_added_count()); EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress, - observer.last_device_address_); + observer.last_device_address()); // Next we should get another two devices... message_loop_.Run(); - EXPECT_EQ(4, observer.device_added_count_); + EXPECT_EQ(4, observer.device_added_count()); // Okay, let's run forward until a device is actually removed... - while (!observer.device_removed_count_) + while (!observer.device_removed_count()) message_loop_.Run(); - EXPECT_EQ(1, observer.device_removed_count_); + EXPECT_EQ(1, observer.device_removed_count()); EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress, - observer.last_device_address_); + observer.last_device_address()); } TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) { @@ -833,40 +728,38 @@ TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) { // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called // with true, and IsPresent(), IsPowered() and IsDiscovering() to all // return true. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_adapter_client_->SetVisible(true); - EXPECT_EQ(1, observer.present_changed_count_); - EXPECT_TRUE(observer.last_present_); + EXPECT_EQ(1, observer.present_changed_count()); + EXPECT_TRUE(observer.last_present()); EXPECT_TRUE(adapter_->IsPresent()); - EXPECT_EQ(1, observer.powered_changed_count_); - EXPECT_TRUE(observer.last_powered_); + EXPECT_EQ(1, observer.powered_changed_count()); + EXPECT_TRUE(observer.last_powered()); EXPECT_TRUE(adapter_->IsPowered()); - EXPECT_EQ(1, observer.discovering_changed_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_EQ(1, observer.discovering_changed_count()); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); - observer.present_changed_count_ = 0; - observer.powered_changed_count_ = 0; - observer.discovering_changed_count_ = 0; + observer.Reset(); // Now mark the adapter not present again. Expect the methods to be called // again, to reset the properties back to false fake_bluetooth_adapter_client_->SetVisible(false); - EXPECT_EQ(1, observer.present_changed_count_); - EXPECT_FALSE(observer.last_present_); + EXPECT_EQ(1, observer.present_changed_count()); + EXPECT_FALSE(observer.last_present()); EXPECT_FALSE(adapter_->IsPresent()); - EXPECT_EQ(1, observer.powered_changed_count_); - EXPECT_FALSE(observer.last_powered_); + EXPECT_EQ(1, observer.powered_changed_count()); + EXPECT_FALSE(observer.last_powered()); EXPECT_FALSE(adapter_->IsPowered()); - EXPECT_EQ(1, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_EQ(1, observer.discovering_changed_count()); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); } @@ -880,10 +773,10 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { EXPECT_TRUE(adapter_->IsPowered()); callback_count_ = 0; - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); - EXPECT_EQ(0, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_EQ(0, observer.discovering_changed_count()); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); // Request device discovery 3 times. @@ -899,10 +792,10 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { // The observer should have received the discovering changed event exactly // once, the success callback should have been called 3 times and the adapter // should be discovering. - EXPECT_EQ(1, observer.discovering_changed_count_); + EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(3, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)3, discovery_sessions_.size()); @@ -914,10 +807,10 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { // The observer should have received no additional discovering changed events, // the success callback should have been called 2 times and the adapter should // still be discovering. - EXPECT_EQ(1, observer.discovering_changed_count_); + EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(5, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); EXPECT_TRUE(adapter_->IsDiscovering()); EXPECT_FALSE(discovery_sessions_[0]->IsActive()); @@ -935,10 +828,10 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { // The observer should have received no additional discovering changed events, // the success callback should have been called 3 times and the adapter should // still be discovering. - EXPECT_EQ(1, observer.discovering_changed_count_); + EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(8, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)6, discovery_sessions_.size()); @@ -952,10 +845,10 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { // The observer should have received the discovering changed event exactly // once, the success callback should have been called 4 times and the adapter // should no longer be discovering. - EXPECT_EQ(2, observer.discovering_changed_count_); + EXPECT_EQ(2, observer.discovering_changed_count()); EXPECT_EQ(12, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); // All discovery sessions should be inactive. @@ -966,10 +859,10 @@ TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); // The call should have failed. - EXPECT_EQ(2, observer.discovering_changed_count_); + EXPECT_EQ(2, observer.discovering_changed_count()); EXPECT_EQ(12, callback_count_); EXPECT_EQ(1, error_callback_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); } @@ -985,10 +878,10 @@ TEST_F(BluetoothChromeOSTest, EXPECT_TRUE(adapter_->IsPowered()); callback_count_ = 0; - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); - EXPECT_EQ(0, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_EQ(0, observer.discovering_changed_count()); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); // Request device discovery 3 times. @@ -1004,10 +897,10 @@ TEST_F(BluetoothChromeOSTest, // The observer should have received the discovering changed event exactly // once, the success callback should have been called 3 times and the adapter // should be discovering. - EXPECT_EQ(1, observer.discovering_changed_count_); + EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(3, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)3, discovery_sessions_.size()); @@ -1032,10 +925,10 @@ TEST_F(BluetoothChromeOSTest, base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, base::Unretained(this))); message_loop_.Run(); - EXPECT_EQ(2, observer.discovering_changed_count_); + EXPECT_EQ(2, observer.discovering_changed_count()); EXPECT_EQ(4, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); // All discovery session instances should have been updated. @@ -1052,10 +945,10 @@ TEST_F(BluetoothChromeOSTest, } // Run only once, as there should have been one D-Bus call. message_loop_.Run(); - EXPECT_EQ(3, observer.discovering_changed_count_); + EXPECT_EQ(3, observer.discovering_changed_count()); EXPECT_EQ(6, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)2, discovery_sessions_.size()); @@ -1070,10 +963,10 @@ TEST_F(BluetoothChromeOSTest, // remain 0 as no new request was made through the BluetoothAdapter. fake_bluetooth_adapter_client_->SetVisible(false); ASSERT_FALSE(adapter_->IsPresent()); - EXPECT_EQ(4, observer.discovering_changed_count_); + EXPECT_EQ(4, observer.discovering_changed_count()); EXPECT_EQ(6, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); for (int i = 0; i < 2; i++) @@ -1082,10 +975,10 @@ TEST_F(BluetoothChromeOSTest, fake_bluetooth_adapter_client_->SetVisible(true); ASSERT_TRUE(adapter_->IsPresent()); - EXPECT_EQ(5, observer.discovering_changed_count_); + EXPECT_EQ(5, observer.discovering_changed_count()); EXPECT_EQ(6, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); // Start and stop discovery. At this point, FakeBluetoothAdapterClient has @@ -1097,20 +990,20 @@ TEST_F(BluetoothChromeOSTest, base::Unretained(this)), GetErrorCallback()); message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. - EXPECT_EQ(5, observer.discovering_changed_count_); + EXPECT_EQ(5, observer.discovering_changed_count()); EXPECT_EQ(7, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)1, discovery_sessions_.size()); EXPECT_TRUE(discovery_sessions_[0]->IsActive()); discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. - EXPECT_EQ(5, observer.discovering_changed_count_); + EXPECT_EQ(5, observer.discovering_changed_count()); EXPECT_EQ(8, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); EXPECT_FALSE(discovery_sessions_[0]->IsActive()); discovery_sessions_.clear(); @@ -1121,10 +1014,10 @@ TEST_F(BluetoothChromeOSTest, base::Unretained(this)), GetErrorCallback()); message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. - EXPECT_EQ(5, observer.discovering_changed_count_); + EXPECT_EQ(5, observer.discovering_changed_count()); EXPECT_EQ(9, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)1, discovery_sessions_.size()); EXPECT_TRUE(discovery_sessions_[0]->IsActive()); @@ -1137,20 +1030,20 @@ TEST_F(BluetoothChromeOSTest, base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, base::Unretained(this))); message_loop_.Run(); - EXPECT_EQ(5, observer.discovering_changed_count_); + EXPECT_EQ(5, observer.discovering_changed_count()); EXPECT_EQ(10, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); // Now end the discovery session. This should change the adapter's discovery // state. discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); message_loop_.Run(); - EXPECT_EQ(6, observer.discovering_changed_count_); + EXPECT_EQ(6, observer.discovering_changed_count()); EXPECT_EQ(11, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_FALSE(discovery_sessions_[0]->IsActive()); } @@ -1163,10 +1056,10 @@ TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) { EXPECT_TRUE(adapter_->IsPowered()); callback_count_ = 0; - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); - EXPECT_EQ(0, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_EQ(0, observer.discovering_changed_count()); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); // Request device discovery 3 times. @@ -1182,10 +1075,10 @@ TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) { // The observer should have received the discovering changed event exactly // once, the success callback should have been called 3 times and the adapter // should be discovering. - EXPECT_EQ(1, observer.discovering_changed_count_); + EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(3, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)3, discovery_sessions_.size()); @@ -1215,10 +1108,10 @@ TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) { base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, base::Unretained(this))); message_loop_.Run(); - EXPECT_EQ(2, observer.discovering_changed_count_); + EXPECT_EQ(2, observer.discovering_changed_count()); EXPECT_EQ(4, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_FALSE(discovery_sessions_[0]->IsActive()); } @@ -1232,10 +1125,10 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { EXPECT_TRUE(adapter_->IsPowered()); callback_count_ = 0; - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); - EXPECT_EQ(0, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_EQ(0, observer.discovering_changed_count()); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); // Request to start discovery. The call should be pending. @@ -1250,8 +1143,8 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { // The underlying adapter has started discovery, but our call hasn't returned // yet. - EXPECT_EQ(1, observer.discovering_changed_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_EQ(1, observer.discovering_changed_count()); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); EXPECT_TRUE(discovery_sessions_.empty()); @@ -1265,8 +1158,8 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { } EXPECT_EQ(0, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.discovering_changed_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_EQ(1, observer.discovering_changed_count()); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); EXPECT_TRUE(discovery_sessions_.empty()); @@ -1275,8 +1168,8 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { message_loop_.Run(); EXPECT_EQ(3, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.discovering_changed_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_EQ(1, observer.discovering_changed_count()); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)3, discovery_sessions_.size()); @@ -1287,8 +1180,8 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { } EXPECT_EQ(5, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(2, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_EQ(2, observer.discovering_changed_count()); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_FALSE(discovery_sessions_[0]->IsActive()); EXPECT_FALSE(discovery_sessions_[1]->IsActive()); @@ -1298,8 +1191,8 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { discovery_sessions_[2]->Stop(GetCallback(), GetErrorCallback()); EXPECT_EQ(5, callback_count_); EXPECT_EQ(1, error_callback_count_); - EXPECT_EQ(2, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_EQ(2, observer.discovering_changed_count()); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_TRUE(discovery_sessions_[2]->IsActive()); @@ -1310,8 +1203,8 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { GetErrorCallback()); EXPECT_EQ(5, callback_count_); EXPECT_EQ(1, error_callback_count_); - EXPECT_EQ(2, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_EQ(2, observer.discovering_changed_count()); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)3, discovery_sessions_.size()); @@ -1319,8 +1212,8 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { message_loop_.Run(); EXPECT_EQ(6, callback_count_); EXPECT_EQ(1, error_callback_count_); - EXPECT_EQ(3, observer.discovering_changed_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_EQ(3, observer.discovering_changed_count()); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)3, discovery_sessions_.size()); EXPECT_FALSE(discovery_sessions_[2]->IsActive()); @@ -1330,8 +1223,8 @@ TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { message_loop_.Run(); EXPECT_EQ(7, callback_count_); EXPECT_EQ(1, error_callback_count_); - EXPECT_EQ(3, observer.discovering_changed_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_EQ(3, observer.discovering_changed_count()); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)4, discovery_sessions_.size()); EXPECT_TRUE(discovery_sessions_[3]->IsActive()); @@ -1346,10 +1239,10 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { EXPECT_TRUE(adapter_->IsPowered()); callback_count_ = 0; - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); - EXPECT_EQ(0, observer.discovering_changed_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_EQ(0, observer.discovering_changed_count()); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_TRUE(discovery_sessions_.empty()); @@ -1359,10 +1252,10 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { base::Unretained(this)), GetErrorCallback()); message_loop_.Run(); - EXPECT_EQ(1, observer.discovering_changed_count_); + EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(1, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)1, discovery_sessions_.size()); EXPECT_TRUE(discovery_sessions_[0]->IsActive()); @@ -1375,10 +1268,10 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { base::Unretained(this)), GetErrorCallback()); message_loop_.Run(); - EXPECT_EQ(1, observer.discovering_changed_count_); + EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(2, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)2, discovery_sessions_.size()); EXPECT_TRUE(discovery_sessions_[0]->IsActive()); @@ -1389,10 +1282,10 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { base::Unretained(this)), GetErrorCallback()); message_loop_.Run(); - EXPECT_EQ(1, observer.discovering_changed_count_); + EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(3, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)3, discovery_sessions_.size()); EXPECT_TRUE(discovery_sessions_[1]->IsActive()); @@ -1402,10 +1295,10 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { // should continue. discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); message_loop_.Run(); - EXPECT_EQ(1, observer.discovering_changed_count_); + EXPECT_EQ(1, observer.discovering_changed_count()); EXPECT_EQ(4, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_TRUE(observer.last_discovering_); + EXPECT_TRUE(observer.last_discovering()); EXPECT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)3, discovery_sessions_.size()); EXPECT_FALSE(discovery_sessions_[0]->IsActive()); @@ -1413,13 +1306,13 @@ TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { // Delete the current active session. Discovery should eventually stop. discovery_sessions_.clear(); - while (observer.last_discovering_) + while (observer.last_discovering()) message_loop_.RunUntilIdle(); - EXPECT_EQ(2, observer.discovering_changed_count_); + EXPECT_EQ(2, observer.discovering_changed_count()); EXPECT_EQ(4, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_FALSE(observer.last_discovering_); + EXPECT_FALSE(observer.last_discovering()); EXPECT_FALSE(adapter_->IsDiscovering()); } @@ -1428,7 +1321,7 @@ TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscovery) { fake_bluetooth_device_client_->SetSimulationIntervalMs(10); GetAdapter(); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); @@ -1491,7 +1384,7 @@ TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryFail) { fake_bluetooth_device_client_->SetSimulationIntervalMs(10); GetAdapter(); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); @@ -1536,7 +1429,7 @@ TEST_F(BluetoothChromeOSTest, QueuedSetDiscoveryFilterBeforeStartDiscovery) { fake_bluetooth_device_client_->SetSimulationIntervalMs(10); GetAdapter(); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); @@ -1637,7 +1530,7 @@ TEST_F(BluetoothChromeOSTest, fake_bluetooth_device_client_->SetSimulationIntervalMs(10); GetAdapter(); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); @@ -1734,7 +1627,7 @@ TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterAfterStartDiscovery) { fake_bluetooth_device_client_->SetSimulationIntervalMs(10); GetAdapter(); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), @@ -1754,8 +1647,8 @@ TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterAfterStartDiscovery) { ASSERT_TRUE(adapter_->IsDiscovering()); ASSERT_EQ((size_t)1, discovery_sessions_.size()); ASSERT_TRUE(discovery_sessions_[0]->IsActive()); - EXPECT_EQ(1, observer.discovering_changed_count_); - observer.discovering_changed_count_ = 0; + EXPECT_EQ(1, observer.discovering_changed_count()); + observer.Reset(); auto nullInstance = scoped_ptr<BluetoothDiscoveryFilter>(); nullInstance.reset(); @@ -1825,7 +1718,7 @@ TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryMultiple) { EXPECT_TRUE(adapter_->IsPowered()); callback_count_ = 0; - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Request device discovery with pre-set filter 3 times. for (int i = 0; i < 3; i++) { @@ -1862,8 +1755,8 @@ TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryMultiple) { message_loop_.Run(); if (i == 0) { - EXPECT_EQ(1, observer.discovering_changed_count_); - observer.discovering_changed_count_ = 0; + EXPECT_EQ(1, observer.discovering_changed_count()); + observer.Reset(); auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); EXPECT_EQ("le", *filter->transport); @@ -2038,8 +1931,7 @@ TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryMultiple) { EXPECT_EQ(4, callback_count_); EXPECT_EQ(0, error_callback_count_); EXPECT_FALSE(adapter_->IsDiscovering()); - EXPECT_EQ(1, observer.discovering_changed_count_); - observer.discovering_changed_count_ = 0; + EXPECT_EQ(1, observer.discovering_changed_count()); // All discovery sessions should be inactive. for (int i = 0; i < 6; i++) @@ -2201,7 +2093,7 @@ TEST_F(BluetoothChromeOSTest, DeviceClassChanged) { // Install an observer; expect the DeviceChanged method to be called when // we change the class of the device. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); FakeBluetoothDeviceClient::Properties* properties = fake_bluetooth_device_client_->GetProperties( @@ -2209,8 +2101,8 @@ TEST_F(BluetoothChromeOSTest, DeviceClassChanged) { properties->bluetooth_class.ReplaceValue(0x002580); - EXPECT_EQ(1, observer.device_changed_count_); - EXPECT_EQ(devices[0], observer.last_device_); + EXPECT_EQ(1, observer.device_changed_count()); + EXPECT_EQ(devices[0], observer.last_device()); EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType()); } @@ -2228,7 +2120,7 @@ TEST_F(BluetoothChromeOSTest, DeviceNameChanged) { // Install an observer; expect the DeviceChanged method to be called when // we change the alias of the device. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); FakeBluetoothDeviceClient::Properties* properties = fake_bluetooth_device_client_->GetProperties( @@ -2237,8 +2129,8 @@ TEST_F(BluetoothChromeOSTest, DeviceNameChanged) { static const std::string new_name("New Device Name"); properties->alias.ReplaceValue(new_name); - EXPECT_EQ(1, observer.device_changed_count_); - EXPECT_EQ(devices[0], observer.last_device_); + EXPECT_EQ(1, observer.device_changed_count()); + EXPECT_EQ(devices[0], observer.last_device()); EXPECT_EQ(base::UTF8ToUTF16(new_name), devices[0]->GetName()); } @@ -2259,7 +2151,7 @@ TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) { // Install an observer; expect the DeviceChanged method to be called when // we change the class of the device. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); FakeBluetoothDeviceClient::Properties* properties = fake_bluetooth_device_client_->GetProperties( @@ -2274,8 +2166,8 @@ TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) { properties->uuids.ReplaceValue(new_uuids); - EXPECT_EQ(1, observer.device_changed_count_); - EXPECT_EQ(devices[0], observer.last_device_); + EXPECT_EQ(1, observer.device_changed_count()); + EXPECT_EQ(devices[0], observer.last_device()); // Fetching the value should give the new one. uuids = devices[0]->GetUUIDs(); @@ -2299,13 +2191,13 @@ TEST_F(BluetoothChromeOSTest, ForgetDevice) { // Install an observer; expect the DeviceRemoved method to be called // with the device we remove. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); devices[0]->Forget(GetErrorCallback()); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.device_removed_count_); - EXPECT_EQ(address, observer.last_device_address_); + EXPECT_EQ(1, observer.device_removed_count()); + EXPECT_EQ(address, observer.last_device_address()); // GetDevices shouldn't return the device either. devices = adapter_->GetDevices(); @@ -2341,14 +2233,14 @@ TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) { // Install an observer; expect the DeviceRemoved method to be called // with the device we remove. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); device->Forget(GetErrorCallback()); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.device_removed_count_); + EXPECT_EQ(1, observer.device_removed_count()); EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress, - observer.last_device_address_); + observer.last_device_address()); // GetDevices shouldn't return the device either. device = adapter_->GetDevice( @@ -2364,7 +2256,7 @@ TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) { ASSERT_TRUE(device != NULL); ASSERT_TRUE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Connect without a pairing delegate; since the device is already Paired // this should succeed and the device should become connected. @@ -2377,8 +2269,8 @@ TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) { // Two changes for connecting, one for connected and one for for trusted // after connecting. - EXPECT_EQ(4, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(4, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2393,7 +2285,7 @@ TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Connect without a pairing delegate; since the device does not require // pairing, this should succeed and the device should become connected. @@ -2406,8 +2298,8 @@ TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) { // Two changes for connecting, one for connected, one for for trusted after // connection, and one for the reconnect mode (IsConnectable). - EXPECT_EQ(5, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(5, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2445,7 +2337,7 @@ TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) { // Connect again; since the device is already Connected, this shouldn't do // anything to initiate the connection. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); device->Connect(NULL, GetCallback(), base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, @@ -2456,7 +2348,7 @@ TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) { // The observer will be called because Connecting will toggle true and false, // and the trusted property will be updated to true. - EXPECT_EQ(3, observer.device_changed_count_); + EXPECT_EQ(3, observer.device_changed_count()); EXPECT_TRUE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2471,7 +2363,7 @@ TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Connect without a pairing delegate; since the device requires pairing, // this should fail with an error. @@ -2483,7 +2375,7 @@ TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) { EXPECT_EQ(1, error_callback_count_); EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_); - EXPECT_EQ(2, observer.device_changed_count_); + EXPECT_EQ(2, observer.device_changed_count()); EXPECT_FALSE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2510,15 +2402,15 @@ TEST_F(BluetoothChromeOSTest, DisconnectDevice) { // Disconnect the device, we should see the observer method fire and the // device get dropped. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); device->Disconnect(GetCallback(), GetErrorCallback()); EXPECT_EQ(1, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(1, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_FALSE(device->IsConnected()); } @@ -2534,14 +2426,14 @@ TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) { // Disconnect the device, we should see the observer method fire and the // device get dropped. - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); device->Disconnect(GetCallback(), GetErrorCallback()); EXPECT_EQ(0, callback_count_); EXPECT_EQ(1, error_callback_count_); - EXPECT_EQ(0, observer.device_changed_count_); + EXPECT_EQ(0, observer.device_changed_count()); EXPECT_FALSE(device->IsConnected()); } @@ -2559,7 +2451,7 @@ TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -2577,8 +2469,8 @@ TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) { // Two changes for connecting, one change for connected, one for paired, // two for trusted (after pairing and connection), and one for the reconnect // mode (IsConnectable). - EXPECT_EQ(7, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(7, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2610,7 +2502,7 @@ TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -2630,8 +2522,8 @@ TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) { // Two changes for connecting, one change for connected, one for paired, // two for trusted (after pairing and connection), and one for the reconnect // mode (IsConnectable). - EXPECT_EQ(7, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(7, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2664,7 +2556,7 @@ TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -2703,8 +2595,8 @@ TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) { // Two changes for connecting, one change for connected, one for paired, // two for trusted (after pairing and connection), and one for the reconnect // mode (IsConnectable). - EXPECT_EQ(7, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(7, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2738,7 +2630,7 @@ TEST_F(BluetoothChromeOSTest, PairRequestPinCode) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -2758,8 +2650,8 @@ TEST_F(BluetoothChromeOSTest, PairRequestPinCode) { // Two changes for connecting, one change for connected, one for paired and // two for trusted (after pairing and connection). - EXPECT_EQ(6, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(6, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2792,7 +2684,7 @@ TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -2813,8 +2705,8 @@ TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) { // Two changes for connecting, one change for connected, one for paired and // two for trusted (after pairing and connection). - EXPECT_EQ(6, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(6, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2844,7 +2736,7 @@ TEST_F(BluetoothChromeOSTest, PairRequestPasskey) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -2864,8 +2756,8 @@ TEST_F(BluetoothChromeOSTest, PairRequestPasskey) { // Two changes for connecting, one change for connected, one for paired and // two for trusted (after pairing and connection). - EXPECT_EQ(6, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(6, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2895,7 +2787,7 @@ TEST_F(BluetoothChromeOSTest, PairJustWorks) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -2911,8 +2803,8 @@ TEST_F(BluetoothChromeOSTest, PairJustWorks) { // Two changes for connecting, one change for connected, one for paired and // two for trusted (after pairing and connection). - EXPECT_EQ(6, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(6, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -2940,7 +2832,7 @@ TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -2975,7 +2867,7 @@ TEST_F(BluetoothChromeOSTest, PairingFails) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -3011,7 +2903,7 @@ TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -3029,8 +2921,8 @@ TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) { // Two changes for connecting, one for paired and one for trusted after // pairing. The device should not be connected. - EXPECT_EQ(4, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(4, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_FALSE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); @@ -3058,7 +2950,7 @@ TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -3078,7 +2970,7 @@ TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) { EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_); // Should be no changes except connecting going true and false. - EXPECT_EQ(2, observer.device_changed_count_); + EXPECT_EQ(2, observer.device_changed_count()); EXPECT_FALSE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); EXPECT_FALSE(device->IsPaired()); @@ -3096,7 +2988,7 @@ TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -3116,7 +3008,7 @@ TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) { EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); // Should be no changes except connecting going true and false. - EXPECT_EQ(2, observer.device_changed_count_); + EXPECT_EQ(2, observer.device_changed_count()); EXPECT_FALSE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); EXPECT_FALSE(device->IsPaired()); @@ -3134,7 +3026,7 @@ TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -3154,7 +3046,7 @@ TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) { EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_); // Should be no changes except connecting going true and false. - EXPECT_EQ(2, observer.device_changed_count_); + EXPECT_EQ(2, observer.device_changed_count()); EXPECT_FALSE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); EXPECT_FALSE(device->IsPaired()); @@ -3172,7 +3064,7 @@ TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -3192,7 +3084,7 @@ TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) { EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); // Should be no changes except connecting going true and false. - EXPECT_EQ(2, observer.device_changed_count_); + EXPECT_EQ(2, observer.device_changed_count()); EXPECT_FALSE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); EXPECT_FALSE(device->IsPaired()); @@ -3210,7 +3102,7 @@ TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -3230,7 +3122,7 @@ TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) { EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_); // Should be no changes except connecting going true and false. - EXPECT_EQ(2, observer.device_changed_count_); + EXPECT_EQ(2, observer.device_changed_count()); EXPECT_FALSE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); EXPECT_FALSE(device->IsPaired()); @@ -3248,7 +3140,7 @@ TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -3268,7 +3160,7 @@ TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) { EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); // Should be no changes except connecting going true and false. - EXPECT_EQ(2, observer.device_changed_count_); + EXPECT_EQ(2, observer.device_changed_count()); EXPECT_FALSE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); EXPECT_FALSE(device->IsPaired()); @@ -3286,7 +3178,7 @@ TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); TestPairingDelegate pairing_delegate; device->Connect(&pairing_delegate, GetCallback(), @@ -3305,7 +3197,7 @@ TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) { EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); // Should be no changes except connecting going true and false. - EXPECT_EQ(2, observer.device_changed_count_); + EXPECT_EQ(2, observer.device_changed_count()); EXPECT_FALSE(device->IsConnected()); EXPECT_FALSE(device->IsConnecting()); EXPECT_FALSE(device->IsPaired()); @@ -3330,7 +3222,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_device_client_->SimulatePairing( dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath), true, @@ -3348,8 +3240,8 @@ TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) { EXPECT_EQ(0, error_callback_count_); // One change for paired, and one for trusted. - EXPECT_EQ(2, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(2, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsPaired()); @@ -3384,7 +3276,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_device_client_->SimulatePairing( dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath), true, @@ -3403,8 +3295,8 @@ TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) { EXPECT_EQ(0, error_callback_count_); // One change for paired, and one for trusted. - EXPECT_EQ(2, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(2, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsPaired()); @@ -3439,7 +3331,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_device_client_->SimulatePairing( dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true, @@ -3457,8 +3349,8 @@ TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) { EXPECT_EQ(0, error_callback_count_); // One change for paired, and one for trusted. - EXPECT_EQ(2, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(2, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsPaired()); @@ -3494,7 +3386,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_device_client_->SimulatePairing( dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath), true, @@ -3512,8 +3404,8 @@ TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) { EXPECT_EQ(0, error_callback_count_); // One change for paired, and one for trusted. - EXPECT_EQ(2, observer.device_changed_count_); - EXPECT_EQ(device, observer.last_device_); + EXPECT_EQ(2, observer.device_changed_count()); + EXPECT_EQ(device, observer.last_device()); EXPECT_TRUE(device->IsPaired()); @@ -3544,7 +3436,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_device_client_->SimulatePairing( dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath), true, @@ -3558,7 +3450,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) { EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_); // No changes should be observer. - EXPECT_EQ(0, observer.device_changed_count_); + EXPECT_EQ(0, observer.device_changed_count()); EXPECT_FALSE(device->IsPaired()); @@ -3583,7 +3475,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_device_client_->SimulatePairing( dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath), true, @@ -3597,7 +3489,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) { EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_); // No changes should be observer. - EXPECT_EQ(0, observer.device_changed_count_); + EXPECT_EQ(0, observer.device_changed_count()); EXPECT_FALSE(device->IsPaired()); @@ -3622,7 +3514,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_device_client_->SimulatePairing( dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true, @@ -3636,7 +3528,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) { EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_); // No changes should be observer. - EXPECT_EQ(0, observer.device_changed_count_); + EXPECT_EQ(0, observer.device_changed_count()); EXPECT_FALSE(device->IsPaired()); @@ -3661,7 +3553,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_device_client_->SimulatePairing( dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath), true, @@ -3675,7 +3567,7 @@ TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) { EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_); // No changes should be observer. - EXPECT_EQ(0, observer.device_changed_count_); + EXPECT_EQ(0, observer.device_changed_count()); EXPECT_FALSE(device->IsPaired()); @@ -3704,7 +3596,7 @@ TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) { ASSERT_TRUE(device != NULL); ASSERT_FALSE(device->IsPaired()); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); fake_bluetooth_device_client_->SimulatePairing( dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true, @@ -3730,7 +3622,7 @@ TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) { EXPECT_EQ(0, callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(0, observer.device_changed_count_); + EXPECT_EQ(0, observer.device_changed_count()); EXPECT_FALSE(device->IsPaired()); } @@ -3868,8 +3760,8 @@ TEST_F(BluetoothChromeOSTest, Shutdown) { adapter_->Shutdown(); // DeleteOnCorrectThread omitted as we don't want to delete in this test. { - TestObserver observer(adapter_); // Calls AddObserver - } // TestObserver::~TestObserver calls RemoveObserver. + TestBluetoothAdapterObserver observer(adapter_); // Calls AddObserver + } // ~TestBluetoothAdapterObserver calls RemoveObserver. EXPECT_EQ("", adapter_->GetAddress()); EXPECT_EQ("", adapter_->GetName()); diff --git a/device/bluetooth/bluetooth_gatt_chromeos_unittest.cc b/device/bluetooth/bluetooth_gatt_chromeos_unittest.cc index b4549c7..3c34663 100644 --- a/device/bluetooth/bluetooth_gatt_chromeos_unittest.cc +++ b/device/bluetooth/bluetooth_gatt_chromeos_unittest.cc @@ -23,6 +23,7 @@ #include "device/bluetooth/bluetooth_gatt_notify_session.h" #include "device/bluetooth/bluetooth_gatt_service.h" #include "device/bluetooth/bluetooth_uuid.h" +#include "device/bluetooth/test/test_bluetooth_adapter_observer.h" #include "testing/gtest/include/gtest/gtest.h" using device::BluetoothAdapter; @@ -33,6 +34,7 @@ using device::BluetoothGattDescriptor; using device::BluetoothGattService; using device::BluetoothGattNotifySession; using device::BluetoothUUID; +using device::TestBluetoothAdapterObserver; namespace chromeos { @@ -57,213 +59,6 @@ bool ValuesEqual(const std::vector<uint8>& value0, return true; } -class TestObserver : public BluetoothAdapter::Observer { - public: - TestObserver(scoped_refptr<BluetoothAdapter> adapter) - : gatt_service_added_count_(0), - gatt_service_removed_count_(0), - gatt_service_changed_count_(0), - gatt_discovery_complete_count_(0), - gatt_characteristic_added_count_(0), - gatt_characteristic_removed_count_(0), - gatt_characteristic_value_changed_count_(0), - gatt_descriptor_added_count_(0), - gatt_descriptor_removed_count_(0), - gatt_descriptor_value_changed_count_(0), - adapter_(adapter) { - adapter_->AddObserver(this); - } - - ~TestObserver() override { adapter_->RemoveObserver(this); } - - // BluetoothAdapter::Observer overrides. - void GattServiceAdded(BluetoothAdapter* adapter, - BluetoothDevice* device, - BluetoothGattService* service) override { - ASSERT_EQ(adapter_.get(), adapter); - ASSERT_EQ(service->GetDevice(), device); - - ++gatt_service_added_count_; - last_gatt_service_id_ = service->GetIdentifier(); - last_gatt_service_uuid_ = service->GetUUID(); - - EXPECT_FALSE(service->IsLocal()); - EXPECT_TRUE(service->IsPrimary()); - - EXPECT_EQ(device->GetGattService(last_gatt_service_id_), service); - - QuitMessageLoop(); - } - - void GattServiceRemoved(BluetoothAdapter* adapter, - BluetoothDevice* device, - BluetoothGattService* service) override { - ASSERT_EQ(adapter_.get(), adapter); - ASSERT_EQ(service->GetDevice(), device); - - ++gatt_service_removed_count_; - last_gatt_service_id_ = service->GetIdentifier(); - last_gatt_service_uuid_ = service->GetUUID(); - - EXPECT_FALSE(service->IsLocal()); - EXPECT_TRUE(service->IsPrimary()); - - // The device should return NULL for this service. - EXPECT_FALSE(device->GetGattService(last_gatt_service_id_)); - - QuitMessageLoop(); - } - - void GattDiscoveryCompleteForService(BluetoothAdapter* adapter, - BluetoothGattService* service) override { - ASSERT_EQ(adapter_.get(), adapter); - ++gatt_discovery_complete_count_; - - QuitMessageLoop(); - } - - void GattServiceChanged(BluetoothAdapter* adapter, - BluetoothGattService* service) override { - ASSERT_EQ(adapter_.get(), adapter); - ++gatt_service_changed_count_; - - QuitMessageLoop(); - } - - void GattCharacteristicAdded( - BluetoothAdapter* adapter, - BluetoothGattCharacteristic* characteristic) override { - ASSERT_EQ(adapter_.get(), adapter); - - ++gatt_characteristic_added_count_; - last_gatt_characteristic_id_ = characteristic->GetIdentifier(); - last_gatt_characteristic_uuid_ = characteristic->GetUUID(); - - ASSERT_TRUE(characteristic->GetService()); - EXPECT_EQ(characteristic->GetService()->GetCharacteristic( - last_gatt_characteristic_id_), - characteristic); - - QuitMessageLoop(); - } - - void GattCharacteristicRemoved( - BluetoothAdapter* adapter, - BluetoothGattCharacteristic* characteristic) override { - ASSERT_EQ(adapter_.get(), adapter); - - ++gatt_characteristic_removed_count_; - last_gatt_characteristic_id_ = characteristic->GetIdentifier(); - last_gatt_characteristic_uuid_ = characteristic->GetUUID(); - - // The service should return NULL for this characteristic. - ASSERT_TRUE(characteristic->GetService()); - EXPECT_FALSE(characteristic->GetService()->GetCharacteristic( - last_gatt_characteristic_id_)); - - QuitMessageLoop(); - } - - void GattCharacteristicValueChanged( - BluetoothAdapter* adapter, - BluetoothGattCharacteristic* characteristic, - const std::vector<uint8>& value) override { - ASSERT_EQ(adapter_.get(), adapter); - - ++gatt_characteristic_value_changed_count_; - last_gatt_characteristic_id_ = characteristic->GetIdentifier(); - last_gatt_characteristic_uuid_ = characteristic->GetUUID(); - last_changed_characteristic_value_ = value; - - ASSERT_TRUE(characteristic->GetService()); - EXPECT_EQ(characteristic->GetService()->GetCharacteristic( - last_gatt_characteristic_id_), - characteristic); - - QuitMessageLoop(); - } - - void GattDescriptorAdded(BluetoothAdapter* adapter, - BluetoothGattDescriptor* descriptor) override { - ASSERT_EQ(adapter_.get(), adapter); - - ++gatt_descriptor_added_count_; - last_gatt_descriptor_id_ = descriptor->GetIdentifier(); - last_gatt_descriptor_uuid_ = descriptor->GetUUID(); - - ASSERT_TRUE(descriptor->GetCharacteristic()); - EXPECT_EQ(descriptor->GetCharacteristic()->GetDescriptor( - last_gatt_descriptor_id_), - descriptor); - - QuitMessageLoop(); - } - - void GattDescriptorRemoved(BluetoothAdapter* adapter, - BluetoothGattDescriptor* descriptor) override { - ASSERT_EQ(adapter_.get(), adapter); - - ++gatt_descriptor_removed_count_; - last_gatt_descriptor_id_ = descriptor->GetIdentifier(); - last_gatt_descriptor_uuid_ = descriptor->GetUUID(); - - // The characteristic should return NULL for this descriptor.. - ASSERT_TRUE(descriptor->GetCharacteristic()); - EXPECT_FALSE(descriptor->GetCharacteristic()->GetDescriptor( - last_gatt_descriptor_id_)); - - QuitMessageLoop(); - } - - void GattDescriptorValueChanged(BluetoothAdapter* adapter, - BluetoothGattDescriptor* descriptor, - const std::vector<uint8>& value) override { - ASSERT_EQ(adapter_.get(), adapter); - - ++gatt_descriptor_value_changed_count_; - last_gatt_descriptor_id_ = descriptor->GetIdentifier(); - last_gatt_descriptor_uuid_ = descriptor->GetUUID(); - last_changed_descriptor_value_ = value; - - ASSERT_TRUE(descriptor->GetCharacteristic()); - EXPECT_EQ(descriptor->GetCharacteristic()->GetDescriptor( - last_gatt_descriptor_id_), - descriptor); - - QuitMessageLoop(); - } - - int gatt_service_added_count_; - int gatt_service_removed_count_; - int gatt_service_changed_count_; - int gatt_discovery_complete_count_; - int gatt_characteristic_added_count_; - int gatt_characteristic_removed_count_; - int gatt_characteristic_value_changed_count_; - int gatt_descriptor_added_count_; - int gatt_descriptor_removed_count_; - int gatt_descriptor_value_changed_count_; - std::string last_gatt_service_id_; - BluetoothUUID last_gatt_service_uuid_; - std::string last_gatt_characteristic_id_; - BluetoothUUID last_gatt_characteristic_uuid_; - std::vector<uint8> last_changed_characteristic_value_; - std::string last_gatt_descriptor_id_; - BluetoothUUID last_gatt_descriptor_uuid_; - std::vector<uint8> last_changed_descriptor_value_; - - private: - // Some tests use a message loop since background processing is simulated; - // break out of those loops. - void QuitMessageLoop() { - if (base::MessageLoop::current() && - base::MessageLoop::current()->is_running()) - base::MessageLoop::current()->Quit(); - } - - scoped_refptr<BluetoothAdapter> adapter_; -}; - } // namespace class BluetoothGattChromeOSTest : public testing::Test { @@ -480,85 +275,85 @@ TEST_F(BluetoothGattChromeOSTest, GattServiceAddedAndRemoved) { FakeBluetoothDeviceClient::kLowEnergyAddress); ASSERT_TRUE(device); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); - EXPECT_EQ(0, observer.gatt_service_added_count_); - EXPECT_EQ(0, observer.gatt_service_removed_count_); - EXPECT_TRUE(observer.last_gatt_service_id_.empty()); - EXPECT_FALSE(observer.last_gatt_service_uuid_.IsValid()); + EXPECT_EQ(0, observer.gatt_service_added_count()); + EXPECT_EQ(0, observer.gatt_service_removed_count()); + EXPECT_TRUE(observer.last_gatt_service_id().empty()); + EXPECT_FALSE(observer.last_gatt_service_uuid().IsValid()); EXPECT_TRUE(device->GetGattServices().empty()); // Expose the fake Heart Rate Service. fake_bluetooth_gatt_service_client_->ExposeHeartRateService( dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); - EXPECT_EQ(1, observer.gatt_service_added_count_); - EXPECT_EQ(0, observer.gatt_service_removed_count_); - EXPECT_FALSE(observer.last_gatt_service_id_.empty()); + EXPECT_EQ(1, observer.gatt_service_added_count()); + EXPECT_EQ(0, observer.gatt_service_removed_count()); + EXPECT_FALSE(observer.last_gatt_service_id().empty()); EXPECT_EQ(1U, device->GetGattServices().size()); EXPECT_EQ( BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID), - observer.last_gatt_service_uuid_); + observer.last_gatt_service_uuid()); BluetoothGattService* service = - device->GetGattService(observer.last_gatt_service_id_); + device->GetGattService(observer.last_gatt_service_id()); EXPECT_FALSE(service->IsLocal()); EXPECT_TRUE(service->IsPrimary()); EXPECT_EQ(service, device->GetGattServices()[0]); EXPECT_EQ(service, device->GetGattService(service->GetIdentifier())); - EXPECT_EQ(observer.last_gatt_service_uuid_, service->GetUUID()); + EXPECT_EQ(observer.last_gatt_service_uuid(), service->GetUUID()); // Hide the service. - observer.last_gatt_service_uuid_ = BluetoothUUID(); - observer.last_gatt_service_id_.clear(); + observer.last_gatt_service_uuid() = BluetoothUUID(); + observer.last_gatt_service_id().clear(); fake_bluetooth_gatt_service_client_->HideHeartRateService(); - EXPECT_EQ(1, observer.gatt_service_added_count_); - EXPECT_EQ(1, observer.gatt_service_removed_count_); - EXPECT_FALSE(observer.last_gatt_service_id_.empty()); + EXPECT_EQ(1, observer.gatt_service_added_count()); + EXPECT_EQ(1, observer.gatt_service_removed_count()); + EXPECT_FALSE(observer.last_gatt_service_id().empty()); EXPECT_TRUE(device->GetGattServices().empty()); EXPECT_EQ( BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID), - observer.last_gatt_service_uuid_); + observer.last_gatt_service_uuid()); - EXPECT_EQ(NULL, device->GetGattService(observer.last_gatt_service_id_)); + EXPECT_EQ(NULL, device->GetGattService(observer.last_gatt_service_id())); // Expose the service again. - observer.last_gatt_service_uuid_ = BluetoothUUID(); - observer.last_gatt_service_id_.clear(); + observer.last_gatt_service_uuid() = BluetoothUUID(); + observer.last_gatt_service_id().clear(); fake_bluetooth_gatt_service_client_->ExposeHeartRateService( dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); - EXPECT_EQ(2, observer.gatt_service_added_count_); - EXPECT_EQ(1, observer.gatt_service_removed_count_); - EXPECT_FALSE(observer.last_gatt_service_id_.empty()); + EXPECT_EQ(2, observer.gatt_service_added_count()); + EXPECT_EQ(1, observer.gatt_service_removed_count()); + EXPECT_FALSE(observer.last_gatt_service_id().empty()); EXPECT_EQ(1U, device->GetGattServices().size()); EXPECT_EQ( BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID), - observer.last_gatt_service_uuid_); + observer.last_gatt_service_uuid()); // The object |service| points to should have been deallocated. |device| // should contain a brand new instance. - service = device->GetGattService(observer.last_gatt_service_id_); + service = device->GetGattService(observer.last_gatt_service_id()); EXPECT_EQ(service, device->GetGattServices()[0]); EXPECT_FALSE(service->IsLocal()); EXPECT_TRUE(service->IsPrimary()); - EXPECT_EQ(observer.last_gatt_service_uuid_, service->GetUUID()); + EXPECT_EQ(observer.last_gatt_service_uuid(), service->GetUUID()); // Remove the device. The observer should be notified of the removed service. // |device| becomes invalid after this. - observer.last_gatt_service_uuid_ = BluetoothUUID(); - observer.last_gatt_service_id_.clear(); + observer.last_gatt_service_uuid() = BluetoothUUID(); + observer.last_gatt_service_id().clear(); fake_bluetooth_device_client_->RemoveDevice( dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); - EXPECT_EQ(2, observer.gatt_service_added_count_); - EXPECT_EQ(2, observer.gatt_service_removed_count_); - EXPECT_FALSE(observer.last_gatt_service_id_.empty()); + EXPECT_EQ(2, observer.gatt_service_added_count()); + EXPECT_EQ(2, observer.gatt_service_removed_count()); + EXPECT_FALSE(observer.last_gatt_service_id().empty()); EXPECT_EQ( BluetoothUUID(FakeBluetoothGattServiceClient::kHeartRateServiceUUID), - observer.last_gatt_service_uuid_); + observer.last_gatt_service_uuid()); EXPECT_EQ( NULL, adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress)); } @@ -571,22 +366,22 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicAddedAndRemoved) { FakeBluetoothDeviceClient::kLowEnergyAddress); ASSERT_TRUE(device); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Expose the fake Heart Rate service. This will asynchronously expose // characteristics. fake_bluetooth_gatt_service_client_->ExposeHeartRateService( dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); - ASSERT_EQ(1, observer.gatt_service_added_count_); + ASSERT_EQ(1, observer.gatt_service_added_count()); BluetoothGattService* service = - device->GetGattService(observer.last_gatt_service_id_); + device->GetGattService(observer.last_gatt_service_id()); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(0, observer.gatt_discovery_complete_count_); - EXPECT_EQ(0, observer.gatt_characteristic_added_count_); - EXPECT_EQ(0, observer.gatt_characteristic_removed_count_); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(0, observer.gatt_discovery_complete_count()); + EXPECT_EQ(0, observer.gatt_characteristic_added_count()); + EXPECT_EQ(0, observer.gatt_characteristic_removed_count()); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(service->GetCharacteristics().empty()); // Run the message loop so that the characteristics appear. @@ -595,19 +390,19 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicAddedAndRemoved) { // 3 characteristics should appear. Only 1 of the characteristics sends // value changed signals. Service changed should be fired once for // descriptor added. - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(1, observer.gatt_discovery_complete_count_); - EXPECT_EQ(3, observer.gatt_characteristic_added_count_); - EXPECT_EQ(0, observer.gatt_characteristic_removed_count_); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(1, observer.gatt_discovery_complete_count()); + EXPECT_EQ(3, observer.gatt_characteristic_added_count()); + EXPECT_EQ(0, observer.gatt_characteristic_removed_count()); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); EXPECT_EQ(3U, service->GetCharacteristics().size()); // Hide the characteristics. 3 removed signals should be received. fake_bluetooth_gatt_characteristic_client_->HideHeartRateCharacteristics(); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(3, observer.gatt_characteristic_added_count_); - EXPECT_EQ(3, observer.gatt_characteristic_removed_count_); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(3, observer.gatt_characteristic_added_count()); + EXPECT_EQ(3, observer.gatt_characteristic_removed_count()); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(service->GetCharacteristics().empty()); // Re-expose the heart rate characteristics. We shouldn't get another @@ -616,19 +411,19 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicAddedAndRemoved) { // only if the service will also be subsequently removed. fake_bluetooth_gatt_characteristic_client_->ExposeHeartRateCharacteristics( fake_bluetooth_gatt_service_client_->GetHeartRateServicePath()); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(1, observer.gatt_discovery_complete_count_); - EXPECT_EQ(6, observer.gatt_characteristic_added_count_); - EXPECT_EQ(3, observer.gatt_characteristic_removed_count_); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(1, observer.gatt_discovery_complete_count()); + EXPECT_EQ(6, observer.gatt_characteristic_added_count()); + EXPECT_EQ(3, observer.gatt_characteristic_removed_count()); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); EXPECT_EQ(3U, service->GetCharacteristics().size()); // Hide the service. All characteristics should disappear. fake_bluetooth_gatt_service_client_->HideHeartRateService(); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(6, observer.gatt_characteristic_added_count_); - EXPECT_EQ(6, observer.gatt_characteristic_removed_count_); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(6, observer.gatt_characteristic_added_count()); + EXPECT_EQ(6, observer.gatt_characteristic_removed_count()); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); } TEST_F(BluetoothGattChromeOSTest, GattDescriptorAddedAndRemoved) { @@ -639,32 +434,32 @@ TEST_F(BluetoothGattChromeOSTest, GattDescriptorAddedAndRemoved) { FakeBluetoothDeviceClient::kLowEnergyAddress); ASSERT_TRUE(device); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Expose the fake Heart Rate service. This will asynchronously expose // characteristics. fake_bluetooth_gatt_service_client_->ExposeHeartRateService( dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); - ASSERT_EQ(1, observer.gatt_service_added_count_); + ASSERT_EQ(1, observer.gatt_service_added_count()); BluetoothGattService* service = - device->GetGattService(observer.last_gatt_service_id_); + device->GetGattService(observer.last_gatt_service_id()); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(0, observer.gatt_descriptor_added_count_); - EXPECT_EQ(0, observer.gatt_descriptor_removed_count_); - EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(0, observer.gatt_descriptor_added_count()); + EXPECT_EQ(0, observer.gatt_descriptor_removed_count()); + EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count()); EXPECT_TRUE(service->GetCharacteristics().empty()); // Run the message loop so that the characteristics appear. base::MessageLoop::current()->Run(); - EXPECT_EQ(0, observer.gatt_service_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); // Only the Heart Rate Measurement characteristic has a descriptor. - EXPECT_EQ(1, observer.gatt_descriptor_added_count_); - EXPECT_EQ(0, observer.gatt_descriptor_removed_count_); - EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count_); + EXPECT_EQ(1, observer.gatt_descriptor_added_count()); + EXPECT_EQ(0, observer.gatt_descriptor_removed_count()); + EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count()); BluetoothGattCharacteristic* characteristic = service->GetCharacteristic( fake_bluetooth_gatt_characteristic_client_-> @@ -688,37 +483,37 @@ TEST_F(BluetoothGattChromeOSTest, GattDescriptorAddedAndRemoved) { EXPECT_FALSE(descriptor->IsLocal()); EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(), descriptor->GetUUID()); - EXPECT_EQ(descriptor->GetUUID(), observer.last_gatt_descriptor_uuid_); - EXPECT_EQ(descriptor->GetIdentifier(), observer.last_gatt_descriptor_id_); + EXPECT_EQ(descriptor->GetUUID(), observer.last_gatt_descriptor_uuid()); + EXPECT_EQ(descriptor->GetIdentifier(), observer.last_gatt_descriptor_id()); // Hide the descriptor. fake_bluetooth_gatt_descriptor_client_->HideDescriptor( dbus::ObjectPath(descriptor->GetIdentifier())); EXPECT_TRUE(characteristic->GetDescriptors().empty()); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(1, observer.gatt_descriptor_added_count_); - EXPECT_EQ(1, observer.gatt_descriptor_removed_count_); - EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(1, observer.gatt_descriptor_added_count()); + EXPECT_EQ(1, observer.gatt_descriptor_removed_count()); + EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count()); // Expose the descriptor again. - observer.last_gatt_descriptor_id_.clear(); - observer.last_gatt_descriptor_uuid_ = BluetoothUUID(); + observer.last_gatt_descriptor_id().clear(); + observer.last_gatt_descriptor_uuid() = BluetoothUUID(); fake_bluetooth_gatt_descriptor_client_->ExposeDescriptor( dbus::ObjectPath(characteristic->GetIdentifier()), FakeBluetoothGattDescriptorClient:: kClientCharacteristicConfigurationUUID); - EXPECT_EQ(0, observer.gatt_service_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); EXPECT_EQ(1U, characteristic->GetDescriptors().size()); - EXPECT_EQ(2, observer.gatt_descriptor_added_count_); - EXPECT_EQ(1, observer.gatt_descriptor_removed_count_); - EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count_); + EXPECT_EQ(2, observer.gatt_descriptor_added_count()); + EXPECT_EQ(1, observer.gatt_descriptor_removed_count()); + EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count()); descriptor = characteristic->GetDescriptors()[0]; EXPECT_FALSE(descriptor->IsLocal()); EXPECT_EQ(BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid(), descriptor->GetUUID()); - EXPECT_EQ(descriptor->GetUUID(), observer.last_gatt_descriptor_uuid_); - EXPECT_EQ(descriptor->GetIdentifier(), observer.last_gatt_descriptor_id_); + EXPECT_EQ(descriptor->GetUUID(), observer.last_gatt_descriptor_uuid()); + EXPECT_EQ(descriptor->GetIdentifier(), observer.last_gatt_descriptor_id()); } TEST_F(BluetoothGattChromeOSTest, AdapterAddedAfterGattService) { @@ -805,25 +600,24 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { FakeBluetoothDeviceClient::kLowEnergyAddress); ASSERT_TRUE(device); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Expose the fake Heart Rate service. This will asynchronously expose // characteristics. fake_bluetooth_gatt_service_client_->ExposeHeartRateService( dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); - ASSERT_EQ(1, observer.gatt_service_added_count_); + ASSERT_EQ(1, observer.gatt_service_added_count()); BluetoothGattService* service = - device->GetGattService(observer.last_gatt_service_id_); + device->GetGattService(observer.last_gatt_service_id()); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); // Run the message loop so that the characteristics appear. base::MessageLoop::current()->Run(); // Issue write request to non-writable characteristics. - observer.last_gatt_characteristic_id_.clear(); - observer.last_gatt_characteristic_uuid_ = BluetoothUUID(); + observer.Reset(); std::vector<uint8> write_value; write_value.push_back(0x01); @@ -842,13 +636,13 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { base::Unretained(this)), base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback, base::Unretained(this))); - EXPECT_TRUE(observer.last_gatt_characteristic_id_.empty()); - EXPECT_FALSE(observer.last_gatt_characteristic_uuid_.IsValid()); + EXPECT_TRUE(observer.last_gatt_characteristic_id().empty()); + EXPECT_FALSE(observer.last_gatt_characteristic_uuid().IsValid()); EXPECT_EQ(0, success_callback_count_); EXPECT_EQ(1, error_callback_count_); EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_SUPPORTED, last_service_error_); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); characteristic = service->GetCharacteristic( fake_bluetooth_gatt_characteristic_client_-> @@ -864,13 +658,13 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { base::Unretained(this)), base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback, base::Unretained(this))); - EXPECT_TRUE(observer.last_gatt_characteristic_id_.empty()); - EXPECT_FALSE(observer.last_gatt_characteristic_uuid_.IsValid()); + EXPECT_TRUE(observer.last_gatt_characteristic_id().empty()); + EXPECT_FALSE(observer.last_gatt_characteristic_uuid().IsValid()); EXPECT_EQ(0, success_callback_count_); EXPECT_EQ(2, error_callback_count_); EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_PERMITTED, last_service_error_); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); // Issue write request to writable characteristic. The "Body Sensor Location" // characteristic does not send notifications and WriteValue does not result @@ -890,11 +684,11 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { base::Unretained(this)), base::Bind(&BluetoothGattChromeOSTest::ServiceErrorCallback, base::Unretained(this))); - EXPECT_TRUE(observer.last_gatt_characteristic_id_.empty()); - EXPECT_FALSE(observer.last_gatt_characteristic_uuid_.IsValid()); + EXPECT_TRUE(observer.last_gatt_characteristic_id().empty()); + EXPECT_FALSE(observer.last_gatt_characteristic_uuid().IsValid()); EXPECT_EQ(1, success_callback_count_); EXPECT_EQ(2, error_callback_count_); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); // Issue some invalid write requests to the characteristic. // The value should still not change. @@ -912,7 +706,7 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { EXPECT_EQ(3, error_callback_count_); EXPECT_EQ(BluetoothGattService::GATT_ERROR_INVALID_LENGTH, last_service_error_); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); std::vector<uint8> invalid_write_value; invalid_write_value.push_back(0x02); @@ -925,7 +719,7 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { EXPECT_EQ(1, success_callback_count_); EXPECT_EQ(4, error_callback_count_); EXPECT_EQ(BluetoothGattService::GATT_ERROR_FAILED, last_service_error_); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); // Issue a read request. A successful read results in a // CharacteristicValueChanged notification. @@ -944,7 +738,7 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { base::Unretained(this))); EXPECT_EQ(2, success_callback_count_); EXPECT_EQ(4, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(ValuesEqual(characteristic->GetValue(), last_read_value_)); // Test long-running actions. @@ -968,7 +762,7 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { // tne next one. EXPECT_EQ(2, success_callback_count_); EXPECT_EQ(4, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); // Next read should error because IN_PROGRESS characteristic->ReadRemoteCharacteristic( @@ -981,7 +775,7 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { // But previous call finished. EXPECT_EQ(3, success_callback_count_); - EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(ValuesEqual(characteristic->GetValue(), last_read_value_)); fake_bluetooth_gatt_characteristic_client_->SetExtraProcessing(0); @@ -996,7 +790,7 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { EXPECT_EQ(6, error_callback_count_); EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_AUTHORIZED, last_service_error_); - EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count()); fake_bluetooth_gatt_characteristic_client_->SetAuthorized(true); // Test unauthenticated / needs login. @@ -1009,7 +803,7 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicValue) { EXPECT_EQ(3, success_callback_count_); EXPECT_EQ(7, error_callback_count_); EXPECT_EQ(BluetoothGattService::GATT_ERROR_NOT_PAIRED, last_service_error_); - EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(2, observer.gatt_characteristic_value_changed_count()); fake_bluetooth_gatt_characteristic_client_->SetAuthenticated(true); } @@ -1021,7 +815,7 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicProperties) { FakeBluetoothDeviceClient::kLowEnergyAddress); ASSERT_TRUE(device); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Expose the fake Heart Rate service. This will asynchronously expose // characteristics. @@ -1029,7 +823,7 @@ TEST_F(BluetoothGattChromeOSTest, GattCharacteristicProperties) { dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); BluetoothGattService* service = - device->GetGattService(observer.last_gatt_service_id_); + device->GetGattService(observer.last_gatt_service_id()); EXPECT_TRUE(service->GetCharacteristics().empty()); @@ -1063,26 +857,26 @@ TEST_F(BluetoothGattChromeOSTest, GattDescriptorValue) { FakeBluetoothDeviceClient::kLowEnergyAddress); ASSERT_TRUE(device); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Expose the fake Heart Rate service. This will asynchronously expose // characteristics. fake_bluetooth_gatt_service_client_->ExposeHeartRateService( dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); - ASSERT_EQ(1, observer.gatt_service_added_count_); + ASSERT_EQ(1, observer.gatt_service_added_count()); BluetoothGattService* service = - device->GetGattService(observer.last_gatt_service_id_); + device->GetGattService(observer.last_gatt_service_id()); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(0, observer.gatt_discovery_complete_count_); - EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(0, observer.gatt_discovery_complete_count()); + EXPECT_EQ(0, observer.gatt_descriptor_value_changed_count()); EXPECT_TRUE(service->GetCharacteristics().empty()); // Run the message loop so that the characteristics appear. base::MessageLoop::current()->Run(); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(1, observer.gatt_discovery_complete_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(1, observer.gatt_discovery_complete_count()); // Only the Heart Rate Measurement characteristic has a descriptor. BluetoothGattCharacteristic* characteristic = service->GetCharacteristic( @@ -1118,8 +912,8 @@ TEST_F(BluetoothGattChromeOSTest, GattDescriptorValue) { EXPECT_EQ(0, error_callback_count_); EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue())); EXPECT_TRUE(ValuesEqual(desc_value, descriptor->GetValue())); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(1, observer.gatt_descriptor_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(1, observer.gatt_descriptor_value_changed_count()); // Write value. Writes to this descriptor will fail. desc_value[0] = 0x03; @@ -1135,8 +929,8 @@ TEST_F(BluetoothGattChromeOSTest, GattDescriptorValue) { last_service_error_); EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue())); EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue())); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(1, observer.gatt_descriptor_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(1, observer.gatt_descriptor_value_changed_count()); // Read value. The value should remain unchanged. descriptor->ReadRemoteDescriptor( @@ -1148,8 +942,8 @@ TEST_F(BluetoothGattChromeOSTest, GattDescriptorValue) { EXPECT_EQ(1, error_callback_count_); EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue())); EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue())); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(1, observer.gatt_descriptor_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(1, observer.gatt_descriptor_value_changed_count()); // Start notifications on the descriptor's characteristic. The descriptor // value should change. @@ -1174,8 +968,8 @@ TEST_F(BluetoothGattChromeOSTest, GattDescriptorValue) { EXPECT_EQ(1, error_callback_count_); EXPECT_TRUE(ValuesEqual(last_read_value_, descriptor->GetValue())); EXPECT_FALSE(ValuesEqual(desc_value, descriptor->GetValue())); - EXPECT_EQ(0, observer.gatt_service_changed_count_); - EXPECT_EQ(2, observer.gatt_descriptor_value_changed_count_); + EXPECT_EQ(0, observer.gatt_service_changed_count()); + EXPECT_EQ(2, observer.gatt_descriptor_value_changed_count()); } TEST_F(BluetoothGattChromeOSTest, NotifySessions) { @@ -1186,18 +980,18 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessions) { adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress); ASSERT_TRUE(device); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Expose the fake Heart Rate service. This will asynchronously expose // characteristics. fake_bluetooth_gatt_service_client_->ExposeHeartRateService( dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); - ASSERT_EQ(1, observer.gatt_service_added_count_); + ASSERT_EQ(1, observer.gatt_service_added_count()); BluetoothGattService* service = - device->GetGattService(observer.last_gatt_service_id_); + device->GetGattService(observer.last_gatt_service_id()); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); // Run the message loop so that the characteristics appear. base::MessageLoop::current()->Run(); @@ -1220,7 +1014,7 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessions) { // notification. EXPECT_EQ(0, success_callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(update_sessions_.empty()); // Send a two more requests, which should get queued. @@ -1236,7 +1030,7 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessions) { base::Unretained(this))); EXPECT_EQ(0, success_callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(update_sessions_.empty()); EXPECT_TRUE(characteristic->IsNotifying()); @@ -1246,12 +1040,12 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessions) { EXPECT_EQ(3, success_callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); EXPECT_EQ(3U, update_sessions_.size()); // Notifications should be getting sent regularly now. base::MessageLoop::current()->Run(); - EXPECT_GT(observer.gatt_characteristic_value_changed_count_, 1); + EXPECT_GT(observer.gatt_characteristic_value_changed_count(), 1); // Stop one of the sessions. The session should become inactive but the // characteristic should still be notifying. @@ -1279,7 +1073,7 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessions) { EXPECT_FALSE(characteristic->IsNotifying()); success_callback_count_ = 0; - observer.gatt_characteristic_value_changed_count_ = 0; + observer.Reset(); // Enable notifications again. characteristic->StartNotifySession( @@ -1289,7 +1083,7 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessions) { base::Unretained(this))); EXPECT_EQ(0, success_callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(update_sessions_.empty()); EXPECT_TRUE(characteristic->IsNotifying()); @@ -1298,14 +1092,14 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessions) { EXPECT_EQ(1, success_callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); EXPECT_EQ(1U, update_sessions_.size()); EXPECT_TRUE(update_sessions_[0]->IsActive()); EXPECT_TRUE(characteristic->IsNotifying()); // Check that notifications are happening. base::MessageLoop::current()->Run(); - EXPECT_GT(observer.gatt_characteristic_value_changed_count_, 1); + EXPECT_GT(observer.gatt_characteristic_value_changed_count(), 1); // Request another session. This should return immediately. characteristic->StartNotifySession( @@ -1335,18 +1129,18 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessionsMadeInactive) { adapter_->GetDevice(FakeBluetoothDeviceClient::kLowEnergyAddress); ASSERT_TRUE(device); - TestObserver observer(adapter_); + TestBluetoothAdapterObserver observer(adapter_); // Expose the fake Heart Rate service. This will asynchronously expose // characteristics. fake_bluetooth_gatt_service_client_->ExposeHeartRateService( dbus::ObjectPath(FakeBluetoothDeviceClient::kLowEnergyPath)); - ASSERT_EQ(1, observer.gatt_service_added_count_); + ASSERT_EQ(1, observer.gatt_service_added_count()); BluetoothGattService* service = - device->GetGattService(observer.last_gatt_service_id_); + device->GetGattService(observer.last_gatt_service_id()); - EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(0, observer.gatt_characteristic_value_changed_count()); // Run the message loop so that the characteristics appear. base::MessageLoop::current()->Run(); @@ -1384,7 +1178,7 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessionsMadeInactive) { // notification. EXPECT_EQ(0, success_callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(characteristic->IsNotifying()); EXPECT_TRUE(update_sessions_.empty()); @@ -1394,7 +1188,7 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessionsMadeInactive) { EXPECT_EQ(4, success_callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(characteristic->IsNotifying()); EXPECT_EQ(4U, update_sessions_.size()); @@ -1421,7 +1215,7 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessionsMadeInactive) { // the session count and make a request through the client. update_sessions_.clear(); success_callback_count_ = 0; - observer.gatt_characteristic_value_changed_count_ = 0; + observer.Reset(); characteristic->StartNotifySession( base::Bind(&BluetoothGattChromeOSTest::NotifySessionCallback, base::Unretained(this)), @@ -1430,7 +1224,7 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessionsMadeInactive) { EXPECT_EQ(0, success_callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(characteristic->IsNotifying()); EXPECT_TRUE(update_sessions_.empty()); @@ -1438,7 +1232,7 @@ TEST_F(BluetoothGattChromeOSTest, NotifySessionsMadeInactive) { EXPECT_EQ(1, success_callback_count_); EXPECT_EQ(0, error_callback_count_); - EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count_); + EXPECT_EQ(1, observer.gatt_characteristic_value_changed_count()); EXPECT_TRUE(characteristic->IsNotifying()); EXPECT_EQ(1U, update_sessions_.size()); EXPECT_TRUE(update_sessions_[0]->IsActive()); diff --git a/device/bluetooth/test/test_bluetooth_adapter_observer.cc b/device/bluetooth/test/test_bluetooth_adapter_observer.cc new file mode 100644 index 0000000..e03ae6c --- /dev/null +++ b/device/bluetooth/test/test_bluetooth_adapter_observer.cc @@ -0,0 +1,296 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "device/bluetooth/test/test_bluetooth_adapter_observer.h" + +#include "base/message_loop/message_loop.h" +#include "device/bluetooth/bluetooth_gatt_characteristic.h" +#include "device/bluetooth/bluetooth_gatt_descriptor.h" +#include "device/bluetooth/bluetooth_gatt_service.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace device { + +TestBluetoothAdapterObserver::TestBluetoothAdapterObserver( + scoped_refptr<BluetoothAdapter> adapter) + : adapter_(adapter) { + Reset(); + adapter_->AddObserver(this); +} + +TestBluetoothAdapterObserver::~TestBluetoothAdapterObserver() { + adapter_->RemoveObserver(this); +} + +void TestBluetoothAdapterObserver::Reset() { + present_changed_count_ = 0; + powered_changed_count_ = 0; + discoverable_changed_count_ = 0; + discovering_changed_count_ = 0; + last_present_ = false; + last_powered_ = false; + last_discovering_ = false; + device_added_count_ = 0; + device_changed_count_ = 0; + device_removed_count_ = 0; + last_device_ = NULL; + last_device_address_.clear(); + gatt_service_added_count_ = 0; + gatt_service_removed_count_ = 0; + gatt_service_changed_count_ = 0; + gatt_discovery_complete_count_ = 0; + gatt_characteristic_added_count_ = 0; + gatt_characteristic_removed_count_ = 0; + gatt_characteristic_value_changed_count_ = 0; + gatt_descriptor_added_count_ = 0; + gatt_descriptor_removed_count_ = 0; + gatt_descriptor_value_changed_count_ = 0; + last_gatt_service_id_.clear(); + last_gatt_service_uuid_ = BluetoothUUID(); + last_gatt_characteristic_id_.clear(); + last_gatt_characteristic_uuid_ = BluetoothUUID(); + last_changed_characteristic_value_.clear(); + last_gatt_descriptor_id_.clear(); + last_gatt_descriptor_uuid_ = BluetoothUUID(); + last_changed_descriptor_value_.clear(); +} + +void TestBluetoothAdapterObserver::AdapterPresentChanged( + BluetoothAdapter* adapter, + bool present) { + EXPECT_EQ(adapter_.get(), adapter); + + ++present_changed_count_; + last_present_ = present; +} + +void TestBluetoothAdapterObserver::AdapterPoweredChanged( + BluetoothAdapter* adapter, + bool powered) { + EXPECT_EQ(adapter_.get(), adapter); + + ++powered_changed_count_; + last_powered_ = powered; +} + +void TestBluetoothAdapterObserver::AdapterDiscoverableChanged( + BluetoothAdapter* adapter, + bool discoverable) { + EXPECT_EQ(adapter_.get(), adapter); + + ++discoverable_changed_count_; +} + +void TestBluetoothAdapterObserver::AdapterDiscoveringChanged( + BluetoothAdapter* adapter, + bool discovering) { + EXPECT_EQ(adapter_.get(), adapter); + + ++discovering_changed_count_; + last_discovering_ = discovering; +} + +void TestBluetoothAdapterObserver::DeviceAdded(BluetoothAdapter* adapter, + BluetoothDevice* device) { + EXPECT_EQ(adapter_.get(), adapter); + + ++device_added_count_; + last_device_ = device; + last_device_address_ = device->GetAddress(); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::DeviceChanged(BluetoothAdapter* adapter, + BluetoothDevice* device) { + EXPECT_EQ(adapter_.get(), adapter); + + ++device_changed_count_; + last_device_ = device; + last_device_address_ = device->GetAddress(); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::DeviceRemoved(BluetoothAdapter* adapter, + BluetoothDevice* device) { + EXPECT_EQ(adapter_.get(), adapter); + + ++device_removed_count_; + // Can't save device, it may be freed + last_device_address_ = device->GetAddress(); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::GattServiceAdded( + BluetoothAdapter* adapter, + BluetoothDevice* device, + BluetoothGattService* service) { + ASSERT_EQ(adapter_.get(), adapter); + ASSERT_EQ(service->GetDevice(), device); + + ++gatt_service_added_count_; + last_gatt_service_id_ = service->GetIdentifier(); + last_gatt_service_uuid_ = service->GetUUID(); + + EXPECT_FALSE(service->IsLocal()); + EXPECT_TRUE(service->IsPrimary()); + + EXPECT_EQ(device->GetGattService(last_gatt_service_id_), service); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::GattServiceRemoved( + BluetoothAdapter* adapter, + BluetoothDevice* device, + BluetoothGattService* service) { + ASSERT_EQ(adapter_.get(), adapter); + ASSERT_EQ(service->GetDevice(), device); + + ++gatt_service_removed_count_; + last_gatt_service_id_ = service->GetIdentifier(); + last_gatt_service_uuid_ = service->GetUUID(); + + EXPECT_FALSE(service->IsLocal()); + EXPECT_TRUE(service->IsPrimary()); + + // The device should return NULL for this service. + EXPECT_FALSE(device->GetGattService(last_gatt_service_id_)); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::GattDiscoveryCompleteForService( + BluetoothAdapter* adapter, + BluetoothGattService* service) { + ASSERT_EQ(adapter_.get(), adapter); + ++gatt_discovery_complete_count_; + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::GattServiceChanged( + BluetoothAdapter* adapter, + BluetoothGattService* service) { + ASSERT_EQ(adapter_.get(), adapter); + ++gatt_service_changed_count_; + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::GattCharacteristicAdded( + BluetoothAdapter* adapter, + BluetoothGattCharacteristic* characteristic) { + ASSERT_EQ(adapter_.get(), adapter); + + ++gatt_characteristic_added_count_; + last_gatt_characteristic_id_ = characteristic->GetIdentifier(); + last_gatt_characteristic_uuid_ = characteristic->GetUUID(); + + ASSERT_TRUE(characteristic->GetService()); + EXPECT_EQ(characteristic->GetService()->GetCharacteristic( + last_gatt_characteristic_id_), + characteristic); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::GattCharacteristicRemoved( + BluetoothAdapter* adapter, + BluetoothGattCharacteristic* characteristic) { + ASSERT_EQ(adapter_.get(), adapter); + + ++gatt_characteristic_removed_count_; + last_gatt_characteristic_id_ = characteristic->GetIdentifier(); + last_gatt_characteristic_uuid_ = characteristic->GetUUID(); + + // The service should return NULL for this characteristic. + ASSERT_TRUE(characteristic->GetService()); + EXPECT_FALSE(characteristic->GetService()->GetCharacteristic( + last_gatt_characteristic_id_)); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::GattDescriptorAdded( + BluetoothAdapter* adapter, + BluetoothGattDescriptor* descriptor) { + ASSERT_EQ(adapter_.get(), adapter); + + ++gatt_descriptor_added_count_; + last_gatt_descriptor_id_ = descriptor->GetIdentifier(); + last_gatt_descriptor_uuid_ = descriptor->GetUUID(); + + ASSERT_TRUE(descriptor->GetCharacteristic()); + EXPECT_EQ( + descriptor->GetCharacteristic()->GetDescriptor(last_gatt_descriptor_id_), + descriptor); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::GattDescriptorRemoved( + BluetoothAdapter* adapter, + BluetoothGattDescriptor* descriptor) { + ASSERT_EQ(adapter_.get(), adapter); + + ++gatt_descriptor_removed_count_; + last_gatt_descriptor_id_ = descriptor->GetIdentifier(); + last_gatt_descriptor_uuid_ = descriptor->GetUUID(); + + // The characteristic should return NULL for this descriptor.. + ASSERT_TRUE(descriptor->GetCharacteristic()); + EXPECT_FALSE( + descriptor->GetCharacteristic()->GetDescriptor(last_gatt_descriptor_id_)); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::GattCharacteristicValueChanged( + BluetoothAdapter* adapter, + BluetoothGattCharacteristic* characteristic, + const std::vector<uint8>& value) { + ASSERT_EQ(adapter_.get(), adapter); + + ++gatt_characteristic_value_changed_count_; + last_gatt_characteristic_id_ = characteristic->GetIdentifier(); + last_gatt_characteristic_uuid_ = characteristic->GetUUID(); + last_changed_characteristic_value_ = value; + + ASSERT_TRUE(characteristic->GetService()); + EXPECT_EQ(characteristic->GetService()->GetCharacteristic( + last_gatt_characteristic_id_), + characteristic); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::GattDescriptorValueChanged( + BluetoothAdapter* adapter, + BluetoothGattDescriptor* descriptor, + const std::vector<uint8>& value) { + ASSERT_EQ(adapter_.get(), adapter); + + ++gatt_descriptor_value_changed_count_; + last_gatt_descriptor_id_ = descriptor->GetIdentifier(); + last_gatt_descriptor_uuid_ = descriptor->GetUUID(); + last_changed_descriptor_value_ = value; + + ASSERT_TRUE(descriptor->GetCharacteristic()); + EXPECT_EQ( + descriptor->GetCharacteristic()->GetDescriptor(last_gatt_descriptor_id_), + descriptor); + + QuitMessageLoop(); +} + +void TestBluetoothAdapterObserver::QuitMessageLoop() { + if (base::MessageLoop::current() && + base::MessageLoop::current()->is_running()) + base::MessageLoop::current()->Quit(); +} + +} // namespace device diff --git a/device/bluetooth/test/test_bluetooth_adapter_observer.h b/device/bluetooth/test/test_bluetooth_adapter_observer.h new file mode 100644 index 0000000..f3a9cf8 --- /dev/null +++ b/device/bluetooth/test/test_bluetooth_adapter_observer.h @@ -0,0 +1,164 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef DEVICE_BLUETOOTH_TEST_BLUETOOTH_ADAPTER_OBSERVER_H_ +#define DEVICE_BLUETOOTH_TEST_BLUETOOTH_ADAPTER_OBSERVER_H_ + +#include "device/bluetooth/bluetooth_adapter.h" + +namespace device { + +// Test implementation of BluetoothAdapter::Observer counting method calls and +// caching last reported values. +class TestBluetoothAdapterObserver : public BluetoothAdapter::Observer { + public: + TestBluetoothAdapterObserver(scoped_refptr<BluetoothAdapter> adapter); + ~TestBluetoothAdapterObserver() override; + + // Reset counters and cached values. + void Reset(); + + // BluetoothAdapter::Observer + void AdapterPresentChanged(BluetoothAdapter* adapter, bool present) override; + void AdapterPoweredChanged(BluetoothAdapter* adapter, bool powered) override; + void AdapterDiscoverableChanged(BluetoothAdapter* adapter, + bool discoverable) override; + void AdapterDiscoveringChanged(BluetoothAdapter* adapter, + bool discovering) override; + void DeviceAdded(BluetoothAdapter* adapter, BluetoothDevice* device) override; + void DeviceChanged(BluetoothAdapter* adapter, + BluetoothDevice* device) override; + void DeviceRemoved(BluetoothAdapter* adapter, + BluetoothDevice* device) override; + void GattServiceAdded(BluetoothAdapter* adapter, + BluetoothDevice* device, + BluetoothGattService* service) override; + void GattServiceRemoved(BluetoothAdapter* adapter, + BluetoothDevice* device, + BluetoothGattService* service) override; + void GattDiscoveryCompleteForService(BluetoothAdapter* adapter, + BluetoothGattService* service) override; + void GattServiceChanged(BluetoothAdapter* adapter, + BluetoothGattService* service) override; + void GattCharacteristicAdded( + BluetoothAdapter* adapter, + BluetoothGattCharacteristic* characteristic) override; + void GattCharacteristicRemoved( + BluetoothAdapter* adapter, + BluetoothGattCharacteristic* characteristic) override; + void GattDescriptorAdded(BluetoothAdapter* adapter, + BluetoothGattDescriptor* descriptor) override; + void GattDescriptorRemoved(BluetoothAdapter* adapter, + BluetoothGattDescriptor* descriptor) override; + void GattCharacteristicValueChanged( + BluetoothAdapter* adapter, + BluetoothGattCharacteristic* characteristic, + const std::vector<uint8>& value) override; + void GattDescriptorValueChanged(BluetoothAdapter* adapter, + BluetoothGattDescriptor* descriptor, + const std::vector<uint8>& value) override; + + // Adapter related: + int present_changed_count() { return present_changed_count_; } + int powered_changed_count() { return powered_changed_count_; } + int discoverable_changed_count() { return discoverable_changed_count_; } + int discovering_changed_count() { return discovering_changed_count_; } + bool last_present() { return last_present_; } + bool last_powered() { return last_powered_; } + bool last_discovering() { return last_discovering_; } + + // Device related: + int device_added_count() { return device_added_count_; } + int device_changed_count() { return device_changed_count_; } + int device_removed_count() { return device_removed_count_; } + BluetoothDevice* last_device() { return last_device_; } + std::string last_device_address() { return last_device_address_; } + + // GATT related: + int gatt_service_added_count() { return gatt_service_added_count_; } + int gatt_service_removed_count() { return gatt_service_removed_count_; } + int gatt_service_changed_count() { return gatt_service_changed_count_; } + int gatt_discovery_complete_count() { return gatt_discovery_complete_count_; } + int gatt_characteristic_added_count() { + return gatt_characteristic_added_count_; + } + int gatt_characteristic_removed_count() { + return gatt_characteristic_removed_count_; + } + int gatt_characteristic_value_changed_count() { + return gatt_characteristic_value_changed_count_; + } + int gatt_descriptor_added_count() { return gatt_descriptor_added_count_; } + int gatt_descriptor_removed_count() { return gatt_descriptor_removed_count_; } + int gatt_descriptor_value_changed_count() { + return gatt_descriptor_value_changed_count_; + } + std::string last_gatt_service_id() { return last_gatt_service_id_; } + BluetoothUUID last_gatt_service_uuid() { return last_gatt_service_uuid_; } + std::string last_gatt_characteristic_id() { + return last_gatt_characteristic_id_; + } + BluetoothUUID last_gatt_characteristic_uuid() { + return last_gatt_characteristic_uuid_; + } + std::vector<uint8> last_changed_characteristic_value() { + return last_changed_characteristic_value_; + } + std::string last_gatt_descriptor_id() { return last_gatt_descriptor_id_; } + BluetoothUUID last_gatt_descriptor_uuid() { + return last_gatt_descriptor_uuid_; + } + std::vector<uint8> last_changed_descriptor_value() { + return last_changed_descriptor_value_; + } + + private: + // Some tests use a message loop since background processing is simulated; + // break out of those loops. + void QuitMessageLoop(); + + scoped_refptr<BluetoothAdapter> adapter_; + + // Adapter related: + int present_changed_count_; + int powered_changed_count_; + int discoverable_changed_count_; + int discovering_changed_count_; + bool last_present_; + bool last_powered_; + bool last_discovering_; + + // Device related: + int device_added_count_; + int device_changed_count_; + int device_removed_count_; + BluetoothDevice* last_device_; + std::string last_device_address_; + + // GATT related: + int gatt_service_added_count_; + int gatt_service_removed_count_; + int gatt_service_changed_count_; + int gatt_discovery_complete_count_; + int gatt_characteristic_added_count_; + int gatt_characteristic_removed_count_; + int gatt_characteristic_value_changed_count_; + int gatt_descriptor_added_count_; + int gatt_descriptor_removed_count_; + int gatt_descriptor_value_changed_count_; + std::string last_gatt_service_id_; + BluetoothUUID last_gatt_service_uuid_; + std::string last_gatt_characteristic_id_; + BluetoothUUID last_gatt_characteristic_uuid_; + std::vector<uint8> last_changed_characteristic_value_; + std::string last_gatt_descriptor_id_; + BluetoothUUID last_gatt_descriptor_uuid_; + std::vector<uint8> last_changed_descriptor_value_; + + DISALLOW_COPY_AND_ASSIGN(TestBluetoothAdapterObserver); +}; + +} // namespace device + +#endif // DEVICE_BLUETOOTH_TEST_BLUETOOTH_ADAPTER_OBSERVER_H_ diff --git a/device/device_tests.gyp b/device/device_tests.gyp index 15b63c4..432102a 100644 --- a/device/device_tests.gyp +++ b/device/device_tests.gyp @@ -49,6 +49,8 @@ 'bluetooth/bluetooth_socket_chromeos_unittest.cc', 'bluetooth/bluetooth_task_manager_win_unittest.cc', 'bluetooth/bluetooth_uuid_unittest.cc', + 'bluetooth/test/test_bluetooth_adapter_observer.cc', + 'bluetooth/test/test_bluetooth_adapter_observer.h', 'hid/hid_connection_unittest.cc', 'hid/hid_device_filter_unittest.cc', 'hid/hid_report_descriptor_unittest.cc', |