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_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 return make_scoped_ptr(new BluetoothAdvertisement::Data( | |
92 BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST, | |
93 make_scoped_ptr(new BluetoothAdvertisement::UUIDList()).Pass(), | |
94 make_scoped_ptr(new BluetoothAdvertisement::ManufacturerData()).Pass(), | |
95 make_scoped_ptr(new BluetoothAdvertisement::UUIDList()).Pass(), | |
96 make_scoped_ptr(new BluetoothAdvertisement::ServiceData()).Pass())); | |
97 } | |
98 | |
99 // Creates and registers an advertisement with the adapter. | |
100 scoped_refptr<BluetoothAdvertisement> CreateAdvertisement() { | |
101 // Clear the last advertisement we created. | |
102 advertisement_ = nullptr; | |
103 | |
104 adapter_->RegisterAdvertisement( | |
105 CreateAdvertisementData().Pass(), | |
106 base::Bind(&BluetoothAdvertisementChromeOSTest::RegisterCallback, | |
107 base::Unretained(this)), | |
108 base::Bind( | |
109 &BluetoothAdvertisementChromeOSTest::AdvertisementErrorCallback, | |
110 base::Unretained(this))); | |
111 | |
112 message_loop_.RunUntilIdle(); | |
113 return advertisement_; | |
114 } | |
115 | |
116 void UnregisterAdvertisement( | |
117 scoped_refptr<BluetoothAdvertisement> advertisement) { | |
118 advertisement->Unregister( | |
119 base::Bind(&BluetoothAdvertisementChromeOSTest::Callback, | |
120 base::Unretained(this)), | |
121 base::Bind( | |
122 &BluetoothAdvertisementChromeOSTest::AdvertisementErrorCallback, | |
123 base::Unretained(this))); | |
124 | |
125 message_loop_.RunUntilIdle(); | |
126 } | |
127 | |
128 void TriggerReleased(scoped_refptr<BluetoothAdvertisement> advertisement) { | |
129 BluetoothAdvertisementChromeOS* adv = | |
130 static_cast<BluetoothAdvertisementChromeOS*>(advertisement.get()); | |
131 FakeBluetoothLEAdvertisementServiceProvider* provider = | |
132 static_cast<FakeBluetoothLEAdvertisementServiceProvider*>( | |
133 adv->provider()); | |
134 provider->Release(); | |
135 } | |
136 | |
137 // Called whenever RegisterAdvertisement is completed successfully. | |
138 void RegisterCallback(scoped_refptr<BluetoothAdvertisement> advertisement) { | |
139 ++callback_count_; | |
140 advertisement_ = advertisement; | |
141 | |
142 ASSERT_NE(advertisement_.get(), nullptr); | |
143 } | |
144 | |
145 void AdvertisementErrorCallback( | |
146 BluetoothAdvertisement::ErrorCode error_code) { | |
147 ++error_callback_count_; | |
148 last_error_code_ = error_code; | |
149 } | |
150 | |
151 // Generic callbacks. | |
152 void Callback() { ++callback_count_; } | |
153 | |
154 void ErrorCallback() { ++error_callback_count_; } | |
155 | |
156 void ExpectSuccess() { | |
157 EXPECT_EQ(last_error_callback_count_, error_callback_count_); | |
158 EXPECT_EQ(last_callback_count_ + 1, callback_count_); | |
159 last_callback_count_++; | |
Marie Janssen
2015/04/21 18:15:10
This should be last_callback_count_ = callback_cou
rkc
2015/04/23 19:32:17
Done.
| |
160 } | |
161 | |
162 void ExpectError(BluetoothAdvertisement::ErrorCode error_code) { | |
163 EXPECT_EQ(last_callback_count_, callback_count_); | |
164 EXPECT_EQ(last_error_callback_count_ + 1, error_callback_count_); | |
165 last_error_callback_count_++; | |
166 EXPECT_EQ(error_code, last_error_code_); | |
167 } | |
168 | |
169 protected: | |
170 int callback_count_; | |
171 int error_callback_count_; | |
172 | |
173 int last_callback_count_; | |
174 int last_error_callback_count_; | |
175 | |
176 BluetoothAdvertisement::ErrorCode last_error_code_; | |
177 | |
178 base::MessageLoopForIO message_loop_; | |
179 | |
180 scoped_ptr<TestAdvertisementObserver> observer_; | |
181 scoped_refptr<BluetoothAdapter> adapter_; | |
182 scoped_refptr<BluetoothAdvertisement> advertisement_; | |
183 }; | |
184 | |
185 TEST_F(BluetoothAdvertisementChromeOSTest, RegisterSucceeded) { | |
186 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement(); | |
187 ExpectSuccess(); | |
188 EXPECT_NE(nullptr, advertisement); | |
189 | |
190 UnregisterAdvertisement(advertisement); | |
191 ExpectSuccess(); | |
192 } | |
193 | |
194 TEST_F(BluetoothAdvertisementChromeOSTest, DoubleRegisterFailed) { | |
195 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement(); | |
196 ExpectSuccess(); | |
197 EXPECT_NE(nullptr, advertisement); | |
198 | |
199 // Creating a second advertisement should give us an error. | |
200 scoped_refptr<BluetoothAdvertisement> advertisement2 = CreateAdvertisement(); | |
201 ExpectError(BluetoothAdvertisement::ERROR_ALREADY_EXISTS); | |
202 EXPECT_EQ(nullptr, advertisement2); | |
203 } | |
204 | |
205 TEST_F(BluetoothAdvertisementChromeOSTest, DoubleUnregisterFailed) { | |
206 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement(); | |
207 ExpectSuccess(); | |
208 EXPECT_NE(nullptr, advertisement); | |
209 | |
210 UnregisterAdvertisement(advertisement); | |
211 ExpectSuccess(); | |
212 | |
213 // Unregistering an already unregistered advertisement should give us an | |
214 // error. | |
215 UnregisterAdvertisement(advertisement); | |
216 ExpectError(BluetoothAdvertisement::ERROR_DOES_NOT_EXIST); | |
217 } | |
218 | |
219 TEST_F(BluetoothAdvertisementChromeOSTest, UnregisterAfterReleasedFailed) { | |
220 scoped_refptr<BluetoothAdvertisement> advertisement = CreateAdvertisement(); | |
221 ExpectSuccess(); | |
222 EXPECT_NE(nullptr, advertisement); | |
223 | |
224 observer_.reset(new TestAdvertisementObserver(advertisement)); | |
225 TriggerReleased(advertisement); | |
226 EXPECT_TRUE(observer_->released()); | |
227 | |
228 // Unregistering an advertisement that has been released should give us an | |
229 // error. | |
230 UnregisterAdvertisement(advertisement); | |
231 ExpectError(BluetoothAdvertisement::ERROR_DOES_NOT_EXIST); | |
232 } | |
233 | |
234 } // namespace chromeos | |
OLD | NEW |