OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 <vector> | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "chromeos/dbus/dbus_thread_manager.h" | |
12 #include "dbus/object_path.h" | |
13 #include "device/bluetooth/bluetooth_adapter.h" | |
14 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
15 #include "device/bluetooth/bluetooth_audio_sink.h" | |
16 #include "device/bluetooth/bluetooth_audio_sink_chromeos.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using device::BluetoothAdapter; | |
20 using device::BluetoothAdapterFactory; | |
21 using device::BluetoothAudioSink; | |
22 | |
23 namespace chromeos { | |
24 | |
25 class TestAudioSinkObserver : public BluetoothAudioSink::Observer { | |
26 public: | |
27 explicit TestAudioSinkObserver(scoped_refptr<BluetoothAudioSink> audio_sink) | |
28 : state_changed_count_(0), | |
29 volume_changed_count_(0), | |
30 state_(audio_sink->GetState()), | |
31 audio_sink_(audio_sink) { | |
32 audio_sink_->AddObserver(this); | |
33 } | |
34 | |
35 ~TestAudioSinkObserver() override { audio_sink_->RemoveObserver(this); } | |
36 | |
37 void BluetoothAudioSinkStateChanged( | |
38 BluetoothAudioSink* audio_sink, | |
39 BluetoothAudioSink::State state) override { | |
40 ++state_changed_count_; | |
41 } | |
42 | |
43 void BluetoothAudioSinkVolumeChanged(BluetoothAudioSink* audio_sink, | |
44 uint16_t volume) override { | |
45 ++volume_changed_count_; | |
46 } | |
47 | |
48 int state_changed_count_; | |
49 int volume_changed_count_; | |
50 BluetoothAudioSink::State state_; | |
51 | |
52 private: | |
53 scoped_refptr<BluetoothAudioSink> audio_sink_; | |
54 }; | |
55 | |
56 class BluetoothAudioSinkChromeOSTest : public testing::Test { | |
57 public: | |
58 void SetUp() override { | |
59 chromeos::DBusThreadManager::Initialize(); | |
60 callback_count_ = 0; | |
61 error_callback_count_ = 0; | |
62 } | |
63 | |
64 void TearDown() override { | |
65 callback_count_ = 0; | |
66 error_callback_count_ = 0; | |
67 | |
68 // The adapter should outlive audio sink. | |
69 audio_sink_ = nullptr; | |
70 adapter_ = nullptr; | |
71 DBusThreadManager::Shutdown(); | |
72 } | |
73 | |
74 void GetAdapter() { | |
75 BluetoothAdapterFactory::GetAdapter( | |
76 base::Bind(&BluetoothAudioSinkChromeOSTest::AdapterCallback, | |
Ben Chan
2015/01/28 15:54:28
GetAdapterCallback
Miao
2015/01/28 22:34:21
Done.
| |
77 base::Unretained(this))); | |
78 } | |
79 | |
80 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { | |
81 adapter_ = adapter; | |
82 | |
83 ASSERT_TRUE(adapter_.get() != nullptr); | |
Ben Chan
2015/01/28 15:54:28
ASSERT_NE
Miao
2015/01/28 22:34:21
Done.
| |
84 ASSERT_TRUE(adapter_->IsInitialized()); | |
85 adapter_->SetPowered( | |
86 true, | |
87 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback, | |
88 base::Unretained(this)), | |
89 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback, | |
90 base::Unretained(this))); | |
91 ASSERT_TRUE(adapter_->IsPresent()); | |
92 ASSERT_TRUE(adapter_->IsPowered()); | |
93 ASSERT_EQ(callback_count_, 1); | |
94 ASSERT_EQ(error_callback_count_, 0); | |
95 --callback_count_; | |
96 } | |
97 | |
98 void RegisterCallback( | |
99 scoped_refptr<BluetoothAudioSink> audio_sink) { | |
100 ++callback_count_; | |
101 audio_sink_ = audio_sink; | |
102 ASSERT_TRUE(audio_sink_.get() != nullptr); | |
Ben Chan
2015/01/28 15:54:28
ditto
Miao
2015/01/28 22:34:21
Done.
| |
103 ASSERT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); | |
104 } | |
105 | |
106 void RegisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) { | |
107 ++error_callback_count_; | |
108 ASSERT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_REGISTERED); | |
109 } | |
110 | |
111 // Generic callbacks. | |
112 void Callback() { | |
113 ++callback_count_; | |
114 } | |
115 | |
116 void ErrorCallback() { | |
117 ++error_callback_count_; | |
118 } | |
119 | |
120 protected: | |
121 int callback_count_; | |
Ben Chan
2015/01/28 15:54:28
add a constructor to initialize these variables?
Miao
2015/01/28 22:34:21
These variables can be initialized in SetUp() as w
| |
122 int error_callback_count_; | |
123 base::MessageLoop message_loop_; | |
124 scoped_refptr<BluetoothAdapter> adapter_; | |
125 scoped_refptr<BluetoothAudioSink> audio_sink_; | |
126 }; | |
127 | |
128 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterSucceeded) { | |
129 GetAdapter(); | |
130 | |
131 BluetoothAudioSink::Options options; | |
132 ASSERT_EQ(options.codec, 0x00); | |
133 ASSERT_EQ(options.capabilities, | |
134 std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35})); | |
135 adapter_->RegisterAudioSink( | |
136 options, | |
137 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback, | |
138 base::Unretained(this)), | |
139 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, | |
140 base::Unretained(this))); | |
141 | |
142 TestAudioSinkObserver observer(audio_sink_); | |
143 | |
144 ASSERT_EQ(callback_count_, 1); | |
145 ASSERT_EQ(error_callback_count_, 0); | |
146 ASSERT_EQ(observer.state_changed_count_, 0); | |
147 ASSERT_EQ(observer.volume_changed_count_, 0); | |
148 } | |
149 | |
150 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterFailedWithInvalidOptions) { | |
151 GetAdapter(); | |
152 | |
153 // Initiates invalid options. | |
154 BluetoothAudioSink::Options options; | |
155 options.codec = 0x11; | |
156 options.capabilities = std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}); | |
157 | |
158 adapter_->RegisterAudioSink( | |
159 options, | |
160 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback, | |
161 base::Unretained(this)), | |
162 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, | |
163 base::Unretained(this))); | |
164 | |
165 ASSERT_EQ(callback_count_, 0); | |
166 ASSERT_EQ(error_callback_count_, 1); | |
167 | |
168 options.codec = 0x00; | |
169 options.capabilities = std::vector<uint8_t>({}); | |
Ben Chan
2015/01/28 15:54:28
{} isn't necessary. or just call options.capabili
Miao
2015/01/28 22:34:21
Done.
| |
170 adapter_->RegisterAudioSink( | |
171 options, | |
172 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback, | |
173 base::Unretained(this)), | |
174 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, | |
175 base::Unretained(this))); | |
176 | |
177 ASSERT_EQ(callback_count_, 0); | |
178 ASSERT_EQ(error_callback_count_, 2); | |
179 } | |
180 | |
181 } // namespace chromeos | |
OLD | NEW |