blob: 10215698b8d87325661e423e7b0c6b1abcd8d7d6 [file] [log] [blame]
Kevin Rocardf0357882017-02-10 16:19:28 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "VtsHalAudioV2_0TargetTest"
18
19#include <algorithm>
20#include <cmath>
21#include <cstddef>
22#include <cstdio>
23#include <limits>
24#include <list>
25#include <string>
26#include <type_traits>
27#include <vector>
28
Yuexi Ma161b5642017-03-10 13:44:22 -080029#include <VtsHalHidlTargetTestBase.h>
Kevin Rocardf0357882017-02-10 16:19:28 -080030
31#include <android-base/logging.h>
32
33#include <android/hardware/audio/2.0/IDevice.h>
34#include <android/hardware/audio/2.0/IDevicesFactory.h>
Kevin Rocard3c405a72017-03-08 16:46:51 -080035#include <android/hardware/audio/2.0/IPrimaryDevice.h>
Kevin Rocardf0357882017-02-10 16:19:28 -080036#include <android/hardware/audio/2.0/types.h>
37#include <android/hardware/audio/common/2.0/types.h>
38
39#include "utility/ReturnIn.h"
40#include "utility/AssertOk.h"
41#include "utility/PrettyPrintAudioTypes.h"
42
43using std::string;
44using std::to_string;
45using std::vector;
46
47using ::android::sp;
48using ::android::hardware::Return;
49using ::android::hardware::hidl_handle;
50using ::android::hardware::hidl_string;
51using ::android::hardware::hidl_vec;
Kevin Rocardc9963522017-03-10 18:47:37 -080052using ::android::hardware::MQDescriptorSync;
Kevin Rocard624800c2017-03-10 18:47:37 -080053using ::android::hardware::audio::V2_0::AudioDrain;
Kevin Rocardf0357882017-02-10 16:19:28 -080054using ::android::hardware::audio::V2_0::DeviceAddress;
55using ::android::hardware::audio::V2_0::IDevice;
Kevin Rocard3c405a72017-03-08 16:46:51 -080056using ::android::hardware::audio::V2_0::IPrimaryDevice;
57using TtyMode = ::android::hardware::audio::V2_0::IPrimaryDevice::TtyMode;
Kevin Rocardf0357882017-02-10 16:19:28 -080058using ::android::hardware::audio::V2_0::IDevicesFactory;
59using ::android::hardware::audio::V2_0::IStream;
60using ::android::hardware::audio::V2_0::IStreamIn;
Kevin Rocard624800c2017-03-10 18:47:37 -080061using ::android::hardware::audio::V2_0::TimeSpec;
Kevin Rocardc9963522017-03-10 18:47:37 -080062using ReadParameters = ::android::hardware::audio::V2_0::IStreamIn::ReadParameters;
63using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus;
Kevin Rocardf0357882017-02-10 16:19:28 -080064using ::android::hardware::audio::V2_0::IStreamOut;
Kevin Rocard624800c2017-03-10 18:47:37 -080065using ::android::hardware::audio::V2_0::IStreamOutCallback;
Kevin Rocard8878b4b2017-03-10 18:47:37 -080066using ::android::hardware::audio::V2_0::MmapBufferInfo;
67using ::android::hardware::audio::V2_0::MmapPosition;
Kevin Rocardf0357882017-02-10 16:19:28 -080068using ::android::hardware::audio::V2_0::ParameterValue;
69using ::android::hardware::audio::V2_0::Result;
70using ::android::hardware::audio::common::V2_0::AudioChannelMask;
71using ::android::hardware::audio::common::V2_0::AudioConfig;
72using ::android::hardware::audio::common::V2_0::AudioDevice;
73using ::android::hardware::audio::common::V2_0::AudioFormat;
74using ::android::hardware::audio::common::V2_0::AudioHandleConsts;
75using ::android::hardware::audio::common::V2_0::AudioInputFlag;
76using ::android::hardware::audio::common::V2_0::AudioIoHandle;
Kevin Rocard3c405a72017-03-08 16:46:51 -080077using ::android::hardware::audio::common::V2_0::AudioMode;
Kevin Rocardf0357882017-02-10 16:19:28 -080078using ::android::hardware::audio::common::V2_0::AudioOffloadInfo;
79using ::android::hardware::audio::common::V2_0::AudioOutputFlag;
80using ::android::hardware::audio::common::V2_0::AudioSource;
Kevin Rocardc9963522017-03-10 18:47:37 -080081using ::android::hardware::audio::common::V2_0::ThreadInfo;
Kevin Rocardf0357882017-02-10 16:19:28 -080082
83using utility::returnIn;
84
85namespace doc {
86/** Document the current test case.
87 * Eg: calling `doc::test("Dump the state of the hal")` in the "debugDump" test will output:
88 * <testcase name="debugDump" status="run" time="6" classname="AudioPrimaryHidlTest"
89 description="Dump the state of the hal." />
90 * see https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#logging-additional-information
91 */
92void test(const std::string& testCaseDocumentation) {
93 ::testing::Test::RecordProperty("description", testCaseDocumentation);
94}
95
96/** Document why a test was not fully run. Usually due to an optional feature not implemented. */
97void partialTest(const std::string& reason) {
98 ::testing::Test::RecordProperty("partialyRunTest", reason);
99}
100}
101
102// Register callback for static object destruction
103// Avoid destroying static objects after main return.
104// Post main return destruction leads to incorrect gtest timing measurements as well as harder
105// debuging if anything goes wrong during destruction.
106class Environment : public ::testing::Environment {
107public:
108 using TearDownFunc = std::function<void ()>;
109 void registerTearDown(TearDownFunc&& tearDown) {
110 tearDowns.push_back(std::move(tearDown));
111 }
112
113private:
114 void TearDown() override {
115 // Call the tear downs in reverse order of insertion
116 for (auto& tearDown : tearDowns) {
117 tearDown();
118 }
119 }
120 std::list<TearDownFunc> tearDowns;
121};
122// Instance to register global tearDown
123static Environment* environment;
124
Yuexi Ma161b5642017-03-10 13:44:22 -0800125class HidlTest : public ::testing::VtsHalHidlTargetTestBase {
Kevin Rocardf0357882017-02-10 16:19:28 -0800126protected:
127 // Convenient member to store results
128 Result res;
129};
130
131//////////////////////////////////////////////////////////////////////////////
132////////////////////// getService audio_devices_factory //////////////////////
133//////////////////////////////////////////////////////////////////////////////
134
135// Test all audio devices
136class AudioHidlTest : public HidlTest {
137public:
138 void SetUp() override {
139 ASSERT_NO_FATAL_FAILURE(HidlTest::SetUp()); // setup base
140
141 if (devicesFactory == nullptr) {
142 environment->registerTearDown([]{ devicesFactory.clear(); });
Yuexi Ma161b5642017-03-10 13:44:22 -0800143 devicesFactory = ::testing::VtsHalHidlTargetTestBase::getService<IDevicesFactory>();
Kevin Rocardf0357882017-02-10 16:19:28 -0800144 }
145 ASSERT_TRUE(devicesFactory != nullptr);
146 }
147
148protected:
149 // Cache the devicesFactory retrieval to speed up each test by ~0.5s
150 static sp<IDevicesFactory> devicesFactory;
151};
152sp<IDevicesFactory> AudioHidlTest::devicesFactory;
153
154TEST_F(AudioHidlTest, GetAudioDevicesFactoryService) {
155 doc::test("test the getService (called in SetUp)");
156}
157
158//////////////////////////////////////////////////////////////////////////////
159/////////////////////////////// openDevice primary ///////////////////////////
160//////////////////////////////////////////////////////////////////////////////
161
162// Test the primary device
163class AudioPrimaryHidlTest : public AudioHidlTest {
164public:
165 /** Primary HAL test are NOT thread safe. */
166 void SetUp() override {
167 ASSERT_NO_FATAL_FAILURE(AudioHidlTest::SetUp()); // setup base
168
169 if (device == nullptr) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800170 IDevicesFactory::Result result;
Kevin Rocard3c405a72017-03-08 16:46:51 -0800171 sp<IDevice> baseDevice;
Kevin Rocardf0357882017-02-10 16:19:28 -0800172 ASSERT_OK(devicesFactory->openDevice(IDevicesFactory::Device::PRIMARY,
Kevin Rocard3c405a72017-03-08 16:46:51 -0800173 returnIn(result, baseDevice)));
174 ASSERT_TRUE(baseDevice != nullptr);
175
176 environment->registerTearDown([]{ device.clear(); });
177 device = IPrimaryDevice::castFrom(baseDevice);
178 ASSERT_TRUE(device != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800179 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800180 }
181
182protected:
183 // Cache the device opening to speed up each test by ~0.5s
Kevin Rocard3c405a72017-03-08 16:46:51 -0800184 static sp<IPrimaryDevice> device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800185};
Kevin Rocard3c405a72017-03-08 16:46:51 -0800186sp<IPrimaryDevice> AudioPrimaryHidlTest::device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800187
188TEST_F(AudioPrimaryHidlTest, OpenPrimaryDevice) {
189 doc::test("Test the openDevice (called in SetUp)");
190}
191
192TEST_F(AudioPrimaryHidlTest, Init) {
193 doc::test("Test that the audio primary hal initialized correctly");
194 ASSERT_OK(device->initCheck());
195}
196
197//////////////////////////////////////////////////////////////////////////////
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800198///////////////////// {set,get}{Master,Mic}{Mute,Volume} /////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800199//////////////////////////////////////////////////////////////////////////////
200
Kevin Rocard3c405a72017-03-08 16:46:51 -0800201template <class Property>
202class AccessorPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocardf0357882017-02-10 16:19:28 -0800203protected:
Kevin Rocard3c405a72017-03-08 16:46:51 -0800204
205 /** Test a property getter and setter. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800206 template <class Getter, class Setter>
Kevin Rocard3c405a72017-03-08 16:46:51 -0800207 void testAccessors(const string& propertyName, const vector<Property>& valuesToTest,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800208 Setter setter, Getter getter,
209 const vector<Property>& invalidValues = {}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -0800210
211 Property initialValue; // Save initial value to restore it at the end of the test
212 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
213 ASSERT_OK(res);
214
215 for (Property setValue : valuesToTest) {
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800216 SCOPED_TRACE("Test " + propertyName + " getter and setter for " +
217 testing::PrintToString(setValue));
Kevin Rocard3c405a72017-03-08 16:46:51 -0800218 ASSERT_OK((device.get()->*setter)(setValue));
219 Property getValue;
220 // Make sure the getter returns the same value just set
221 ASSERT_OK((device.get()->*getter)(returnIn(res, getValue)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800222 ASSERT_OK(res);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800223 EXPECT_EQ(setValue, getValue);
Kevin Rocardf0357882017-02-10 16:19:28 -0800224 }
Kevin Rocard3c405a72017-03-08 16:46:51 -0800225
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800226 for (Property invalidValue : invalidValues) {
227 SCOPED_TRACE("Try to set " + propertyName + " with the invalid value " +
228 testing::PrintToString(invalidValue));
Kevin Rocard20e7af62017-03-10 17:10:43 -0800229 EXPECT_RESULT(Result::INVALID_ARGUMENTS, (device.get()->*setter)(invalidValue));
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800230 }
231
Kevin Rocard3c405a72017-03-08 16:46:51 -0800232 ASSERT_OK((device.get()->*setter)(initialValue)); // restore initial value
233 }
234
235 /** Test the getter and setter of an optional feature. */
236 template <class Getter, class Setter>
237 void testOptionalAccessors(const string& propertyName, const vector<Property>& valuesToTest,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800238 Setter setter, Getter getter,
239 const vector<Property>& invalidValues = {}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -0800240 doc::test("Test the optional " + propertyName + " getters and setter");
241 {
242 SCOPED_TRACE("Test feature support by calling the getter");
243 Property initialValue;
244 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
245 if (res == Result::NOT_SUPPORTED) {
246 doc::partialTest(propertyName + " getter is not supported");
247 return;
248 }
249 ASSERT_OK(res); // If it is supported it must succeed
250 }
251 // The feature is supported, test it
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800252 testAccessors(propertyName, valuesToTest, setter, getter, invalidValues);
Kevin Rocardf0357882017-02-10 16:19:28 -0800253 }
254};
255
Kevin Rocard3c405a72017-03-08 16:46:51 -0800256using BoolAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<bool>;
257
Kevin Rocardf0357882017-02-10 16:19:28 -0800258TEST_F(BoolAccessorPrimaryHidlTest, MicMuteTest) {
259 doc::test("Check that the mic can be muted and unmuted");
Kevin Rocard3c405a72017-03-08 16:46:51 -0800260 testAccessors("mic mute", {true, false, true}, &IDevice::setMicMute, &IDevice::getMicMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800261 // TODO: check that the mic is really muted (all sample are 0)
262}
263
264TEST_F(BoolAccessorPrimaryHidlTest, MasterMuteTest) {
265 doc::test("If master mute is supported, try to mute and unmute the master output");
Kevin Rocard3c405a72017-03-08 16:46:51 -0800266 testOptionalAccessors("master mute", {true, false, true},
267 &IDevice::setMasterMute, &IDevice::getMasterMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800268 // TODO: check that the master volume is really muted
269}
270
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800271using FloatAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<float>;
272TEST_F(FloatAccessorPrimaryHidlTest, MasterVolumeTest) {
273 doc::test("Test the master volume if supported");
274 testOptionalAccessors("master volume", {0, 0.5, 1},
275 &IDevice::setMasterVolume, &IDevice::getMasterVolume,
276 {-0.1, 1.1, NAN, INFINITY, -INFINITY,
277 1 + std::numeric_limits<float>::epsilon()});
278 // TODO: check that the master volume is really changed
279}
280
Kevin Rocardf0357882017-02-10 16:19:28 -0800281//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800282//////////////////////////////// AudioPatches ////////////////////////////////
283//////////////////////////////////////////////////////////////////////////////
284
285class AudioPatchPrimaryHidlTest : public AudioPrimaryHidlTest {
286protected:
287 bool areAudioPatchesSupported() {
288 auto result = device->supportsAudioPatches();
289 EXPECT_TRUE(result.isOk());
290 return result;
291 }
292};
293
294TEST_F(AudioPatchPrimaryHidlTest, AudioPatches) {
295 doc::test("Test if audio patches are supported");
296 if (!areAudioPatchesSupported()) {
297 doc::partialTest("Audio patches are not supported");
298 return;
299 }
300 // TODO: test audio patches
301}
302
303
304//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800305//////////////// Required and recommended audio format support ///////////////
306// From: https://source.android.com/compatibility/android-cdd.html#5_4_audio_recording
307// From: https://source.android.com/compatibility/android-cdd.html#5_5_audio_playback
308/////////// TODO: move to the beginning of the file for easier update ////////
309//////////////////////////////////////////////////////////////////////////////
310
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800311class AudioConfigPrimaryTest : public AudioPatchPrimaryHidlTest {
Kevin Rocardf0357882017-02-10 16:19:28 -0800312public:
313 // Cache result ?
314 static const vector<AudioConfig> getRequiredSupportPlaybackAudioConfig() {
315 return combineAudioConfig({AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
316 {8000, 11025, 16000, 22050, 32000, 44100},
317 {AudioFormat::PCM_16_BIT});
318 }
319
320 static const vector<AudioConfig> getRecommendedSupportPlaybackAudioConfig() {
321 return combineAudioConfig({AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
322 {24000, 48000},
323 {AudioFormat::PCM_16_BIT});
324 }
325
326 static const vector<AudioConfig> getSupportedPlaybackAudioConfig() {
327 // TODO: retrieve audio config supported by the platform
328 // as declared in the policy configuration
329 return {};
330 }
331
332 static const vector<AudioConfig> getRequiredSupportCaptureAudioConfig() {
333 return combineAudioConfig({AudioChannelMask::IN_MONO},
334 {8000, 11025, 16000, 44100},
335 {AudioFormat::PCM_16_BIT});
336 }
337 static const vector<AudioConfig> getRecommendedSupportCaptureAudioConfig() {
338 return combineAudioConfig({AudioChannelMask::IN_STEREO},
339 {22050, 48000},
340 {AudioFormat::PCM_16_BIT});
341 }
342 static const vector<AudioConfig> getSupportedCaptureAudioConfig() {
343 // TODO: retrieve audio config supported by the platform
344 // as declared in the policy configuration
345 return {};
346 }
347private:
348 static const vector<AudioConfig> combineAudioConfig(
349 vector<AudioChannelMask> channelMasks,
350 vector<uint32_t> sampleRates,
351 vector<AudioFormat> formats) {
352 vector<AudioConfig> configs;
353 for (auto channelMask: channelMasks) {
354 for (auto sampleRate : sampleRates) {
355 for (auto format : formats) {
356 AudioConfig config{};
357 // leave offloadInfo to 0
358 config.channelMask = channelMask;
359 config.sampleRateHz = sampleRate;
360 config.format = format;
361 // FIXME: leave frameCount to 0 ?
362 configs.push_back(config);
363 }
364 }
365 }
366 return configs;
367 }
368};
369
Kevin Rocard9c369142017-03-08 17:17:25 -0800370/** Generate a test name based on an audio config.
371 *
372 * As the only parameter changing are channel mask and sample rate,
373 * only print those ones in the test name.
374 */
375static string generateTestName(const testing::TestParamInfo<AudioConfig>& info) {
376 const AudioConfig& config = info.param;
377 return to_string(info.index) + "__" + to_string(config.sampleRateHz)+ "_" +
378 // "MONO" is more clear than "FRONT_LEFT"
379 ((config.channelMask == AudioChannelMask::OUT_MONO ||
380 config.channelMask == AudioChannelMask::IN_MONO) ?
381 "MONO" : toString(config.channelMask));
382}
383
Kevin Rocardf0357882017-02-10 16:19:28 -0800384//////////////////////////////////////////////////////////////////////////////
385///////////////////////////// getInputBufferSize /////////////////////////////
386//////////////////////////////////////////////////////////////////////////////
387
388// FIXME: execute input test only if platform declares android.hardware.microphone
389// how to get this value ? is it a property ???
390
391class AudioCaptureConfigPrimaryTest : public AudioConfigPrimaryTest,
392 public ::testing::WithParamInterface<AudioConfig> {
393protected:
394 void inputBufferSizeTest(const AudioConfig& audioConfig, bool supportRequired) {
395 uint64_t bufferSize;
396 ASSERT_OK(device->getInputBufferSize(audioConfig, returnIn(res, bufferSize)));
397
398 switch (res) {
399 case Result::INVALID_ARGUMENTS:
400 EXPECT_FALSE(supportRequired);
401 break;
402 case Result::OK:
403 // Check that the buffer is of a sane size
404 // For now only that it is > 0
405 EXPECT_GT(bufferSize, uint64_t(0));
406 break;
407 default:
408 FAIL() << "Invalid return status: " << ::testing::PrintToString(res);
409 }
410 }
411};
412
413// Test that the required capture config and those declared in the policy are indeed supported
414class RequiredInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
415TEST_P(RequiredInputBufferSizeTest, RequiredInputBufferSizeTest) {
416 doc::test("Input buffer size must be retrievable for a format with required support.");
417 inputBufferSizeTest(GetParam(), true);
418}
419INSTANTIATE_TEST_CASE_P(
420 RequiredInputBufferSize, RequiredInputBufferSizeTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800421 ::testing::ValuesIn(AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
422 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800423INSTANTIATE_TEST_CASE_P(
424 SupportedInputBufferSize, RequiredInputBufferSizeTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800425 ::testing::ValuesIn(AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
426 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800427
428// Test that the recommended capture config are supported or lead to a INVALID_ARGUMENTS return
429class OptionalInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
430TEST_P(OptionalInputBufferSizeTest, OptionalInputBufferSizeTest) {
431 doc::test("Input buffer size should be retrievable for a format with recommended support.");
432 inputBufferSizeTest(GetParam(), false);
433}
434INSTANTIATE_TEST_CASE_P(
435 RecommendedCaptureAudioConfigSupport, OptionalInputBufferSizeTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800436 ::testing::ValuesIn(AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
437 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800438
439//////////////////////////////////////////////////////////////////////////////
440/////////////////////////////// setScreenState ///////////////////////////////
441//////////////////////////////////////////////////////////////////////////////
442
443TEST_F(AudioPrimaryHidlTest, setScreenState) {
444 doc::test("Check that the hal can receive the screen state");
445 for (bool turnedOn : {false, true, true, false, false}) {
446 auto ret = device->setScreenState(turnedOn);
447 ASSERT_TRUE(ret.isOk());
448 Result result = ret;
449 ASSERT_TRUE(result == Result::OK || result == Result::NOT_SUPPORTED) << toString(result);
450 }
451}
452
453//////////////////////////////////////////////////////////////////////////////
454//////////////////////////// {get,set}Parameters /////////////////////////////
455//////////////////////////////////////////////////////////////////////////////
456
457TEST_F(AudioPrimaryHidlTest, getParameters) {
458 doc::test("Check that the hal can set and get parameters");
459 hidl_vec<hidl_string> keys;
460 hidl_vec<ParameterValue> values;
461 ASSERT_OK(device->getParameters(keys, returnIn(res, values)));
462 ASSERT_OK(device->setParameters(values));
463 values.resize(0);
464 ASSERT_OK(device->setParameters(values));
465}
466
467//////////////////////////////////////////////////////////////////////////////
468//////////////////////////////// debugDebug //////////////////////////////////
469//////////////////////////////////////////////////////////////////////////////
470
Kevin Rocardb9031242017-03-13 12:20:54 -0700471template <class DebugDump>
472static void testDebugDump(DebugDump debugDump) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800473 FILE* file = tmpfile();
474 ASSERT_NE(nullptr, file) << errno;
475
476 auto* nativeHandle = native_handle_create(1, 0);
477 ASSERT_NE(nullptr, nativeHandle);
478 nativeHandle->data[0] = fileno(file);
479
480 hidl_handle handle;
481 handle.setTo(nativeHandle, true /*take ownership*/);
482
Kevin Rocardee771e92017-03-08 17:17:25 -0800483 // TODO: debugDump does not return a Result.
Kevin Rocardf0357882017-02-10 16:19:28 -0800484 // This mean that the hal can not report that it not implementing the function.
Kevin Rocardb9031242017-03-13 12:20:54 -0700485 ASSERT_OK(debugDump(handle));
Kevin Rocardee771e92017-03-08 17:17:25 -0800486
487 rewind(file); // can not fail
Kevin Rocardf0357882017-02-10 16:19:28 -0800488
489 // Check that at least one bit was written by the hal
490 char buff;
Kevin Rocardee771e92017-03-08 17:17:25 -0800491 ASSERT_EQ(size_t{1}, fread(&buff, sizeof(buff), 1, file));
Kevin Rocardf0357882017-02-10 16:19:28 -0800492 EXPECT_EQ(0, fclose(file)) << errno;
493}
494
Kevin Rocardb9031242017-03-13 12:20:54 -0700495TEST_F(AudioPrimaryHidlTest, debugDump) {
496 doc::test("Check that the hal can dump its state without error");
497 testDebugDump([this](const auto& handle){ return device->debugDump(handle); });
498}
499
Kevin Rocardf0357882017-02-10 16:19:28 -0800500//////////////////////////////////////////////////////////////////////////////
501////////////////////////// open{Output,Input}Stream //////////////////////////
502//////////////////////////////////////////////////////////////////////////////
503
504template <class Stream>
505class OpenStreamTest : public AudioConfigPrimaryTest,
506 public ::testing::WithParamInterface<AudioConfig> {
507protected:
508 template <class Open>
509 void testOpen(Open openStream, const AudioConfig& config) {
510 // FIXME: Open a stream without an IOHandle
511 // This is not required to be accepted by hal implementations
512 AudioIoHandle ioHandle = (AudioIoHandle)AudioHandleConsts::AUDIO_IO_HANDLE_NONE;
513 AudioConfig suggestedConfig{};
514 ASSERT_OK(openStream(ioHandle, config, returnIn(res, stream, suggestedConfig)));
515
516 // TODO: only allow failure for RecommendedPlaybackAudioConfig
517 switch (res) {
518 case Result::OK:
519 ASSERT_TRUE(stream != nullptr);
520 audioConfig = config;
521 break;
522 case Result::INVALID_ARGUMENTS:
523 ASSERT_TRUE(stream == nullptr);
524 AudioConfig suggestedConfigRetry;
525 // Could not open stream with config, try again with the suggested one
526 ASSERT_OK(openStream(ioHandle, suggestedConfig,
527 returnIn(res, stream, suggestedConfigRetry)));
528 // This time it must succeed
529 ASSERT_OK(res);
530 ASSERT_TRUE(stream == nullptr);
531 audioConfig = suggestedConfig;
532 break;
533 default:
534 FAIL() << "Invalid return status: " << ::testing::PrintToString(res);
535 }
536 open = true;
537 }
538
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800539 Return<Result> closeStream() {
540 open = false;
541 return stream->close();
542 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800543private:
544 void TearDown() override {
545 if (open) {
546 ASSERT_OK(stream->close());
547 }
548 }
549
550protected:
551
552 AudioConfig audioConfig;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800553 DeviceAddress address = {};
Kevin Rocardf0357882017-02-10 16:19:28 -0800554 sp<Stream> stream;
555 bool open = false;
556};
557
558////////////////////////////// openOutputStream //////////////////////////////
559
560class OutputStreamTest : public OpenStreamTest<IStreamOut> {
561 virtual void SetUp() override {
562 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800563 address.device = AudioDevice::OUT_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800564 const AudioConfig& config = GetParam();
Kevin Rocardf0357882017-02-10 16:19:28 -0800565 AudioOutputFlag flags = AudioOutputFlag::NONE; // TODO: test all flag combination
566 testOpen([&](AudioIoHandle handle, AudioConfig config, auto cb)
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800567 { return device->openOutputStream(handle, address, config, flags, cb); },
Kevin Rocardf0357882017-02-10 16:19:28 -0800568 config);
569 }
570};
571TEST_P(OutputStreamTest, OpenOutputStreamTest) {
572 doc::test("Check that output streams can be open with the required and recommended config");
573 // Open done in SetUp
574}
575INSTANTIATE_TEST_CASE_P(
576 RequiredOutputStreamConfigSupport, OutputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800577 ::testing::ValuesIn(AudioConfigPrimaryTest::getRequiredSupportPlaybackAudioConfig()),
578 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800579INSTANTIATE_TEST_CASE_P(
580 SupportedOutputStreamConfig, OutputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800581 ::testing::ValuesIn(AudioConfigPrimaryTest::getSupportedPlaybackAudioConfig()),
582 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800583
584INSTANTIATE_TEST_CASE_P(
585 RecommendedOutputStreamConfigSupport, OutputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800586 ::testing::ValuesIn(AudioConfigPrimaryTest::getRecommendedSupportPlaybackAudioConfig()),
587 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800588
589////////////////////////////// openInputStream //////////////////////////////
590
591class InputStreamTest : public OpenStreamTest<IStreamIn> {
592
593 virtual void SetUp() override {
594 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800595 address.device = AudioDevice::IN_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800596 const AudioConfig& config = GetParam();
Kevin Rocardf0357882017-02-10 16:19:28 -0800597 AudioInputFlag flags = AudioInputFlag::NONE; // TODO: test all flag combination
598 AudioSource source = AudioSource::DEFAULT; // TODO: test all flag combination
599 testOpen([&](AudioIoHandle handle, AudioConfig config, auto cb)
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800600 { return device->openInputStream(handle, address, config, flags, source, cb); },
Kevin Rocardf0357882017-02-10 16:19:28 -0800601 config);
602 }
603};
604
605TEST_P(InputStreamTest, OpenInputStreamTest) {
606 doc::test("Check that input streams can be open with the required and recommended config");
607 // Open done in setup
608}
609INSTANTIATE_TEST_CASE_P(
610 RequiredInputStreamConfigSupport, InputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800611 ::testing::ValuesIn(AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
612 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800613INSTANTIATE_TEST_CASE_P(
614 SupportedInputStreamConfig, InputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800615 ::testing::ValuesIn(AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
616 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800617
618INSTANTIATE_TEST_CASE_P(
619 RecommendedInputStreamConfigSupport, InputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800620 ::testing::ValuesIn(AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
621 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800622
623//////////////////////////////////////////////////////////////////////////////
624////////////////////////////// IStream getters ///////////////////////////////
625//////////////////////////////////////////////////////////////////////////////
626
627/** Unpack the provided result.
628 * If the result is not OK, register a failure and return an undefined value. */
629template <class R>
630static R extract(Return<R> ret) {
631 if (!ret.isOk()) {
632 ADD_FAILURE();
633 return R{};
634 }
635 return ret;
636}
637
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800638/* Could not find a way to write a test for two parametrized class fixure
639 * thus use this macro do duplicate tests for Input and Output stream */
640#define TEST_IO_STREAM(test_name, documentation, code) \
641 TEST_P(InputStreamTest, test_name) { \
642 doc::test(documentation); \
643 code; \
644 } \
645 TEST_P(OutputStreamTest, test_name) { \
646 doc::test(documentation); \
647 code; \
648 }
649
650TEST_IO_STREAM(GetFrameCount, "Check that the stream frame count == the one it was opened with",
651 ASSERT_EQ(audioConfig.frameCount, extract(stream->getFrameCount())))
652
653TEST_IO_STREAM(GetSampleRate, "Check that the stream sample rate == the one it was opened with",
654 ASSERT_EQ(audioConfig.sampleRateHz, extract(stream->getSampleRate())))
655
656TEST_IO_STREAM(GetChannelMask, "Check that the stream channel mask == the one it was opened with",
657 ASSERT_EQ(audioConfig.channelMask, extract(stream->getChannelMask())))
658
659TEST_IO_STREAM(GetFormat, "Check that the stream format == the one it was opened with",
660 ASSERT_EQ(audioConfig.format, extract(stream->getFormat())))
661
662// TODO: for now only check that the framesize is not incoherent
663TEST_IO_STREAM(GetFrameSize, "Check that the stream frame size == the one it was opened with",
664 ASSERT_GT(extract(stream->getFrameSize()), 0U))
665
666TEST_IO_STREAM(GetBufferSize, "Check that the stream buffer size== the one it was opened with",
667 ASSERT_GE(extract(stream->getBufferSize()), \
668 extract(stream->getFrameSize())));
669
Kevin Rocardf0357882017-02-10 16:19:28 -0800670template <class Property, class CapabilityGetter, class Getter, class Setter>
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800671static void testCapabilityGetter(const string& name, IStream* stream, Property currentValue,
Kevin Rocardf0357882017-02-10 16:19:28 -0800672 CapabilityGetter capablityGetter, Getter getter, Setter setter) {
673 hidl_vec<Property> capabilities;
674 ASSERT_OK((stream->*capablityGetter)(returnIn(capabilities)));
675 if (capabilities.size() == 0) {
676 // The default hal should probably return a NOT_SUPPORTED if the hal does not expose
677 // capability retrieval. For now it returns an empty list if not implemented
678 doc::partialTest(name + " is not supported");
679 return;
680 };
681 // TODO: This code has never been tested on a hal that supports getSupportedSampleRates
682 EXPECT_NE(std::find(capabilities.begin(), capabilities.end(), currentValue),
683 capabilities.end())
684 << "current " << name << " is not in the list of the supported ones "
685 << toString(capabilities);
686
687 // Check that all declared supported values are indeed supported
688 for (auto capability : capabilities) {
689 ASSERT_OK((stream->*setter)(capability));
690 ASSERT_EQ(capability, extract((stream->*getter)()));
691 }
692}
693
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800694TEST_IO_STREAM(SupportedSampleRate, "Check that the stream sample rate is declared as supported",
695 testCapabilityGetter("getSupportedSampleRate", stream.get(), \
696 extract(stream->getSampleRate()), \
697 &IStream::getSupportedSampleRates, \
698 &IStream::getSampleRate, &IStream::setSampleRate))
699
700TEST_IO_STREAM(SupportedChannelMask, "Check that the stream channel mask is declared as supported",
701 testCapabilityGetter("getSupportedChannelMask", stream.get(), \
702 extract(stream->getChannelMask()), \
703 &IStream::getSupportedChannelMasks, \
704 &IStream::getChannelMask, &IStream::setChannelMask))
705
706TEST_IO_STREAM(SupportedFormat, "Check that the stream format is declared as supported",
707 testCapabilityGetter("getSupportedFormat", stream.get(), \
708 extract(stream->getFormat()), \
709 &IStream::getSupportedFormats, \
710 &IStream::getFormat, &IStream::setFormat))
711
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800712static void testGetDevice(IStream* stream, AudioDevice expectedDevice) {
713 auto ret = stream->getDevice();
714 ASSERT_TRUE(ret.isOk());
715 AudioDevice device = ret;
716 ASSERT_EQ(expectedDevice, device);
717}
718
719TEST_IO_STREAM(GetDevice, "Check that the stream device == the one it was opened with",
720 areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported") : \
721 testGetDevice(stream.get(), address.device))
722
723static void testSetDevice(IStream* stream, const DeviceAddress& address) {
724 DeviceAddress otherAddress = address;
725 otherAddress.device = (address.device & AudioDevice::BIT_IN) == 0 ?
726 AudioDevice::OUT_SPEAKER : AudioDevice::IN_BUILTIN_MIC;
727 EXPECT_OK(stream->setDevice(otherAddress));
728
729 ASSERT_OK(stream->setDevice(address)); // Go back to the original value
730}
731
732TEST_IO_STREAM(SetDevice, "Check that the stream can be rerouted to SPEAKER or BUILTIN_MIC",
733 areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported") : \
734 testSetDevice(stream.get(), address))
735
Kevin Rocardf0357882017-02-10 16:19:28 -0800736static void testGetAudioProperties(IStream* stream, AudioConfig expectedConfig) {
737 uint32_t sampleRateHz;
738 AudioChannelMask mask;
739 AudioFormat format;
740
741 stream->getAudioProperties(returnIn(sampleRateHz, mask, format));
742
743 // FIXME: the qcom hal it does not currently negotiate the sampleRate & channel mask
Kevin Rocardd1e98ae2017-03-08 17:17:25 -0800744 EXPECT_EQ(expectedConfig.sampleRateHz, sampleRateHz);
745 EXPECT_EQ(expectedConfig.channelMask, mask);
Kevin Rocardf0357882017-02-10 16:19:28 -0800746 EXPECT_EQ(expectedConfig.format, format);
747}
748
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800749TEST_IO_STREAM(GetAudioProperties,
750 "Check that the stream audio properties == the ones it was opened with",
751 testGetAudioProperties(stream.get(), audioConfig))
Kevin Rocardf0357882017-02-10 16:19:28 -0800752
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800753static void testConnectedState(IStream* stream) {
754 DeviceAddress address = {};
755 using AD = AudioDevice;
756 for (auto device : {AD::OUT_HDMI, AD::OUT_WIRED_HEADPHONE, AD::IN_USB_HEADSET}) {
757 address.device = device;
758
759 ASSERT_OK(stream->setConnectedState(address, true));
760 ASSERT_OK(stream->setConnectedState(address, false));
761 }
762}
763TEST_IO_STREAM(SetConnectedState,
764 "Check that the stream can be notified of device connection and deconnection",
765 testConnectedState(stream.get()))
766
767
768static auto invalidArgsOrNotSupported = {Result::INVALID_ARGUMENTS, Result::NOT_SUPPORTED};
769TEST_IO_STREAM(SetHwAvSync, "Try to set hardware sync to an invalid value",
770 ASSERT_RESULT(invalidArgsOrNotSupported, stream->setHwAvSync(666)))
771
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700772TEST_IO_STREAM(GetHwAvSync, "Get hardware sync can not fail",
773 ASSERT_TRUE(device->getHwAvSync().isOk()))
774
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800775static void checkGetParameter(IStream* stream, hidl_vec<hidl_string> keys,
776 vector<Result> expectedResults) {
777 hidl_vec<ParameterValue> parameters;
778 Result res;
779 ASSERT_OK(stream->getParameters(keys, returnIn(res, parameters)));
780 ASSERT_RESULT(expectedResults, res);
781 if (res == Result::OK) {
782 ASSERT_EQ(0U, parameters.size());
783 }
784}
785
786/* Get/Set parameter is intended to be an opaque channel between vendors app and their HALs.
787 * Thus can not be meaningfully tested.
788 * TODO: Doc missing. Should asking for an empty set of params raise an error ?
789 */
790TEST_IO_STREAM(getEmptySetParameter, "Retrieve the values of an empty set",
791 checkGetParameter(stream.get(), {} /* keys */,
792 {Result::OK, Result::INVALID_ARGUMENTS}))
793
794
795TEST_IO_STREAM(getNonExistingParameter, "Retrieve the values of an non existing parameter",
796 checkGetParameter(stream.get(), {"Non existing key"} /* keys */,
797 {Result::INVALID_ARGUMENTS}))
798
Kevin Rocard624800c2017-03-10 18:47:37 -0800799static vector<Result> okOrInvalidArguments = {Result::OK, Result::INVALID_ARGUMENTS};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800800TEST_IO_STREAM(setEmptySetParameter, "Set the values of an empty set of parameters",
Kevin Rocard624800c2017-03-10 18:47:37 -0800801 ASSERT_RESULT(okOrInvalidArguments, stream->setParameters({})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800802
803TEST_IO_STREAM(setNonExistingParameter, "Set the values of an non existing parameter",
804 ASSERT_RESULT(Result::INVALID_ARGUMENTS,
805 stream->setParameters({{"non existing key", "0"}})))
806
Kevin Rocardb9031242017-03-13 12:20:54 -0700807TEST_IO_STREAM(DebugDump,
808 "Check that a stream can dump its state without error",
809 testDebugDump([this](const auto& handle){ return stream->debugDump(handle); }))
810
Kevin Rocardf0357882017-02-10 16:19:28 -0800811//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800812////////////////////////////// addRemoveEffect ///////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800813//////////////////////////////////////////////////////////////////////////////
814
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800815TEST_IO_STREAM(AddNonExistingEffect, "Adding a non existing effect should fail",
816 ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->addEffect(666)))
817TEST_IO_STREAM(RemoveNonExistingEffect, "Removing a non existing effect should fail",
818 ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->removeEffect(666)))
Kevin Rocardf0357882017-02-10 16:19:28 -0800819
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800820//TODO: positive tests
821
822//////////////////////////////////////////////////////////////////////////////
823/////////////////////////////// Control ////////////////////////////////
824//////////////////////////////////////////////////////////////////////////////
825
826TEST_IO_STREAM(standby, "Make sure the stream can be put in stanby",
827 ASSERT_OK(stream->standby())) // can not fail
828
829static vector<Result> invalidStateOrNotSupported = {Result::INVALID_STATE, Result::NOT_SUPPORTED};
830
831TEST_IO_STREAM(startNoMmap, "Starting a mmaped stream before mapping it should fail",
832 ASSERT_RESULT(invalidStateOrNotSupported, stream->start()))
833
834TEST_IO_STREAM(stopNoMmap, "Stopping a mmaped stream before mapping it should fail",
835 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
836
837TEST_IO_STREAM(getMmapPositionNoMmap, "Get a stream Mmap position before mapping it should fail",
838 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
839
840TEST_IO_STREAM(close, "Make sure a stream can be closed", ASSERT_OK(closeStream()))
841TEST_IO_STREAM(closeTwice, "Make sure a stream can not be closed twice",
842 ASSERT_OK(closeStream()); \
843 ASSERT_RESULT(Result::INVALID_STATE, closeStream()))
844
845static void testCreateTooBigMmapBuffer(IStream* stream) {
846 MmapBufferInfo info;
847 Result res;
848 // Assume that int max is a value too big to be allocated
849 // This is true currently with a 32bit media server, but might not when it will run in 64 bit
850 auto minSizeFrames = std::numeric_limits<int32_t>::max();
851 ASSERT_OK(stream->createMmapBuffer(minSizeFrames, returnIn(res, info)));
852 ASSERT_RESULT(invalidArgsOrNotSupported, res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800853}
854
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800855TEST_IO_STREAM(CreateTooBigMmapBuffer, "Create mmap buffer too big should fail",
856 testCreateTooBigMmapBuffer(stream.get()))
857
858
859static void testGetMmapPositionOfNonMmapedStream(IStream* stream) {
860 Result res;
861 MmapPosition position;
862 ASSERT_OK(stream->getMmapPosition(returnIn(res, position)));
863 ASSERT_RESULT(invalidArgsOrNotSupported, res);
864}
865
866TEST_IO_STREAM(GetMmapPositionOfNonMmapedStream,
867 "Retrieving the mmap position of a non mmaped stream should fail",
868 testGetMmapPositionOfNonMmapedStream(stream.get()))
869
Kevin Rocardf0357882017-02-10 16:19:28 -0800870//////////////////////////////////////////////////////////////////////////////
Kevin Rocardc9963522017-03-10 18:47:37 -0800871///////////////////////////////// StreamIn ///////////////////////////////////
872//////////////////////////////////////////////////////////////////////////////
873
874TEST_P(InputStreamTest, GetAudioSource) {
875 doc::test("Retrieving the audio source of an input stream should always succeed");
876 AudioSource source;
877 ASSERT_OK(stream->getAudioSource(returnIn(res, source)));
878 ASSERT_OK(res);
879 ASSERT_EQ(AudioSource::DEFAULT, source);
880}
881
882static void testUnitaryGain(std::function<Return<Result> (float)> setGain) {
883 for (float value : {0.0, 0.01, 0.5, 0.09, 1.0}) {
884 SCOPED_TRACE("value=" + to_string(value));
885 ASSERT_OK(setGain(value));
886 }
887 for (float value : (float[]){-INFINITY,-1.0, -0.0,
888 1.0 + std::numeric_limits<float>::epsilon(), 2.0, INFINITY,
889 NAN}) {
890 SCOPED_TRACE("value=" + to_string(value));
891 // FIXME: NAN should never be accepted
892 // FIXME: Missing api doc. What should the impl do if the volume is outside [0,1] ?
893 ASSERT_RESULT(Result::INVALID_ARGUMENTS, setGain(value));
894 }
895}
896
897TEST_P(InputStreamTest, SetGain) {
898 doc::test("The gain of an input stream should only be set between [0,1]");
899 testUnitaryGain([this](float volume) { return stream->setGain(volume); });
900}
901
902static void testPrepareForReading(IStreamIn* stream, uint32_t frameSize, uint32_t framesCount) {
903 Result res;
904 // Ignore output parameters as the call should fail
905 ASSERT_OK(stream->prepareForReading(frameSize, framesCount,
906 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
907 EXPECT_RESULT(invalidArgsOrNotSupported, res);
908}
909
910TEST_P(InputStreamTest, PrepareForReadingWithHugeBuffer) {
911 doc::test("Preparing a stream for reading with a 2^32 sized buffer should fail");
912 testPrepareForReading(stream.get(), 1, std::numeric_limits<uint32_t>::max());
913}
914
915TEST_P(InputStreamTest, PrepareForReadingCheckOverflow) {
916 doc::test("Preparing a stream for reading with a overflowing sized buffer should fail");
917 auto uintMax = std::numeric_limits<uint32_t>::max();
918 testPrepareForReading(stream.get(), uintMax, uintMax);
919}
920
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700921TEST_P(InputStreamTest, GetInputFramesLost) {
922 doc::test("The number of frames lost on a never started stream should be 0");
923 auto ret = stream->getInputFramesLost();
924 ASSERT_TRUE(ret.isOk());
925 uint32_t framesLost{ret};
926 ASSERT_EQ(0U, framesLost);
927}
928
Kevin Rocardc9963522017-03-10 18:47:37 -0800929TEST_P(InputStreamTest, getCapturePosition) {
930 doc::test("The capture position of a non prepared stream should not be retrievable");
931 uint64_t frames;
932 uint64_t time;
933 ASSERT_OK(stream->getCapturePosition(returnIn(res, frames, time)));
934 ASSERT_RESULT(invalidStateOrNotSupported, res);
935}
936
937//////////////////////////////////////////////////////////////////////////////
Kevin Rocard624800c2017-03-10 18:47:37 -0800938///////////////////////////////// StreamIn ///////////////////////////////////
939//////////////////////////////////////////////////////////////////////////////
940
941TEST_P(OutputStreamTest, getLatency) {
942 doc::test("Make sure latency is over 0");
943 auto result = stream->getLatency();
944 ASSERT_TRUE(result.isOk());
945 ASSERT_GT(result, 0U);
946}
947
948TEST_P(OutputStreamTest, setVolume) {
949 doc::test("Try to set the output volume");
950 auto result = stream->setVolume(1, 1);
951 ASSERT_TRUE(result.isOk());
952 if (result == Result::NOT_SUPPORTED) {
953 doc::partialTest("setVolume is not supported");
954 return;
955 }
956 testUnitaryGain([this](float volume) { return stream->setVolume(volume, volume); });
957}
958
959static void testPrepareForWriting(IStreamOut* stream, uint32_t frameSize, uint32_t framesCount) {
960 Result res;
961 // Ignore output parameters as the call should fail
962 ASSERT_OK(stream->prepareForWriting(frameSize, framesCount,
963 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
964 EXPECT_RESULT(invalidArgsOrNotSupported, res);
965}
966
967TEST_P(OutputStreamTest, PrepareForWriteWithHugeBuffer) {
968 doc::test("Preparing a stream for writing with a 2^32 sized buffer should fail");
969 testPrepareForWriting(stream.get(), 1, std::numeric_limits<uint32_t>::max());
970}
971
972TEST_P(OutputStreamTest, PrepareForWritingCheckOverflow) {
973 doc::test("Preparing a stream for writing with a overflowing sized buffer should fail");
974 auto uintMax = std::numeric_limits<uint32_t>::max();
975 testPrepareForWriting(stream.get(), uintMax, uintMax);
976}
977
978struct Capability {
979 Capability(IStreamOut* stream) {
980 EXPECT_OK(stream->supportsPauseAndResume(returnIn(pause, resume)));
981 auto ret = stream->supportsDrain();
982 EXPECT_TRUE(ret.isOk());
983 if (ret.isOk()) {
984 drain = ret;
985 }
986 }
987 bool pause = false;
988 bool resume = false;
989 bool drain = false;
990};
991
992TEST_P(OutputStreamTest, SupportsPauseAndResumeAndDrain) {
993 Capability(stream.get());
994}
995
996TEST_P(OutputStreamTest, GetRenderPosition) {
997 uint32_t dspFrames;
998 ASSERT_OK(stream->getRenderPosition(returnIn(res, dspFrames)));
999 if (res == Result::NOT_SUPPORTED) {
1000 doc::partialTest("getRenderPosition is not supported");
1001 return;
1002 }
1003 ASSERT_OK(res);
1004 ASSERT_EQ(0U, dspFrames);
1005}
1006
1007TEST_P(OutputStreamTest, GetNextWriteTimestamp) {
1008 uint64_t timestampUs;
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001009 ASSERT_OK(stream->getNextWriteTimestamp(returnIn(res, timestampUs)));
Kevin Rocard624800c2017-03-10 18:47:37 -08001010 if (res == Result::NOT_SUPPORTED) {
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001011 doc::partialTest("getNextWriteTimestamp is not supported");
Kevin Rocard624800c2017-03-10 18:47:37 -08001012 return;
1013 }
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001014 ASSERT_EQ(Result::INVALID_STATE, res);
Kevin Rocard624800c2017-03-10 18:47:37 -08001015}
1016
1017/** Stub implementation of out stream callback. */
1018class MockOutCallbacks : public IStreamOutCallback {
1019 Return<void> onWriteReady() override { return {}; }
1020 Return<void> onDrainReady() override { return {}; }
1021 Return<void> onError() override { return {}; }
1022};
1023
1024static bool isAsyncModeSupported(IStreamOut *stream) {
1025 auto res = stream->setCallback(new MockOutCallbacks);
1026 stream->clearCallback(); // try to restore the no callback state, ignore any error
1027 auto okOrNotSupported = { Result::OK, Result::NOT_SUPPORTED };
1028 EXPECT_RESULT(okOrNotSupported, res);
1029 return res.isOk() ? res == Result::OK : false;
1030}
1031
1032TEST_P(OutputStreamTest, SetCallback) {
1033 if (!isAsyncModeSupported(stream.get())) {
1034 doc::partialTest("The stream does not support async operations");
1035 return;
1036 }
1037 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1038 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1039}
1040
1041TEST_P(OutputStreamTest, clearCallback) {
1042 if (!isAsyncModeSupported(stream.get())) {
1043 doc::partialTest("The stream does not support async operations");
1044 return;
1045 }
1046 // TODO: Clarify if clearing a non existing callback should fail
1047 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1048 ASSERT_OK(stream->clearCallback());
1049}
1050
1051TEST_P(OutputStreamTest, Resume) {
1052 if (!Capability(stream.get()).resume) {
1053 doc::partialTest("The output stream does not support resume");
1054 return;
1055 }
1056 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1057}
1058
1059TEST_P(OutputStreamTest, Pause) {
1060 if (!Capability(stream.get()).pause) {
1061 doc::partialTest("The output stream does not support pause");
1062 return;
1063 }
1064 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1065}
1066
1067static void testDrain(IStreamOut *stream, AudioDrain type) {
1068 if (!Capability(stream).drain) {
1069 doc::partialTest("The output stream does not support pause");
1070 return;
1071 }
1072 ASSERT_RESULT(Result::OK, stream->drain(type));
1073}
1074
1075TEST_P(OutputStreamTest, DrainAll) {
1076 testDrain(stream.get(), AudioDrain::ALL);
1077}
1078
1079TEST_P(OutputStreamTest, DrainEarlyNotify) {
1080 testDrain(stream.get(), AudioDrain::EARLY_NOTIFY);
1081}
1082
1083TEST_P(OutputStreamTest, FlushStop) {
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001084 auto ret = stream->flush();
1085 ASSERT_TRUE(ret.isOk());
1086 if (ret == Result::NOT_SUPPORTED) {
1087 doc::partialTest("Flush is not supported");
1088 return;
1089 }
1090 ASSERT_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001091}
1092
1093TEST_P(OutputStreamTest, GetPresentationPositionStop) {
1094 uint64_t frames;
1095 TimeSpec mesureTS;
1096 ASSERT_OK(stream->getPresentationPosition(returnIn(res, frames, mesureTS)));
1097 if (res == Result::NOT_SUPPORTED) {
1098 doc::partialTest("getpresentationPosition is not supported");
1099 return;
1100 }
1101 ASSERT_EQ(0U, frames);
1102
1103 struct timespec currentTS;
1104 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currentTS)) << errno;
1105
1106 auto toMicroSec = [](uint64_t sec, auto nsec) { return sec * 1e+6 + nsec / 1e+3; };
1107 auto currentTime = toMicroSec(currentTS.tv_sec, currentTS.tv_nsec);
1108 auto mesureTime = toMicroSec(mesureTS.tvSec, mesureTS.tvNSec);
1109 ASSERT_PRED2([](auto c, auto m){ return c - m < 1e+6; }, currentTime, mesureTime);
1110}
1111
1112
1113//////////////////////////////////////////////////////////////////////////////
Kevin Rocard3c405a72017-03-08 16:46:51 -08001114/////////////////////////////// PrimaryDevice ////////////////////////////////
1115//////////////////////////////////////////////////////////////////////////////
1116
Kevin Rocardc9963522017-03-10 18:47:37 -08001117
Kevin Rocard3c405a72017-03-08 16:46:51 -08001118TEST_F(AudioPrimaryHidlTest, setVoiceVolume) {
1119 doc::test("Make sure setVoiceVolume only succeed if volume is in [0,1]");
Kevin Rocardc9963522017-03-10 18:47:37 -08001120 testUnitaryGain([this](float volume) { return device->setVoiceVolume(volume); });
Kevin Rocard3c405a72017-03-08 16:46:51 -08001121}
1122
1123TEST_F(AudioPrimaryHidlTest, setMode) {
1124 doc::test("Make sure setMode always succeeds if mode is valid");
1125 for (AudioMode mode : {AudioMode::IN_CALL, AudioMode::IN_COMMUNICATION,
1126 AudioMode::RINGTONE, AudioMode::CURRENT,
1127 AudioMode::NORMAL /* Make sure to leave the test in normal mode */ }) {
1128 SCOPED_TRACE("mode=" + toString(mode));
1129 ASSERT_OK(device->setMode(mode));
1130 }
1131
1132 // FIXME: Missing api doc. What should the impl do if the mode is invalid ?
Kevin Rocard20e7af62017-03-10 17:10:43 -08001133 ASSERT_RESULT(Result::INVALID_ARGUMENTS, device->setMode(AudioMode::INVALID));
Kevin Rocard3c405a72017-03-08 16:46:51 -08001134}
1135
1136
1137TEST_F(BoolAccessorPrimaryHidlTest, BtScoNrecEnabled) {
1138 doc::test("Query and set the BT SCO NR&EC state");
1139 testOptionalAccessors("BtScoNrecEnabled", {true, false, true},
1140 &IPrimaryDevice::setBtScoNrecEnabled,
1141 &IPrimaryDevice::getBtScoNrecEnabled);
1142}
1143
1144TEST_F(BoolAccessorPrimaryHidlTest, setGetBtScoWidebandEnabled) {
1145 doc::test("Query and set the SCO whideband state");
1146 testOptionalAccessors("BtScoWideband", {true, false, true},
1147 &IPrimaryDevice::setBtScoWidebandEnabled,
1148 &IPrimaryDevice::getBtScoWidebandEnabled);
1149}
1150
1151using TtyModeAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<TtyMode>;
1152TEST_F(TtyModeAccessorPrimaryHidlTest, setGetTtyMode) {
1153 doc::test("Query and set the TTY mode state");
1154 testOptionalAccessors("TTY mode", {TtyMode::OFF, TtyMode::HCO, TtyMode::VCO, TtyMode::FULL},
1155 &IPrimaryDevice::setTtyMode, &IPrimaryDevice::getTtyMode);
1156}
1157
1158TEST_F(BoolAccessorPrimaryHidlTest, setGetHac) {
1159 doc::test("Query and set the HAC state");
1160 testAccessors("HAC", {true, false, true},
1161 &IPrimaryDevice::setHacEnabled,
1162 &IPrimaryDevice::getHacEnabled);
1163}
1164
1165//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -08001166//////////////////// Clean caches on global tear down ////////////////////////
1167//////////////////////////////////////////////////////////////////////////////
1168
1169int main(int argc, char** argv) {
1170 environment = new Environment;
1171 ::testing::AddGlobalTestEnvironment(environment);
1172 ::testing::InitGoogleTest(&argc, argv);
1173 int status = RUN_ALL_TESTS();
1174 LOG(INFO) << "Test result = " << status;
1175 return status;
1176}