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

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: . 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
30 } // namespace
31
32 class BluetoothServiceRecordBlueZTest : public testing::Test {
33 public:
34 BluetoothServiceRecordBlueZTest()
35 : adapter_bluez_(nullptr),
36 success_callbacks_(0),
37 error_callbacks_(0),
38 run_loop_(nullptr),
39 last_seen_handle_(0) {}
40
41 void SetUp() override {
42 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter =
43 bluez::BluezDBusManager::GetSetterForTesting();
44
45 device::BluetoothAdapterFactory::GetAdapter(
46 base::Bind(&BluetoothServiceRecordBlueZTest::AdapterCallback,
47 base::Unretained(this)));
48 RunRunLoop();
49 }
50
51 void TearDown() override {
52 device::BluetoothAdapterFactory::Shutdown();
53 bluez::BluezDBusManager::Shutdown();
54 }
55
56 uint32_t CreateServiceRecordWithCallbacks(
57 const BluetoothServiceRecordBlueZ& record) {
58 const size_t old_success_callbacks = success_callbacks_;
59 const size_t old_error_callbacks = error_callbacks_;
60 // Note: Our fake implementation will never give us a handle of 0, so we
61 // can know that we will never have a record with the handle 0. Hence using
62 // 0 as an invalid handle return value is okay.
63 last_seen_handle_ = 0;
64 adapter_bluez_->CreateServiceRecord(
65 record,
66 base::Bind(
67 &BluetoothServiceRecordBlueZTest::CreateServiceSuccessCallback,
68 base::Unretained(this)),
69 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback,
70 base::Unretained(this)));
71 EXPECT_EQ(old_success_callbacks + 1, success_callbacks_);
72 EXPECT_EQ(old_error_callbacks, error_callbacks_);
73 EXPECT_NE(0u, last_seen_handle_);
74 return last_seen_handle_;
75 }
76
77 void RemoveServiceRecordWithCallbacks(uint32_t handle, bool expect_success) {
78 const size_t old_success_callbacks = success_callbacks_;
79 const size_t old_error_callbacks = error_callbacks_;
80 adapter_bluez_->RemoveServiceRecord(
81 handle,
82 base::Bind(
83 &BluetoothServiceRecordBlueZTest::RemoveServiceSuccessCallback,
84 base::Unretained(this)),
85 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback,
86 base::Unretained(this)));
87 size_t success = expect_success ? 1 : 0;
88 EXPECT_EQ(old_success_callbacks + success, success_callbacks_);
89 EXPECT_EQ(old_error_callbacks + 1 - success, error_callbacks_);
90 }
91
92 protected:
93 BluetoothServiceRecordBlueZ CreateaServiceRecord(const std::string uuid) {
94 std::map<uint16_t, BluetoothServiceAttributeValueBlueZ> attributes;
95 attributes.insert(std::pair<uint16_t, BluetoothServiceAttributeValueBlueZ>(
96 kServiceUuidAttributeId,
97 BluetoothServiceAttributeValueBlueZ(
98 BluetoothServiceAttributeValueBlueZ::UUID, 16,
99 base::MakeUnique<base::StringValue>(uuid))));
100 return BluetoothServiceRecordBlueZ(attributes);
101 }
102
103 scoped_refptr<device::BluetoothAdapter> adapter_;
104 BluetoothAdapterBlueZ* adapter_bluez_;
105 size_t success_callbacks_;
106 size_t error_callbacks_;
107
108 private:
109 void RunRunLoop() {
110 run_loop_ = base::MakeUnique<base::RunLoop>();
111 run_loop_->Run();
112 }
113
114 void QuitRunLoop() {
115 if (run_loop_)
116 run_loop_->Quit();
117 }
118
119 void AdapterCallback(scoped_refptr<device::BluetoothAdapter> adapter) {
120 adapter_ = adapter;
121 adapter_bluez_ = static_cast<BluetoothAdapterBlueZ*>(adapter_.get());
122 QuitRunLoop();
123 }
124
125 void CreateServiceSuccessCallback(uint32_t handle) {
126 last_seen_handle_ = handle;
127 ++success_callbacks_;
128 }
129
130 void RemoveServiceSuccessCallback() { ++success_callbacks_; }
131
132 void ErrorCallback(BluetoothServiceRecordBlueZ::ErrorCode code) {
133 ++error_callbacks_;
134 }
135
136 base::MessageLoop message_loop_;
137 std::unique_ptr<base::RunLoop> run_loop_;
138 uint32_t last_seen_handle_;
139
140 DISALLOW_COPY_AND_ASSIGN(BluetoothServiceRecordBlueZTest);
141 };
142
143 TEST_F(BluetoothServiceRecordBlueZTest, CreateAndRemove) {
144 uint32_t handle1 =
145 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1));
146 uint32_t handle2 =
147 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid2));
148 uint32_t handle3 =
149 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1));
150 uint32_t handle4 =
151 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid3));
152
153 RemoveServiceRecordWithCallbacks(handle1, true);
154 RemoveServiceRecordWithCallbacks(handle3, true);
155 RemoveServiceRecordWithCallbacks(handle1, false);
156 RemoveServiceRecordWithCallbacks(handle4, true);
157 RemoveServiceRecordWithCallbacks(handle2, true);
158 }
159
160 } // namespace bluez
OLDNEW
« no previous file with comments | « device/bluetooth/bluez/bluetooth_service_record_bluez.cc ('k') | device/bluetooth/dbus/bluetooth_adapter_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698