Chromium Code Reviews| Index: device/bluetooth/bluetooth_audio_sink_chromeos_unittest.cc |
| diff --git a/device/bluetooth/bluetooth_audio_sink_chromeos_unittest.cc b/device/bluetooth/bluetooth_audio_sink_chromeos_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8220adba17a3d5575e1269323b18c9b16c3795ce |
| --- /dev/null |
| +++ b/device/bluetooth/bluetooth_audio_sink_chromeos_unittest.cc |
| @@ -0,0 +1,158 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
armansito
2015/01/27 04:38:06
2015
Miao
2015/01/28 02:05:02
:P
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "dbus/object_path.h" |
| +#include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| +#include "device/bluetooth/bluetooth_audio_sink.h" |
| +#include "device/bluetooth/bluetooth_audio_sink_chromeos.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using device::BluetoothAdapter; |
| +using device::BluetoothAudioSink; |
| + |
| +namespace chromeos { |
| + |
| +class TestAudioSinkObserver : public BluetoothAudioSink::Observer { |
| + public: |
| + explicit TestAudioSinkObserver(scoped_refptr<BluetoothAudioSink> audio_sink) |
| + : state_changed_count_(0), |
| + volume_changed_count_(0), |
| + state_(audio_sink->GetState()), |
| + audio_sink_(audio_sink) { |
| + audio_sink_->AddObserver(this); |
| + } |
| + |
| + ~TestAudioSinkObserver() override { audio_sink_->RemoveObserver(this); } |
| + |
| + void BluetoothAudioSinkStateChanged( |
| + BluetoothAudioSink* audio_sink, |
| + BluetoothAudioSink::State state) override { |
| + ++state_changed_count_; |
| + } |
| + |
| + void BluetoothAudioSinkVolumeChanged(BluetoothAudioSink* audio_sink, |
| + uint16_t volume) override { |
| + ++volume_changed_count_; |
| + } |
| + |
| + int state_changed_count_; |
| + int volume_changed_count_; |
| + BluetoothAudioSink::State state_; |
| + |
| + private: |
| + scoped_refptr<BluetoothAudioSink> audio_sink_; |
| +}; |
| + |
| +class BluetoothAudioSinkChromeOSTest : public testing::Test { |
| + public: |
| + void SetUp() override { |
| + chromeos::DBusThreadManager::Initialize(); |
| + callback_count_ = 0; |
| + error_callback_count_ = 0; |
| + } |
| + |
| + void TearDown() override { |
| + callback_count_ = 0; |
| + error_callback_count_ = 0; |
| + audio_sink_ = nullptr; |
| + adapter_ = nullptr; |
| + DBusThreadManager::Shutdown(); |
| + } |
| + |
| + void GetAdapter() { |
| + adapter_ = new BluetoothAdapterChromeOS(); |
| + ASSERT_TRUE(adapter_.get() != nullptr); |
| + ASSERT_TRUE(adapter_->IsInitialized()); |
| + adapter_->SetPowered( |
| + true, |
| + base::Bind(&BluetoothAudioSinkChromeOSTest::Callback, |
| + base::Unretained(this)), |
| + base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback, |
| + base::Unretained(this))); |
| + ASSERT_TRUE(adapter_->IsPresent()); |
| + ASSERT_TRUE(adapter_->IsPowered()); |
| + ASSERT_EQ(callback_count_, 1); |
| + ASSERT_EQ(error_callback_count_, 0); |
| + --callback_count_; |
| + } |
| + |
| + void RegisterCallback( |
| + scoped_refptr<BluetoothAudioSink> audio_sink) { |
| + ++callback_count_; |
| + audio_sink_ = audio_sink; |
| + ASSERT_TRUE(audio_sink_.get() != nullptr); |
| + ASSERT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED); |
| + } |
| + |
| + void RegisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) { |
| + ++error_callback_count_; |
| + ASSERT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_REGISTERED); |
| + } |
| + |
| + // Generic callbacks. |
| + void Callback() { |
| + ++callback_count_; |
| + } |
| + |
| + void ErrorCallback() { |
| + ++error_callback_count_; |
| + } |
| + |
| + protected: |
| + int callback_count_; |
| + int error_callback_count_; |
| + base::MessageLoop message_loop_; |
| + scoped_refptr<BluetoothAdapterChromeOS> adapter_; |
| + scoped_refptr<BluetoothAudioSink> audio_sink_; |
| +}; |
| + |
| +TEST_F(BluetoothAudioSinkChromeOSTest, RegisterSucceeded) { |
| + GetAdapter(); |
| + |
| + BluetoothAudioSink::Options options; |
| + ASSERT_EQ(options.codec, 0x00); |
| + ASSERT_EQ(options.capabilities, |
| + std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35})); |
| + |
| + adapter_->RegisterAudioSink( |
| + options, |
| + base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback, |
| + base::Unretained(this)), |
| + base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, |
| + base::Unretained(this))); |
| + |
| + TestAudioSinkObserver observer(audio_sink_); |
| + |
| + ASSERT_EQ(callback_count_, 1); |
| + ASSERT_EQ(error_callback_count_, 0); |
| + ASSERT_EQ(observer.state_changed_count_, 0); |
| + ASSERT_EQ(observer.volume_changed_count_, 0); |
| +} |
| + |
| +TEST_F(BluetoothAudioSinkChromeOSTest, RegisterFailedWithInvalidOptions) { |
| + GetAdapter(); |
| + |
| + // Initiates invalid options. |
| + BluetoothAudioSink::Options options; |
| + options.codec = 0x00; |
| + options.capabilities = std::vector<uint8_t>({}); |
| + adapter_->RegisterAudioSink( |
| + options, |
| + base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback, |
| + base::Unretained(this)), |
| + base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback, |
| + base::Unretained(this))); |
| + |
| + ASSERT_EQ(callback_count_, 0); |
| + ASSERT_EQ(error_callback_count_, 1); |
| +} |
| + |
| +} // namespace chromeos |