| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | |
| 6 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" | |
| 7 #include "chromeos/dbus/mock_bluetooth_adapter_client.h" | |
| 8 #include "chromeos/dbus/mock_bluetooth_device_client.h" | |
| 9 #include "chromeos/dbus/mock_bluetooth_manager_client.h" | |
| 10 #include "chromeos/dbus/mock_dbus_thread_manager.h" | |
| 11 #include "dbus/object_path.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 using ::testing::_; | |
| 15 using ::testing::Mock; | |
| 16 using ::testing::Return; | |
| 17 using ::testing::SaveArg; | |
| 18 | |
| 19 namespace chromeos { | |
| 20 | |
| 21 class BluetoothAdapterDevicesTest : public testing::Test { | |
| 22 public: | |
| 23 virtual void SetUp() { | |
| 24 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager; | |
| 25 | |
| 26 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus()) | |
| 27 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); | |
| 28 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); | |
| 29 | |
| 30 mock_manager_client_ = | |
| 31 mock_dbus_thread_manager->mock_bluetooth_manager_client(); | |
| 32 mock_adapter_client_ = | |
| 33 mock_dbus_thread_manager->mock_bluetooth_adapter_client(); | |
| 34 mock_device_client_ = | |
| 35 mock_dbus_thread_manager->mock_bluetooth_device_client(); | |
| 36 | |
| 37 // Create the default adapter instance; | |
| 38 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 39 // a callback to obtain the adapter path. | |
| 40 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 41 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 42 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 43 | |
| 44 EXPECT_CALL(*mock_manager_client_, AddObserver(_)) | |
| 45 .Times(1); | |
| 46 EXPECT_CALL(*mock_adapter_client_, AddObserver(_)) | |
| 47 .Times(1); | |
| 48 | |
| 49 adapter_ = BluetoothAdapter::DefaultAdapter(); | |
| 50 | |
| 51 // Call the adapter callback; | |
| 52 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 53 // the property set. | |
| 54 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 55 adapter_properties.address.ReplaceValue(adapter_address_); | |
| 56 adapter_properties.powered.ReplaceValue(true); | |
| 57 | |
| 58 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path_)) | |
| 59 .WillRepeatedly(Return(&adapter_properties)); | |
| 60 | |
| 61 // Add an observer to the adapter; expect the usual set of changes to | |
| 62 // an adapter becoming present and then clear to clean up for the test. | |
| 63 adapter_->AddObserver(&adapter_observer_); | |
| 64 | |
| 65 EXPECT_CALL(adapter_observer_, AdapterPresentChanged(adapter_.get(), true)) | |
| 66 .Times(1); | |
| 67 EXPECT_CALL(adapter_observer_, AdapterPoweredChanged(adapter_.get(), true)) | |
| 68 .Times(1); | |
| 69 | |
| 70 adapter_callback.Run(adapter_path_, true); | |
| 71 | |
| 72 Mock::VerifyAndClearExpectations(mock_manager_client_); | |
| 73 Mock::VerifyAndClearExpectations(mock_adapter_client_); | |
| 74 Mock::VerifyAndClearExpectations(mock_device_client_); | |
| 75 Mock::VerifyAndClearExpectations(&adapter_observer_); | |
| 76 } | |
| 77 | |
| 78 virtual void TearDown() { | |
| 79 EXPECT_CALL(*mock_device_client_, RemoveObserver(adapter_.get())) | |
| 80 .Times(1); | |
| 81 EXPECT_CALL(*mock_adapter_client_, RemoveObserver(adapter_.get())) | |
| 82 .Times(1); | |
| 83 EXPECT_CALL(*mock_manager_client_, RemoveObserver(adapter_.get())) | |
| 84 .Times(1); | |
| 85 | |
| 86 adapter_ = NULL; | |
| 87 DBusThreadManager::Shutdown(); | |
| 88 } | |
| 89 | |
| 90 protected: | |
| 91 MockBluetoothManagerClient* mock_manager_client_; | |
| 92 MockBluetoothAdapterClient* mock_adapter_client_; | |
| 93 MockBluetoothDeviceClient* mock_device_client_; | |
| 94 | |
| 95 static const dbus::ObjectPath adapter_path_; | |
| 96 static const std::string adapter_address_; | |
| 97 scoped_refptr<BluetoothAdapter> adapter_; | |
| 98 | |
| 99 MockBluetoothAdapter::Observer adapter_observer_; | |
| 100 }; | |
| 101 | |
| 102 const dbus::ObjectPath BluetoothAdapterDevicesTest::adapter_path_( | |
| 103 "/fake/hci0"); | |
| 104 const std::string BluetoothAdapterDevicesTest::adapter_address_ = | |
| 105 "CA:FE:4A:C0:FE:FE"; | |
| 106 | |
| 107 TEST_F(BluetoothAdapterDevicesTest, DeviceRemovedAfterFound) { | |
| 108 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_01"); | |
| 109 const std::string device_address = "BA:C0:11:00:00:01"; | |
| 110 | |
| 111 MockBluetoothDeviceClient::Properties device_properties; | |
| 112 device_properties.address.ReplaceValue(device_address); | |
| 113 device_properties.name.ReplaceValue("Fake Keyboard"); | |
| 114 device_properties.bluetooth_class.ReplaceValue(0x2540); | |
| 115 | |
| 116 // needs to be a supported class | |
| 117 | |
| 118 // should create a bluetooth device, | |
| 119 // put it in the hash map, | |
| 120 // set visible to true, | |
| 121 // update its properties, | |
| 122 | |
| 123 // Inform the adapter that the device has been found; | |
| 124 // BluetoothAdapterClient::Observer::DeviceAdded will be called, passing | |
| 125 // the device object. | |
| 126 BluetoothDevice* device; | |
| 127 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) | |
| 128 .Times(1) | |
| 129 .WillOnce(SaveArg<1>(&device)); | |
| 130 | |
| 131 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) | |
| 132 ->DeviceFound(adapter_path_, device_address, device_properties); | |
| 133 | |
| 134 // Now inform the adapter that the device has been added and assigned an | |
| 135 // object path; BluetoothDeviceClient::GetProperties will be called to | |
| 136 // obtain the property set; and | |
| 137 // BluetoothAdapterClient::Observer::DeviceChanged will be called passing | |
| 138 // the same device object as before. | |
| 139 EXPECT_CALL(*mock_device_client_, GetProperties(device_path)) | |
| 140 .WillRepeatedly(Return(&device_properties)); | |
| 141 | |
| 142 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device)) | |
| 143 .Times(1); | |
| 144 | |
| 145 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) | |
| 146 ->DeviceCreated(adapter_path_, device_path); | |
| 147 | |
| 148 // Finally remove the adapter again; since this is a supported device | |
| 149 // BluetoothAdapterClient::Observer::DeviceRemoved should be not called, | |
| 150 // instead BluetoothAdapterClient::Observer::DeviceChanged will be called. | |
| 151 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device)) | |
| 152 .Times(0); | |
| 153 EXPECT_CALL(adapter_observer_, DeviceChanged(adapter_.get(), device)) | |
| 154 .Times(1); | |
| 155 | |
| 156 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) | |
| 157 ->DeviceRemoved(adapter_path_, device_path); | |
| 158 | |
| 159 // Verify that the device is still visible, just no longer paired. | |
| 160 EXPECT_TRUE(device->IsVisible()); | |
| 161 EXPECT_FALSE(device->IsPaired()); | |
| 162 } | |
| 163 | |
| 164 TEST_F(BluetoothAdapterDevicesTest, UnsupportedDeviceRemovedAfterFound) { | |
| 165 const dbus::ObjectPath device_path("/fake/hci0/dev_ba_c0_11_00_00_02"); | |
| 166 const std::string device_address = "BA:C0:11:00:00:02"; | |
| 167 | |
| 168 MockBluetoothDeviceClient::Properties device_properties; | |
| 169 device_properties.address.ReplaceValue(device_address); | |
| 170 device_properties.name.ReplaceValue("Fake Computer"); | |
| 171 device_properties.bluetooth_class.ReplaceValue(0x400100); | |
| 172 | |
| 173 // Inform the adapter that the unsupported device has been found; | |
| 174 // BluetoothAdapterClient::Observer::DeviceAdded should not be called | |
| 175 // yet because this device is not supported so is hidden from the UI. | |
| 176 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) | |
| 177 .Times(0); | |
| 178 | |
| 179 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) | |
| 180 ->DeviceFound(adapter_path_, device_address, device_properties); | |
| 181 | |
| 182 // Now inform the adapter the device has been added and assigned an | |
| 183 // object path; BluetoothDeviceClient::GetProperties will be called | |
| 184 // to obtain the property set; and | |
| 185 // BluetoothAdapterClient::Observer::DeviceAdded will be called, | |
| 186 // passing the device object. | |
| 187 EXPECT_CALL(*mock_device_client_, GetProperties(device_path)) | |
| 188 .WillRepeatedly(Return(&device_properties)); | |
| 189 | |
| 190 BluetoothDevice* device; | |
| 191 EXPECT_CALL(adapter_observer_, DeviceAdded(adapter_.get(), _)) | |
| 192 .Times(1) | |
| 193 .WillOnce(SaveArg<1>(&device)); | |
| 194 | |
| 195 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) | |
| 196 ->DeviceCreated(adapter_path_, device_path); | |
| 197 | |
| 198 // Finally remove the device again; | |
| 199 // BluetoothAdapterClient::Observer::DeviceRemoved will be called | |
| 200 // before the device object is deleted. | |
| 201 EXPECT_CALL(adapter_observer_, DeviceRemoved(adapter_.get(), device)) | |
| 202 .Times(1); | |
| 203 | |
| 204 static_cast<BluetoothAdapterClient::Observer*>(adapter_.get()) | |
| 205 ->DeviceRemoved(adapter_path_, device_path); | |
| 206 } | |
| 207 | |
| 208 } // namespace chromeos | |
| OLD | NEW |