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

Side by Side Diff: device/bluetooth/test/bluetooth_gatt_server_test.cc

Issue 1920693002: Complete //device/bt implementation for hosting local GATT attributes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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/test/bluetooth_gatt_server_test.h"
6
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
12 #include "device/bluetooth/bluetooth_uuid.h"
13 #include "device/bluetooth/test/bluetooth_test.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace device {
17
18 // TestLocalGattServiceDelegate:
19
20 bool TestLocalGattServiceDelegate::should_fail_ = false;
21 uint64_t TestLocalGattServiceDelegate::last_written_value_ = 0;
22 uint64_t TestLocalGattServiceDelegate::value_to_write_ = 0;
23
24 TestLocalGattServiceDelegate::TestLocalGattServiceDelegate()
25 : expected_service_(nullptr),
26 expected_characteristic_(nullptr),
27 expected_descriptor_(nullptr) {}
28
29 void TestLocalGattServiceDelegate::OnCharacteristicReadRequest(
30 const BluetoothLocalGattService* service,
31 const BluetoothLocalGattCharacteristic* characteristic,
32 int offset,
33 const ValueCallback& callback,
34 const ErrorCallback& error_callback) {
35 EXPECT_EQ(expected_service_, service);
36 EXPECT_EQ(expected_characteristic_, characteristic);
37 if (should_fail_) {
38 error_callback.Run();
39 return;
40 }
41 callback.Run(BluetoothGattServerTest::GetValue(value_to_write_));
42 }
43
44 void TestLocalGattServiceDelegate::OnCharacteristicWriteRequest(
45 const BluetoothLocalGattService* service,
46 const BluetoothLocalGattCharacteristic* characteristic,
47 const std::vector<uint8_t>& value,
48 int offset,
49 const base::Closure& callback,
50 const ErrorCallback& error_callback) {
51 EXPECT_EQ(expected_service_, service);
52 EXPECT_EQ(expected_characteristic_, characteristic);
53 if (should_fail_) {
54 error_callback.Run();
55 return;
56 }
57 last_written_value_ = BluetoothGattServerTest::GetInteger(value);
58 callback.Run();
59 }
60
61 void TestLocalGattServiceDelegate::OnDescriptorReadRequest(
62 const BluetoothLocalGattService* service,
63 const BluetoothLocalGattDescriptor* descriptor,
64 int offset,
65 const ValueCallback& callback,
66 const ErrorCallback& error_callback) {
67 EXPECT_EQ(expected_service_, service);
68 EXPECT_EQ(expected_descriptor_, descriptor);
69 if (should_fail_) {
70 error_callback.Run();
71 return;
72 }
73 callback.Run(BluetoothGattServerTest::GetValue(value_to_write_));
74 }
75
76 void TestLocalGattServiceDelegate::OnDescriptorWriteRequest(
77 const BluetoothLocalGattService* service,
78 const BluetoothLocalGattDescriptor* descriptor,
79 const std::vector<uint8_t>& value,
80 int offset,
81 const base::Closure& callback,
82 const ErrorCallback& error_callback) {
83 EXPECT_EQ(expected_service_, service);
84 EXPECT_EQ(expected_descriptor_, descriptor);
85 if (should_fail_) {
86 error_callback.Run();
87 return;
88 }
89 last_written_value_ = BluetoothGattServerTest::GetInteger(value);
90 callback.Run();
91 }
92
93 // BluetoothGattServerTest:
94
95 BluetoothGattServerTest::BluetoothGattServerTest() {}
96
97 BluetoothGattServerTest::~BluetoothGattServerTest() {}
98
99 void BluetoothGattServerTest::StartGattSetup() {
100 InitWithFakeAdapter();
101 delegate_ = base::WrapUnique(new TestLocalGattServiceDelegate());
102 service_ = BluetoothLocalGattService::Create(
103 adapter_.get(), BluetoothUUID(kTestUUIDGenericAttribute), true, nullptr,
104 delegate_.get());
105 delegate_->set_expected_service(service_.get());
106 }
107
108 void BluetoothGattServerTest::CompleteGattSetup() {
109 service_->Register(GetCallback(Call::EXPECTED),
110 GetGattErrorCallback(Call::NOT_EXPECTED));
111 EXPECT_EQ(1, callback_count_);
112 EXPECT_EQ(0, error_callback_count_);
113 }
114
115 void BluetoothGattServerTest::SetUp() {
116 BluetoothTest::SetUp();
117
118 last_read_value_ = std::vector<uint8_t>();
119 TestLocalGattServiceDelegate::value_to_write_ = 0;
120 TestLocalGattServiceDelegate::last_written_value_ = 0;
121 TestLocalGattServiceDelegate::should_fail_ = false;
122 }
123
124 void BluetoothGattServerTest::TearDown() {
125 int callback_count = callback_count_;
126 int error_callback_count = error_callback_count_;
127 service_->Unregister(GetCallback(Call::EXPECTED),
128 GetGattErrorCallback(Call::NOT_EXPECTED));
129 EXPECT_EQ(callback_count + 1, callback_count_);
130 EXPECT_EQ(error_callback_count, error_callback_count_);
131
132 delegate_ = nullptr;
133
134 BluetoothTest::TearDown();
135 }
136
137 // static
138 uint64_t BluetoothGattServerTest::GetInteger(
139 const std::vector<uint8_t>& value) {
140 // Handling only up to 4 bytes value for tests.
141 CHECK_LE(value.size(), 4u);
142 uint64_t int_value = 0;
143 uint64_t powers_of_256 = 1;
144 for (uint8_t v : value) {
145 int_value += v * powers_of_256;
146 powers_of_256 *= 256;
147 }
148 return int_value;
149 }
150
151 // static
152 std::vector<uint8_t> BluetoothGattServerTest::GetValue(uint64_t int_value) {
153 CHECK_LE(int_value, 0xFFFFFFFFul);
154 std::vector<uint8_t> value;
155 while (int_value) {
156 value.push_back(int_value & 0xff);
157 int_value >>= 8;
158 }
159 return value;
160 }
161
162 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/test/bluetooth_gatt_server_test.h ('k') | device/bluetooth/test/bluetooth_test.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698