Move notification sending out of AutocompleteController
[chromium-blink-merge.git] / device / bluetooth / bluetooth_advertisement_chromeos_unittest.cc
blob02fe1e4a574799a6c5624dd9666badc025b4db58
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.
5 #include <vector>
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"
20 using device::BluetoothAdapter;
21 using device::BluetoothAdapterFactory;
22 using device::BluetoothAdvertisement;
24 namespace chromeos {
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);
34 ~TestAdvertisementObserver() override {
35 advertisement_->RemoveObserver(this);
38 // BluetoothAdvertisement::Observer overrides:
39 void AdvertisementReleased(BluetoothAdvertisement* advertisement) override {
40 released_ = true;
43 bool released() { return released_; }
45 private:
46 bool released_;
47 scoped_refptr<BluetoothAdvertisement> advertisement_;
49 DISALLOW_COPY_AND_ASSIGN(TestAdvertisementObserver);
52 class BluetoothAdvertisementChromeOSTest : public testing::Test {
53 public:
54 void SetUp() override {
55 DBusThreadManager::Initialize();
57 callback_count_ = 0;
58 error_callback_count_ = 0;
60 last_callback_count_ = 0;
61 last_error_callback_count_ = 0;
63 last_error_code_ = BluetoothAdvertisement::INVALID_ADVERTISEMENT_ERROR_CODE;
65 GetAdapter();
68 void TearDown() override {
69 observer_.reset();
70 // The adapter should outlive the advertisement.
71 advertisement_ = nullptr;
72 adapter_ = nullptr;
73 DBusThreadManager::Shutdown();
76 // Gets the existing Bluetooth adapter.
77 void GetAdapter() {
78 BluetoothAdapterFactory::GetAdapter(
79 base::Bind(&BluetoothAdvertisementChromeOSTest::GetAdapterCallback,
80 base::Unretained(this)));
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());
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();
105 // Creates and registers an advertisement with the adapter.
106 scoped_refptr<BluetoothAdvertisement> CreateAdvertisement() {
107 // Clear the last advertisement we created.
108 advertisement_ = nullptr;
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)));
118 message_loop_.RunUntilIdle();
119 return advertisement_;
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)));
131 message_loop_.RunUntilIdle();
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();
143 // Called whenever RegisterAdvertisement is completed successfully.
144 void RegisterCallback(scoped_refptr<BluetoothAdvertisement> advertisement) {
145 ++callback_count_;
146 advertisement_ = advertisement;
148 ASSERT_NE(advertisement_.get(), nullptr);
151 void AdvertisementErrorCallback(
152 BluetoothAdvertisement::ErrorCode error_code) {
153 ++error_callback_count_;
154 last_error_code_ = error_code;
157 // Generic callbacks.
158 void Callback() { ++callback_count_; }
160 void ErrorCallback() { ++error_callback_count_; }
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_;
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_);
177 protected:
178 int callback_count_;
179 int error_callback_count_;
181 int last_callback_count_;
182 int last_error_callback_count_;
184 BluetoothAdvertisement::ErrorCode last_error_code_;
186 base::MessageLoopForIO message_loop_;
188 scoped_ptr<TestAdvertisementObserver> observer_;
189 scoped_refptr<BluetoothAdapter> adapter_;
190 scoped_refptr<BluetoothAdvertisement> advertisement_;
193 TEST_F(BluetoothAdvertisementChromeOSTest, RegisterSucceeded) {
194 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement();
195 ExpectSuccess();
196 EXPECT_NE(nullptr, advertisement);
198 UnregisterAdvertisement(advertisement);
199 ExpectSuccess();
202 TEST_F(BluetoothAdvertisementChromeOSTest, DoubleRegisterFailed) {
203 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement();
204 ExpectSuccess();
205 EXPECT_NE(nullptr, advertisement);
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);
213 TEST_F(BluetoothAdvertisementChromeOSTest, DoubleUnregisterFailed) {
214 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement();
215 ExpectSuccess();
216 EXPECT_NE(nullptr, advertisement);
218 UnregisterAdvertisement(advertisement);
219 ExpectSuccess();
221 // Unregistering an already unregistered advertisement should give us an
222 // error.
223 UnregisterAdvertisement(advertisement);
224 ExpectError(BluetoothAdvertisement::ERROR_ADVERTISEMENT_DOES_NOT_EXIST);
227 TEST_F(BluetoothAdvertisementChromeOSTest, UnregisterAfterReleasedFailed) {
228 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement();
229 ExpectSuccess();
230 EXPECT_NE(nullptr, advertisement);
232 observer_.reset(new TestAdvertisementObserver(advertisement));
233 TriggerReleased(advertisement);
234 EXPECT_TRUE(observer_->released());
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);
242 } // namespace chromeos