Chromium Code Reviews| Index: device/bluetooth/device_unittest.cc |
| diff --git a/device/bluetooth/device_unittest.cc b/device/bluetooth/device_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8f52bc2e9cb0846a595d64037b2d72642ba74c0c |
| --- /dev/null |
| +++ b/device/bluetooth/device_unittest.cc |
| @@ -0,0 +1,168 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/bluetooth/device.h" |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/run_loop.h" |
| +#include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| +#include "device/bluetooth/test/mock_bluetooth_device.h" |
| +#include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using ::testing::Return; |
| + |
| +namespace bluetooth { |
| + |
| +typedef testing::NiceMock<device::MockBluetoothAdapter> |
| + NiceMockBluetoothAdapter; |
| +typedef testing::NiceMock<device::MockBluetoothDevice> NiceMockBluetoothDevice; |
| +typedef testing::NiceMock<device::MockBluetoothGattService> |
| + NiceMockBluetoothGattService; |
| +typedef testing::NiceMock<device::MockBluetoothGattConnection> |
| + NiceMockBluetoothGattConnection; |
| + |
| +namespace { |
| +const char kTestLeDeviceAddress0[] = "11:22:33:44:55:66"; |
| +const char kTestLeDeviceName0[] = "Test LE Device 0"; |
| + |
| +const char kTestServiceId0[] = "service_id0"; |
| +const char kTestServiceUuid0[] = "1234"; |
| + |
| +const char kTestServiceId1[] = "service_id1"; |
| +const char kTestServiceUuid1[] = "5678"; |
| + |
| +class DeviceTest : public testing::Test { |
| + public: |
| + enum class Call { EXPECTED, NOT_EXPECTED }; |
| + |
| + DeviceTest() |
| + : adapter_(new NiceMockBluetoothAdapter), |
| + message_loop_(new base::MessageLoop), |
| + weak_factory_(this) { |
| + device_.reset( |
| + new NiceMockBluetoothDevice(adapter_.get(), 0, kTestLeDeviceName0, |
| + kTestLeDeviceAddress0, false, true)); |
| + |
| + ON_CALL(*adapter_, GetDevice(kTestLeDeviceAddress0)) |
| + .WillByDefault(Return(device_.get())); |
| + |
| + std::unique_ptr<NiceMockBluetoothGattService> service1( |
| + new NiceMockBluetoothGattService( |
| + device_.get(), kTestServiceId0, |
| + device::BluetoothUUID(kTestServiceUuid0), true /* is_primary */, |
| + false /* is_local */)); |
| + std::unique_ptr<NiceMockBluetoothGattService> service2( |
| + new NiceMockBluetoothGattService( |
| + device_.get(), kTestServiceId1, |
| + device::BluetoothUUID(kTestServiceUuid1), true /* is_primary */, |
| + false /* is_local */)); |
| + |
| + device_->AddMockService(std::move(service1)); |
| + device_->AddMockService(std::move(service2)); |
| + |
| + EXPECT_CALL(*device_, GetGattServices()) |
| + .WillRepeatedly(Invoke(device_.get(), |
| + &device::MockBluetoothDevice::GetMockServices)); |
| + |
| + std::unique_ptr<NiceMockBluetoothGattConnection> connection( |
| + new NiceMockBluetoothGattConnection(adapter_, device_->GetAddress())); |
| + |
| + mojom::DevicePtr proxy; |
| + |
| + // Owns itself. |
| + device_service_.reset( |
| + new Device(adapter_, std::move(connection), mojo::GetProxy(&proxy))); |
| + } |
| + |
| + void TearDown() override { |
| + EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_); |
| + } |
| + |
| + protected: |
| + void GetServicesCallback(Call expected, |
| + size_t expected_length, |
| + const base::Closure& continuation, |
| + std::vector<mojom::ServiceInfoPtr> services) { |
| + if (expected == Call::EXPECTED) |
| + ++actual_success_callback_calls_; |
| + |
| + EXPECT_EQ(expected_length, services.size()); |
| + continuation.Run(); |
| + } |
| + |
| + Device::GetServicesCallback GetGetServicesCallback( |
| + Call expected, |
| + const base::Closure& continuation) { |
| + if (expected == Call::EXPECTED) |
| + ++expected_success_callback_calls_; |
| + |
| + return base::Bind(&DeviceTest::GetServicesCallback, |
| + weak_factory_.GetWeakPtr(), expected, 2, continuation); |
| + } |
| + |
| + scoped_refptr<NiceMockBluetoothAdapter> adapter_; |
| + std::unique_ptr<NiceMockBluetoothDevice> device_; |
| + std::unique_ptr<Device> device_service_; |
| + std::unique_ptr<base::MessageLoop> message_loop_; |
| + |
| + int expected_success_callback_calls_ = 0; |
| + int actual_success_callback_calls_ = 0; |
| + |
| + base::WeakPtrFactory<DeviceTest> weak_factory_; |
| +}; |
| +} // namespace |
| + |
| +TEST_F(DeviceTest, GetServices) { |
| + EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) |
| + .WillRepeatedly(Return(true)); |
| + |
| + base::RunLoop loop; |
| + device_service_->set_connection_error_handler(loop.QuitClosure()); |
| + device_service_->GetServices( |
| + GetGetServicesCallback(Call::EXPECTED, loop.QuitClosure())); |
| + loop.Run(); |
| +} |
| + |
| +TEST_F(DeviceTest, GetServicesNotDiscovered) { |
|
ortuno
2016/11/01 06:27:39
Can you add a test for when there are pending call
mbrunson
2016/11/02 01:25:46
I added a few more tests for the various disconnec
|
| + EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) |
| + .WillOnce(Return(false)) |
| + .WillOnce(Return(false)) |
| + .WillRepeatedly(Return(true)); |
| + |
| + base::RunLoop loop; |
| + |
| + device_service_->set_connection_error_handler(loop.QuitClosure()); |
| + |
| + // Add more than one request to queue. |
| + device_service_->GetServices( |
| + GetGetServicesCallback(Call::EXPECTED, loop.QuitClosure())); |
| + device_service_->GetServices( |
| + GetGetServicesCallback(Call::EXPECTED, loop.QuitClosure())); |
| + |
| + // Completes 2 queued GetServices tasks. |
| + device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); |
|
scheib
2016/10/31 22:46:54
high level comment: it's a bit hard to see which c
mbrunson
2016/11/02 01:25:46
I think more descriptive comments will be enough h
|
| + |
| + // No more GetServices calls will complete. |
| + device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); |
| + |
| + // Runs immediately. |
| + device_service_->GetServices( |
| + GetGetServicesCallback(Call::EXPECTED, loop.QuitClosure())); |
|
ortuno
2016/11/01 06:27:39
What do you think about adding the ability to spec
mbrunson
2016/11/02 01:25:46
Seems helpful. That way we know for sure the count
|
| + device_service_->GetServices( |
| + GetGetServicesCallback(Call::EXPECTED, loop.QuitClosure())); |
| + |
| + // No more GetServices calls will complete. |
| + device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); |
| + |
| + loop.Run(); |
| +} |
| + |
| +} // namespace bluetooth |