Index: device/bluetooth/bluez/bluetooth_service_attribute_value_bluez_unittest.cc |
diff --git a/device/bluetooth/bluez/bluetooth_service_attribute_value_bluez_unittest.cc b/device/bluetooth/bluez/bluetooth_service_attribute_value_bluez_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5b5123b460b10b4d6e9aa30bb89633882ab87230 |
--- /dev/null |
+++ b/device/bluetooth/bluez/bluetooth_service_attribute_value_bluez_unittest.cc |
@@ -0,0 +1,116 @@ |
+// 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/bluez/bluetooth_service_attribute_value_bluez.h" |
+ |
+#include <cstdint> |
+ |
+#include "base/memory/ptr_util.h" |
+#include "base/values.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace bluez { |
+ |
+namespace { |
+ |
+using Type = BluetoothServiceAttributeValueBlueZ::Type; |
+using Sequence = BluetoothServiceAttributeValueBlueZ::Sequence; |
+ |
+constexpr char kServiceUuid[] = "00001801-0000-1000-8000-00805f9b34fb"; |
+ |
+void CheckUuidValue(const BluetoothServiceAttributeValueBlueZ& value, |
+ const std::string& uuid) { |
+ EXPECT_EQ(Type::UUID, value.type()); |
+ std::string str; |
+ EXPECT_TRUE(value.value().GetAsString(&str)); |
+ EXPECT_EQ(uuid, str); |
+} |
+ |
+void CheckIntValue(const BluetoothServiceAttributeValueBlueZ& value, |
+ uint32_t val) { |
+ EXPECT_EQ(Type::INT, value.type()); |
+ int i_val; |
+ EXPECT_TRUE(value.value().GetAsInteger(&i_val)); |
+ EXPECT_EQ(val, static_cast<uint32_t>(i_val)); |
+} |
+} |
+ |
+TEST(BluetoothServiceAttributeBlueZTest, BasicValue) { |
+ BluetoothServiceAttributeValueBlueZ value1( |
+ Type::UUID, 16, base::WrapUnique(new base::StringValue(kServiceUuid))); |
xiyuan
2016/06/22 21:28:44
nit: learned from my other review, we can do
ba
rkc
2016/06/23 19:55:44
Done.
|
+ BluetoothServiceAttributeValueBlueZ value2 = value1; |
+ |
+ CheckUuidValue(value2, kServiceUuid); |
+} |
+ |
+TEST(BluetoothServiceAttributeBlueZTest, Sequence) { |
+ BluetoothServiceAttributeValueBlueZ value1( |
+ Type::UUID, 16, base::WrapUnique(new base::StringValue(kServiceUuid))); |
+ BluetoothServiceAttributeValueBlueZ value2( |
+ Type::INT, 4, base::WrapUnique(new base::FundamentalValue(0x1337))); |
+ BluetoothServiceAttributeValueBlueZ value3( |
+ Type::INT, 4, base::WrapUnique(new base::FundamentalValue(0x7331))); |
+ |
+ BluetoothServiceAttributeValueBlueZ* value4 = |
+ new BluetoothServiceAttributeValueBlueZ( |
+ base::WrapUnique(new Sequence({value1, value2, value3}))); |
+ BluetoothServiceAttributeValueBlueZ value = *value4; |
+ delete value4; |
+ |
+ EXPECT_EQ(3u, value.size()); |
+ const Sequence& s = value.sequence(); |
+ EXPECT_EQ(3u, s.size()); |
+ |
+ CheckUuidValue(s[0], kServiceUuid); |
+ CheckIntValue(s[2], 0x7331); // Checking out of order by intention. |
+ CheckIntValue(s[1], 0x1337); |
+} |
+ |
+TEST(BluetoothServiceAttributeBlueZTest, NestedValue) { |
+ BluetoothServiceAttributeValueBlueZ value1( |
+ Type::UUID, 16, base::WrapUnique(new base::StringValue(kServiceUuid))); |
+ BluetoothServiceAttributeValueBlueZ value2( |
+ Type::INT, 4, base::WrapUnique(new base::FundamentalValue(0x1337))); |
+ BluetoothServiceAttributeValueBlueZ value3( |
+ base::WrapUnique(new Sequence({value1, value2}))); |
+ BluetoothServiceAttributeValueBlueZ value4( |
+ base::WrapUnique(new Sequence({value3}))); |
+ |
+ BluetoothServiceAttributeValueBlueZ* value5 = |
+ new BluetoothServiceAttributeValueBlueZ( |
+ base::WrapUnique(new Sequence({value1, value2, value3, value4}))); |
+ BluetoothServiceAttributeValueBlueZ value = *value5; |
+ delete value5; |
+ |
+ // Check outer layer first. |
+ EXPECT_EQ(4u, value.size()); |
+ const Sequence& v5 = value.sequence(); |
+ EXPECT_EQ(4u, v5.size()); |
+ CheckUuidValue(v5[0], kServiceUuid); |
+ CheckIntValue(v5[1], 0x1337); |
+ |
+ // Check value3. |
+ EXPECT_EQ(2u, v5[2].size()); |
+ EXPECT_EQ(Type::SEQUENCE, v5[2].type()); |
+ const Sequence& v3 = v5[2].sequence(); |
+ EXPECT_EQ(2u, v3.size()); |
+ CheckUuidValue(v3[0], kServiceUuid); |
+ CheckIntValue(v3[1], 0x1337); |
+ |
+ // Check value4 now. |
+ EXPECT_EQ(1u, v5[3].size()); |
+ EXPECT_EQ(Type::SEQUENCE, v5[3].type()); |
+ const Sequence& v4 = v5[3].sequence(); |
+ EXPECT_EQ(1u, v4.size()); |
+ |
+ // Check value3 again. |
+ EXPECT_EQ(2u, v4[0].size()); |
+ EXPECT_EQ(Type::SEQUENCE, v4[0].type()); |
+ const Sequence& v31 = v4[0].sequence(); |
+ EXPECT_EQ(2u, v31.size()); |
+ CheckUuidValue(v31[0], kServiceUuid); |
+ CheckIntValue(v31[1], 0x1337); |
+} |
+ |
+} // namespace bluez |