OLD | NEW |
(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_attribute_value_bluez.h" |
| 6 |
| 7 #include <cstdint> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/values.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace bluez { |
| 14 |
| 15 namespace { |
| 16 |
| 17 using Type = BluetoothServiceAttributeValueBlueZ::Type; |
| 18 using Sequence = BluetoothServiceAttributeValueBlueZ::Sequence; |
| 19 |
| 20 constexpr char kServiceUuid[] = "00001801-0000-1000-8000-00805f9b34fb"; |
| 21 |
| 22 void CheckUuidValue(const BluetoothServiceAttributeValueBlueZ& value, |
| 23 const std::string& uuid) { |
| 24 EXPECT_EQ(Type::UUID, value.type()); |
| 25 std::string str; |
| 26 EXPECT_TRUE(value.value().GetAsString(&str)); |
| 27 EXPECT_EQ(uuid, str); |
| 28 } |
| 29 |
| 30 void CheckIntValue(const BluetoothServiceAttributeValueBlueZ& value, |
| 31 uint32_t val) { |
| 32 EXPECT_EQ(Type::INT, value.type()); |
| 33 int i_val; |
| 34 EXPECT_TRUE(value.value().GetAsInteger(&i_val)); |
| 35 EXPECT_EQ(val, static_cast<uint32_t>(i_val)); |
| 36 } |
| 37 |
| 38 // MakeUnique can't use a initializer list directly, since it can't derive the |
| 39 // template type for it unless we explicitly use std::initializer_list({...}) |
| 40 // in the call. This function helps us avoid all that ugly syntax. |
| 41 std::unique_ptr<Sequence> MakeSequence( |
| 42 const std::initializer_list<BluetoothServiceAttributeValueBlueZ> list) { |
| 43 return base::MakeUnique<Sequence>(list); |
| 44 } |
| 45 } |
| 46 |
| 47 TEST(BluetoothServiceAttributeBlueZTest, BasicValue) { |
| 48 BluetoothServiceAttributeValueBlueZ value1( |
| 49 Type::UUID, 16, base::MakeUnique<base::StringValue>(kServiceUuid)); |
| 50 BluetoothServiceAttributeValueBlueZ value2 = value1; |
| 51 |
| 52 CheckUuidValue(value2, kServiceUuid); |
| 53 } |
| 54 |
| 55 TEST(BluetoothServiceAttributeBlueZTest, Sequence) { |
| 56 BluetoothServiceAttributeValueBlueZ value1( |
| 57 Type::UUID, 16, base::MakeUnique<base::StringValue>(kServiceUuid)); |
| 58 BluetoothServiceAttributeValueBlueZ value2( |
| 59 Type::INT, 4, base::MakeUnique<base::FundamentalValue>(0x1337)); |
| 60 BluetoothServiceAttributeValueBlueZ value3( |
| 61 Type::INT, 4, base::MakeUnique<base::FundamentalValue>(0x7331)); |
| 62 |
| 63 BluetoothServiceAttributeValueBlueZ* value4 = |
| 64 new BluetoothServiceAttributeValueBlueZ( |
| 65 MakeSequence({value1, value2, value3})); |
| 66 |
| 67 BluetoothServiceAttributeValueBlueZ value = *value4; |
| 68 delete value4; |
| 69 |
| 70 EXPECT_EQ(3u, value.size()); |
| 71 const Sequence& s = value.sequence(); |
| 72 EXPECT_EQ(3u, s.size()); |
| 73 |
| 74 CheckUuidValue(s[0], kServiceUuid); |
| 75 CheckIntValue(s[2], 0x7331); // Checking out of order by intention. |
| 76 CheckIntValue(s[1], 0x1337); |
| 77 } |
| 78 |
| 79 TEST(BluetoothServiceAttributeBlueZTest, NestedValue) { |
| 80 BluetoothServiceAttributeValueBlueZ value1( |
| 81 Type::UUID, 16, base::MakeUnique<base::StringValue>(kServiceUuid)); |
| 82 BluetoothServiceAttributeValueBlueZ value2( |
| 83 Type::INT, 4, base::MakeUnique<base::FundamentalValue>(0x1337)); |
| 84 BluetoothServiceAttributeValueBlueZ value3(MakeSequence({value1, value2})); |
| 85 BluetoothServiceAttributeValueBlueZ value4(MakeSequence({value3})); |
| 86 |
| 87 BluetoothServiceAttributeValueBlueZ* value5 = |
| 88 new BluetoothServiceAttributeValueBlueZ( |
| 89 MakeSequence({value1, value2, value3, value4})); |
| 90 BluetoothServiceAttributeValueBlueZ value = *value5; |
| 91 delete value5; |
| 92 |
| 93 // Check outer layer first. |
| 94 EXPECT_EQ(4u, value.size()); |
| 95 const Sequence& v5 = value.sequence(); |
| 96 EXPECT_EQ(4u, v5.size()); |
| 97 CheckUuidValue(v5[0], kServiceUuid); |
| 98 CheckIntValue(v5[1], 0x1337); |
| 99 |
| 100 // Check value3. |
| 101 EXPECT_EQ(2u, v5[2].size()); |
| 102 EXPECT_EQ(Type::SEQUENCE, v5[2].type()); |
| 103 const Sequence& v3 = v5[2].sequence(); |
| 104 EXPECT_EQ(2u, v3.size()); |
| 105 CheckUuidValue(v3[0], kServiceUuid); |
| 106 CheckIntValue(v3[1], 0x1337); |
| 107 |
| 108 // Check value4 now. |
| 109 EXPECT_EQ(1u, v5[3].size()); |
| 110 EXPECT_EQ(Type::SEQUENCE, v5[3].type()); |
| 111 const Sequence& v4 = v5[3].sequence(); |
| 112 EXPECT_EQ(1u, v4.size()); |
| 113 |
| 114 // Check value3 again. |
| 115 EXPECT_EQ(2u, v4[0].size()); |
| 116 EXPECT_EQ(Type::SEQUENCE, v4[0].type()); |
| 117 const Sequence& v31 = v4[0].sequence(); |
| 118 EXPECT_EQ(2u, v31.size()); |
| 119 CheckUuidValue(v31[0], kServiceUuid); |
| 120 CheckIntValue(v31[1], 0x1337); |
| 121 } |
| 122 |
| 123 } // namespace bluez |
OLD | NEW |