• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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 "base/memory/scoped_vector.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
9 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
10 #include "chromeos/dbus/fake_bluetooth_device_client.h"
11 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
12 #include "chromeos/dbus/fake_bluetooth_input_client.h"
13 #include "chromeos/dbus/fake_dbus_thread_manager.h"
14 #include "dbus/object_path.h"
15 #include "device/bluetooth/bluetooth_adapter.h"
16 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
17 #include "device/bluetooth/bluetooth_adapter_factory.h"
18 #include "device/bluetooth/bluetooth_device.h"
19 #include "device/bluetooth/bluetooth_device_chromeos.h"
20 #include "device/bluetooth/bluetooth_discovery_session.h"
21 #include "device/bluetooth/bluetooth_pairing_chromeos.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/cros_system_api/dbus/service_constants.h"
24 
25 using device::BluetoothAdapter;
26 using device::BluetoothAdapterFactory;
27 using device::BluetoothDevice;
28 using device::BluetoothDiscoverySession;
29 using device::BluetoothUUID;
30 
31 namespace chromeos {
32 
33 namespace {
34 
35 class TestObserver : public BluetoothAdapter::Observer {
36  public:
TestObserver(scoped_refptr<BluetoothAdapter> adapter)37   TestObserver(scoped_refptr<BluetoothAdapter> adapter)
38       : present_changed_count_(0),
39         powered_changed_count_(0),
40         discoverable_changed_count_(0),
41         discovering_changed_count_(0),
42         last_present_(false),
43         last_powered_(false),
44         last_discovering_(false),
45         device_added_count_(0),
46         device_changed_count_(0),
47         device_removed_count_(0),
48         last_device_(NULL),
49         adapter_(adapter) {
50     adapter_->AddObserver(this);
51   }
52 
~TestObserver()53   virtual ~TestObserver() {
54     adapter_->RemoveObserver(this);
55   }
56 
AdapterPresentChanged(BluetoothAdapter * adapter,bool present)57   virtual void AdapterPresentChanged(BluetoothAdapter* adapter,
58                                      bool present) OVERRIDE {
59     EXPECT_EQ(adapter_, adapter);
60 
61     ++present_changed_count_;
62     last_present_ = present;
63   }
64 
AdapterPoweredChanged(BluetoothAdapter * adapter,bool powered)65   virtual void AdapterPoweredChanged(BluetoothAdapter* adapter,
66                                      bool powered) OVERRIDE {
67     EXPECT_EQ(adapter_, adapter);
68 
69     ++powered_changed_count_;
70     last_powered_ = powered;
71   }
72 
AdapterDiscoverableChanged(BluetoothAdapter * adapter,bool discoverable)73   virtual void AdapterDiscoverableChanged(BluetoothAdapter* adapter,
74                                           bool discoverable) OVERRIDE {
75     EXPECT_EQ(adapter_, adapter);
76 
77     ++discoverable_changed_count_;
78   }
79 
AdapterDiscoveringChanged(BluetoothAdapter * adapter,bool discovering)80   virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
81                                          bool discovering) OVERRIDE {
82     EXPECT_EQ(adapter_, adapter);
83 
84     ++discovering_changed_count_;
85     last_discovering_ = discovering;
86   }
87 
DeviceAdded(BluetoothAdapter * adapter,BluetoothDevice * device)88   virtual void DeviceAdded(BluetoothAdapter* adapter,
89                            BluetoothDevice* device) OVERRIDE {
90     EXPECT_EQ(adapter_, adapter);
91 
92     ++device_added_count_;
93     last_device_ = device;
94     last_device_address_ = device->GetAddress();
95 
96     QuitMessageLoop();
97   }
98 
DeviceChanged(BluetoothAdapter * adapter,BluetoothDevice * device)99   virtual void DeviceChanged(BluetoothAdapter* adapter,
100                              BluetoothDevice* device) OVERRIDE {
101     EXPECT_EQ(adapter_, adapter);
102 
103     ++device_changed_count_;
104     last_device_ = device;
105     last_device_address_ = device->GetAddress();
106 
107     QuitMessageLoop();
108   }
109 
DeviceRemoved(BluetoothAdapter * adapter,BluetoothDevice * device)110   virtual void DeviceRemoved(BluetoothAdapter* adapter,
111                              BluetoothDevice* device) OVERRIDE {
112     EXPECT_EQ(adapter_, adapter);
113 
114     ++device_removed_count_;
115     // Can't save device, it may be freed
116     last_device_address_ = device->GetAddress();
117 
118     QuitMessageLoop();
119   }
120 
121   int present_changed_count_;
122   int powered_changed_count_;
123   int discoverable_changed_count_;
124   int discovering_changed_count_;
125   bool last_present_;
126   bool last_powered_;
127   bool last_discovering_;
128   int device_added_count_;
129   int device_changed_count_;
130   int device_removed_count_;
131   BluetoothDevice* last_device_;
132   std::string last_device_address_;
133 
134  private:
135   // Some tests use a message loop since background processing is simulated;
136   // break out of those loops.
QuitMessageLoop()137   void QuitMessageLoop() {
138     if (base::MessageLoop::current() &&
139         base::MessageLoop::current()->is_running())
140       base::MessageLoop::current()->Quit();
141   }
142 
143   scoped_refptr<BluetoothAdapter> adapter_;
144 };
145 
146 }  // namespace
147 
148 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
149  public:
TestPairingDelegate()150   TestPairingDelegate()
151       : call_count_(0),
152         request_pincode_count_(0),
153         request_passkey_count_(0),
154         display_pincode_count_(0),
155         display_passkey_count_(0),
156         keys_entered_count_(0),
157         confirm_passkey_count_(0),
158         authorize_pairing_count_(0),
159         last_passkey_(9999999U),
160         last_entered_(999U) {}
~TestPairingDelegate()161   virtual ~TestPairingDelegate() {}
162 
RequestPinCode(BluetoothDevice * device)163   virtual void RequestPinCode(BluetoothDevice* device) OVERRIDE {
164     ++call_count_;
165     ++request_pincode_count_;
166     QuitMessageLoop();
167   }
168 
RequestPasskey(BluetoothDevice * device)169   virtual void RequestPasskey(BluetoothDevice* device) OVERRIDE {
170     ++call_count_;
171     ++request_passkey_count_;
172     QuitMessageLoop();
173   }
174 
DisplayPinCode(BluetoothDevice * device,const std::string & pincode)175   virtual void DisplayPinCode(BluetoothDevice* device,
176                               const std::string& pincode) OVERRIDE {
177     ++call_count_;
178     ++display_pincode_count_;
179     last_pincode_ = pincode;
180     QuitMessageLoop();
181   }
182 
DisplayPasskey(BluetoothDevice * device,uint32 passkey)183   virtual void DisplayPasskey(BluetoothDevice* device,
184                               uint32 passkey) OVERRIDE {
185     ++call_count_;
186     ++display_passkey_count_;
187     last_passkey_ = passkey;
188     QuitMessageLoop();
189   }
190 
KeysEntered(BluetoothDevice * device,uint32 entered)191   virtual void KeysEntered(BluetoothDevice* device, uint32 entered) OVERRIDE {
192     ++call_count_;
193     ++keys_entered_count_;
194     last_entered_ = entered;
195     QuitMessageLoop();
196   }
197 
ConfirmPasskey(BluetoothDevice * device,uint32 passkey)198   virtual void ConfirmPasskey(BluetoothDevice* device,
199                               uint32 passkey) OVERRIDE {
200     ++call_count_;
201     ++confirm_passkey_count_;
202     last_passkey_ = passkey;
203     QuitMessageLoop();
204   }
205 
AuthorizePairing(BluetoothDevice * device)206   virtual void AuthorizePairing(BluetoothDevice* device) OVERRIDE {
207     ++call_count_;
208     ++authorize_pairing_count_;
209     QuitMessageLoop();
210   }
211 
212   int call_count_;
213   int request_pincode_count_;
214   int request_passkey_count_;
215   int display_pincode_count_;
216   int display_passkey_count_;
217   int keys_entered_count_;
218   int confirm_passkey_count_;
219   int authorize_pairing_count_;
220   uint32 last_passkey_;
221   uint32 last_entered_;
222   std::string last_pincode_;
223 
224   private:
225    // Some tests use a message loop since background processing is simulated;
226    // break out of those loops.
QuitMessageLoop()227    void QuitMessageLoop() {
228      if (base::MessageLoop::current() &&
229          base::MessageLoop::current()->is_running())
230        base::MessageLoop::current()->Quit();
231    }
232 };
233 
234 class BluetoothChromeOSTest : public testing::Test {
235  public:
SetUp()236   virtual void SetUp() {
237     FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager;
238     fake_bluetooth_adapter_client_ = new FakeBluetoothAdapterClient;
239     fake_dbus_thread_manager->SetBluetoothAdapterClient(
240         scoped_ptr<BluetoothAdapterClient>(fake_bluetooth_adapter_client_));
241     fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient;
242     fake_dbus_thread_manager->SetBluetoothDeviceClient(
243         scoped_ptr<BluetoothDeviceClient>(fake_bluetooth_device_client_));
244     fake_dbus_thread_manager->SetBluetoothInputClient(
245         scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
246     fake_dbus_thread_manager->SetBluetoothAgentManagerClient(
247         scoped_ptr<BluetoothAgentManagerClient>(
248             new FakeBluetoothAgentManagerClient));
249     fake_dbus_thread_manager->SetBluetoothGattServiceClient(
250         scoped_ptr<BluetoothGattServiceClient>(
251             new FakeBluetoothGattServiceClient));
252     DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager);
253 
254     fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10);
255 
256     callback_count_ = 0;
257     error_callback_count_ = 0;
258     last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN;
259     last_client_error_ = "";
260   }
261 
TearDown()262   virtual void TearDown() {
263     for (ScopedVector<BluetoothDiscoverySession>::iterator iter =
264             discovery_sessions_.begin();
265          iter != discovery_sessions_.end();
266          ++iter) {
267       BluetoothDiscoverySession* session = *iter;
268       if (!session->IsActive())
269         continue;
270       callback_count_ = 0;
271       session->Stop(
272           base::Bind(&BluetoothChromeOSTest::Callback,
273                      base::Unretained(this)),
274           base::Bind(&BluetoothChromeOSTest::ErrorCallback,
275                      base::Unretained(this)));
276       message_loop_.Run();
277       ASSERT_EQ(1, callback_count_);
278     }
279     discovery_sessions_.clear();
280     adapter_ = NULL;
281     DBusThreadManager::Shutdown();
282   }
283 
284   // Generic callbacks
Callback()285   void Callback() {
286     ++callback_count_;
287     QuitMessageLoop();
288   }
289 
DiscoverySessionCallback(scoped_ptr<BluetoothDiscoverySession> discovery_session)290   void DiscoverySessionCallback(
291       scoped_ptr<BluetoothDiscoverySession> discovery_session) {
292     ++callback_count_;
293     discovery_sessions_.push_back(discovery_session.release());
294     QuitMessageLoop();
295   }
296 
ErrorCallback()297   void ErrorCallback() {
298     ++error_callback_count_;
299     QuitMessageLoop();
300   }
301 
DBusErrorCallback(const std::string & error_name,const std::string & error_message)302   void DBusErrorCallback(const std::string& error_name,
303                          const std::string& error_message) {
304     ++error_callback_count_;
305     last_client_error_ = error_name;
306     QuitMessageLoop();
307   }
308 
ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error)309   void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) {
310     ++error_callback_count_;
311     last_connect_error_ = error;
312   }
313 
314   // Call to fill the adapter_ member with a BluetoothAdapter instance.
GetAdapter()315   void GetAdapter() {
316     adapter_ = new BluetoothAdapterChromeOS();
317     ASSERT_TRUE(adapter_.get() != NULL);
318     ASSERT_TRUE(adapter_->IsInitialized());
319   }
320 
321   // Run a discovery phase until the named device is detected, or if the named
322   // device is not created, the discovery process ends without finding it.
323   //
324   // The correct behavior of discovery is tested by the "Discovery" test case
325   // without using this function.
DiscoverDevice(const std::string & address)326   void DiscoverDevice(const std::string& address) {
327     ASSERT_TRUE(adapter_.get() != NULL);
328     ASSERT_TRUE(base::MessageLoop::current() != NULL);
329     fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
330 
331     TestObserver observer(adapter_);
332 
333     adapter_->SetPowered(
334         true,
335         base::Bind(&BluetoothChromeOSTest::Callback,
336                    base::Unretained(this)),
337         base::Bind(&BluetoothChromeOSTest::ErrorCallback,
338                    base::Unretained(this)));
339     adapter_->StartDiscoverySession(
340         base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
341                    base::Unretained(this)),
342         base::Bind(&BluetoothChromeOSTest::ErrorCallback,
343                    base::Unretained(this)));
344     base::MessageLoop::current()->Run();
345     ASSERT_EQ(2, callback_count_);
346     ASSERT_EQ(0, error_callback_count_);
347     ASSERT_EQ((size_t)1, discovery_sessions_.size());
348     ASSERT_TRUE(discovery_sessions_[0]->IsActive());
349     callback_count_ = 0;
350 
351     ASSERT_TRUE(adapter_->IsPowered());
352     ASSERT_TRUE(adapter_->IsDiscovering());
353 
354     while (!observer.device_removed_count_ &&
355            observer.last_device_address_ != address)
356       base::MessageLoop::current()->Run();
357 
358     discovery_sessions_[0]->Stop(
359         base::Bind(&BluetoothChromeOSTest::Callback,
360                    base::Unretained(this)),
361         base::Bind(&BluetoothChromeOSTest::ErrorCallback,
362                    base::Unretained(this)));
363     base::MessageLoop::current()->Run();
364     ASSERT_EQ(1, callback_count_);
365     ASSERT_EQ(0, error_callback_count_);
366     callback_count_ = 0;
367 
368     ASSERT_FALSE(adapter_->IsDiscovering());
369   }
370 
371   // Run a discovery phase so we have devices that can be paired with.
DiscoverDevices()372   void DiscoverDevices() {
373     // Pass an invalid address for the device so that the discovery process
374     // completes with all devices.
375     DiscoverDevice("does not exist");
376   }
377 
378  protected:
379   base::MessageLoop message_loop_;
380   FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
381   FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
382   scoped_refptr<BluetoothAdapter> adapter_;
383 
384   int callback_count_;
385   int error_callback_count_;
386   enum BluetoothDevice::ConnectErrorCode last_connect_error_;
387   std::string last_client_error_;
388   ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
389 
390  private:
391   // Some tests use a message loop since background processing is simulated;
392   // break out of those loops.
QuitMessageLoop()393   void QuitMessageLoop() {
394     if (base::MessageLoop::current() &&
395         base::MessageLoop::current()->is_running())
396       base::MessageLoop::current()->Quit();
397   }
398 };
399 
TEST_F(BluetoothChromeOSTest,AlreadyPresent)400 TEST_F(BluetoothChromeOSTest, AlreadyPresent) {
401   GetAdapter();
402 
403   // This verifies that the class gets the list of adapters when created;
404   // and initializes with an existing adapter if there is one.
405   EXPECT_TRUE(adapter_->IsPresent());
406   EXPECT_FALSE(adapter_->IsPowered());
407   EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
408             adapter_->GetAddress());
409   EXPECT_FALSE(adapter_->IsDiscovering());
410 
411   // There should be a device
412   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
413   EXPECT_EQ(1U, devices.size());
414   EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
415             devices[0]->GetAddress());
416 }
417 
TEST_F(BluetoothChromeOSTest,BecomePresent)418 TEST_F(BluetoothChromeOSTest, BecomePresent) {
419   fake_bluetooth_adapter_client_->SetVisible(false);
420   GetAdapter();
421   ASSERT_FALSE(adapter_->IsPresent());
422 
423   // Install an observer; expect the AdapterPresentChanged to be called
424   // with true, and IsPresent() to return true.
425   TestObserver observer(adapter_);
426 
427   fake_bluetooth_adapter_client_->SetVisible(true);
428 
429   EXPECT_EQ(1, observer.present_changed_count_);
430   EXPECT_TRUE(observer.last_present_);
431 
432   EXPECT_TRUE(adapter_->IsPresent());
433 
434   // We should have had a device announced.
435   EXPECT_EQ(1, observer.device_added_count_);
436   EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
437             observer.last_device_address_);
438 
439   // Other callbacks shouldn't be called if the values are false.
440   EXPECT_EQ(0, observer.powered_changed_count_);
441   EXPECT_EQ(0, observer.discovering_changed_count_);
442   EXPECT_FALSE(adapter_->IsPowered());
443   EXPECT_FALSE(adapter_->IsDiscovering());
444 }
445 
TEST_F(BluetoothChromeOSTest,BecomeNotPresent)446 TEST_F(BluetoothChromeOSTest, BecomeNotPresent) {
447   GetAdapter();
448   ASSERT_TRUE(adapter_->IsPresent());
449 
450   // Install an observer; expect the AdapterPresentChanged to be called
451   // with false, and IsPresent() to return false.
452   TestObserver observer(adapter_);
453 
454   fake_bluetooth_adapter_client_->SetVisible(false);
455 
456   EXPECT_EQ(1, observer.present_changed_count_);
457   EXPECT_FALSE(observer.last_present_);
458 
459   EXPECT_FALSE(adapter_->IsPresent());
460 
461   // We should have had a device removed.
462   EXPECT_EQ(1, observer.device_removed_count_);
463   EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
464             observer.last_device_address_);
465 
466   // Other callbacks shouldn't be called since the values are false.
467   EXPECT_EQ(0, observer.powered_changed_count_);
468   EXPECT_EQ(0, observer.discovering_changed_count_);
469   EXPECT_FALSE(adapter_->IsPowered());
470   EXPECT_FALSE(adapter_->IsDiscovering());
471 }
472 
TEST_F(BluetoothChromeOSTest,SecondAdapter)473 TEST_F(BluetoothChromeOSTest, SecondAdapter) {
474   GetAdapter();
475   ASSERT_TRUE(adapter_->IsPresent());
476 
477   // Install an observer, then add a second adapter. Nothing should change,
478   // we ignore the second adapter.
479   TestObserver observer(adapter_);
480 
481   fake_bluetooth_adapter_client_->SetSecondVisible(true);
482 
483   EXPECT_EQ(0, observer.present_changed_count_);
484 
485   EXPECT_TRUE(adapter_->IsPresent());
486   EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
487             adapter_->GetAddress());
488 
489   // Try removing the first adapter, we should now act as if the adapter
490   // is no longer present rather than fall back to the second.
491   fake_bluetooth_adapter_client_->SetVisible(false);
492 
493   EXPECT_EQ(1, observer.present_changed_count_);
494   EXPECT_FALSE(observer.last_present_);
495 
496   EXPECT_FALSE(adapter_->IsPresent());
497 
498   // We should have had a device removed.
499   EXPECT_EQ(1, observer.device_removed_count_);
500   EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
501             observer.last_device_address_);
502 
503   // Other callbacks shouldn't be called since the values are false.
504   EXPECT_EQ(0, observer.powered_changed_count_);
505   EXPECT_EQ(0, observer.discovering_changed_count_);
506   EXPECT_FALSE(adapter_->IsPowered());
507   EXPECT_FALSE(adapter_->IsDiscovering());
508 
509   observer.device_removed_count_ = 0;
510 
511   // Removing the second adapter shouldn't set anything either.
512   fake_bluetooth_adapter_client_->SetSecondVisible(false);
513 
514   EXPECT_EQ(0, observer.device_removed_count_);
515   EXPECT_EQ(0, observer.powered_changed_count_);
516   EXPECT_EQ(0, observer.discovering_changed_count_);
517 }
518 
TEST_F(BluetoothChromeOSTest,BecomePowered)519 TEST_F(BluetoothChromeOSTest, BecomePowered) {
520   GetAdapter();
521   ASSERT_FALSE(adapter_->IsPowered());
522 
523   // Install an observer; expect the AdapterPoweredChanged to be called
524   // with true, and IsPowered() to return true.
525   TestObserver observer(adapter_);
526 
527   adapter_->SetPowered(
528       true,
529       base::Bind(&BluetoothChromeOSTest::Callback,
530                  base::Unretained(this)),
531       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
532                  base::Unretained(this)));
533   EXPECT_EQ(1, callback_count_);
534   EXPECT_EQ(0, error_callback_count_);
535 
536   EXPECT_EQ(1, observer.powered_changed_count_);
537   EXPECT_TRUE(observer.last_powered_);
538 
539   EXPECT_TRUE(adapter_->IsPowered());
540 }
541 
TEST_F(BluetoothChromeOSTest,BecomeNotPowered)542 TEST_F(BluetoothChromeOSTest, BecomeNotPowered) {
543   GetAdapter();
544   adapter_->SetPowered(
545       true,
546       base::Bind(&BluetoothChromeOSTest::Callback,
547                  base::Unretained(this)),
548       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
549                  base::Unretained(this)));
550   EXPECT_EQ(1, callback_count_);
551   EXPECT_EQ(0, error_callback_count_);
552   callback_count_ = 0;
553 
554   ASSERT_TRUE(adapter_->IsPowered());
555 
556   // Install an observer; expect the AdapterPoweredChanged to be called
557   // with false, and IsPowered() to return false.
558   TestObserver observer(adapter_);
559 
560   adapter_->SetPowered(
561       false,
562       base::Bind(&BluetoothChromeOSTest::Callback,
563                  base::Unretained(this)),
564       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
565                  base::Unretained(this)));
566   EXPECT_EQ(1, callback_count_);
567   EXPECT_EQ(0, error_callback_count_);
568 
569   EXPECT_EQ(1, observer.powered_changed_count_);
570   EXPECT_FALSE(observer.last_powered_);
571 
572   EXPECT_FALSE(adapter_->IsPowered());
573 }
574 
TEST_F(BluetoothChromeOSTest,ChangeAdapterName)575 TEST_F(BluetoothChromeOSTest, ChangeAdapterName) {
576   GetAdapter();
577 
578   static const std::string new_name(".__.");
579 
580   adapter_->SetName(
581       new_name,
582       base::Bind(&BluetoothChromeOSTest::Callback,
583                  base::Unretained(this)),
584       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
585                  base::Unretained(this)));
586   EXPECT_EQ(1, callback_count_);
587   EXPECT_EQ(0, error_callback_count_);
588 
589   EXPECT_EQ(new_name, adapter_->GetName());
590 }
591 
TEST_F(BluetoothChromeOSTest,BecomeDiscoverable)592 TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) {
593   GetAdapter();
594   ASSERT_FALSE(adapter_->IsDiscoverable());
595 
596   // Install an observer; expect the AdapterDiscoverableChanged to be called
597   // with true, and IsDiscoverable() to return true.
598   TestObserver observer(adapter_);
599 
600   adapter_->SetDiscoverable(
601       true,
602       base::Bind(&BluetoothChromeOSTest::Callback,
603                  base::Unretained(this)),
604       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
605                  base::Unretained(this)));
606   EXPECT_EQ(1, callback_count_);
607   EXPECT_EQ(0, error_callback_count_);
608 
609   EXPECT_EQ(1, observer.discoverable_changed_count_);
610 
611   EXPECT_TRUE(adapter_->IsDiscoverable());
612 }
613 
TEST_F(BluetoothChromeOSTest,BecomeNotDiscoverable)614 TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) {
615   GetAdapter();
616   adapter_->SetDiscoverable(
617       true,
618       base::Bind(&BluetoothChromeOSTest::Callback,
619                  base::Unretained(this)),
620       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
621                  base::Unretained(this)));
622   EXPECT_EQ(1, callback_count_);
623   EXPECT_EQ(0, error_callback_count_);
624   callback_count_ = 0;
625 
626   ASSERT_TRUE(adapter_->IsDiscoverable());
627 
628   // Install an observer; expect the AdapterDiscoverableChanged to be called
629   // with false, and IsDiscoverable() to return false.
630   TestObserver observer(adapter_);
631 
632   adapter_->SetDiscoverable(
633       false,
634       base::Bind(&BluetoothChromeOSTest::Callback,
635                  base::Unretained(this)),
636       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
637                  base::Unretained(this)));
638   EXPECT_EQ(1, callback_count_);
639   EXPECT_EQ(0, error_callback_count_);
640 
641   EXPECT_EQ(1, observer.discoverable_changed_count_);
642 
643   EXPECT_FALSE(adapter_->IsDiscoverable());
644 }
645 
TEST_F(BluetoothChromeOSTest,StopDiscovery)646 TEST_F(BluetoothChromeOSTest, StopDiscovery) {
647   GetAdapter();
648 
649   adapter_->SetPowered(
650       true,
651       base::Bind(&BluetoothChromeOSTest::Callback,
652                  base::Unretained(this)),
653       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
654                  base::Unretained(this)));
655   adapter_->StartDiscoverySession(
656       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
657                  base::Unretained(this)),
658       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
659                  base::Unretained(this)));
660   message_loop_.Run();
661   EXPECT_EQ(2, callback_count_);
662   EXPECT_EQ(0, error_callback_count_);
663   callback_count_ = 0;
664 
665   ASSERT_TRUE(adapter_->IsPowered());
666   ASSERT_TRUE(adapter_->IsDiscovering());
667   ASSERT_EQ((size_t)1, discovery_sessions_.size());
668   ASSERT_TRUE(discovery_sessions_[0]->IsActive());
669 
670   // Install an observer; aside from the callback, expect the
671   // AdapterDiscoveringChanged method to be called and no longer to be
672   // discovering,
673   TestObserver observer(adapter_);
674 
675   discovery_sessions_[0]->Stop(
676       base::Bind(&BluetoothChromeOSTest::Callback,
677                  base::Unretained(this)),
678       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
679                  base::Unretained(this)));
680   message_loop_.Run();
681   EXPECT_EQ(1, callback_count_);
682   EXPECT_EQ(0, error_callback_count_);
683 
684   EXPECT_EQ(1, observer.discovering_changed_count_);
685   EXPECT_FALSE(observer.last_discovering_);
686 
687   EXPECT_FALSE(adapter_->IsDiscovering());
688 }
689 
TEST_F(BluetoothChromeOSTest,Discovery)690 TEST_F(BluetoothChromeOSTest, Discovery) {
691   // Test a simulated discovery session.
692   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
693   GetAdapter();
694 
695   TestObserver observer(adapter_);
696 
697   adapter_->SetPowered(
698       true,
699       base::Bind(&BluetoothChromeOSTest::Callback,
700                  base::Unretained(this)),
701       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
702                  base::Unretained(this)));
703   adapter_->StartDiscoverySession(
704       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
705                  base::Unretained(this)),
706       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
707                  base::Unretained(this)));
708   message_loop_.Run();
709   EXPECT_EQ(2, callback_count_);
710   EXPECT_EQ(0, error_callback_count_);
711   callback_count_ = 0;
712 
713   ASSERT_TRUE(adapter_->IsPowered());
714   ASSERT_TRUE(adapter_->IsDiscovering());
715   ASSERT_EQ((size_t)1, discovery_sessions_.size());
716   ASSERT_TRUE(discovery_sessions_[0]->IsActive());
717 
718   // First two devices to appear.
719   message_loop_.Run();
720 
721   EXPECT_EQ(2, observer.device_added_count_);
722   EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress,
723             observer.last_device_address_);
724 
725   // Next we should get another two devices...
726   message_loop_.Run();
727   EXPECT_EQ(4, observer.device_added_count_);
728 
729   // Okay, let's run forward until a device is actually removed...
730   while (!observer.device_removed_count_)
731     message_loop_.Run();
732 
733   EXPECT_EQ(1, observer.device_removed_count_);
734   EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress,
735             observer.last_device_address_);
736 }
737 
TEST_F(BluetoothChromeOSTest,PoweredAndDiscovering)738 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) {
739   GetAdapter();
740   adapter_->SetPowered(
741       true,
742       base::Bind(&BluetoothChromeOSTest::Callback,
743                  base::Unretained(this)),
744       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
745                  base::Unretained(this)));
746   adapter_->StartDiscoverySession(
747       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
748                  base::Unretained(this)),
749       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
750                  base::Unretained(this)));
751   message_loop_.Run();
752   EXPECT_EQ(2, callback_count_);
753   EXPECT_EQ(0, error_callback_count_);
754   callback_count_ = 0;
755   ASSERT_EQ((size_t)1, discovery_sessions_.size());
756   ASSERT_TRUE(discovery_sessions_[0]->IsActive());
757 
758   // Stop the timers that the simulation uses
759   fake_bluetooth_device_client_->EndDiscoverySimulation(
760       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
761 
762   ASSERT_TRUE(adapter_->IsPowered());
763   ASSERT_TRUE(adapter_->IsDiscovering());
764 
765   fake_bluetooth_adapter_client_->SetVisible(false);
766   ASSERT_FALSE(adapter_->IsPresent());
767   ASSERT_FALSE(discovery_sessions_[0]->IsActive());
768 
769   // Install an observer; expect the AdapterPresentChanged,
770   // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
771   // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
772   // return true.
773   TestObserver observer(adapter_);
774 
775   fake_bluetooth_adapter_client_->SetVisible(true);
776 
777   EXPECT_EQ(1, observer.present_changed_count_);
778   EXPECT_TRUE(observer.last_present_);
779   EXPECT_TRUE(adapter_->IsPresent());
780 
781   EXPECT_EQ(1, observer.powered_changed_count_);
782   EXPECT_TRUE(observer.last_powered_);
783   EXPECT_TRUE(adapter_->IsPowered());
784 
785   EXPECT_EQ(1, observer.discovering_changed_count_);
786   EXPECT_TRUE(observer.last_discovering_);
787   EXPECT_TRUE(adapter_->IsDiscovering());
788 
789   observer.present_changed_count_ = 0;
790   observer.powered_changed_count_ = 0;
791   observer.discovering_changed_count_ = 0;
792 
793   // Now mark the adapter not present again. Expect the methods to be called
794   // again, to reset the properties back to false
795   fake_bluetooth_adapter_client_->SetVisible(false);
796 
797   EXPECT_EQ(1, observer.present_changed_count_);
798   EXPECT_FALSE(observer.last_present_);
799   EXPECT_FALSE(adapter_->IsPresent());
800 
801   EXPECT_EQ(1, observer.powered_changed_count_);
802   EXPECT_FALSE(observer.last_powered_);
803   EXPECT_FALSE(adapter_->IsPowered());
804 
805   EXPECT_EQ(1, observer.discovering_changed_count_);
806   EXPECT_FALSE(observer.last_discovering_);
807   EXPECT_FALSE(adapter_->IsDiscovering());
808 }
809 
810 // This unit test asserts that the basic reference counting logic works
811 // correctly for discovery requests done via the BluetoothAdapter.
TEST_F(BluetoothChromeOSTest,MultipleDiscoverySessions)812 TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) {
813   GetAdapter();
814   adapter_->SetPowered(
815       true,
816       base::Bind(&BluetoothChromeOSTest::Callback,
817                  base::Unretained(this)),
818       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
819                  base::Unretained(this)));
820   EXPECT_EQ(1, callback_count_);
821   EXPECT_EQ(0, error_callback_count_);
822   EXPECT_TRUE(adapter_->IsPowered());
823   callback_count_ = 0;
824 
825   TestObserver observer(adapter_);
826 
827   EXPECT_EQ(0, observer.discovering_changed_count_);
828   EXPECT_FALSE(observer.last_discovering_);
829   EXPECT_FALSE(adapter_->IsDiscovering());
830 
831   // Request device discovery 3 times.
832   for (int i = 0; i < 3; i++) {
833     adapter_->StartDiscoverySession(
834       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
835                  base::Unretained(this)),
836       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
837                  base::Unretained(this)));
838   }
839   // Run only once, as there should have been one D-Bus call.
840   message_loop_.Run();
841 
842   // The observer should have received the discovering changed event exactly
843   // once, the success callback should have been called 3 times and the adapter
844   // should be discovering.
845   EXPECT_EQ(1, observer.discovering_changed_count_);
846   EXPECT_EQ(3, callback_count_);
847   EXPECT_EQ(0, error_callback_count_);
848   EXPECT_TRUE(observer.last_discovering_);
849   EXPECT_TRUE(adapter_->IsDiscovering());
850   ASSERT_EQ((size_t)3, discovery_sessions_.size());
851 
852   // Request to stop discovery twice.
853   for (int i = 0; i < 2; i++) {
854     discovery_sessions_[i]->Stop(
855       base::Bind(&BluetoothChromeOSTest::Callback,
856                  base::Unretained(this)),
857       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
858                  base::Unretained(this)));
859   }
860 
861   // The observer should have received no additional discovering changed events,
862   // the success callback should have been called 2 times and the adapter should
863   // still be discovering.
864   EXPECT_EQ(1, observer.discovering_changed_count_);
865   EXPECT_EQ(5, callback_count_);
866   EXPECT_EQ(0, error_callback_count_);
867   EXPECT_TRUE(observer.last_discovering_);
868   EXPECT_TRUE(adapter_->IsDiscovering());
869   EXPECT_TRUE(adapter_->IsDiscovering());
870   EXPECT_FALSE(discovery_sessions_[0]->IsActive());
871   EXPECT_FALSE(discovery_sessions_[1]->IsActive());
872   EXPECT_TRUE(discovery_sessions_[2]->IsActive());
873 
874   // Request device discovery 3 times.
875   for (int i = 0; i < 3; i++) {
876     adapter_->StartDiscoverySession(
877       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
878                  base::Unretained(this)),
879       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
880                  base::Unretained(this)));
881   }
882 
883   // The observer should have received no additional discovering changed events,
884   // the success callback should have been called 3 times and the adapter should
885   // still be discovering.
886   EXPECT_EQ(1, observer.discovering_changed_count_);
887   EXPECT_EQ(8, callback_count_);
888   EXPECT_EQ(0, error_callback_count_);
889   EXPECT_TRUE(observer.last_discovering_);
890   EXPECT_TRUE(adapter_->IsDiscovering());
891   ASSERT_EQ((size_t)6, discovery_sessions_.size());
892 
893   // Request to stop discovery 4 times.
894   for (int i = 2; i < 6; i++) {
895     discovery_sessions_[i]->Stop(
896       base::Bind(&BluetoothChromeOSTest::Callback,
897                  base::Unretained(this)),
898       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
899                  base::Unretained(this)));
900   }
901   // Run only once, as there should have been one D-Bus call.
902   message_loop_.Run();
903 
904   // The observer should have received the discovering changed event exactly
905   // once, the success callback should have been called 4 times and the adapter
906   // should no longer be discovering.
907   EXPECT_EQ(2, observer.discovering_changed_count_);
908   EXPECT_EQ(12, callback_count_);
909   EXPECT_EQ(0, error_callback_count_);
910   EXPECT_FALSE(observer.last_discovering_);
911   EXPECT_FALSE(adapter_->IsDiscovering());
912 
913   // All discovery sessions should be inactive.
914   for (int i = 0; i < 6; i++)
915     EXPECT_FALSE(discovery_sessions_[i]->IsActive());
916 
917   // Request to stop discovery on of the inactive sessions.
918   discovery_sessions_[0]->Stop(
919     base::Bind(&BluetoothChromeOSTest::Callback,
920                base::Unretained(this)),
921     base::Bind(&BluetoothChromeOSTest::ErrorCallback,
922                base::Unretained(this)));
923 
924   // The call should have failed.
925   EXPECT_EQ(2, observer.discovering_changed_count_);
926   EXPECT_EQ(12, callback_count_);
927   EXPECT_EQ(1, error_callback_count_);
928   EXPECT_FALSE(observer.last_discovering_);
929   EXPECT_FALSE(adapter_->IsDiscovering());
930 }
931 
932 // This unit test asserts that the reference counting logic works correctly in
933 // the cases when the adapter gets reset and D-Bus calls are made outside of
934 // the BluetoothAdapter.
TEST_F(BluetoothChromeOSTest,UnexpectedChangesDuringMultipleDiscoverySessions)935 TEST_F(BluetoothChromeOSTest,
936        UnexpectedChangesDuringMultipleDiscoverySessions) {
937   GetAdapter();
938   adapter_->SetPowered(
939       true,
940       base::Bind(&BluetoothChromeOSTest::Callback,
941                  base::Unretained(this)),
942       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
943                  base::Unretained(this)));
944   EXPECT_EQ(1, callback_count_);
945   EXPECT_EQ(0, error_callback_count_);
946   EXPECT_TRUE(adapter_->IsPowered());
947   callback_count_ = 0;
948 
949   TestObserver observer(adapter_);
950 
951   EXPECT_EQ(0, observer.discovering_changed_count_);
952   EXPECT_FALSE(observer.last_discovering_);
953   EXPECT_FALSE(adapter_->IsDiscovering());
954 
955   // Request device discovery 3 times.
956   for (int i = 0; i < 3; i++) {
957     adapter_->StartDiscoverySession(
958       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
959                  base::Unretained(this)),
960       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
961                  base::Unretained(this)));
962   }
963   // Run only once, as there should have been one D-Bus call.
964   message_loop_.Run();
965 
966   // The observer should have received the discovering changed event exactly
967   // once, the success callback should have been called 3 times and the adapter
968   // should be discovering.
969   EXPECT_EQ(1, observer.discovering_changed_count_);
970   EXPECT_EQ(3, callback_count_);
971   EXPECT_EQ(0, error_callback_count_);
972   EXPECT_TRUE(observer.last_discovering_);
973   EXPECT_TRUE(adapter_->IsDiscovering());
974   ASSERT_EQ((size_t)3, discovery_sessions_.size());
975 
976   for (int i = 0; i < 3; i++)
977     EXPECT_TRUE(discovery_sessions_[i]->IsActive());
978 
979   // Stop the timers that the simulation uses
980   fake_bluetooth_device_client_->EndDiscoverySimulation(
981       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
982 
983   ASSERT_TRUE(adapter_->IsPowered());
984   ASSERT_TRUE(adapter_->IsDiscovering());
985 
986   // Stop device discovery behind the adapter. The adapter and the observer
987   // should be notified of the change and the reference count should be reset.
988   // Even though FakeBluetoothAdapterClient does its own reference counting and
989   // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
990   // FakeBluetoothAdapterClient's count should be only 1 and a single call to
991   // FakeBluetoothAdapterClient::StopDiscovery should work.
992   fake_bluetooth_adapter_client_->StopDiscovery(
993       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
994       base::Bind(&BluetoothChromeOSTest::Callback,
995                  base::Unretained(this)),
996       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
997                  base::Unretained(this)));
998   message_loop_.Run();
999   EXPECT_EQ(2, observer.discovering_changed_count_);
1000   EXPECT_EQ(4, callback_count_);
1001   EXPECT_EQ(0, error_callback_count_);
1002   EXPECT_FALSE(observer.last_discovering_);
1003   EXPECT_FALSE(adapter_->IsDiscovering());
1004 
1005   // All discovery session instances should have been updated.
1006   for (int i = 0; i < 3; i++)
1007     EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1008   discovery_sessions_.clear();
1009 
1010   // It should be possible to successfully start discovery.
1011   for (int i = 0; i < 2; i++) {
1012     adapter_->StartDiscoverySession(
1013       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1014                  base::Unretained(this)),
1015       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1016                  base::Unretained(this)));
1017   }
1018   // Run only once, as there should have been one D-Bus call.
1019   message_loop_.Run();
1020   EXPECT_EQ(3, observer.discovering_changed_count_);
1021   EXPECT_EQ(6, callback_count_);
1022   EXPECT_EQ(0, error_callback_count_);
1023   EXPECT_TRUE(observer.last_discovering_);
1024   EXPECT_TRUE(adapter_->IsDiscovering());
1025   ASSERT_EQ((size_t)2, discovery_sessions_.size());
1026 
1027   for (int i = 0; i < 2; i++)
1028     EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1029 
1030   fake_bluetooth_device_client_->EndDiscoverySimulation(
1031       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1032 
1033   // Make the adapter disappear and appear. This will make it come back as
1034   // discovering. When this happens, the reference count should become and
1035   // remain 0 as no new request was made through the BluetoothAdapter.
1036   fake_bluetooth_adapter_client_->SetVisible(false);
1037   ASSERT_FALSE(adapter_->IsPresent());
1038   EXPECT_EQ(4, observer.discovering_changed_count_);
1039   EXPECT_EQ(6, callback_count_);
1040   EXPECT_EQ(0, error_callback_count_);
1041   EXPECT_FALSE(observer.last_discovering_);
1042   EXPECT_FALSE(adapter_->IsDiscovering());
1043 
1044   for (int i = 0; i < 2; i++)
1045     EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1046   discovery_sessions_.clear();
1047 
1048   fake_bluetooth_adapter_client_->SetVisible(true);
1049   ASSERT_TRUE(adapter_->IsPresent());
1050   EXPECT_EQ(5, observer.discovering_changed_count_);
1051   EXPECT_EQ(6, callback_count_);
1052   EXPECT_EQ(0, error_callback_count_);
1053   EXPECT_TRUE(observer.last_discovering_);
1054   EXPECT_TRUE(adapter_->IsDiscovering());
1055 
1056   // Start and stop discovery. At this point, FakeBluetoothAdapterClient has
1057   // a reference count that is equal to 1. Pretend that this was done by an
1058   // application other than us. Starting and stopping discovery will succeed
1059   // but it won't cause the discovery state to change.
1060   adapter_->StartDiscoverySession(
1061     base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1062                base::Unretained(this)),
1063     base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1064                base::Unretained(this)));
1065   message_loop_.Run();  // Run the loop, as there should have been a D-Bus call.
1066   EXPECT_EQ(5, observer.discovering_changed_count_);
1067   EXPECT_EQ(7, callback_count_);
1068   EXPECT_EQ(0, error_callback_count_);
1069   EXPECT_TRUE(observer.last_discovering_);
1070   EXPECT_TRUE(adapter_->IsDiscovering());
1071   ASSERT_EQ((size_t)1, discovery_sessions_.size());
1072   EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1073 
1074   discovery_sessions_[0]->Stop(
1075     base::Bind(&BluetoothChromeOSTest::Callback,
1076                base::Unretained(this)),
1077     base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1078                base::Unretained(this)));
1079   message_loop_.Run();  // Run the loop, as there should have been a D-Bus call.
1080   EXPECT_EQ(5, observer.discovering_changed_count_);
1081   EXPECT_EQ(8, callback_count_);
1082   EXPECT_EQ(0, error_callback_count_);
1083   EXPECT_TRUE(observer.last_discovering_);
1084   EXPECT_TRUE(adapter_->IsDiscovering());
1085   EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1086   discovery_sessions_.clear();
1087 
1088   // Start discovery again.
1089   adapter_->StartDiscoverySession(
1090     base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1091                base::Unretained(this)),
1092     base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1093                base::Unretained(this)));
1094   message_loop_.Run();  // Run the loop, as there should have been a D-Bus call.
1095   EXPECT_EQ(5, observer.discovering_changed_count_);
1096   EXPECT_EQ(9, callback_count_);
1097   EXPECT_EQ(0, error_callback_count_);
1098   EXPECT_TRUE(observer.last_discovering_);
1099   EXPECT_TRUE(adapter_->IsDiscovering());
1100   ASSERT_EQ((size_t)1, discovery_sessions_.size());
1101   EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1102 
1103   // Stop discovery via D-Bus. The fake client's reference count will drop but
1104   // the discovery state won't change since our BluetoothAdapter also just
1105   // requested it via D-Bus.
1106   fake_bluetooth_adapter_client_->StopDiscovery(
1107       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
1108       base::Bind(&BluetoothChromeOSTest::Callback,
1109                  base::Unretained(this)),
1110       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1111                  base::Unretained(this)));
1112   message_loop_.Run();
1113   EXPECT_EQ(5, observer.discovering_changed_count_);
1114   EXPECT_EQ(10, callback_count_);
1115   EXPECT_EQ(0, error_callback_count_);
1116   EXPECT_TRUE(observer.last_discovering_);
1117   EXPECT_TRUE(adapter_->IsDiscovering());
1118 
1119   // Now end the discovery session. This should change the adapter's discovery
1120   // state.
1121   discovery_sessions_[0]->Stop(
1122     base::Bind(&BluetoothChromeOSTest::Callback,
1123                base::Unretained(this)),
1124     base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1125                base::Unretained(this)));
1126   message_loop_.Run();
1127   EXPECT_EQ(6, observer.discovering_changed_count_);
1128   EXPECT_EQ(11, callback_count_);
1129   EXPECT_EQ(0, error_callback_count_);
1130   EXPECT_FALSE(observer.last_discovering_);
1131   EXPECT_FALSE(adapter_->IsDiscovering());
1132   EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1133 }
1134 
TEST_F(BluetoothChromeOSTest,InvalidatedDiscoverySessions)1135 TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) {
1136   GetAdapter();
1137   adapter_->SetPowered(
1138       true,
1139       base::Bind(&BluetoothChromeOSTest::Callback,
1140                  base::Unretained(this)),
1141       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1142                  base::Unretained(this)));
1143   EXPECT_EQ(1, callback_count_);
1144   EXPECT_EQ(0, error_callback_count_);
1145   EXPECT_TRUE(adapter_->IsPowered());
1146   callback_count_ = 0;
1147 
1148   TestObserver observer(adapter_);
1149 
1150   EXPECT_EQ(0, observer.discovering_changed_count_);
1151   EXPECT_FALSE(observer.last_discovering_);
1152   EXPECT_FALSE(adapter_->IsDiscovering());
1153 
1154   // Request device discovery 3 times.
1155   for (int i = 0; i < 3; i++) {
1156     adapter_->StartDiscoverySession(
1157       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1158                  base::Unretained(this)),
1159       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1160                  base::Unretained(this)));
1161   }
1162   // Run only once, as there should have been one D-Bus call.
1163   message_loop_.Run();
1164 
1165   // The observer should have received the discovering changed event exactly
1166   // once, the success callback should have been called 3 times and the adapter
1167   // should be discovering.
1168   EXPECT_EQ(1, observer.discovering_changed_count_);
1169   EXPECT_EQ(3, callback_count_);
1170   EXPECT_EQ(0, error_callback_count_);
1171   EXPECT_TRUE(observer.last_discovering_);
1172   EXPECT_TRUE(adapter_->IsDiscovering());
1173   ASSERT_EQ((size_t)3, discovery_sessions_.size());
1174 
1175   for (int i = 0; i < 3; i++)
1176     EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1177 
1178   // Stop the timers that the simulation uses
1179   fake_bluetooth_device_client_->EndDiscoverySimulation(
1180       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1181 
1182   ASSERT_TRUE(adapter_->IsPowered());
1183   ASSERT_TRUE(adapter_->IsDiscovering());
1184 
1185   // Delete all but one discovery session.
1186   discovery_sessions_.pop_back();
1187   discovery_sessions_.pop_back();
1188   ASSERT_EQ((size_t)1, discovery_sessions_.size());
1189   EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1190   EXPECT_TRUE(adapter_->IsDiscovering());
1191 
1192   // Stop device discovery behind the adapter. The one active discovery session
1193   // should become inactive, but more importantly, we shouldn't run into any
1194   // memory errors as the sessions that we explicitly deleted should get
1195   // cleaned up.
1196   fake_bluetooth_adapter_client_->StopDiscovery(
1197       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
1198       base::Bind(&BluetoothChromeOSTest::Callback,
1199                  base::Unretained(this)),
1200       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1201                  base::Unretained(this)));
1202   message_loop_.Run();
1203   EXPECT_EQ(2, observer.discovering_changed_count_);
1204   EXPECT_EQ(4, callback_count_);
1205   EXPECT_EQ(0, error_callback_count_);
1206   EXPECT_FALSE(observer.last_discovering_);
1207   EXPECT_FALSE(adapter_->IsDiscovering());
1208   EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1209 }
1210 
TEST_F(BluetoothChromeOSTest,QueuedDiscoveryRequests)1211 TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) {
1212   GetAdapter();
1213 
1214   adapter_->SetPowered(
1215       true,
1216       base::Bind(&BluetoothChromeOSTest::Callback,
1217                  base::Unretained(this)),
1218       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1219                  base::Unretained(this)));
1220   EXPECT_EQ(1, callback_count_);
1221   EXPECT_EQ(0, error_callback_count_);
1222   EXPECT_TRUE(adapter_->IsPowered());
1223   callback_count_ = 0;
1224 
1225   TestObserver observer(adapter_);
1226 
1227   EXPECT_EQ(0, observer.discovering_changed_count_);
1228   EXPECT_FALSE(observer.last_discovering_);
1229   EXPECT_FALSE(adapter_->IsDiscovering());
1230 
1231   // Request to start discovery. The call should be pending.
1232   adapter_->StartDiscoverySession(
1233     base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1234                base::Unretained(this)),
1235     base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1236                base::Unretained(this)));
1237   EXPECT_EQ(0, callback_count_);
1238 
1239   fake_bluetooth_device_client_->EndDiscoverySimulation(
1240       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1241 
1242   // The underlying adapter has started discovery, but our call hasn't returned
1243   // yet.
1244   EXPECT_EQ(1, observer.discovering_changed_count_);
1245   EXPECT_TRUE(observer.last_discovering_);
1246   EXPECT_TRUE(adapter_->IsDiscovering());
1247   EXPECT_TRUE(discovery_sessions_.empty());
1248 
1249   // Request to start discovery twice. These should get queued and there should
1250   // be no change in state.
1251   for (int i = 0; i < 2; i++) {
1252     adapter_->StartDiscoverySession(
1253       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1254                  base::Unretained(this)),
1255       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1256                  base::Unretained(this)));
1257   }
1258   EXPECT_EQ(0, callback_count_);
1259   EXPECT_EQ(0, error_callback_count_);
1260   EXPECT_EQ(1, observer.discovering_changed_count_);
1261   EXPECT_TRUE(observer.last_discovering_);
1262   EXPECT_TRUE(adapter_->IsDiscovering());
1263   EXPECT_TRUE(discovery_sessions_.empty());
1264 
1265   // Process the pending call. The queued calls should execute and the discovery
1266   // session reference count should increase.
1267   message_loop_.Run();
1268   EXPECT_EQ(3, callback_count_);
1269   EXPECT_EQ(0, error_callback_count_);
1270   EXPECT_EQ(1, observer.discovering_changed_count_);
1271   EXPECT_TRUE(observer.last_discovering_);
1272   EXPECT_TRUE(adapter_->IsDiscovering());
1273   ASSERT_EQ((size_t)3, discovery_sessions_.size());
1274 
1275   // Verify the reference count by removing sessions 3 times. The last request
1276   // should remain pending.
1277   for (int i = 0; i < 3; i++) {
1278     discovery_sessions_[i]->Stop(
1279       base::Bind(&BluetoothChromeOSTest::Callback,
1280                  base::Unretained(this)),
1281       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1282                  base::Unretained(this)));
1283   }
1284   EXPECT_EQ(5, callback_count_);
1285   EXPECT_EQ(0, error_callback_count_);
1286   EXPECT_EQ(2, observer.discovering_changed_count_);
1287   EXPECT_FALSE(observer.last_discovering_);
1288   EXPECT_FALSE(adapter_->IsDiscovering());
1289   EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1290   EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1291   EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1292 
1293   // Request to stop the session whose call is pending should fail.
1294   discovery_sessions_[2]->Stop(
1295     base::Bind(&BluetoothChromeOSTest::Callback,
1296                base::Unretained(this)),
1297     base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1298                base::Unretained(this)));
1299   EXPECT_EQ(5, callback_count_);
1300   EXPECT_EQ(1, error_callback_count_);
1301   EXPECT_EQ(2, observer.discovering_changed_count_);
1302   EXPECT_FALSE(observer.last_discovering_);
1303   EXPECT_FALSE(adapter_->IsDiscovering());
1304   EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1305 
1306   // Request to start should get queued.
1307   adapter_->StartDiscoverySession(
1308     base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1309                base::Unretained(this)),
1310     base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1311                base::Unretained(this)));
1312   EXPECT_EQ(5, callback_count_);
1313   EXPECT_EQ(1, error_callback_count_);
1314   EXPECT_EQ(2, observer.discovering_changed_count_);
1315   EXPECT_FALSE(observer.last_discovering_);
1316   EXPECT_FALSE(adapter_->IsDiscovering());
1317   ASSERT_EQ((size_t)3, discovery_sessions_.size());
1318 
1319   // Run the pending request.
1320   message_loop_.Run();
1321   EXPECT_EQ(6, callback_count_);
1322   EXPECT_EQ(1, error_callback_count_);
1323   EXPECT_EQ(3, observer.discovering_changed_count_);
1324   EXPECT_TRUE(observer.last_discovering_);
1325   EXPECT_TRUE(adapter_->IsDiscovering());
1326   ASSERT_EQ((size_t)3, discovery_sessions_.size());
1327   EXPECT_FALSE(discovery_sessions_[2]->IsActive());
1328 
1329   // The queued request to start discovery should have been issued but is still
1330   // pending. Run the loop and verify.
1331   message_loop_.Run();
1332   EXPECT_EQ(7, callback_count_);
1333   EXPECT_EQ(1, error_callback_count_);
1334   EXPECT_EQ(3, observer.discovering_changed_count_);
1335   EXPECT_TRUE(observer.last_discovering_);
1336   EXPECT_TRUE(adapter_->IsDiscovering());
1337   ASSERT_EQ((size_t)4, discovery_sessions_.size());
1338   EXPECT_TRUE(discovery_sessions_[3]->IsActive());
1339 }
1340 
TEST_F(BluetoothChromeOSTest,StartDiscoverySession)1341 TEST_F(BluetoothChromeOSTest, StartDiscoverySession) {
1342   GetAdapter();
1343 
1344   adapter_->SetPowered(
1345       true,
1346       base::Bind(&BluetoothChromeOSTest::Callback,
1347                  base::Unretained(this)),
1348       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1349                  base::Unretained(this)));
1350   EXPECT_EQ(1, callback_count_);
1351   EXPECT_EQ(0, error_callback_count_);
1352   EXPECT_TRUE(adapter_->IsPowered());
1353   callback_count_ = 0;
1354 
1355   TestObserver observer(adapter_);
1356 
1357   EXPECT_EQ(0, observer.discovering_changed_count_);
1358   EXPECT_FALSE(observer.last_discovering_);
1359   EXPECT_FALSE(adapter_->IsDiscovering());
1360   EXPECT_TRUE(discovery_sessions_.empty());
1361 
1362   // Request a new discovery session.
1363   adapter_->StartDiscoverySession(
1364       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1365                  base::Unretained(this)),
1366       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1367                  base::Unretained(this)));
1368   message_loop_.Run();
1369   EXPECT_EQ(1, observer.discovering_changed_count_);
1370   EXPECT_EQ(1, callback_count_);
1371   EXPECT_EQ(0, error_callback_count_);
1372   EXPECT_TRUE(observer.last_discovering_);
1373   EXPECT_TRUE(adapter_->IsDiscovering());
1374   ASSERT_EQ((size_t)1, discovery_sessions_.size());
1375   EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1376 
1377   // Start another session. A new one should be returned in the callback, which
1378   // in turn will destroy the previous session. Adapter should still be
1379   // discovering and the reference count should be 1.
1380   adapter_->StartDiscoverySession(
1381       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1382                  base::Unretained(this)),
1383       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1384                  base::Unretained(this)));
1385   message_loop_.Run();
1386   EXPECT_EQ(1, observer.discovering_changed_count_);
1387   EXPECT_EQ(2, callback_count_);
1388   EXPECT_EQ(0, error_callback_count_);
1389   EXPECT_TRUE(observer.last_discovering_);
1390   EXPECT_TRUE(adapter_->IsDiscovering());
1391   ASSERT_EQ((size_t)2, discovery_sessions_.size());
1392   EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1393 
1394   // Request a new session.
1395   adapter_->StartDiscoverySession(
1396       base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1397                  base::Unretained(this)),
1398       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1399                  base::Unretained(this)));
1400   message_loop_.Run();
1401   EXPECT_EQ(1, observer.discovering_changed_count_);
1402   EXPECT_EQ(3, callback_count_);
1403   EXPECT_EQ(0, error_callback_count_);
1404   EXPECT_TRUE(observer.last_discovering_);
1405   EXPECT_TRUE(adapter_->IsDiscovering());
1406   ASSERT_EQ((size_t)3, discovery_sessions_.size());
1407   EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1408   EXPECT_NE(discovery_sessions_[0], discovery_sessions_[1]);
1409 
1410   // Stop the previous discovery session. The session should end but discovery
1411   // should continue.
1412   discovery_sessions_[0]->Stop(
1413       base::Bind(&BluetoothChromeOSTest::Callback,
1414                  base::Unretained(this)),
1415       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1416                  base::Unretained(this)));
1417   message_loop_.Run();
1418   EXPECT_EQ(1, observer.discovering_changed_count_);
1419   EXPECT_EQ(4, callback_count_);
1420   EXPECT_EQ(0, error_callback_count_);
1421   EXPECT_TRUE(observer.last_discovering_);
1422   EXPECT_TRUE(adapter_->IsDiscovering());
1423   ASSERT_EQ((size_t)3, discovery_sessions_.size());
1424   EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1425   EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1426 
1427   // Delete the current active session. Discovery should eventually stop.
1428   discovery_sessions_.clear();
1429   while (observer.last_discovering_)
1430     message_loop_.RunUntilIdle();
1431 
1432   EXPECT_EQ(2, observer.discovering_changed_count_);
1433   EXPECT_EQ(4, callback_count_);
1434   EXPECT_EQ(0, error_callback_count_);
1435   EXPECT_FALSE(observer.last_discovering_);
1436   EXPECT_FALSE(adapter_->IsDiscovering());
1437 }
1438 
TEST_F(BluetoothChromeOSTest,DeviceProperties)1439 TEST_F(BluetoothChromeOSTest, DeviceProperties) {
1440   GetAdapter();
1441 
1442   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1443   ASSERT_EQ(1U, devices.size());
1444   ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1445             devices[0]->GetAddress());
1446 
1447   // Verify the other device properties.
1448   EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
1449             devices[0]->GetName());
1450   EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
1451   EXPECT_TRUE(devices[0]->IsPaired());
1452   EXPECT_FALSE(devices[0]->IsConnected());
1453   EXPECT_FALSE(devices[0]->IsConnecting());
1454 
1455   // Non HID devices are always connectable.
1456   EXPECT_TRUE(devices[0]->IsConnectable());
1457 
1458   BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
1459   ASSERT_EQ(2U, uuids.size());
1460   EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
1461   EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
1462 
1463   EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, devices[0]->GetVendorIDSource());
1464   EXPECT_EQ(0x05ac, devices[0]->GetVendorID());
1465   EXPECT_EQ(0x030d, devices[0]->GetProductID());
1466   EXPECT_EQ(0x0306, devices[0]->GetDeviceID());
1467 }
1468 
TEST_F(BluetoothChromeOSTest,DeviceClassChanged)1469 TEST_F(BluetoothChromeOSTest, DeviceClassChanged) {
1470   // Simulate a change of class of a device, as sometimes occurs
1471   // during discovery.
1472   GetAdapter();
1473 
1474   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1475   ASSERT_EQ(1U, devices.size());
1476   ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1477             devices[0]->GetAddress());
1478   ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
1479 
1480   // Install an observer; expect the DeviceChanged method to be called when
1481   // we change the class of the device.
1482   TestObserver observer(adapter_);
1483 
1484   FakeBluetoothDeviceClient::Properties* properties =
1485       fake_bluetooth_device_client_->GetProperties(
1486           dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
1487 
1488   properties->bluetooth_class.ReplaceValue(0x002580);
1489 
1490   EXPECT_EQ(1, observer.device_changed_count_);
1491   EXPECT_EQ(devices[0], observer.last_device_);
1492 
1493   EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType());
1494 }
1495 
TEST_F(BluetoothChromeOSTest,DeviceNameChanged)1496 TEST_F(BluetoothChromeOSTest, DeviceNameChanged) {
1497   // Simulate a change of name of a device.
1498   GetAdapter();
1499 
1500   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1501   ASSERT_EQ(1U, devices.size());
1502   ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1503             devices[0]->GetAddress());
1504   ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
1505             devices[0]->GetName());
1506 
1507   // Install an observer; expect the DeviceChanged method to be called when
1508   // we change the alias of the device.
1509   TestObserver observer(adapter_);
1510 
1511   FakeBluetoothDeviceClient::Properties* properties =
1512       fake_bluetooth_device_client_->GetProperties(
1513           dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
1514 
1515   static const std::string new_name("New Device Name");
1516   properties->alias.ReplaceValue(new_name);
1517 
1518   EXPECT_EQ(1, observer.device_changed_count_);
1519   EXPECT_EQ(devices[0], observer.last_device_);
1520 
1521   EXPECT_EQ(base::UTF8ToUTF16(new_name), devices[0]->GetName());
1522 }
1523 
TEST_F(BluetoothChromeOSTest,DeviceUuidsChanged)1524 TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) {
1525   // Simulate a change of advertised services of a device.
1526   GetAdapter();
1527 
1528   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1529   ASSERT_EQ(1U, devices.size());
1530   ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1531             devices[0]->GetAddress());
1532 
1533   BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
1534   ASSERT_EQ(2U, uuids.size());
1535   ASSERT_EQ(uuids[0], BluetoothUUID("1800"));
1536   ASSERT_EQ(uuids[1], BluetoothUUID("1801"));
1537 
1538   // Install an observer; expect the DeviceChanged method to be called when
1539   // we change the class of the device.
1540   TestObserver observer(adapter_);
1541 
1542   FakeBluetoothDeviceClient::Properties* properties =
1543       fake_bluetooth_device_client_->GetProperties(
1544           dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
1545 
1546   std::vector<std::string> new_uuids;
1547   new_uuids.push_back(uuids[0].canonical_value());
1548   new_uuids.push_back(uuids[1].canonical_value());
1549   new_uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb");
1550   new_uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb");
1551   new_uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb");
1552 
1553   properties->uuids.ReplaceValue(new_uuids);
1554 
1555   EXPECT_EQ(1, observer.device_changed_count_);
1556   EXPECT_EQ(devices[0], observer.last_device_);
1557 
1558   // Fetching the value should give the new one.
1559   uuids = devices[0]->GetUUIDs();
1560   ASSERT_EQ(5U, uuids.size());
1561   EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
1562   EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
1563   EXPECT_EQ(uuids[2], BluetoothUUID("110c"));
1564   EXPECT_EQ(uuids[3], BluetoothUUID("110e"));
1565   EXPECT_EQ(uuids[4], BluetoothUUID("110a"));
1566 }
1567 
TEST_F(BluetoothChromeOSTest,ForgetDevice)1568 TEST_F(BluetoothChromeOSTest, ForgetDevice) {
1569   GetAdapter();
1570 
1571   BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1572   ASSERT_EQ(1U, devices.size());
1573   ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1574             devices[0]->GetAddress());
1575 
1576   std::string address = devices[0]->GetAddress();
1577 
1578   // Install an observer; expect the DeviceRemoved method to be called
1579   // with the device we remove.
1580   TestObserver observer(adapter_);
1581 
1582   devices[0]->Forget(
1583       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1584                  base::Unretained(this)));
1585   EXPECT_EQ(0, error_callback_count_);
1586 
1587   EXPECT_EQ(1, observer.device_removed_count_);
1588   EXPECT_EQ(address, observer.last_device_address_);
1589 
1590   // GetDevices shouldn't return the device either.
1591   devices = adapter_->GetDevices();
1592   ASSERT_EQ(0U, devices.size());
1593 }
1594 
TEST_F(BluetoothChromeOSTest,ForgetUnpairedDevice)1595 TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) {
1596   GetAdapter();
1597   DiscoverDevices();
1598 
1599   BluetoothDevice* device = adapter_->GetDevice(
1600       FakeBluetoothDeviceClient::kConnectUnpairableAddress);
1601   ASSERT_TRUE(device != NULL);
1602   ASSERT_FALSE(device->IsPaired());
1603 
1604   // Connect the device so it becomes trusted and remembered.
1605   device->Connect(
1606       NULL,
1607       base::Bind(&BluetoothChromeOSTest::Callback,
1608                  base::Unretained(this)),
1609       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1610                  base::Unretained(this)));
1611 
1612   ASSERT_EQ(1, callback_count_);
1613   ASSERT_EQ(0, error_callback_count_);
1614   callback_count_ = 0;
1615 
1616   ASSERT_TRUE(device->IsConnected());
1617   ASSERT_FALSE(device->IsConnecting());
1618 
1619   // Make sure the trusted property has been set to true.
1620   FakeBluetoothDeviceClient::Properties* properties =
1621       fake_bluetooth_device_client_->GetProperties(
1622           dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
1623   ASSERT_TRUE(properties->trusted.value());
1624 
1625   // Install an observer; expect the DeviceRemoved method to be called
1626   // with the device we remove.
1627   TestObserver observer(adapter_);
1628 
1629   device->Forget(
1630       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1631                  base::Unretained(this)));
1632   EXPECT_EQ(0, error_callback_count_);
1633 
1634   EXPECT_EQ(1, observer.device_removed_count_);
1635   EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress,
1636             observer.last_device_address_);
1637 
1638   // GetDevices shouldn't return the device either.
1639   device = adapter_->GetDevice(
1640       FakeBluetoothDeviceClient::kConnectUnpairableAddress);
1641   EXPECT_FALSE(device != NULL);
1642 }
1643 
TEST_F(BluetoothChromeOSTest,ConnectPairedDevice)1644 TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) {
1645   GetAdapter();
1646 
1647   BluetoothDevice* device = adapter_->GetDevice(
1648       FakeBluetoothDeviceClient::kPairedDeviceAddress);
1649   ASSERT_TRUE(device != NULL);
1650   ASSERT_TRUE(device->IsPaired());
1651 
1652   TestObserver observer(adapter_);
1653 
1654   // Connect without a pairing delegate; since the device is already Paired
1655   // this should succeed and the device should become connected.
1656   device->Connect(
1657       NULL,
1658       base::Bind(&BluetoothChromeOSTest::Callback,
1659                  base::Unretained(this)),
1660       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1661                  base::Unretained(this)));
1662 
1663   EXPECT_EQ(1, callback_count_);
1664   EXPECT_EQ(0, error_callback_count_);
1665 
1666   // Two changes for connecting, one for connected and one for for trusted
1667   // after connecting.
1668   EXPECT_EQ(4, observer.device_changed_count_);
1669   EXPECT_EQ(device, observer.last_device_);
1670 
1671   EXPECT_TRUE(device->IsConnected());
1672   EXPECT_FALSE(device->IsConnecting());
1673 }
1674 
TEST_F(BluetoothChromeOSTest,ConnectUnpairableDevice)1675 TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) {
1676   GetAdapter();
1677   DiscoverDevices();
1678 
1679   BluetoothDevice* device = adapter_->GetDevice(
1680       FakeBluetoothDeviceClient::kConnectUnpairableAddress);
1681   ASSERT_TRUE(device != NULL);
1682   ASSERT_FALSE(device->IsPaired());
1683 
1684   TestObserver observer(adapter_);
1685 
1686   // Connect without a pairing delegate; since the device does not require
1687   // pairing, this should succeed and the device should become connected.
1688   device->Connect(
1689       NULL,
1690       base::Bind(&BluetoothChromeOSTest::Callback,
1691                  base::Unretained(this)),
1692       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1693                  base::Unretained(this)));
1694 
1695   EXPECT_EQ(1, callback_count_);
1696   EXPECT_EQ(0, error_callback_count_);
1697 
1698   // Two changes for connecting, one for connected, one for for trusted after
1699   // connection, and one for the reconnect mode (IsConnectable).
1700   EXPECT_EQ(5, observer.device_changed_count_);
1701   EXPECT_EQ(device, observer.last_device_);
1702 
1703   EXPECT_TRUE(device->IsConnected());
1704   EXPECT_FALSE(device->IsConnecting());
1705 
1706   // Make sure the trusted property has been set to true.
1707   FakeBluetoothDeviceClient::Properties* properties =
1708       fake_bluetooth_device_client_->GetProperties(
1709           dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
1710   EXPECT_TRUE(properties->trusted.value());
1711 
1712   // Verify is a HID device and is not connectable.
1713   BluetoothDevice::UUIDList uuids = device->GetUUIDs();
1714   ASSERT_EQ(1U, uuids.size());
1715   EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
1716   EXPECT_FALSE(device->IsConnectable());
1717 }
1718 
TEST_F(BluetoothChromeOSTest,ConnectConnectedDevice)1719 TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) {
1720   GetAdapter();
1721 
1722   BluetoothDevice* device = adapter_->GetDevice(
1723       FakeBluetoothDeviceClient::kPairedDeviceAddress);
1724   ASSERT_TRUE(device != NULL);
1725   ASSERT_TRUE(device->IsPaired());
1726 
1727   device->Connect(
1728       NULL,
1729       base::Bind(&BluetoothChromeOSTest::Callback,
1730                  base::Unretained(this)),
1731       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1732                  base::Unretained(this)));
1733 
1734   ASSERT_EQ(1, callback_count_);
1735   ASSERT_EQ(0, error_callback_count_);
1736   callback_count_ = 0;
1737 
1738   ASSERT_TRUE(device->IsConnected());
1739 
1740   // Connect again; since the device is already Connected, this shouldn't do
1741   // anything to initiate the connection.
1742   TestObserver observer(adapter_);
1743 
1744   device->Connect(
1745       NULL,
1746       base::Bind(&BluetoothChromeOSTest::Callback,
1747                  base::Unretained(this)),
1748       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1749                  base::Unretained(this)));
1750 
1751   EXPECT_EQ(1, callback_count_);
1752   EXPECT_EQ(0, error_callback_count_);
1753 
1754   // The observer will be called because Connecting will toggle true and false,
1755   // and the trusted property will be updated to true.
1756   EXPECT_EQ(3, observer.device_changed_count_);
1757 
1758   EXPECT_TRUE(device->IsConnected());
1759   EXPECT_FALSE(device->IsConnecting());
1760 }
1761 
TEST_F(BluetoothChromeOSTest,ConnectDeviceFails)1762 TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) {
1763   GetAdapter();
1764   DiscoverDevices();
1765 
1766   BluetoothDevice* device = adapter_->GetDevice(
1767       FakeBluetoothDeviceClient::kLegacyAutopairAddress);
1768   ASSERT_TRUE(device != NULL);
1769   ASSERT_FALSE(device->IsPaired());
1770 
1771   TestObserver observer(adapter_);
1772 
1773   // Connect without a pairing delegate; since the device requires pairing,
1774   // this should fail with an error.
1775   device->Connect(
1776       NULL,
1777       base::Bind(&BluetoothChromeOSTest::Callback,
1778                  base::Unretained(this)),
1779       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1780                  base::Unretained(this)));
1781 
1782   EXPECT_EQ(0, callback_count_);
1783   EXPECT_EQ(1, error_callback_count_);
1784   EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
1785 
1786   EXPECT_EQ(2, observer.device_changed_count_);
1787 
1788   EXPECT_FALSE(device->IsConnected());
1789   EXPECT_FALSE(device->IsConnecting());
1790 }
1791 
TEST_F(BluetoothChromeOSTest,DisconnectDevice)1792 TEST_F(BluetoothChromeOSTest, DisconnectDevice) {
1793   GetAdapter();
1794 
1795   BluetoothDevice* device = adapter_->GetDevice(
1796       FakeBluetoothDeviceClient::kPairedDeviceAddress);
1797   ASSERT_TRUE(device != NULL);
1798   ASSERT_TRUE(device->IsPaired());
1799 
1800   device->Connect(
1801       NULL,
1802       base::Bind(&BluetoothChromeOSTest::Callback,
1803                  base::Unretained(this)),
1804       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1805                  base::Unretained(this)));
1806 
1807   ASSERT_EQ(1, callback_count_);
1808   ASSERT_EQ(0, error_callback_count_);
1809   callback_count_ = 0;
1810 
1811   ASSERT_TRUE(device->IsConnected());
1812   ASSERT_FALSE(device->IsConnecting());
1813 
1814   // Disconnect the device, we should see the observer method fire and the
1815   // device get dropped.
1816   TestObserver observer(adapter_);
1817 
1818   device->Disconnect(
1819       base::Bind(&BluetoothChromeOSTest::Callback,
1820                  base::Unretained(this)),
1821       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1822                  base::Unretained(this)));
1823 
1824   EXPECT_EQ(1, callback_count_);
1825   EXPECT_EQ(0, error_callback_count_);
1826 
1827   EXPECT_EQ(1, observer.device_changed_count_);
1828   EXPECT_EQ(device, observer.last_device_);
1829 
1830   EXPECT_FALSE(device->IsConnected());
1831 }
1832 
TEST_F(BluetoothChromeOSTest,DisconnectUnconnectedDevice)1833 TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) {
1834   GetAdapter();
1835 
1836   BluetoothDevice* device = adapter_->GetDevice(
1837       FakeBluetoothDeviceClient::kPairedDeviceAddress);
1838   ASSERT_TRUE(device != NULL);
1839   ASSERT_TRUE(device->IsPaired());
1840   ASSERT_FALSE(device->IsConnected());
1841 
1842   // Disconnect the device, we should see the observer method fire and the
1843   // device get dropped.
1844   TestObserver observer(adapter_);
1845 
1846   device->Disconnect(
1847       base::Bind(&BluetoothChromeOSTest::Callback,
1848                  base::Unretained(this)),
1849       base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1850                  base::Unretained(this)));
1851 
1852   EXPECT_EQ(0, callback_count_);
1853   EXPECT_EQ(1, error_callback_count_);
1854 
1855   EXPECT_EQ(0, observer.device_changed_count_);
1856 
1857   EXPECT_FALSE(device->IsConnected());
1858 }
1859 
TEST_F(BluetoothChromeOSTest,PairLegacyAutopair)1860 TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) {
1861   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1862 
1863   GetAdapter();
1864   DiscoverDevices();
1865 
1866   // The Legacy Autopair device requires no PIN or Passkey to pair because
1867   // the daemon provides 0000 to the device for us.
1868   BluetoothDevice* device = adapter_->GetDevice(
1869       FakeBluetoothDeviceClient::kLegacyAutopairAddress);
1870   ASSERT_TRUE(device != NULL);
1871   ASSERT_FALSE(device->IsPaired());
1872 
1873   TestObserver observer(adapter_);
1874 
1875   TestPairingDelegate pairing_delegate;
1876   device->Connect(
1877       &pairing_delegate,
1878       base::Bind(&BluetoothChromeOSTest::Callback,
1879                  base::Unretained(this)),
1880       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1881                  base::Unretained(this)));
1882 
1883   EXPECT_EQ(0, pairing_delegate.call_count_);
1884   EXPECT_TRUE(device->IsConnecting());
1885 
1886   message_loop_.Run();
1887 
1888   EXPECT_EQ(1, callback_count_);
1889   EXPECT_EQ(0, error_callback_count_);
1890 
1891   // Two changes for connecting, one change for connected, one for paired,
1892   // two for trusted (after pairing and connection), and one for the reconnect
1893   // mode (IsConnectable).
1894   EXPECT_EQ(7, observer.device_changed_count_);
1895   EXPECT_EQ(device, observer.last_device_);
1896 
1897   EXPECT_TRUE(device->IsConnected());
1898   EXPECT_FALSE(device->IsConnecting());
1899 
1900   EXPECT_TRUE(device->IsPaired());
1901 
1902   // Verify is a HID device and is connectable.
1903   BluetoothDevice::UUIDList uuids = device->GetUUIDs();
1904   ASSERT_EQ(1U, uuids.size());
1905   EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
1906   EXPECT_TRUE(device->IsConnectable());
1907 
1908   // Make sure the trusted property has been set to true.
1909   FakeBluetoothDeviceClient::Properties* properties =
1910       fake_bluetooth_device_client_->GetProperties(
1911           dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath));
1912   EXPECT_TRUE(properties->trusted.value());
1913 }
1914 
TEST_F(BluetoothChromeOSTest,PairDisplayPinCode)1915 TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) {
1916   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1917 
1918   GetAdapter();
1919   DiscoverDevices();
1920 
1921   // Requires that we display a randomly generated PIN on the screen.
1922   BluetoothDevice* device = adapter_->GetDevice(
1923       FakeBluetoothDeviceClient::kDisplayPinCodeAddress);
1924   ASSERT_TRUE(device != NULL);
1925   ASSERT_FALSE(device->IsPaired());
1926 
1927   TestObserver observer(adapter_);
1928 
1929   TestPairingDelegate pairing_delegate;
1930   device->Connect(
1931       &pairing_delegate,
1932       base::Bind(&BluetoothChromeOSTest::Callback,
1933                  base::Unretained(this)),
1934       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1935                  base::Unretained(this)));
1936 
1937   EXPECT_EQ(1, pairing_delegate.call_count_);
1938   EXPECT_EQ(1, pairing_delegate.display_pincode_count_);
1939   EXPECT_EQ("123456", pairing_delegate.last_pincode_);
1940   EXPECT_TRUE(device->IsConnecting());
1941 
1942   message_loop_.Run();
1943 
1944   EXPECT_EQ(1, callback_count_);
1945   EXPECT_EQ(0, error_callback_count_);
1946 
1947   // Two changes for connecting, one change for connected, one for paired,
1948   // two for trusted (after pairing and connection), and one for the reconnect
1949   // mode (IsConnectable).
1950   EXPECT_EQ(7, observer.device_changed_count_);
1951   EXPECT_EQ(device, observer.last_device_);
1952 
1953   EXPECT_TRUE(device->IsConnected());
1954   EXPECT_FALSE(device->IsConnecting());
1955 
1956   EXPECT_TRUE(device->IsPaired());
1957 
1958   // Verify is a HID device and is connectable.
1959   BluetoothDevice::UUIDList uuids = device->GetUUIDs();
1960   ASSERT_EQ(1U, uuids.size());
1961   EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
1962   EXPECT_TRUE(device->IsConnectable());
1963 
1964   // Make sure the trusted property has been set to true.
1965   FakeBluetoothDeviceClient::Properties* properties =
1966       fake_bluetooth_device_client_->GetProperties(
1967           dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath));
1968   EXPECT_TRUE(properties->trusted.value());
1969 }
1970 
TEST_F(BluetoothChromeOSTest,PairDisplayPasskey)1971 TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) {
1972   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1973 
1974   GetAdapter();
1975   DiscoverDevices();
1976 
1977   // Requires that we display a randomly generated Passkey on the screen,
1978   // and notifies us as it's typed in.
1979   BluetoothDevice* device = adapter_->GetDevice(
1980       FakeBluetoothDeviceClient::kDisplayPasskeyAddress);
1981   ASSERT_TRUE(device != NULL);
1982   ASSERT_FALSE(device->IsPaired());
1983 
1984   TestObserver observer(adapter_);
1985 
1986   TestPairingDelegate pairing_delegate;
1987   device->Connect(
1988       &pairing_delegate,
1989       base::Bind(&BluetoothChromeOSTest::Callback,
1990                  base::Unretained(this)),
1991       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1992                  base::Unretained(this)));
1993 
1994   // One call for DisplayPasskey() and one for KeysEntered().
1995   EXPECT_EQ(2, pairing_delegate.call_count_);
1996   EXPECT_EQ(1, pairing_delegate.display_passkey_count_);
1997   EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
1998   EXPECT_EQ(1, pairing_delegate.keys_entered_count_);
1999   EXPECT_EQ(0U, pairing_delegate.last_entered_);
2000 
2001   EXPECT_TRUE(device->IsConnecting());
2002 
2003   // One call to KeysEntered() for each key, including [enter].
2004   for(int i = 1; i <= 7; ++i) {
2005     message_loop_.Run();
2006 
2007     EXPECT_EQ(2 + i, pairing_delegate.call_count_);
2008     EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_);
2009     EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_);
2010   }
2011 
2012   message_loop_.Run();
2013 
2014   // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
2015   // DisplayPasskey().
2016   EXPECT_EQ(9, pairing_delegate.call_count_);
2017   EXPECT_EQ(8, pairing_delegate.keys_entered_count_);
2018   EXPECT_EQ(7U, pairing_delegate.last_entered_);
2019 
2020   EXPECT_EQ(1, callback_count_);
2021   EXPECT_EQ(0, error_callback_count_);
2022 
2023   // Two changes for connecting, one change for connected, one for paired,
2024   // two for trusted (after pairing and connection), and one for the reconnect
2025   // mode (IsConnectable).
2026   EXPECT_EQ(7, observer.device_changed_count_);
2027   EXPECT_EQ(device, observer.last_device_);
2028 
2029   EXPECT_TRUE(device->IsConnected());
2030   EXPECT_FALSE(device->IsConnecting());
2031 
2032   EXPECT_TRUE(device->IsPaired());
2033 
2034   // Verify is a HID device.
2035   BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2036   ASSERT_EQ(1U, uuids.size());
2037   EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2038 
2039   // And usually not connectable.
2040   EXPECT_FALSE(device->IsConnectable());
2041 
2042   // Make sure the trusted property has been set to true.
2043   FakeBluetoothDeviceClient::Properties* properties =
2044       fake_bluetooth_device_client_->GetProperties(
2045           dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath));
2046   EXPECT_TRUE(properties->trusted.value());
2047 }
2048 
TEST_F(BluetoothChromeOSTest,PairRequestPinCode)2049 TEST_F(BluetoothChromeOSTest, PairRequestPinCode) {
2050   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2051 
2052   GetAdapter();
2053   DiscoverDevices();
2054 
2055   // Requires that the user enters a PIN for them.
2056   BluetoothDevice* device = adapter_->GetDevice(
2057       FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2058   ASSERT_TRUE(device != NULL);
2059   ASSERT_FALSE(device->IsPaired());
2060 
2061   TestObserver observer(adapter_);
2062 
2063   TestPairingDelegate pairing_delegate;
2064   device->Connect(
2065       &pairing_delegate,
2066       base::Bind(&BluetoothChromeOSTest::Callback,
2067                  base::Unretained(this)),
2068       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2069                  base::Unretained(this)));
2070 
2071   EXPECT_EQ(1, pairing_delegate.call_count_);
2072   EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2073   EXPECT_TRUE(device->IsConnecting());
2074 
2075   // Set the PIN.
2076   device->SetPinCode("1234");
2077   message_loop_.Run();
2078 
2079   EXPECT_EQ(1, callback_count_);
2080   EXPECT_EQ(0, error_callback_count_);
2081 
2082   // Two changes for connecting, one change for connected, one for paired and
2083   // two for trusted (after pairing and connection).
2084   EXPECT_EQ(6, observer.device_changed_count_);
2085   EXPECT_EQ(device, observer.last_device_);
2086 
2087   EXPECT_TRUE(device->IsConnected());
2088   EXPECT_FALSE(device->IsConnecting());
2089 
2090   EXPECT_TRUE(device->IsPaired());
2091 
2092   // Verify is not a HID device.
2093   BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2094   ASSERT_EQ(0U, uuids.size());
2095 
2096   // Non HID devices are always connectable.
2097   EXPECT_TRUE(device->IsConnectable());
2098 
2099   // Make sure the trusted property has been set to true.
2100   FakeBluetoothDeviceClient::Properties* properties =
2101       fake_bluetooth_device_client_->GetProperties(
2102           dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2103   EXPECT_TRUE(properties->trusted.value());
2104 }
2105 
TEST_F(BluetoothChromeOSTest,PairConfirmPasskey)2106 TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) {
2107   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2108 
2109   GetAdapter();
2110   DiscoverDevices();
2111 
2112   // Requests that we confirm a displayed passkey.
2113   BluetoothDevice* device = adapter_->GetDevice(
2114       FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2115   ASSERT_TRUE(device != NULL);
2116   ASSERT_FALSE(device->IsPaired());
2117 
2118   TestObserver observer(adapter_);
2119 
2120   TestPairingDelegate pairing_delegate;
2121   device->Connect(
2122       &pairing_delegate,
2123       base::Bind(&BluetoothChromeOSTest::Callback,
2124                  base::Unretained(this)),
2125       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2126                  base::Unretained(this)));
2127 
2128   EXPECT_EQ(1, pairing_delegate.call_count_);
2129   EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2130   EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2131   EXPECT_TRUE(device->IsConnecting());
2132 
2133   // Confirm the passkey.
2134   device->ConfirmPairing();
2135   message_loop_.Run();
2136 
2137   EXPECT_EQ(1, callback_count_);
2138   EXPECT_EQ(0, error_callback_count_);
2139 
2140   // Two changes for connecting, one change for connected, one for paired and
2141   // two for trusted (after pairing and connection).
2142   EXPECT_EQ(6, observer.device_changed_count_);
2143   EXPECT_EQ(device, observer.last_device_);
2144 
2145   EXPECT_TRUE(device->IsConnected());
2146   EXPECT_FALSE(device->IsConnecting());
2147 
2148   EXPECT_TRUE(device->IsPaired());
2149 
2150   // Non HID devices are always connectable.
2151   EXPECT_TRUE(device->IsConnectable());
2152 
2153   // Make sure the trusted property has been set to true.
2154   FakeBluetoothDeviceClient::Properties* properties =
2155       fake_bluetooth_device_client_->GetProperties(
2156           dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2157   EXPECT_TRUE(properties->trusted.value());
2158 }
2159 
TEST_F(BluetoothChromeOSTest,PairRequestPasskey)2160 TEST_F(BluetoothChromeOSTest, PairRequestPasskey) {
2161   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2162 
2163   GetAdapter();
2164   DiscoverDevices();
2165 
2166   // Requires that the user enters a Passkey, this would be some kind of
2167   // device that has a display, but doesn't use "just works" - maybe a car?
2168   BluetoothDevice* device = adapter_->GetDevice(
2169       FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2170   ASSERT_TRUE(device != NULL);
2171   ASSERT_FALSE(device->IsPaired());
2172 
2173   TestObserver observer(adapter_);
2174 
2175   TestPairingDelegate pairing_delegate;
2176   device->Connect(
2177       &pairing_delegate,
2178       base::Bind(&BluetoothChromeOSTest::Callback,
2179                  base::Unretained(this)),
2180       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2181                  base::Unretained(this)));
2182 
2183   EXPECT_EQ(1, pairing_delegate.call_count_);
2184   EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2185   EXPECT_TRUE(device->IsConnecting());
2186 
2187   // Set the Passkey.
2188   device->SetPasskey(1234);
2189   message_loop_.Run();
2190 
2191   EXPECT_EQ(1, callback_count_);
2192   EXPECT_EQ(0, error_callback_count_);
2193 
2194   // Two changes for connecting, one change for connected, one for paired and
2195   // two for trusted (after pairing and connection).
2196   EXPECT_EQ(6, observer.device_changed_count_);
2197   EXPECT_EQ(device, observer.last_device_);
2198 
2199   EXPECT_TRUE(device->IsConnected());
2200   EXPECT_FALSE(device->IsConnecting());
2201 
2202   EXPECT_TRUE(device->IsPaired());
2203 
2204   // Non HID devices are always connectable.
2205   EXPECT_TRUE(device->IsConnectable());
2206 
2207   // Make sure the trusted property has been set to true.
2208   FakeBluetoothDeviceClient::Properties* properties =
2209       fake_bluetooth_device_client_->GetProperties(
2210           dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2211   EXPECT_TRUE(properties->trusted.value());
2212 }
2213 
TEST_F(BluetoothChromeOSTest,PairJustWorks)2214 TEST_F(BluetoothChromeOSTest, PairJustWorks) {
2215   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2216 
2217   GetAdapter();
2218   DiscoverDevices();
2219 
2220   // Uses just-works pairing, since this is an outgoing pairing, no delegate
2221   // interaction is required.
2222   BluetoothDevice* device = adapter_->GetDevice(
2223       FakeBluetoothDeviceClient::kJustWorksAddress);
2224   ASSERT_TRUE(device != NULL);
2225   ASSERT_FALSE(device->IsPaired());
2226 
2227   TestObserver observer(adapter_);
2228 
2229   TestPairingDelegate pairing_delegate;
2230   device->Connect(
2231       &pairing_delegate,
2232       base::Bind(&BluetoothChromeOSTest::Callback,
2233                  base::Unretained(this)),
2234       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2235                  base::Unretained(this)));
2236 
2237   EXPECT_EQ(0, pairing_delegate.call_count_);
2238 
2239   message_loop_.Run();
2240 
2241   EXPECT_EQ(1, callback_count_);
2242   EXPECT_EQ(0, error_callback_count_);
2243 
2244   // Two changes for connecting, one change for connected, one for paired and
2245   // two for trusted (after pairing and connection).
2246   EXPECT_EQ(6, observer.device_changed_count_);
2247   EXPECT_EQ(device, observer.last_device_);
2248 
2249   EXPECT_TRUE(device->IsConnected());
2250   EXPECT_FALSE(device->IsConnecting());
2251 
2252   EXPECT_TRUE(device->IsPaired());
2253 
2254   // Non HID devices are always connectable.
2255   EXPECT_TRUE(device->IsConnectable());
2256 
2257   // Make sure the trusted property has been set to true.
2258   FakeBluetoothDeviceClient::Properties* properties =
2259       fake_bluetooth_device_client_->GetProperties(
2260           dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2261   EXPECT_TRUE(properties->trusted.value());
2262 }
2263 
TEST_F(BluetoothChromeOSTest,PairUnpairableDeviceFails)2264 TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) {
2265   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2266 
2267   GetAdapter();
2268   DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
2269 
2270   BluetoothDevice* device = adapter_->GetDevice(
2271       FakeBluetoothDeviceClient::kUnpairableDeviceAddress);
2272   ASSERT_TRUE(device != NULL);
2273   ASSERT_FALSE(device->IsPaired());
2274 
2275   TestObserver observer(adapter_);
2276 
2277   TestPairingDelegate pairing_delegate;
2278   device->Connect(
2279       &pairing_delegate,
2280       base::Bind(&BluetoothChromeOSTest::Callback,
2281                  base::Unretained(this)),
2282       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2283                  base::Unretained(this)));
2284 
2285   EXPECT_EQ(0, pairing_delegate.call_count_);
2286   EXPECT_TRUE(device->IsConnecting());
2287 
2288   // Run the loop to get the error..
2289   message_loop_.Run();
2290 
2291   EXPECT_EQ(0, callback_count_);
2292   EXPECT_EQ(1, error_callback_count_);
2293 
2294   EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2295 
2296   EXPECT_FALSE(device->IsConnected());
2297   EXPECT_FALSE(device->IsConnecting());
2298   EXPECT_FALSE(device->IsPaired());
2299 }
2300 
TEST_F(BluetoothChromeOSTest,PairingFails)2301 TEST_F(BluetoothChromeOSTest, PairingFails) {
2302   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2303 
2304   GetAdapter();
2305   DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2306 
2307   // The vanishing device times out during pairing
2308   BluetoothDevice* device = adapter_->GetDevice(
2309       FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2310   ASSERT_TRUE(device != NULL);
2311   ASSERT_FALSE(device->IsPaired());
2312 
2313   TestObserver observer(adapter_);
2314 
2315   TestPairingDelegate pairing_delegate;
2316   device->Connect(
2317       &pairing_delegate,
2318       base::Bind(&BluetoothChromeOSTest::Callback,
2319                  base::Unretained(this)),
2320       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2321                  base::Unretained(this)));
2322 
2323   EXPECT_EQ(0, pairing_delegate.call_count_);
2324   EXPECT_TRUE(device->IsConnecting());
2325 
2326   // Run the loop to get the error..
2327   message_loop_.Run();
2328 
2329   EXPECT_EQ(0, callback_count_);
2330   EXPECT_EQ(1, error_callback_count_);
2331 
2332   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_);
2333 
2334   EXPECT_FALSE(device->IsConnected());
2335   EXPECT_FALSE(device->IsConnecting());
2336   EXPECT_FALSE(device->IsPaired());
2337 }
2338 
TEST_F(BluetoothChromeOSTest,PairingFailsAtConnection)2339 TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) {
2340   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2341 
2342   GetAdapter();
2343   DiscoverDevices();
2344 
2345   // Everything seems to go according to plan with the unconnectable device;
2346   // it pairs, but then you can't make connections to it after.
2347   BluetoothDevice* device = adapter_->GetDevice(
2348       FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
2349   ASSERT_TRUE(device != NULL);
2350   ASSERT_FALSE(device->IsPaired());
2351 
2352   TestObserver observer(adapter_);
2353 
2354   TestPairingDelegate pairing_delegate;
2355   device->Connect(
2356       &pairing_delegate,
2357       base::Bind(&BluetoothChromeOSTest::Callback,
2358                  base::Unretained(this)),
2359       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2360                  base::Unretained(this)));
2361 
2362   EXPECT_EQ(0, pairing_delegate.call_count_);
2363   EXPECT_TRUE(device->IsConnecting());
2364 
2365   message_loop_.Run();
2366 
2367   EXPECT_EQ(0, callback_count_);
2368   EXPECT_EQ(1, error_callback_count_);
2369   EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2370 
2371   // Two changes for connecting, one for paired and one for trusted after
2372   // pairing. The device should not be connected.
2373   EXPECT_EQ(4, observer.device_changed_count_);
2374   EXPECT_EQ(device, observer.last_device_);
2375 
2376   EXPECT_FALSE(device->IsConnected());
2377   EXPECT_FALSE(device->IsConnecting());
2378 
2379   EXPECT_TRUE(device->IsPaired());
2380 
2381   // Make sure the trusted property has been set to true still (since pairing
2382   // worked).
2383   FakeBluetoothDeviceClient::Properties* properties =
2384       fake_bluetooth_device_client_->GetProperties(
2385           dbus::ObjectPath(
2386               FakeBluetoothDeviceClient::kUnconnectableDevicePath));
2387   EXPECT_TRUE(properties->trusted.value());
2388 }
2389 
TEST_F(BluetoothChromeOSTest,PairingRejectedAtPinCode)2390 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) {
2391   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2392 
2393   GetAdapter();
2394   DiscoverDevices();
2395 
2396   // Reject the pairing after we receive a request for the PIN code.
2397   BluetoothDevice* device = adapter_->GetDevice(
2398       FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2399   ASSERT_TRUE(device != NULL);
2400   ASSERT_FALSE(device->IsPaired());
2401 
2402   TestObserver observer(adapter_);
2403 
2404   TestPairingDelegate pairing_delegate;
2405   device->Connect(
2406       &pairing_delegate,
2407       base::Bind(&BluetoothChromeOSTest::Callback,
2408                  base::Unretained(this)),
2409       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2410                  base::Unretained(this)));
2411 
2412   EXPECT_EQ(1, pairing_delegate.call_count_);
2413   EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2414   EXPECT_TRUE(device->IsConnecting());
2415 
2416   // Reject the pairing.
2417   device->RejectPairing();
2418   message_loop_.Run();
2419 
2420   EXPECT_EQ(0, callback_count_);
2421   EXPECT_EQ(1, error_callback_count_);
2422   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
2423 
2424   // Should be no changes except connecting going true and false.
2425   EXPECT_EQ(2, observer.device_changed_count_);
2426   EXPECT_FALSE(device->IsConnected());
2427   EXPECT_FALSE(device->IsConnecting());
2428   EXPECT_FALSE(device->IsPaired());
2429 }
2430 
TEST_F(BluetoothChromeOSTest,PairingCancelledAtPinCode)2431 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) {
2432   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2433 
2434   GetAdapter();
2435   DiscoverDevices();
2436 
2437   // Cancel the pairing after we receive a request for the PIN code.
2438   BluetoothDevice* device = adapter_->GetDevice(
2439       FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2440   ASSERT_TRUE(device != NULL);
2441   ASSERT_FALSE(device->IsPaired());
2442 
2443   TestObserver observer(adapter_);
2444 
2445   TestPairingDelegate pairing_delegate;
2446   device->Connect(
2447       &pairing_delegate,
2448       base::Bind(&BluetoothChromeOSTest::Callback,
2449                  base::Unretained(this)),
2450       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2451                  base::Unretained(this)));
2452 
2453   EXPECT_EQ(1, pairing_delegate.call_count_);
2454   EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2455   EXPECT_TRUE(device->IsConnecting());
2456 
2457   // Cancel the pairing.
2458   device->CancelPairing();
2459   message_loop_.Run();
2460 
2461   EXPECT_EQ(0, callback_count_);
2462   EXPECT_EQ(1, error_callback_count_);
2463   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2464 
2465   // Should be no changes except connecting going true and false.
2466   EXPECT_EQ(2, observer.device_changed_count_);
2467   EXPECT_FALSE(device->IsConnected());
2468   EXPECT_FALSE(device->IsConnecting());
2469   EXPECT_FALSE(device->IsPaired());
2470 }
2471 
TEST_F(BluetoothChromeOSTest,PairingRejectedAtPasskey)2472 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) {
2473   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2474 
2475   GetAdapter();
2476   DiscoverDevices();
2477 
2478   // Reject the pairing after we receive a request for the passkey.
2479   BluetoothDevice* device = adapter_->GetDevice(
2480       FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2481   ASSERT_TRUE(device != NULL);
2482   ASSERT_FALSE(device->IsPaired());
2483 
2484   TestObserver observer(adapter_);
2485 
2486   TestPairingDelegate pairing_delegate;
2487   device->Connect(
2488       &pairing_delegate,
2489       base::Bind(&BluetoothChromeOSTest::Callback,
2490                  base::Unretained(this)),
2491       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2492                  base::Unretained(this)));
2493 
2494   EXPECT_EQ(1, pairing_delegate.call_count_);
2495   EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2496   EXPECT_TRUE(device->IsConnecting());
2497 
2498   // Reject the pairing.
2499   device->RejectPairing();
2500   message_loop_.Run();
2501 
2502   EXPECT_EQ(0, callback_count_);
2503   EXPECT_EQ(1, error_callback_count_);
2504   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
2505 
2506   // Should be no changes except connecting going true and false.
2507   EXPECT_EQ(2, observer.device_changed_count_);
2508   EXPECT_FALSE(device->IsConnected());
2509   EXPECT_FALSE(device->IsConnecting());
2510   EXPECT_FALSE(device->IsPaired());
2511 }
2512 
TEST_F(BluetoothChromeOSTest,PairingCancelledAtPasskey)2513 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) {
2514   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2515 
2516   GetAdapter();
2517   DiscoverDevices();
2518 
2519   // Cancel the pairing after we receive a request for the passkey.
2520   BluetoothDevice* device = adapter_->GetDevice(
2521       FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2522   ASSERT_TRUE(device != NULL);
2523   ASSERT_FALSE(device->IsPaired());
2524 
2525   TestObserver observer(adapter_);
2526 
2527   TestPairingDelegate pairing_delegate;
2528   device->Connect(
2529       &pairing_delegate,
2530       base::Bind(&BluetoothChromeOSTest::Callback,
2531                  base::Unretained(this)),
2532       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2533                  base::Unretained(this)));
2534 
2535   EXPECT_EQ(1, pairing_delegate.call_count_);
2536   EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2537   EXPECT_TRUE(device->IsConnecting());
2538 
2539   // Cancel the pairing.
2540   device->CancelPairing();
2541   message_loop_.Run();
2542 
2543   EXPECT_EQ(0, callback_count_);
2544   EXPECT_EQ(1, error_callback_count_);
2545   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2546 
2547   // Should be no changes except connecting going true and false.
2548   EXPECT_EQ(2, observer.device_changed_count_);
2549   EXPECT_FALSE(device->IsConnected());
2550   EXPECT_FALSE(device->IsConnecting());
2551   EXPECT_FALSE(device->IsPaired());
2552 }
2553 
TEST_F(BluetoothChromeOSTest,PairingRejectedAtConfirmation)2554 TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) {
2555   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2556 
2557   GetAdapter();
2558   DiscoverDevices();
2559 
2560   // Reject the pairing after we receive a request for passkey confirmation.
2561   BluetoothDevice* device = adapter_->GetDevice(
2562       FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2563   ASSERT_TRUE(device != NULL);
2564   ASSERT_FALSE(device->IsPaired());
2565 
2566   TestObserver observer(adapter_);
2567 
2568   TestPairingDelegate pairing_delegate;
2569   device->Connect(
2570       &pairing_delegate,
2571       base::Bind(&BluetoothChromeOSTest::Callback,
2572                  base::Unretained(this)),
2573       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2574                  base::Unretained(this)));
2575 
2576   EXPECT_EQ(1, pairing_delegate.call_count_);
2577   EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2578   EXPECT_TRUE(device->IsConnecting());
2579 
2580   // Reject the pairing.
2581   device->RejectPairing();
2582   message_loop_.Run();
2583 
2584   EXPECT_EQ(0, callback_count_);
2585   EXPECT_EQ(1, error_callback_count_);
2586   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
2587 
2588   // Should be no changes except connecting going true and false.
2589   EXPECT_EQ(2, observer.device_changed_count_);
2590   EXPECT_FALSE(device->IsConnected());
2591   EXPECT_FALSE(device->IsConnecting());
2592   EXPECT_FALSE(device->IsPaired());
2593 }
2594 
TEST_F(BluetoothChromeOSTest,PairingCancelledAtConfirmation)2595 TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) {
2596   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2597 
2598   GetAdapter();
2599   DiscoverDevices();
2600 
2601   // Cancel the pairing after we receive a request for the passkey.
2602   BluetoothDevice* device = adapter_->GetDevice(
2603       FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2604   ASSERT_TRUE(device != NULL);
2605   ASSERT_FALSE(device->IsPaired());
2606 
2607   TestObserver observer(adapter_);
2608 
2609   TestPairingDelegate pairing_delegate;
2610   device->Connect(
2611       &pairing_delegate,
2612       base::Bind(&BluetoothChromeOSTest::Callback,
2613                  base::Unretained(this)),
2614       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2615                  base::Unretained(this)));
2616 
2617   EXPECT_EQ(1, pairing_delegate.call_count_);
2618   EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2619   EXPECT_TRUE(device->IsConnecting());
2620 
2621   // Cancel the pairing.
2622   device->CancelPairing();
2623   message_loop_.Run();
2624 
2625   EXPECT_EQ(0, callback_count_);
2626   EXPECT_EQ(1, error_callback_count_);
2627   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2628 
2629   // Should be no changes except connecting going true and false.
2630   EXPECT_EQ(2, observer.device_changed_count_);
2631   EXPECT_FALSE(device->IsConnected());
2632   EXPECT_FALSE(device->IsConnecting());
2633   EXPECT_FALSE(device->IsPaired());
2634 }
2635 
TEST_F(BluetoothChromeOSTest,PairingCancelledInFlight)2636 TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) {
2637   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2638 
2639   GetAdapter();
2640   DiscoverDevices();
2641 
2642   // Cancel the pairing while we're waiting for the remote host.
2643   BluetoothDevice* device = adapter_->GetDevice(
2644       FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2645   ASSERT_TRUE(device != NULL);
2646   ASSERT_FALSE(device->IsPaired());
2647 
2648   TestObserver observer(adapter_);
2649 
2650   TestPairingDelegate pairing_delegate;
2651   device->Connect(
2652       &pairing_delegate,
2653       base::Bind(&BluetoothChromeOSTest::Callback,
2654                  base::Unretained(this)),
2655       base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2656                  base::Unretained(this)));
2657 
2658   EXPECT_EQ(0, pairing_delegate.call_count_);
2659   EXPECT_TRUE(device->IsConnecting());
2660 
2661   // Cancel the pairing.
2662   device->CancelPairing();
2663   message_loop_.Run();
2664 
2665   EXPECT_EQ(0, callback_count_);
2666   EXPECT_EQ(1, error_callback_count_);
2667   EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2668 
2669   // Should be no changes except connecting going true and false.
2670   EXPECT_EQ(2, observer.device_changed_count_);
2671   EXPECT_FALSE(device->IsConnected());
2672   EXPECT_FALSE(device->IsConnecting());
2673   EXPECT_FALSE(device->IsPaired());
2674 }
2675 
TEST_F(BluetoothChromeOSTest,IncomingPairRequestPinCode)2676 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) {
2677   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2678 
2679   GetAdapter();
2680 
2681   TestPairingDelegate pairing_delegate;
2682   adapter_->AddPairingDelegate(
2683       &pairing_delegate,
2684       BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2685 
2686   // Requires that we provide a PIN code.
2687   fake_bluetooth_device_client_->CreateDevice(
2688       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2689       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2690   BluetoothDevice* device = adapter_->GetDevice(
2691       FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2692   ASSERT_TRUE(device != NULL);
2693   ASSERT_FALSE(device->IsPaired());
2694 
2695   TestObserver observer(adapter_);
2696 
2697   fake_bluetooth_device_client_->SimulatePairing(
2698       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath),
2699       true,
2700       base::Bind(&BluetoothChromeOSTest::Callback,
2701                  base::Unretained(this)),
2702       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2703                  base::Unretained(this)));
2704 
2705   EXPECT_EQ(1, pairing_delegate.call_count_);
2706   EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2707 
2708   // Set the PIN.
2709   device->SetPinCode("1234");
2710   message_loop_.Run();
2711 
2712   EXPECT_EQ(1, callback_count_);
2713   EXPECT_EQ(0, error_callback_count_);
2714 
2715   // One change for paired, and one for trusted.
2716   EXPECT_EQ(2, observer.device_changed_count_);
2717   EXPECT_EQ(device, observer.last_device_);
2718 
2719   EXPECT_TRUE(device->IsPaired());
2720 
2721   // Make sure the trusted property has been set to true.
2722   FakeBluetoothDeviceClient::Properties* properties =
2723       fake_bluetooth_device_client_->GetProperties(
2724           dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2725   ASSERT_TRUE(properties->trusted.value());
2726 
2727   // No pairing context should remain on the device.
2728   BluetoothDeviceChromeOS* device_chromeos =
2729       static_cast<BluetoothDeviceChromeOS*>(device);
2730   EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2731 }
2732 
TEST_F(BluetoothChromeOSTest,IncomingPairConfirmPasskey)2733 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) {
2734   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2735 
2736   GetAdapter();
2737 
2738   TestPairingDelegate pairing_delegate;
2739   adapter_->AddPairingDelegate(
2740       &pairing_delegate,
2741       BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2742 
2743   // Requests that we confirm a displayed passkey.
2744   fake_bluetooth_device_client_->CreateDevice(
2745       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2746       dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2747   BluetoothDevice* device = adapter_->GetDevice(
2748       FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2749   ASSERT_TRUE(device != NULL);
2750   ASSERT_FALSE(device->IsPaired());
2751 
2752   TestObserver observer(adapter_);
2753 
2754   fake_bluetooth_device_client_->SimulatePairing(
2755       dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath),
2756       true,
2757       base::Bind(&BluetoothChromeOSTest::Callback,
2758                  base::Unretained(this)),
2759       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2760                  base::Unretained(this)));
2761 
2762   EXPECT_EQ(1, pairing_delegate.call_count_);
2763   EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2764   EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2765 
2766   // Confirm the passkey.
2767   device->ConfirmPairing();
2768   message_loop_.Run();
2769 
2770   EXPECT_EQ(1, callback_count_);
2771   EXPECT_EQ(0, error_callback_count_);
2772 
2773   // One change for paired, and one for trusted.
2774   EXPECT_EQ(2, observer.device_changed_count_);
2775   EXPECT_EQ(device, observer.last_device_);
2776 
2777   EXPECT_TRUE(device->IsPaired());
2778 
2779   // Make sure the trusted property has been set to true.
2780   FakeBluetoothDeviceClient::Properties* properties =
2781       fake_bluetooth_device_client_->GetProperties(
2782           dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2783   ASSERT_TRUE(properties->trusted.value());
2784 
2785   // No pairing context should remain on the device.
2786   BluetoothDeviceChromeOS* device_chromeos =
2787       static_cast<BluetoothDeviceChromeOS*>(device);
2788   EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2789 }
2790 
TEST_F(BluetoothChromeOSTest,IncomingPairRequestPasskey)2791 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) {
2792   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2793 
2794   GetAdapter();
2795 
2796   TestPairingDelegate pairing_delegate;
2797   adapter_->AddPairingDelegate(
2798       &pairing_delegate,
2799       BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2800 
2801   // Requests that we provide a Passkey.
2802   fake_bluetooth_device_client_->CreateDevice(
2803       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2804       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2805   BluetoothDevice* device = adapter_->GetDevice(
2806       FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2807   ASSERT_TRUE(device != NULL);
2808   ASSERT_FALSE(device->IsPaired());
2809 
2810   TestObserver observer(adapter_);
2811 
2812   fake_bluetooth_device_client_->SimulatePairing(
2813       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath),
2814       true,
2815       base::Bind(&BluetoothChromeOSTest::Callback,
2816                  base::Unretained(this)),
2817       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2818                  base::Unretained(this)));
2819 
2820   EXPECT_EQ(1, pairing_delegate.call_count_);
2821   EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2822 
2823   // Set the Passkey.
2824   device->SetPasskey(1234);
2825   message_loop_.Run();
2826 
2827   EXPECT_EQ(1, callback_count_);
2828   EXPECT_EQ(0, error_callback_count_);
2829 
2830   // One change for paired, and one for trusted.
2831   EXPECT_EQ(2, observer.device_changed_count_);
2832   EXPECT_EQ(device, observer.last_device_);
2833 
2834   EXPECT_TRUE(device->IsPaired());
2835 
2836   // Make sure the trusted property has been set to true.
2837   FakeBluetoothDeviceClient::Properties* properties =
2838       fake_bluetooth_device_client_->GetProperties(
2839           dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2840   ASSERT_TRUE(properties->trusted.value());
2841 
2842   // No pairing context should remain on the device.
2843   BluetoothDeviceChromeOS* device_chromeos =
2844       static_cast<BluetoothDeviceChromeOS*>(device);
2845   EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2846 }
2847 
TEST_F(BluetoothChromeOSTest,IncomingPairJustWorks)2848 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) {
2849   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2850 
2851   GetAdapter();
2852 
2853   TestPairingDelegate pairing_delegate;
2854   adapter_->AddPairingDelegate(
2855       &pairing_delegate,
2856       BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2857 
2858   // Uses just-works pairing so, sinec this an incoming pairing, require
2859   // authorization from the user.
2860   fake_bluetooth_device_client_->CreateDevice(
2861       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2862       dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2863   BluetoothDevice* device = adapter_->GetDevice(
2864       FakeBluetoothDeviceClient::kJustWorksAddress);
2865   ASSERT_TRUE(device != NULL);
2866   ASSERT_FALSE(device->IsPaired());
2867 
2868   TestObserver observer(adapter_);
2869 
2870   fake_bluetooth_device_client_->SimulatePairing(
2871       dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath),
2872       true,
2873       base::Bind(&BluetoothChromeOSTest::Callback,
2874                  base::Unretained(this)),
2875       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2876                  base::Unretained(this)));
2877 
2878   EXPECT_EQ(1, pairing_delegate.call_count_);
2879   EXPECT_EQ(1, pairing_delegate.authorize_pairing_count_);
2880 
2881   // Confirm the pairing.
2882   device->ConfirmPairing();
2883   message_loop_.Run();
2884 
2885   EXPECT_EQ(1, callback_count_);
2886   EXPECT_EQ(0, error_callback_count_);
2887 
2888   // One change for paired, and one for trusted.
2889   EXPECT_EQ(2, observer.device_changed_count_);
2890   EXPECT_EQ(device, observer.last_device_);
2891 
2892   EXPECT_TRUE(device->IsPaired());
2893 
2894   // Make sure the trusted property has been set to true.
2895   FakeBluetoothDeviceClient::Properties* properties =
2896       fake_bluetooth_device_client_->GetProperties(
2897           dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2898   ASSERT_TRUE(properties->trusted.value());
2899 
2900   // No pairing context should remain on the device.
2901   BluetoothDeviceChromeOS* device_chromeos =
2902       static_cast<BluetoothDeviceChromeOS*>(device);
2903   EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2904 }
2905 
TEST_F(BluetoothChromeOSTest,IncomingPairRequestPinCodeWithoutDelegate)2906 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) {
2907   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2908 
2909   GetAdapter();
2910 
2911   // Requires that we provide a PIN Code, without a pairing delegate,
2912   // that will be rejected.
2913   fake_bluetooth_device_client_->CreateDevice(
2914       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2915       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2916   BluetoothDevice* device = adapter_->GetDevice(
2917       FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2918   ASSERT_TRUE(device != NULL);
2919   ASSERT_FALSE(device->IsPaired());
2920 
2921   TestObserver observer(adapter_);
2922 
2923   fake_bluetooth_device_client_->SimulatePairing(
2924       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath),
2925       true,
2926       base::Bind(&BluetoothChromeOSTest::Callback,
2927                  base::Unretained(this)),
2928       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2929                  base::Unretained(this)));
2930 
2931   message_loop_.Run();
2932 
2933   EXPECT_EQ(0, callback_count_);
2934   EXPECT_EQ(1, error_callback_count_);
2935   EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
2936 
2937   // No changes should be observer.
2938   EXPECT_EQ(0, observer.device_changed_count_);
2939 
2940   EXPECT_FALSE(device->IsPaired());
2941 
2942   // No pairing context should remain on the device.
2943   BluetoothDeviceChromeOS* device_chromeos =
2944       static_cast<BluetoothDeviceChromeOS*>(device);
2945   EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2946 }
2947 
TEST_F(BluetoothChromeOSTest,IncomingPairConfirmPasskeyWithoutDelegate)2948 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) {
2949   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2950 
2951   GetAdapter();
2952 
2953   // Requests that we confirm a displayed passkey, without a pairing delegate,
2954   // that will be rejected.
2955   fake_bluetooth_device_client_->CreateDevice(
2956       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2957       dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2958   BluetoothDevice* device = adapter_->GetDevice(
2959       FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2960   ASSERT_TRUE(device != NULL);
2961   ASSERT_FALSE(device->IsPaired());
2962 
2963   TestObserver observer(adapter_);
2964 
2965   fake_bluetooth_device_client_->SimulatePairing(
2966       dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath),
2967       true,
2968       base::Bind(&BluetoothChromeOSTest::Callback,
2969                  base::Unretained(this)),
2970       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2971                  base::Unretained(this)));
2972 
2973   message_loop_.Run();
2974 
2975   EXPECT_EQ(0, callback_count_);
2976   EXPECT_EQ(1, error_callback_count_);
2977   EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
2978 
2979   // No changes should be observer.
2980   EXPECT_EQ(0, observer.device_changed_count_);
2981 
2982   EXPECT_FALSE(device->IsPaired());
2983 
2984   // No pairing context should remain on the device.
2985   BluetoothDeviceChromeOS* device_chromeos =
2986       static_cast<BluetoothDeviceChromeOS*>(device);
2987   EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2988 }
2989 
TEST_F(BluetoothChromeOSTest,IncomingPairRequestPasskeyWithoutDelegate)2990 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) {
2991   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2992 
2993   GetAdapter();
2994 
2995   // Requests that we provide a displayed passkey, without a pairing delegate,
2996   // that will be rejected.
2997   fake_bluetooth_device_client_->CreateDevice(
2998       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2999       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3000   BluetoothDevice* device = adapter_->GetDevice(
3001       FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3002   ASSERT_TRUE(device != NULL);
3003   ASSERT_FALSE(device->IsPaired());
3004 
3005   TestObserver observer(adapter_);
3006 
3007   fake_bluetooth_device_client_->SimulatePairing(
3008       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath),
3009       true,
3010       base::Bind(&BluetoothChromeOSTest::Callback,
3011                  base::Unretained(this)),
3012       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3013                  base::Unretained(this)));
3014 
3015   message_loop_.Run();
3016 
3017   EXPECT_EQ(0, callback_count_);
3018   EXPECT_EQ(1, error_callback_count_);
3019   EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3020 
3021   // No changes should be observer.
3022   EXPECT_EQ(0, observer.device_changed_count_);
3023 
3024   EXPECT_FALSE(device->IsPaired());
3025 
3026   // No pairing context should remain on the device.
3027   BluetoothDeviceChromeOS* device_chromeos =
3028       static_cast<BluetoothDeviceChromeOS*>(device);
3029   EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3030 }
3031 
TEST_F(BluetoothChromeOSTest,IncomingPairJustWorksWithoutDelegate)3032 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) {
3033   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3034 
3035   GetAdapter();
3036 
3037   // Uses just-works pairing and thus requires authorization for incoming
3038   // pairings, without a pairing delegate, that will be rejected.
3039   fake_bluetooth_device_client_->CreateDevice(
3040       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3041       dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3042   BluetoothDevice* device = adapter_->GetDevice(
3043       FakeBluetoothDeviceClient::kJustWorksAddress);
3044   ASSERT_TRUE(device != NULL);
3045   ASSERT_FALSE(device->IsPaired());
3046 
3047   TestObserver observer(adapter_);
3048 
3049   fake_bluetooth_device_client_->SimulatePairing(
3050       dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath),
3051       true,
3052       base::Bind(&BluetoothChromeOSTest::Callback,
3053                  base::Unretained(this)),
3054       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3055                  base::Unretained(this)));
3056 
3057   message_loop_.Run();
3058 
3059   EXPECT_EQ(0, callback_count_);
3060   EXPECT_EQ(1, error_callback_count_);
3061   EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3062 
3063   // No changes should be observer.
3064   EXPECT_EQ(0, observer.device_changed_count_);
3065 
3066   EXPECT_FALSE(device->IsPaired());
3067 
3068   // No pairing context should remain on the device.
3069   BluetoothDeviceChromeOS* device_chromeos =
3070       static_cast<BluetoothDeviceChromeOS*>(device);
3071   EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3072 }
3073 
TEST_F(BluetoothChromeOSTest,RemovePairingDelegateDuringPairing)3074 TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) {
3075   fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3076 
3077   GetAdapter();
3078 
3079   TestPairingDelegate pairing_delegate;
3080   adapter_->AddPairingDelegate(
3081       &pairing_delegate,
3082       BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3083 
3084   // Requests that we provide a Passkey.
3085   fake_bluetooth_device_client_->CreateDevice(
3086       dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3087       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3088   BluetoothDevice* device = adapter_->GetDevice(
3089       FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3090   ASSERT_TRUE(device != NULL);
3091   ASSERT_FALSE(device->IsPaired());
3092 
3093   TestObserver observer(adapter_);
3094 
3095   fake_bluetooth_device_client_->SimulatePairing(
3096       dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath),
3097       true,
3098       base::Bind(&BluetoothChromeOSTest::Callback,
3099                  base::Unretained(this)),
3100       base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3101                  base::Unretained(this)));
3102 
3103   EXPECT_EQ(1, pairing_delegate.call_count_);
3104   EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3105 
3106   // A pairing context should now be set on the device.
3107   BluetoothDeviceChromeOS* device_chromeos =
3108       static_cast<BluetoothDeviceChromeOS*>(device);
3109   ASSERT_TRUE(device_chromeos->GetPairing() != NULL);
3110 
3111   // Removing the pairing delegate should remove that pairing context.
3112   adapter_->RemovePairingDelegate(&pairing_delegate);
3113 
3114   EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3115 
3116   // Set the Passkey, this should now have no effect since the pairing has
3117   // been, in-effect, cancelled
3118   device->SetPasskey(1234);
3119 
3120   EXPECT_EQ(0, callback_count_);
3121   EXPECT_EQ(0, error_callback_count_);
3122   EXPECT_EQ(0, observer.device_changed_count_);
3123 
3124   EXPECT_FALSE(device->IsPaired());
3125 }
3126 
TEST_F(BluetoothChromeOSTest,DeviceId)3127 TEST_F(BluetoothChromeOSTest, DeviceId) {
3128   GetAdapter();
3129 
3130   // Use the built-in paired device for this test, grab its Properties
3131   // structure so we can adjust the underlying modalias property.
3132   BluetoothDevice* device = adapter_->GetDevice(
3133       FakeBluetoothDeviceClient::kPairedDeviceAddress);
3134   FakeBluetoothDeviceClient::Properties* properties =
3135       fake_bluetooth_device_client_->GetProperties(
3136           dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
3137 
3138   ASSERT_TRUE(device != NULL);
3139   ASSERT_TRUE(properties != NULL);
3140 
3141   // Valid USB IF-assigned identifier.
3142   ASSERT_EQ("usb:v05ACp030Dd0306", properties->modalias.value());
3143 
3144   EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, device->GetVendorIDSource());
3145   EXPECT_EQ(0x05ac, device->GetVendorID());
3146   EXPECT_EQ(0x030d, device->GetProductID());
3147   EXPECT_EQ(0x0306, device->GetDeviceID());
3148 
3149   // Valid Bluetooth SIG-assigned identifier.
3150   properties->modalias.ReplaceValue("bluetooth:v00E0p2400d0400");
3151 
3152   EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH, device->GetVendorIDSource());
3153   EXPECT_EQ(0x00e0, device->GetVendorID());
3154   EXPECT_EQ(0x2400, device->GetProductID());
3155   EXPECT_EQ(0x0400, device->GetDeviceID());
3156 
3157   // Invalid USB IF-assigned identifier.
3158   properties->modalias.ReplaceValue("usb:x00E0p2400d0400");
3159 
3160   EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3161   EXPECT_EQ(0, device->GetVendorID());
3162   EXPECT_EQ(0, device->GetProductID());
3163   EXPECT_EQ(0, device->GetDeviceID());
3164 
3165   // Invalid Bluetooth SIG-assigned identifier.
3166   properties->modalias.ReplaceValue("bluetooth:x00E0p2400d0400");
3167 
3168   EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3169   EXPECT_EQ(0, device->GetVendorID());
3170   EXPECT_EQ(0, device->GetProductID());
3171   EXPECT_EQ(0, device->GetDeviceID());
3172 
3173   // Unknown vendor specification identifier.
3174   properties->modalias.ReplaceValue("chrome:v00E0p2400d0400");
3175 
3176   EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3177   EXPECT_EQ(0, device->GetVendorID());
3178   EXPECT_EQ(0, device->GetProductID());
3179   EXPECT_EQ(0, device->GetDeviceID());
3180 }
3181 
3182 }  // namespace chromeos
3183