blob: 7c213d4e6b9d44df21f77b9183ef9f254e2212c2 [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>
Kevin Rocardf0357882017-02-10 16:19:28 -080024#include <string>
Kevin Rocardf0357882017-02-10 16:19:28 -080025#include <vector>
26
Tri Vo6c00ac32017-10-12 15:47:32 -070027#include <fcntl.h>
28#include <unistd.h>
29
Yuexi Ma161b5642017-03-10 13:44:22 -080030#include <VtsHalHidlTargetTestBase.h>
Kevin Rocardf0357882017-02-10 16:19:28 -080031
32#include <android-base/logging.h>
33
34#include <android/hardware/audio/2.0/IDevice.h>
35#include <android/hardware/audio/2.0/IDevicesFactory.h>
Kevin Rocard3c405a72017-03-08 16:46:51 -080036#include <android/hardware/audio/2.0/IPrimaryDevice.h>
Kevin Rocardf0357882017-02-10 16:19:28 -080037#include <android/hardware/audio/2.0/types.h>
38#include <android/hardware/audio/common/2.0/types.h>
39
Kevin Rocardf0357882017-02-10 16:19:28 -080040#include "utility/AssertOk.h"
Kevin Rocardb6d79ff2017-06-02 11:44:06 -070041#include "utility/Documentation.h"
42#include "utility/EnvironmentTearDown.h"
Kevin Rocardf0357882017-02-10 16:19:28 -080043#include "utility/PrettyPrintAudioTypes.h"
Kevin Rocard72e50e22017-05-05 14:02:55 -070044#include "utility/ReturnIn.h"
Kevin Rocardf0357882017-02-10 16:19:28 -080045
46using std::string;
47using std::to_string;
48using std::vector;
49
50using ::android::sp;
51using ::android::hardware::Return;
52using ::android::hardware::hidl_handle;
53using ::android::hardware::hidl_string;
54using ::android::hardware::hidl_vec;
Kevin Rocardc9963522017-03-10 18:47:37 -080055using ::android::hardware::MQDescriptorSync;
Kevin Rocard624800c2017-03-10 18:47:37 -080056using ::android::hardware::audio::V2_0::AudioDrain;
Kevin Rocardf0357882017-02-10 16:19:28 -080057using ::android::hardware::audio::V2_0::DeviceAddress;
58using ::android::hardware::audio::V2_0::IDevice;
Kevin Rocard3c405a72017-03-08 16:46:51 -080059using ::android::hardware::audio::V2_0::IPrimaryDevice;
60using TtyMode = ::android::hardware::audio::V2_0::IPrimaryDevice::TtyMode;
Kevin Rocardf0357882017-02-10 16:19:28 -080061using ::android::hardware::audio::V2_0::IDevicesFactory;
62using ::android::hardware::audio::V2_0::IStream;
63using ::android::hardware::audio::V2_0::IStreamIn;
Kevin Rocard624800c2017-03-10 18:47:37 -080064using ::android::hardware::audio::V2_0::TimeSpec;
Kevin Rocardb6d79ff2017-06-02 11:44:06 -070065using ReadParameters = ::android::hardware::audio::V2_0::IStreamIn::ReadParameters;
Kevin Rocardc9963522017-03-10 18:47:37 -080066using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus;
Kevin Rocardf0357882017-02-10 16:19:28 -080067using ::android::hardware::audio::V2_0::IStreamOut;
Kevin Rocard624800c2017-03-10 18:47:37 -080068using ::android::hardware::audio::V2_0::IStreamOutCallback;
Kevin Rocard8878b4b2017-03-10 18:47:37 -080069using ::android::hardware::audio::V2_0::MmapBufferInfo;
70using ::android::hardware::audio::V2_0::MmapPosition;
Kevin Rocardf0357882017-02-10 16:19:28 -080071using ::android::hardware::audio::V2_0::ParameterValue;
72using ::android::hardware::audio::V2_0::Result;
73using ::android::hardware::audio::common::V2_0::AudioChannelMask;
74using ::android::hardware::audio::common::V2_0::AudioConfig;
75using ::android::hardware::audio::common::V2_0::AudioDevice;
76using ::android::hardware::audio::common::V2_0::AudioFormat;
77using ::android::hardware::audio::common::V2_0::AudioHandleConsts;
78using ::android::hardware::audio::common::V2_0::AudioInputFlag;
79using ::android::hardware::audio::common::V2_0::AudioIoHandle;
Kevin Rocard3c405a72017-03-08 16:46:51 -080080using ::android::hardware::audio::common::V2_0::AudioMode;
Kevin Rocardf0357882017-02-10 16:19:28 -080081using ::android::hardware::audio::common::V2_0::AudioOffloadInfo;
82using ::android::hardware::audio::common::V2_0::AudioOutputFlag;
83using ::android::hardware::audio::common::V2_0::AudioSource;
Kevin Rocardc9963522017-03-10 18:47:37 -080084using ::android::hardware::audio::common::V2_0::ThreadInfo;
Kevin Rocardf0357882017-02-10 16:19:28 -080085
Kevin Rocardb6d79ff2017-06-02 11:44:06 -070086using namespace ::android::hardware::audio::common::test::utility;
Kevin Rocardf0357882017-02-10 16:19:28 -080087
Kevin Rocardf0357882017-02-10 16:19:28 -080088// Instance to register global tearDown
89static Environment* environment;
90
Yuexi Ma161b5642017-03-10 13:44:22 -080091class HidlTest : public ::testing::VtsHalHidlTargetTestBase {
Kevin Rocard72e50e22017-05-05 14:02:55 -070092 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -080093 // Convenient member to store results
94 Result res;
95};
96
97//////////////////////////////////////////////////////////////////////////////
98////////////////////// getService audio_devices_factory //////////////////////
99//////////////////////////////////////////////////////////////////////////////
100
101// Test all audio devices
102class AudioHidlTest : public HidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700103 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800104 void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700105 ASSERT_NO_FATAL_FAILURE(HidlTest::SetUp()); // setup base
Kevin Rocardf0357882017-02-10 16:19:28 -0800106
107 if (devicesFactory == nullptr) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700108 environment->registerTearDown([] { devicesFactory.clear(); });
109 devicesFactory = ::testing::VtsHalHidlTargetTestBase::getService<
110 IDevicesFactory>();
Kevin Rocardf0357882017-02-10 16:19:28 -0800111 }
112 ASSERT_TRUE(devicesFactory != nullptr);
113 }
114
Kevin Rocard72e50e22017-05-05 14:02:55 -0700115 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800116 // Cache the devicesFactory retrieval to speed up each test by ~0.5s
117 static sp<IDevicesFactory> devicesFactory;
118};
119sp<IDevicesFactory> AudioHidlTest::devicesFactory;
120
121TEST_F(AudioHidlTest, GetAudioDevicesFactoryService) {
122 doc::test("test the getService (called in SetUp)");
123}
124
Mikhail Naganov8604a732017-04-24 09:29:22 -0700125TEST_F(AudioHidlTest, OpenDeviceInvalidParameter) {
126 doc::test("test passing an invalid parameter to openDevice");
127 IDevicesFactory::Result result;
128 sp<IDevice> device;
129 ASSERT_OK(devicesFactory->openDevice(IDevicesFactory::Device(-1),
130 returnIn(result, device)));
131 ASSERT_EQ(IDevicesFactory::Result::INVALID_ARGUMENTS, result);
132 ASSERT_TRUE(device == nullptr);
133}
134
Kevin Rocardf0357882017-02-10 16:19:28 -0800135//////////////////////////////////////////////////////////////////////////////
136/////////////////////////////// openDevice primary ///////////////////////////
137//////////////////////////////////////////////////////////////////////////////
138
139// Test the primary device
140class AudioPrimaryHidlTest : public AudioHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700141 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800142 /** Primary HAL test are NOT thread safe. */
143 void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700144 ASSERT_NO_FATAL_FAILURE(AudioHidlTest::SetUp()); // setup base
Kevin Rocardf0357882017-02-10 16:19:28 -0800145
146 if (device == nullptr) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800147 IDevicesFactory::Result result;
Kevin Rocard3c405a72017-03-08 16:46:51 -0800148 sp<IDevice> baseDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700149 ASSERT_OK(
150 devicesFactory->openDevice(IDevicesFactory::Device::PRIMARY,
151 returnIn(result, baseDevice)));
Kevin Rocardfba442a2017-03-31 19:34:41 -0700152 ASSERT_OK(result);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800153 ASSERT_TRUE(baseDevice != nullptr);
154
Kevin Rocard72e50e22017-05-05 14:02:55 -0700155 environment->registerTearDown([] { device.clear(); });
Kevin Rocard3c405a72017-03-08 16:46:51 -0800156 device = IPrimaryDevice::castFrom(baseDevice);
157 ASSERT_TRUE(device != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800158 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800159 }
160
Kevin Rocard72e50e22017-05-05 14:02:55 -0700161 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800162 // Cache the device opening to speed up each test by ~0.5s
Kevin Rocard3c405a72017-03-08 16:46:51 -0800163 static sp<IPrimaryDevice> device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800164};
Kevin Rocard3c405a72017-03-08 16:46:51 -0800165sp<IPrimaryDevice> AudioPrimaryHidlTest::device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800166
167TEST_F(AudioPrimaryHidlTest, OpenPrimaryDevice) {
168 doc::test("Test the openDevice (called in SetUp)");
169}
170
171TEST_F(AudioPrimaryHidlTest, Init) {
172 doc::test("Test that the audio primary hal initialized correctly");
173 ASSERT_OK(device->initCheck());
174}
175
176//////////////////////////////////////////////////////////////////////////////
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800177///////////////////// {set,get}{Master,Mic}{Mute,Volume} /////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800178//////////////////////////////////////////////////////////////////////////////
179
Kevin Rocard3c405a72017-03-08 16:46:51 -0800180template <class Property>
181class AccessorPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700182 protected:
Kevin Rocard3c405a72017-03-08 16:46:51 -0800183 /** Test a property getter and setter. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800184 template <class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700185 void testAccessors(const string& propertyName,
186 const vector<Property>& valuesToTest, Setter setter,
187 Getter getter,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800188 const vector<Property>& invalidValues = {}) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700189 Property initialValue; // Save initial value to restore it at the end
190 // of the test
Kevin Rocard3c405a72017-03-08 16:46:51 -0800191 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
192 ASSERT_OK(res);
193
194 for (Property setValue : valuesToTest) {
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800195 SCOPED_TRACE("Test " + propertyName + " getter and setter for " +
196 testing::PrintToString(setValue));
Kevin Rocard3c405a72017-03-08 16:46:51 -0800197 ASSERT_OK((device.get()->*setter)(setValue));
198 Property getValue;
199 // Make sure the getter returns the same value just set
200 ASSERT_OK((device.get()->*getter)(returnIn(res, getValue)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800201 ASSERT_OK(res);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800202 EXPECT_EQ(setValue, getValue);
Kevin Rocardf0357882017-02-10 16:19:28 -0800203 }
Kevin Rocard3c405a72017-03-08 16:46:51 -0800204
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800205 for (Property invalidValue : invalidValues) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700206 SCOPED_TRACE("Try to set " + propertyName +
207 " with the invalid value " +
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800208 testing::PrintToString(invalidValue));
Kevin Rocard72e50e22017-05-05 14:02:55 -0700209 EXPECT_RESULT(Result::INVALID_ARGUMENTS,
210 (device.get()->*setter)(invalidValue));
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800211 }
212
Kevin Rocard72e50e22017-05-05 14:02:55 -0700213 ASSERT_OK(
214 (device.get()->*setter)(initialValue)); // restore initial value
Kevin Rocard3c405a72017-03-08 16:46:51 -0800215 }
216
217 /** Test the getter and setter of an optional feature. */
218 template <class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700219 void testOptionalAccessors(const string& propertyName,
220 const vector<Property>& valuesToTest,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800221 Setter setter, Getter getter,
222 const vector<Property>& invalidValues = {}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -0800223 doc::test("Test the optional " + propertyName + " getters and setter");
224 {
225 SCOPED_TRACE("Test feature support by calling the getter");
226 Property initialValue;
227 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
228 if (res == Result::NOT_SUPPORTED) {
229 doc::partialTest(propertyName + " getter is not supported");
230 return;
231 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700232 ASSERT_OK(res); // If it is supported it must succeed
Kevin Rocard3c405a72017-03-08 16:46:51 -0800233 }
234 // The feature is supported, test it
Kevin Rocard72e50e22017-05-05 14:02:55 -0700235 testAccessors(propertyName, valuesToTest, setter, getter,
236 invalidValues);
Kevin Rocardf0357882017-02-10 16:19:28 -0800237 }
238};
239
Kevin Rocard3c405a72017-03-08 16:46:51 -0800240using BoolAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<bool>;
241
Kevin Rocardf0357882017-02-10 16:19:28 -0800242TEST_F(BoolAccessorPrimaryHidlTest, MicMuteTest) {
243 doc::test("Check that the mic can be muted and unmuted");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700244 testAccessors("mic mute", {true, false, true}, &IDevice::setMicMute,
245 &IDevice::getMicMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800246 // TODO: check that the mic is really muted (all sample are 0)
247}
248
249TEST_F(BoolAccessorPrimaryHidlTest, MasterMuteTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700250 doc::test(
251 "If master mute is supported, try to mute and unmute the master "
252 "output");
Kevin Rocard3c405a72017-03-08 16:46:51 -0800253 testOptionalAccessors("master mute", {true, false, true},
254 &IDevice::setMasterMute, &IDevice::getMasterMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800255 // TODO: check that the master volume is really muted
256}
257
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800258using FloatAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<float>;
259TEST_F(FloatAccessorPrimaryHidlTest, MasterVolumeTest) {
260 doc::test("Test the master volume if supported");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700261 testOptionalAccessors("master volume", {0, 0.5, 1},
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800262 &IDevice::setMasterVolume, &IDevice::getMasterVolume,
263 {-0.1, 1.1, NAN, INFINITY, -INFINITY,
264 1 + std::numeric_limits<float>::epsilon()});
265 // TODO: check that the master volume is really changed
266}
267
Kevin Rocardf0357882017-02-10 16:19:28 -0800268//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800269//////////////////////////////// AudioPatches ////////////////////////////////
270//////////////////////////////////////////////////////////////////////////////
271
272class AudioPatchPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700273 protected:
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800274 bool areAudioPatchesSupported() {
275 auto result = device->supportsAudioPatches();
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700276 EXPECT_IS_OK(result);
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800277 return result;
278 }
279};
280
281TEST_F(AudioPatchPrimaryHidlTest, AudioPatches) {
282 doc::test("Test if audio patches are supported");
283 if (!areAudioPatchesSupported()) {
284 doc::partialTest("Audio patches are not supported");
285 return;
286 }
287 // TODO: test audio patches
288}
289
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800290//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800291//////////////// Required and recommended audio format support ///////////////
Kevin Rocard72e50e22017-05-05 14:02:55 -0700292// From:
293// https://source.android.com/compatibility/android-cdd.html#5_4_audio_recording
294// From:
295// https://source.android.com/compatibility/android-cdd.html#5_5_audio_playback
Kevin Rocardf0357882017-02-10 16:19:28 -0800296/////////// TODO: move to the beginning of the file for easier update ////////
297//////////////////////////////////////////////////////////////////////////////
298
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800299class AudioConfigPrimaryTest : public AudioPatchPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700300 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800301 // Cache result ?
302 static const vector<AudioConfig> getRequiredSupportPlaybackAudioConfig() {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700303 return combineAudioConfig(
304 {AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
305 {8000, 11025, 16000, 22050, 32000, 44100},
306 {AudioFormat::PCM_16_BIT});
Kevin Rocardf0357882017-02-10 16:19:28 -0800307 }
308
Kevin Rocard72e50e22017-05-05 14:02:55 -0700309 static const vector<AudioConfig>
310 getRecommendedSupportPlaybackAudioConfig() {
311 return combineAudioConfig(
312 {AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
313 {24000, 48000}, {AudioFormat::PCM_16_BIT});
Kevin Rocardf0357882017-02-10 16:19:28 -0800314 }
315
316 static const vector<AudioConfig> getSupportedPlaybackAudioConfig() {
317 // TODO: retrieve audio config supported by the platform
318 // as declared in the policy configuration
319 return {};
320 }
321
322 static const vector<AudioConfig> getRequiredSupportCaptureAudioConfig() {
323 return combineAudioConfig({AudioChannelMask::IN_MONO},
324 {8000, 11025, 16000, 44100},
325 {AudioFormat::PCM_16_BIT});
326 }
327 static const vector<AudioConfig> getRecommendedSupportCaptureAudioConfig() {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700328 return combineAudioConfig({AudioChannelMask::IN_STEREO}, {22050, 48000},
Kevin Rocardf0357882017-02-10 16:19:28 -0800329 {AudioFormat::PCM_16_BIT});
330 }
331 static const vector<AudioConfig> getSupportedCaptureAudioConfig() {
332 // TODO: retrieve audio config supported by the platform
333 // as declared in the policy configuration
334 return {};
335 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700336
337 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800338 static const vector<AudioConfig> combineAudioConfig(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700339 vector<AudioChannelMask> channelMasks, vector<uint32_t> sampleRates,
340 vector<AudioFormat> formats) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800341 vector<AudioConfig> configs;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700342 for (auto channelMask : channelMasks) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800343 for (auto sampleRate : sampleRates) {
344 for (auto format : formats) {
345 AudioConfig config{};
346 // leave offloadInfo to 0
347 config.channelMask = channelMask;
348 config.sampleRateHz = sampleRate;
349 config.format = format;
350 // FIXME: leave frameCount to 0 ?
351 configs.push_back(config);
352 }
353 }
354 }
355 return configs;
356 }
357};
358
Kevin Rocard9c369142017-03-08 17:17:25 -0800359/** Generate a test name based on an audio config.
360 *
361 * As the only parameter changing are channel mask and sample rate,
362 * only print those ones in the test name.
363 */
Kevin Rocard72e50e22017-05-05 14:02:55 -0700364static string generateTestName(
365 const testing::TestParamInfo<AudioConfig>& info) {
Kevin Rocard9c369142017-03-08 17:17:25 -0800366 const AudioConfig& config = info.param;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700367 return to_string(info.index) + "__" + to_string(config.sampleRateHz) + "_" +
368 // "MONO" is more clear than "FRONT_LEFT"
369 ((config.channelMask == AudioChannelMask::OUT_MONO ||
370 config.channelMask == AudioChannelMask::IN_MONO)
371 ? "MONO"
372 : toString(config.channelMask));
Kevin Rocard9c369142017-03-08 17:17:25 -0800373}
374
Kevin Rocardf0357882017-02-10 16:19:28 -0800375//////////////////////////////////////////////////////////////////////////////
376///////////////////////////// getInputBufferSize /////////////////////////////
377//////////////////////////////////////////////////////////////////////////////
378
Kevin Rocard72e50e22017-05-05 14:02:55 -0700379// FIXME: execute input test only if platform declares
380// android.hardware.microphone
Kevin Rocardf0357882017-02-10 16:19:28 -0800381// how to get this value ? is it a property ???
382
Kevin Rocard72e50e22017-05-05 14:02:55 -0700383class AudioCaptureConfigPrimaryTest
384 : public AudioConfigPrimaryTest,
385 public ::testing::WithParamInterface<AudioConfig> {
386 protected:
387 void inputBufferSizeTest(const AudioConfig& audioConfig,
388 bool supportRequired) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800389 uint64_t bufferSize;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700390 ASSERT_OK(
391 device->getInputBufferSize(audioConfig, returnIn(res, bufferSize)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800392
393 switch (res) {
394 case Result::INVALID_ARGUMENTS:
395 EXPECT_FALSE(supportRequired);
396 break;
397 case Result::OK:
398 // Check that the buffer is of a sane size
399 // For now only that it is > 0
400 EXPECT_GT(bufferSize, uint64_t(0));
401 break;
402 default:
Kevin Rocard72e50e22017-05-05 14:02:55 -0700403 FAIL() << "Invalid return status: "
404 << ::testing::PrintToString(res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800405 }
406 }
407};
408
Kevin Rocard72e50e22017-05-05 14:02:55 -0700409// Test that the required capture config and those declared in the policy are
410// indeed supported
Kevin Rocardf0357882017-02-10 16:19:28 -0800411class RequiredInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
412TEST_P(RequiredInputBufferSizeTest, RequiredInputBufferSizeTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700413 doc::test(
414 "Input buffer size must be retrievable for a format with required "
415 "support.");
Kevin Rocardf0357882017-02-10 16:19:28 -0800416 inputBufferSizeTest(GetParam(), true);
417}
418INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700419 RequiredInputBufferSize, RequiredInputBufferSizeTest,
420 ::testing::ValuesIn(
421 AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
422 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800423INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700424 SupportedInputBufferSize, RequiredInputBufferSizeTest,
425 ::testing::ValuesIn(
426 AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
427 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800428
Kevin Rocard72e50e22017-05-05 14:02:55 -0700429// Test that the recommended capture config are supported or lead to a
430// INVALID_ARGUMENTS return
Kevin Rocardf0357882017-02-10 16:19:28 -0800431class OptionalInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
432TEST_P(OptionalInputBufferSizeTest, OptionalInputBufferSizeTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700433 doc::test(
434 "Input buffer size should be retrievable for a format with recommended "
435 "support.");
Kevin Rocardf0357882017-02-10 16:19:28 -0800436 inputBufferSizeTest(GetParam(), false);
437}
438INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700439 RecommendedCaptureAudioConfigSupport, OptionalInputBufferSizeTest,
440 ::testing::ValuesIn(
441 AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
442 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800443
444//////////////////////////////////////////////////////////////////////////////
445/////////////////////////////// setScreenState ///////////////////////////////
446//////////////////////////////////////////////////////////////////////////////
447
448TEST_F(AudioPrimaryHidlTest, setScreenState) {
449 doc::test("Check that the hal can receive the screen state");
450 for (bool turnedOn : {false, true, true, false, false}) {
451 auto ret = device->setScreenState(turnedOn);
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700452 ASSERT_IS_OK(ret);
Kevin Rocardf0357882017-02-10 16:19:28 -0800453 Result result = ret;
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700454 auto okOrNotSupported = {Result::OK, Result::NOT_SUPPORTED};
455 ASSERT_RESULT(okOrNotSupported, result);
Kevin Rocardf0357882017-02-10 16:19:28 -0800456 }
457}
458
459//////////////////////////////////////////////////////////////////////////////
460//////////////////////////// {get,set}Parameters /////////////////////////////
461//////////////////////////////////////////////////////////////////////////////
462
463TEST_F(AudioPrimaryHidlTest, getParameters) {
464 doc::test("Check that the hal can set and get parameters");
465 hidl_vec<hidl_string> keys;
466 hidl_vec<ParameterValue> values;
467 ASSERT_OK(device->getParameters(keys, returnIn(res, values)));
468 ASSERT_OK(device->setParameters(values));
469 values.resize(0);
470 ASSERT_OK(device->setParameters(values));
471}
472
473//////////////////////////////////////////////////////////////////////////////
474//////////////////////////////// debugDebug //////////////////////////////////
475//////////////////////////////////////////////////////////////////////////////
476
Kevin Rocardb9031242017-03-13 12:20:54 -0700477template <class DebugDump>
478static void testDebugDump(DebugDump debugDump) {
Tri Vo6c00ac32017-10-12 15:47:32 -0700479 // File descriptors to our pipe. fds[0] corresponds to the read end and
480 // fds[1] to the write end.
481 int fds[2];
482 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
Kevin Rocardf0357882017-02-10 16:19:28 -0800483
Tri Vo7d35a3d2017-10-17 11:07:29 -0700484 // Make sure that the pipe is at least 1 MB in size. The test process runs
485 // in su domain, so it should be safe to make this call.
486 fcntl(fds[0], F_SETPIPE_SZ, 1 << 20);
487
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700488 // Wrap the temporary file file descriptor in a native handle
Kevin Rocardf0357882017-02-10 16:19:28 -0800489 auto* nativeHandle = native_handle_create(1, 0);
490 ASSERT_NE(nullptr, nativeHandle);
Tri Vo6c00ac32017-10-12 15:47:32 -0700491 nativeHandle->data[0] = fds[1];
Kevin Rocardf0357882017-02-10 16:19:28 -0800492
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700493 // Wrap this native handle in a hidl handle
Kevin Rocardf0357882017-02-10 16:19:28 -0800494 hidl_handle handle;
Tri Vo6c00ac32017-10-12 15:47:32 -0700495 handle.setTo(nativeHandle, false /*take ownership*/);
Kevin Rocardf0357882017-02-10 16:19:28 -0800496
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700497 ASSERT_OK(debugDump(handle));
498
499 // Check that at least one bit was written by the hal
Kevin Rocardee771e92017-03-08 17:17:25 -0800500 // TODO: debugDump does not return a Result.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700501 // This mean that the hal can not report that it not implementing the
502 // function.
Kevin Rocardf0357882017-02-10 16:19:28 -0800503 char buff;
Tri Vo6c00ac32017-10-12 15:47:32 -0700504 if (read(fds[0], &buff, 1) != 1) {
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700505 doc::note("debugDump does not seem implemented");
506 }
Tri Vo6c00ac32017-10-12 15:47:32 -0700507 EXPECT_EQ(0, close(fds[0])) << errno;
508 EXPECT_EQ(0, close(fds[1])) << errno;
Kevin Rocardf0357882017-02-10 16:19:28 -0800509}
510
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700511TEST_F(AudioPrimaryHidlTest, DebugDump) {
Kevin Rocardb9031242017-03-13 12:20:54 -0700512 doc::test("Check that the hal can dump its state without error");
Chih-Hung Hsieh7eb57b62017-05-15 12:31:25 -0700513 testDebugDump([](const auto& handle) { return device->debugDump(handle); });
Kevin Rocardb9031242017-03-13 12:20:54 -0700514}
515
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700516TEST_F(AudioPrimaryHidlTest, DebugDumpInvalidArguments) {
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700517 doc::test("Check that the hal dump doesn't crash on invalid arguments");
518 ASSERT_OK(device->debugDump(hidl_handle()));
519}
520
Kevin Rocardf0357882017-02-10 16:19:28 -0800521//////////////////////////////////////////////////////////////////////////////
522////////////////////////// open{Output,Input}Stream //////////////////////////
523//////////////////////////////////////////////////////////////////////////////
524
525template <class Stream>
526class OpenStreamTest : public AudioConfigPrimaryTest,
527 public ::testing::WithParamInterface<AudioConfig> {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700528 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800529 template <class Open>
530 void testOpen(Open openStream, const AudioConfig& config) {
531 // FIXME: Open a stream without an IOHandle
532 // This is not required to be accepted by hal implementations
Kevin Rocard72e50e22017-05-05 14:02:55 -0700533 AudioIoHandle ioHandle =
534 (AudioIoHandle)AudioHandleConsts::AUDIO_IO_HANDLE_NONE;
Kevin Rocardf0357882017-02-10 16:19:28 -0800535 AudioConfig suggestedConfig{};
Kevin Rocard72e50e22017-05-05 14:02:55 -0700536 ASSERT_OK(openStream(ioHandle, config,
537 returnIn(res, stream, suggestedConfig)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800538
539 // TODO: only allow failure for RecommendedPlaybackAudioConfig
540 switch (res) {
541 case Result::OK:
542 ASSERT_TRUE(stream != nullptr);
543 audioConfig = config;
544 break;
545 case Result::INVALID_ARGUMENTS:
546 ASSERT_TRUE(stream == nullptr);
547 AudioConfig suggestedConfigRetry;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700548 // Could not open stream with config, try again with the
549 // suggested one
550 ASSERT_OK(
551 openStream(ioHandle, suggestedConfig,
552 returnIn(res, stream, suggestedConfigRetry)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800553 // This time it must succeed
554 ASSERT_OK(res);
Kevin Rocard4aefd1c2017-05-02 18:58:58 -0700555 ASSERT_TRUE(stream != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800556 audioConfig = suggestedConfig;
557 break;
558 default:
Kevin Rocard72e50e22017-05-05 14:02:55 -0700559 FAIL() << "Invalid return status: "
560 << ::testing::PrintToString(res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800561 }
562 open = true;
563 }
564
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800565 Return<Result> closeStream() {
566 open = false;
567 return stream->close();
568 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700569
570 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800571 void TearDown() override {
572 if (open) {
573 ASSERT_OK(stream->close());
574 }
575 }
576
Kevin Rocard72e50e22017-05-05 14:02:55 -0700577 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800578 AudioConfig audioConfig;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800579 DeviceAddress address = {};
Kevin Rocardf0357882017-02-10 16:19:28 -0800580 sp<Stream> stream;
581 bool open = false;
582};
583
584////////////////////////////// openOutputStream //////////////////////////////
585
586class OutputStreamTest : public OpenStreamTest<IStreamOut> {
587 virtual void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700588 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800589 address.device = AudioDevice::OUT_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800590 const AudioConfig& config = GetParam();
Kevin Rocard72e50e22017-05-05 14:02:55 -0700591 AudioOutputFlag flags =
592 AudioOutputFlag::NONE; // TODO: test all flag combination
593 testOpen(
594 [&](AudioIoHandle handle, AudioConfig config, auto cb) {
595 return device->openOutputStream(handle, address, config, flags,
596 cb);
597 },
598 config);
Kevin Rocardf0357882017-02-10 16:19:28 -0800599 }
600};
601TEST_P(OutputStreamTest, OpenOutputStreamTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700602 doc::test(
603 "Check that output streams can be open with the required and "
604 "recommended config");
Kevin Rocardf0357882017-02-10 16:19:28 -0800605 // Open done in SetUp
606}
607INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700608 RequiredOutputStreamConfigSupport, OutputStreamTest,
609 ::testing::ValuesIn(
610 AudioConfigPrimaryTest::getRequiredSupportPlaybackAudioConfig()),
611 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800612INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700613 SupportedOutputStreamConfig, OutputStreamTest,
614 ::testing::ValuesIn(
615 AudioConfigPrimaryTest::getSupportedPlaybackAudioConfig()),
616 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800617
618INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700619 RecommendedOutputStreamConfigSupport, OutputStreamTest,
620 ::testing::ValuesIn(
621 AudioConfigPrimaryTest::getRecommendedSupportPlaybackAudioConfig()),
622 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800623
624////////////////////////////// openInputStream //////////////////////////////
625
626class InputStreamTest : public OpenStreamTest<IStreamIn> {
Kevin Rocardf0357882017-02-10 16:19:28 -0800627 virtual void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700628 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800629 address.device = AudioDevice::IN_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800630 const AudioConfig& config = GetParam();
Kevin Rocard72e50e22017-05-05 14:02:55 -0700631 AudioInputFlag flags =
632 AudioInputFlag::NONE; // TODO: test all flag combination
633 AudioSource source =
634 AudioSource::DEFAULT; // TODO: test all flag combination
635 testOpen(
636 [&](AudioIoHandle handle, AudioConfig config, auto cb) {
637 return device->openInputStream(handle, address, config, flags,
638 source, cb);
639 },
640 config);
Kevin Rocardf0357882017-02-10 16:19:28 -0800641 }
642};
643
644TEST_P(InputStreamTest, OpenInputStreamTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700645 doc::test(
646 "Check that input streams can be open with the required and "
647 "recommended config");
Kevin Rocardf0357882017-02-10 16:19:28 -0800648 // Open done in setup
649}
650INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700651 RequiredInputStreamConfigSupport, InputStreamTest,
652 ::testing::ValuesIn(
653 AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
654 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800655INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700656 SupportedInputStreamConfig, InputStreamTest,
657 ::testing::ValuesIn(
658 AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
659 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800660
661INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700662 RecommendedInputStreamConfigSupport, InputStreamTest,
663 ::testing::ValuesIn(
664 AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
665 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800666
667//////////////////////////////////////////////////////////////////////////////
668////////////////////////////// IStream getters ///////////////////////////////
669//////////////////////////////////////////////////////////////////////////////
670
671/** Unpack the provided result.
672 * If the result is not OK, register a failure and return an undefined value. */
673template <class R>
674static R extract(Return<R> ret) {
675 if (!ret.isOk()) {
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700676 EXPECT_IS_OK(ret);
Kevin Rocardf0357882017-02-10 16:19:28 -0800677 return R{};
678 }
679 return ret;
680}
681
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800682/* Could not find a way to write a test for two parametrized class fixure
683 * thus use this macro do duplicate tests for Input and Output stream */
684#define TEST_IO_STREAM(test_name, documentation, code) \
Kevin Rocard72e50e22017-05-05 14:02:55 -0700685 TEST_P(InputStreamTest, test_name) { \
686 doc::test(documentation); \
687 code; \
688 } \
689 TEST_P(OutputStreamTest, test_name) { \
690 doc::test(documentation); \
691 code; \
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800692 }
693
Kevin Rocard72e50e22017-05-05 14:02:55 -0700694TEST_IO_STREAM(
695 GetFrameCount,
696 "Check that the stream frame count == the one it was opened with",
697 ASSERT_EQ(audioConfig.frameCount, extract(stream->getFrameCount())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800698
Kevin Rocard07fb7362017-05-26 17:23:07 -0700699TEST_IO_STREAM(GetSampleRate, "Check that the stream sample rate == the one it was opened with",
700 ASSERT_EQ(audioConfig.sampleRateHz, extract(stream->getSampleRate())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800701
Kevin Rocard07fb7362017-05-26 17:23:07 -0700702TEST_IO_STREAM(GetChannelMask, "Check that the stream channel mask == the one it was opened with",
703 ASSERT_EQ(audioConfig.channelMask, extract(stream->getChannelMask())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800704
Kevin Rocard72e50e22017-05-05 14:02:55 -0700705TEST_IO_STREAM(GetFormat,
706 "Check that the stream format == the one it was opened with",
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800707 ASSERT_EQ(audioConfig.format, extract(stream->getFormat())))
708
709// TODO: for now only check that the framesize is not incoherent
Kevin Rocard72e50e22017-05-05 14:02:55 -0700710TEST_IO_STREAM(GetFrameSize,
711 "Check that the stream frame size == the one it was opened with",
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800712 ASSERT_GT(extract(stream->getFrameSize()), 0U))
713
Kevin Rocard72e50e22017-05-05 14:02:55 -0700714TEST_IO_STREAM(GetBufferSize,
715 "Check that the stream buffer size== the one it was opened with",
716 ASSERT_GE(extract(stream->getBufferSize()),
717 extract(stream->getFrameSize())));
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800718
Kevin Rocardf0357882017-02-10 16:19:28 -0800719template <class Property, class CapabilityGetter, class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700720static void testCapabilityGetter(const string& name, IStream* stream,
721 Property currentValue,
722 CapabilityGetter capablityGetter,
723 Getter getter, Setter setter) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800724 hidl_vec<Property> capabilities;
725 ASSERT_OK((stream->*capablityGetter)(returnIn(capabilities)));
726 if (capabilities.size() == 0) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700727 // The default hal should probably return a NOT_SUPPORTED if the hal
728 // does not expose
729 // capability retrieval. For now it returns an empty list if not
730 // implemented
Kevin Rocardf0357882017-02-10 16:19:28 -0800731 doc::partialTest(name + " is not supported");
732 return;
733 };
Kevin Rocard72e50e22017-05-05 14:02:55 -0700734 // TODO: This code has never been tested on a hal that supports
735 // getSupportedSampleRates
Kevin Rocardf0357882017-02-10 16:19:28 -0800736 EXPECT_NE(std::find(capabilities.begin(), capabilities.end(), currentValue),
Kevin Rocard72e50e22017-05-05 14:02:55 -0700737 capabilities.end())
Kevin Rocardf0357882017-02-10 16:19:28 -0800738 << "current " << name << " is not in the list of the supported ones "
739 << toString(capabilities);
740
741 // Check that all declared supported values are indeed supported
742 for (auto capability : capabilities) {
743 ASSERT_OK((stream->*setter)(capability));
744 ASSERT_EQ(capability, extract((stream->*getter)()));
745 }
746}
747
Kevin Rocard72e50e22017-05-05 14:02:55 -0700748TEST_IO_STREAM(SupportedSampleRate,
749 "Check that the stream sample rate is declared as supported",
750 testCapabilityGetter("getSupportedSampleRate", stream.get(),
751 extract(stream->getSampleRate()),
752 &IStream::getSupportedSampleRates,
753 &IStream::getSampleRate,
754 &IStream::setSampleRate))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800755
Kevin Rocard72e50e22017-05-05 14:02:55 -0700756TEST_IO_STREAM(SupportedChannelMask,
757 "Check that the stream channel mask is declared as supported",
758 testCapabilityGetter("getSupportedChannelMask", stream.get(),
759 extract(stream->getChannelMask()),
760 &IStream::getSupportedChannelMasks,
761 &IStream::getChannelMask,
762 &IStream::setChannelMask))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800763
Kevin Rocard72e50e22017-05-05 14:02:55 -0700764TEST_IO_STREAM(SupportedFormat,
765 "Check that the stream format is declared as supported",
766 testCapabilityGetter("getSupportedFormat", stream.get(),
767 extract(stream->getFormat()),
768 &IStream::getSupportedFormats,
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800769 &IStream::getFormat, &IStream::setFormat))
770
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800771static void testGetDevice(IStream* stream, AudioDevice expectedDevice) {
Kevin Rocard8f8730c2017-05-02 19:34:29 -0700772 // Unfortunately the interface does not allow the implementation to return
773 // NOT_SUPPORTED
774 // Thus allow NONE as signaling that the call is not supported.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800775 auto ret = stream->getDevice();
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700776 ASSERT_IS_OK(ret);
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800777 AudioDevice device = ret;
Kevin Rocard8f8730c2017-05-02 19:34:29 -0700778 ASSERT_TRUE(device == expectedDevice || device == AudioDevice::NONE)
779 << "Expected: " << ::testing::PrintToString(expectedDevice)
780 << "\n Actual: " << ::testing::PrintToString(device);
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800781}
782
Kevin Rocard72e50e22017-05-05 14:02:55 -0700783TEST_IO_STREAM(GetDevice,
784 "Check that the stream device == the one it was opened with",
785 areAudioPatchesSupported()
786 ? doc::partialTest("Audio patches are supported")
787 : testGetDevice(stream.get(), address.device))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800788
789static void testSetDevice(IStream* stream, const DeviceAddress& address) {
790 DeviceAddress otherAddress = address;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700791 otherAddress.device = (address.device & AudioDevice::BIT_IN) == 0
792 ? AudioDevice::OUT_SPEAKER
793 : AudioDevice::IN_BUILTIN_MIC;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800794 EXPECT_OK(stream->setDevice(otherAddress));
795
Kevin Rocard72e50e22017-05-05 14:02:55 -0700796 ASSERT_OK(stream->setDevice(address)); // Go back to the original value
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800797}
798
Kevin Rocard72e50e22017-05-05 14:02:55 -0700799TEST_IO_STREAM(
800 SetDevice,
801 "Check that the stream can be rerouted to SPEAKER or BUILTIN_MIC",
802 areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported")
803 : testSetDevice(stream.get(), address))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800804
Kevin Rocard07fb7362017-05-26 17:23:07 -0700805static void testGetAudioProperties(IStream* stream, AudioConfig expectedConfig) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800806 uint32_t sampleRateHz;
807 AudioChannelMask mask;
808 AudioFormat format;
Kevin Rocard07fb7362017-05-26 17:23:07 -0700809
Kevin Rocardf0357882017-02-10 16:19:28 -0800810 stream->getAudioProperties(returnIn(sampleRateHz, mask, format));
Kevin Rocard07fb7362017-05-26 17:23:07 -0700811
812 // FIXME: the qcom hal it does not currently negotiate the sampleRate &
813 // channel mask
814 EXPECT_EQ(expectedConfig.sampleRateHz, sampleRateHz);
815 EXPECT_EQ(expectedConfig.channelMask, mask);
816 EXPECT_EQ(expectedConfig.format, format);
Kevin Rocardf0357882017-02-10 16:19:28 -0800817}
818
Kevin Rocard07fb7362017-05-26 17:23:07 -0700819TEST_IO_STREAM(GetAudioProperties,
820 "Check that the stream audio properties == the ones it was opened with",
821 testGetAudioProperties(stream.get(), audioConfig))
Kevin Rocardf0357882017-02-10 16:19:28 -0800822
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800823static void testConnectedState(IStream* stream) {
824 DeviceAddress address = {};
825 using AD = AudioDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700826 for (auto device :
827 {AD::OUT_HDMI, AD::OUT_WIRED_HEADPHONE, AD::IN_USB_HEADSET}) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800828 address.device = device;
829
830 ASSERT_OK(stream->setConnectedState(address, true));
831 ASSERT_OK(stream->setConnectedState(address, false));
832 }
833}
834TEST_IO_STREAM(SetConnectedState,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700835 "Check that the stream can be notified of device connection and "
836 "deconnection",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800837 testConnectedState(stream.get()))
838
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700839static auto invalidArgsOrNotSupportedOrOK = {Result::INVALID_ARGUMENTS,
840 Result::NOT_SUPPORTED, Result::OK};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800841TEST_IO_STREAM(SetHwAvSync, "Try to set hardware sync to an invalid value",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700842 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700843 stream->setHwAvSync(666)))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800844
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700845TEST_IO_STREAM(GetHwAvSync, "Get hardware sync can not fail",
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700846 ASSERT_IS_OK(device->getHwAvSync()));
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700847
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700848static void checkGetNoParameter(IStream* stream, hidl_vec<hidl_string> keys,
849 vector<Result> expectedResults) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800850 hidl_vec<ParameterValue> parameters;
851 Result res;
852 ASSERT_OK(stream->getParameters(keys, returnIn(res, parameters)));
853 ASSERT_RESULT(expectedResults, res);
854 if (res == Result::OK) {
Kevin Rocard4aefd1c2017-05-02 18:58:58 -0700855 for (auto& parameter : parameters) {
856 ASSERT_EQ(0U, parameter.value.size()) << toString(parameter);
857 }
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800858 }
859}
860
Kevin Rocard72e50e22017-05-05 14:02:55 -0700861/* Get/Set parameter is intended to be an opaque channel between vendors app and
862 * their HALs.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800863 * Thus can not be meaningfully tested.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800864 */
865TEST_IO_STREAM(getEmptySetParameter, "Retrieve the values of an empty set",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700866 checkGetNoParameter(stream.get(), {} /* keys */, {Result::OK}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800867
Kevin Rocard72e50e22017-05-05 14:02:55 -0700868TEST_IO_STREAM(getNonExistingParameter,
869 "Retrieve the values of an non existing parameter",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700870 checkGetNoParameter(stream.get(),
871 {"Non existing key"} /* keys */,
872 {Result::NOT_SUPPORTED}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800873
Kevin Rocard72e50e22017-05-05 14:02:55 -0700874TEST_IO_STREAM(setEmptySetParameter,
875 "Set the values of an empty set of parameters",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700876 ASSERT_RESULT(Result::OK, stream->setParameters({})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800877
Kevin Rocard72e50e22017-05-05 14:02:55 -0700878TEST_IO_STREAM(
879 setNonExistingParameter, "Set the values of an non existing parameter",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700880 // Unfortunately, the set_parameter legacy interface did not return any
881 // error code when a key is not supported.
882 // To allow implementation to just wrapped the legacy one, consider OK as a
883 // valid result for setting a non existing parameter.
884 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700885 stream->setParameters({{"non existing key", "0"}})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800886
Kevin Rocardb9031242017-03-13 12:20:54 -0700887TEST_IO_STREAM(DebugDump,
888 "Check that a stream can dump its state without error",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700889 testDebugDump([this](const auto& handle) {
890 return stream->debugDump(handle);
891 }))
Kevin Rocardb9031242017-03-13 12:20:54 -0700892
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700893TEST_IO_STREAM(DebugDumpInvalidArguments,
894 "Check that the stream dump doesn't crash on invalid arguments",
895 ASSERT_OK(stream->debugDump(hidl_handle())))
896
Kevin Rocardf0357882017-02-10 16:19:28 -0800897//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800898////////////////////////////// addRemoveEffect ///////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800899//////////////////////////////////////////////////////////////////////////////
900
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800901TEST_IO_STREAM(AddNonExistingEffect, "Adding a non existing effect should fail",
902 ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->addEffect(666)))
Kevin Rocard72e50e22017-05-05 14:02:55 -0700903TEST_IO_STREAM(RemoveNonExistingEffect,
904 "Removing a non existing effect should fail",
905 ASSERT_RESULT(Result::INVALID_ARGUMENTS,
906 stream->removeEffect(666)))
Kevin Rocardf0357882017-02-10 16:19:28 -0800907
Kevin Rocard72e50e22017-05-05 14:02:55 -0700908// TODO: positive tests
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800909
910//////////////////////////////////////////////////////////////////////////////
911/////////////////////////////// Control ////////////////////////////////
912//////////////////////////////////////////////////////////////////////////////
913
914TEST_IO_STREAM(standby, "Make sure the stream can be put in stanby",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700915 ASSERT_OK(stream->standby())) // can not fail
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800916
Kevin Rocard72e50e22017-05-05 14:02:55 -0700917static vector<Result> invalidStateOrNotSupported = {Result::INVALID_STATE,
918 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800919
Kevin Rocard72e50e22017-05-05 14:02:55 -0700920TEST_IO_STREAM(startNoMmap,
921 "Starting a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800922 ASSERT_RESULT(invalidStateOrNotSupported, stream->start()))
923
Kevin Rocard72e50e22017-05-05 14:02:55 -0700924TEST_IO_STREAM(stopNoMmap,
925 "Stopping a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800926 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
927
Kevin Rocard72e50e22017-05-05 14:02:55 -0700928TEST_IO_STREAM(getMmapPositionNoMmap,
929 "Get a stream Mmap position before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800930 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
931
Kevin Rocard72e50e22017-05-05 14:02:55 -0700932TEST_IO_STREAM(close, "Make sure a stream can be closed",
933 ASSERT_OK(closeStream()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800934TEST_IO_STREAM(closeTwice, "Make sure a stream can not be closed twice",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700935 ASSERT_OK(closeStream());
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800936 ASSERT_RESULT(Result::INVALID_STATE, closeStream()))
937
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700938static auto invalidArgsOrNotSupported = {Result::INVALID_ARGUMENTS,
939 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800940static void testCreateTooBigMmapBuffer(IStream* stream) {
941 MmapBufferInfo info;
942 Result res;
943 // Assume that int max is a value too big to be allocated
Kevin Rocard72e50e22017-05-05 14:02:55 -0700944 // This is true currently with a 32bit media server, but might not when it
945 // will run in 64 bit
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800946 auto minSizeFrames = std::numeric_limits<int32_t>::max();
947 ASSERT_OK(stream->createMmapBuffer(minSizeFrames, returnIn(res, info)));
948 ASSERT_RESULT(invalidArgsOrNotSupported, res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800949}
950
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800951TEST_IO_STREAM(CreateTooBigMmapBuffer, "Create mmap buffer too big should fail",
952 testCreateTooBigMmapBuffer(stream.get()))
953
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800954static void testGetMmapPositionOfNonMmapedStream(IStream* stream) {
955 Result res;
956 MmapPosition position;
957 ASSERT_OK(stream->getMmapPosition(returnIn(res, position)));
958 ASSERT_RESULT(invalidArgsOrNotSupported, res);
959}
960
Kevin Rocard72e50e22017-05-05 14:02:55 -0700961TEST_IO_STREAM(
962 GetMmapPositionOfNonMmapedStream,
963 "Retrieving the mmap position of a non mmaped stream should fail",
964 testGetMmapPositionOfNonMmapedStream(stream.get()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800965
Kevin Rocardf0357882017-02-10 16:19:28 -0800966//////////////////////////////////////////////////////////////////////////////
Kevin Rocardc9963522017-03-10 18:47:37 -0800967///////////////////////////////// StreamIn ///////////////////////////////////
968//////////////////////////////////////////////////////////////////////////////
969
970TEST_P(InputStreamTest, GetAudioSource) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700971 doc::test(
972 "Retrieving the audio source of an input stream should always succeed");
Kevin Rocardc9963522017-03-10 18:47:37 -0800973 AudioSource source;
974 ASSERT_OK(stream->getAudioSource(returnIn(res, source)));
Kevin Rocard98390a62017-05-03 11:16:05 -0700975 if (res == Result::NOT_SUPPORTED) {
976 doc::partialTest("getAudioSource is not supported");
977 return;
978 }
Kevin Rocardc9963522017-03-10 18:47:37 -0800979 ASSERT_OK(res);
980 ASSERT_EQ(AudioSource::DEFAULT, source);
981}
982
Kevin Rocard72e50e22017-05-05 14:02:55 -0700983static void testUnitaryGain(std::function<Return<Result>(float)> setGain) {
Kevin Rocardc4f1b2f2017-05-03 11:19:25 -0700984 for (float value :
985 (float[]){-INFINITY, -1.0, 1.0 + std::numeric_limits<float>::epsilon(),
986 2.0, INFINITY, NAN}) {
Kevin Rocarda1d6ea42017-05-08 17:08:11 -0700987 EXPECT_RESULT(Result::INVALID_ARGUMENTS, setGain(value)) << "value="
988 << value;
Kevin Rocardc9963522017-03-10 18:47:37 -0800989 }
Kevin Rocardc4f1b2f2017-05-03 11:19:25 -0700990 // Do not consider -0.0 as an invalid value as it is == with 0.0
991 for (float value : {-0.0, 0.0, 0.01, 0.5, 0.09, 1.0 /* Restore volume*/}) {
Kevin Rocarda1d6ea42017-05-08 17:08:11 -0700992 EXPECT_OK(setGain(value)) << "value=" << value;
Kevin Rocardc4f1b2f2017-05-03 11:19:25 -0700993 }
Kevin Rocardc9963522017-03-10 18:47:37 -0800994}
995
Kevin Rocard98390a62017-05-03 11:16:05 -0700996static void testOptionalUnitaryGain(
997 std::function<Return<Result>(float)> setGain, string debugName) {
998 auto result = setGain(1);
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700999 ASSERT_IS_OK(result);
Kevin Rocard98390a62017-05-03 11:16:05 -07001000 if (result == Result::NOT_SUPPORTED) {
1001 doc::partialTest(debugName + " is not supported");
1002 return;
1003 }
1004 testUnitaryGain(setGain);
1005}
1006
Kevin Rocardc9963522017-03-10 18:47:37 -08001007TEST_P(InputStreamTest, SetGain) {
1008 doc::test("The gain of an input stream should only be set between [0,1]");
Kevin Rocard98390a62017-05-03 11:16:05 -07001009 testOptionalUnitaryGain(
1010 [this](float volume) { return stream->setGain(volume); },
1011 "InputStream::setGain");
Kevin Rocardc9963522017-03-10 18:47:37 -08001012}
1013
Kevin Rocard72e50e22017-05-05 14:02:55 -07001014static void testPrepareForReading(IStreamIn* stream, uint32_t frameSize,
1015 uint32_t framesCount) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001016 Result res;
1017 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001018 ASSERT_OK(stream->prepareForReading(
1019 frameSize, framesCount,
1020 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocardfcf186b2017-05-03 11:16:05 -07001021 EXPECT_RESULT(Result::INVALID_ARGUMENTS, res);
Kevin Rocardc9963522017-03-10 18:47:37 -08001022}
1023
Kevin Rocard195205b2017-05-02 18:34:59 -07001024TEST_P(InputStreamTest, PrepareForReadingWithZeroBuffer) {
1025 doc::test(
1026 "Preparing a stream for reading with a 0 sized buffer should fail");
1027 testPrepareForReading(stream.get(), 0, 0);
1028}
1029
Kevin Rocardc9963522017-03-10 18:47:37 -08001030TEST_P(InputStreamTest, PrepareForReadingWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001031 doc::test(
1032 "Preparing a stream for reading with a 2^32 sized buffer should fail");
1033 testPrepareForReading(stream.get(), 1,
1034 std::numeric_limits<uint32_t>::max());
Kevin Rocardc9963522017-03-10 18:47:37 -08001035}
1036
1037TEST_P(InputStreamTest, PrepareForReadingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001038 doc::test(
1039 "Preparing a stream for reading with a overflowing sized buffer should "
1040 "fail");
Kevin Rocardc9963522017-03-10 18:47:37 -08001041 auto uintMax = std::numeric_limits<uint32_t>::max();
1042 testPrepareForReading(stream.get(), uintMax, uintMax);
1043}
1044
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001045TEST_P(InputStreamTest, GetInputFramesLost) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001046 doc::test(
1047 "The number of frames lost on a never started stream should be 0");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001048 auto ret = stream->getInputFramesLost();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001049 ASSERT_IS_OK(ret);
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001050 uint32_t framesLost{ret};
1051 ASSERT_EQ(0U, framesLost);
1052}
1053
Kevin Rocardc9963522017-03-10 18:47:37 -08001054TEST_P(InputStreamTest, getCapturePosition) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001055 doc::test(
1056 "The capture position of a non prepared stream should not be "
1057 "retrievable");
Kevin Rocardc9963522017-03-10 18:47:37 -08001058 uint64_t frames;
1059 uint64_t time;
1060 ASSERT_OK(stream->getCapturePosition(returnIn(res, frames, time)));
1061 ASSERT_RESULT(invalidStateOrNotSupported, res);
1062}
1063
1064//////////////////////////////////////////////////////////////////////////////
Kevin Rocard624800c2017-03-10 18:47:37 -08001065///////////////////////////////// StreamIn ///////////////////////////////////
1066//////////////////////////////////////////////////////////////////////////////
1067
1068TEST_P(OutputStreamTest, getLatency) {
1069 doc::test("Make sure latency is over 0");
1070 auto result = stream->getLatency();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001071 ASSERT_IS_OK(result);
Kevin Rocard624800c2017-03-10 18:47:37 -08001072 ASSERT_GT(result, 0U);
1073}
1074
1075TEST_P(OutputStreamTest, setVolume) {
1076 doc::test("Try to set the output volume");
Kevin Rocard98390a62017-05-03 11:16:05 -07001077 testOptionalUnitaryGain(
1078 [this](float volume) { return stream->setVolume(volume, volume); },
1079 "setVolume");
Kevin Rocard624800c2017-03-10 18:47:37 -08001080}
1081
Kevin Rocard72e50e22017-05-05 14:02:55 -07001082static void testPrepareForWriting(IStreamOut* stream, uint32_t frameSize,
1083 uint32_t framesCount) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001084 Result res;
1085 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001086 ASSERT_OK(stream->prepareForWriting(
1087 frameSize, framesCount,
1088 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocardfcf186b2017-05-03 11:16:05 -07001089 EXPECT_RESULT(Result::INVALID_ARGUMENTS, res);
Kevin Rocard624800c2017-03-10 18:47:37 -08001090}
1091
Kevin Rocard195205b2017-05-02 18:34:59 -07001092TEST_P(OutputStreamTest, PrepareForWriteWithZeroBuffer) {
1093 doc::test(
1094 "Preparing a stream for writing with a 0 sized buffer should fail");
1095 testPrepareForWriting(stream.get(), 0, 0);
1096}
1097
Kevin Rocard624800c2017-03-10 18:47:37 -08001098TEST_P(OutputStreamTest, PrepareForWriteWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001099 doc::test(
1100 "Preparing a stream for writing with a 2^32 sized buffer should fail");
1101 testPrepareForWriting(stream.get(), 1,
1102 std::numeric_limits<uint32_t>::max());
Kevin Rocard624800c2017-03-10 18:47:37 -08001103}
1104
1105TEST_P(OutputStreamTest, PrepareForWritingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001106 doc::test(
1107 "Preparing a stream for writing with a overflowing sized buffer should "
1108 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001109 auto uintMax = std::numeric_limits<uint32_t>::max();
1110 testPrepareForWriting(stream.get(), uintMax, uintMax);
1111}
1112
1113struct Capability {
1114 Capability(IStreamOut* stream) {
1115 EXPECT_OK(stream->supportsPauseAndResume(returnIn(pause, resume)));
1116 auto ret = stream->supportsDrain();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001117 EXPECT_IS_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001118 if (ret.isOk()) {
1119 drain = ret;
1120 }
1121 }
1122 bool pause = false;
1123 bool resume = false;
1124 bool drain = false;
1125};
1126
1127TEST_P(OutputStreamTest, SupportsPauseAndResumeAndDrain) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001128 doc::test(
1129 "Implementation must expose pause, resume and drain capabilities");
Kevin Rocard624800c2017-03-10 18:47:37 -08001130 Capability(stream.get());
1131}
1132
Kevin Rocard304b6c82017-05-03 10:57:06 -07001133template <class Value>
1134static void checkInvalidStateOr0(Result res, Value value) {
1135 switch (res) {
1136 case Result::INVALID_STATE:
1137 break;
1138 case Result::OK:
1139 ASSERT_EQ(0U, value);
1140 break;
1141 default:
1142 FAIL() << "Unexpected result " << toString(res);
1143 }
1144}
1145
Kevin Rocard624800c2017-03-10 18:47:37 -08001146TEST_P(OutputStreamTest, GetRenderPosition) {
Kevin Rocard304b6c82017-05-03 10:57:06 -07001147 doc::test("A new stream render position should be 0 or INVALID_STATE");
Kevin Rocard624800c2017-03-10 18:47:37 -08001148 uint32_t dspFrames;
1149 ASSERT_OK(stream->getRenderPosition(returnIn(res, dspFrames)));
1150 if (res == Result::NOT_SUPPORTED) {
1151 doc::partialTest("getRenderPosition is not supported");
1152 return;
1153 }
Kevin Rocard304b6c82017-05-03 10:57:06 -07001154 checkInvalidStateOr0(res, dspFrames);
Kevin Rocard624800c2017-03-10 18:47:37 -08001155}
1156
1157TEST_P(OutputStreamTest, GetNextWriteTimestamp) {
Kevin Rocard304b6c82017-05-03 10:57:06 -07001158 doc::test("A new stream next write timestamp should be 0 or INVALID_STATE");
Kevin Rocard624800c2017-03-10 18:47:37 -08001159 uint64_t timestampUs;
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001160 ASSERT_OK(stream->getNextWriteTimestamp(returnIn(res, timestampUs)));
Kevin Rocard624800c2017-03-10 18:47:37 -08001161 if (res == Result::NOT_SUPPORTED) {
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001162 doc::partialTest("getNextWriteTimestamp is not supported");
Kevin Rocard624800c2017-03-10 18:47:37 -08001163 return;
1164 }
Kevin Rocard304b6c82017-05-03 10:57:06 -07001165 checkInvalidStateOr0(res, timestampUs);
Kevin Rocard624800c2017-03-10 18:47:37 -08001166}
1167
1168/** Stub implementation of out stream callback. */
1169class MockOutCallbacks : public IStreamOutCallback {
1170 Return<void> onWriteReady() override { return {}; }
1171 Return<void> onDrainReady() override { return {}; }
1172 Return<void> onError() override { return {}; }
1173};
1174
Kevin Rocard72e50e22017-05-05 14:02:55 -07001175static bool isAsyncModeSupported(IStreamOut* stream) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001176 auto res = stream->setCallback(new MockOutCallbacks);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001177 stream->clearCallback(); // try to restore the no callback state, ignore
1178 // any error
1179 auto okOrNotSupported = {Result::OK, Result::NOT_SUPPORTED};
Kevin Rocard624800c2017-03-10 18:47:37 -08001180 EXPECT_RESULT(okOrNotSupported, res);
1181 return res.isOk() ? res == Result::OK : false;
1182}
1183
1184TEST_P(OutputStreamTest, SetCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001185 doc::test(
1186 "If supported, registering callback for async operation should never "
1187 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001188 if (!isAsyncModeSupported(stream.get())) {
1189 doc::partialTest("The stream does not support async operations");
1190 return;
1191 }
1192 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1193 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1194}
1195
1196TEST_P(OutputStreamTest, clearCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001197 doc::test(
1198 "If supported, clearing a callback to go back to sync operation should "
1199 "not fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001200 if (!isAsyncModeSupported(stream.get())) {
1201 doc::partialTest("The stream does not support async operations");
1202 return;
1203 }
1204 // TODO: Clarify if clearing a non existing callback should fail
1205 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1206 ASSERT_OK(stream->clearCallback());
1207}
1208
1209TEST_P(OutputStreamTest, Resume) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001210 doc::test(
1211 "If supported, a stream should fail to resume if not previously "
1212 "paused");
Kevin Rocard624800c2017-03-10 18:47:37 -08001213 if (!Capability(stream.get()).resume) {
1214 doc::partialTest("The output stream does not support resume");
1215 return;
1216 }
1217 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1218}
1219
1220TEST_P(OutputStreamTest, Pause) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001221 doc::test(
1222 "If supported, a stream should fail to pause if not previously "
1223 "started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001224 if (!Capability(stream.get()).pause) {
1225 doc::partialTest("The output stream does not support pause");
1226 return;
1227 }
1228 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1229}
1230
Kevin Rocard72e50e22017-05-05 14:02:55 -07001231static void testDrain(IStreamOut* stream, AudioDrain type) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001232 if (!Capability(stream).drain) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001233 doc::partialTest("The output stream does not support drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001234 return;
1235 }
1236 ASSERT_RESULT(Result::OK, stream->drain(type));
1237}
1238
1239TEST_P(OutputStreamTest, DrainAll) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001240 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001241 testDrain(stream.get(), AudioDrain::ALL);
1242}
1243
1244TEST_P(OutputStreamTest, DrainEarlyNotify) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001245 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001246 testDrain(stream.get(), AudioDrain::EARLY_NOTIFY);
1247}
1248
1249TEST_P(OutputStreamTest, FlushStop) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001250 doc::test("If supported, a stream should always succeed to flush");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001251 auto ret = stream->flush();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001252 ASSERT_IS_OK(ret);
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001253 if (ret == Result::NOT_SUPPORTED) {
1254 doc::partialTest("Flush is not supported");
1255 return;
1256 }
1257 ASSERT_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001258}
1259
1260TEST_P(OutputStreamTest, GetPresentationPositionStop) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001261 doc::test(
1262 "If supported, a stream should always succeed to retrieve the "
1263 "presentation position");
Kevin Rocard624800c2017-03-10 18:47:37 -08001264 uint64_t frames;
1265 TimeSpec mesureTS;
1266 ASSERT_OK(stream->getPresentationPosition(returnIn(res, frames, mesureTS)));
1267 if (res == Result::NOT_SUPPORTED) {
1268 doc::partialTest("getpresentationPosition is not supported");
1269 return;
1270 }
1271 ASSERT_EQ(0U, frames);
1272
Kevin Rocard476e38f2017-05-03 10:52:43 -07001273 if (mesureTS.tvNSec == 0 && mesureTS.tvSec == 0) {
1274 // As the stream has never written a frame yet,
1275 // the timestamp does not really have a meaning, allow to return 0
1276 return;
1277 }
1278
1279 // Make sure the return measure is not more than 1s old.
Kevin Rocard624800c2017-03-10 18:47:37 -08001280 struct timespec currentTS;
1281 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currentTS)) << errno;
1282
Kevin Rocard72e50e22017-05-05 14:02:55 -07001283 auto toMicroSec = [](uint64_t sec, auto nsec) {
1284 return sec * 1e+6 + nsec / 1e+3;
1285 };
Kevin Rocard624800c2017-03-10 18:47:37 -08001286 auto currentTime = toMicroSec(currentTS.tv_sec, currentTS.tv_nsec);
1287 auto mesureTime = toMicroSec(mesureTS.tvSec, mesureTS.tvNSec);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001288 ASSERT_PRED2([](auto c, auto m) { return c - m < 1e+6; }, currentTime,
1289 mesureTime);
Kevin Rocard624800c2017-03-10 18:47:37 -08001290}
1291
Kevin Rocard624800c2017-03-10 18:47:37 -08001292//////////////////////////////////////////////////////////////////////////////
Kevin Rocard3c405a72017-03-08 16:46:51 -08001293/////////////////////////////// PrimaryDevice ////////////////////////////////
1294//////////////////////////////////////////////////////////////////////////////
1295
1296TEST_F(AudioPrimaryHidlTest, setVoiceVolume) {
1297 doc::test("Make sure setVoiceVolume only succeed if volume is in [0,1]");
Chih-Hung Hsieh7eb57b62017-05-15 12:31:25 -07001298 testUnitaryGain([](float volume) { return device->setVoiceVolume(volume); });
Kevin Rocard3c405a72017-03-08 16:46:51 -08001299}
1300
1301TEST_F(AudioPrimaryHidlTest, setMode) {
Kevin Rocard04364ed2017-05-02 18:16:00 -07001302 doc::test(
1303 "Make sure setMode always succeeds if mode is valid "
1304 "and fails otherwise");
1305 // Test Invalid values
1306 for (AudioMode mode :
1307 {AudioMode::INVALID, AudioMode::CURRENT, AudioMode::CNT}) {
1308 SCOPED_TRACE("mode=" + toString(mode));
1309 ASSERT_RESULT(Result::INVALID_ARGUMENTS, device->setMode(mode));
1310 }
1311 // Test valid values
Kevin Rocard72e50e22017-05-05 14:02:55 -07001312 for (AudioMode mode :
1313 {AudioMode::IN_CALL, AudioMode::IN_COMMUNICATION, AudioMode::RINGTONE,
Kevin Rocard72e50e22017-05-05 14:02:55 -07001314 AudioMode::NORMAL /* Make sure to leave the test in normal mode */}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -08001315 SCOPED_TRACE("mode=" + toString(mode));
1316 ASSERT_OK(device->setMode(mode));
1317 }
Kevin Rocard3c405a72017-03-08 16:46:51 -08001318}
1319
Kevin Rocard3c405a72017-03-08 16:46:51 -08001320TEST_F(BoolAccessorPrimaryHidlTest, BtScoNrecEnabled) {
1321 doc::test("Query and set the BT SCO NR&EC state");
1322 testOptionalAccessors("BtScoNrecEnabled", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001323 &IPrimaryDevice::setBtScoNrecEnabled,
1324 &IPrimaryDevice::getBtScoNrecEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001325}
1326
1327TEST_F(BoolAccessorPrimaryHidlTest, setGetBtScoWidebandEnabled) {
1328 doc::test("Query and set the SCO whideband state");
1329 testOptionalAccessors("BtScoWideband", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001330 &IPrimaryDevice::setBtScoWidebandEnabled,
1331 &IPrimaryDevice::getBtScoWidebandEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001332}
1333
1334using TtyModeAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<TtyMode>;
1335TEST_F(TtyModeAccessorPrimaryHidlTest, setGetTtyMode) {
1336 doc::test("Query and set the TTY mode state");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001337 testOptionalAccessors(
1338 "TTY mode", {TtyMode::OFF, TtyMode::HCO, TtyMode::VCO, TtyMode::FULL},
1339 &IPrimaryDevice::setTtyMode, &IPrimaryDevice::getTtyMode);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001340}
1341
1342TEST_F(BoolAccessorPrimaryHidlTest, setGetHac) {
1343 doc::test("Query and set the HAC state");
Kevin Rocard98390a62017-05-03 11:16:05 -07001344 testOptionalAccessors("HAC", {true, false, true},
1345 &IPrimaryDevice::setHacEnabled,
1346 &IPrimaryDevice::getHacEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001347}
1348
1349//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -08001350//////////////////// Clean caches on global tear down ////////////////////////
1351//////////////////////////////////////////////////////////////////////////////
1352
1353int main(int argc, char** argv) {
1354 environment = new Environment;
1355 ::testing::AddGlobalTestEnvironment(environment);
1356 ::testing::InitGoogleTest(&argc, argv);
1357 int status = RUN_ALL_TESTS();
Kevin Rocardf0357882017-02-10 16:19:28 -08001358 return status;
1359}