1 /******************************************************************************
2 *
3 * Copyright 2019 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 #include "device.h"
19 #include "classic_device.h"
20
21 #include <gtest/gtest.h>
22
23 using namespace bluetooth::hci;
24
25 static const char* test_addr_str = "bc:9a:78:56:34:12";
26 static const uint8_t test_addr[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc};
27 static const Address address(test_addr);
28
29 namespace bluetooth::hci {
30 namespace {
31 class TestableDevice : public Device {
32 public:
TestableDevice(Address a)33 explicit TestableDevice(Address a) : Device(a, CLASSIC) {}
34
SetTheAddress()35 void SetTheAddress() {
36 Address a({0x01, 0x02, 0x03, 0x04, 0x05, 0x06});
37 this->SetAddress(a);
38 }
SetTheClassOfDevice()39 void SetTheClassOfDevice() {
40 ClassOfDevice class_of_device({0x01, 0x02, 0x03});
41 this->SetClassOfDevice(class_of_device);
42 }
SetTheName()43 void SetTheName() {
44 std::string name = "Some Name";
45 this->SetName(name);
46 }
SetTheIsBonded()47 void SetTheIsBonded() {
48 this->SetIsBonded(true);
49 }
50 };
51 class DeviceTest : public ::testing::Test {
52 public:
DeviceTest()53 DeviceTest() : device_(Address(test_addr)) {}
54
55 protected:
SetUp()56 void SetUp() override {}
57
TearDown()58 void TearDown() override {}
59 TestableDevice device_;
60 };
61
TEST_F(DeviceTest,initial_integrity)62 TEST_F(DeviceTest, initial_integrity) {
63 ASSERT_STREQ(test_addr_str, device_.GetAddress().ToString().c_str());
64 ASSERT_STREQ(test_addr_str, device_.GetUuid().c_str());
65 ASSERT_EQ(DeviceType::CLASSIC, device_.GetDeviceType());
66 ASSERT_EQ("", device_.GetName());
67 }
68
TEST_F(DeviceTest,set_get_class_of_device)69 TEST_F(DeviceTest, set_get_class_of_device) {
70 ClassOfDevice class_of_device({0x01, 0x02, 0x03});
71 ASSERT_NE(class_of_device, device_.GetClassOfDevice());
72 device_.SetTheClassOfDevice();
73 ASSERT_EQ(class_of_device, device_.GetClassOfDevice());
74 }
75
TEST_F(DeviceTest,set_get_name)76 TEST_F(DeviceTest, set_get_name) {
77 std::string name = "Some Name";
78 ASSERT_EQ("", device_.GetName());
79 device_.SetTheName();
80 ASSERT_EQ(name, device_.GetName());
81 }
82
TEST_F(DeviceTest,operator_iseq)83 TEST_F(DeviceTest, operator_iseq) {
84 TestableDevice d(address);
85 EXPECT_EQ(device_, d);
86 }
87
TEST_F(DeviceTest,set_address)88 TEST_F(DeviceTest, set_address) {
89 ASSERT_EQ(test_addr_str, device_.GetAddress().ToString());
90 device_.SetTheAddress();
91 ASSERT_EQ("06:05:04:03:02:01", device_.GetAddress().ToString());
92 }
93
TEST_F(DeviceTest,set_bonded)94 TEST_F(DeviceTest, set_bonded) {
95 ASSERT_FALSE(device_.IsBonded());
96 device_.SetTheIsBonded();
97 ASSERT_TRUE(device_.IsBonded());
98 }
99
100 } // namespace
101 } // namespace bluetooth::hci
102