1 // Copyright (c) 2013 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/bluetooth_device_win.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/test/test_simple_task_runner.h"
15 #include "device/bluetooth/bluetooth_service_record_win.h"
16 #include "device/bluetooth/bluetooth_socket_thread.h"
17 #include "device/bluetooth/bluetooth_task_manager_win.h"
18 #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace {
22
23 const char kDeviceName[] = "Device";
24 const char kDeviceAddress[] = "01:02:03:0A:10:A0";
25
26 const char kTestAudioSdpName[] = "Audio";
27 const char kTestAudioSdpBytes[] =
28 "35510900000a00010001090001350319110a09000435103506190100090019350619001909"
29 "010209000535031910020900093508350619110d090102090100250c417564696f20536f75"
30 "726365090311090001";
31 const device::BluetoothUUID kTestAudioSdpUuid("110a");
32
33 const char kTestVideoSdpName[] = "Video";
34 const char kTestVideoSdpBytes[] =
35 "354b0900000a000100030900013506191112191203090004350c3503190100350519000308"
36 "0b090005350319100209000935083506191108090100090100250d566f6963652047617465"
37 "776179";
38 const device::BluetoothUUID kTestVideoSdpUuid("1112");
39
40 } // namespace
41
42 namespace device {
43
44 class BluetoothDeviceWinTest : public testing::Test {
45 public:
BluetoothDeviceWinTest()46 BluetoothDeviceWinTest() {
47 scoped_refptr<base::SequencedTaskRunner> ui_task_runner(
48 new base::TestSimpleTaskRunner());
49 scoped_refptr<BluetoothSocketThread> socket_thread(
50 BluetoothSocketThread::Get());
51
52 // Add device with audio/video services.
53 device_state_.reset(new BluetoothTaskManagerWin::DeviceState());
54 device_state_->name = std::string(kDeviceName);
55 device_state_->address = kDeviceAddress;
56
57 auto audio_state =
58 std::make_unique<BluetoothTaskManagerWin::ServiceRecordState>();
59 audio_state->name = kTestAudioSdpName;
60 base::HexStringToBytes(kTestAudioSdpBytes, &audio_state->sdp_bytes);
61 device_state_->service_record_states.push_back(std::move(audio_state));
62
63 auto video_state =
64 std::make_unique<BluetoothTaskManagerWin::ServiceRecordState>();
65 video_state->name = kTestVideoSdpName;
66 base::HexStringToBytes(kTestVideoSdpBytes, &video_state->sdp_bytes);
67 device_state_->service_record_states.push_back(std::move(video_state));
68
69 device_.reset(new BluetoothDeviceWin(nullptr, *device_state_,
70 ui_task_runner, socket_thread));
71
72 // Add empty device.
73 empty_device_state_.reset(new BluetoothTaskManagerWin::DeviceState());
74 empty_device_state_->name = std::string(kDeviceName);
75 empty_device_state_->address = kDeviceAddress;
76 empty_device_.reset(new BluetoothDeviceWin(nullptr, *empty_device_state_,
77 ui_task_runner, socket_thread));
78 }
79
80 protected:
81 std::unique_ptr<BluetoothDeviceWin> device_;
82 std::unique_ptr<BluetoothTaskManagerWin::DeviceState> device_state_;
83 std::unique_ptr<BluetoothDeviceWin> empty_device_;
84 std::unique_ptr<BluetoothTaskManagerWin::DeviceState> empty_device_state_;
85 };
86
TEST_F(BluetoothDeviceWinTest,GetUUIDs)87 TEST_F(BluetoothDeviceWinTest, GetUUIDs) {
88 BluetoothDevice::UUIDSet uuids = device_->GetUUIDs();
89
90 EXPECT_EQ(2u, uuids.size());
91 EXPECT_TRUE(base::Contains(uuids, kTestAudioSdpUuid));
92 EXPECT_TRUE(base::Contains(uuids, kTestVideoSdpUuid));
93
94 uuids = empty_device_->GetUUIDs();
95 EXPECT_EQ(0u, uuids.size());
96 }
97
TEST_F(BluetoothDeviceWinTest,IsEqual)98 TEST_F(BluetoothDeviceWinTest, IsEqual) {
99 EXPECT_TRUE(device_->IsEqual(*device_state_));
100 EXPECT_FALSE(device_->IsEqual(*empty_device_state_));
101 EXPECT_FALSE(empty_device_->IsEqual(*device_state_));
102 EXPECT_TRUE(empty_device_->IsEqual(*empty_device_state_));
103 }
104
105 } // namespace device
106