upload android base code part6

This commit is contained in:
August 2018-08-08 17:48:24 +08:00
parent 421e214c7d
commit 4e516ec6ed
35396 changed files with 9188716 additions and 0 deletions

View file

@ -0,0 +1,44 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Mock of audio daemon.
#ifndef BRILLO_AUDIO_AUDIOSERVICE_TEST_AUDIO_DAEMON_MOCK_H_
#define BRILLO_AUDIO_AUDIOSERVICE_TEST_AUDIO_DAEMON_MOCK_H_
#include <gmock/gmock.h>
#include <gtest/gtest_prod.h>
#include "audio_daemon.h"
namespace brillo {
class AudioDaemonMock : public AudioDaemon {
public:
AudioDaemonMock() = default;
~AudioDaemonMock() {}
private:
friend class AudioDaemonTest;
FRIEND_TEST(AudioDaemonTest, RegisterService);
FRIEND_TEST(AudioDaemonTest, TestAPSConnectInitializesHandlersOnlyOnce);
FRIEND_TEST(AudioDaemonTest, TestDeviceCallbackInitializesBASIfNULL);
MOCK_METHOD0(InitializeHandlers, void());
};
} // namespace brillo
#endif // BRILLO_AUDIO_AUDIOSERVICE_TEST_AUDIO_DAEMON_MOCK_H_

View file

@ -0,0 +1,68 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Tests for audio daemon.
#include "audio_daemon_mock.h"
#include <memory>
#include <vector>
#include <binder/Binder.h>
#include <binderwrapper/binder_test_base.h>
#include <binderwrapper/stub_binder_wrapper.h>
#include <gmock/gmock.h>
#include "audio_device_handler_mock.h"
using android::BinderTestBase;
using android::IInterface;
using std::make_shared;
using testing::_;
using testing::AnyNumber;
namespace brillo {
class AudioDaemonTest : public BinderTestBase {
public:
AudioDaemonMock daemon_;
AudioDeviceHandlerMock device_handler_;
};
TEST_F(AudioDaemonTest, RegisterService) {
daemon_.InitializeBrilloAudioService();
EXPECT_EQ(daemon_.brillo_audio_service_,
binder_wrapper()->GetRegisteredService(
"android.brillo.brilloaudioservice.BrilloAudioService"));
}
TEST_F(AudioDaemonTest, TestAPSConnectInitializesHandlersOnlyOnce) {
binder_wrapper()->SetBinderForService("media.audio_policy",
binder_wrapper()->CreateLocalBinder());
daemon_.handlers_initialized_ = false;
EXPECT_CALL(daemon_, InitializeHandlers()).Times(1);
daemon_.ConnectToAPS();
}
TEST_F(AudioDaemonTest, TestDeviceCallbackInitializesBASIfNULL) {
daemon_.DeviceCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected,
std::vector<int>());
EXPECT_EQ(daemon_.brillo_audio_service_,
binder_wrapper()->GetRegisteredService(
"android.brillo.brilloaudioservice.BrilloAudioService"));
}
} // namespace brillo

View file

@ -0,0 +1,80 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Mock of AudioDeviceHandler.
#ifndef BRILLO_AUDIO_AUDIOSERVICE_TEST_AUDIO_DEVICE_HANDLER_MOCK_H_
#define BRILLO_AUDIO_AUDIOSERVICE_TEST_AUDIO_DEVICE_HANDLER_MOCK_H_
#include <base/files/file_path.h>
#include <gmock/gmock.h>
#include <gtest/gtest_prod.h>
#include <system/audio.h>
#include <system/audio_policy.h>
#include "audio_device_handler.h"
namespace brillo {
class AudioDeviceHandlerMock : public AudioDeviceHandler {
public:
AudioDeviceHandlerMock() = default;
~AudioDeviceHandlerMock() {}
// Reset all local data.
void Reset() {
connected_input_devices_.clear();
connected_output_devices_.clear();
headphone_ = false;
microphone_ = false;
}
private:
friend class AudioDeviceHandlerTest;
FRIEND_TEST(AudioDeviceHandlerTest,
DisconnectAllSupportedDevicesCallsDisconnect);
FRIEND_TEST(AudioDeviceHandlerTest, InitCallsDisconnectAllSupportedDevices);
FRIEND_TEST(AudioDeviceHandlerTest, InitialAudioStateMic);
FRIEND_TEST(AudioDeviceHandlerTest, InitialAudioStateHeadphone);
FRIEND_TEST(AudioDeviceHandlerTest, InitialAudioStateHeadset);
FRIEND_TEST(AudioDeviceHandlerTest, InitialAudioStateNone);
FRIEND_TEST(AudioDeviceHandlerTest, InitialAudioStateInvalid);
FRIEND_TEST(AudioDeviceHandlerTest, InitCallsDisconnect);
FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventEmpty);
FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventMicrophonePresent);
FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventHeadphonePresent);
FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventMicrophoneNotPresent);
FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventHeadphoneNotPresent);
FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventInvalid);
FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemNone);
FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemConnectMic);
FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemConnectHeadphone);
FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemConnectHeadset);
FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectMic);
FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectHeadphone);
FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectHeadset);
FRIEND_TEST(AudioDeviceHandlerTest, ConnectAudioDeviceInput);
FRIEND_TEST(AudioDeviceHandlerTest, ConnectAudioDeviceOutput);
FRIEND_TEST(AudioDeviceHandlerTest, DisconnectAudioDeviceInput);
FRIEND_TEST(AudioDeviceHandlerTest, DisconnectAudioDeviceOutput);
MOCK_METHOD2(NotifyAudioPolicyService,
void(audio_devices_t device, audio_policy_dev_state_t state));
MOCK_METHOD1(TriggerCallback, void(DeviceConnectionState));
};
} // namespace brillo
#endif // BRILLO_AUDIO_AUDIOSERVICE_TEST_AUDIO_DEVICE_HANDLER_MOCK_H_

View file

