blob: 074903f66534a1180feb6b385133e1be51496392 [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
Mikhail Naganov8604a732017-04-24 09:29:22 -0700158TEST_F(AudioHidlTest, OpenDeviceInvalidParameter) {
159 doc::test("test passing an invalid parameter to openDevice");
160 IDevicesFactory::Result result;
161 sp<IDevice> device;
162 ASSERT_OK(devicesFactory->openDevice(IDevicesFactory::Device(-1),
163 returnIn(result, device)));
164 ASSERT_EQ(IDevicesFactory::Result::INVALID_ARGUMENTS, result);
165 ASSERT_TRUE(device == nullptr);
166}
167
Kevin Rocardf0357882017-02-10 16:19:28 -0800168//////////////////////////////////////////////////////////////////////////////
169/////////////////////////////// openDevice primary ///////////////////////////
170//////////////////////////////////////////////////////////////////////////////
171
172// Test the primary device
173class AudioPrimaryHidlTest : public AudioHidlTest {
174public:
175 /** Primary HAL test are NOT thread safe. */
176 void SetUp() override {
177 ASSERT_NO_FATAL_FAILURE(AudioHidlTest::SetUp()); // setup base
178
179 if (device == nullptr) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800180 IDevicesFactory::Result result;
Kevin Rocard3c405a72017-03-08 16:46:51 -0800181 sp<IDevice> baseDevice;
Kevin Rocardf0357882017-02-10 16:19:28 -0800182 ASSERT_OK(devicesFactory->openDevice(IDevicesFactory::Device::PRIMARY,
Kevin Rocard3c405a72017-03-08 16:46:51 -0800183 returnIn(result, baseDevice)));
Kevin Rocardfba442a2017-03-31 19:34:41 -0700184 ASSERT_OK(result);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800185 ASSERT_TRUE(baseDevice != nullptr);
186
187 environment->registerTearDown([]{ device.clear(); });
188 device = IPrimaryDevice::castFrom(baseDevice);
189 ASSERT_TRUE(device != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800190 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800191 }
192
193protected:
194 // Cache the device opening to speed up each test by ~0.5s
Kevin Rocard3c405a72017-03-08 16:46:51 -0800195 static sp<IPrimaryDevice> device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800196};
Kevin Rocard3c405a72017-03-08 16:46:51 -0800197sp<IPrimaryDevice> AudioPrimaryHidlTest::device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800198
199TEST_F(AudioPrimaryHidlTest, OpenPrimaryDevice) {
200 doc::test("Test the openDevice (called in SetUp)");
201}
202
203TEST_F(AudioPrimaryHidlTest, Init) {
204 doc::test("Test that the audio primary hal initialized correctly");
205 ASSERT_OK(device->initCheck());
206}
207
208//////////////////////////////////////////////////////////////////////////////
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800209///////////////////// {set,get}{Master,Mic}{Mute,Volume} /////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800210//////////////////////////////////////////////////////////////////////////////
211
Kevin Rocard3c405a72017-03-08 16:46:51 -0800212template <class Property>
213class AccessorPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocardf0357882017-02-10 16:19:28 -0800214protected:
Kevin Rocard3c405a72017-03-08 16:46:51 -0800215
216 /** Test a property getter and setter. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800217 template <class Getter, class Setter>
Kevin Rocard3c405a72017-03-08 16:46:51 -0800218 void testAccessors(const string& propertyName, const vector<Property>& valuesToTest,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800219 Setter setter, Getter getter,
220 const vector<Property>& invalidValues = {}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -0800221
222 Property initialValue; // Save initial value to restore it at the end of the test
223 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
224 ASSERT_OK(res);
225
226 for (Property setValue : valuesToTest) {
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800227 SCOPED_TRACE("Test " + propertyName + " getter and setter for " +
228 testing::PrintToString(setValue));
Kevin Rocard3c405a72017-03-08 16:46:51 -0800229 ASSERT_OK((device.get()->*setter)(setValue));
230 Property getValue;
231 // Make sure the getter returns the same value just set
232 ASSERT_OK((device.get()->*getter)(returnIn(res, getValue)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800233 ASSERT_OK(res);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800234 EXPECT_EQ(setValue, getValue);
Kevin Rocardf0357882017-02-10 16:19:28 -0800235 }
Kevin Rocard3c405a72017-03-08 16:46:51 -0800236
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800237 for (Property invalidValue : invalidValues) {
238 SCOPED_TRACE("Try to set " + propertyName + " with the invalid value " +
239 testing::PrintToString(invalidValue));
Kevin Rocard20e7af62017-03-10 17:10:43 -0800240 EXPECT_RESULT(Result::INVALID_ARGUMENTS, (device.get()->*setter)(invalidValue));
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800241 }
242
Kevin Rocard3c405a72017-03-08 16:46:51 -0800243 ASSERT_OK((device.get()->*setter)(initialValue)); // restore initial value
244 }
245
246 /** Test the getter and setter of an optional feature. */
247 template <class Getter, class Setter>
248 void testOptionalAccessors(const string& propertyName, const vector<Property>& valuesToTest,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800249 Setter setter, Getter getter,
250 const vector<Property>& invalidValues = {}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -0800251 doc::test("Test the optional " + propertyName + " getters and setter");
252 {
253 SCOPED_TRACE("Test feature support by calling the getter");
254 Property initialValue;
255 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
256 if (res == Result::NOT_SUPPORTED) {
257 doc::partialTest(propertyName + " getter is not supported");
258 return;
259 }
260 ASSERT_OK(res); // If it is supported it must succeed
261 }
262 // The feature is supported, test it
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800263 testAccessors(propertyName, valuesToTest, setter, getter, invalidValues);
Kevin Rocardf0357882017-02-10 16:19:28 -0800264 }
265};
266
Kevin Rocard3c405a72017-03-08 16:46:51 -0800267using BoolAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<bool>;
268
Kevin Rocardf0357882017-02-10 16:19:28 -0800269TEST_F(BoolAccessorPrimaryHidlTest, MicMuteTest) {
270 doc::test("Check that the mic can be muted and unmuted");
Kevin Rocard3c405a72017-03-08 16:46:51 -0800271 testAccessors("mic mute", {true, false, true}, &IDevice::setMicMute, &IDevice::getMicMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800272 // TODO: check that the mic is really muted (all sample are 0)
273}
274
275TEST_F(BoolAccessorPrimaryHidlTest, MasterMuteTest) {
276 doc::test("If master mute is supported, try to mute and unmute the master output");
Kevin Rocard3c405a72017-03-08 16:46:51 -0800277 testOptionalAccessors("master mute", {true, false, true},
278 &IDevice::setMasterMute, &IDevice::getMasterMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800279 // TODO: check that the master volume is really muted
280}
281
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800282using FloatAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<float>;
283TEST_F(FloatAccessorPrimaryHidlTest, MasterVolumeTest) {
284 doc::test("Test the master volume if supported");
285 testOptionalAccessors("master volume", {0, 0.5, 1},
286 &IDevice::setMasterVolume, &IDevice::getMasterVolume,
287 {-0.1, 1.1, NAN, INFINITY, -INFINITY,
288 1 + std::numeric_limits<float>::epsilon()});
289 // TODO: check that the master volume is really changed
290}
291
Kevin Rocardf0357882017-02-10 16:19:28 -0800292//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800293//////////////////////////////// AudioPatches ////////////////////////////////
294//////////////////////////////////////////////////////////////////////////////
295
296class AudioPatchPrimaryHidlTest : public AudioPrimaryHidlTest {
297protected:
298 bool areAudioPatchesSupported() {
299 auto result = device->supportsAudioPatches();
300 EXPECT_TRUE(result.isOk());
301 return result;
302 }
303};
304
305TEST_F(AudioPatchPrimaryHidlTest, AudioPatches) {
306 doc::test("Test if audio patches are supported");
307 if (!areAudioPatchesSupported()) {
308 doc::partialTest("Audio patches are not supported");
309 return;
310 }
311 // TODO: test audio patches
312}
313
314
315//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800316//////////////// Required and recommended audio format support ///////////////
317// From: https://source.android.com/compatibility/android-cdd.html#5_4_audio_recording
318// From: https://source.android.com/compatibility/android-cdd.html#5_5_audio_playback
319/////////// TODO: move to the beginning of the file for easier update ////////
320//////////////////////////////////////////////////////////////////////////////
321
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800322class AudioConfigPrimaryTest : public AudioPatchPrimaryHidlTest {
Kevin Rocardf0357882017-02-10 16:19:28 -0800323public:
324 // Cache result ?
325 static const vector<AudioConfig> getRequiredSupportPlaybackAudioConfig() {
326 return combineAudioConfig({AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
327 {8000, 11025, 16000, 22050, 32000, 44100},
328 {AudioFormat::PCM_16_BIT});
329 }
330
331 static const vector<AudioConfig> getRecommendedSupportPlaybackAudioConfig() {
332 return combineAudioConfig({AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
333 {24000, 48000},
334 {AudioFormat::PCM_16_BIT});
335 }
336
337 static const vector<AudioConfig> getSupportedPlaybackAudioConfig() {
338 // TODO: retrieve audio config supported by the platform
339 // as declared in the policy configuration
340 return {};
341 }
342
343 static const vector<AudioConfig> getRequiredSupportCaptureAudioConfig() {
344 return combineAudioConfig({AudioChannelMask::IN_MONO},
345 {8000, 11025, 16000, 44100},
346 {AudioFormat::PCM_16_BIT});
347 }
348 static const vector<AudioConfig> getRecommendedSupportCaptureAudioConfig() {
349 return combineAudioConfig({AudioChannelMask::IN_STEREO},
350 {22050, 48000},
351 {AudioFormat::PCM_16_BIT});
352 }
353 static const vector<AudioConfig> getSupportedCaptureAudioConfig() {
354 // TODO: retrieve audio config supported by the platform
355 // as declared in the policy configuration
356 return {};
357 }
358private:
359 static const vector<AudioConfig> combineAudioConfig(
360 vector<AudioChannelMask> channelMasks,
361 vector<uint32_t> sampleRates,
362 vector<AudioFormat> formats) {
363 vector<AudioConfig> configs;
364 for (auto channelMask: channelMasks) {
365 for (auto sampleRate : sampleRates) {
366 for (auto format : formats) {
367 AudioConfig config{};
368 // leave offloadInfo to 0
369 config.channelMask = channelMask;
370 config.sampleRateHz = sampleRate;
371 config.format = format;
372 // FIXME: leave frameCount to 0 ?
373 configs.push_back(config);
374 }
375 }
376 }
377 return configs;
378 }
379};
380
Kevin Rocard9c369142017-03-08 17:17:25 -0800381/** Generate a test name based on an audio config.
382 *
383 * As the only parameter changing are channel mask and sample rate,
384 * only print those ones in the test name.
385 */
386static string generateTestName(const testing::TestParamInfo<AudioConfig>& info) {
387 const AudioConfig& config = info.param;
388 return to_string(info.index) + "__" + to_string(config.sampleRateHz)+ "_" +
389 // "MONO" is more clear than "FRONT_LEFT"
390 ((config.channelMask == AudioChannelMask::OUT_MONO ||
391 config.channelMask == AudioChannelMask::IN_MONO) ?
392 "MONO" : toString(config.channelMask));
393}
394
Kevin Rocardf0357882017-02-10 16:19:28 -0800395//////////////////////////////////////////////////////////////////////////////
396///////////////////////////// getInputBufferSize /////////////////////////////
397//////////////////////////////////////////////////////////////////////////////
398
399// FIXME: execute input test only if platform declares android.hardware.microphone
400// how to get this value ? is it a property ???
401
402class AudioCaptureConfigPrimaryTest : public AudioConfigPrimaryTest,
403 public ::testing::WithParamInterface<AudioConfig> {
404protected:
405 void inputBufferSizeTest(const AudioConfig& audioConfig, bool supportRequired) {
406 uint64_t bufferSize;
407 ASSERT_OK(device->getInputBufferSize(audioConfig, returnIn(res, bufferSize)));
408
409 switch (res) {
410 case Result::INVALID_ARGUMENTS:
411 EXPECT_FALSE(supportRequired);
412 break;
413 case Result::OK:
414 // Check that the buffer is of a sane size
415 // For now only that it is > 0
416 EXPECT_GT(bufferSize, uint64_t(0));
417 break;
418 default:
419 FAIL() << "Invalid return status: " << ::testing::PrintToString(res);
420 }
421 }
422};
423
424// Test that the required capture config and those declared in the policy are indeed supported
425class RequiredInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
426TEST_P(RequiredInputBufferSizeTest, RequiredInputBufferSizeTest) {
427 doc::test("Input buffer size must be retrievable for a format with required support.");
428 inputBufferSizeTest(GetParam(), true);
429}
430INSTANTIATE_TEST_CASE_P(
431 RequiredInputBufferSize, RequiredInputBufferSizeTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800432 ::testing::ValuesIn(AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
433 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800434INSTANTIATE_TEST_CASE_P(
435 SupportedInputBufferSize, RequiredInputBufferSizeTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800436 ::testing::ValuesIn(AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
437 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800438
439// Test that the recommended capture config are supported or lead to a INVALID_ARGUMENTS return
440class OptionalInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
441TEST_P(OptionalInputBufferSizeTest, OptionalInputBufferSizeTest) {
442 doc::test("Input buffer size should be retrievable for a format with recommended support.");
443 inputBufferSizeTest(GetParam(), false);
444}
445INSTANTIATE_TEST_CASE_P(
446 RecommendedCaptureAudioConfigSupport, OptionalInputBufferSizeTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800447 ::testing::ValuesIn(AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
448 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800449
450//////////////////////////////////////////////////////////////////////////////
451/////////////////////////////// setScreenState ///////////////////////////////
452//////////////////////////////////////////////////////////////////////////////
453
454TEST_F(AudioPrimaryHidlTest, setScreenState) {
455 doc::test("Check that the hal can receive the screen state");
456 for (bool turnedOn : {false, true, true, false, false}) {
457 auto ret = device->setScreenState(turnedOn);
458 ASSERT_TRUE(ret.isOk());
459 Result result = ret;
460 ASSERT_TRUE(result == Result::OK || result == Result::NOT_SUPPORTED) << toString(result);
461 }
462}
463
464//////////////////////////////////////////////////////////////////////////////
465//////////////////////////// {get,set}Parameters /////////////////////////////
466//////////////////////////////////////////////////////////////////////////////
467
468TEST_F(AudioPrimaryHidlTest, getParameters) {
469 doc::test("Check that the hal can set and get parameters");
470 hidl_vec<hidl_string> keys;
471 hidl_vec<ParameterValue> values;
472 ASSERT_OK(device->getParameters(keys, returnIn(res, values)));
473 ASSERT_OK(device->setParameters(values));
474 values.resize(0);
475 ASSERT_OK(device->setParameters(values));
476}
477
478//////////////////////////////////////////////////////////////////////////////
479//////////////////////////////// debugDebug //////////////////////////////////
480//////////////////////////////////////////////////////////////////////////////
481
Kevin Rocardb9031242017-03-13 12:20:54 -0700482template <class DebugDump>
483static void testDebugDump(DebugDump debugDump) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800484 FILE* file = tmpfile();
485 ASSERT_NE(nullptr, file) << errno;
486
487 auto* nativeHandle = native_handle_create(1, 0);
488 ASSERT_NE(nullptr, nativeHandle);
489 nativeHandle->data[0] = fileno(file);
490
491 hidl_handle handle;
492 handle.setTo(nativeHandle, true /*take ownership*/);
493
Kevin Rocardee771e92017-03-08 17:17:25 -0800494 // TODO: debugDump does not return a Result.
Kevin Rocardf0357882017-02-10 16:19:28 -0800495 // This mean that the hal can not report that it not implementing the function.
Kevin Rocardb9031242017-03-13 12:20:54 -0700496 ASSERT_OK(debugDump(handle));
Kevin Rocardee771e92017-03-08 17:17:25 -0800497
498 rewind(file); // can not fail
Kevin Rocardf0357882017-02-10 16:19:28 -0800499
500 // Check that at least one bit was written by the hal
501 char buff;
Kevin Rocardee771e92017-03-08 17:17:25 -0800502 ASSERT_EQ(size_t{1}, fread(&buff, sizeof(buff), 1, file));
Kevin Rocardf0357882017-02-10 16:19:28 -0800503 EXPECT_EQ(0, fclose(file)) << errno;
504}
505
Kevin Rocardb9031242017-03-13 12:20:54 -0700506TEST_F(AudioPrimaryHidlTest, debugDump) {
507 doc::test("Check that the hal can dump its state without error");
508 testDebugDump([this](const auto& handle){ return device->debugDump(handle); });
509}
510
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700511TEST_F(AudioPrimaryHidlTest, debugDumpInvalidArguments) {
512 doc::test("Check that the hal dump doesn't crash on invalid arguments");
513 ASSERT_OK(device->debugDump(hidl_handle()));
514}
515
Kevin Rocardf0357882017-02-10 16:19:28 -0800516//////////////////////////////////////////////////////////////////////////////
517////////////////////////// open{Output,Input}Stream //////////////////////////
518//////////////////////////////////////////////////////////////////////////////
519
520template <class Stream>
521class OpenStreamTest : public AudioConfigPrimaryTest,
522 public ::testing::WithParamInterface<AudioConfig> {
523protected:
524 template <class Open>
525 void testOpen(Open openStream, const AudioConfig& config) {
526 // FIXME: Open a stream without an IOHandle
527 // This is not required to be accepted by hal implementations
528 AudioIoHandle ioHandle = (AudioIoHandle)AudioHandleConsts::AUDIO_IO_HANDLE_NONE;
529 AudioConfig suggestedConfig{};
530 ASSERT_OK(openStream(ioHandle, config, returnIn(res, stream, suggestedConfig)));
531
532 // TODO: only allow failure for RecommendedPlaybackAudioConfig
533 switch (res) {
534 case Result::OK:
535 ASSERT_TRUE(stream != nullptr);
536 audioConfig = config;
537 break;
538 case Result::INVALID_ARGUMENTS:
539 ASSERT_TRUE(stream == nullptr);
540 AudioConfig suggestedConfigRetry;
541 // Could not open stream with config, try again with the suggested one
542 ASSERT_OK(openStream(ioHandle, suggestedConfig,
543 returnIn(res, stream, suggestedConfigRetry)));
544 // This time it must succeed
545 ASSERT_OK(res);
546 ASSERT_TRUE(stream == nullptr);
547 audioConfig = suggestedConfig;
548 break;
549 default:
550 FAIL() << "Invalid return status: " << ::testing::PrintToString(res);
551 }
552 open = true;
553 }
554
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800555 Return<Result> closeStream() {
556 open = false;
557 return stream->close();
558 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800559private:
560 void TearDown() override {
561 if (open) {
562 ASSERT_OK(stream->close());
563 }
564 }
565
566protected:
567
568 AudioConfig audioConfig;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800569 DeviceAddress address = {};
Kevin Rocardf0357882017-02-10 16:19:28 -0800570 sp<Stream> stream;
571 bool open = false;
572};
573
574////////////////////////////// openOutputStream //////////////////////////////
575
576class OutputStreamTest : public OpenStreamTest<IStreamOut> {
577 virtual void SetUp() override {
578 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800579 address.device = AudioDevice::OUT_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800580 const AudioConfig& config = GetParam();
Kevin Rocardf0357882017-02-10 16:19:28 -0800581 AudioOutputFlag flags = AudioOutputFlag::NONE; // TODO: test all flag combination
582 testOpen([&](AudioIoHandle handle, AudioConfig config, auto cb)
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800583 { return device->openOutputStream(handle, address, config, flags, cb); },
Kevin Rocardf0357882017-02-10 16:19:28 -0800584 config);
585 }
586};
587TEST_P(OutputStreamTest, OpenOutputStreamTest) {
588 doc::test("Check that output streams can be open with the required and recommended config");
589 // Open done in SetUp
590}
591INSTANTIATE_TEST_CASE_P(
592 RequiredOutputStreamConfigSupport, OutputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800593 ::testing::ValuesIn(AudioConfigPrimaryTest::getRequiredSupportPlaybackAudioConfig()),
594 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800595INSTANTIATE_TEST_CASE_P(
596 SupportedOutputStreamConfig, OutputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800597 ::testing::ValuesIn(AudioConfigPrimaryTest::getSupportedPlaybackAudioConfig()),
598 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800599
600INSTANTIATE_TEST_CASE_P(
601 RecommendedOutputStreamConfigSupport, OutputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800602 ::testing::ValuesIn(AudioConfigPrimaryTest::getRecommendedSupportPlaybackAudioConfig()),
603 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800604
605////////////////////////////// openInputStream //////////////////////////////
606
607class InputStreamTest : public OpenStreamTest<IStreamIn> {
608
609 virtual void SetUp() override {
610 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800611 address.device = AudioDevice::IN_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800612 const AudioConfig& config = GetParam();
Kevin Rocardf0357882017-02-10 16:19:28 -0800613 AudioInputFlag flags = AudioInputFlag::NONE; // TODO: test all flag combination
614 AudioSource source = AudioSource::DEFAULT; // TODO: test all flag combination
615 testOpen([&](AudioIoHandle handle, AudioConfig config, auto cb)
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800616 { return device->openInputStream(handle, address, config, flags, source, cb); },
Kevin Rocardf0357882017-02-10 16:19:28 -0800617 config);
618 }
619};
620
621TEST_P(InputStreamTest, OpenInputStreamTest) {
622 doc::test("Check that input streams can be open with the required and recommended config");
623 // Open done in setup
624}
625INSTANTIATE_TEST_CASE_P(
626 RequiredInputStreamConfigSupport, InputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800627 ::testing::ValuesIn(AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
628 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800629INSTANTIATE_TEST_CASE_P(
630 SupportedInputStreamConfig, InputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800631 ::testing::ValuesIn(AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
632 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800633
634INSTANTIATE_TEST_CASE_P(
635 RecommendedInputStreamConfigSupport, InputStreamTest,
Kevin Rocard9c369142017-03-08 17:17:25 -0800636 ::testing::ValuesIn(AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
637 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800638
639//////////////////////////////////////////////////////////////////////////////
640////////////////////////////// IStream getters ///////////////////////////////
641//////////////////////////////////////////////////////////////////////////////
642
643/** Unpack the provided result.
644 * If the result is not OK, register a failure and return an undefined value. */
645template <class R>
646static R extract(Return<R> ret) {
647 if (!ret.isOk()) {
648 ADD_FAILURE();
649 return R{};
650 }
651 return ret;
652}
653
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800654/* Could not find a way to write a test for two parametrized class fixure
655 * thus use this macro do duplicate tests for Input and Output stream */
656#define TEST_IO_STREAM(test_name, documentation, code) \
657 TEST_P(InputStreamTest, test_name) { \
658 doc::test(documentation); \
659 code; \
660 } \
661 TEST_P(OutputStreamTest, test_name) { \
662 doc::test(documentation); \
663 code; \
664 }
665
666TEST_IO_STREAM(GetFrameCount, "Check that the stream frame count == the one it was opened with",
667 ASSERT_EQ(audioConfig.frameCount, extract(stream->getFrameCount())))
668
669TEST_IO_STREAM(GetSampleRate, "Check that the stream sample rate == the one it was opened with",
670 ASSERT_EQ(audioConfig.sampleRateHz, extract(stream->getSampleRate())))
671
672TEST_IO_STREAM(GetChannelMask, "Check that the stream channel mask == the one it was opened with",
673 ASSERT_EQ(audioConfig.channelMask, extract(stream->getChannelMask())))
674
675TEST_IO_STREAM(GetFormat, "Check that the stream format == the one it was opened with",
676 ASSERT_EQ(audioConfig.format, extract(stream->getFormat())))
677
678// TODO: for now only check that the framesize is not incoherent
679TEST_IO_STREAM(GetFrameSize, "Check that the stream frame size == the one it was opened with",
680 ASSERT_GT(extract(stream->getFrameSize()), 0U))
681
682TEST_IO_STREAM(GetBufferSize, "Check that the stream buffer size== the one it was opened with",
683 ASSERT_GE(extract(stream->getBufferSize()), \
684 extract(stream->getFrameSize())));
685
Kevin Rocardf0357882017-02-10 16:19:28 -0800686template <class Property, class CapabilityGetter, class Getter, class Setter>
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800687static void testCapabilityGetter(const string& name, IStream* stream, Property currentValue,
Kevin Rocardf0357882017-02-10 16:19:28 -0800688 CapabilityGetter capablityGetter, Getter getter, Setter setter) {
689 hidl_vec<Property> capabilities;
690 ASSERT_OK((stream->*capablityGetter)(returnIn(capabilities)));
691 if (capabilities.size() == 0) {
692 // The default hal should probably return a NOT_SUPPORTED if the hal does not expose
693 // capability retrieval. For now it returns an empty list if not implemented
694 doc::partialTest(name + " is not supported");
695 return;
696 };
697 // TODO: This code has never been tested on a hal that supports getSupportedSampleRates
698 EXPECT_NE(std::find(capabilities.begin(), capabilities.end(), currentValue),
699 capabilities.end())
700 << "current " << name << " is not in the list of the supported ones "
701 << toString(capabilities);
702
703 // Check that all declared supported values are indeed supported
704 for (auto capability : capabilities) {
705 ASSERT_OK((stream->*setter)(capability));
706 ASSERT_EQ(capability, extract((stream->*getter)()));
707 }
708}
709
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800710TEST_IO_STREAM(SupportedSampleRate, "Check that the stream sample rate is declared as supported",
711 testCapabilityGetter("getSupportedSampleRate", stream.get(), \
712 extract(stream->getSampleRate()), \
713 &IStream::getSupportedSampleRates, \
714 &IStream::getSampleRate, &IStream::setSampleRate))
715
716TEST_IO_STREAM(SupportedChannelMask, "Check that the stream channel mask is declared as supported",
717 testCapabilityGetter("getSupportedChannelMask", stream.get(), \
718 extract(stream->getChannelMask()), \
719 &IStream::getSupportedChannelMasks, \
720 &IStream::getChannelMask, &IStream::setChannelMask))
721
722TEST_IO_STREAM(SupportedFormat, "Check that the stream format is declared as supported",
723 testCapabilityGetter("getSupportedFormat", stream.get(), \
724 extract(stream->getFormat()), \
725 &IStream::getSupportedFormats, \
726 &IStream::getFormat, &IStream::setFormat))
727
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800728static void testGetDevice(IStream* stream, AudioDevice expectedDevice) {
729 auto ret = stream->getDevice();
730 ASSERT_TRUE(ret.isOk());
731 AudioDevice device = ret;
732 ASSERT_EQ(expectedDevice, device);
733}
734
735TEST_IO_STREAM(GetDevice, "Check that the stream device == the one it was opened with",
736 areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported") : \
737 testGetDevice(stream.get(), address.device))
738
739static void testSetDevice(IStream* stream, const DeviceAddress& address) {
740 DeviceAddress otherAddress = address;
741 otherAddress.device = (address.device & AudioDevice::BIT_IN) == 0 ?
742 AudioDevice::OUT_SPEAKER : AudioDevice::IN_BUILTIN_MIC;
743 EXPECT_OK(stream->setDevice(otherAddress));
744
745 ASSERT_OK(stream->setDevice(address)); // Go back to the original value
746}
747
748TEST_IO_STREAM(SetDevice, "Check that the stream can be rerouted to SPEAKER or BUILTIN_MIC",
749 areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported") : \
750 testSetDevice(stream.get(), address))
751
Kevin Rocardf0357882017-02-10 16:19:28 -0800752static void testGetAudioProperties(IStream* stream, AudioConfig expectedConfig) {
753 uint32_t sampleRateHz;
754 AudioChannelMask mask;
755 AudioFormat format;
756
757 stream->getAudioProperties(returnIn(sampleRateHz, mask, format));
758
759 // FIXME: the qcom hal it does not currently negotiate the sampleRate & channel mask
Kevin Rocardd1e98ae2017-03-08 17:17:25 -0800760 EXPECT_EQ(expectedConfig.sampleRateHz, sampleRateHz);
761 EXPECT_EQ(expectedConfig.channelMask, mask);
Kevin Rocardf0357882017-02-10 16:19:28 -0800762 EXPECT_EQ(expectedConfig.format, format);
763}
764
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800765TEST_IO_STREAM(GetAudioProperties,
766 "Check that the stream audio properties == the ones it was opened with",
767 testGetAudioProperties(stream.get(), audioConfig))
Kevin Rocardf0357882017-02-10 16:19:28 -0800768
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800769static void testConnectedState(IStream* stream) {
770 DeviceAddress address = {};
771 using AD = AudioDevice;
772 for (auto device : {AD::OUT_HDMI, AD::OUT_WIRED_HEADPHONE, AD::IN_USB_HEADSET}) {
773 address.device = device;
774
775 ASSERT_OK(stream->setConnectedState(address, true));
776 ASSERT_OK(stream->setConnectedState(address, false));
777 }
778}
779TEST_IO_STREAM(SetConnectedState,
780 "Check that the stream can be notified of device connection and deconnection",
781 testConnectedState(stream.get()))
782
783
784static auto invalidArgsOrNotSupported = {Result::INVALID_ARGUMENTS, Result::NOT_SUPPORTED};
785TEST_IO_STREAM(SetHwAvSync, "Try to set hardware sync to an invalid value",
786 ASSERT_RESULT(invalidArgsOrNotSupported, stream->setHwAvSync(666)))
787
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700788TEST_IO_STREAM(GetHwAvSync, "Get hardware sync can not fail",
789 ASSERT_TRUE(device->getHwAvSync().isOk()))
790
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800791static void checkGetParameter(IStream* stream, hidl_vec<hidl_string> keys,
792 vector<Result> expectedResults) {
793 hidl_vec<ParameterValue> parameters;
794 Result res;
795 ASSERT_OK(stream->getParameters(keys, returnIn(res, parameters)));
796 ASSERT_RESULT(expectedResults, res);
797 if (res == Result::OK) {
798 ASSERT_EQ(0U, parameters.size());
799 }
800}
801
802/* Get/Set parameter is intended to be an opaque channel between vendors app and their HALs.
803 * Thus can not be meaningfully tested.
804 * TODO: Doc missing. Should asking for an empty set of params raise an error ?
805 */
806TEST_IO_STREAM(getEmptySetParameter, "Retrieve the values of an empty set",
807 checkGetParameter(stream.get(), {} /* keys */,
808 {Result::OK, Result::INVALID_ARGUMENTS}))
809
810
811TEST_IO_STREAM(getNonExistingParameter, "Retrieve the values of an non existing parameter",
812 checkGetParameter(stream.get(), {"Non existing key"} /* keys */,
813 {Result::INVALID_ARGUMENTS}))
814
Kevin Rocard624800c2017-03-10 18:47:37 -0800815static vector<Result> okOrInvalidArguments = {Result::OK, Result::INVALID_ARGUMENTS};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800816TEST_IO_STREAM(setEmptySetParameter, "Set the values of an empty set of parameters",
Kevin Rocard624800c2017-03-10 18:47:37 -0800817 ASSERT_RESULT(okOrInvalidArguments, stream->setParameters({})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800818
819TEST_IO_STREAM(setNonExistingParameter, "Set the values of an non existing parameter",
820 ASSERT_RESULT(Result::INVALID_ARGUMENTS,
821 stream->setParameters({{"non existing key", "0"}})))
822
Kevin Rocardb9031242017-03-13 12:20:54 -0700823TEST_IO_STREAM(DebugDump,
824 "Check that a stream can dump its state without error",
825 testDebugDump([this](const auto& handle){ return stream->debugDump(handle); }))
826
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700827TEST_IO_STREAM(DebugDumpInvalidArguments,
828 "Check that the stream dump doesn't crash on invalid arguments",
829 ASSERT_OK(stream->debugDump(hidl_handle())))
830
Kevin Rocardf0357882017-02-10 16:19:28 -0800831//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800832////////////////////////////// addRemoveEffect ///////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800833//////////////////////////////////////////////////////////////////////////////
834
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800835TEST_IO_STREAM(AddNonExistingEffect, "Adding a non existing effect should fail",
836 ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->addEffect(666)))
837TEST_IO_STREAM(RemoveNonExistingEffect, "Removing a non existing effect should fail",
838 ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->removeEffect(666)))
Kevin Rocardf0357882017-02-10 16:19:28 -0800839
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800840//TODO: positive tests
841
842//////////////////////////////////////////////////////////////////////////////
843/////////////////////////////// Control ////////////////////////////////
844//////////////////////////////////////////////////////////////////////////////
845
846TEST_IO_STREAM(standby, "Make sure the stream can be put in stanby",
847 ASSERT_OK(stream->standby())) // can not fail
848
849static vector<Result> invalidStateOrNotSupported = {Result::INVALID_STATE, Result::NOT_SUPPORTED};
850
851TEST_IO_STREAM(startNoMmap, "Starting a mmaped stream before mapping it should fail",
852 ASSERT_RESULT(invalidStateOrNotSupported, stream->start()))
853
854TEST_IO_STREAM(stopNoMmap, "Stopping a mmaped stream before mapping it should fail",
855 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
856
857TEST_IO_STREAM(getMmapPositionNoMmap, "Get a stream Mmap position before mapping it should fail",
858 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
859
860TEST_IO_STREAM(close, "Make sure a stream can be closed", ASSERT_OK(closeStream()))
861TEST_IO_STREAM(closeTwice, "Make sure a stream can not be closed twice",
862 ASSERT_OK(closeStream()); \
863 ASSERT_RESULT(Result::INVALID_STATE, closeStream()))
864
865static void testCreateTooBigMmapBuffer(IStream* stream) {
866 MmapBufferInfo info;
867 Result res;
868 // Assume that int max is a value too big to be allocated
869 // This is true currently with a 32bit media server, but might not when it will run in 64 bit
870 auto minSizeFrames = std::numeric_limits<int32_t>::max();
871 ASSERT_OK(stream->createMmapBuffer(minSizeFrames, returnIn(res, info)));
872 ASSERT_RESULT(invalidArgsOrNotSupported, res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800873}
874
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800875TEST_IO_STREAM(CreateTooBigMmapBuffer, "Create mmap buffer too big should fail",
876 testCreateTooBigMmapBuffer(stream.get()))
877
878
879static void testGetMmapPositionOfNonMmapedStream(IStream* stream) {
880 Result res;
881 MmapPosition position;
882 ASSERT_OK(stream->getMmapPosition(returnIn(res, position)));
883 ASSERT_RESULT(invalidArgsOrNotSupported, res);
884}
885
886TEST_IO_STREAM(GetMmapPositionOfNonMmapedStream,
887 "Retrieving the mmap position of a non mmaped stream should fail",
888 testGetMmapPositionOfNonMmapedStream(stream.get()))
889
Kevin Rocardf0357882017-02-10 16:19:28 -0800890//////////////////////////////////////////////////////////////////////////////
Kevin Rocardc9963522017-03-10 18:47:37 -0800891///////////////////////////////// StreamIn ///////////////////////////////////
892//////////////////////////////////////////////////////////////////////////////
893
894TEST_P(InputStreamTest, GetAudioSource) {
895 doc::test("Retrieving the audio source of an input stream should always succeed");
896 AudioSource source;
897 ASSERT_OK(stream->getAudioSource(returnIn(res, source)));
898 ASSERT_OK(res);
899 ASSERT_EQ(AudioSource::DEFAULT, source);
900}
901
902static void testUnitaryGain(std::function<Return<Result> (float)> setGain) {
903 for (float value : {0.0, 0.01, 0.5, 0.09, 1.0}) {
904 SCOPED_TRACE("value=" + to_string(value));
905 ASSERT_OK(setGain(value));
906 }
907 for (float value : (float[]){-INFINITY,-1.0, -0.0,
908 1.0 + std::numeric_limits<float>::epsilon(), 2.0, INFINITY,
909 NAN}) {
910 SCOPED_TRACE("value=" + to_string(value));
911 // FIXME: NAN should never be accepted
912 // FIXME: Missing api doc. What should the impl do if the volume is outside [0,1] ?
913 ASSERT_RESULT(Result::INVALID_ARGUMENTS, setGain(value));
914 }
915}
916
917TEST_P(InputStreamTest, SetGain) {
918 doc::test("The gain of an input stream should only be set between [0,1]");
919 testUnitaryGain([this](float volume) { return stream->setGain(volume); });
920}
921
922static void testPrepareForReading(IStreamIn* stream, uint32_t frameSize, uint32_t framesCount) {
923 Result res;
924 // Ignore output parameters as the call should fail
925 ASSERT_OK(stream->prepareForReading(frameSize, framesCount,
926 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
927 EXPECT_RESULT(invalidArgsOrNotSupported, res);
928}
929
930TEST_P(InputStreamTest, PrepareForReadingWithHugeBuffer) {
931 doc::test("Preparing a stream for reading with a 2^32 sized buffer should fail");
932 testPrepareForReading(stream.get(), 1, std::numeric_limits<uint32_t>::max());
933}
934
935TEST_P(InputStreamTest, PrepareForReadingCheckOverflow) {
936 doc::test("Preparing a stream for reading with a overflowing sized buffer should fail");
937 auto uintMax = std::numeric_limits<uint32_t>::max();
938 testPrepareForReading(stream.get(), uintMax, uintMax);
939}
940
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700941TEST_P(InputStreamTest, GetInputFramesLost) {
942 doc::test("The number of frames lost on a never started stream should be 0");
943 auto ret = stream->getInputFramesLost();
944 ASSERT_TRUE(ret.isOk());
945 uint32_t framesLost{ret};
946 ASSERT_EQ(0U, framesLost);
947}
948
Kevin Rocardc9963522017-03-10 18:47:37 -0800949TEST_P(InputStreamTest, getCapturePosition) {
950 doc::test("The capture position of a non prepared stream should not be retrievable");
951 uint64_t frames;
952 uint64_t time;
953 ASSERT_OK(stream->getCapturePosition(returnIn(res, frames, time)));
954 ASSERT_RESULT(invalidStateOrNotSupported, res);
955}
956
957//////////////////////////////////////////////////////////////////////////////
Kevin Rocard624800c2017-03-10 18:47:37 -0800958///////////////////////////////// StreamIn ///////////////////////////////////
959//////////////////////////////////////////////////////////////////////////////
960
961TEST_P(OutputStreamTest, getLatency) {
962 doc::test("Make sure latency is over 0");
963 auto result = stream->getLatency();
964 ASSERT_TRUE(result.isOk());
965 ASSERT_GT(result, 0U);
966}
967
968TEST_P(OutputStreamTest, setVolume) {
969 doc::test("Try to set the output volume");
970 auto result = stream->setVolume(1, 1);
971 ASSERT_TRUE(result.isOk());
972 if (result == Result::NOT_SUPPORTED) {
973 doc::partialTest("setVolume is not supported");
974 return;
975 }
976 testUnitaryGain([this](float volume) { return stream->setVolume(volume, volume); });
977}
978
979static void testPrepareForWriting(IStreamOut* stream, uint32_t frameSize, uint32_t framesCount) {
980 Result res;
981 // Ignore output parameters as the call should fail
982 ASSERT_OK(stream->prepareForWriting(frameSize, framesCount,
983 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
984 EXPECT_RESULT(invalidArgsOrNotSupported, res);
985}
986
987TEST_P(OutputStreamTest, PrepareForWriteWithHugeBuffer) {
988 doc::test("Preparing a stream for writing with a 2^32 sized buffer should fail");
989 testPrepareForWriting(stream.get(), 1, std::numeric_limits<uint32_t>::max());
990}
991
992TEST_P(OutputStreamTest, PrepareForWritingCheckOverflow) {
993 doc::test("Preparing a stream for writing with a overflowing sized buffer should fail");
994 auto uintMax = std::numeric_limits<uint32_t>::max();
995 testPrepareForWriting(stream.get(), uintMax, uintMax);
996}
997
998struct Capability {
999 Capability(IStreamOut* stream) {
1000 EXPECT_OK(stream->supportsPauseAndResume(returnIn(pause, resume)));
1001 auto ret = stream->supportsDrain();
1002 EXPECT_TRUE(ret.isOk());
1003 if (ret.isOk()) {
1004 drain = ret;
1005 }
1006 }
1007 bool pause = false;
1008 bool resume = false;
1009 bool drain = false;
1010};
1011
1012TEST_P(OutputStreamTest, SupportsPauseAndResumeAndDrain) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001013 doc::test("Implementation must expose pause, resume and drain capabilities");
Kevin Rocard624800c2017-03-10 18:47:37 -08001014 Capability(stream.get());
1015}
1016
1017TEST_P(OutputStreamTest, GetRenderPosition) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001018 doc::test("The render position should be 0 on a not started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001019 uint32_t dspFrames;
1020 ASSERT_OK(stream->getRenderPosition(returnIn(res, dspFrames)));
1021 if (res == Result::NOT_SUPPORTED) {
1022 doc::partialTest("getRenderPosition is not supported");
1023 return;
1024 }
1025 ASSERT_OK(res);
1026 ASSERT_EQ(0U, dspFrames);
1027}
1028
1029TEST_P(OutputStreamTest, GetNextWriteTimestamp) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001030 doc::test("The render position of a stream just created should be 0");
Kevin Rocard624800c2017-03-10 18:47:37 -08001031 uint64_t timestampUs;
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001032 ASSERT_OK(stream->getNextWriteTimestamp(returnIn(res, timestampUs)));
Kevin Rocard624800c2017-03-10 18:47:37 -08001033 if (res == Result::NOT_SUPPORTED) {
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001034 doc::partialTest("getNextWriteTimestamp is not supported");
Kevin Rocard624800c2017-03-10 18:47:37 -08001035 return;
1036 }
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001037 ASSERT_EQ(Result::INVALID_STATE, res);
Kevin Rocard624800c2017-03-10 18:47:37 -08001038}
1039
1040/** Stub implementation of out stream callback. */
1041class MockOutCallbacks : public IStreamOutCallback {
1042 Return<void> onWriteReady() override { return {}; }
1043 Return<void> onDrainReady() override { return {}; }
1044 Return<void> onError() override { return {}; }
1045};
1046
1047static bool isAsyncModeSupported(IStreamOut *stream) {
1048 auto res = stream->setCallback(new MockOutCallbacks);
1049 stream->clearCallback(); // try to restore the no callback state, ignore any error
1050 auto okOrNotSupported = { Result::OK, Result::NOT_SUPPORTED };
1051 EXPECT_RESULT(okOrNotSupported, res);
1052 return res.isOk() ? res == Result::OK : false;
1053}
1054
1055TEST_P(OutputStreamTest, SetCallback) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001056 doc::test("If supported, registering callback for async operation should never fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001057 if (!isAsyncModeSupported(stream.get())) {
1058 doc::partialTest("The stream does not support async operations");
1059 return;
1060 }
1061 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1062 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1063}
1064
1065TEST_P(OutputStreamTest, clearCallback) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001066 doc::test("If supported, clearing a callback to go back to sync operation should not fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001067 if (!isAsyncModeSupported(stream.get())) {
1068 doc::partialTest("The stream does not support async operations");
1069 return;
1070 }
1071 // TODO: Clarify if clearing a non existing callback should fail
1072 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1073 ASSERT_OK(stream->clearCallback());
1074}
1075
1076TEST_P(OutputStreamTest, Resume) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001077 doc::test("If supported, a stream should fail to resume if not previously paused");
Kevin Rocard624800c2017-03-10 18:47:37 -08001078 if (!Capability(stream.get()).resume) {
1079 doc::partialTest("The output stream does not support resume");
1080 return;
1081 }
1082 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1083}
1084
1085TEST_P(OutputStreamTest, Pause) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001086 doc::test("If supported, a stream should fail to pause if not previously started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001087 if (!Capability(stream.get()).pause) {
1088 doc::partialTest("The output stream does not support pause");
1089 return;
1090 }
1091 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1092}
1093
1094static void testDrain(IStreamOut *stream, AudioDrain type) {
1095 if (!Capability(stream).drain) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001096 doc::partialTest("The output stream does not support drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001097 return;
1098 }
1099 ASSERT_RESULT(Result::OK, stream->drain(type));
1100}
1101
1102TEST_P(OutputStreamTest, DrainAll) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001103 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001104 testDrain(stream.get(), AudioDrain::ALL);
1105}
1106
1107TEST_P(OutputStreamTest, DrainEarlyNotify) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001108 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001109 testDrain(stream.get(), AudioDrain::EARLY_NOTIFY);
1110}
1111
1112TEST_P(OutputStreamTest, FlushStop) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001113 doc::test("If supported, a stream should always succeed to flush");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001114 auto ret = stream->flush();
1115 ASSERT_TRUE(ret.isOk());
1116 if (ret == Result::NOT_SUPPORTED) {
1117 doc::partialTest("Flush is not supported");
1118 return;
1119 }
1120 ASSERT_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001121}
1122
1123TEST_P(OutputStreamTest, GetPresentationPositionStop) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001124 doc::test("If supported, a stream should always succeed to retrieve the presentation position");
Kevin Rocard624800c2017-03-10 18:47:37 -08001125 uint64_t frames;
1126 TimeSpec mesureTS;
1127 ASSERT_OK(stream->getPresentationPosition(returnIn(res, frames, mesureTS)));
1128 if (res == Result::NOT_SUPPORTED) {
1129 doc::partialTest("getpresentationPosition is not supported");
1130 return;
1131 }
1132 ASSERT_EQ(0U, frames);
1133
1134 struct timespec currentTS;
1135 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currentTS)) << errno;
1136
1137 auto toMicroSec = [](uint64_t sec, auto nsec) { return sec * 1e+6 + nsec / 1e+3; };
1138 auto currentTime = toMicroSec(currentTS.tv_sec, currentTS.tv_nsec);
1139 auto mesureTime = toMicroSec(mesureTS.tvSec, mesureTS.tvNSec);
1140 ASSERT_PRED2([](auto c, auto m){ return c - m < 1e+6; }, currentTime, mesureTime);
1141}
1142
1143
1144//////////////////////////////////////////////////////////////////////////////
Kevin Rocard3c405a72017-03-08 16:46:51 -08001145/////////////////////////////// PrimaryDevice ////////////////////////////////
1146//////////////////////////////////////////////////////////////////////////////
1147
Kevin Rocardc9963522017-03-10 18:47:37 -08001148
Kevin Rocard3c405a72017-03-08 16:46:51 -08001149TEST_F(AudioPrimaryHidlTest, setVoiceVolume) {
1150 doc::test("Make sure setVoiceVolume only succeed if volume is in [0,1]");
Kevin Rocardc9963522017-03-10 18:47:37 -08001151 testUnitaryGain([this](float volume) { return device->setVoiceVolume(volume); });
Kevin Rocard3c405a72017-03-08 16:46:51 -08001152}
1153
1154TEST_F(AudioPrimaryHidlTest, setMode) {
1155 doc::test("Make sure setMode always succeeds if mode is valid");
1156 for (AudioMode mode : {AudioMode::IN_CALL, AudioMode::IN_COMMUNICATION,
1157 AudioMode::RINGTONE, AudioMode::CURRENT,
1158 AudioMode::NORMAL /* Make sure to leave the test in normal mode */ }) {
1159 SCOPED_TRACE("mode=" + toString(mode));
1160 ASSERT_OK(device->setMode(mode));
1161 }
1162
1163 // FIXME: Missing api doc. What should the impl do if the mode is invalid ?
Kevin Rocard20e7af62017-03-10 17:10:43 -08001164 ASSERT_RESULT(Result::INVALID_ARGUMENTS, device->setMode(AudioMode::INVALID));
Kevin Rocard3c405a72017-03-08 16:46:51 -08001165}
1166
1167
1168TEST_F(BoolAccessorPrimaryHidlTest, BtScoNrecEnabled) {
1169 doc::test("Query and set the BT SCO NR&EC state");
1170 testOptionalAccessors("BtScoNrecEnabled", {true, false, true},
1171 &IPrimaryDevice::setBtScoNrecEnabled,
1172 &IPrimaryDevice::getBtScoNrecEnabled);
1173}
1174
1175TEST_F(BoolAccessorPrimaryHidlTest, setGetBtScoWidebandEnabled) {
1176 doc::test("Query and set the SCO whideband state");
1177 testOptionalAccessors("BtScoWideband", {true, false, true},
1178 &IPrimaryDevice::setBtScoWidebandEnabled,
1179 &IPrimaryDevice::getBtScoWidebandEnabled);
1180}
1181
1182using TtyModeAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<TtyMode>;
1183TEST_F(TtyModeAccessorPrimaryHidlTest, setGetTtyMode) {
1184 doc::test("Query and set the TTY mode state");
1185 testOptionalAccessors("TTY mode", {TtyMode::OFF, TtyMode::HCO, TtyMode::VCO, TtyMode::FULL},
1186 &IPrimaryDevice::setTtyMode, &IPrimaryDevice::getTtyMode);
1187}
1188
1189TEST_F(BoolAccessorPrimaryHidlTest, setGetHac) {
1190 doc::test("Query and set the HAC state");
1191 testAccessors("HAC", {true, false, true},
1192 &IPrimaryDevice::setHacEnabled,
1193 &IPrimaryDevice::getHacEnabled);
1194}
1195
1196//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -08001197//////////////////// Clean caches on global tear down ////////////////////////
1198//////////////////////////////////////////////////////////////////////////////
1199
1200int main(int argc, char** argv) {
1201 environment = new Environment;
1202 ::testing::AddGlobalTestEnvironment(environment);
1203 ::testing::InitGoogleTest(&argc, argv);
1204 int status = RUN_ALL_TESTS();
1205 LOG(INFO) << "Test result = " << status;
1206 return status;
1207}