1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15
16 // Tests for the audio service callback object.
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include <hardware/audio.h>
22
23 #include "audio_service_callback.h"
24
25 namespace brillo {
26
27 class AudioServiceCallbackTest : public testing::Test {
28 public:
SetUp()29 void SetUp() override {
30 connected_call_count_ = 0;
31 disconnected_call_count_ = 0;
32 callback_.OnAudioDeviceAdded = OnDeviceConnectedMock;
33 callback_.OnAudioDeviceRemoved = OnDeviceDisconnectedMock;
34 user_data_ = static_cast<void*>(this);
35 }
36
OnDeviceConnectedMock(const BAudioDeviceInfo *,void * user_data)37 static void OnDeviceConnectedMock(const BAudioDeviceInfo*, void* user_data) {
38 static_cast<AudioServiceCallbackTest*>(user_data)->connected_call_count_++;
39 }
40
OnDeviceDisconnectedMock(const BAudioDeviceInfo *,void * user_data)41 static void OnDeviceDisconnectedMock(const BAudioDeviceInfo*, void* user_data) {
42 static_cast<AudioServiceCallbackTest*>(
43 user_data)->disconnected_call_count_++;
44 }
45
46 BAudioCallback callback_;
47 void* user_data_;
48 int connected_call_count_;
49 int disconnected_call_count_;
50 };
51
TEST_F(AudioServiceCallbackTest,CallbackCallCount)52 TEST_F(AudioServiceCallbackTest, CallbackCallCount) {
53 std::vector<int> devices = {AUDIO_DEVICE_OUT_WIRED_HEADSET,
54 AUDIO_DEVICE_OUT_WIRED_HEADPHONE};
55 AudioServiceCallback service_callback(&callback_, user_data_);
56 service_callback.OnAudioDevicesConnected(devices);
57 EXPECT_EQ(connected_call_count_, devices.size());
58 service_callback.OnAudioDevicesDisconnected(devices);
59 EXPECT_EQ(disconnected_call_count_, devices.size());
60 }
61
62 } // namespace brillo
63