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

Side by Side Diff: device/bluetooth/bluez/bluetooth_service_record_bluez_unittest.cc

Issue 2084463002: BlueZ + DBus implementations of create/remove service record functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes + moar tests Created 4 years, 6 months 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/bluez/bluetooth_service_record_bluez.h"
6
7 #include <memory>
8 #include <string>
9
10 #include "base/bind_helpers.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h"
17 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace bluez {
21
22 namespace {
23
24 constexpr uint16_t kServiceUuidAttributeId = 0x0003;
25
26 constexpr char kServiceUuid1[] = "00001801-0000-1000-8000-00805f9b34fb";
27 constexpr char kServiceUuid2[] = "00001801-0000-1000-7000-00805f9b34fb";
28 constexpr char kServiceUuid3[] = "00001801-0000-1000-3000-00805f9b34fb";
29 }
xiyuan 2016/06/22 15:53:06 nit: insert one empty line before and add comment
rkc 2016/06/22 21:06:37 Done.
30
31 class BluetoothServiceRecordBlueZTest : public testing::Test {
32 public:
33 BluetoothServiceRecordBlueZTest()
34 : adapter_bluez_(nullptr),
35 success_callbacks_(0),
36 error_callbacks_(0),
37 run_loop_(nullptr),
38 last_seen_handle_(0) {}
39
40 void SetUp() override {
41 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter =
42 bluez::BluezDBusManager::GetSetterForTesting();
43
44 device::BluetoothAdapterFactory::GetAdapter(
45 base::Bind(&BluetoothServiceRecordBlueZTest::AdapterCallback,
46 base::Unretained(this)));
47 RunRunLoop();
48 }
49
50 void TearDown() override {
51 device::BluetoothAdapterFactory::Shutdown();
52 bluez::BluezDBusManager::Shutdown();
53 }
54
55 uint32_t CreateServiceRecordWithCallbacks(
56 const BluetoothServiceRecordBlueZ& record) {
57 size_t old_success_callbacks = success_callbacks_;
58 size_t old_error_callbacks = error_callbacks_;
xiyuan 2016/06/22 15:53:05 nit: const for both old counts
rkc 2016/06/22 21:06:37 Done.
59 // Note: Our fake implementation will never give us a handle of 0, so we
60 // can know that we will never have a record with the handle 0. Hence using
61 // 0 as an invalid handle return value is okay.
62 last_seen_handle_ = 0;
63 adapter_bluez_->CreateServiceRecord(
64 record,
65 base::Bind(
66 &BluetoothServiceRecordBlueZTest::CreateServiceSuccessCallback,
67 base::Unretained(this)),
68 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback,
69 base::Unretained(this)));
70 EXPECT_EQ(old_success_callbacks + 1, success_callbacks_);
71 EXPECT_EQ(old_error_callbacks, error_callbacks_);
72 EXPECT_NE(0u, last_seen_handle_);
73 return last_seen_handle_;
74 }
75
76 void RemoveServiceRecordWithCallbacks(uint32_t handle, bool expect_success) {
77 size_t old_success_callbacks = success_callbacks_;
78 size_t old_error_callbacks = error_callbacks_;
xiyuan 2016/06/22 15:53:05 nit: const for both old counts
rkc 2016/06/22 21:06:37 Done.
79 adapter_bluez_->RemoveServiceRecord(
80 handle,
81 base::Bind(
82 &BluetoothServiceRecordBlueZTest::RemoveServiceSuccessCallback,
83 base::Unretained(this)),
84 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback,
85 base::Unretained(this)));
86 size_t success = expect_success ? 1 : 0;
87 EXPECT_EQ(old_success_callbacks + success, success_callbacks_);
88 EXPECT_EQ(old_error_callbacks + 1 - success, error_callbacks_);
89 }
90
91 protected:
92 BluetoothServiceRecordBlueZ CreateaServiceRecord(const std::string uuid) {
93 std::map<uint16_t, BluetoothServiceAttributeValueBlueZ> attributes;
94 attributes.emplace(kServiceUuidAttributeId,
95 BluetoothServiceAttributeValueBlueZ(
96 BluetoothServiceAttributeValueBlueZ::UUID, 16,
97 new base::StringValue(uuid)));
98 return BluetoothServiceRecordBlueZ(attributes);
99 }
100
101 scoped_refptr<device::BluetoothAdapter> adapter_;
102 BluetoothAdapterBlueZ* adapter_bluez_;
103 size_t success_callbacks_;
104 size_t error_callbacks_;
105
106 private:
107 void RunRunLoop() {
108 run_loop_ = base::WrapUnique(new base::RunLoop());
109 run_loop_->Run();
110 }
111
112 void QuitRunLoop() {
113 if (run_loop_)
114 run_loop_->Quit();
115 }
116
117 void AdapterCallback(scoped_refptr<device::BluetoothAdapter> adapter) {
118 adapter_ = adapter;
119 adapter_bluez_ = static_cast<BluetoothAdapterBlueZ*>(adapter_.get());
120 QuitRunLoop();
121 }
122
123 void CreateServiceSuccessCallback(uint32_t handle) {
124 last_seen_handle_ = handle;
125 success_callbacks_++;
xiyuan 2016/06/22 15:53:05 nit: ++success_callbacks_;
rkc 2016/06/22 21:06:37 Done.
126 }
127
128 void RemoveServiceSuccessCallback() { success_callbacks_++; }
xiyuan 2016/06/22 15:53:06 nit: ++success_callbacks_;
rkc 2016/06/22 21:06:37 Done.
129
130 void ErrorCallback(BluetoothServiceRecordBlueZ::ErrorCode code) {
131 error_callbacks_++;
xiyuan 2016/06/22 15:53:05 nit: ++error_callbacks_;
rkc 2016/06/22 21:06:37 Done.
132 }
133
134 base::MessageLoop message_loop_;
135 std::unique_ptr<base::RunLoop> run_loop_;
136 uint32_t last_seen_handle_;
137
138 DISALLOW_COPY_AND_ASSIGN(BluetoothServiceRecordBlueZTest);
139 };
140
141 TEST_F(BluetoothServiceRecordBlueZTest, CreateAndRemove) {
142 uint32_t handle1 =
143 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1));
144 uint32_t handle2 =
145 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid2));
146 uint32_t handle3 =
147 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1));
148 uint32_t handle4 =
149 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid3));
150
151 RemoveServiceRecordWithCallbacks(handle1, true);
152 RemoveServiceRecordWithCallbacks(handle3, true);
153 RemoveServiceRecordWithCallbacks(handle1, false);
154 RemoveServiceRecordWithCallbacks(handle4, true);
155 RemoveServiceRecordWithCallbacks(handle2, true);
156 }
157
158 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698