@ -0,0 +1,408 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Tests for audio device handler.
#include "audio_device_handler_mock.h"
#include <string>
#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/files/scoped_temp_dir.h>
#include <base/strings/string_number_conversions.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using base::FilePath;
using base::IntToString;
using base::ScopedTempDir;
using base::WriteFile;
using brillo::AudioDeviceHandlerMock;
using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
namespace brillo {
class AudioDeviceHandlerTest : public testing::Test {
public:
void SetUp() override {
EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
h2w_file_path_ = temp_dir_.path().Append("h2wstate");
}
void TearDown() override { handler_.Reset(); }
// Method to store the current state of the audio jack to a file.
//
// |value| - Value in the h2w file.
void WriteToH2WFile(int value) {
std::string value_string = IntToString(value);
WriteFile(h2w_file_path_, value_string.c_str(), value_string.length());
}
AudioDeviceHandlerMock handler_;
FilePath h2w_file_path_;
private:
ScopedTempDir temp_dir_;
};
// Test that DisconnectAllSupportedDevices() calls NotifyAudioPolicyService()
// the right number of times.
TEST_F(AudioDeviceHandlerTest, DisconnectAllSupportedDevicesCallsDisconnect) {
EXPECT_CALL(handler_,
NotifyAudioPolicyService(
_, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)).Times(3);
handler_.DisconnectAllSupportedDevices();
EXPECT_EQ(handler_.changed_devices_.size(), 3);
}
// Test that Init() calls DisconnectAllSupportedDevices().
TEST_F(AudioDeviceHandlerTest, InitCallsDisconnectAllSupportedDevices) {
EXPECT_CALL(handler_,
NotifyAudioPolicyService(
_, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)).Times(3);
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesDisconnected))
.Times(AtLeast(1));
EXPECT_CALL(handler_,
NotifyAudioPolicyService(
_, AUDIO_POLICY_DEVICE_STATE_AVAILABLE)).Times(AnyNumber());
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected))
.Times(AnyNumber());
handler_.Init(nullptr);
}
// Test GetInitialAudioDeviceState() with just a microphone.
TEST_F(AudioDeviceHandlerTest, InitialAudioStateMic) {
WriteToH2WFile(2);
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET,
AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
handler_.GetInitialAudioDeviceState(h2w_file_path_);
EXPECT_NE(
handler_.connected_input_devices_.find(AUDIO_DEVICE_IN_WIRED_HEADSET),
handler_.connected_input_devices_.end());
EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
EXPECT_EQ(handler_.changed_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_[0], AUDIO_DEVICE_IN_WIRED_HEADSET);
}
// Test GetInitialAudioDeviceState() with a headphone.
TEST_F(AudioDeviceHandlerTest, InitialAudioStateHeadphone) {
WriteToH2WFile(1);
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
handler_.GetInitialAudioDeviceState(h2w_file_path_);
EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
EXPECT_NE(
handler_.connected_output_devices_.find(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
handler_.connected_output_devices_.end());
EXPECT_EQ(handler_.changed_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_[0], AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
}
// Test GetInitialAudioDeviceState() with a headset.
TEST_F(AudioDeviceHandlerTest, InitialAudioStateHeadset) {
WriteToH2WFile(3);
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET,
AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADSET,
AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
handler_.GetInitialAudioDeviceState(h2w_file_path_);
EXPECT_NE(
handler_.connected_input_devices_.find(AUDIO_DEVICE_IN_WIRED_HEADSET),
handler_.connected_input_devices_.end());
EXPECT_NE(
handler_.connected_output_devices_.find(AUDIO_DEVICE_OUT_WIRED_HEADSET),
handler_.connected_output_devices_.end());
EXPECT_EQ(handler_.changed_devices_.size(), 2);
}
// Test GetInitialAudioDeviceState() without any devices connected to the audio
// jack. No need to call NotifyAudioPolicyService() since that's already handled
// by Init().
TEST_F(AudioDeviceHandlerTest, InitialAudioStateNone) {
WriteToH2WFile(0);
EXPECT_CALL(handler_, TriggerCallback(_));
handler_.GetInitialAudioDeviceState(h2w_file_path_);
EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
EXPECT_EQ(handler_.changed_devices_.size(), 0);
}
// Test GetInitialAudioDeviceState() with an invalid file. The audio handler
// should not fail in this case because it should work on boards that don't
// support audio jacks.
TEST_F(AudioDeviceHandlerTest, InitialAudioStateInvalid) {
FilePath path = h2w_file_path_;
handler_.GetInitialAudioDeviceState(h2w_file_path_);
EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
}
// Test ProcessEvent() with an empty input_event arg.
TEST_F(AudioDeviceHandlerTest, ProcessEventEmpty) {
struct input_event event;
event.type = 0;
event.code = 0;
event.value = 0;
EXPECT_CALL(handler_, TriggerCallback(_));
handler_.ProcessEvent(event);
EXPECT_FALSE(handler_.headphone_);
EXPECT_FALSE(handler_.microphone_);
}
// Test ProcessEvent() with a microphone present input_event arg.
TEST_F(AudioDeviceHandlerTest, ProcessEventMicrophonePresent) {
struct input_event event;
event.type = EV_SW;
event.code = SW_MICROPHONE_INSERT;
event.value = 1;
handler_.ProcessEvent(event);
EXPECT_FALSE(handler_.headphone_);
EXPECT_TRUE(handler_.microphone_);
}
// Test ProcessEvent() with a headphone present input_event arg.
TEST_F(AudioDeviceHandlerTest, ProcessEventHeadphonePresent) {
struct input_event event;
event.type = EV_SW;
event.code = SW_HEADPHONE_INSERT;
event.value = 1;
handler_.ProcessEvent(event);
EXPECT_TRUE(handler_.headphone_);
EXPECT_FALSE(handler_.microphone_);
}
// Test ProcessEvent() with a microphone not present input_event arg.
TEST_F(AudioDeviceHandlerTest, ProcessEventMicrophoneNotPresent) {
struct input_event event;
event.type = EV_SW;
event.code = SW_MICROPHONE_INSERT;
event.value = 0;
handler_.ProcessEvent(event);
EXPECT_FALSE(handler_.headphone_);
EXPECT_FALSE(handler_.microphone_);
}
// Test ProcessEvent() with a headphone not preset input_event arg.
TEST_F(AudioDeviceHandlerTest, ProcessEventHeadphoneNotPresent) {
struct input_event event;
event.type = EV_SW;
event.code = SW_HEADPHONE_INSERT;
event.value = 0;
handler_.ProcessEvent(event);
EXPECT_FALSE(handler_.headphone_);
EXPECT_FALSE(handler_.microphone_);
}
// Test ProcessEvent() with an unsupported input_event arg.
TEST_F(AudioDeviceHandlerTest, ProcessEventInvalid) {
struct input_event event;
event.type = EV_SW;
event.code = SW_MAX;
event.value = 0;
handler_.ProcessEvent(event);
EXPECT_FALSE(handler_.headphone_);
EXPECT_FALSE(handler_.microphone_);
}
// Test UpdateAudioSystem() without any devices connected.
TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemNone) {
EXPECT_CALL(handler_,
NotifyAudioPolicyService(
_, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)).Times(0);
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesDisconnected));
handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
EXPECT_EQ(handler_.changed_devices_.size(), 0);
}
// Test UpdateAudioSystem() when disconnecting a microphone.
TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectMic) {
audio_devices_t device = AUDIO_DEVICE_IN_WIRED_HEADSET;
handler_.connected_input_devices_.insert(device);
EXPECT_CALL(handler_,
NotifyAudioPolicyService(device,
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesDisconnected));
handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
EXPECT_EQ(handler_.changed_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_[0], device);
}
// Test UpdateAudioSystem() when disconnecting a headphone.
TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectHeadphone) {
audio_devices_t device = AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
handler_.connected_output_devices_.insert(device);
EXPECT_CALL(handler_,
NotifyAudioPolicyService(device,
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesDisconnected));
handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
EXPECT_EQ(handler_.changed_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_[0], device);
}
// Test UpdateAudioSystem() when disconnecting a headset & headphones.
TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectHeadset) {
handler_.connected_input_devices_.insert(AUDIO_DEVICE_IN_WIRED_HEADSET);
handler_.connected_output_devices_.insert(AUDIO_DEVICE_OUT_WIRED_HEADSET);
handler_.connected_output_devices_.insert(AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET,
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADSET,
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesDisconnected));
handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
EXPECT_EQ(handler_.changed_devices_.size(), 3);
}
// Test UpdateAudioSystem() when connecting a microphone.
TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemConnectMic) {
handler_.microphone_ = true;
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET,
AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
EXPECT_EQ(handler_.connected_input_devices_.size(), 1);
EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
EXPECT_EQ(handler_.changed_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_[0], AUDIO_DEVICE_IN_WIRED_HEADSET);
}
// Test UpdateAudioSystem() when connecting a headphone.
TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemConnectHeadphone) {
handler_.headphone_ = true;
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
EXPECT_EQ(handler_.connected_output_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_[0], AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
}
// Test UpdateAudioSystem() when connecting a headset.
TEST_F(AudioDeviceHandlerTest, UpdateAudioSystemConnectHeadset) {
handler_.headphone_ = true;
handler_.microphone_ = true;
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_IN_WIRED_HEADSET,
AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
EXPECT_CALL(handler_,
NotifyAudioPolicyService(AUDIO_DEVICE_OUT_WIRED_HEADSET,
AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
EXPECT_CALL(handler_, TriggerCallback(
AudioDeviceHandlerMock::DeviceConnectionState::kDevicesConnected));
handler_.UpdateAudioSystem(handler_.headphone_, handler_.microphone_);
EXPECT_EQ(handler_.connected_input_devices_.size(), 1);
EXPECT_EQ(handler_.connected_output_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_.size(), 2);
}
// Test ConnectAudioDevice() with an input device.
TEST_F(AudioDeviceHandlerTest, ConnectAudioDeviceInput) {
audio_devices_t device = AUDIO_DEVICE_IN_WIRED_HEADSET;
EXPECT_CALL(handler_,
NotifyAudioPolicyService(device,
AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
handler_.ConnectAudioDevice(device);
EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
EXPECT_NE(
handler_.connected_input_devices_.find(device),
handler_.connected_input_devices_.end());
EXPECT_EQ(handler_.changed_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_[0], device);
}
// Test ConnectAudioDevice() with an output device.
TEST_F(AudioDeviceHandlerTest, ConnectAudioDeviceOutput) {
audio_devices_t device = AUDIO_DEVICE_OUT_WIRED_HEADSET;
EXPECT_CALL(handler_,
NotifyAudioPolicyService(device,
AUDIO_POLICY_DEVICE_STATE_AVAILABLE));
handler_.ConnectAudioDevice(device);
EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
EXPECT_NE(
handler_.connected_output_devices_.find(device),
handler_.connected_output_devices_.end());
EXPECT_EQ(handler_.changed_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_[0], device);
}
// Test DisconnectAudioDevice() with an input device.
TEST_F(AudioDeviceHandlerTest, DisconnectAudioDeviceInput) {
audio_devices_t device = AUDIO_DEVICE_IN_WIRED_HEADSET;
handler_.connected_input_devices_.insert(device);
handler_.connected_output_devices_.insert(device);
EXPECT_CALL(handler_,
NotifyAudioPolicyService(device,
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
handler_.DisconnectAudioDevice(device);
EXPECT_EQ(handler_.connected_input_devices_.size(), 0);
EXPECT_EQ(handler_.connected_output_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_[0], device);
}
// Test DisconnectAudioDevice() with an output device.
TEST_F(AudioDeviceHandlerTest, DisconnectAudioDeviceOutput) {
audio_devices_t device = AUDIO_DEVICE_OUT_WIRED_HEADSET;
handler_.connected_input_devices_.insert(device);
handler_.connected_output_devices_.insert(device);
EXPECT_CALL(handler_,
NotifyAudioPolicyService(device,
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
handler_.DisconnectAudioDevice(device);
EXPECT_EQ(handler_.connected_input_devices_.size(), 1);
EXPECT_EQ(handler_.connected_output_devices_.size(), 0);
EXPECT_EQ(handler_.changed_devices_.size(), 1);
EXPECT_EQ(handler_.changed_devices_[0], device);
}
} // namespace brillo

View file

@ -0,0 +1,62 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Tests for the audio service callback object.
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <hardware/audio.h>
#include "audio_service_callback.h"
namespace brillo {
class AudioServiceCallbackTest : public testing::Test {
public:
void SetUp() override {
connected_call_count_ = 0;
disconnected_call_count_ = 0;
callback_.OnAudioDeviceAdded = OnDeviceConnectedMock;
callback_.OnAudioDeviceRemoved = OnDeviceDisconnectedMock;
user_data_ = static_cast<void*>(this);
}
static void OnDeviceConnectedMock(const BAudioDeviceInfo*, void* user_data) {
static_cast<AudioServiceCallbackTest*>(user_data)->connected_call_count_++;
}
static void OnDeviceDisconnectedMock(const BAudioDeviceInfo*, void* user_data) {
static_cast<AudioServiceCallbackTest*>(
user_data)->disconnected_call_count_++;
}
BAudioCallback callback_;
void* user_data_;
int connected_call_count_;
int disconnected_call_count_;
};
TEST_F(AudioServiceCallbackTest, CallbackCallCount) {
std::vector<int> devices = {AUDIO_DEVICE_OUT_WIRED_HEADSET,
AUDIO_DEVICE_OUT_WIRED_HEADPHONE};
AudioServiceCallback service_callback(&callback_, user_data_);
service_callback.OnAudioDevicesConnected(devices);
EXPECT_EQ(connected_call_count_, devices.size());
service_callback.OnAudioDevicesDisconnected(devices);
EXPECT_EQ(disconnected_call_count_, devices.size());
}
} // namespace brillo

View file

@ -0,0 +1,55 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Mock of AudioVolumeHandler.
#ifndef BRILLO_AUDIO_AUDIOSERVICE_TEST_AUDIO_VOLUME_HANDLER_MOCK_H_
#define BRILLO_AUDIO_AUDIOSERVICE_TEST_AUDIO_VOLUME_HANDLER_MOCK_H_
#include <gmock/gmock.h>
#include <gtest/gtest_prod.h>
#include "audio_volume_handler.h"
namespace brillo {
class AudioVolumeHandlerMock : public AudioVolumeHandler {
public:
AudioVolumeHandlerMock() = default;
~AudioVolumeHandlerMock() {}
private:
friend class AudioVolumeHandlerTest;
FRIEND_TEST(AudioVolumeHandlerTest, FileGeneration);
FRIEND_TEST(AudioVolumeHandlerTest, GetVolumeForKey);
FRIEND_TEST(AudioVolumeHandlerTest, GetVolumeForStreamDeviceTuple);
FRIEND_TEST(AudioVolumeHandlerTest, SetVolumeForStreamDeviceTuple);
FRIEND_TEST(AudioVolumeHandlerTest, InitNoFile);
FRIEND_TEST(AudioVolumeHandlerTest, InitFilePresent);
FRIEND_TEST(AudioVolumeHandlerTest, ProcessEventEmpty);
FRIEND_TEST(AudioVolumeHandlerTest, ProcessEventKeyUp);
FRIEND_TEST(AudioVolumeHandlerTest, ProcessEventKeyDown);
FRIEND_TEST(AudioVolumeHandlerTest, SelectStream);
FRIEND_TEST(AudioVolumeHandlerTest, ComputeNewVolume);
FRIEND_TEST(AudioVolumeHandlerTest, GetSetVolumeIndex);
MOCK_METHOD3(TriggerCallback, void(audio_stream_type_t, int, int));
MOCK_METHOD0(InitAPSAllStreams, void());
MOCK_METHOD1(AdjustVolumeActiveStreams, void(int));
};
} // namespace brillo
#endif // BRILLO_AUDIO_AUDIOSERVICE_TEST_AUDIO_VOLUME_HANDLER_MOCK_H_

View file

@ -0,0 +1,212 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Tests for audio volume handler.
#include "audio_volume_handler_mock.h"
#include <memory>
#include <string>
#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/files/scoped_temp_dir.h>
#include <brillo/key_value_store.h>
#include <brillo/strings/string_utils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "audio_device_handler.h"
using base::FilePath;
using base::PathExists;
using base::ScopedTempDir;
using brillo::string_utils::ToString;
using std::stoi;
using testing::_;
namespace brillo {
class AudioVolumeHandlerTest : public testing::Test {
public:
void SetUp() override {
EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
volume_file_path_ = temp_dir_.path().Append("vol_file");
handler_.SetVolumeFilePathForTesting(volume_file_path_);
}
void SetupHandlerVolumeFile() {
handler_.kv_store_ = std::unique_ptr<KeyValueStore>(new KeyValueStore);
handler_.GenerateVolumeFile();
}
AudioVolumeHandlerMock handler_;
FilePath volume_file_path_;
private:
ScopedTempDir temp_dir_;
};
// Test that the volume file is formatted correctly.
TEST_F(AudioVolumeHandlerTest, FileGeneration) {
SetupHandlerVolumeFile();
KeyValueStore kv_store;
kv_store.Load(volume_file_path_);
for (auto stream : handler_.kSupportedStreams_) {
std::string value;
ASSERT_EQ(handler_.kMinIndex_, 0);
ASSERT_EQ(handler_.kMaxIndex_, 100);
for (auto device : AudioDeviceHandler::kSupportedOutputDevices_) {
ASSERT_TRUE(kv_store.GetString(handler_.kCurrentIndexKey_ + "." +
ToString(stream) + "." +
ToString(device),
&value));
ASSERT_EQ(handler_.kDefaultCurrentIndex_, stoi(value));
}
}
}
// Test GetVolumeCurrentIndex.
TEST_F(AudioVolumeHandlerTest, GetVolumeForStreamDeviceTuple) {
handler_.kv_store_ = std::unique_ptr<KeyValueStore>(new KeyValueStore);
handler_.kv_store_->SetString(handler_.kCurrentIndexKey_ + ".1.2", "100");
ASSERT_EQ(
handler_.GetVolumeCurrentIndex(static_cast<audio_stream_type_t>(1), 2),
100);
}
// Test SetVolumeCurrentIndex.
TEST_F(AudioVolumeHandlerTest, SetVolumeForStreamDeviceTuple) {
handler_.kv_store_ = std::unique_ptr<KeyValueStore>(new KeyValueStore);
handler_.PersistVolumeConfiguration(
static_cast<audio_stream_type_t>(1), 2, 100);
std::string value;
auto key = handler_.kCurrentIndexKey_ + ".1.2";
handler_.kv_store_->GetString(key, &value);
ASSERT_EQ(stoi(value), 100);
}
// Test that a new volume file is generated if it doesn't exist.
TEST_F(AudioVolumeHandlerTest, InitNoFile) {
EXPECT_CALL(handler_, InitAPSAllStreams());
handler_.Init(nullptr);
EXPECT_TRUE(PathExists(volume_file_path_));
}
// Test that a new volume file isn't generated it already exists.
TEST_F(AudioVolumeHandlerTest, InitFilePresent) {
KeyValueStore kv_store;
kv_store.SetString("foo", "100");
kv_store.Save(volume_file_path_);
EXPECT_CALL(handler_, InitAPSAllStreams());
handler_.Init(nullptr);
EXPECT_TRUE(PathExists(volume_file_path_));
std::string value;
handler_.kv_store_->GetString("foo", &value);
EXPECT_EQ(stoi(value), 100);
}
TEST_F(AudioVolumeHandlerTest, ProcessEventEmpty) {
struct input_event event;
event.type = 0;
event.code = 0;
event.value = 0;
EXPECT_CALL(handler_, AdjustVolumeActiveStreams(_)).Times(0);
handler_.ProcessEvent(event);
}
TEST_F(AudioVolumeHandlerTest, ProcessEventKeyUp) {
struct input_event event;
event.type = EV_KEY;
event.code = KEY_VOLUMEUP;
event.value = 1;
EXPECT_CALL(handler_, AdjustVolumeActiveStreams(1));
handler_.ProcessEvent(event);
}
TEST_F(AudioVolumeHandlerTest, ProcessEventKeyDown) {
struct input_event event;
event.type = EV_KEY;
event.code = KEY_VOLUMEDOWN;
event.value = 1;
EXPECT_CALL(handler_, AdjustVolumeActiveStreams(-1));
handler_.ProcessEvent(event);
}
TEST_F(AudioVolumeHandlerTest, SelectStream) {
EXPECT_EQ(handler_.GetVolumeControlStream(), AUDIO_STREAM_DEFAULT);
handler_.SetVolumeControlStream(AUDIO_STREAM_MUSIC);
EXPECT_EQ(handler_.GetVolumeControlStream(), AUDIO_STREAM_MUSIC);
}
TEST_F(AudioVolumeHandlerTest, ComputeNewVolume) {
EXPECT_EQ(handler_.GetNewVolumeIndex(50, 1, AUDIO_STREAM_MUSIC), 51);
EXPECT_EQ(handler_.GetNewVolumeIndex(50, -1, AUDIO_STREAM_MUSIC), 49);
handler_.step_sizes_[AUDIO_STREAM_MUSIC] = 10;
EXPECT_EQ(handler_.GetNewVolumeIndex(50, 1, AUDIO_STREAM_MUSIC), 60);
EXPECT_EQ(handler_.GetNewVolumeIndex(50, -1, AUDIO_STREAM_MUSIC), 40);
SetupHandlerVolumeFile();
EXPECT_EQ(handler_.GetNewVolumeIndex(100, 1, AUDIO_STREAM_MUSIC), 100);
EXPECT_EQ(handler_.GetNewVolumeIndex(0, -1, AUDIO_STREAM_MUSIC), 0);
}
TEST_F(AudioVolumeHandlerTest, GetSetMaxSteps) {
EXPECT_EQ(handler_.GetVolumeMaxSteps(AUDIO_STREAM_MUSIC), 100);
EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, 0), EINVAL);
EXPECT_EQ(handler_.GetVolumeMaxSteps(AUDIO_STREAM_MUSIC), 100);
EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, 100), 0);
EXPECT_EQ(handler_.GetVolumeMaxSteps(AUDIO_STREAM_MUSIC), 100);
EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, -1), EINVAL);
EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, 101), EINVAL);
}
TEST_F(AudioVolumeHandlerTest, GetSetVolumeIndex) {
SetupHandlerVolumeFile();
EXPECT_CALL(handler_, TriggerCallback(AUDIO_STREAM_MUSIC, _, 0));
EXPECT_EQ(handler_.SetVolumeIndex(
AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 0),
0);
EXPECT_CALL(handler_, TriggerCallback(AUDIO_STREAM_MUSIC, 0, 50));
EXPECT_EQ(handler_.SetVolumeIndex(
AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 50),
0);
EXPECT_CALL(handler_, TriggerCallback(AUDIO_STREAM_MUSIC, 50, 100));
EXPECT_EQ(handler_.SetVolumeIndex(
AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 100),
0);
EXPECT_EQ(handler_.SetVolumeIndex(
AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, -1),
EINVAL);
EXPECT_EQ(handler_.SetVolumeIndex(
AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 101),
EINVAL);
EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, 10), 0);
EXPECT_EQ(handler_.GetVolumeIndex(AUDIO_STREAM_MUSIC,
AUDIO_DEVICE_OUT_WIRED_HEADSET),
10);
EXPECT_EQ(handler_.SetVolumeIndex(
AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 11),
EINVAL);
EXPECT_CALL(handler_, TriggerCallback(AUDIO_STREAM_MUSIC, 100, 50));
EXPECT_EQ(handler_.SetVolumeIndex(
AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADSET, 5),
0);
EXPECT_EQ(handler_.SetVolumeMaxSteps(AUDIO_STREAM_MUSIC, 20), 0);
EXPECT_EQ(handler_.GetVolumeIndex(AUDIO_STREAM_MUSIC,
AUDIO_DEVICE_OUT_WIRED_HEADSET),
10);
}
} // namespace brillo

