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 brillo audio manager interface.
17 
18 #include <binderwrapper/binder_test_base.h>
19 #include <binderwrapper/stub_binder_wrapper.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 
23 #include "audio_service_callback.h"
24 #include "brillo_audio_client.h"
25 #include "include/brillo_audio_manager.h"
26 #include "test/brillo_audio_service_mock.h"
27 
28 using android::sp;
29 using testing::Mock;
30 using testing::Return;
31 using testing::_;
32 
33 namespace brillo {
34 
35 static const char kBrilloAudioServiceName[] =
36     "android.brillo.brilloaudioservice.BrilloAudioService";
37 
38 class BrilloAudioManagerTest : public android::BinderTestBase {
39  public:
ConnectBAS()40   void ConnectBAS() {
41     bas_ = new BrilloAudioServiceMock();
42     binder_wrapper()->SetBinderForService(kBrilloAudioServiceName, bas_);
43   }
44 
GetValidManager()45   BAudioManager* GetValidManager() {
46     ConnectBAS();
47     auto bam = BAudioManager_new();
48     EXPECT_NE(bam, nullptr);
49     return bam;
50   }
51 
TearDown()52   void TearDown() {
53     // Stopping the BAS will cause the client to delete itself.
54     binder_wrapper()->NotifyAboutBinderDeath(bas_);
55     bas_.clear();
56   }
57 
58   sp<BrilloAudioServiceMock> bas_;
59 };
60 
TEST_F(BrilloAudioManagerTest,NewNoService)61 TEST_F(BrilloAudioManagerTest, NewNoService) {
62   EXPECT_EQ(BAudioManager_new(), nullptr);
63 }
64 
TEST_F(BrilloAudioManagerTest,NewWithBAS)65 TEST_F(BrilloAudioManagerTest, NewWithBAS) {
66   ConnectBAS();
67   auto bam = BAudioManager_new();
68   EXPECT_NE(bam, nullptr);
69 }
70 
TEST_F(BrilloAudioManagerTest,GetDevicesInvalidParams)71 TEST_F(BrilloAudioManagerTest, GetDevicesInvalidParams) {
72   auto bam = GetValidManager();
73   unsigned int num_devices;
74   EXPECT_EQ(BAudioManager_getDevices(nullptr, 1, nullptr, 0, &num_devices),
75             EINVAL);
76   EXPECT_EQ(BAudioManager_getDevices(bam, 1, nullptr, 0, nullptr), EINVAL);
77   EXPECT_EQ(BAudioManager_getDevices(bam, -1, nullptr, 0, &num_devices),
78             EINVAL);
79 }
80 
TEST_F(BrilloAudioManagerTest,GetDevicesNullArrNoDevices)81 TEST_F(BrilloAudioManagerTest, GetDevicesNullArrNoDevices) {
82   auto bam = GetValidManager();
83   unsigned int num_devices = -1;
84   EXPECT_CALL(*bas_.get(), GetDevices(1, _)).WillOnce(Return(Status::ok()));
85   EXPECT_EQ(BAudioManager_getDevices(bam, 1, nullptr, 0, &num_devices), 0);
86   EXPECT_EQ(num_devices, 0);
87 }
88 
TEST_F(BrilloAudioManagerTest,SetInputDeviceInvalidParams)89 TEST_F(BrilloAudioManagerTest, SetInputDeviceInvalidParams) {
90   auto bam = GetValidManager();
91   auto device = BAudioDeviceInfo_new(TYPE_UNKNOWN);
92   EXPECT_EQ(BAudioManager_setInputDevice(nullptr, nullptr), EINVAL);
93   EXPECT_EQ(BAudioManager_setInputDevice(bam, nullptr), EINVAL);
94   EXPECT_EQ(BAudioManager_setInputDevice(nullptr, device), EINVAL);
95   BAudioDeviceInfo_delete(device);
96 }
97 
TEST_F(BrilloAudioManagerTest,SetInputDeviceHeadsetMic)98 TEST_F(BrilloAudioManagerTest, SetInputDeviceHeadsetMic) {
99   auto bam = GetValidManager();
100   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADSET_MIC);
101   EXPECT_CALL(*bas_.get(), SetDevice(AUDIO_POLICY_FORCE_FOR_RECORD,
102                                      AUDIO_POLICY_FORCE_HEADPHONES))
103       .WillOnce(Return(Status::ok()));
104   EXPECT_EQ(BAudioManager_setInputDevice(bam, device), 0);
105   BAudioDeviceInfo_delete(device);
106 }
107 
TEST_F(BrilloAudioManagerTest,SetInputDeviceBuiltinMic)108 TEST_F(BrilloAudioManagerTest, SetInputDeviceBuiltinMic) {
109   auto bam = GetValidManager();
110   auto device = BAudioDeviceInfo_new(TYPE_BUILTIN_MIC);
111   EXPECT_CALL(*bas_.get(),
112               SetDevice(AUDIO_POLICY_FORCE_FOR_RECORD, AUDIO_POLICY_FORCE_NONE))
113       .WillOnce(Return(Status::ok()));
114   EXPECT_EQ(BAudioManager_setInputDevice(bam, device), 0);
115   BAudioDeviceInfo_delete(device);
116 }
117 
TEST_F(BrilloAudioManagerTest,SetOutputDeviceInvalidParams)118 TEST_F(BrilloAudioManagerTest, SetOutputDeviceInvalidParams) {
119   auto bam = GetValidManager();
120   auto device = BAudioDeviceInfo_new(TYPE_UNKNOWN);
121   EXPECT_EQ(BAudioManager_setOutputDevice(nullptr, nullptr, kUsageMedia),
122             EINVAL);
123   EXPECT_EQ(BAudioManager_setOutputDevice(bam, nullptr, kUsageMedia), EINVAL);
124   EXPECT_EQ(BAudioManager_setOutputDevice(nullptr, device, kUsageMedia),
125             EINVAL);
126   BAudioDeviceInfo_delete(device);
127 }
128 
TEST_F(BrilloAudioManagerTest,SetOutputDeviceWiredHeadset)129 TEST_F(BrilloAudioManagerTest, SetOutputDeviceWiredHeadset) {
130   auto bam = GetValidManager();
131   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADSET);
132   EXPECT_CALL(*bas_.get(), SetDevice(AUDIO_POLICY_FORCE_FOR_MEDIA,
133                                      AUDIO_POLICY_FORCE_HEADPHONES))
134       .WillOnce(Return(Status::ok()));
135   EXPECT_EQ(BAudioManager_setOutputDevice(bam, device, kUsageMedia), 0);
136   BAudioDeviceInfo_delete(device);
137 }
138 
TEST_F(BrilloAudioManagerTest,SetOutputDeviceBuiltinSpeaker)139 TEST_F(BrilloAudioManagerTest, SetOutputDeviceBuiltinSpeaker) {
140   auto bam = GetValidManager();
141   auto device = BAudioDeviceInfo_new(TYPE_BUILTIN_SPEAKER);
142   EXPECT_CALL(*bas_.get(), SetDevice(AUDIO_POLICY_FORCE_FOR_SYSTEM,
143                                      AUDIO_POLICY_FORCE_SPEAKER))
144       .WillOnce(Return(Status::ok()));
145   EXPECT_EQ(BAudioManager_setOutputDevice(bam, device, kUsageSystem), 0);
146   BAudioDeviceInfo_delete(device);
147 }
148 
TEST_F(BrilloAudioManagerTest,SetOutputDeviceWiredHeadphoneNotification)149 TEST_F(BrilloAudioManagerTest, SetOutputDeviceWiredHeadphoneNotification) {
150   auto bam = GetValidManager();
151   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
152   EXPECT_CALL(*bas_.get(), SetDevice(AUDIO_POLICY_FORCE_FOR_SYSTEM,
153                                      AUDIO_POLICY_FORCE_HEADPHONES))
154       .WillOnce(Return(Status::ok()));
155   EXPECT_EQ(BAudioManager_setOutputDevice(bam, device, kUsageNotifications), 0);
156   BAudioDeviceInfo_delete(device);
157 }
158 
TEST_F(BrilloAudioManagerTest,SetOutputDeviceWiredHeadphoneAlarm)159 TEST_F(BrilloAudioManagerTest, SetOutputDeviceWiredHeadphoneAlarm) {
160   auto bam = GetValidManager();
161   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
162   EXPECT_CALL(*bas_.get(), SetDevice(AUDIO_POLICY_FORCE_FOR_SYSTEM,
163                                      AUDIO_POLICY_FORCE_HEADPHONES))
164       .WillOnce(Return(Status::ok()));
165   EXPECT_EQ(BAudioManager_setOutputDevice(bam, device, kUsageAlarm), 0);
166   BAudioDeviceInfo_delete(device);
167 }
168 
TEST_F(BrilloAudioManagerTest,RegisterCallbackInvalidParams)169 TEST_F(BrilloAudioManagerTest, RegisterCallbackInvalidParams) {
170   auto bam = GetValidManager();
171   BAudioCallback callback;
172   int callback_id;
173   EXPECT_EQ(
174       BAudioManager_registerAudioCallback(nullptr, nullptr, nullptr, nullptr),
175       EINVAL);
176   EXPECT_EQ(BAudioManager_registerAudioCallback(bam, nullptr, nullptr, nullptr),
177             EINVAL);
178   EXPECT_EQ(
179       BAudioManager_registerAudioCallback(bam, &callback, nullptr, nullptr),
180       EINVAL);
181   EXPECT_EQ(
182       BAudioManager_registerAudioCallback(bam, nullptr, nullptr, &callback_id),
183       EINVAL);
184 }
185 
TEST_F(BrilloAudioManagerTest,RegisterCallbackOnStack)186 TEST_F(BrilloAudioManagerTest, RegisterCallbackOnStack) {
187   auto bam = GetValidManager();
188   BAudioCallback callback;
189   callback.OnAudioDeviceAdded = nullptr;
190   callback.OnAudioDeviceRemoved = nullptr;
191   int callback_id = 0;
192   EXPECT_CALL(*bas_.get(), RegisterServiceCallback(_))
193       .WillOnce(Return(Status::ok()));
194   EXPECT_EQ(BAudioManager_registerAudioCallback(bam, &callback, nullptr,
195                                                 &callback_id),
196             0);
197   EXPECT_NE(callback_id, 0);
198 }
199 
TEST_F(BrilloAudioManagerTest,RegisterCallbackOnHeap)200 TEST_F(BrilloAudioManagerTest, RegisterCallbackOnHeap) {
201   auto bam = GetValidManager();
202   BAudioCallback* callback = new BAudioCallback;
203   callback->OnAudioDeviceAdded = nullptr;
204   callback->OnAudioDeviceRemoved = nullptr;
205   int callback_id = 0;
206   EXPECT_CALL(*bas_.get(), RegisterServiceCallback(_))
207       .WillOnce(Return(Status::ok()));
208   EXPECT_EQ(
209       BAudioManager_registerAudioCallback(bam, callback, nullptr, &callback_id),
210       0);
211   EXPECT_NE(callback_id, 0);
212   delete callback;
213 }
214 
TEST_F(BrilloAudioManagerTest,UnregisterCallbackInvalidParams)215 TEST_F(BrilloAudioManagerTest, UnregisterCallbackInvalidParams) {
216   auto bam = GetValidManager();
217   EXPECT_EQ(BAudioManager_unregisterAudioCallback(nullptr, 1), EINVAL);
218   EXPECT_EQ(BAudioManager_unregisterAudioCallback(bam, 1), EINVAL);
219 }
220 
TEST_F(BrilloAudioManagerTest,UnregisterCallback)221 TEST_F(BrilloAudioManagerTest, UnregisterCallback) {
222   auto bam = GetValidManager();
223   BAudioCallback callback;
224   callback.OnAudioDeviceAdded = nullptr;
225   callback.OnAudioDeviceRemoved = nullptr;
226   int callback_id = 0;
227   EXPECT_CALL(*bas_.get(), RegisterServiceCallback(_))
228       .WillOnce(Return(Status::ok()));
229   EXPECT_EQ(BAudioManager_registerAudioCallback(bam, &callback, nullptr,
230                                                 &callback_id),
231             0);
232   EXPECT_NE(callback_id, 0);
233   EXPECT_CALL(*bas_.get(), UnregisterServiceCallback(_))
234       .WillOnce(Return(Status::ok()));
235   EXPECT_EQ(BAudioManager_unregisterAudioCallback(bam, callback_id), 0);
236   // 2nd call shouldn't result in a call to BAS.
237   EXPECT_EQ(BAudioManager_unregisterAudioCallback(bam, callback_id), EINVAL);
238 }
239 
TEST_F(BrilloAudioManagerTest,GetDevicesBASDies)240 TEST_F(BrilloAudioManagerTest, GetDevicesBASDies) {
241   auto bam = GetValidManager();
242   unsigned int num_devices = -1;
243   binder_wrapper()->NotifyAboutBinderDeath(bas_);
244   EXPECT_EQ(BAudioManager_getDevices(bam, 1, nullptr, 0, &num_devices),
245             ECONNABORTED);
246 }
247 
TEST_F(BrilloAudioManagerTest,SetInputDeviceBASDies)248 TEST_F(BrilloAudioManagerTest, SetInputDeviceBASDies) {
249   auto bam = GetValidManager();
250   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADSET_MIC);
251   binder_wrapper()->NotifyAboutBinderDeath(bas_);
252   EXPECT_EQ(BAudioManager_setInputDevice(bam, device), ECONNABORTED);
253   BAudioDeviceInfo_delete(device);
254 }
255 
TEST_F(BrilloAudioManagerTest,SetOutputDeviceBASDies)256 TEST_F(BrilloAudioManagerTest, SetOutputDeviceBASDies) {
257   auto bam = GetValidManager();
258   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
259   binder_wrapper()->NotifyAboutBinderDeath(bas_);
260   EXPECT_EQ(BAudioManager_setOutputDevice(bam, device, kUsageNotifications),
261             ECONNABORTED);
262   BAudioDeviceInfo_delete(device);
263 }
264 
TEST_F(BrilloAudioManagerTest,RegisterServiceCallbackBASDies)265 TEST_F(BrilloAudioManagerTest, RegisterServiceCallbackBASDies) {
266   auto bam = GetValidManager();
267   BAudioCallback callback;
268   callback.OnAudioDeviceAdded = nullptr;
269   callback.OnAudioDeviceRemoved = nullptr;
270   int callback_id = 1;
271   binder_wrapper()->NotifyAboutBinderDeath(bas_);
272   EXPECT_EQ(BAudioManager_registerAudioCallback(bam, &callback, nullptr,
273                                                 &callback_id),
274             ECONNABORTED);
275   EXPECT_EQ(callback_id, 0);
276 }
277 
TEST_F(BrilloAudioManagerTest,UnregisterCallbackBASDies)278 TEST_F(BrilloAudioManagerTest, UnregisterCallbackBASDies) {
279   auto bam = GetValidManager();
280   BAudioCallback callback;
281   callback.OnAudioDeviceAdded = nullptr;
282   callback.OnAudioDeviceRemoved = nullptr;
283   int callback_id = 0;
284   EXPECT_CALL(*bas_.get(), RegisterServiceCallback(_))
285       .WillOnce(Return(Status::ok()));
286   EXPECT_EQ(BAudioManager_registerAudioCallback(bam, &callback, nullptr,
287                                                 &callback_id),
288             0);
289   EXPECT_NE(callback_id, 0);
290   binder_wrapper()->NotifyAboutBinderDeath(bas_);
291   EXPECT_EQ(BAudioManager_unregisterAudioCallback(bam, callback_id),
292             ECONNABORTED);
293 }
294 
TEST_F(BrilloAudioManagerTest,GetMaxVolumeStepsInvalidParams)295 TEST_F(BrilloAudioManagerTest, GetMaxVolumeStepsInvalidParams) {
296   auto bam = GetValidManager();
297   int foo;
298   EXPECT_EQ(BAudioManager_getMaxVolumeSteps(
299                 nullptr, BAudioUsage::kUsageMedia, nullptr),
300             EINVAL);
301   EXPECT_EQ(
302       BAudioManager_getMaxVolumeSteps(nullptr, BAudioUsage::kUsageMedia, &foo),
303       EINVAL);
304   EXPECT_EQ(
305       BAudioManager_getMaxVolumeSteps(bam, BAudioUsage::kUsageMedia, nullptr),
306       EINVAL);
307 }
308 
TEST_F(BrilloAudioManagerTest,GetMaxVolStepsWithBAS)309 TEST_F(BrilloAudioManagerTest, GetMaxVolStepsWithBAS) {
310   auto bam = GetValidManager();
311   int foo;
312   EXPECT_CALL(*bas_.get(), GetMaxVolumeSteps(AUDIO_STREAM_MUSIC, &foo))
313       .WillOnce(Return(Status::ok()));
314   EXPECT_EQ(
315       BAudioManager_getMaxVolumeSteps(bam, BAudioUsage::kUsageMedia, &foo), 0);
316 }
317 
TEST_F(BrilloAudioManagerTest,GetMaxVolStepsBASDies)318 TEST_F(BrilloAudioManagerTest, GetMaxVolStepsBASDies) {
319   auto bam = GetValidManager();
320   int foo;
321   binder_wrapper()->NotifyAboutBinderDeath(bas_);
322   EXPECT_EQ(
323       BAudioManager_getMaxVolumeSteps(bam, BAudioUsage::kUsageMedia, &foo),
324       ECONNABORTED);
325 }
326 
TEST_F(BrilloAudioManagerTest,SetMaxVolumeStepsInvalidParams)327 TEST_F(BrilloAudioManagerTest, SetMaxVolumeStepsInvalidParams) {
328   EXPECT_EQ(
329       BAudioManager_setMaxVolumeSteps(nullptr, BAudioUsage::kUsageMedia, 100),
330       EINVAL);
331 }
332 
TEST_F(BrilloAudioManagerTest,SetMaxVolStepsWithBAS)333 TEST_F(BrilloAudioManagerTest, SetMaxVolStepsWithBAS) {
334   auto bam = GetValidManager();
335   EXPECT_CALL(*bas_.get(), SetMaxVolumeSteps(AUDIO_STREAM_MUSIC, 100))
336       .WillOnce(Return(Status::ok()));
337   EXPECT_EQ(BAudioManager_setMaxVolumeSteps(bam, BAudioUsage::kUsageMedia, 100),
338             0);
339 }
340 
TEST_F(BrilloAudioManagerTest,SetMaxVolStepsBASDies)341 TEST_F(BrilloAudioManagerTest, SetMaxVolStepsBASDies) {
342   auto bam = GetValidManager();
343   binder_wrapper()->NotifyAboutBinderDeath(bas_);
344   EXPECT_EQ(BAudioManager_setMaxVolumeSteps(bam, BAudioUsage::kUsageMedia, 100),
345             ECONNABORTED);
346 }
347 
TEST_F(BrilloAudioManagerTest,SetVolIndexInvalidParams)348 TEST_F(BrilloAudioManagerTest, SetVolIndexInvalidParams) {
349   auto bam = GetValidManager();
350   EXPECT_EQ(BAudioManager_setVolumeIndex(
351                 nullptr, BAudioUsage::kUsageMedia, nullptr, 100),
352             EINVAL);
353   EXPECT_EQ(
354       BAudioManager_setVolumeIndex(bam, BAudioUsage::kUsageMedia, nullptr, 100),
355       EINVAL);
356 }
357 
TEST_F(BrilloAudioManagerTest,SetVolIndexWithBAS)358 TEST_F(BrilloAudioManagerTest, SetVolIndexWithBAS) {
359   auto bam = GetValidManager();
360   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
361   EXPECT_CALL(
362       *bas_.get(),
363       SetVolumeIndex(AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, 100))
364       .WillOnce(Return(Status::ok()));
365   EXPECT_EQ(
366       BAudioManager_setVolumeIndex(bam, BAudioUsage::kUsageMedia, device, 100),
367       0);
368   BAudioDeviceInfo_delete(device);
369 }
370 
TEST_F(BrilloAudioManagerTest,SetVolIndexBASDies)371 TEST_F(BrilloAudioManagerTest, SetVolIndexBASDies) {
372   auto bam = GetValidManager();
373   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
374   binder_wrapper()->NotifyAboutBinderDeath(bas_);
375   EXPECT_EQ(
376       BAudioManager_setVolumeIndex(bam, BAudioUsage::kUsageMedia, device, 100),
377       ECONNABORTED);
378   BAudioDeviceInfo_delete(device);
379 }
380 
TEST_F(BrilloAudioManagerTest,GetVolIndexInvalidParams)381 TEST_F(BrilloAudioManagerTest, GetVolIndexInvalidParams) {
382   auto bam = GetValidManager();
383   int foo;
384   EXPECT_EQ(BAudioManager_getVolumeIndex(
385                 nullptr, BAudioUsage::kUsageMedia, nullptr, nullptr),
386             EINVAL);
387   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
388   EXPECT_EQ(BAudioManager_getVolumeIndex(
389                 bam, BAudioUsage::kUsageMedia, device, nullptr),
390             EINVAL);
391   EXPECT_EQ(BAudioManager_getVolumeIndex(
392                 nullptr, BAudioUsage::kUsageMedia, device, &foo),
393             EINVAL);
394   EXPECT_EQ(BAudioManager_getVolumeIndex(
395                 bam, BAudioUsage::kUsageMedia, nullptr, &foo),
396             EINVAL);
397 }
398 
TEST_F(BrilloAudioManagerTest,GetVolIndexWithBAS)399 TEST_F(BrilloAudioManagerTest, GetVolIndexWithBAS) {
400   auto bam = GetValidManager();
401   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
402   int foo;
403   EXPECT_CALL(*bas_.get(),
404               GetVolumeIndex(
405                   AUDIO_STREAM_MUSIC, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, &foo))
406       .WillOnce(Return(Status::ok()));
407   EXPECT_EQ(
408       BAudioManager_getVolumeIndex(bam, BAudioUsage::kUsageMedia, device, &foo),
409       0);
410   BAudioDeviceInfo_delete(device);
411 }
412 
TEST_F(BrilloAudioManagerTest,GetVolIndexBASDies)413 TEST_F(BrilloAudioManagerTest, GetVolIndexBASDies) {
414   auto bam = GetValidManager();
415   auto device = BAudioDeviceInfo_new(TYPE_WIRED_HEADPHONES);
416   int foo;
417   binder_wrapper()->NotifyAboutBinderDeath(bas_);
418   EXPECT_EQ(
419       BAudioManager_getVolumeIndex(bam, BAudioUsage::kUsageMedia, device, &foo),
420       ECONNABORTED);
421   BAudioDeviceInfo_delete(device);
422 }
423 
TEST_F(BrilloAudioManagerTest,GetVolumeControlUsageInvalidParams)424 TEST_F(BrilloAudioManagerTest, GetVolumeControlUsageInvalidParams) {
425   auto bam = GetValidManager();
426   BAudioUsage foo;
427   EXPECT_EQ(BAudioManager_getVolumeControlUsage(nullptr, nullptr), EINVAL);
428   EXPECT_EQ(BAudioManager_getVolumeControlUsage(nullptr, &foo), EINVAL);
429   EXPECT_EQ(BAudioManager_getVolumeControlUsage(bam, nullptr), EINVAL);
430 }
431 
TEST_F(BrilloAudioManagerTest,GetVolumeControlStreamWithBAS)432 TEST_F(BrilloAudioManagerTest, GetVolumeControlStreamWithBAS) {
433   auto bam = GetValidManager();
434   BAudioUsage foo;
435   EXPECT_CALL(*bas_.get(), GetVolumeControlStream(_))
436       .WillOnce(Return(Status::ok()));
437   EXPECT_EQ(BAudioManager_getVolumeControlUsage(bam, &foo), 0);
438 }
439 
TEST_F(BrilloAudioManagerTest,GetVolumeControlStreamBASDies)440 TEST_F(BrilloAudioManagerTest, GetVolumeControlStreamBASDies) {
441   auto bam = GetValidManager();
442   BAudioUsage foo;
443   binder_wrapper()->NotifyAboutBinderDeath(bas_);
444   EXPECT_EQ(BAudioManager_getVolumeControlUsage(bam, &foo), ECONNABORTED);
445 }
446 
TEST_F(BrilloAudioManagerTest,SetVolumeControlUsageInvalidParams)447 TEST_F(BrilloAudioManagerTest, SetVolumeControlUsageInvalidParams) {
448   EXPECT_EQ(
449       BAudioManager_setVolumeControlUsage(nullptr, BAudioUsage::kUsageMedia),
450       EINVAL);
451 }
452 
TEST_F(BrilloAudioManagerTest,SetVolumeControlStreamWithBAS)453 TEST_F(BrilloAudioManagerTest, SetVolumeControlStreamWithBAS) {
454   auto bam = GetValidManager();
455   EXPECT_CALL(*bas_.get(), SetVolumeControlStream(AUDIO_STREAM_MUSIC))
456       .WillOnce(Return(Status::ok()));
457   EXPECT_EQ(BAudioManager_setVolumeControlUsage(bam, BAudioUsage::kUsageMedia),
458             0);
459 }
460 
TEST_F(BrilloAudioManagerTest,SetVolumeControlStreamBASDies)461 TEST_F(BrilloAudioManagerTest, SetVolumeControlStreamBASDies) {
462   auto bam = GetValidManager();
463   binder_wrapper()->NotifyAboutBinderDeath(bas_);
464   EXPECT_EQ(BAudioManager_setVolumeControlUsage(bam, BAudioUsage::kUsageMedia),
465             ECONNABORTED);
466 }
467 
TEST_F(BrilloAudioManagerTest,DecIncInvalidParams)468 TEST_F(BrilloAudioManagerTest, DecIncInvalidParams) {
469   EXPECT_EQ(BAudioManager_decrementVolume(nullptr), EINVAL);
470   EXPECT_EQ(BAudioManager_incrementVolume(nullptr), EINVAL);
471 }
472 
TEST_F(BrilloAudioManagerTest,IncVolWithBAS)473 TEST_F(BrilloAudioManagerTest, IncVolWithBAS) {
474   auto bam = GetValidManager();
475   EXPECT_CALL(*bas_.get(), IncrementVolume()).WillOnce(Return(Status::ok()));
476   EXPECT_EQ(BAudioManager_incrementVolume(bam), 0);
477 }
478 
TEST_F(BrilloAudioManagerTest,IncVolBASDies)479 TEST_F(BrilloAudioManagerTest, IncVolBASDies) {
480   auto bam = GetValidManager();
481   binder_wrapper()->NotifyAboutBinderDeath(bas_);
482   EXPECT_EQ(BAudioManager_incrementVolume(bam), ECONNABORTED);
483 }
484 
TEST_F(BrilloAudioManagerTest,DecVolWithBAS)485 TEST_F(BrilloAudioManagerTest, DecVolWithBAS) {
486   auto bam = GetValidManager();
487   EXPECT_CALL(*bas_.get(), DecrementVolume()).WillOnce(Return(Status::ok()));
488   EXPECT_EQ(BAudioManager_decrementVolume(bam), 0);
489 }
490 
TEST_F(BrilloAudioManagerTest,DecVolBASDies)491 TEST_F(BrilloAudioManagerTest, DecVolBASDies) {
492   auto bam = GetValidManager();
493   binder_wrapper()->NotifyAboutBinderDeath(bas_);
494   EXPECT_EQ(BAudioManager_decrementVolume(bam), ECONNABORTED);
495 }
496 
497 }  // namespace brillo
498