blob: f49c9261f33e7ffb7c5709fe6e140a3a11881704 [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
Kevin Rocardf0357882017-02-10 16:19:28 -080039#include "utility/AssertOk.h"
40#include "utility/PrettyPrintAudioTypes.h"
Kevin Rocard72e50e22017-05-05 14:02:55 -070041#include "utility/ReturnIn.h"
Kevin Rocardf0357882017-02-10 16:19:28 -080042
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 Rocard72e50e22017-05-05 14:02:55 -070062using ReadParameters =
63 ::android::hardware::audio::V2_0::IStreamIn::ReadParameters;
Kevin Rocardc9963522017-03-10 18:47:37 -080064using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus;
Kevin Rocardf0357882017-02-10 16:19:28 -080065using ::android::hardware::audio::V2_0::IStreamOut;
Kevin Rocard624800c2017-03-10 18:47:37 -080066using ::android::hardware::audio::V2_0::IStreamOutCallback;
Kevin Rocard8878b4b2017-03-10 18:47:37 -080067using ::android::hardware::audio::V2_0::MmapBufferInfo;
68using ::android::hardware::audio::V2_0::MmapPosition;
Kevin Rocardf0357882017-02-10 16:19:28 -080069using ::android::hardware::audio::V2_0::ParameterValue;
70using ::android::hardware::audio::V2_0::Result;
71using ::android::hardware::audio::common::V2_0::AudioChannelMask;
72using ::android::hardware::audio::common::V2_0::AudioConfig;
73using ::android::hardware::audio::common::V2_0::AudioDevice;
74using ::android::hardware::audio::common::V2_0::AudioFormat;
75using ::android::hardware::audio::common::V2_0::AudioHandleConsts;
76using ::android::hardware::audio::common::V2_0::AudioInputFlag;
77using ::android::hardware::audio::common::V2_0::AudioIoHandle;
Kevin Rocard3c405a72017-03-08 16:46:51 -080078using ::android::hardware::audio::common::V2_0::AudioMode;
Kevin Rocardf0357882017-02-10 16:19:28 -080079using ::android::hardware::audio::common::V2_0::AudioOffloadInfo;
80using ::android::hardware::audio::common::V2_0::AudioOutputFlag;
81using ::android::hardware::audio::common::V2_0::AudioSource;
Kevin Rocardc9963522017-03-10 18:47:37 -080082using ::android::hardware::audio::common::V2_0::ThreadInfo;
Kevin Rocardf0357882017-02-10 16:19:28 -080083
84using utility::returnIn;
85
86namespace doc {
87/** Document the current test case.
Kevin Rocard72e50e22017-05-05 14:02:55 -070088 * Eg: calling `doc::test("Dump the state of the hal")` in the "debugDump" test
89 * will output:
90 * <testcase name="debugDump" status="run" time="6"
91 * classname="AudioPrimaryHidlTest"
92 description="Dump the state of the hal." />
93 * see
94 https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#logging-additional-information
Kevin Rocardf0357882017-02-10 16:19:28 -080095 */
96void test(const std::string& testCaseDocumentation) {
97 ::testing::Test::RecordProperty("description", testCaseDocumentation);
98}
99
Kevin Rocard72e50e22017-05-05 14:02:55 -0700100/** Document why a test was not fully run. Usually due to an optional feature
101 * not implemented. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800102void partialTest(const std::string& reason) {
103 ::testing::Test::RecordProperty("partialyRunTest", reason);
104}
105}
106
107// Register callback for static object destruction
108// Avoid destroying static objects after main return.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700109// Post main return destruction leads to incorrect gtest timing measurements as
110// well as harder
Kevin Rocardf0357882017-02-10 16:19:28 -0800111// debuging if anything goes wrong during destruction.
112class Environment : public ::testing::Environment {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700113 public:
114 using TearDownFunc = std::function<void()>;
115 void registerTearDown(TearDownFunc&& tearDown) {
116 tearDowns.push_back(std::move(tearDown));
Kevin Rocardf0357882017-02-10 16:19:28 -0800117 }
118
Kevin Rocard72e50e22017-05-05 14:02:55 -0700119 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800120 void TearDown() override {
121 // Call the tear downs in reverse order of insertion
122 for (auto& tearDown : tearDowns) {
123 tearDown();
124 }
125 }
126 std::list<TearDownFunc> tearDowns;
127};
128// Instance to register global tearDown
129static Environment* environment;
130
Yuexi Ma161b5642017-03-10 13:44:22 -0800131class HidlTest : public ::testing::VtsHalHidlTargetTestBase {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700132 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800133 // Convenient member to store results
134 Result res;
135};
136
137//////////////////////////////////////////////////////////////////////////////
138////////////////////// getService audio_devices_factory //////////////////////
139//////////////////////////////////////////////////////////////////////////////
140
141// Test all audio devices
142class AudioHidlTest : public HidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700143 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800144 void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700145 ASSERT_NO_FATAL_FAILURE(HidlTest::SetUp()); // setup base
Kevin Rocardf0357882017-02-10 16:19:28 -0800146
147 if (devicesFactory == nullptr) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700148 environment->registerTearDown([] { devicesFactory.clear(); });
149 devicesFactory = ::testing::VtsHalHidlTargetTestBase::getService<
150 IDevicesFactory>();
Kevin Rocardf0357882017-02-10 16:19:28 -0800151 }
152 ASSERT_TRUE(devicesFactory != nullptr);
153 }
154
Kevin Rocard72e50e22017-05-05 14:02:55 -0700155 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800156 // Cache the devicesFactory retrieval to speed up each test by ~0.5s
157 static sp<IDevicesFactory> devicesFactory;
158};
159sp<IDevicesFactory> AudioHidlTest::devicesFactory;
160
161TEST_F(AudioHidlTest, GetAudioDevicesFactoryService) {
162 doc::test("test the getService (called in SetUp)");
163}
164
Mikhail Naganov8604a732017-04-24 09:29:22 -0700165TEST_F(AudioHidlTest, OpenDeviceInvalidParameter) {
166 doc::test("test passing an invalid parameter to openDevice");
167 IDevicesFactory::Result result;
168 sp<IDevice> device;
169 ASSERT_OK(devicesFactory->openDevice(IDevicesFactory::Device(-1),
170 returnIn(result, device)));
171 ASSERT_EQ(IDevicesFactory::Result::INVALID_ARGUMENTS, result);
172 ASSERT_TRUE(device == nullptr);
173}
174
Kevin Rocardf0357882017-02-10 16:19:28 -0800175//////////////////////////////////////////////////////////////////////////////
176/////////////////////////////// openDevice primary ///////////////////////////
177//////////////////////////////////////////////////////////////////////////////
178
179// Test the primary device
180class AudioPrimaryHidlTest : public AudioHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700181 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800182 /** Primary HAL test are NOT thread safe. */
183 void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700184 ASSERT_NO_FATAL_FAILURE(AudioHidlTest::SetUp()); // setup base
Kevin Rocardf0357882017-02-10 16:19:28 -0800185
186 if (device == nullptr) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800187 IDevicesFactory::Result result;
Kevin Rocard3c405a72017-03-08 16:46:51 -0800188 sp<IDevice> baseDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700189 ASSERT_OK(
190 devicesFactory->openDevice(IDevicesFactory::Device::PRIMARY,
191 returnIn(result, baseDevice)));
Kevin Rocardfba442a2017-03-31 19:34:41 -0700192 ASSERT_OK(result);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800193 ASSERT_TRUE(baseDevice != nullptr);
194
Kevin Rocard72e50e22017-05-05 14:02:55 -0700195 environment->registerTearDown([] { device.clear(); });
Kevin Rocard3c405a72017-03-08 16:46:51 -0800196 device = IPrimaryDevice::castFrom(baseDevice);
197 ASSERT_TRUE(device != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800198 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800199 }
200
Kevin Rocard72e50e22017-05-05 14:02:55 -0700201 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800202 // Cache the device opening to speed up each test by ~0.5s
Kevin Rocard3c405a72017-03-08 16:46:51 -0800203 static sp<IPrimaryDevice> device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800204};
Kevin Rocard3c405a72017-03-08 16:46:51 -0800205sp<IPrimaryDevice> AudioPrimaryHidlTest::device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800206
207TEST_F(AudioPrimaryHidlTest, OpenPrimaryDevice) {
208 doc::test("Test the openDevice (called in SetUp)");
209}
210
211TEST_F(AudioPrimaryHidlTest, Init) {
212 doc::test("Test that the audio primary hal initialized correctly");
213 ASSERT_OK(device->initCheck());
214}
215
216//////////////////////////////////////////////////////////////////////////////
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800217///////////////////// {set,get}{Master,Mic}{Mute,Volume} /////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800218//////////////////////////////////////////////////////////////////////////////
219
Kevin Rocard3c405a72017-03-08 16:46:51 -0800220template <class Property>
221class AccessorPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700222 protected:
Kevin Rocard3c405a72017-03-08 16:46:51 -0800223 /** Test a property getter and setter. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800224 template <class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700225 void testAccessors(const string& propertyName,
226 const vector<Property>& valuesToTest, Setter setter,
227 Getter getter,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800228 const vector<Property>& invalidValues = {}) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700229 Property initialValue; // Save initial value to restore it at the end
230 // of the test
Kevin Rocard3c405a72017-03-08 16:46:51 -0800231 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
232 ASSERT_OK(res);
233
234 for (Property setValue : valuesToTest) {
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800235 SCOPED_TRACE("Test " + propertyName + " getter and setter for " +
236 testing::PrintToString(setValue));
Kevin Rocard3c405a72017-03-08 16:46:51 -0800237 ASSERT_OK((device.get()->*setter)(setValue));
238 Property getValue;
239 // Make sure the getter returns the same value just set
240 ASSERT_OK((device.get()->*getter)(returnIn(res, getValue)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800241 ASSERT_OK(res);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800242 EXPECT_EQ(setValue, getValue);
Kevin Rocardf0357882017-02-10 16:19:28 -0800243 }
Kevin Rocard3c405a72017-03-08 16:46:51 -0800244
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800245 for (Property invalidValue : invalidValues) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700246 SCOPED_TRACE("Try to set " + propertyName +
247 " with the invalid value " +
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800248 testing::PrintToString(invalidValue));
Kevin Rocard72e50e22017-05-05 14:02:55 -0700249 EXPECT_RESULT(Result::INVALID_ARGUMENTS,
250 (device.get()->*setter)(invalidValue));
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800251 }
252
Kevin Rocard72e50e22017-05-05 14:02:55 -0700253 ASSERT_OK(
254 (device.get()->*setter)(initialValue)); // restore initial value
Kevin Rocard3c405a72017-03-08 16:46:51 -0800255 }
256
257 /** Test the getter and setter of an optional feature. */
258 template <class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700259 void testOptionalAccessors(const string& propertyName,
260 const vector<Property>& valuesToTest,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800261 Setter setter, Getter getter,
262 const vector<Property>& invalidValues = {}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -0800263 doc::test("Test the optional " + propertyName + " getters and setter");
264 {
265 SCOPED_TRACE("Test feature support by calling the getter");
266 Property initialValue;
267 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
268 if (res == Result::NOT_SUPPORTED) {
269 doc::partialTest(propertyName + " getter is not supported");
270 return;
271 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700272 ASSERT_OK(res); // If it is supported it must succeed
Kevin Rocard3c405a72017-03-08 16:46:51 -0800273 }
274 // The feature is supported, test it
Kevin Rocard72e50e22017-05-05 14:02:55 -0700275 testAccessors(propertyName, valuesToTest, setter, getter,
276 invalidValues);
Kevin Rocardf0357882017-02-10 16:19:28 -0800277 }
278};
279
Kevin Rocard3c405a72017-03-08 16:46:51 -0800280using BoolAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<bool>;
281
Kevin Rocardf0357882017-02-10 16:19:28 -0800282TEST_F(BoolAccessorPrimaryHidlTest, MicMuteTest) {
283 doc::test("Check that the mic can be muted and unmuted");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700284 testAccessors("mic mute", {true, false, true}, &IDevice::setMicMute,
285 &IDevice::getMicMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800286 // TODO: check that the mic is really muted (all sample are 0)
287}
288
289TEST_F(BoolAccessorPrimaryHidlTest, MasterMuteTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700290 doc::test(
291 "If master mute is supported, try to mute and unmute the master "
292 "output");
Kevin Rocard3c405a72017-03-08 16:46:51 -0800293 testOptionalAccessors("master mute", {true, false, true},
294 &IDevice::setMasterMute, &IDevice::getMasterMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800295 // TODO: check that the master volume is really muted
296}
297
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800298using FloatAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<float>;
299TEST_F(FloatAccessorPrimaryHidlTest, MasterVolumeTest) {
300 doc::test("Test the master volume if supported");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700301 testOptionalAccessors("master volume", {0, 0.5, 1},
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800302 &IDevice::setMasterVolume, &IDevice::getMasterVolume,
303 {-0.1, 1.1, NAN, INFINITY, -INFINITY,
304 1 + std::numeric_limits<float>::epsilon()});
305 // TODO: check that the master volume is really changed
306}
307
Kevin Rocardf0357882017-02-10 16:19:28 -0800308//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800309//////////////////////////////// AudioPatches ////////////////////////////////
310//////////////////////////////////////////////////////////////////////////////
311
312class AudioPatchPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700313 protected:
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800314 bool areAudioPatchesSupported() {
315 auto result = device->supportsAudioPatches();
316 EXPECT_TRUE(result.isOk());
317 return result;
318 }
319};
320
321TEST_F(AudioPatchPrimaryHidlTest, AudioPatches) {
322 doc::test("Test if audio patches are supported");
323 if (!areAudioPatchesSupported()) {
324 doc::partialTest("Audio patches are not supported");
325 return;
326 }
327 // TODO: test audio patches
328}
329
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800330//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800331//////////////// Required and recommended audio format support ///////////////
Kevin Rocard72e50e22017-05-05 14:02:55 -0700332// From:
333// https://source.android.com/compatibility/android-cdd.html#5_4_audio_recording
334// From:
335// https://source.android.com/compatibility/android-cdd.html#5_5_audio_playback
Kevin Rocardf0357882017-02-10 16:19:28 -0800336/////////// TODO: move to the beginning of the file for easier update ////////
337//////////////////////////////////////////////////////////////////////////////
338
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800339class AudioConfigPrimaryTest : public AudioPatchPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700340 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800341 // Cache result ?
342 static const vector<AudioConfig> getRequiredSupportPlaybackAudioConfig() {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700343 return combineAudioConfig(
344 {AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
345 {8000, 11025, 16000, 22050, 32000, 44100},
346 {AudioFormat::PCM_16_BIT});
Kevin Rocardf0357882017-02-10 16:19:28 -0800347 }
348
Kevin Rocard72e50e22017-05-05 14:02:55 -0700349 static const vector<AudioConfig>
350 getRecommendedSupportPlaybackAudioConfig() {
351 return combineAudioConfig(
352 {AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
353 {24000, 48000}, {AudioFormat::PCM_16_BIT});
Kevin Rocardf0357882017-02-10 16:19:28 -0800354 }
355
356 static const vector<AudioConfig> getSupportedPlaybackAudioConfig() {
357 // TODO: retrieve audio config supported by the platform
358 // as declared in the policy configuration
359 return {};
360 }
361
362 static const vector<AudioConfig> getRequiredSupportCaptureAudioConfig() {
363 return combineAudioConfig({AudioChannelMask::IN_MONO},
364 {8000, 11025, 16000, 44100},
365 {AudioFormat::PCM_16_BIT});
366 }
367 static const vector<AudioConfig> getRecommendedSupportCaptureAudioConfig() {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700368 return combineAudioConfig({AudioChannelMask::IN_STEREO}, {22050, 48000},
Kevin Rocardf0357882017-02-10 16:19:28 -0800369 {AudioFormat::PCM_16_BIT});
370 }
371 static const vector<AudioConfig> getSupportedCaptureAudioConfig() {
372 // TODO: retrieve audio config supported by the platform
373 // as declared in the policy configuration
374 return {};
375 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700376
377 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800378 static const vector<AudioConfig> combineAudioConfig(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700379 vector<AudioChannelMask> channelMasks, vector<uint32_t> sampleRates,
380 vector<AudioFormat> formats) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800381 vector<AudioConfig> configs;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700382 for (auto channelMask : channelMasks) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800383 for (auto sampleRate : sampleRates) {
384 for (auto format : formats) {
385 AudioConfig config{};
386 // leave offloadInfo to 0
387 config.channelMask = channelMask;
388 config.sampleRateHz = sampleRate;
389 config.format = format;
390 // FIXME: leave frameCount to 0 ?
391 configs.push_back(config);
392 }
393 }
394 }
395 return configs;
396 }
397};
398
Kevin Rocard9c369142017-03-08 17:17:25 -0800399/** Generate a test name based on an audio config.
400 *
401 * As the only parameter changing are channel mask and sample rate,
402 * only print those ones in the test name.
403 */
Kevin Rocard72e50e22017-05-05 14:02:55 -0700404static string generateTestName(
405 const testing::TestParamInfo<AudioConfig>& info) {
Kevin Rocard9c369142017-03-08 17:17:25 -0800406 const AudioConfig& config = info.param;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700407 return to_string(info.index) + "__" + to_string(config.sampleRateHz) + "_" +
408 // "MONO" is more clear than "FRONT_LEFT"
409 ((config.channelMask == AudioChannelMask::OUT_MONO ||
410 config.channelMask == AudioChannelMask::IN_MONO)
411 ? "MONO"
412 : toString(config.channelMask));
Kevin Rocard9c369142017-03-08 17:17:25 -0800413}
414
Kevin Rocardf0357882017-02-10 16:19:28 -0800415//////////////////////////////////////////////////////////////////////////////
416///////////////////////////// getInputBufferSize /////////////////////////////
417//////////////////////////////////////////////////////////////////////////////
418
Kevin Rocard72e50e22017-05-05 14:02:55 -0700419// FIXME: execute input test only if platform declares
420// android.hardware.microphone
Kevin Rocardf0357882017-02-10 16:19:28 -0800421// how to get this value ? is it a property ???
422
Kevin Rocard72e50e22017-05-05 14:02:55 -0700423class AudioCaptureConfigPrimaryTest
424 : public AudioConfigPrimaryTest,
425 public ::testing::WithParamInterface<AudioConfig> {
426 protected:
427 void inputBufferSizeTest(const AudioConfig& audioConfig,
428 bool supportRequired) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800429 uint64_t bufferSize;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700430 ASSERT_OK(
431 device->getInputBufferSize(audioConfig, returnIn(res, bufferSize)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800432
433 switch (res) {
434 case Result::INVALID_ARGUMENTS:
435 EXPECT_FALSE(supportRequired);
436 break;
437 case Result::OK:
438 // Check that the buffer is of a sane size
439 // For now only that it is > 0
440 EXPECT_GT(bufferSize, uint64_t(0));
441 break;
442 default:
Kevin Rocard72e50e22017-05-05 14:02:55 -0700443 FAIL() << "Invalid return status: "
444 << ::testing::PrintToString(res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800445 }
446 }
447};
448
Kevin Rocard72e50e22017-05-05 14:02:55 -0700449// Test that the required capture config and those declared in the policy are
450// indeed supported
Kevin Rocardf0357882017-02-10 16:19:28 -0800451class RequiredInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
452TEST_P(RequiredInputBufferSizeTest, RequiredInputBufferSizeTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700453 doc::test(
454 "Input buffer size must be retrievable for a format with required "
455 "support.");
Kevin Rocardf0357882017-02-10 16:19:28 -0800456 inputBufferSizeTest(GetParam(), true);
457}
458INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700459 RequiredInputBufferSize, RequiredInputBufferSizeTest,
460 ::testing::ValuesIn(
461 AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
462 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800463INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700464 SupportedInputBufferSize, RequiredInputBufferSizeTest,
465 ::testing::ValuesIn(
466 AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
467 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800468
Kevin Rocard72e50e22017-05-05 14:02:55 -0700469// Test that the recommended capture config are supported or lead to a
470// INVALID_ARGUMENTS return
Kevin Rocardf0357882017-02-10 16:19:28 -0800471class OptionalInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
472TEST_P(OptionalInputBufferSizeTest, OptionalInputBufferSizeTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700473 doc::test(
474 "Input buffer size should be retrievable for a format with recommended "
475 "support.");
Kevin Rocardf0357882017-02-10 16:19:28 -0800476 inputBufferSizeTest(GetParam(), false);
477}
478INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700479 RecommendedCaptureAudioConfigSupport, OptionalInputBufferSizeTest,
480 ::testing::ValuesIn(
481 AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
482 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800483
484//////////////////////////////////////////////////////////////////////////////
485/////////////////////////////// setScreenState ///////////////////////////////
486//////////////////////////////////////////////////////////////////////////////
487
488TEST_F(AudioPrimaryHidlTest, setScreenState) {
489 doc::test("Check that the hal can receive the screen state");
490 for (bool turnedOn : {false, true, true, false, false}) {
491 auto ret = device->setScreenState(turnedOn);
492 ASSERT_TRUE(ret.isOk());
493 Result result = ret;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700494 ASSERT_TRUE(result == Result::OK || result == Result::NOT_SUPPORTED)
495 << toString(result);
Kevin Rocardf0357882017-02-10 16:19:28 -0800496 }
497}
498
499//////////////////////////////////////////////////////////////////////////////
500//////////////////////////// {get,set}Parameters /////////////////////////////
501//////////////////////////////////////////////////////////////////////////////
502
503TEST_F(AudioPrimaryHidlTest, getParameters) {
504 doc::test("Check that the hal can set and get parameters");
505 hidl_vec<hidl_string> keys;
506 hidl_vec<ParameterValue> values;
507 ASSERT_OK(device->getParameters(keys, returnIn(res, values)));
508 ASSERT_OK(device->setParameters(values));
509 values.resize(0);
510 ASSERT_OK(device->setParameters(values));
511}
512
513//////////////////////////////////////////////////////////////////////////////
514//////////////////////////////// debugDebug //////////////////////////////////
515//////////////////////////////////////////////////////////////////////////////
516
Kevin Rocardb9031242017-03-13 12:20:54 -0700517template <class DebugDump>
518static void testDebugDump(DebugDump debugDump) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800519 FILE* file = tmpfile();
520 ASSERT_NE(nullptr, file) << errno;
521
522 auto* nativeHandle = native_handle_create(1, 0);
523 ASSERT_NE(nullptr, nativeHandle);
524 nativeHandle->data[0] = fileno(file);
525
526 hidl_handle handle;
527 handle.setTo(nativeHandle, true /*take ownership*/);
528
Kevin Rocardee771e92017-03-08 17:17:25 -0800529 // TODO: debugDump does not return a Result.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700530 // This mean that the hal can not report that it not implementing the
531 // function.
Kevin Rocardb9031242017-03-13 12:20:54 -0700532 ASSERT_OK(debugDump(handle));
Kevin Rocardee771e92017-03-08 17:17:25 -0800533
Kevin Rocard72e50e22017-05-05 14:02:55 -0700534 rewind(file); // can not fail
Kevin Rocardf0357882017-02-10 16:19:28 -0800535
536 // Check that at least one bit was written by the hal
537 char buff;
Kevin Rocardee771e92017-03-08 17:17:25 -0800538 ASSERT_EQ(size_t{1}, fread(&buff, sizeof(buff), 1, file));
Kevin Rocardf0357882017-02-10 16:19:28 -0800539 EXPECT_EQ(0, fclose(file)) << errno;
540}
541
Kevin Rocardb9031242017-03-13 12:20:54 -0700542TEST_F(AudioPrimaryHidlTest, debugDump) {
543 doc::test("Check that the hal can dump its state without error");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700544 testDebugDump(
545 [this](const auto& handle) { return device->debugDump(handle); });
Kevin Rocardb9031242017-03-13 12:20:54 -0700546}
547
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700548TEST_F(AudioPrimaryHidlTest, debugDumpInvalidArguments) {
549 doc::test("Check that the hal dump doesn't crash on invalid arguments");
550 ASSERT_OK(device->debugDump(hidl_handle()));
551}
552
Kevin Rocardf0357882017-02-10 16:19:28 -0800553//////////////////////////////////////////////////////////////////////////////
554////////////////////////// open{Output,Input}Stream //////////////////////////
555//////////////////////////////////////////////////////////////////////////////
556
557template <class Stream>
558class OpenStreamTest : public AudioConfigPrimaryTest,
559 public ::testing::WithParamInterface<AudioConfig> {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700560 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800561 template <class Open>
562 void testOpen(Open openStream, const AudioConfig& config) {
563 // FIXME: Open a stream without an IOHandle
564 // This is not required to be accepted by hal implementations
Kevin Rocard72e50e22017-05-05 14:02:55 -0700565 AudioIoHandle ioHandle =
566 (AudioIoHandle)AudioHandleConsts::AUDIO_IO_HANDLE_NONE;
Kevin Rocardf0357882017-02-10 16:19:28 -0800567 AudioConfig suggestedConfig{};
Kevin Rocard72e50e22017-05-05 14:02:55 -0700568 ASSERT_OK(openStream(ioHandle, config,
569 returnIn(res, stream, suggestedConfig)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800570
571 // TODO: only allow failure for RecommendedPlaybackAudioConfig
572 switch (res) {
573 case Result::OK:
574 ASSERT_TRUE(stream != nullptr);
575 audioConfig = config;
576 break;
577 case Result::INVALID_ARGUMENTS:
578 ASSERT_TRUE(stream == nullptr);
579 AudioConfig suggestedConfigRetry;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700580 // Could not open stream with config, try again with the
581 // suggested one
582 ASSERT_OK(
583 openStream(ioHandle, suggestedConfig,
584 returnIn(res, stream, suggestedConfigRetry)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800585 // This time it must succeed
586 ASSERT_OK(res);
587 ASSERT_TRUE(stream == nullptr);
588 audioConfig = suggestedConfig;
589 break;
590 default:
Kevin Rocard72e50e22017-05-05 14:02:55 -0700591 FAIL() << "Invalid return status: "
592 << ::testing::PrintToString(res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800593 }
594 open = true;
595 }
596
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800597 Return<Result> closeStream() {
598 open = false;
599 return stream->close();
600 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700601
602 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800603 void TearDown() override {
604 if (open) {
605 ASSERT_OK(stream->close());
606 }
607 }
608
Kevin Rocard72e50e22017-05-05 14:02:55 -0700609 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800610 AudioConfig audioConfig;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800611 DeviceAddress address = {};
Kevin Rocardf0357882017-02-10 16:19:28 -0800612 sp<Stream> stream;
613 bool open = false;
614};
615
616////////////////////////////// openOutputStream //////////////////////////////
617
618class OutputStreamTest : public OpenStreamTest<IStreamOut> {
619 virtual void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700620 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800621 address.device = AudioDevice::OUT_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800622 const AudioConfig& config = GetParam();
Kevin Rocard72e50e22017-05-05 14:02:55 -0700623 AudioOutputFlag flags =
624 AudioOutputFlag::NONE; // TODO: test all flag combination
625 testOpen(
626 [&](AudioIoHandle handle, AudioConfig config, auto cb) {
627 return device->openOutputStream(handle, address, config, flags,
628 cb);
629 },
630 config);
Kevin Rocardf0357882017-02-10 16:19:28 -0800631 }
632};
633TEST_P(OutputStreamTest, OpenOutputStreamTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700634 doc::test(
635 "Check that output streams can be open with the required and "
636 "recommended config");
Kevin Rocardf0357882017-02-10 16:19:28 -0800637 // Open done in SetUp
638}
639INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700640 RequiredOutputStreamConfigSupport, OutputStreamTest,
641 ::testing::ValuesIn(
642 AudioConfigPrimaryTest::getRequiredSupportPlaybackAudioConfig()),
643 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800644INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700645 SupportedOutputStreamConfig, OutputStreamTest,
646 ::testing::ValuesIn(
647 AudioConfigPrimaryTest::getSupportedPlaybackAudioConfig()),
648 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800649
650INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700651 RecommendedOutputStreamConfigSupport, OutputStreamTest,
652 ::testing::ValuesIn(
653 AudioConfigPrimaryTest::getRecommendedSupportPlaybackAudioConfig()),
654 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800655
656////////////////////////////// openInputStream //////////////////////////////
657
658class InputStreamTest : public OpenStreamTest<IStreamIn> {
Kevin Rocardf0357882017-02-10 16:19:28 -0800659 virtual void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700660 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800661 address.device = AudioDevice::IN_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800662 const AudioConfig& config = GetParam();
Kevin Rocard72e50e22017-05-05 14:02:55 -0700663 AudioInputFlag flags =
664 AudioInputFlag::NONE; // TODO: test all flag combination
665 AudioSource source =
666 AudioSource::DEFAULT; // TODO: test all flag combination
667 testOpen(
668 [&](AudioIoHandle handle, AudioConfig config, auto cb) {
669 return device->openInputStream(handle, address, config, flags,
670 source, cb);
671 },
672 config);
Kevin Rocardf0357882017-02-10 16:19:28 -0800673 }
674};
675
676TEST_P(InputStreamTest, OpenInputStreamTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700677 doc::test(
678 "Check that input streams can be open with the required and "
679 "recommended config");
Kevin Rocardf0357882017-02-10 16:19:28 -0800680 // Open done in setup
681}
682INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700683 RequiredInputStreamConfigSupport, InputStreamTest,
684 ::testing::ValuesIn(
685 AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
686 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800687INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700688 SupportedInputStreamConfig, InputStreamTest,
689 ::testing::ValuesIn(
690 AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
691 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800692
693INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700694 RecommendedInputStreamConfigSupport, InputStreamTest,
695 ::testing::ValuesIn(
696 AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
697 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800698
699//////////////////////////////////////////////////////////////////////////////
700////////////////////////////// IStream getters ///////////////////////////////
701//////////////////////////////////////////////////////////////////////////////
702
703/** Unpack the provided result.
704 * If the result is not OK, register a failure and return an undefined value. */
705template <class R>
706static R extract(Return<R> ret) {
707 if (!ret.isOk()) {
708 ADD_FAILURE();
709 return R{};
710 }
711 return ret;
712}
713
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800714/* Could not find a way to write a test for two parametrized class fixure
715 * thus use this macro do duplicate tests for Input and Output stream */
716#define TEST_IO_STREAM(test_name, documentation, code) \
Kevin Rocard72e50e22017-05-05 14:02:55 -0700717 TEST_P(InputStreamTest, test_name) { \
718 doc::test(documentation); \
719 code; \
720 } \
721 TEST_P(OutputStreamTest, test_name) { \
722 doc::test(documentation); \
723 code; \
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800724 }
725
Kevin Rocard72e50e22017-05-05 14:02:55 -0700726TEST_IO_STREAM(
727 GetFrameCount,
728 "Check that the stream frame count == the one it was opened with",
729 ASSERT_EQ(audioConfig.frameCount, extract(stream->getFrameCount())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800730
Kevin Rocard72e50e22017-05-05 14:02:55 -0700731TEST_IO_STREAM(
732 GetSampleRate,
733 "Check that the stream sample rate == the one it was opened with",
734 ASSERT_EQ(audioConfig.sampleRateHz, extract(stream->getSampleRate())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800735
Kevin Rocard72e50e22017-05-05 14:02:55 -0700736TEST_IO_STREAM(
737 GetChannelMask,
738 "Check that the stream channel mask == the one it was opened with",
739 ASSERT_EQ(audioConfig.channelMask, extract(stream->getChannelMask())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800740
Kevin Rocard72e50e22017-05-05 14:02:55 -0700741TEST_IO_STREAM(GetFormat,
742 "Check that the stream format == the one it was opened with",
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800743 ASSERT_EQ(audioConfig.format, extract(stream->getFormat())))
744
745// TODO: for now only check that the framesize is not incoherent
Kevin Rocard72e50e22017-05-05 14:02:55 -0700746TEST_IO_STREAM(GetFrameSize,
747 "Check that the stream frame size == the one it was opened with",
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800748 ASSERT_GT(extract(stream->getFrameSize()), 0U))
749
Kevin Rocard72e50e22017-05-05 14:02:55 -0700750TEST_IO_STREAM(GetBufferSize,
751 "Check that the stream buffer size== the one it was opened with",
752 ASSERT_GE(extract(stream->getBufferSize()),
753 extract(stream->getFrameSize())));
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800754
Kevin Rocardf0357882017-02-10 16:19:28 -0800755template <class Property, class CapabilityGetter, class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700756static void testCapabilityGetter(const string& name, IStream* stream,
757 Property currentValue,
758 CapabilityGetter capablityGetter,
759 Getter getter, Setter setter) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800760 hidl_vec<Property> capabilities;
761 ASSERT_OK((stream->*capablityGetter)(returnIn(capabilities)));
762 if (capabilities.size() == 0) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700763 // The default hal should probably return a NOT_SUPPORTED if the hal
764 // does not expose
765 // capability retrieval. For now it returns an empty list if not
766 // implemented
Kevin Rocardf0357882017-02-10 16:19:28 -0800767 doc::partialTest(name + " is not supported");
768 return;
769 };
Kevin Rocard72e50e22017-05-05 14:02:55 -0700770 // TODO: This code has never been tested on a hal that supports
771 // getSupportedSampleRates
Kevin Rocardf0357882017-02-10 16:19:28 -0800772 EXPECT_NE(std::find(capabilities.begin(), capabilities.end(), currentValue),
Kevin Rocard72e50e22017-05-05 14:02:55 -0700773 capabilities.end())
Kevin Rocardf0357882017-02-10 16:19:28 -0800774 << "current " << name << " is not in the list of the supported ones "
775 << toString(capabilities);
776
777 // Check that all declared supported values are indeed supported
778 for (auto capability : capabilities) {
779 ASSERT_OK((stream->*setter)(capability));
780 ASSERT_EQ(capability, extract((stream->*getter)()));
781 }
782}
783
Kevin Rocard72e50e22017-05-05 14:02:55 -0700784TEST_IO_STREAM(SupportedSampleRate,
785 "Check that the stream sample rate is declared as supported",
786 testCapabilityGetter("getSupportedSampleRate", stream.get(),
787 extract(stream->getSampleRate()),
788 &IStream::getSupportedSampleRates,
789 &IStream::getSampleRate,
790 &IStream::setSampleRate))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800791
Kevin Rocard72e50e22017-05-05 14:02:55 -0700792TEST_IO_STREAM(SupportedChannelMask,
793 "Check that the stream channel mask is declared as supported",
794 testCapabilityGetter("getSupportedChannelMask", stream.get(),
795 extract(stream->getChannelMask()),
796 &IStream::getSupportedChannelMasks,
797 &IStream::getChannelMask,
798 &IStream::setChannelMask))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800799
Kevin Rocard72e50e22017-05-05 14:02:55 -0700800TEST_IO_STREAM(SupportedFormat,
801 "Check that the stream format is declared as supported",
802 testCapabilityGetter("getSupportedFormat", stream.get(),
803 extract(stream->getFormat()),
804 &IStream::getSupportedFormats,
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800805 &IStream::getFormat, &IStream::setFormat))
806
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800807static void testGetDevice(IStream* stream, AudioDevice expectedDevice) {
808 auto ret = stream->getDevice();
809 ASSERT_TRUE(ret.isOk());
810 AudioDevice device = ret;
811 ASSERT_EQ(expectedDevice, device);
812}
813
Kevin Rocard72e50e22017-05-05 14:02:55 -0700814TEST_IO_STREAM(GetDevice,
815 "Check that the stream device == the one it was opened with",
816 areAudioPatchesSupported()
817 ? doc::partialTest("Audio patches are supported")
818 : testGetDevice(stream.get(), address.device))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800819
820static void testSetDevice(IStream* stream, const DeviceAddress& address) {
821 DeviceAddress otherAddress = address;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700822 otherAddress.device = (address.device & AudioDevice::BIT_IN) == 0
823 ? AudioDevice::OUT_SPEAKER
824 : AudioDevice::IN_BUILTIN_MIC;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800825 EXPECT_OK(stream->setDevice(otherAddress));
826
Kevin Rocard72e50e22017-05-05 14:02:55 -0700827 ASSERT_OK(stream->setDevice(address)); // Go back to the original value
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800828}
829
Kevin Rocard72e50e22017-05-05 14:02:55 -0700830TEST_IO_STREAM(
831 SetDevice,
832 "Check that the stream can be rerouted to SPEAKER or BUILTIN_MIC",
833 areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported")
834 : testSetDevice(stream.get(), address))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800835
Kevin Rocard72e50e22017-05-05 14:02:55 -0700836static void testGetAudioProperties(IStream* stream,
837 AudioConfig expectedConfig) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800838 uint32_t sampleRateHz;
839 AudioChannelMask mask;
840 AudioFormat format;
841
842 stream->getAudioProperties(returnIn(sampleRateHz, mask, format));
843
Kevin Rocard72e50e22017-05-05 14:02:55 -0700844 // FIXME: the qcom hal it does not currently negotiate the sampleRate &
845 // channel mask
Kevin Rocardd1e98ae2017-03-08 17:17:25 -0800846 EXPECT_EQ(expectedConfig.sampleRateHz, sampleRateHz);
847 EXPECT_EQ(expectedConfig.channelMask, mask);
Kevin Rocardf0357882017-02-10 16:19:28 -0800848 EXPECT_EQ(expectedConfig.format, format);
849}
850
Kevin Rocard72e50e22017-05-05 14:02:55 -0700851TEST_IO_STREAM(
852 GetAudioProperties,
853 "Check that the stream audio properties == the ones it was opened with",
854 testGetAudioProperties(stream.get(), audioConfig))
Kevin Rocardf0357882017-02-10 16:19:28 -0800855
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800856static void testConnectedState(IStream* stream) {
857 DeviceAddress address = {};
858 using AD = AudioDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700859 for (auto device :
860 {AD::OUT_HDMI, AD::OUT_WIRED_HEADPHONE, AD::IN_USB_HEADSET}) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800861 address.device = device;
862
863 ASSERT_OK(stream->setConnectedState(address, true));
864 ASSERT_OK(stream->setConnectedState(address, false));
865 }
866}
867TEST_IO_STREAM(SetConnectedState,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700868 "Check that the stream can be notified of device connection and "
869 "deconnection",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800870 testConnectedState(stream.get()))
871
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700872static auto invalidArgsOrNotSupportedOrOK = {Result::INVALID_ARGUMENTS,
873 Result::NOT_SUPPORTED, Result::OK};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800874TEST_IO_STREAM(SetHwAvSync, "Try to set hardware sync to an invalid value",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700875 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700876 stream->setHwAvSync(666)))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800877
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700878TEST_IO_STREAM(GetHwAvSync, "Get hardware sync can not fail",
879 ASSERT_TRUE(device->getHwAvSync().isOk()))
880
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700881static void checkGetNoParameter(IStream* stream, hidl_vec<hidl_string> keys,
882 vector<Result> expectedResults) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800883 hidl_vec<ParameterValue> parameters;
884 Result res;
885 ASSERT_OK(stream->getParameters(keys, returnIn(res, parameters)));
886 ASSERT_RESULT(expectedResults, res);
887 if (res == Result::OK) {
888 ASSERT_EQ(0U, parameters.size());
889 }
890}
891
Kevin Rocard72e50e22017-05-05 14:02:55 -0700892/* Get/Set parameter is intended to be an opaque channel between vendors app and
893 * their HALs.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800894 * Thus can not be meaningfully tested.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800895 */
896TEST_IO_STREAM(getEmptySetParameter, "Retrieve the values of an empty set",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700897 checkGetNoParameter(stream.get(), {} /* keys */, {Result::OK}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800898
Kevin Rocard72e50e22017-05-05 14:02:55 -0700899TEST_IO_STREAM(getNonExistingParameter,
900 "Retrieve the values of an non existing parameter",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700901 checkGetNoParameter(stream.get(),
902 {"Non existing key"} /* keys */,
903 {Result::NOT_SUPPORTED}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800904
Kevin Rocard72e50e22017-05-05 14:02:55 -0700905TEST_IO_STREAM(setEmptySetParameter,
906 "Set the values of an empty set of parameters",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700907 ASSERT_RESULT(Result::OK, stream->setParameters({})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800908
Kevin Rocard72e50e22017-05-05 14:02:55 -0700909TEST_IO_STREAM(
910 setNonExistingParameter, "Set the values of an non existing parameter",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700911 // Unfortunately, the set_parameter legacy interface did not return any
912 // error code when a key is not supported.
913 // To allow implementation to just wrapped the legacy one, consider OK as a
914 // valid result for setting a non existing parameter.
915 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700916 stream->setParameters({{"non existing key", "0"}})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800917
Kevin Rocardb9031242017-03-13 12:20:54 -0700918TEST_IO_STREAM(DebugDump,
919 "Check that a stream can dump its state without error",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700920 testDebugDump([this](const auto& handle) {
921 return stream->debugDump(handle);
922 }))
Kevin Rocardb9031242017-03-13 12:20:54 -0700923
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700924TEST_IO_STREAM(DebugDumpInvalidArguments,
925 "Check that the stream dump doesn't crash on invalid arguments",
926 ASSERT_OK(stream->debugDump(hidl_handle())))
927
Kevin Rocardf0357882017-02-10 16:19:28 -0800928//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800929////////////////////////////// addRemoveEffect ///////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800930//////////////////////////////////////////////////////////////////////////////
931
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800932TEST_IO_STREAM(AddNonExistingEffect, "Adding a non existing effect should fail",
933 ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->addEffect(666)))
Kevin Rocard72e50e22017-05-05 14:02:55 -0700934TEST_IO_STREAM(RemoveNonExistingEffect,
935 "Removing a non existing effect should fail",
936 ASSERT_RESULT(Result::INVALID_ARGUMENTS,
937 stream->removeEffect(666)))
Kevin Rocardf0357882017-02-10 16:19:28 -0800938
Kevin Rocard72e50e22017-05-05 14:02:55 -0700939// TODO: positive tests
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800940
941//////////////////////////////////////////////////////////////////////////////
942/////////////////////////////// Control ////////////////////////////////
943//////////////////////////////////////////////////////////////////////////////
944
945TEST_IO_STREAM(standby, "Make sure the stream can be put in stanby",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700946 ASSERT_OK(stream->standby())) // can not fail
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800947
Kevin Rocard72e50e22017-05-05 14:02:55 -0700948static vector<Result> invalidStateOrNotSupported = {Result::INVALID_STATE,
949 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800950
Kevin Rocard72e50e22017-05-05 14:02:55 -0700951TEST_IO_STREAM(startNoMmap,
952 "Starting a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800953 ASSERT_RESULT(invalidStateOrNotSupported, stream->start()))
954
Kevin Rocard72e50e22017-05-05 14:02:55 -0700955TEST_IO_STREAM(stopNoMmap,
956 "Stopping a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800957 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
958
Kevin Rocard72e50e22017-05-05 14:02:55 -0700959TEST_IO_STREAM(getMmapPositionNoMmap,
960 "Get a stream Mmap position before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800961 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
962
Kevin Rocard72e50e22017-05-05 14:02:55 -0700963TEST_IO_STREAM(close, "Make sure a stream can be closed",
964 ASSERT_OK(closeStream()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800965TEST_IO_STREAM(closeTwice, "Make sure a stream can not be closed twice",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700966 ASSERT_OK(closeStream());
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800967 ASSERT_RESULT(Result::INVALID_STATE, closeStream()))
968
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700969static auto invalidArgsOrNotSupported = {Result::INVALID_ARGUMENTS,
970 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800971static void testCreateTooBigMmapBuffer(IStream* stream) {
972 MmapBufferInfo info;
973 Result res;
974 // Assume that int max is a value too big to be allocated
Kevin Rocard72e50e22017-05-05 14:02:55 -0700975 // This is true currently with a 32bit media server, but might not when it
976 // will run in 64 bit
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800977 auto minSizeFrames = std::numeric_limits<int32_t>::max();
978 ASSERT_OK(stream->createMmapBuffer(minSizeFrames, returnIn(res, info)));
979 ASSERT_RESULT(invalidArgsOrNotSupported, res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800980}
981
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800982TEST_IO_STREAM(CreateTooBigMmapBuffer, "Create mmap buffer too big should fail",
983 testCreateTooBigMmapBuffer(stream.get()))
984
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800985static void testGetMmapPositionOfNonMmapedStream(IStream* stream) {
986 Result res;
987 MmapPosition position;
988 ASSERT_OK(stream->getMmapPosition(returnIn(res, position)));
989 ASSERT_RESULT(invalidArgsOrNotSupported, res);
990}
991
Kevin Rocard72e50e22017-05-05 14:02:55 -0700992TEST_IO_STREAM(
993 GetMmapPositionOfNonMmapedStream,
994 "Retrieving the mmap position of a non mmaped stream should fail",
995 testGetMmapPositionOfNonMmapedStream(stream.get()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800996
Kevin Rocardf0357882017-02-10 16:19:28 -0800997//////////////////////////////////////////////////////////////////////////////
Kevin Rocardc9963522017-03-10 18:47:37 -0800998///////////////////////////////// StreamIn ///////////////////////////////////
999//////////////////////////////////////////////////////////////////////////////
1000
1001TEST_P(InputStreamTest, GetAudioSource) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001002 doc::test(
1003 "Retrieving the audio source of an input stream should always succeed");
Kevin Rocardc9963522017-03-10 18:47:37 -08001004 AudioSource source;
1005 ASSERT_OK(stream->getAudioSource(returnIn(res, source)));
1006 ASSERT_OK(res);
1007 ASSERT_EQ(AudioSource::DEFAULT, source);
1008}
1009
Kevin Rocard72e50e22017-05-05 14:02:55 -07001010static void testUnitaryGain(std::function<Return<Result>(float)> setGain) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001011 for (float value : {0.0, 0.01, 0.5, 0.09, 1.0}) {
1012 SCOPED_TRACE("value=" + to_string(value));
1013 ASSERT_OK(setGain(value));
1014 }
Kevin Rocard72e50e22017-05-05 14:02:55 -07001015 for (float value : (float[]){-INFINITY, -1.0, -0.0,
1016 1.0 + std::numeric_limits<float>::epsilon(),
1017 2.0, INFINITY, NAN}) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001018 SCOPED_TRACE("value=" + to_string(value));
1019 // FIXME: NAN should never be accepted
Kevin Rocard72e50e22017-05-05 14:02:55 -07001020 // FIXME: Missing api doc. What should the impl do if the volume is
1021 // outside [0,1] ?
Kevin Rocardc9963522017-03-10 18:47:37 -08001022 ASSERT_RESULT(Result::INVALID_ARGUMENTS, setGain(value));
1023 }
1024}
1025
1026TEST_P(InputStreamTest, SetGain) {
1027 doc::test("The gain of an input stream should only be set between [0,1]");
1028 testUnitaryGain([this](float volume) { return stream->setGain(volume); });
1029}
1030
Kevin Rocard72e50e22017-05-05 14:02:55 -07001031static void testPrepareForReading(IStreamIn* stream, uint32_t frameSize,
1032 uint32_t framesCount) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001033 Result res;
1034 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001035 ASSERT_OK(stream->prepareForReading(
1036 frameSize, framesCount,
1037 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocardc9963522017-03-10 18:47:37 -08001038 EXPECT_RESULT(invalidArgsOrNotSupported, res);
1039}
1040
Kevin Rocard195205b2017-05-02 18:34:59 -07001041TEST_P(InputStreamTest, PrepareForReadingWithZeroBuffer) {
1042 doc::test(
1043 "Preparing a stream for reading with a 0 sized buffer should fail");
1044 testPrepareForReading(stream.get(), 0, 0);
1045}
1046
Kevin Rocardc9963522017-03-10 18:47:37 -08001047TEST_P(InputStreamTest, PrepareForReadingWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001048 doc::test(
1049 "Preparing a stream for reading with a 2^32 sized buffer should fail");
1050 testPrepareForReading(stream.get(), 1,
1051 std::numeric_limits<uint32_t>::max());
Kevin Rocardc9963522017-03-10 18:47:37 -08001052}
1053
1054TEST_P(InputStreamTest, PrepareForReadingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001055 doc::test(
1056 "Preparing a stream for reading with a overflowing sized buffer should "
1057 "fail");
Kevin Rocardc9963522017-03-10 18:47:37 -08001058 auto uintMax = std::numeric_limits<uint32_t>::max();
1059 testPrepareForReading(stream.get(), uintMax, uintMax);
1060}
1061
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001062TEST_P(InputStreamTest, GetInputFramesLost) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001063 doc::test(
1064 "The number of frames lost on a never started stream should be 0");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001065 auto ret = stream->getInputFramesLost();
1066 ASSERT_TRUE(ret.isOk());
1067 uint32_t framesLost{ret};
1068 ASSERT_EQ(0U, framesLost);
1069}
1070
Kevin Rocardc9963522017-03-10 18:47:37 -08001071TEST_P(InputStreamTest, getCapturePosition) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001072 doc::test(
1073 "The capture position of a non prepared stream should not be "
1074 "retrievable");
Kevin Rocardc9963522017-03-10 18:47:37 -08001075 uint64_t frames;
1076 uint64_t time;
1077 ASSERT_OK(stream->getCapturePosition(returnIn(res, frames, time)));
1078 ASSERT_RESULT(invalidStateOrNotSupported, res);
1079}
1080
1081//////////////////////////////////////////////////////////////////////////////
Kevin Rocard624800c2017-03-10 18:47:37 -08001082///////////////////////////////// StreamIn ///////////////////////////////////
1083//////////////////////////////////////////////////////////////////////////////
1084
1085TEST_P(OutputStreamTest, getLatency) {
1086 doc::test("Make sure latency is over 0");
1087 auto result = stream->getLatency();
1088 ASSERT_TRUE(result.isOk());
1089 ASSERT_GT(result, 0U);
1090}
1091
1092TEST_P(OutputStreamTest, setVolume) {
1093 doc::test("Try to set the output volume");
1094 auto result = stream->setVolume(1, 1);
1095 ASSERT_TRUE(result.isOk());
1096 if (result == Result::NOT_SUPPORTED) {
1097 doc::partialTest("setVolume is not supported");
1098 return;
1099 }
Kevin Rocard72e50e22017-05-05 14:02:55 -07001100 testUnitaryGain(
1101 [this](float volume) { return stream->setVolume(volume, volume); });
Kevin Rocard624800c2017-03-10 18:47:37 -08001102}
1103
Kevin Rocard72e50e22017-05-05 14:02:55 -07001104static void testPrepareForWriting(IStreamOut* stream, uint32_t frameSize,
1105 uint32_t framesCount) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001106 Result res;
1107 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001108 ASSERT_OK(stream->prepareForWriting(
1109 frameSize, framesCount,
1110 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocard624800c2017-03-10 18:47:37 -08001111 EXPECT_RESULT(invalidArgsOrNotSupported, res);
1112}
1113
Kevin Rocard195205b2017-05-02 18:34:59 -07001114TEST_P(OutputStreamTest, PrepareForWriteWithZeroBuffer) {
1115 doc::test(
1116 "Preparing a stream for writing with a 0 sized buffer should fail");
1117 testPrepareForWriting(stream.get(), 0, 0);
1118}
1119
Kevin Rocard624800c2017-03-10 18:47:37 -08001120TEST_P(OutputStreamTest, PrepareForWriteWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001121 doc::test(
1122 "Preparing a stream for writing with a 2^32 sized buffer should fail");
1123 testPrepareForWriting(stream.get(), 1,
1124 std::numeric_limits<uint32_t>::max());
Kevin Rocard624800c2017-03-10 18:47:37 -08001125}
1126
1127TEST_P(OutputStreamTest, PrepareForWritingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001128 doc::test(
1129 "Preparing a stream for writing with a overflowing sized buffer should "
1130 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001131 auto uintMax = std::numeric_limits<uint32_t>::max();
1132 testPrepareForWriting(stream.get(), uintMax, uintMax);
1133}
1134
1135struct Capability {
1136 Capability(IStreamOut* stream) {
1137 EXPECT_OK(stream->supportsPauseAndResume(returnIn(pause, resume)));
1138 auto ret = stream->supportsDrain();
1139 EXPECT_TRUE(ret.isOk());
1140 if (ret.isOk()) {
1141 drain = ret;
1142 }
1143 }
1144 bool pause = false;
1145 bool resume = false;
1146 bool drain = false;
1147};
1148
1149TEST_P(OutputStreamTest, SupportsPauseAndResumeAndDrain) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001150 doc::test(
1151 "Implementation must expose pause, resume and drain capabilities");
Kevin Rocard624800c2017-03-10 18:47:37 -08001152 Capability(stream.get());
1153}
1154
1155TEST_P(OutputStreamTest, GetRenderPosition) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001156 doc::test("The render position should be 0 on a not started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001157 uint32_t dspFrames;
1158 ASSERT_OK(stream->getRenderPosition(returnIn(res, dspFrames)));
1159 if (res == Result::NOT_SUPPORTED) {
1160 doc::partialTest("getRenderPosition is not supported");
1161 return;
1162 }
1163 ASSERT_OK(res);
1164 ASSERT_EQ(0U, dspFrames);
1165}
1166
1167TEST_P(OutputStreamTest, GetNextWriteTimestamp) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001168 doc::test("The render position of a stream just created should be 0");
Kevin Rocard624800c2017-03-10 18:47:37 -08001169 uint64_t timestampUs;
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001170 ASSERT_OK(stream->getNextWriteTimestamp(returnIn(res, timestampUs)));
Kevin Rocard624800c2017-03-10 18:47:37 -08001171 if (res == Result::NOT_SUPPORTED) {
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001172 doc::partialTest("getNextWriteTimestamp is not supported");
Kevin Rocard624800c2017-03-10 18:47:37 -08001173 return;
1174 }
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001175 ASSERT_EQ(Result::INVALID_STATE, res);
Kevin Rocard624800c2017-03-10 18:47:37 -08001176}
1177
1178/** Stub implementation of out stream callback. */
1179class MockOutCallbacks : public IStreamOutCallback {
1180 Return<void> onWriteReady() override { return {}; }
1181 Return<void> onDrainReady() override { return {}; }
1182 Return<void> onError() override { return {}; }
1183};
1184
Kevin Rocard72e50e22017-05-05 14:02:55 -07001185static bool isAsyncModeSupported(IStreamOut* stream) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001186 auto res = stream->setCallback(new MockOutCallbacks);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001187 stream->clearCallback(); // try to restore the no callback state, ignore
1188 // any error
1189 auto okOrNotSupported = {Result::OK, Result::NOT_SUPPORTED};
Kevin Rocard624800c2017-03-10 18:47:37 -08001190 EXPECT_RESULT(okOrNotSupported, res);
1191 return res.isOk() ? res == Result::OK : false;
1192}
1193
1194TEST_P(OutputStreamTest, SetCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001195 doc::test(
1196 "If supported, registering callback for async operation should never "
1197 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001198 if (!isAsyncModeSupported(stream.get())) {
1199 doc::partialTest("The stream does not support async operations");
1200 return;
1201 }
1202 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1203 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1204}
1205
1206TEST_P(OutputStreamTest, clearCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001207 doc::test(
1208 "If supported, clearing a callback to go back to sync operation should "
1209 "not fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001210 if (!isAsyncModeSupported(stream.get())) {
1211 doc::partialTest("The stream does not support async operations");
1212 return;
1213 }
1214 // TODO: Clarify if clearing a non existing callback should fail
1215 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1216 ASSERT_OK(stream->clearCallback());
1217}
1218
1219TEST_P(OutputStreamTest, Resume) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001220 doc::test(
1221 "If supported, a stream should fail to resume if not previously "
1222 "paused");
Kevin Rocard624800c2017-03-10 18:47:37 -08001223 if (!Capability(stream.get()).resume) {
1224 doc::partialTest("The output stream does not support resume");
1225 return;
1226 }
1227 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1228}
1229
1230TEST_P(OutputStreamTest, Pause) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001231 doc::test(
1232 "If supported, a stream should fail to pause if not previously "
1233 "started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001234 if (!Capability(stream.get()).pause) {
1235 doc::partialTest("The output stream does not support pause");
1236 return;
1237 }
1238 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1239}
1240
Kevin Rocard72e50e22017-05-05 14:02:55 -07001241static void testDrain(IStreamOut* stream, AudioDrain type) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001242 if (!Capability(stream).drain) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001243 doc::partialTest("The output stream does not support drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001244 return;
1245 }
1246 ASSERT_RESULT(Result::OK, stream->drain(type));
1247}
1248
1249TEST_P(OutputStreamTest, DrainAll) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001250 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001251 testDrain(stream.get(), AudioDrain::ALL);
1252}
1253
1254TEST_P(OutputStreamTest, DrainEarlyNotify) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001255 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001256 testDrain(stream.get(), AudioDrain::EARLY_NOTIFY);
1257}
1258
1259TEST_P(OutputStreamTest, FlushStop) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001260 doc::test("If supported, a stream should always succeed to flush");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001261 auto ret = stream->flush();
1262 ASSERT_TRUE(ret.isOk());
1263 if (ret == Result::NOT_SUPPORTED) {
1264 doc::partialTest("Flush is not supported");
1265 return;
1266 }
1267 ASSERT_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001268}
1269
1270TEST_P(OutputStreamTest, GetPresentationPositionStop) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001271 doc::test(
1272 "If supported, a stream should always succeed to retrieve the "
1273 "presentation position");
Kevin Rocard624800c2017-03-10 18:47:37 -08001274 uint64_t frames;
1275 TimeSpec mesureTS;
1276 ASSERT_OK(stream->getPresentationPosition(returnIn(res, frames, mesureTS)));
1277 if (res == Result::NOT_SUPPORTED) {
1278 doc::partialTest("getpresentationPosition is not supported");
1279 return;
1280 }
1281 ASSERT_EQ(0U, frames);
1282
1283 struct timespec currentTS;
1284 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currentTS)) << errno;
1285
Kevin Rocard72e50e22017-05-05 14:02:55 -07001286 auto toMicroSec = [](uint64_t sec, auto nsec) {
1287 return sec * 1e+6 + nsec / 1e+3;
1288 };
Kevin Rocard624800c2017-03-10 18:47:37 -08001289 auto currentTime = toMicroSec(currentTS.tv_sec, currentTS.tv_nsec);
1290 auto mesureTime = toMicroSec(mesureTS.tvSec, mesureTS.tvNSec);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001291 ASSERT_PRED2([](auto c, auto m) { return c - m < 1e+6; }, currentTime,
1292 mesureTime);
Kevin Rocard624800c2017-03-10 18:47:37 -08001293}
1294
Kevin Rocard624800c2017-03-10 18:47:37 -08001295//////////////////////////////////////////////////////////////////////////////
Kevin Rocard3c405a72017-03-08 16:46:51 -08001296/////////////////////////////// PrimaryDevice ////////////////////////////////
1297//////////////////////////////////////////////////////////////////////////////
1298
1299TEST_F(AudioPrimaryHidlTest, setVoiceVolume) {
1300 doc::test("Make sure setVoiceVolume only succeed if volume is in [0,1]");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001301 testUnitaryGain(
1302 [this](float volume) { return device->setVoiceVolume(volume); });
Kevin Rocard3c405a72017-03-08 16:46:51 -08001303}
1304
1305TEST_F(AudioPrimaryHidlTest, setMode) {
Kevin Rocard04364ed2017-05-02 18:16:00 -07001306 doc::test(
1307 "Make sure setMode always succeeds if mode is valid "
1308 "and fails otherwise");
1309 // Test Invalid values
1310 for (AudioMode mode :
1311 {AudioMode::INVALID, AudioMode::CURRENT, AudioMode::CNT}) {
1312 SCOPED_TRACE("mode=" + toString(mode));
1313 ASSERT_RESULT(Result::INVALID_ARGUMENTS, device->setMode(mode));
1314 }
1315 // Test valid values
Kevin Rocard72e50e22017-05-05 14:02:55 -07001316 for (AudioMode mode :
1317 {AudioMode::IN_CALL, AudioMode::IN_COMMUNICATION, AudioMode::RINGTONE,
Kevin Rocard72e50e22017-05-05 14:02:55 -07001318 AudioMode::NORMAL /* Make sure to leave the test in normal mode */}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -08001319 SCOPED_TRACE("mode=" + toString(mode));
1320 ASSERT_OK(device->setMode(mode));
1321 }
Kevin Rocard3c405a72017-03-08 16:46:51 -08001322}
1323
Kevin Rocard3c405a72017-03-08 16:46:51 -08001324TEST_F(BoolAccessorPrimaryHidlTest, BtScoNrecEnabled) {
1325 doc::test("Query and set the BT SCO NR&EC state");
1326 testOptionalAccessors("BtScoNrecEnabled", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001327 &IPrimaryDevice::setBtScoNrecEnabled,
1328 &IPrimaryDevice::getBtScoNrecEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001329}
1330
1331TEST_F(BoolAccessorPrimaryHidlTest, setGetBtScoWidebandEnabled) {
1332 doc::test("Query and set the SCO whideband state");
1333 testOptionalAccessors("BtScoWideband", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001334 &IPrimaryDevice::setBtScoWidebandEnabled,
1335 &IPrimaryDevice::getBtScoWidebandEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001336}
1337
1338using TtyModeAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<TtyMode>;
1339TEST_F(TtyModeAccessorPrimaryHidlTest, setGetTtyMode) {
1340 doc::test("Query and set the TTY mode state");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001341 testOptionalAccessors(
1342 "TTY mode", {TtyMode::OFF, TtyMode::HCO, TtyMode::VCO, TtyMode::FULL},
1343 &IPrimaryDevice::setTtyMode, &IPrimaryDevice::getTtyMode);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001344}
1345
1346TEST_F(BoolAccessorPrimaryHidlTest, setGetHac) {
1347 doc::test("Query and set the HAC state");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001348 testAccessors("HAC", {true, false, true}, &IPrimaryDevice::setHacEnabled,
1349 &IPrimaryDevice::getHacEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001350}
1351
1352//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -08001353//////////////////// Clean caches on global tear down ////////////////////////
1354//////////////////////////////////////////////////////////////////////////////
1355
1356int main(int argc, char** argv) {
1357 environment = new Environment;
1358 ::testing::AddGlobalTestEnvironment(environment);
1359 ::testing::InitGoogleTest(&argc, argv);
1360 int status = RUN_ALL_TESTS();
1361 LOG(INFO) << "Test result = " << status;
1362 return status;
1363}