Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: device/bluetooth/device_unittest.cc

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Change tests, ConnectErrorCode -> ConnectResult Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "device/bluetooth/device.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10
11 #include "base/logging.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/run_loop.h"
15 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
16 #include "device/bluetooth/test/mock_bluetooth_device.h"
17 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 using ::testing::Return;
21
22 namespace bluetooth {
23
24 typedef testing::NiceMock<device::MockBluetoothAdapter>
25 NiceMockBluetoothAdapter;
26 typedef testing::NiceMock<device::MockBluetoothDevice> NiceMockBluetoothDevice;
27 typedef testing::NiceMock<device::MockBluetoothGattService>
28 NiceMockBluetoothGattService;
29 typedef testing::NiceMock<device::MockBluetoothGattConnection>
30 NiceMockBluetoothGattConnection;
31
32 namespace {
33 const char kTestLeDeviceAddress0[] = "11:22:33:44:55:66";
34 const char kTestLeDeviceName0[] = "Test LE Device 0";
35
36 const char kTestServiceId0[] = "service_id0";
37 const char kTestServiceUuid0[] = "1234";
38
39 const char kTestServiceId1[] = "service_id1";
40 const char kTestServiceUuid1[] = "5678";
41
42 class DeviceTest : public testing::Test {
43 public:
44 enum class Call { EXPECTED, NOT_EXPECTED };
45
46 DeviceTest()
47 : adapter_(new NiceMockBluetoothAdapter),
48 message_loop_(new base::MessageLoop),
49 weak_factory_(this) {
50 device_.reset(
51 new NiceMockBluetoothDevice(adapter_.get(), 0, kTestLeDeviceName0,
52 kTestLeDeviceAddress0, false, true));
53
54 ON_CALL(*adapter_, GetDevice(kTestLeDeviceAddress0))
55 .WillByDefault(Return(device_.get()));
56
57 std::unique_ptr<NiceMockBluetoothGattService> service1(
58 new NiceMockBluetoothGattService(
59 device_.get(), kTestServiceId0,
60 device::BluetoothUUID(kTestServiceUuid0), true /* is_primary */,
61 false /* is_local */));
62 std::unique_ptr<NiceMockBluetoothGattService> service2(
63 new NiceMockBluetoothGattService(
64 device_.get(), kTestServiceId1,
65 device::BluetoothUUID(kTestServiceUuid1), true /* is_primary */,
66 false /* is_local */));
67
68 device_->AddMockService(std::move(service1));
69 device_->AddMockService(std::move(service2));
70
71 EXPECT_CALL(*device_, GetGattServices())
72 .WillRepeatedly(Invoke(device_.get(),
73 &device::MockBluetoothDevice::GetMockServices));
74
75 std::unique_ptr<NiceMockBluetoothGattConnection> connection(
76 new NiceMockBluetoothGattConnection(adapter_, device_->GetAddress()));
77
78 mojom::DevicePtr proxy;
79
80 // Owns itself.
81 device_service_.reset(
82 new Device(adapter_, std::move(connection), mojo::GetProxy(&proxy)));
83 }
84
85 void TearDown() override {
86 EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_);
87 }
88
89 protected:
90 void GetServicesCallback(Call expected,
91 size_t expected_length,
92 const base::Closure& continuation,
93 std::vector<mojom::ServiceInfoPtr> services) {
94 if (expected == Call::EXPECTED)
95 ++actual_success_callback_calls_;
96
97 EXPECT_EQ(expected_length, services.size());
98 continuation.Run();
99 }
100
101 Device::GetServicesCallback GetGetServicesCallback(
102 Call expected,
103 const base::Closure& continuation) {
104 if (expected == Call::EXPECTED)
105 ++expected_success_callback_calls_;
106
107 return base::Bind(&DeviceTest::GetServicesCallback,
108 weak_factory_.GetWeakPtr(), expected, 2, continuation);
109 }
110
111 scoped_refptr<NiceMockBluetoothAdapter> adapter_;
112 std::unique_ptr<NiceMockBluetoothDevice> device_;
113 std::unique_ptr<Device> device_service_;
114 std::unique_ptr<base::MessageLoop> message_loop_;
115
116 int expected_success_callback_calls_ = 0;
117 int actual_success_callback_calls_ = 0;
118
119 base::WeakPtrFactory<DeviceTest> weak_factory_;
120 };
121 } // namespace
122
123 TEST_F(DeviceTest, GetServices) {
124 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete())
125 .WillRepeatedly(Return(true));
126
127 base::RunLoop loop;
128 device_service_->set_connection_error_handler(loop.QuitClosure());
129 device_service_->GetServices(
130 GetGetServicesCallback(Call::EXPECTED, loop.QuitClosure()));
131 loop.Run();
132 }
133
134 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
135 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete())
136 .WillOnce(Return(false))
137 .WillOnce(Return(false))
138 .WillRepeatedly(Return(true));
139
140 base::RunLoop loop;
141
142 device_service_->set_connection_error_handler(loop.QuitClosure());
143
144 // Add more than one request to queue.
145 device_service_->GetServices(
146 GetGetServicesCallback(Call::EXPECTED, loop.QuitClosure()));
147 device_service_->GetServices(
148 GetGetServicesCallback(Call::EXPECTED, loop.QuitClosure()));
149
150 // Completes 2 queued GetServices tasks.
151 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
152
153 // No more GetServices calls will complete.
154 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get());
155
156 // Runs immediately.
157 device_service_->GetServices(
158 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
159 device_service_->GetServices(
160 GetGetServicesCallback(Call::EXPECTED, loop.QuitClosure()));
161
162 // No more GetServices calls will complete.
163 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get());
164
165 loop.Run();
166 }
167
168 } // namespace bluetooth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698