View file

@ -0,0 +1,45 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Mock for the brillo audio client.
#ifndef BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_MOCK_H_
#define BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_MOCK_H_
#include <gmock/gmock.h>
#include <gtest/gtest_prod.h>
#include "brillo_audio_client.h"
namespace brillo {
class BrilloAudioClientMock : public BrilloAudioClient {
public:
virtual ~BrilloAudioClientMock() = default;
MOCK_METHOD0(OnBASDisconnect, void());
private:
friend class BrilloAudioClientTest;
FRIEND_TEST(BrilloAudioClientTest,
CheckInitializeRegistersForDeathNotifications);
FRIEND_TEST(BrilloAudioClientTest, InitializeNoService);
BrilloAudioClientMock() = default;
};
} // namespace brillo
#endif // BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_MOCK_H_

View file

@ -0,0 +1,287 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Tests for the brillo audio client.
#include <binderwrapper/binder_test_base.h>
#include <binderwrapper/stub_binder_wrapper.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "audio_service_callback.h"
#include "brillo_audio_client.h"
#include "include/brillo_audio_manager.h"
#include "test/brillo_audio_client_mock.h"
#include "test/brillo_audio_service_mock.h"
using android::sp;
using android::String8;
using testing::Return;
using testing::_;
namespace brillo {
static const char kBrilloAudioServiceName[] =
"android.brillo.brilloaudioservice.BrilloAudioService";
class BrilloAudioClientTest : public android::BinderTestBase {
public:
bool ConnectClientToBAS() {
bas_ = new BrilloAudioServiceMock();
binder_wrapper()->SetBinderForService(kBrilloAudioServiceName, bas_);
return client_.Initialize();
}
BrilloAudioClientMock client_;
sp<BrilloAudioServiceMock> bas_;
};
TEST_F(BrilloAudioClientTest, SetDeviceNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
EXPECT_EQ(
client_.SetDevice(AUDIO_POLICY_FORCE_USE_MAX, AUDIO_POLICY_FORCE_NONE),
ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, GetDevicesNoService) {
std::vector<int> foo;
EXPECT_CALL(client_, OnBASDisconnect());
EXPECT_EQ(client_.GetDevices(0, foo), ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, RegisterCallbackNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
EXPECT_EQ(client_.RegisterAudioCallback(nullptr, nullptr), ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, UnregisterAudioCallbackNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
EXPECT_EQ(client_.UnregisterAudioCallback(0), ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, InitializeNoService) {
EXPECT_FALSE(client_.Initialize());
}
TEST_F(BrilloAudioClientTest, CheckInitializeRegistersForDeathNotifications) {
EXPECT_TRUE(ConnectClientToBAS());
EXPECT_CALL(client_, OnBASDisconnect());
binder_wrapper()->NotifyAboutBinderDeath(bas_);
}
TEST_F(BrilloAudioClientTest, GetDevicesWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
std::vector<int> foo;
EXPECT_CALL(*bas_.get(), GetDevices(0, &foo)).WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.GetDevices(0, foo), 0);
}
TEST_F(BrilloAudioClientTest, SetDeviceWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
std::vector<int> foo;
EXPECT_CALL(*bas_.get(),
SetDevice(AUDIO_POLICY_FORCE_USE_MAX, AUDIO_POLICY_FORCE_NONE))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(
client_.SetDevice(AUDIO_POLICY_FORCE_USE_MAX, AUDIO_POLICY_FORCE_NONE),
0);
}
TEST_F(BrilloAudioClientTest, RegisterCallbackWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
BAudioCallback bcallback;
AudioServiceCallback* callback =
new AudioServiceCallback(&bcallback, nullptr);
int id = 0;
EXPECT_CALL(*bas_.get(),
RegisterServiceCallback(sp<IAudioServiceCallback>(callback)))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.RegisterAudioCallback(callback, &id), 0);
EXPECT_NE(id, 0);
}
TEST_F(BrilloAudioClientTest, RegisterSameCallbackTwiceWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
BAudioCallback bcallback;
AudioServiceCallback* callback =
new AudioServiceCallback(&bcallback, nullptr);
int id = -1;
EXPECT_CALL(*bas_.get(),
RegisterServiceCallback(sp<IAudioServiceCallback>(callback)))
.Times(2)
.WillRepeatedly(Return(Status::ok()));
EXPECT_EQ(client_.RegisterAudioCallback(callback, &id), 0);
EXPECT_NE(id, 0);
id = -1;
EXPECT_EQ(client_.RegisterAudioCallback(callback, &id), EINVAL);
EXPECT_EQ(id, 0);
}
TEST_F(BrilloAudioClientTest, UnregisterAudioCallbackValidWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
BAudioCallback bcallback;
AudioServiceCallback* callback =
new AudioServiceCallback(&bcallback, nullptr);
int id = 0;
EXPECT_CALL(*bas_.get(),
RegisterServiceCallback(sp<IAudioServiceCallback>(callback)))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.RegisterAudioCallback(callback, &id), 0);
EXPECT_NE(id, 0);
EXPECT_CALL(*bas_.get(),
UnregisterServiceCallback(sp<IAudioServiceCallback>(callback)))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.UnregisterAudioCallback(id), 0);
}
TEST_F(BrilloAudioClientTest, UnregisterInvalidCallbackWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
EXPECT_EQ(client_.UnregisterAudioCallback(1), EINVAL);
}
TEST_F(BrilloAudioClientTest, RegisterAndUnregisterAudioTwoCallbacks) {
EXPECT_TRUE(ConnectClientToBAS());
BAudioCallback bcallback1, bcallback2;
AudioServiceCallback* callback1 =
new AudioServiceCallback(&bcallback1, nullptr);
AudioServiceCallback* callback2 =
new AudioServiceCallback(&bcallback2, nullptr);
int id1 = 0, id2 = 0;
EXPECT_CALL(*bas_.get(), RegisterServiceCallback(_))
.WillRepeatedly(Return(Status::ok()));
EXPECT_EQ(client_.RegisterAudioCallback(callback1, &id1), 0);
EXPECT_NE(id1, 0);
EXPECT_EQ(client_.RegisterAudioCallback(callback2, &id2), 0);
EXPECT_NE(id2, 0);
EXPECT_CALL(*bas_.get(), UnregisterServiceCallback(_))
.WillRepeatedly(Return(Status::ok()));
EXPECT_EQ(client_.UnregisterAudioCallback(id1), 0);
EXPECT_EQ(client_.UnregisterAudioCallback(id2), 0);
}
TEST_F(BrilloAudioClientTest, GetMaxVolStepsNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
int foo;
EXPECT_EQ(client_.GetMaxVolumeSteps(BAudioUsage::kUsageInvalid, &foo),
ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, GetMaxVolStepsWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
int foo;
EXPECT_CALL(*bas_.get(), GetMaxVolumeSteps(AUDIO_STREAM_MUSIC, &foo))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.GetMaxVolumeSteps(BAudioUsage::kUsageMedia, &foo), 0);
}
TEST_F(BrilloAudioClientTest, SetMaxVolStepsNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
EXPECT_EQ(client_.SetMaxVolumeSteps(BAudioUsage::kUsageInvalid, 100),
ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, SetMaxVolStepsWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
EXPECT_CALL(*bas_.get(), SetMaxVolumeSteps(AUDIO_STREAM_MUSIC, 100))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.SetMaxVolumeSteps(BAudioUsage::kUsageMedia, 100), 0);
}
TEST_F(BrilloAudioClientTest, SetVolIndexNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
EXPECT_EQ(client_.SetVolumeIndex(
BAudioUsage::kUsageInvalid, AUDIO_DEVICE_NONE, 100),
ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, SetVolIndexWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
EXPECT_CALL(*bas_.get(),
SetVolumeIndex(AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_SPEAKER, 100))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.SetVolumeIndex(
BAudioUsage::kUsageMedia, AUDIO_DEVICE_OUT_SPEAKER, 100),
0);
}
TEST_F(BrilloAudioClientTest, GetVolIndexNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
int foo;
EXPECT_EQ(client_.GetVolumeIndex(
BAudioUsage::kUsageInvalid, AUDIO_DEVICE_NONE, &foo),
ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, GetVolIndexWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
int foo;
EXPECT_CALL(
*bas_.get(),
GetVolumeIndex(AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_SPEAKER, &foo))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.GetVolumeIndex(
BAudioUsage::kUsageMedia, AUDIO_DEVICE_OUT_SPEAKER, &foo),
0);
}
TEST_F(BrilloAudioClientTest, GetVolumeControlStreamNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
BAudioUsage foo;
EXPECT_EQ(client_.GetVolumeControlStream(&foo), ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, GetVolumeControlStreamWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
EXPECT_CALL(*bas_.get(), GetVolumeControlStream(_))
.WillOnce(Return(Status::ok()));
BAudioUsage foo;
EXPECT_EQ(client_.GetVolumeControlStream(&foo), 0);
}
TEST_F(BrilloAudioClientTest, SetVolumeControlStreamNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
EXPECT_EQ(client_.SetVolumeControlStream(kUsageMedia), ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, SetVolumeControlStreamWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
EXPECT_CALL(*bas_.get(), SetVolumeControlStream(AUDIO_STREAM_MUSIC))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.SetVolumeControlStream(kUsageMedia), 0);
}
TEST_F(BrilloAudioClientTest, IncrementVolNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
EXPECT_EQ(client_.IncrementVolume(), ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, IncrementVolWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
EXPECT_CALL(*bas_.get(), IncrementVolume()).WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.IncrementVolume(), 0);
}
TEST_F(BrilloAudioClientTest, DecrementVolNoService) {
EXPECT_CALL(client_, OnBASDisconnect());
EXPECT_EQ(client_.DecrementVolume(), ECONNABORTED);
}
TEST_F(BrilloAudioClientTest, DecrementVolWithBAS) {
EXPECT_TRUE(ConnectClientToBAS());
EXPECT_CALL(*bas_.get(), DecrementVolume()).WillOnce(Return(Status::ok()));
EXPECT_EQ(client_.DecrementVolume(), 0);
}
} // namespace brillo

View file

@ -0,0 +1,51 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Tests for the BrilloAudioDeviceInfoInternal test.
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <hardware/audio.h>
#include "brillo_audio_device_info_internal.h"
namespace brillo {
TEST(BrilloAudioDeviceInfoInternalTest, OutWiredHeadset) {
BAudioDeviceInfoInternal* badi =
BAudioDeviceInfoInternal::CreateFromAudioDevicesT(
AUDIO_DEVICE_OUT_WIRED_HEADSET);
EXPECT_EQ(badi->device_id_, TYPE_WIRED_HEADSET);
EXPECT_EQ(badi->GetConfig(), AUDIO_POLICY_FORCE_HEADPHONES);
}
TEST(BrilloAudioDeviceInfoInternalTest, OutWiredHeadphone) {
BAudioDeviceInfoInternal* badi =
BAudioDeviceInfoInternal::CreateFromAudioDevicesT(
AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
EXPECT_EQ(badi->device_id_, TYPE_WIRED_HEADPHONES);
EXPECT_EQ(badi->GetConfig(), AUDIO_POLICY_FORCE_HEADPHONES);
}
TEST(BrilloAudioDeviceInfoInternalTest, InWiredHeadset) {
BAudioDeviceInfoInternal* badi =
BAudioDeviceInfoInternal::CreateFromAudioDevicesT(
AUDIO_DEVICE_IN_WIRED_HEADSET);
EXPECT_EQ(badi->device_id_, TYPE_WIRED_HEADSET_MIC);
EXPECT_EQ(badi->GetConfig(), AUDIO_POLICY_FORCE_HEADPHONES);
}
} // namespace brillo

View file

@ -0,0 +1,497 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Tests for the brillo audio manager interface.
#include <binderwrapper/binder_test_base.h>
#include <binderwrapper/stub_binder_wrapper.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "audio_service_callback.h"
#include "brillo_audio_client.h"
#include "include/brillo_audio_manager.h"
#include "test/brillo_audio_service_mock.h"
using android::sp;
using testing::Mock;
using testing::Return;
using testing::_;
namespace brillo {
static const char kBrilloAudioServiceName[] =
"android.brillo.brilloaudioservice.BrilloAudioService";
class BrilloAudioManagerTest : public android::BinderTestBase {
public:
void ConnectBAS() {
bas_ = new BrilloAudioServiceMock();
binder_wrapper()->SetBinderForService(kBrilloAudioServiceName, bas_);
}
BAudioManager* GetValidManager() {
ConnectBAS();
auto bam = BAudioManager_new();
EXPECT_NE(bam, nullptr);
return bam;
}
void TearDown() {
// Stopping the BAS will cause the client to delete itself.
binder_wrapper()->NotifyAboutBinderDeath(bas_);
bas_.clear();
}
sp<BrilloAudioServiceMock> bas_;
};
TEST_F(BrilloAudioManagerTest, NewNoService) {
EXPECT_EQ(BAudioManager_new(), nullptr);
}
TEST_F(BrilloAudioManagerTest, NewWithBAS) {
ConnectBAS();
auto bam = BAudioManager_new();
EXPECT_NE(bam, nullptr);
}
TEST_F(BrilloAudioManagerTest, GetDevicesInvalidParams) {
auto bam = GetValidManager();
unsigned int num_devices;
EXPECT_EQ(BAudioManager_getDevices(nullptr, 1, nullptr, 0, &num_devices),
EINVAL);
EXPECT_EQ(BAudioManager_getDevices(bam, 1, nullptr, 0, nullptr), EINVAL);
EXPECT_EQ(BAudioManager_getDevices(bam, -1, nullptr, 0, &num_devices),
EINVAL);
}
TEST_F(BrilloAudioManagerTest, GetDevicesNullArrNoDevices) {
auto bam = GetValidManager();
unsigned int num_devices = -1;
EXPECT_CALL(*bas_.get(), GetDevices(1, _)).WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_getDevices(bam, 1, nullptr, 0, &num_devices), 0);
EXPECT_EQ(num_devices, 0);
}
TEST_F(BrilloAudioManagerTest, SetInputDeviceInvalidParams) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_UNKNOWN);
EXPECT_EQ(BAudioManager_setInputDevice(nullptr, nullptr), EINVAL);
EXPECT_EQ(BAudioManager_setInputDevice(bam, nullptr), EINVAL);
EXPECT_EQ(BAudioManager_setInputDevice(nullptr, device), EINVAL);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, SetInputDeviceHeadsetMic) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADSET_MIC);
EXPECT_CALL(*bas_.get(), SetDevice(AUDIO_POLICY_FORCE_FOR_RECORD,
AUDIO_POLICY_FORCE_HEADPHONES))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_setInputDevice(bam, device), 0);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, SetInputDeviceBuiltinMic) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_BUILTIN_MIC);
EXPECT_CALL(*bas_.get(),
SetDevice(AUDIO_POLICY_FORCE_FOR_RECORD, AUDIO_POLICY_FORCE_NONE))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_setInputDevice(bam, device), 0);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, SetOutputDeviceInvalidParams) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_UNKNOWN);
EXPECT_EQ(BAudioManager_setOutputDevice(nullptr, nullptr, kUsageMedia),
EINVAL);
EXPECT_EQ(BAudioManager_setOutputDevice(bam, nullptr, kUsageMedia), EINVAL);
EXPECT_EQ(BAudioManager_setOutputDevice(nullptr, device, kUsageMedia),
EINVAL);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, SetOutputDeviceWiredHeadset) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADSET);
EXPECT_CALL(*bas_.get(), SetDevice(AUDIO_POLICY_FORCE_FOR_MEDIA,
AUDIO_POLICY_FORCE_HEADPHONES))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_setOutputDevice(bam, device, kUsageMedia), 0);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, SetOutputDeviceBuiltinSpeaker) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_BUILTIN_SPEAKER);
EXPECT_CALL(*bas_.get(), SetDevice(AUDIO_POLICY_FORCE_FOR_SYSTEM,
AUDIO_POLICY_FORCE_SPEAKER))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_setOutputDevice(bam, device, kUsageSystem), 0);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, SetOutputDeviceWiredHeadphoneNotification) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
EXPECT_CALL(*bas_.get(), SetDevice(AUDIO_POLICY_FORCE_FOR_SYSTEM,
AUDIO_POLICY_FORCE_HEADPHONES))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_setOutputDevice(bam, device, kUsageNotifications), 0);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, SetOutputDeviceWiredHeadphoneAlarm) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
EXPECT_CALL(*bas_.get(), SetDevice(AUDIO_POLICY_FORCE_FOR_SYSTEM,
AUDIO_POLICY_FORCE_HEADPHONES))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_setOutputDevice(bam, device, kUsageAlarm), 0);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, RegisterCallbackInvalidParams) {
auto bam = GetValidManager();
BAudioCallback callback;
int callback_id;
EXPECT_EQ(
BAudioManager_registerAudioCallback(nullptr, nullptr, nullptr, nullptr),
EINVAL);
EXPECT_EQ(BAudioManager_registerAudioCallback(bam, nullptr, nullptr, nullptr),
EINVAL);
EXPECT_EQ(
BAudioManager_registerAudioCallback(bam, &callback, nullptr, nullptr),
EINVAL);
EXPECT_EQ(
BAudioManager_registerAudioCallback(bam, nullptr, nullptr, &callback_id),
EINVAL);
}
TEST_F(BrilloAudioManagerTest, RegisterCallbackOnStack) {
auto bam = GetValidManager();
BAudioCallback callback;
callback.OnAudioDeviceAdded = nullptr;
callback.OnAudioDeviceRemoved = nullptr;
int callback_id = 0;
EXPECT_CALL(*bas_.get(), RegisterServiceCallback(_))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_registerAudioCallback(bam, &callback, nullptr,
&callback_id),
0);
EXPECT_NE(callback_id, 0);
}
TEST_F(BrilloAudioManagerTest, RegisterCallbackOnHeap) {
auto bam = GetValidManager();
BAudioCallback* callback = new BAudioCallback;
callback->OnAudioDeviceAdded = nullptr;
callback->OnAudioDeviceRemoved = nullptr;
int callback_id = 0;
EXPECT_CALL(*bas_.get(), RegisterServiceCallback(_))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(
BAudioManager_registerAudioCallback(bam, callback, nullptr, &callback_id),
0);
EXPECT_NE(callback_id, 0);
delete callback;
}
TEST_F(BrilloAudioManagerTest, UnregisterCallbackInvalidParams) {
auto bam = GetValidManager();
EXPECT_EQ(BAudioManager_unregisterAudioCallback(nullptr, 1), EINVAL);
EXPECT_EQ(BAudioManager_unregisterAudioCallback(bam, 1), EINVAL);
}
TEST_F(BrilloAudioManagerTest, UnregisterCallback) {
auto bam = GetValidManager();
BAudioCallback callback;
callback.OnAudioDeviceAdded = nullptr;
callback.OnAudioDeviceRemoved = nullptr;
int callback_id = 0;
EXPECT_CALL(*bas_.get(), RegisterServiceCallback(_))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_registerAudioCallback(bam, &callback, nullptr,
&callback_id),
0);
EXPECT_NE(callback_id, 0);
EXPECT_CALL(*bas_.get(), UnregisterServiceCallback(_))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_unregisterAudioCallback(bam, callback_id), 0);
// 2nd call shouldn't result in a call to BAS.
EXPECT_EQ(BAudioManager_unregisterAudioCallback(bam, callback_id), EINVAL);
}
TEST_F(BrilloAudioManagerTest, GetDevicesBASDies) {
auto bam = GetValidManager();
unsigned int num_devices = -1;
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(BAudioManager_getDevices(bam, 1, nullptr, 0, &num_devices),
ECONNABORTED);
}
TEST_F(BrilloAudioManagerTest, SetInputDeviceBASDies) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADSET_MIC);
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(BAudioManager_setInputDevice(bam, device), ECONNABORTED);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, SetOutputDeviceBASDies) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(BAudioManager_setOutputDevice(bam, device, kUsageNotifications),
ECONNABORTED);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, RegisterServiceCallbackBASDies) {
auto bam = GetValidManager();
BAudioCallback callback;
callback.OnAudioDeviceAdded = nullptr;
callback.OnAudioDeviceRemoved = nullptr;
int callback_id = 1;
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(BAudioManager_registerAudioCallback(bam, &callback, nullptr,
&callback_id),
ECONNABORTED);
EXPECT_EQ(callback_id, 0);
}
TEST_F(BrilloAudioManagerTest, UnregisterCallbackBASDies) {
auto bam = GetValidManager();
BAudioCallback callback;
callback.OnAudioDeviceAdded = nullptr;
callback.OnAudioDeviceRemoved = nullptr;
int callback_id = 0;
EXPECT_CALL(*bas_.get(), RegisterServiceCallback(_))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_registerAudioCallback(bam, &callback, nullptr,
&callback_id),
0);
EXPECT_NE(callback_id, 0);
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(BAudioManager_unregisterAudioCallback(bam, callback_id),
ECONNABORTED);
}
TEST_F(BrilloAudioManagerTest, GetMaxVolumeStepsInvalidParams) {
auto bam = GetValidManager();
int foo;
EXPECT_EQ(BAudioManager_getMaxVolumeSteps(
nullptr, BAudioUsage::kUsageMedia, nullptr),
EINVAL);
EXPECT_EQ(
BAudioManager_getMaxVolumeSteps(nullptr, BAudioUsage::kUsageMedia, &foo),
EINVAL);
EXPECT_EQ(
BAudioManager_getMaxVolumeSteps(bam, BAudioUsage::kUsageMedia, nullptr),
EINVAL);
}
TEST_F(BrilloAudioManagerTest, GetMaxVolStepsWithBAS) {
auto bam = GetValidManager();
int foo;
EXPECT_CALL(*bas_.get(), GetMaxVolumeSteps(AUDIO_STREAM_MUSIC, &foo))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(
BAudioManager_getMaxVolumeSteps(bam, BAudioUsage::kUsageMedia, &foo), 0);
}
TEST_F(BrilloAudioManagerTest, GetMaxVolStepsBASDies) {
auto bam = GetValidManager();
int foo;
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(
BAudioManager_getMaxVolumeSteps(bam, BAudioUsage::kUsageMedia, &foo),
ECONNABORTED);
}
TEST_F(BrilloAudioManagerTest, SetMaxVolumeStepsInvalidParams) {
EXPECT_EQ(
BAudioManager_setMaxVolumeSteps(nullptr, BAudioUsage::kUsageMedia, 100),
EINVAL);
}
TEST_F(BrilloAudioManagerTest, SetMaxVolStepsWithBAS) {
auto bam = GetValidManager();
EXPECT_CALL(*bas_.get(), SetMaxVolumeSteps(AUDIO_STREAM_MUSIC, 100))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_setMaxVolumeSteps(bam, BAudioUsage::kUsageMedia, 100),
0);
}
TEST_F(BrilloAudioManagerTest, SetMaxVolStepsBASDies) {
auto bam = GetValidManager();
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(BAudioManager_setMaxVolumeSteps(bam, BAudioUsage::kUsageMedia, 100),
ECONNABORTED);
}
TEST_F(BrilloAudioManagerTest, SetVolIndexInvalidParams) {
auto bam = GetValidManager();
EXPECT_EQ(BAudioManager_setVolumeIndex(
nullptr, BAudioUsage::kUsageMedia, nullptr, 100),
EINVAL);
EXPECT_EQ(
BAudioManager_setVolumeIndex(bam, BAudioUsage::kUsageMedia, nullptr, 100),
EINVAL);
}
TEST_F(BrilloAudioManagerTest, SetVolIndexWithBAS) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
EXPECT_CALL(
*bas_.get(),
SetVolumeIndex(AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, 100))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(
BAudioManager_setVolumeIndex(bam, BAudioUsage::kUsageMedia, device, 100),
0);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, SetVolIndexBASDies) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(
BAudioManager_setVolumeIndex(bam, BAudioUsage::kUsageMedia, device, 100),
ECONNABORTED);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, GetVolIndexInvalidParams) {
auto bam = GetValidManager();
int foo;
EXPECT_EQ(BAudioManager_getVolumeIndex(
nullptr, BAudioUsage::kUsageMedia, nullptr, nullptr),
EINVAL);
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
EXPECT_EQ(BAudioManager_getVolumeIndex(
bam, BAudioUsage::kUsageMedia, device, nullptr),
EINVAL);
EXPECT_EQ(BAudioManager_getVolumeIndex(
nullptr, BAudioUsage::kUsageMedia, device, &foo),
EINVAL);
EXPECT_EQ(BAudioManager_getVolumeIndex(
bam, BAudioUsage::kUsageMedia, nullptr, &foo),
EINVAL);
}
TEST_F(BrilloAudioManagerTest, GetVolIndexWithBAS) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
int foo;
EXPECT_CALL(*bas_.get(),
GetVolumeIndex(
AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, &foo))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(
BAudioManager_getVolumeIndex(bam, BAudioUsage::kUsageMedia, device, &foo),
0);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, GetVolIndexBASDies) {
auto bam = GetValidManager();
auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
int foo;
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(
BAudioManager_getVolumeIndex(bam, BAudioUsage::kUsageMedia, device, &foo),
ECONNABORTED);
BAudioDeviceInfo_delete(device);
}
TEST_F(BrilloAudioManagerTest, GetVolumeControlUsageInvalidParams) {
auto bam = GetValidManager();
BAudioUsage foo;
EXPECT_EQ(BAudioManager_getVolumeControlUsage(nullptr, nullptr), EINVAL);
EXPECT_EQ(BAudioManager_getVolumeControlUsage(nullptr, &foo), EINVAL);
EXPECT_EQ(BAudioManager_getVolumeControlUsage(bam, nullptr), EINVAL);
}
TEST_F(BrilloAudioManagerTest, GetVolumeControlStreamWithBAS) {
auto bam = GetValidManager();
BAudioUsage foo;
EXPECT_CALL(*bas_.get(), GetVolumeControlStream(_))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_getVolumeControlUsage(bam, &foo), 0);
}
TEST_F(BrilloAudioManagerTest, GetVolumeControlStreamBASDies) {
auto bam = GetValidManager();
BAudioUsage foo;
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(BAudioManager_getVolumeControlUsage(bam, &foo), ECONNABORTED);
}
TEST_F(BrilloAudioManagerTest, SetVolumeControlUsageInvalidParams) {
EXPECT_EQ(
BAudioManager_setVolumeControlUsage(nullptr, BAudioUsage::kUsageMedia),
EINVAL);
}
TEST_F(BrilloAudioManagerTest, SetVolumeControlStreamWithBAS) {
auto bam = GetValidManager();
EXPECT_CALL(*bas_.get(), SetVolumeControlStream(AUDIO_STREAM_MUSIC))
.WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_setVolumeControlUsage(bam, BAudioUsage::kUsageMedia),
0);
}
TEST_F(BrilloAudioManagerTest, SetVolumeControlStreamBASDies) {
auto bam = GetValidManager();
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(BAudioManager_setVolumeControlUsage(bam, BAudioUsage::kUsageMedia),
ECONNABORTED);
}
TEST_F(BrilloAudioManagerTest, DecIncInvalidParams) {
EXPECT_EQ(BAudioManager_decrementVolume(nullptr), EINVAL);
EXPECT_EQ(BAudioManager_incrementVolume(nullptr), EINVAL);
}
TEST_F(BrilloAudioManagerTest, IncVolWithBAS) {
auto bam = GetValidManager();
EXPECT_CALL(*bas_.get(), IncrementVolume()).WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_incrementVolume(bam), 0);
}
TEST_F(BrilloAudioManagerTest, IncVolBASDies) {
auto bam = GetValidManager();
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(BAudioManager_incrementVolume(bam), ECONNABORTED);
}
TEST_F(BrilloAudioManagerTest, DecVolWithBAS) {
auto bam = GetValidManager();
EXPECT_CALL(*bas_.get(), DecrementVolume()).WillOnce(Return(Status::ok()));
EXPECT_EQ(BAudioManager_decrementVolume(bam), 0);
}
TEST_F(BrilloAudioManagerTest, DecVolBASDies) {
auto bam = GetValidManager();
binder_wrapper()->NotifyAboutBinderDeath(bas_);
EXPECT_EQ(BAudioManager_decrementVolume(bam), ECONNABORTED);
}
} // namespace brillo

