| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <vector> | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 13 #include "chromeos/dbus/fake_bluetooth_le_advertisement_service_provider.h" | |
| 14 #include "device/bluetooth/bluetooth_adapter.h" | |
| 15 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 16 #include "device/bluetooth/bluetooth_advertisement.h" | |
| 17 #include "device/bluetooth/bluetooth_advertisement_chromeos.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using device::BluetoothAdapter; | |
| 21 using device::BluetoothAdapterFactory; | |
| 22 using device::BluetoothAdvertisement; | |
| 23 | |
| 24 namespace chromeos { | |
| 25 | |
| 26 class TestAdvertisementObserver : public BluetoothAdvertisement::Observer { | |
| 27 public: | |
| 28 explicit TestAdvertisementObserver( | |
| 29 scoped_refptr<BluetoothAdvertisement> advertisement) | |
| 30 : released_(false), advertisement_(advertisement) { | |
| 31 advertisement_->AddObserver(this); | |
| 32 } | |
| 33 | |
| 34 ~TestAdvertisementObserver() override { | |
| 35 advertisement_->RemoveObserver(this); | |
| 36 } | |
| 37 | |
| 38 // BluetoothAdvertisement::Observer overrides: | |
| 39 void AdvertisementReleased(BluetoothAdvertisement* advertisement) override { | |
| 40 released_ = true; | |
| 41 } | |
| 42 | |
| 43 bool released() { return released_; } | |
| 44 | |
| 45 private: | |
| 46 bool released_; | |
| 47 scoped_refptr<BluetoothAdvertisement> advertisement_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(TestAdvertisementObserver); | |
| 50 }; | |
| 51 | |
| 52 class BluetoothAdvertisementChromeOSTest : public testing::Test { | |
| 53 public: | |
| 54 void SetUp() override { | |
| 55 DBusThreadManager::Initialize(); | |
| 56 | |
| 57 callback_count_ = 0; | |
| 58 error_callback_count_ = 0; | |
| 59 | |
| 60 last_callback_count_ = 0; | |
| 61 last_error_callback_count_ = 0; | |
| 62 | |
| 63 last_error_code_ = BluetoothAdvertisement::INVALID_ADVERTISEMENT_ERROR_CODE; | |
| 64 | |
| 65 GetAdapter(); | |
| 66 } | |
| 67 | |
| 68 void TearDown() override { | |
| 69 observer_.reset(); | |
| 70 // The adapter should outlive the advertisement. | |
| 71 advertisement_ = nullptr; | |
| 72 adapter_ = nullptr; | |
| 73 DBusThreadManager::Shutdown(); | |
| 74 } | |
| 75 | |
| 76 // Gets the existing Bluetooth adapter. | |
| 77 void GetAdapter() { | |
| 78 BluetoothAdapterFactory::GetAdapter( | |
| 79 base::Bind(&BluetoothAdvertisementChromeOSTest::GetAdapterCallback, | |
| 80 base::Unretained(this))); | |
| 81 } | |
| 82 | |
| 83 // Called whenever BluetoothAdapter is retrieved successfully. | |
| 84 void GetAdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { | |
| 85 adapter_ = adapter; | |
| 86 ASSERT_NE(adapter_.get(), nullptr); | |
| 87 ASSERT_TRUE(adapter_->IsInitialized()); | |
| 88 } | |
| 89 | |
| 90 scoped_ptr<BluetoothAdvertisement::Data> CreateAdvertisementData() { | |
| 91 scoped_ptr<BluetoothAdvertisement::Data> data = | |
| 92 make_scoped_ptr(new BluetoothAdvertisement::Data( | |
| 93 BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST)); | |
| 94 data->set_service_uuids( | |
| 95 make_scoped_ptr(new BluetoothAdvertisement::UUIDList()).Pass()); | |
| 96 data->set_manufacturer_data( | |
| 97 make_scoped_ptr(new BluetoothAdvertisement::ManufacturerData()).Pass()); | |
| 98 data->set_solicit_uuids( | |
| 99 make_scoped_ptr(new BluetoothAdvertisement::UUIDList()).Pass()); | |
| 100 data->set_service_data( | |
| 101 make_scoped_ptr(new BluetoothAdvertisement::ServiceData()).Pass()); | |
| 102 return data.Pass(); | |
| 103 } | |
| 104 | |
| 105 // Creates and registers an advertisement with the adapter. | |
| 106 scoped_refptr<BluetoothAdvertisement> CreateAdvertisement() { | |
| 107 // Clear the last advertisement we created. | |
| 108 advertisement_ = nullptr; | |
| 109 | |
| 110 adapter_->RegisterAdvertisement( | |
| 111 CreateAdvertisementData().Pass(), | |
| 112 base::Bind(&BluetoothAdvertisementChromeOSTest::RegisterCallback, | |
| 113 base::Unretained(this)), | |
| 114 base::Bind( | |
| 115 &BluetoothAdvertisementChromeOSTest::AdvertisementErrorCallback, | |
| 116 base::Unretained(this))); | |
| 117 | |
| 118 message_loop_.RunUntilIdle(); | |
| 119 return advertisement_; | |
| 120 } | |
| 121 | |
| 122 void UnregisterAdvertisement( | |
| 123 scoped_refptr<BluetoothAdvertisement> advertisement) { | |
| 124 advertisement->Unregister( | |
| 125 base::Bind(&BluetoothAdvertisementChromeOSTest::Callback, | |
| 126 base::Unretained(this)), | |
| 127 base::Bind( | |
| 128 &BluetoothAdvertisementChromeOSTest::AdvertisementErrorCallback, | |
| 129 base::Unretained(this))); | |
| 130 | |
| 131 message_loop_.RunUntilIdle(); | |
| 132 } | |
| 133 | |
| 134 void TriggerReleased(scoped_refptr<BluetoothAdvertisement> advertisement) { | |
| 135 BluetoothAdvertisementChromeOS* adv = | |
| 136 static_cast<BluetoothAdvertisementChromeOS*>(advertisement.get()); | |
| 137 FakeBluetoothLEAdvertisementServiceProvider* provider = | |
| 138 static_cast<FakeBluetoothLEAdvertisementServiceProvider*>( | |
| 139 adv->provider()); | |
| 140 provider->Release(); | |
| 141 } | |
| 142 | |
| 143 // Called whenever RegisterAdvertisement is completed successfully. | |
| 144 void RegisterCallback(scoped_refptr<BluetoothAdvertisement> advertisement) { | |
| 145 ++callback_count_; | |
| 146 advertisement_ = advertisement; | |
| 147 | |
| 148 ASSERT_NE(advertisement_.get(), nullptr); | |
| 149 } | |
| 150 | |
| 151 void AdvertisementErrorCallback( | |
| 152 BluetoothAdvertisement::ErrorCode error_code) { | |
| 153 ++error_callback_count_; | |
| 154 last_error_code_ = error_code; | |
| 155 } | |
| 156 | |
| 157 // Generic callbacks. | |
| 158 void Callback() { ++callback_count_; } | |
| 159 | |
| 160 void ErrorCallback() { ++error_callback_count_; } | |
| 161 | |
| 162 void ExpectSuccess() { | |
| 163 EXPECT_EQ(last_error_callback_count_, error_callback_count_); | |
| 164 EXPECT_EQ(last_callback_count_ + 1, callback_count_); | |
| 165 last_callback_count_ = callback_count_; | |
| 166 last_error_callback_count_ = error_callback_count_; | |
| 167 } | |
| 168 | |
| 169 void ExpectError(BluetoothAdvertisement::ErrorCode error_code) { | |
| 170 EXPECT_EQ(last_callback_count_, callback_count_); | |
| 171 EXPECT_EQ(last_error_callback_count_ + 1, error_callback_count_); | |
| 172 last_callback_count_ = callback_count_; | |
| 173 last_error_callback_count_ = error_callback_count_; | |
| 174 EXPECT_EQ(error_code, last_error_code_); | |
| 175 } | |
| 176 | |
| 177 protected: | |
| 178 int callback_count_; | |
| 179 int error_callback_count_; | |
| 180 | |
| 181 int last_callback_count_; | |
| 182 int last_error_callback_count_; | |
| 183 | |
| 184 BluetoothAdvertisement::ErrorCode last_error_code_; | |
| 185 | |
| 186 base::MessageLoopForIO message_loop_; | |
| 187 | |
| 188 scoped_ptr<TestAdvertisementObserver> observer_; | |
| 189 scoped_refptr<BluetoothAdapter> adapter_; | |
| 190 scoped_refptr<BluetoothAdvertisement> advertisement_; | |
| 191 }; | |
| 192 | |
| 193 TEST_F(BluetoothAdvertisementChromeOSTest, RegisterSucceeded) { | |
| 194 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement(); | |
| 195 ExpectSuccess(); | |
| 196 EXPECT_NE(nullptr, advertisement); | |
| 197 | |
| 198 UnregisterAdvertisement(advertisement); | |
| 199 ExpectSuccess(); | |
| 200 } | |
| 201 | |
| 202 TEST_F(BluetoothAdvertisementChromeOSTest, DoubleRegisterFailed) { | |
| 203 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement(); | |
| 204 ExpectSuccess(); | |
| 205 EXPECT_NE(nullptr, advertisement); | |
| 206 | |
| 207 // Creating a second advertisement should give us an error. | |
| 208 scoped_refptr<BluetoothAdvertisement> advertisement2 = CreateAdvertisement(); | |
| 209 ExpectError(BluetoothAdvertisement::ERROR_ADVERTISEMENT_ALREADY_EXISTS); | |
| 210 EXPECT_EQ(nullptr, advertisement2); | |
| 211 } | |
| 212 | |
| 213 TEST_F(BluetoothAdvertisementChromeOSTest, DoubleUnregisterFailed) { | |
| 214 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement(); | |
| 215 ExpectSuccess(); | |
| 216 EXPECT_NE(nullptr, advertisement); | |
| 217 | |
| 218 UnregisterAdvertisement(advertisement); | |
| 219 ExpectSuccess(); | |
| 220 | |
| 221 // Unregistering an already unregistered advertisement should give us an | |
| 222 // error. | |
| 223 UnregisterAdvertisement(advertisement); | |
| 224 ExpectError(BluetoothAdvertisement::ERROR_ADVERTISEMENT_DOES_NOT_EXIST); | |
| 225 } | |
| 226 | |
| 227 TEST_F(BluetoothAdvertisementChromeOSTest, UnregisterAfterReleasedFailed) { | |
| 228 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement(); | |
| 229 ExpectSuccess(); | |
| 230 EXPECT_NE(nullptr, advertisement); | |
| 231 | |
| 232 observer_.reset(new TestAdvertisementObserver(advertisement)); | |
| 233 TriggerReleased(advertisement); | |
| 234 EXPECT_TRUE(observer_->released()); | |
| 235 | |
| 236 // Unregistering an advertisement that has been released should give us an | |
| 237 // error. | |
| 238 UnregisterAdvertisement(advertisement); | |
| 239 ExpectError(BluetoothAdvertisement::ERROR_ADVERTISEMENT_DOES_NOT_EXIST); | |
| 240 } | |
| 241 | |
| 242 } // namespace chromeos | |
| OLD | NEW |