| 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..a00f0ef0d02f81b53ec30f87afbb734a54325244
|
| --- /dev/null
|
| +++ b/device/bluetooth/device_unittest.cc
|
| @@ -0,0 +1,151 @@
|
| +// 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";
|
| +
|
| +void ExpectServices(size_t expected_length,
|
| + const base::Closure continuation,
|
| + std::vector<mojom::ServiceInfoPtr> services) {
|
| + EXPECT_EQ(expected_length, services.size());
|
| + continuation.Run();
|
| +}
|
| +
|
| +class DeviceTest : public testing::Test {
|
| + public:
|
| + DeviceTest()
|
| + : adapter_(new NiceMockBluetoothAdapter),
|
| + message_loop_(new base::MessageLoop) {
|
| + 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));
|
| +
|
| + 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)));
|
| + }
|
| +
|
| + protected:
|
| + void ExpectGetGattServicesCalls(int expected_calls) {
|
| + EXPECT_CALL(*device_, GetGattServices())
|
| + .Times(expected_calls)
|
| + .WillRepeatedly(Invoke(device_.get(),
|
| + &device::MockBluetoothDevice::GetMockServices));
|
| + }
|
| +
|
| + scoped_refptr<NiceMockBluetoothAdapter> adapter_;
|
| + std::unique_ptr<NiceMockBluetoothDevice> device_;
|
| + std::unique_ptr<Device> device_service_;
|
| + std::unique_ptr<base::MessageLoop> message_loop_;
|
| +};
|
| +} // namespace
|
| +
|
| +TEST_F(DeviceTest, GetServices) {
|
| + EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete())
|
| + .WillRepeatedly(Return(true));
|
| +
|
| + ExpectGetGattServicesCalls(1);
|
| +
|
| + base::RunLoop loop;
|
| +
|
| + device_service_->set_connection_error_handler(loop.QuitClosure());
|
| +
|
| + device_service_->GetServices(
|
| + base::Bind(&ExpectServices, 2, loop.QuitClosure()));
|
| + loop.Run();
|
| +}
|
| +
|
| +TEST_F(DeviceTest, GetServicesNotDiscovered) {
|
| + EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete())
|
| + .WillOnce(Return(false))
|
| + .WillOnce(Return(false))
|
| + .WillRepeatedly(Return(true));
|
| +
|
| + ExpectGetGattServicesCalls(4);
|
| +
|
| + base::RunLoop loop;
|
| +
|
| + device_service_->set_connection_error_handler(loop.QuitClosure());
|
| +
|
| + // Add more than one request to queue.
|
| + device_service_->GetServices(
|
| + base::Bind(&ExpectServices, 2, loop.QuitClosure()));
|
| + device_service_->GetServices(
|
| + base::Bind(&ExpectServices, 2, loop.QuitClosure()));
|
| +
|
| + // Run then flush the queue. Calls GetGattServices twice.
|
| + device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get());
|
| +
|
| + // Queue should be empty so no more calls to GetGattServices.
|
| + device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get());
|
| +
|
| + // These should run immediately.
|
| + device_service_->GetServices(
|
| + base::Bind(&ExpectServices, 2, loop.QuitClosure()));
|
| + device_service_->GetServices(
|
| + base::Bind(&ExpectServices, 2, loop.QuitClosure()));
|
| +
|
| + // Queue should be empty so no more calls to GetGattServices.
|
| + device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get());
|
| +
|
| + loop.Run();
|
| +}
|
| +
|
| +} // namespace bluetooth
|
|
|