Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: chrome/browser/chromeos/bluetooth/bluetooth_adapter_unittest.cc

Issue 10899037: Refactoring bluetooth API code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: A Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 <iostream>
6
7 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
8 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h"
9 #include "chromeos/dbus/mock_bluetooth_adapter_client.h"
10 #include "chromeos/dbus/mock_bluetooth_manager_client.h"
11 #include "chromeos/dbus/mock_dbus_thread_manager.h"
12 #include "dbus/object_path.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using ::testing::_;
16 using ::testing::Return;
17 using ::testing::SaveArg;
18
19 namespace chromeos {
20
21 class BluetoothAdapterTest : public testing::Test {
22 public:
23 virtual void SetUp() {
24 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager;
25
26 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus())
27 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL)));
28 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager);
29
30 mock_manager_client_ =
31 mock_dbus_thread_manager->mock_bluetooth_manager_client();
32 mock_adapter_client_ =
33 mock_dbus_thread_manager->mock_bluetooth_adapter_client();
34 }
35
36 virtual void TearDown() {
37 DBusThreadManager::Shutdown();
38 }
39
40 protected:
41 MockBluetoothManagerClient* mock_manager_client_;
42 MockBluetoothAdapterClient* mock_adapter_client_;
43 };
44
45 TEST_F(BluetoothAdapterTest, DefaultAdapterNotPresent) {
46 // Create the default adapter instance;
47 // BluetoothManagerClient::DefaultAdapter will be called once, passing
48 // a callback to obtain the adapter path.
49 BluetoothManagerClient::AdapterCallback adapter_callback;
50 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
51 .WillOnce(SaveArg<0>(&adapter_callback));
52
53 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter();
54
55 // Call the adapter callback; make out it failed.
56 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called.
57 MockBluetoothAdapter::Observer adapter_observer;
58 adapter->AddObserver(&adapter_observer);
59
60 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _))
61 .Times(0);
62
63 adapter_callback.Run(dbus::ObjectPath(""), false);
64
65 // Adapter should not be present.
66 EXPECT_FALSE(adapter->IsPresent());
67 }
68
69 TEST_F(BluetoothAdapterTest, DefaultAdapterWithAddress) {
70 const dbus::ObjectPath adapter_path("/fake/hci0");
71 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
72
73 // Create the default adapter instance;
74 // BluetoothManagerClient::DefaultAdapter will be called once, passing
75 // a callback to obtain the adapter path.
76 BluetoothManagerClient::AdapterCallback adapter_callback;
77 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
78 .WillOnce(SaveArg<0>(&adapter_callback));
79
80 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter();
81
82 // Call the adapter callback;
83 // BluetoothAdapterClient::GetProperties will be called once to obtain
84 // the property set.
85 MockBluetoothAdapterClient::Properties adapter_properties;
86 adapter_properties.address.ReplaceValue(adapter_address);
87
88 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
89 .WillRepeatedly(Return(&adapter_properties));
90
91 // BluetoothAdapter::Observer::AdapterPresentChanged will be called to
92 // indicate the adapter is now present.
93 MockBluetoothAdapter::Observer adapter_observer;
94 adapter->AddObserver(&adapter_observer);
95
96 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
97 .Times(1);
98
99 adapter_callback.Run(adapter_path, true);
100
101 // Adapter should be present with the given address.
102 EXPECT_TRUE(adapter->IsPresent());
103 EXPECT_EQ(adapter_address, adapter->address());
104 }
105
106 TEST_F(BluetoothAdapterTest, DefaultAdapterWithoutAddress) {
107 const dbus::ObjectPath adapter_path("/fake/hci0");
108 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
109
110 // Create the default adapter instance;
111 // BluetoothManagerClient::DefaultAdapter will be called once, passing
112 // a callback to obtain the adapter path.
113 BluetoothManagerClient::AdapterCallback adapter_callback;
114 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
115 .WillOnce(SaveArg<0>(&adapter_callback));
116
117 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter();
118
119 // Call the adapter callback;
120 // BluetoothAdapterClient::GetProperties will be called once to obtain
121 // the property set.
122 MockBluetoothAdapterClient::Properties adapter_properties;
123
124 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
125 .WillRepeatedly(Return(&adapter_properties));
126
127 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called
128 // yet.
129 MockBluetoothAdapter::Observer adapter_observer;
130 adapter->AddObserver(&adapter_observer);
131
132 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _))
133 .Times(0);
134
135 adapter_callback.Run(adapter_path, true);
136
137 // Adapter should not be present yet.
138 EXPECT_FALSE(adapter->IsPresent());
139
140 // Tell the adapter the address now;
141 // BluetoothAdapter::Observer::AdapterPresentChanged now must be called.
142 adapter_properties.address.ReplaceValue(adapter_address);
143
144 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
145 .Times(1);
146
147 static_cast<BluetoothAdapterClient::Observer*>(adapter.get())
148 ->AdapterPropertyChanged(adapter_path,
149 adapter_properties.address.name());
150
151 // Adapter should be present with the given address.
152 EXPECT_TRUE(adapter->IsPresent());
153 EXPECT_EQ(adapter_address, adapter->address());
154 }
155
156 TEST_F(BluetoothAdapterTest, DefaultAdapterBecomesPresentWithAddress) {
157 const dbus::ObjectPath adapter_path("/fake/hci0");
158 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
159
160 // Create the default adapter instance;
161 // BluetoothManagerClient::DefaultAdapter will be called once, passing
162 // a callback to obtain the adapter path.
163 BluetoothManagerClient::AdapterCallback adapter_callback;
164 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
165 .WillOnce(SaveArg<0>(&adapter_callback));
166
167 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter();
168
169 // Call the adapter callback; make out it failed.
170 adapter_callback.Run(dbus::ObjectPath(""), false);
171
172 // Tell the adapter the default adapter changed;
173 // BluetoothAdapterClient::GetProperties will be called once to obtain
174 // the property set.
175 MockBluetoothAdapterClient::Properties adapter_properties;
176 adapter_properties.address.ReplaceValue(adapter_address);
177
178 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
179 .WillRepeatedly(Return(&adapter_properties));
180
181 // BluetoothAdapter::Observer::AdapterPresentChanged must be called.
182 MockBluetoothAdapter::Observer adapter_observer;
183 adapter->AddObserver(&adapter_observer);
184
185 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
186 .Times(1);
187
188 static_cast<BluetoothManagerClient::Observer*>(adapter.get())
189 ->DefaultAdapterChanged(adapter_path);
190
191 // Adapter should be present with the new address.
192 EXPECT_TRUE(adapter->IsPresent());
193 EXPECT_EQ(adapter_address, adapter->address());
194 }
195
196 TEST_F(BluetoothAdapterTest, DefaultAdapterReplacedWithAddress) {
197 const dbus::ObjectPath initial_adapter_path("/fake/hci0");
198 const dbus::ObjectPath new_adapter_path("/fake/hci1");
199 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE";
200 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE";
201
202 // Create the default adapter instance;
203 // BluetoothManagerClient::DefaultAdapter will be called once, passing
204 // a callback to obtain the adapter path.
205 BluetoothManagerClient::AdapterCallback adapter_callback;
206 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
207 .WillOnce(SaveArg<0>(&adapter_callback));
208
209 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter();
210
211 // Call the adapter callback;
212 // BluetoothAdapterClient::GetProperties will be called once to obtain
213 // the property set.
214 MockBluetoothAdapterClient::Properties initial_adapter_properties;
215 initial_adapter_properties.address.ReplaceValue(initial_adapter_address);
216
217 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path))
218 .WillRepeatedly(Return(&initial_adapter_properties));
219
220 adapter_callback.Run(initial_adapter_path, true);
221
222 // Tell the adapter the default adapter changed;
223 // BluetoothAdapterClient::GetProperties will be called once to obtain
224 // the property set.
225 MockBluetoothAdapterClient::Properties new_adapter_properties;
226 new_adapter_properties.address.ReplaceValue(new_adapter_address);
227
228 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path))
229 .WillRepeatedly(Return(&new_adapter_properties));
230
231 // BluetoothAdapter::Observer::AdapterPresentChanged must be called once
232 // with false to indicate the old adapter being removed and once with true
233 // to announce the new adapter.
234 MockBluetoothAdapter::Observer adapter_observer;
235 adapter->AddObserver(&adapter_observer);
236
237 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
238 .Times(1);
239 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
240 .Times(1);
241
242 static_cast<BluetoothManagerClient::Observer*>(adapter.get())
243 ->DefaultAdapterChanged(new_adapter_path);
244
245 // Adapter should be present with the new address.
246 EXPECT_TRUE(adapter->IsPresent());
247 EXPECT_EQ(new_adapter_address, adapter->address());
248 }
249
250 TEST_F(BluetoothAdapterTest, DefaultAdapterBecomesPresentWithoutAddress) {
251 const dbus::ObjectPath adapter_path("/fake/hci0");
252 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
253
254 // Create the default adapter instance;
255 // BluetoothManagerClient::DefaultAdapter will be called once, passing
256 // a callback to obtain the adapter path.
257 BluetoothManagerClient::AdapterCallback adapter_callback;
258 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
259 .WillOnce(SaveArg<0>(&adapter_callback));
260
261 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter();
262
263 // Call the adapter callback; make out it failed.
264 adapter_callback.Run(dbus::ObjectPath(""), false);
265
266 // Tell the adapter the default adapter changed;
267 // BluetoothAdapterClient::GetProperties will be called once to obtain
268 // the property set.
269 MockBluetoothAdapterClient::Properties adapter_properties;
270
271 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
272 .WillRepeatedly(Return(&adapter_properties));
273
274 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called.
275 MockBluetoothAdapter::Observer adapter_observer;
276 adapter->AddObserver(&adapter_observer);
277
278 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _))
279 .Times(0);
280
281 static_cast<BluetoothManagerClient::Observer*>(adapter.get())
282 ->DefaultAdapterChanged(adapter_path);
283
284 // Adapter should not be present yet.
285 EXPECT_FALSE(adapter->IsPresent());
286
287 // Tell the adapter the address now;
288 // BluetoothAdapter::Observer::AdapterPresentChanged now must be called.
289 adapter_properties.address.ReplaceValue(adapter_address);
290
291 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
292 .Times(1);
293
294 static_cast<BluetoothAdapterClient::Observer*>(adapter.get())
295 ->AdapterPropertyChanged(adapter_path,
296 adapter_properties.address.name());
297
298 // Adapter should be present with the new address.
299 EXPECT_TRUE(adapter->IsPresent());
300 EXPECT_EQ(adapter_address, adapter->address());
301 }
302
303 TEST_F(BluetoothAdapterTest, DefaultAdapterReplacedWithoutAddress) {
304 const dbus::ObjectPath initial_adapter_path("/fake/hci0");
305 const dbus::ObjectPath new_adapter_path("/fake/hci1");
306 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE";
307 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE";
308
309 // Create the default adapter instance;
310 // BluetoothManagerClient::DefaultAdapter will be called once, passing
311 // a callback to obtain the adapter path.
312 BluetoothManagerClient::AdapterCallback adapter_callback;
313 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
314 .WillOnce(SaveArg<0>(&adapter_callback));
315
316 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter();
317
318 // Call the adapter callback;
319 // BluetoothAdapterClient::GetProperties will be called once to obtain
320 // the property set.
321 MockBluetoothAdapterClient::Properties initial_adapter_properties;
322 initial_adapter_properties.address.ReplaceValue(initial_adapter_address);
323
324 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path))
325 .WillRepeatedly(Return(&initial_adapter_properties));
326
327 adapter_callback.Run(initial_adapter_path, true);
328
329 // Tell the adapter the default adapter changed;
330 // BluetoothAdapterClient::GetProperties will be called once to obtain
331 // the property set.
332 MockBluetoothAdapterClient::Properties new_adapter_properties;
333
334 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path))
335 .WillRepeatedly(Return(&new_adapter_properties));
336
337 // BluetoothAdapter::Observer::AdapterPresentChanged must be called to
338 // indicate the adapter has gone away.
339 MockBluetoothAdapter::Observer adapter_observer;
340 adapter->AddObserver(&adapter_observer);
341
342 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
343 .Times(1);
344
345 static_cast<BluetoothManagerClient::Observer*>(adapter.get())
346 ->DefaultAdapterChanged(new_adapter_path);
347
348 // Adapter should be now marked not present.
349 EXPECT_FALSE(adapter->IsPresent());
350
351 // Tell the adapter the address now;
352 // BluetoothAdapter::Observer::AdapterPresentChanged now must be called.
353 new_adapter_properties.address.ReplaceValue(new_adapter_address);
354
355 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
356 .Times(1);
357
358 static_cast<BluetoothAdapterClient::Observer*>(adapter.get())
359 ->AdapterPropertyChanged(new_adapter_path,
360 new_adapter_properties.address.name());
361
362 // Adapter should be present with the new address.
363 EXPECT_TRUE(adapter->IsPresent());
364 EXPECT_EQ(new_adapter_address, adapter->address());
365 }
366
367 TEST_F(BluetoothAdapterTest, DefaultAdapterRemoved) {
368 const dbus::ObjectPath adapter_path("/fake/hci0");
369 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
370
371 // Create the default adapter instance;
372 // BluetoothManagerClient::DefaultAdapter will be called once, passing
373 // a callback to obtain the adapter path.
374 BluetoothManagerClient::AdapterCallback adapter_callback;
375 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
376 .WillOnce(SaveArg<0>(&adapter_callback));
377
378 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter();
379
380 // Call the adapter callback;
381 // BluetoothAdapterClient::GetProperties will be called once to obtain
382 // the property set.
383 MockBluetoothAdapterClient::Properties adapter_properties;
384 adapter_properties.address.ReplaceValue(adapter_address);
385
386 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
387 .WillRepeatedly(Return(&adapter_properties));
388
389 adapter_callback.Run(adapter_path, true);
390
391 // Report that the adapter has been removed;
392 // BluetoothAdapter::Observer::AdapterPresentChanged will be called to
393 // indicate the adapter is no longer present.
394 MockBluetoothAdapter::Observer adapter_observer;
395 adapter->AddObserver(&adapter_observer);
396
397 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
398 .Times(1);
399
400 static_cast<BluetoothManagerClient::Observer*>(adapter.get())
401 ->AdapterRemoved(adapter_path);
402
403 // Adapter should be no longer present.
404 EXPECT_FALSE(adapter->IsPresent());
405 }
406
407 TEST_F(BluetoothAdapterTest, DefaultAdapterWithoutAddressRemoved) {
408 const dbus::ObjectPath adapter_path("/fake/hci0");
409
410 // Create the default adapter instance;
411 // BluetoothManagerClient::DefaultAdapter will be called once, passing
412 // a callback to obtain the adapter path.
413 BluetoothManagerClient::AdapterCallback adapter_callback;
414 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
415 .WillOnce(SaveArg<0>(&adapter_callback));
416
417 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter();
418
419 // Call the adapter callback;
420 // BluetoothAdapterClient::GetProperties will be called once to obtain
421 // the property set.
422 MockBluetoothAdapterClient::Properties adapter_properties;
423
424 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
425 .WillRepeatedly(Return(&adapter_properties));
426
427 adapter_callback.Run(adapter_path, true);
428
429 // Report that the adapter has been removed;
430 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called
431 // since we never should have announced it in the first place.
432 MockBluetoothAdapter::Observer adapter_observer;
433 adapter->AddObserver(&adapter_observer);
434
435 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _))
436 .Times(0);
437
438 static_cast<BluetoothManagerClient::Observer*>(adapter.get())
439 ->AdapterRemoved(adapter_path);
440
441 // Adapter should be still no longer present.
442 EXPECT_FALSE(adapter->IsPresent());
443 }
444
445 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698