View file

@ -0,0 +1,58 @@
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_SERVICE_MOCK_H_
#define BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_SERVICE_MOCK_H_
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest_prod.h>
#include "brillo_audio_service.h"
namespace brillo {
class BrilloAudioServiceMock : public BrilloAudioService {
public:
BrilloAudioServiceMock() = default;
~BrilloAudioServiceMock() {}
MOCK_METHOD2(GetDevices, Status(int flag, std::vector<int>* _aidl_return));
MOCK_METHOD2(SetDevice, Status(int usage, int config));
MOCK_METHOD2(GetMaxVolumeSteps, Status(int stream, int* _aidl_return));
MOCK_METHOD2(SetMaxVolumeSteps, Status(int stream, int max_steps));
MOCK_METHOD3(SetVolumeIndex, Status(int stream, int device, int index));
MOCK_METHOD3(GetVolumeIndex,
Status(int stream, int device, int* _aidl_return));
MOCK_METHOD1(GetVolumeControlStream, Status(int* _aidl_return));
MOCK_METHOD1(SetVolumeControlStream, Status(int stream));
MOCK_METHOD0(IncrementVolume, Status());
MOCK_METHOD0(DecrementVolume, Status());
MOCK_METHOD1(RegisterServiceCallback,
Status(const android::sp<IAudioServiceCallback>& callback));
MOCK_METHOD1(UnregisterServiceCallback,
Status(const android::sp<IAudioServiceCallback>& callback));
void RegisterHandlers(std::weak_ptr<AudioDeviceHandler>,
std::weak_ptr<AudioVolumeHandler>){};
void OnDevicesConnected(const std::vector<int>&) {}
void OnDevicesDisconnected(const std::vector<int>&) {}
void OnVolumeChanged(audio_stream_type_t, int, int){};
};
} // namespace brillo
#endif // BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_SERVICE_MOCK_H_