Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: device/bluetooth/bluetooth_audio_sink_chromeos_unittest.cc

Issue 876153002: device/bluetooth:Implement Register() for BluetoothAudioSinkChromeOS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
armansito 2015/01/27 04:38:06 2015
Miao 2015/01/28 02:05:02 :P
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_chromeos.h"
14 #include "device/bluetooth/bluetooth_audio_sink.h"
15 #include "device/bluetooth/bluetooth_audio_sink_chromeos.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 using device::BluetoothAdapter;
19 using device::BluetoothAudioSink;
20
21 namespace chromeos {
22
23 class TestAudioSinkObserver : public BluetoothAudioSink::Observer {
24 public:
25 explicit TestAudioSinkObserver(scoped_refptr<BluetoothAudioSink> audio_sink)
26 : state_changed_count_(0),
27 volume_changed_count_(0),
28 state_(audio_sink->GetState()),
29 audio_sink_(audio_sink) {
30 audio_sink_->AddObserver(this);
31 }
32
33 ~TestAudioSinkObserver() override { audio_sink_->RemoveObserver(this); }
34
35 void BluetoothAudioSinkStateChanged(
36 BluetoothAudioSink* audio_sink,
37 BluetoothAudioSink::State state) override {
38 ++state_changed_count_;
39 }
40
41 void BluetoothAudioSinkVolumeChanged(BluetoothAudioSink* audio_sink,
42 uint16_t volume) override {
43 ++volume_changed_count_;
44 }
45
46 int state_changed_count_;
47 int volume_changed_count_;
48 BluetoothAudioSink::State state_;
49
50 private:
51 scoped_refptr<BluetoothAudioSink> audio_sink_;
52 };
53
54 class BluetoothAudioSinkChromeOSTest : public testing::Test {
55 public:
56 void SetUp() override {
57 chromeos::DBusThreadManager::Initialize();
58 callback_count_ = 0;
59 error_callback_count_ = 0;
60 }
61
62 void TearDown() override {
63 callback_count_ = 0;
64 error_callback_count_ = 0;
65 audio_sink_ = nullptr;
66 adapter_ = nullptr;
67 DBusThreadManager::Shutdown();
68 }
69
70 void GetAdapter() {
71 adapter_ = new BluetoothAdapterChromeOS();
72 ASSERT_TRUE(adapter_.get() != nullptr);
73 ASSERT_TRUE(adapter_->IsInitialized());
74 adapter_->SetPowered(
75 true,
76 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
77 base::Unretained(this)),
78 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback,
79 base::Unretained(this)));
80 ASSERT_TRUE(adapter_->IsPresent());
81 ASSERT_TRUE(adapter_->IsPowered());
82 ASSERT_EQ(callback_count_, 1);
83 ASSERT_EQ(error_callback_count_, 0);
84 --callback_count_;
85 }
86
87 void RegisterCallback(
88 scoped_refptr<BluetoothAudioSink> audio_sink) {
89 ++callback_count_;
90 audio_sink_ = audio_sink;
91 ASSERT_TRUE(audio_sink_.get() != nullptr);
92 ASSERT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
93 }
94
95 void RegisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) {
96 ++error_callback_count_;
97 ASSERT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_REGISTERED);
98 }
99
100 // Generic callbacks.
101 void Callback() {
102 ++callback_count_;
103 }
104
105 void ErrorCallback() {
106 ++error_callback_count_;
107 }
108
109 protected:
110 int callback_count_;
111 int error_callback_count_;
112 base::MessageLoop message_loop_;
113 scoped_refptr<BluetoothAdapterChromeOS> adapter_;
114 scoped_refptr<BluetoothAudioSink> audio_sink_;
115 };
116
117 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterSucceeded) {
118 GetAdapter();
119
120 BluetoothAudioSink::Options options;
121 ASSERT_EQ(options.codec, 0x00);
122 ASSERT_EQ(options.capabilities,
123 std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}));
124
125 adapter_->RegisterAudioSink(
126 options,
127 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback,
128 base::Unretained(this)),
129 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback,
130 base::Unretained(this)));
131
132 TestAudioSinkObserver observer(audio_sink_);
133
134 ASSERT_EQ(callback_count_, 1);
135 ASSERT_EQ(error_callback_count_, 0);
136 ASSERT_EQ(observer.state_changed_count_, 0);
137 ASSERT_EQ(observer.volume_changed_count_, 0);
138 }
139
140 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterFailedWithInvalidOptions) {
141 GetAdapter();
142
143 // Initiates invalid options.
144 BluetoothAudioSink::Options options;
145 options.codec = 0x00;
146 options.capabilities = std::vector<uint8_t>({});
147 adapter_->RegisterAudioSink(
148 options,
149 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback,
150 base::Unretained(this)),
151 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback,
152 base::Unretained(this)));
153
154 ASSERT_EQ(callback_count_, 0);
155 ASSERT_EQ(error_callback_count_, 1);
156 }
157
158 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698