| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | |
| 6 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_chromeos.h" | |
| 7 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_factory.h" | |
| 8 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" | |
| 9 #include "chromeos/dbus/mock_bluetooth_adapter_client.h" | 5 #include "chromeos/dbus/mock_bluetooth_adapter_client.h" |
| 10 #include "chromeos/dbus/mock_bluetooth_device_client.h" | 6 #include "chromeos/dbus/mock_bluetooth_device_client.h" |
| 11 #include "chromeos/dbus/mock_bluetooth_manager_client.h" | 7 #include "chromeos/dbus/mock_bluetooth_manager_client.h" |
| 12 #include "chromeos/dbus/mock_dbus_thread_manager.h" | 8 #include "chromeos/dbus/mock_dbus_thread_manager.h" |
| 13 #include "dbus/object_path.h" | 9 #include "dbus/object_path.h" |
| 10 #include "device/bluetooth/bluetooth_adapter.h" |
| 11 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 13 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using ::testing::_; | 16 using ::testing::_; |
| 17 using ::testing::Mock; | 17 using ::testing::Mock; |
| 18 using ::testing::Return; | 18 using ::testing::Return; |
| 19 using ::testing::SaveArg; | 19 using ::testing::SaveArg; |
| 20 | 20 |
| 21 namespace chromeos { | 21 namespace bluetooth { |
| 22 | 22 |
| 23 class BluetoothAdapterDevicesChromeOsTest : public testing::Test { | 23 class BluetoothAdapterChromeOsDevicesTest : public testing::Test { |
| 24 public: | 24 public: |
| 25 virtual void SetUp() { | 25 virtual void SetUp() { |
| 26 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager; | 26 chromeos::MockDBusThreadManager* mock_dbus_thread_manager = |
| 27 new chromeos::MockDBusThreadManager; |
| 27 | 28 |
| 28 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus()) | 29 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus()) |
| 29 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); | 30 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); |
| 30 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); | 31 chromeos::DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); |
| 31 | 32 |
| 32 mock_manager_client_ = | 33 mock_manager_client_ = |
| 33 mock_dbus_thread_manager->mock_bluetooth_manager_client(); | 34 mock_dbus_thread_manager->mock_bluetooth_manager_client(); |
| 34 mock_adapter_client_ = | 35 mock_adapter_client_ = |
| 35 mock_dbus_thread_manager->mock_bluetooth_adapter_client(); | 36 mock_dbus_thread_manager->mock_bluetooth_adapter_client(); |
| 36 mock_device_client_ = | 37 mock_device_client_ = |
| 37 mock_dbus_thread_manager->mock_bluetooth_device_client(); | 38 mock_dbus_thread_manager->mock_bluetooth_device_client(); |
| 38 | 39 |
| 39 // Create the default adapter instance; | 40 // Create the default adapter instance; |
| 40 // BluetoothManagerClient::DefaultAdapter will be called once, passing | 41 // BluetoothManagerClient::DefaultAdapter will be called once, passing |
| 41 // a callback to obtain the adapter path. | 42 // a callback to obtain the adapter path. |
| 42 BluetoothManagerClient::AdapterCallback adapter_callback; | 43 chromeos::BluetoothManagerClient::AdapterCallback adapter_callback; |
| 43 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | 44 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) |
| 44 .WillOnce(SaveArg<0>(&adapter_callback)); | 45 .WillOnce(SaveArg<0>(&adapter_callback)); |
| 45 | 46 |
| 46 EXPECT_CALL(*mock_manager_client_, AddObserver(_)) | 47 EXPECT_CALL(*mock_manager_client_, AddObserver(_)) |
| 47 .Times(1); | 48 .Times(1); |
| 48 EXPECT_CALL(*mock_adapter_client_, AddObserver(_)) | 49 EXPECT_CALL(*mock_adapter_client_, AddObserver(_)) |
| 49 .Times(1); | 50 .Times(1); |
| 50 | 51 |
| 51 adapter_ = BluetoothAdapterFactory::DefaultAdapter(); | 52 adapter_ = BluetoothAdapterFactory::DefaultAdapter(); |
| 53 ASSERT_TRUE(adapter_.get() != NULL); |
| 52 | 54 |
| 53 // Call the adapter callback; | 55 // Call the adapter callback; |
| 54 // BluetoothAdapterClient::GetProperties will be called once to obtain | 56 // BluetoothAdapterClient::GetProperties will be called once to obtain |
| 55 // the property set. | 57 // the property set. |
| 56 MockBluetoothAdapterClient::Properties adapter_properties; | 58 chromeos::MockBluetoothAdapterClient::Properties adapter_properties; |
| 57 adapter_properties.address.ReplaceValue(adapter_address_); | 59 adapter_properties.address.ReplaceValue(adapter_address_); |
| 58 adapter_properties.powered.ReplaceValue(true); | 60 adapter_properties.powered.ReplaceValue(true); |
| 59 | 61 |
| 60 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path_)) | 62 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path_)) |
| 61 .WillRepeatedly(Return(&adapter_properties)); | 63 .WillRepeatedly(Return(&adapter_properties)); |
| 62 | 64 |
| 63 // Add an observer to the adapter; expect the usual set of changes to | 65 // Add an observer to the adapter; expect the usual set of changes to |
| 64 // an adapter becoming present and then clear to clean up for the test. | 66 // an adapter becoming present and then clear to clean up for the test. |
| 65 adapter_->AddObserver(&adapter_observer_); | 67 adapter_->AddObserver(&adapter_observer_); |
| 66 | 68 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 81 BluetoothAdapterChromeOs* adapter_chromeos = | 83 BluetoothAdapterChromeOs* adapter_chromeos = |
| 82 static_cast<BluetoothAdapterChromeOs*>(adapter_.get()); | 84 static_cast<BluetoothAdapterChromeOs*>(adapter_.get()); |
| 83 EXPECT_CALL(*mock_device_client_, RemoveObserver(adapter_chromeos)) | 85 EXPECT_CALL(*mock_device_client_, RemoveObserver(adapter_chromeos)) |
| 84 .Times(1); | 86 .Times(1); |
| 85 EXPECT_CALL(*mock_adapter_client_, RemoveObserver(adapter_chromeos)) | 87 EXPECT_CALL(*mock_adapter_client_, RemoveObserver(adapter_chromeos)) |
| 86 .Times(1); | 88 .Times(1); |
| 87 EXPECT_CALL(*mock_manager_client_, RemoveObserver(adapter_chromeos)) | 89 EXPECT_CALL(*mock_manager_client_, RemoveObserver(adapter_chromeos)) |
| 88 .Times(1); | 90 .Times(1); |
| 89 | 91 |
| 90 adapter_ = NULL; | 92 adapter_ = NULL; |
| 91 DBusThreadManager::Shutdown(); | 93 chromeos::DBusThreadManager::Shutdown(); |
| 92 } | 94 } |
| 93 | 95 |
| 94 protected: | 96 protected: |
| 95 MockBluetoothManagerClient* mock_manager_client_; | 97 chromeos::MockBluetoothManagerClient* mock_manager_client_; |
| 96 MockBluetoothAdapterClient* mock_adapter_client_; | 98 chromeos::MockBluetoothAdapterClient* mock_adapter_client_; |
| 97 MockBluetoothDeviceClient* mock_device_client_; | 99 chromeos::MockBluetoothDeviceClient* mock_device_client_; |
| 98 | 100 |
| 99 static const dbus::ObjectPath adapter_path_; | 101 static const dbus::ObjectPath adapter_path_; |
| 100 static const std::string adapter_address_; | 102 static const std::string adapter_address_; |
| 101 scoped_refptr<BluetoothAdapter> adapter_; | 103 scoped_refptr<BluetoothAdapter> adapter_; |
| 102 | 104 |
| 103 MockBluetoothAdapter::Observer adapter_observer_; | 105 MockBluetoothAdapter::Observer adapter_observer_; |
| 104 }; | 106 }; |
| 105 | 107 |
| 106 const dbus::ObjectPath BluetoothAdapterDevicesChromeOsTest::adapter_path_( | 108 const dbus::ObjectPath BluetoothAdapterChromeOsDevicesTest::adapter_path_( |
| 107 "/fake/hci0"); | 109 "/fake/hci0"); |
| 108 const std::string BluetoothAdapterDevicesChromeOsTest::adapter_address_ = | 110 const std::string BluetoothAdapterChromeOsDevicesTest::adapter_address_ = |
| 109 "CA:FE:4A:C0:FE:FE"; | 111 "CA:FE:4A:C0:FE:FE"; |
| 110 | 112 |
| 111 TEST_F(BluetoothAdapterDevicesChromeOsTest, DeviceRemovedAfterFound) { | 113 TEST_F(BluetoothAdapterChromeOsDevicesTest, DeviceRemovedAfterFound) { |
| 112 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_01"); | 114 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_01"); |
| 113 const std::string device_address = "BA:C0:11:00:00:01"; | 115 const std::string device_address = "BA:C0:11:00:00:01"; |
| 114 | 116 |
| 115 MockBluetoothDeviceClient::Properties device_properties; | 117 chromeos::MockBluetoothDeviceClient::Properties device_properties; |
| 116 device_properties.address.ReplaceValue(device_address); | 118 device_properties.address.ReplaceValue(device_address); |
| 117 device_properties.name.ReplaceValue("Fake Keyboard"); | 119 device_properties.name.ReplaceValue("Fake Keyboard"); |
| 118 device_properties.bluetooth_class.ReplaceValue(0x2540); | 120 device_properties.bluetooth_class.ReplaceValue(0x2540); |
| 119 | 121 |
| 120 // Inform the adapter that the device has been found; | 122 // Inform the adapter that the device has been found; |
| 121 // BluetoothAdapterClient::Observer::DeviceAdded will be called, passing | 123 // BluetoothAdapterClient::Observer::DeviceAdded will be called, passing |
| 122 // the device object. | 124 // the device object. |
| 123 BluetoothDevice* device; | 125 BluetoothDevice* device; |
| 124 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) | 126 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) |
| 125 .Times(1) | 127 .Times(1) |
| 126 .WillOnce(SaveArg<1>(&device)); | 128 .WillOnce(SaveArg<1>(&device)); |
| 127 | 129 |
| 128 BluetoothAdapterChromeOs* adapter_chromeos = | 130 BluetoothAdapterChromeOs* adapter_chromeos = |
| 129 static_cast<BluetoothAdapterChromeOs*>(adapter_.get()); | 131 static_cast<BluetoothAdapterChromeOs*>(adapter_.get()); |
| 130 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos) | 132 static_cast<chromeos::BluetoothAdapterClient::Observer*>(adapter_chromeos) |
| 131 ->DeviceFound(adapter_path_, device_address, device_properties); | 133 ->DeviceFound(adapter_path_, device_address, device_properties); |
| 132 | 134 |
| 133 // Now inform the adapter that the device has been added and assigned an | 135 // Now inform the adapter that the device has been added and assigned an |
| 134 // object path; BluetoothDeviceClient::GetProperties will be called to | 136 // object path; BluetoothDeviceClient::GetProperties will be called to |
| 135 // obtain the property set; and | 137 // obtain the property set; and |
| 136 // BluetoothAdapterClient::Observer::DeviceChanged will be called passing | 138 // BluetoothAdapterClient::Observer::DeviceChanged will be called passing |
| 137 // the same device object as before. | 139 // the same device object as before. |
| 138 EXPECT_CALL(*mock_device_client_, GetProperties(device_path)) | 140 EXPECT_CALL(*mock_device_client_, GetProperties(device_path)) |
| 139 .WillRepeatedly(Return(&device_properties)); | 141 .WillRepeatedly(Return(&device_properties)); |
| 140 | 142 |
| 141 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_chromeos, device)) | 143 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_chromeos, device)) |
| 142 .Times(1); | 144 .Times(1); |
| 143 | 145 |
| 144 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos) | 146 static_cast<chromeos::BluetoothAdapterClient::Observer*>(adapter_chromeos) |
| 145 ->DeviceCreated(adapter_path_, device_path); | 147 ->DeviceCreated(adapter_path_, device_path); |
| 146 | 148 |
| 147 // Finally remove the adapter again; | 149 // Finally remove the adapter again; |
| 148 // BluetoothAdapterClient::Observer::DeviceRemoved should be not called, | 150 // BluetoothAdapterClient::Observer::DeviceRemoved should be not called, |
| 149 // instead BluetoothAdapterClient::Observer::DeviceChanged will be called. | 151 // instead BluetoothAdapterClient::Observer::DeviceChanged will be called. |
| 150 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device)) | 152 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device)) |
| 151 .Times(0); | 153 .Times(0); |
| 152 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device)) | 154 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device)) |
| 153 .Times(1); | 155 .Times(1); |
| 154 | 156 |
| 155 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos) | 157 static_cast<chromeos::BluetoothAdapterClient::Observer*>(adapter_chromeos) |
| 156 ->DeviceRemoved(adapter_path_, device_path); | 158 ->DeviceRemoved(adapter_path_, device_path); |
| 157 | 159 |
| 158 // Verify that the device is still visible, just no longer paired. | 160 // Verify that the device is still visible, just no longer paired. |
| 159 EXPECT_TRUE(device->IsVisible()); | 161 EXPECT_TRUE(device->IsVisible()); |
| 160 EXPECT_FALSE(device->IsPaired()); | 162 EXPECT_FALSE(device->IsPaired()); |
| 161 } | 163 } |
| 162 | 164 |
| 163 } // namespace chromeos | 165 } // namespace bluetooth |
| OLD | NEW |