| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 2472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2483 ASSERT_NE(-1, idx); | 2483 ASSERT_NE(-1, idx); |
| 2484 | 2484 |
| 2485 bluez::FakeBluetoothDeviceClient::Properties* properties = | 2485 bluez::FakeBluetoothDeviceClient::Properties* properties = |
| 2486 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | 2486 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( |
| 2487 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); | 2487 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); |
| 2488 | 2488 |
| 2489 // During discovery, rssi is a valid value (-75) | 2489 // During discovery, rssi is a valid value (-75) |
| 2490 properties->rssi.ReplaceValue(-75); | 2490 properties->rssi.ReplaceValue(-75); |
| 2491 properties->rssi.set_valid(true); | 2491 properties->rssi.set_valid(true); |
| 2492 | 2492 |
| 2493 ASSERT_EQ(-75, devices[idx]->GetInquiryRSSI()); | 2493 ASSERT_EQ(-75, devices[idx]->GetInquiryRSSI().value()); |
| 2494 | 2494 |
| 2495 // Install an observer; expect the DeviceChanged method to be called when | 2495 // Install an observer; expect the DeviceChanged method to be called when |
| 2496 // we invalidate the RSSI of the device. | 2496 // we invalidate the RSSI of the device. |
| 2497 TestBluetoothAdapterObserver observer(adapter_); | 2497 TestBluetoothAdapterObserver observer(adapter_); |
| 2498 | 2498 |
| 2499 // When discovery is over, the value should be invalidated. | 2499 // When discovery is over, the value should be invalidated. |
| 2500 properties->rssi.set_valid(false); | 2500 properties->rssi.set_valid(false); |
| 2501 properties->NotifyPropertyChanged(properties->rssi.name()); | 2501 properties->NotifyPropertyChanged(properties->rssi.name()); |
| 2502 | 2502 |
| 2503 EXPECT_EQ(1, observer.device_changed_count()); | 2503 EXPECT_EQ(1, observer.device_changed_count()); |
| 2504 EXPECT_EQ(devices[idx], observer.last_device()); | 2504 EXPECT_EQ(devices[idx], observer.last_device()); |
| 2505 | 2505 |
| 2506 int unknown_power = BluetoothDevice::kUnknownPower; | 2506 EXPECT_FALSE(devices[idx]->GetInquiryRSSI()); |
| 2507 EXPECT_EQ(unknown_power, devices[idx]->GetInquiryRSSI()); | 2507 } |
| 2508 |
| 2509 TEST_F(BluetoothBlueZTest, DeviceInquiryPowerOutOfBounds) { |
| 2510 // RSSI and Tx Power should be a int8 but Bluez returns a int16 because DBus |
| 2511 // doesn't support int8 even though Bluez uses a int8 internally. This test |
| 2512 // makes sure we handle values outside of the int8 range. |
| 2513 GetAdapter(); |
| 2514 |
| 2515 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 2516 ASSERT_EQ(2U, devices.size()); |
| 2517 |
| 2518 int idx = GetDeviceIndexByAddress( |
| 2519 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 2520 ASSERT_NE(-1, idx); |
| 2521 |
| 2522 bluez::FakeBluetoothDeviceClient::Properties* properties = |
| 2523 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( |
| 2524 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); |
| 2525 |
| 2526 properties->rssi.set_valid(true); |
| 2527 properties->tx_power.set_valid(true); |
| 2528 |
| 2529 // Small invalid value |
| 2530 properties->rssi.ReplaceValue(-129); |
| 2531 EXPECT_FALSE(devices[idx]->GetInquiryRSSI()); |
| 2532 |
| 2533 properties->tx_power.ReplaceValue(-129); |
| 2534 EXPECT_FALSE(devices[idx]->GetInquiryTxPower()); |
| 2535 |
| 2536 // Small valid value |
| 2537 properties->rssi.ReplaceValue(-128); |
| 2538 EXPECT_EQ(-128, devices[idx]->GetInquiryRSSI().value()); |
| 2539 |
| 2540 properties->tx_power.ReplaceValue(-128); |
| 2541 EXPECT_EQ(-128, devices[idx]->GetInquiryTxPower().value()); |
| 2542 |
| 2543 // Large valid value. |
| 2544 properties->rssi.ReplaceValue(127); |
| 2545 EXPECT_EQ(127, devices[idx]->GetInquiryRSSI().value()); |
| 2546 |
| 2547 properties->tx_power.ReplaceValue(127); |
| 2548 EXPECT_EQ(127, devices[idx]->GetInquiryTxPower().value()); |
| 2549 |
| 2550 // Large invalid value |
| 2551 properties->rssi.ReplaceValue(128); |
| 2552 EXPECT_FALSE(devices[idx]->GetInquiryRSSI()); |
| 2553 |
| 2554 properties->tx_power.ReplaceValue(128); |
| 2555 EXPECT_FALSE(devices[idx]->GetInquiryTxPower()); |
| 2508 } | 2556 } |
| 2509 | 2557 |
| 2510 TEST_F(BluetoothBlueZTest, DeviceInquiryTxPowerInvalidated) { | 2558 TEST_F(BluetoothBlueZTest, DeviceInquiryTxPowerInvalidated) { |
| 2511 // Simulate invalidation of inquiry TxPower of a device, as it occurs | 2559 // Simulate invalidation of inquiry TxPower of a device, as it occurs |
| 2512 // when discovery is finished. | 2560 // when discovery is finished. |
| 2513 GetAdapter(); | 2561 GetAdapter(); |
| 2514 | 2562 |
| 2515 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | 2563 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 2516 ASSERT_EQ(2U, devices.size()); | 2564 ASSERT_EQ(2U, devices.size()); |
| 2517 | 2565 |
| 2518 int idx = GetDeviceIndexByAddress( | 2566 int idx = GetDeviceIndexByAddress( |
| 2519 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | 2567 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 2520 ASSERT_NE(-1, idx); | 2568 ASSERT_NE(-1, idx); |
| 2521 | 2569 |
| 2522 bluez::FakeBluetoothDeviceClient::Properties* properties = | 2570 bluez::FakeBluetoothDeviceClient::Properties* properties = |
| 2523 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | 2571 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( |
| 2524 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); | 2572 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); |
| 2525 | 2573 |
| 2526 // During discovery, tx_power is a valid value (0) | 2574 // During discovery, tx_power is a valid value (0) |
| 2527 properties->tx_power.ReplaceValue(0); | 2575 properties->tx_power.ReplaceValue(0); |
| 2528 properties->tx_power.set_valid(true); | 2576 properties->tx_power.set_valid(true); |
| 2529 | 2577 |
| 2530 ASSERT_EQ(0, devices[idx]->GetInquiryTxPower()); | 2578 ASSERT_EQ(0, devices[idx]->GetInquiryTxPower().value()); |
| 2531 | 2579 |
| 2532 // Install an observer; expect the DeviceChanged method to be called when | 2580 // Install an observer; expect the DeviceChanged method to be called when |
| 2533 // we invalidate the tx_power of the device. | 2581 // we invalidate the tx_power of the device. |
| 2534 TestBluetoothAdapterObserver observer(adapter_); | 2582 TestBluetoothAdapterObserver observer(adapter_); |
| 2535 | 2583 |
| 2536 // When discovery is over, the value should be invalidated. | 2584 // When discovery is over, the value should be invalidated. |
| 2537 properties->tx_power.set_valid(false); | 2585 properties->tx_power.set_valid(false); |
| 2538 properties->NotifyPropertyChanged(properties->tx_power.name()); | 2586 properties->NotifyPropertyChanged(properties->tx_power.name()); |
| 2539 | 2587 |
| 2540 EXPECT_EQ(1, observer.device_changed_count()); | 2588 EXPECT_EQ(1, observer.device_changed_count()); |
| 2541 EXPECT_EQ(devices[idx], observer.last_device()); | 2589 EXPECT_EQ(devices[idx], observer.last_device()); |
| 2542 | 2590 |
| 2543 int unknown_power = BluetoothDevice::kUnknownPower; | 2591 EXPECT_FALSE(devices[idx]->GetInquiryTxPower()); |
| 2544 EXPECT_EQ(unknown_power, devices[idx]->GetInquiryTxPower()); | |
| 2545 } | 2592 } |
| 2546 | 2593 |
| 2547 TEST_F(BluetoothBlueZTest, ForgetDevice) { | 2594 TEST_F(BluetoothBlueZTest, ForgetDevice) { |
| 2548 GetAdapter(); | 2595 GetAdapter(); |
| 2549 | 2596 |
| 2550 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | 2597 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 2551 ASSERT_EQ(2U, devices.size()); | 2598 ASSERT_EQ(2U, devices.size()); |
| 2552 | 2599 |
| 2553 int idx = GetDeviceIndexByAddress( | 2600 int idx = GetDeviceIndexByAddress( |
| 2554 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | 2601 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| (...skipping 1570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4125 EXPECT_EQ(0, device->GetProductID()); | 4172 EXPECT_EQ(0, device->GetProductID()); |
| 4126 EXPECT_EQ(0, device->GetDeviceID()); | 4173 EXPECT_EQ(0, device->GetDeviceID()); |
| 4127 } | 4174 } |
| 4128 | 4175 |
| 4129 TEST_F(BluetoothBlueZTest, GetConnectionInfoForDisconnectedDevice) { | 4176 TEST_F(BluetoothBlueZTest, GetConnectionInfoForDisconnectedDevice) { |
| 4130 GetAdapter(); | 4177 GetAdapter(); |
| 4131 BluetoothDevice* device = adapter_->GetDevice( | 4178 BluetoothDevice* device = adapter_->GetDevice( |
| 4132 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | 4179 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 4133 | 4180 |
| 4134 // Calling GetConnectionInfo for an unconnected device should return a result | 4181 // Calling GetConnectionInfo for an unconnected device should return a result |
| 4135 // in which all fields are filled with BluetoothDevice::kUnknownPower. | 4182 // in which all fields are filled with BluetoothDevice::kUnknownRSSI and |
| 4183 // BluetoothDevice::kUnknownTxPower. |
| 4136 BluetoothDevice::ConnectionInfo conn_info(0, 0, 0); | 4184 BluetoothDevice::ConnectionInfo conn_info(0, 0, 0); |
| 4137 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info)); | 4185 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info)); |
| 4138 int unknown_power = BluetoothDevice::kUnknownPower; | 4186 |
| 4139 EXPECT_NE(0, unknown_power); | 4187 EXPECT_FALSE(conn_info.rssi); |
| 4140 EXPECT_EQ(unknown_power, conn_info.rssi); | 4188 EXPECT_FALSE(conn_info.transmit_power); |
| 4141 EXPECT_EQ(unknown_power, conn_info.transmit_power); | 4189 EXPECT_FALSE(conn_info.max_transmit_power); |
| 4142 EXPECT_EQ(unknown_power, conn_info.max_transmit_power); | |
| 4143 } | 4190 } |
| 4144 | 4191 |
| 4145 TEST_F(BluetoothBlueZTest, GetConnectionInfoForConnectedDevice) { | 4192 TEST_F(BluetoothBlueZTest, GetConnectionInfoForConnectedDevice) { |
| 4146 GetAdapter(); | 4193 GetAdapter(); |
| 4147 BluetoothDevice* device = adapter_->GetDevice( | 4194 BluetoothDevice* device = adapter_->GetDevice( |
| 4148 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | 4195 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 4149 | 4196 |
| 4150 device->Connect(nullptr, GetCallback(), | 4197 device->Connect(nullptr, GetCallback(), |
| 4151 base::Bind(&BluetoothBlueZTest::ConnectErrorCallback, | 4198 base::Bind(&BluetoothBlueZTest::ConnectErrorCallback, |
| 4152 base::Unretained(this))); | 4199 base::Unretained(this))); |
| 4153 EXPECT_TRUE(device->IsConnected()); | 4200 EXPECT_TRUE(device->IsConnected()); |
| 4154 | 4201 |
| 4155 // Calling GetConnectionInfo for a connected device should return valid | 4202 // Calling GetConnectionInfo for a connected device should return valid |
| 4156 // results. | 4203 // results. |
| 4157 fake_bluetooth_device_client_->UpdateConnectionInfo(-10, 3, 4); | 4204 fake_bluetooth_device_client_->UpdateConnectionInfo(-10, 3, 4); |
| 4158 BluetoothDevice::ConnectionInfo conn_info; | 4205 BluetoothDevice::ConnectionInfo conn_info; |
| 4159 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info)); | 4206 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info)); |
| 4160 EXPECT_EQ(-10, conn_info.rssi); | 4207 EXPECT_EQ(-10, conn_info.rssi.value()); |
| 4161 EXPECT_EQ(3, conn_info.transmit_power); | 4208 EXPECT_EQ(3, conn_info.transmit_power.value()); |
| 4162 EXPECT_EQ(4, conn_info.max_transmit_power); | 4209 EXPECT_EQ(4, conn_info.max_transmit_power.value()); |
| 4163 } | 4210 } |
| 4164 | 4211 |
| 4165 // Verifies Shutdown shuts down the adapter as expected. | 4212 // Verifies Shutdown shuts down the adapter as expected. |
| 4166 TEST_F(BluetoothBlueZTest, Shutdown) { | 4213 TEST_F(BluetoothBlueZTest, Shutdown) { |
| 4167 // Set up adapter. Set powered & discoverable, start discovery. | 4214 // Set up adapter. Set powered & discoverable, start discovery. |
| 4168 GetAdapter(); | 4215 GetAdapter(); |
| 4169 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | 4216 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); |
| 4170 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback()); | 4217 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback()); |
| 4171 adapter_->StartDiscoverySession( | 4218 adapter_->StartDiscoverySession( |
| 4172 base::Bind(&BluetoothBlueZTest::DiscoverySessionCallback, | 4219 base::Bind(&BluetoothBlueZTest::DiscoverySessionCallback, |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4481 adapter_->Shutdown(); | 4528 adapter_->Shutdown(); |
| 4482 adapter_bluez->OnStopDiscoveryError(GetDiscoveryErrorCallback(), "", ""); | 4529 adapter_bluez->OnStopDiscoveryError(GetDiscoveryErrorCallback(), "", ""); |
| 4483 | 4530 |
| 4484 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError, | 4531 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError, |
| 4485 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession. | 4532 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession. |
| 4486 EXPECT_EQ(0, callback_count_); | 4533 EXPECT_EQ(0, callback_count_); |
| 4487 EXPECT_EQ(1 + kNumberOfDiscoverySessions, error_callback_count_); | 4534 EXPECT_EQ(1 + kNumberOfDiscoverySessions, error_callback_count_); |
| 4488 } | 4535 } |
| 4489 | 4536 |
| 4490 } // namespace bluez | 4537 } // namespace bluez |
| OLD | NEW |