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 |
| 61 callback_count_ = 0; |
| 62 error_callback_count_ = 0; |
| 63 audio_sink_ = nullptr; |
| 64 adapter_ = nullptr; |
| 65 |
| 66 GetAdapter(); |
| 67 } |
| 68 |
| 69 void TearDown() override { |
| 70 callback_count_ = 0; |
| 71 error_callback_count_ = 0; |
| 72 |
| 73 // The adapter should outlive audio sink. |
| 74 audio_sink_ = nullptr; |
| 75 adapter_ = nullptr; |
| 76 DBusThreadManager::Shutdown(); |
| 77 } |
| 78 |
| 79 // Get the existing Bluetooth adapter. |
| 80 void GetAdapter() { |
| 81 BluetoothAdapterFactory::GetAdapter( |
| 82 base::Bind(&BluetoothAudioSinkChromeOSTest::GetAdapterCallback, |
| 83 base::Unretained(this))); |
| 84 } |
| 85 |
| 86 void GetAdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { |
| 87 adapter_ = adapter; |
| 88 |
| 89 ASSERT_NE(adapter_.get(), nullptr); |
| 90 ASSERT_TRUE(adapter_->IsInitialized()); |
| 91 adapter_->SetPowered( |
| 92 true, |
| 93 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback, |
| 94 base::Unretained(this)), |
| 95 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback, |
| 96 base::Unretained(this))); |
| 97 ASSERT_TRUE(adapter_->IsPresent()); |
| 98 ASSERT_TRUE(adapter_->IsPowered()); |
| 99 ASSERT_EQ(callback_count_, 1); |
| 100 ASSERT_EQ(error_callback_count_, 0); |
| 101 --callback_count_; |
| 102 } |
| 103 |
| 104 // Called whenever RegisterAudioSink is completed successfully. |
| 105 void RegisterCallback( |
| 106 scoped_refptr<BluetoothAudioSink> audio_sink) { |
| 107 ++callback_count_; |
| 108 audio_sink_ = audio_sink; |
| 109 ASSERT_NE(audio_sink_.get(), nullptr); |
| 110 ASSERT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| 111 } |
| 112 |
| 113 // Called whenever RegisterAudioSink failed. |
| 114 void RegisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) { |
| 115 ++error_callback_count_; |
| 116 ASSERT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_REGISTERED); |
| 117 } |
| 118 |
| 119 // Generic callbacks. |
| 120 void Callback() { |
| 121 ++callback_count_; |
| 122 } |
| 123 |
| 124 void ErrorCallback() { |
| 125 ++error_callback_count_; |
| 126 } |
| 127 |
| 128 protected: |
| 129 int callback_count_; |
| 130 int error_callback_count_; |
| 131 base::MessageLoop message_loop_; |
| 132 scoped_refptr<BluetoothAdapter> adapter_; |
| 133 scoped_refptr<BluetoothAudioSink> audio_sink_; |
| 134 }; |
| 135 |
| 136 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterSucceeded) { |
| 137 // Sets up valid codec and capabilities. |
| 138 BluetoothAudioSink::Options options; |
| 139 ASSERT_EQ(options.codec, 0x00); |
| 140 ASSERT_EQ(options.capabilities, |
| 141 std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35})); |
| 142 adapter_->RegisterAudioSink( |
| 143 options, |
| 144 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback, |
| 145 base::Unretained(this)), |
| 146 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, |
| 147 base::Unretained(this))); |
| 148 |
| 149 // Adds observer for the audio sink. |
| 150 TestAudioSinkObserver observer(audio_sink_); |
| 151 |
| 152 ASSERT_EQ(callback_count_, 1); |
| 153 ASSERT_EQ(error_callback_count_, 0); |
| 154 ASSERT_EQ(observer.state_changed_count_, 0); |
| 155 ASSERT_EQ(observer.volume_changed_count_, 0); |
| 156 } |
| 157 |
| 158 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterFailedWithInvalidOptions) { |
| 159 // Sets options with an invalid codec and valid capabilities. |
| 160 BluetoothAudioSink::Options options; |
| 161 options.codec = 0xff; |
| 162 options.capabilities = std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}); |
| 163 |
| 164 adapter_->RegisterAudioSink( |
| 165 options, |
| 166 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback, |
| 167 base::Unretained(this)), |
| 168 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, |
| 169 base::Unretained(this))); |
| 170 |
| 171 ASSERT_EQ(callback_count_, 0); |
| 172 ASSERT_EQ(error_callback_count_, 1); |
| 173 |
| 174 // Sets options with a valid codec and invalid capabilities. |
| 175 options.codec = 0x00; |
| 176 options.capabilities.clear(); |
| 177 adapter_->RegisterAudioSink( |
| 178 options, |
| 179 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback, |
| 180 base::Unretained(this)), |
| 181 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, |
| 182 base::Unretained(this))); |
| 183 |
| 184 ASSERT_EQ(callback_count_, 0); |
| 185 ASSERT_EQ(error_callback_count_, 2); |
| 186 } |
| 187 |
| 188 } // namespace chromeos |
OLD | NEW |