blob: 27f7aa809612f00ff36c2f0523d6d6a52d92738d [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
Kevin Rocard96f46c42017-05-08 11:53:07 -070086const char* getTestName() {
87 return ::testing::UnitTest::GetInstance()->current_test_info()->name();
88}
89
Kevin Rocardf0357882017-02-10 16:19:28 -080090namespace doc {
91/** Document the current test case.
Kevin Rocard72e50e22017-05-05 14:02:55 -070092 * Eg: calling `doc::test("Dump the state of the hal")` in the "debugDump" test
93 * will output:
94 * <testcase name="debugDump" status="run" time="6"
95 * classname="AudioPrimaryHidlTest"
96 description="Dump the state of the hal." />
97 * see
98 https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#logging-additional-information
Kevin Rocardf0357882017-02-10 16:19:28 -080099 */
100void test(const std::string& testCaseDocumentation) {
101 ::testing::Test::RecordProperty("description", testCaseDocumentation);
102}
103
Kevin Rocard72e50e22017-05-05 14:02:55 -0700104/** Document why a test was not fully run. Usually due to an optional feature
105 * not implemented. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800106void partialTest(const std::string& reason) {
Kevin Rocard96f46c42017-05-08 11:53:07 -0700107 LOG(INFO) << "Test " << getTestName() << " partially run: " << reason;
Kevin Rocardf0357882017-02-10 16:19:28 -0800108 ::testing::Test::RecordProperty("partialyRunTest", reason);
109}
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700110
111/** Add a note to the test. */
112void note(const std::string& note) {
Kevin Rocard96f46c42017-05-08 11:53:07 -0700113 LOG(INFO) << "Test " << getTestName() << " noted: " << note;
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700114 ::testing::Test::RecordProperty("note", note);
115}
Kevin Rocardf0357882017-02-10 16:19:28 -0800116}
117
118// Register callback for static object destruction
119// Avoid destroying static objects after main return.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700120// Post main return destruction leads to incorrect gtest timing measurements as
121// well as harder
Kevin Rocardf0357882017-02-10 16:19:28 -0800122// debuging if anything goes wrong during destruction.
123class Environment : public ::testing::Environment {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700124 public:
125 using TearDownFunc = std::function<void()>;
126 void registerTearDown(TearDownFunc&& tearDown) {
127 tearDowns.push_back(std::move(tearDown));
Kevin Rocardf0357882017-02-10 16:19:28 -0800128 }
129
Kevin Rocard72e50e22017-05-05 14:02:55 -0700130 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800131 void TearDown() override {
132 // Call the tear downs in reverse order of insertion
133 for (auto& tearDown : tearDowns) {
134 tearDown();
135 }
136 }
137 std::list<TearDownFunc> tearDowns;
138};
139// Instance to register global tearDown
140static Environment* environment;
141
Yuexi Ma161b5642017-03-10 13:44:22 -0800142class HidlTest : public ::testing::VtsHalHidlTargetTestBase {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700143 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800144 // Convenient member to store results
145 Result res;
146};
147
148//////////////////////////////////////////////////////////////////////////////
149////////////////////// getService audio_devices_factory //////////////////////
150//////////////////////////////////////////////////////////////////////////////
151
152// Test all audio devices
153class AudioHidlTest : public HidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700154 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800155 void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700156 ASSERT_NO_FATAL_FAILURE(HidlTest::SetUp()); // setup base
Kevin Rocardf0357882017-02-10 16:19:28 -0800157
158 if (devicesFactory == nullptr) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700159 environment->registerTearDown([] { devicesFactory.clear(); });
160 devicesFactory = ::testing::VtsHalHidlTargetTestBase::getService<
161 IDevicesFactory>();
Kevin Rocardf0357882017-02-10 16:19:28 -0800162 }
163 ASSERT_TRUE(devicesFactory != nullptr);
164 }
165
Kevin Rocard72e50e22017-05-05 14:02:55 -0700166 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800167 // Cache the devicesFactory retrieval to speed up each test by ~0.5s
168 static sp<IDevicesFactory> devicesFactory;
169};
170sp<IDevicesFactory> AudioHidlTest::devicesFactory;
171
172TEST_F(AudioHidlTest, GetAudioDevicesFactoryService) {
173 doc::test("test the getService (called in SetUp)");
174}
175
Mikhail Naganov8604a732017-04-24 09:29:22 -0700176TEST_F(AudioHidlTest, OpenDeviceInvalidParameter) {
177 doc::test("test passing an invalid parameter to openDevice");
178 IDevicesFactory::Result result;
179 sp<IDevice> device;
180 ASSERT_OK(devicesFactory->openDevice(IDevicesFactory::Device(-1),
181 returnIn(result, device)));
182 ASSERT_EQ(IDevicesFactory::Result::INVALID_ARGUMENTS, result);
183 ASSERT_TRUE(device == nullptr);
184}
185
Kevin Rocardf0357882017-02-10 16:19:28 -0800186//////////////////////////////////////////////////////////////////////////////
187/////////////////////////////// openDevice primary ///////////////////////////
188//////////////////////////////////////////////////////////////////////////////
189
190// Test the primary device
191class AudioPrimaryHidlTest : public AudioHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700192 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800193 /** Primary HAL test are NOT thread safe. */
194 void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700195 ASSERT_NO_FATAL_FAILURE(AudioHidlTest::SetUp()); // setup base
Kevin Rocardf0357882017-02-10 16:19:28 -0800196
197 if (device == nullptr) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800198 IDevicesFactory::Result result;
Kevin Rocard3c405a72017-03-08 16:46:51 -0800199 sp<IDevice> baseDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700200 ASSERT_OK(
201 devicesFactory->openDevice(IDevicesFactory::Device::PRIMARY,
202 returnIn(result, baseDevice)));
Kevin Rocardfba442a2017-03-31 19:34:41 -0700203 ASSERT_OK(result);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800204 ASSERT_TRUE(baseDevice != nullptr);
205
Kevin Rocard72e50e22017-05-05 14:02:55 -0700206 environment->registerTearDown([] { device.clear(); });
Kevin Rocard3c405a72017-03-08 16:46:51 -0800207 device = IPrimaryDevice::castFrom(baseDevice);
208 ASSERT_TRUE(device != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800209 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800210 }
211
Kevin Rocard72e50e22017-05-05 14:02:55 -0700212 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800213 // Cache the device opening to speed up each test by ~0.5s
Kevin Rocard3c405a72017-03-08 16:46:51 -0800214 static sp<IPrimaryDevice> device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800215};
Kevin Rocard3c405a72017-03-08 16:46:51 -0800216sp<IPrimaryDevice> AudioPrimaryHidlTest::device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800217
218TEST_F(AudioPrimaryHidlTest, OpenPrimaryDevice) {
219 doc::test("Test the openDevice (called in SetUp)");
220}
221
222TEST_F(AudioPrimaryHidlTest, Init) {
223 doc::test("Test that the audio primary hal initialized correctly");
224 ASSERT_OK(device->initCheck());
225}
226
227//////////////////////////////////////////////////////////////////////////////
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800228///////////////////// {set,get}{Master,Mic}{Mute,Volume} /////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800229//////////////////////////////////////////////////////////////////////////////
230
Kevin Rocard3c405a72017-03-08 16:46:51 -0800231template <class Property>
232class AccessorPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700233 protected:
Kevin Rocard3c405a72017-03-08 16:46:51 -0800234 /** Test a property getter and setter. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800235 template <class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700236 void testAccessors(const string& propertyName,
237 const vector<Property>& valuesToTest, Setter setter,
238 Getter getter,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800239 const vector<Property>& invalidValues = {}) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700240 Property initialValue; // Save initial value to restore it at the end
241 // of the test
Kevin Rocard3c405a72017-03-08 16:46:51 -0800242 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
243 ASSERT_OK(res);
244
245 for (Property setValue : valuesToTest) {
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800246 SCOPED_TRACE("Test " + propertyName + " getter and setter for " +
247 testing::PrintToString(setValue));
Kevin Rocard3c405a72017-03-08 16:46:51 -0800248 ASSERT_OK((device.get()->*setter)(setValue));
249 Property getValue;
250 // Make sure the getter returns the same value just set
251 ASSERT_OK((device.get()->*getter)(returnIn(res, getValue)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800252 ASSERT_OK(res);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800253 EXPECT_EQ(setValue, getValue);
Kevin Rocardf0357882017-02-10 16:19:28 -0800254 }
Kevin Rocard3c405a72017-03-08 16:46:51 -0800255
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800256 for (Property invalidValue : invalidValues) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700257 SCOPED_TRACE("Try to set " + propertyName +
258 " with the invalid value " +
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800259 testing::PrintToString(invalidValue));
Kevin Rocard72e50e22017-05-05 14:02:55 -0700260 EXPECT_RESULT(Result::INVALID_ARGUMENTS,
261 (device.get()->*setter)(invalidValue));
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800262 }
263
Kevin Rocard72e50e22017-05-05 14:02:55 -0700264 ASSERT_OK(
265 (device.get()->*setter)(initialValue)); // restore initial value
Kevin Rocard3c405a72017-03-08 16:46:51 -0800266 }
267
268 /** Test the getter and setter of an optional feature. */
269 template <class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700270 void testOptionalAccessors(const string& propertyName,
271 const vector<Property>& valuesToTest,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800272 Setter setter, Getter getter,
273 const vector<Property>& invalidValues = {}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -0800274 doc::test("Test the optional " + propertyName + " getters and setter");
275 {
276 SCOPED_TRACE("Test feature support by calling the getter");
277 Property initialValue;
278 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
279 if (res == Result::NOT_SUPPORTED) {
280 doc::partialTest(propertyName + " getter is not supported");
281 return;
282 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700283 ASSERT_OK(res); // If it is supported it must succeed
Kevin Rocard3c405a72017-03-08 16:46:51 -0800284 }
285 // The feature is supported, test it
Kevin Rocard72e50e22017-05-05 14:02:55 -0700286 testAccessors(propertyName, valuesToTest, setter, getter,
287 invalidValues);
Kevin Rocardf0357882017-02-10 16:19:28 -0800288 }
289};
290
Kevin Rocard3c405a72017-03-08 16:46:51 -0800291using BoolAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<bool>;
292
Kevin Rocardf0357882017-02-10 16:19:28 -0800293TEST_F(BoolAccessorPrimaryHidlTest, MicMuteTest) {
294 doc::test("Check that the mic can be muted and unmuted");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700295 testAccessors("mic mute", {true, false, true}, &IDevice::setMicMute,
296 &IDevice::getMicMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800297 // TODO: check that the mic is really muted (all sample are 0)
298}
299
300TEST_F(BoolAccessorPrimaryHidlTest, MasterMuteTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700301 doc::test(
302 "If master mute is supported, try to mute and unmute the master "
303 "output");
Kevin Rocard3c405a72017-03-08 16:46:51 -0800304 testOptionalAccessors("master mute", {true, false, true},
305 &IDevice::setMasterMute, &IDevice::getMasterMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800306 // TODO: check that the master volume is really muted
307}
308
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800309using FloatAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<float>;
310TEST_F(FloatAccessorPrimaryHidlTest, MasterVolumeTest) {
311 doc::test("Test the master volume if supported");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700312 testOptionalAccessors("master volume", {0, 0.5, 1},
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800313 &IDevice::setMasterVolume, &IDevice::getMasterVolume,
314 {-0.1, 1.1, NAN, INFINITY, -INFINITY,
315 1 + std::numeric_limits<float>::epsilon()});
316 // TODO: check that the master volume is really changed
317}
318
Kevin Rocardf0357882017-02-10 16:19:28 -0800319//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800320//////////////////////////////// AudioPatches ////////////////////////////////
321//////////////////////////////////////////////////////////////////////////////
322
323class AudioPatchPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700324 protected:
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800325 bool areAudioPatchesSupported() {
326 auto result = device->supportsAudioPatches();
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700327 EXPECT_IS_OK(result);
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800328 return result;
329 }
330};
331
332TEST_F(AudioPatchPrimaryHidlTest, AudioPatches) {
333 doc::test("Test if audio patches are supported");
334 if (!areAudioPatchesSupported()) {
335 doc::partialTest("Audio patches are not supported");
336 return;
337 }
338 // TODO: test audio patches
339}
340
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800341//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800342//////////////// Required and recommended audio format support ///////////////
Kevin Rocard72e50e22017-05-05 14:02:55 -0700343// From:
344// https://source.android.com/compatibility/android-cdd.html#5_4_audio_recording
345// From:
346// https://source.android.com/compatibility/android-cdd.html#5_5_audio_playback
Kevin Rocardf0357882017-02-10 16:19:28 -0800347/////////// TODO: move to the beginning of the file for easier update ////////
348//////////////////////////////////////////////////////////////////////////////
349
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800350class AudioConfigPrimaryTest : public AudioPatchPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700351 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800352 // Cache result ?
353 static const vector<AudioConfig> getRequiredSupportPlaybackAudioConfig() {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700354 return combineAudioConfig(
355 {AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
356 {8000, 11025, 16000, 22050, 32000, 44100},
357 {AudioFormat::PCM_16_BIT});
Kevin Rocardf0357882017-02-10 16:19:28 -0800358 }
359
Kevin Rocard72e50e22017-05-05 14:02:55 -0700360 static const vector<AudioConfig>
361 getRecommendedSupportPlaybackAudioConfig() {
362 return combineAudioConfig(
363 {AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
364 {24000, 48000}, {AudioFormat::PCM_16_BIT});
Kevin Rocardf0357882017-02-10 16:19:28 -0800365 }
366
367 static const vector<AudioConfig> getSupportedPlaybackAudioConfig() {
368 // TODO: retrieve audio config supported by the platform
369 // as declared in the policy configuration
370 return {};
371 }
372
373 static const vector<AudioConfig> getRequiredSupportCaptureAudioConfig() {
374 return combineAudioConfig({AudioChannelMask::IN_MONO},
375 {8000, 11025, 16000, 44100},
376 {AudioFormat::PCM_16_BIT});
377 }
378 static const vector<AudioConfig> getRecommendedSupportCaptureAudioConfig() {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700379 return combineAudioConfig({AudioChannelMask::IN_STEREO}, {22050, 48000},
Kevin Rocardf0357882017-02-10 16:19:28 -0800380 {AudioFormat::PCM_16_BIT});
381 }
382 static const vector<AudioConfig> getSupportedCaptureAudioConfig() {
383 // TODO: retrieve audio config supported by the platform
384 // as declared in the policy configuration
385 return {};
386 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700387
388 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800389 static const vector<AudioConfig> combineAudioConfig(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700390 vector<AudioChannelMask> channelMasks, vector<uint32_t> sampleRates,
391 vector<AudioFormat> formats) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800392 vector<AudioConfig> configs;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700393 for (auto channelMask : channelMasks) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800394 for (auto sampleRate : sampleRates) {
395 for (auto format : formats) {
396 AudioConfig config{};
397 // leave offloadInfo to 0
398 config.channelMask = channelMask;
399 config.sampleRateHz = sampleRate;
400 config.format = format;
401 // FIXME: leave frameCount to 0 ?
402 configs.push_back(config);
403 }
404 }
405 }
406 return configs;
407 }
408};
409
Kevin Rocard9c369142017-03-08 17:17:25 -0800410/** Generate a test name based on an audio config.
411 *
412 * As the only parameter changing are channel mask and sample rate,
413 * only print those ones in the test name.
414 */
Kevin Rocard72e50e22017-05-05 14:02:55 -0700415static string generateTestName(
416 const testing::TestParamInfo<AudioConfig>& info) {
Kevin Rocard9c369142017-03-08 17:17:25 -0800417 const AudioConfig& config = info.param;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700418 return to_string(info.index) + "__" + to_string(config.sampleRateHz) + "_" +
419 // "MONO" is more clear than "FRONT_LEFT"
420 ((config.channelMask == AudioChannelMask::OUT_MONO ||
421 config.channelMask == AudioChannelMask::IN_MONO)
422 ? "MONO"
423 : toString(config.channelMask));
Kevin Rocard9c369142017-03-08 17:17:25 -0800424}
425
Kevin Rocardf0357882017-02-10 16:19:28 -0800426//////////////////////////////////////////////////////////////////////////////
427///////////////////////////// getInputBufferSize /////////////////////////////
428//////////////////////////////////////////////////////////////////////////////
429
Kevin Rocard72e50e22017-05-05 14:02:55 -0700430// FIXME: execute input test only if platform declares
431// android.hardware.microphone
Kevin Rocardf0357882017-02-10 16:19:28 -0800432// how to get this value ? is it a property ???
433
Kevin Rocard72e50e22017-05-05 14:02:55 -0700434class AudioCaptureConfigPrimaryTest
435 : public AudioConfigPrimaryTest,
436 public ::testing::WithParamInterface<AudioConfig> {
437 protected:
438 void inputBufferSizeTest(const AudioConfig& audioConfig,
439 bool supportRequired) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800440 uint64_t bufferSize;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700441 ASSERT_OK(
442 device->getInputBufferSize(audioConfig, returnIn(res, bufferSize)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800443
444 switch (res) {
445 case Result::INVALID_ARGUMENTS:
446 EXPECT_FALSE(supportRequired);
447 break;
448 case Result::OK:
449 // Check that the buffer is of a sane size
450 // For now only that it is > 0
451 EXPECT_GT(bufferSize, uint64_t(0));
452 break;
453 default:
Kevin Rocard72e50e22017-05-05 14:02:55 -0700454 FAIL() << "Invalid return status: "
455 << ::testing::PrintToString(res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800456 }
457 }
458};
459
Kevin Rocard72e50e22017-05-05 14:02:55 -0700460// Test that the required capture config and those declared in the policy are
461// indeed supported
Kevin Rocardf0357882017-02-10 16:19:28 -0800462class RequiredInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
463TEST_P(RequiredInputBufferSizeTest, RequiredInputBufferSizeTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700464 doc::test(
465 "Input buffer size must be retrievable for a format with required "
466 "support.");
Kevin Rocardf0357882017-02-10 16:19:28 -0800467 inputBufferSizeTest(GetParam(), true);
468}
469INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700470 RequiredInputBufferSize, RequiredInputBufferSizeTest,
471 ::testing::ValuesIn(
472 AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
473 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800474INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700475 SupportedInputBufferSize, RequiredInputBufferSizeTest,
476 ::testing::ValuesIn(
477 AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
478 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800479
Kevin Rocard72e50e22017-05-05 14:02:55 -0700480// Test that the recommended capture config are supported or lead to a
481// INVALID_ARGUMENTS return
Kevin Rocardf0357882017-02-10 16:19:28 -0800482class OptionalInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
483TEST_P(OptionalInputBufferSizeTest, OptionalInputBufferSizeTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700484 doc::test(
485 "Input buffer size should be retrievable for a format with recommended "
486 "support.");
Kevin Rocardf0357882017-02-10 16:19:28 -0800487 inputBufferSizeTest(GetParam(), false);
488}
489INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700490 RecommendedCaptureAudioConfigSupport, OptionalInputBufferSizeTest,
491 ::testing::ValuesIn(
492 AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
493 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800494
495//////////////////////////////////////////////////////////////////////////////
496/////////////////////////////// setScreenState ///////////////////////////////
497//////////////////////////////////////////////////////////////////////////////
498
499TEST_F(AudioPrimaryHidlTest, setScreenState) {
500 doc::test("Check that the hal can receive the screen state");
501 for (bool turnedOn : {false, true, true, false, false}) {
502 auto ret = device->setScreenState(turnedOn);
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700503 ASSERT_IS_OK(ret);
Kevin Rocardf0357882017-02-10 16:19:28 -0800504 Result result = ret;
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700505 auto okOrNotSupported = {Result::OK, Result::NOT_SUPPORTED};
506 ASSERT_RESULT(okOrNotSupported, result);
Kevin Rocardf0357882017-02-10 16:19:28 -0800507 }
508}
509
510//////////////////////////////////////////////////////////////////////////////
511//////////////////////////// {get,set}Parameters /////////////////////////////
512//////////////////////////////////////////////////////////////////////////////
513
514TEST_F(AudioPrimaryHidlTest, getParameters) {
515 doc::test("Check that the hal can set and get parameters");
516 hidl_vec<hidl_string> keys;
517 hidl_vec<ParameterValue> values;
518 ASSERT_OK(device->getParameters(keys, returnIn(res, values)));
519 ASSERT_OK(device->setParameters(values));
520 values.resize(0);
521 ASSERT_OK(device->setParameters(values));
522}
523
524//////////////////////////////////////////////////////////////////////////////
525//////////////////////////////// debugDebug //////////////////////////////////
526//////////////////////////////////////////////////////////////////////////////
527
Kevin Rocardb9031242017-03-13 12:20:54 -0700528template <class DebugDump>
529static void testDebugDump(DebugDump debugDump) {
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700530 // Dump in a temporary file
531 // Note that SELinux must be deactivate for this test to work
Kevin Rocardf0357882017-02-10 16:19:28 -0800532 FILE* file = tmpfile();
533 ASSERT_NE(nullptr, file) << errno;
534
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700535 // Wrap the temporary file file descriptor in a native handle
Kevin Rocardf0357882017-02-10 16:19:28 -0800536 auto* nativeHandle = native_handle_create(1, 0);
537 ASSERT_NE(nullptr, nativeHandle);
538 nativeHandle->data[0] = fileno(file);
539
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700540 // Wrap this native handle in a hidl handle
Kevin Rocardf0357882017-02-10 16:19:28 -0800541 hidl_handle handle;
542 handle.setTo(nativeHandle, true /*take ownership*/);
543
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700544 ASSERT_OK(debugDump(handle));
545
546 // Check that at least one bit was written by the hal
Kevin Rocardee771e92017-03-08 17:17:25 -0800547 // TODO: debugDump does not return a Result.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700548 // This mean that the hal can not report that it not implementing the
549 // function.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700550 rewind(file); // can not fail
Kevin Rocardf0357882017-02-10 16:19:28 -0800551 char buff;
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700552 if (fread(&buff, sizeof(buff), 1, file) != 1) {
553 doc::note("debugDump does not seem implemented");
554 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800555 EXPECT_EQ(0, fclose(file)) << errno;
556}
557
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700558TEST_F(AudioPrimaryHidlTest, DebugDump) {
Kevin Rocardb9031242017-03-13 12:20:54 -0700559 doc::test("Check that the hal can dump its state without error");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700560 testDebugDump(
561 [this](const auto& handle) { return device->debugDump(handle); });
Kevin Rocardb9031242017-03-13 12:20:54 -0700562}
563
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700564TEST_F(AudioPrimaryHidlTest, DebugDumpInvalidArguments) {
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700565 doc::test("Check that the hal dump doesn't crash on invalid arguments");
566 ASSERT_OK(device->debugDump(hidl_handle()));
567}
568
Kevin Rocardf0357882017-02-10 16:19:28 -0800569//////////////////////////////////////////////////////////////////////////////
570////////////////////////// open{Output,Input}Stream //////////////////////////
571//////////////////////////////////////////////////////////////////////////////
572
573template <class Stream>
574class OpenStreamTest : public AudioConfigPrimaryTest,
575 public ::testing::WithParamInterface<AudioConfig> {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700576 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800577 template <class Open>
578 void testOpen(Open openStream, const AudioConfig& config) {
579 // FIXME: Open a stream without an IOHandle
580 // This is not required to be accepted by hal implementations
Kevin Rocard72e50e22017-05-05 14:02:55 -0700581 AudioIoHandle ioHandle =
582 (AudioIoHandle)AudioHandleConsts::AUDIO_IO_HANDLE_NONE;
Kevin Rocardf0357882017-02-10 16:19:28 -0800583 AudioConfig suggestedConfig{};
Kevin Rocard72e50e22017-05-05 14:02:55 -0700584 ASSERT_OK(openStream(ioHandle, config,
585 returnIn(res, stream, suggestedConfig)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800586
587 // TODO: only allow failure for RecommendedPlaybackAudioConfig
588 switch (res) {
589 case Result::OK:
590 ASSERT_TRUE(stream != nullptr);
591 audioConfig = config;
592 break;
593 case Result::INVALID_ARGUMENTS:
594 ASSERT_TRUE(stream == nullptr);
595 AudioConfig suggestedConfigRetry;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700596 // Could not open stream with config, try again with the
597 // suggested one
598 ASSERT_OK(
599 openStream(ioHandle, suggestedConfig,
600 returnIn(res, stream, suggestedConfigRetry)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800601 // This time it must succeed
602 ASSERT_OK(res);
Kevin Rocard4aefd1c2017-05-02 18:58:58 -0700603 ASSERT_TRUE(stream != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800604 audioConfig = suggestedConfig;
605 break;
606 default:
Kevin Rocard72e50e22017-05-05 14:02:55 -0700607 FAIL() << "Invalid return status: "
608 << ::testing::PrintToString(res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800609 }
610 open = true;
611 }
612
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800613 Return<Result> closeStream() {
614 open = false;
615 return stream->close();
616 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700617
618 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800619 void TearDown() override {
620 if (open) {
621 ASSERT_OK(stream->close());
622 }
623 }
624
Kevin Rocard72e50e22017-05-05 14:02:55 -0700625 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800626 AudioConfig audioConfig;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800627 DeviceAddress address = {};
Kevin Rocardf0357882017-02-10 16:19:28 -0800628 sp<Stream> stream;
629 bool open = false;
630};
631
632////////////////////////////// openOutputStream //////////////////////////////
633
634class OutputStreamTest : public OpenStreamTest<IStreamOut> {
635 virtual void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700636 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800637 address.device = AudioDevice::OUT_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800638 const AudioConfig& config = GetParam();
Kevin Rocard72e50e22017-05-05 14:02:55 -0700639 AudioOutputFlag flags =
640 AudioOutputFlag::NONE; // TODO: test all flag combination
641 testOpen(
642 [&](AudioIoHandle handle, AudioConfig config, auto cb) {
643 return device->openOutputStream(handle, address, config, flags,
644 cb);
645 },
646 config);
Kevin Rocardf0357882017-02-10 16:19:28 -0800647 }
648};
649TEST_P(OutputStreamTest, OpenOutputStreamTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700650 doc::test(
651 "Check that output streams can be open with the required and "
652 "recommended config");
Kevin Rocardf0357882017-02-10 16:19:28 -0800653 // Open done in SetUp
654}
655INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700656 RequiredOutputStreamConfigSupport, OutputStreamTest,
657 ::testing::ValuesIn(
658 AudioConfigPrimaryTest::getRequiredSupportPlaybackAudioConfig()),
659 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800660INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700661 SupportedOutputStreamConfig, OutputStreamTest,
662 ::testing::ValuesIn(
663 AudioConfigPrimaryTest::getSupportedPlaybackAudioConfig()),
664 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800665
666INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700667 RecommendedOutputStreamConfigSupport, OutputStreamTest,
668 ::testing::ValuesIn(
669 AudioConfigPrimaryTest::getRecommendedSupportPlaybackAudioConfig()),
670 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800671
672////////////////////////////// openInputStream //////////////////////////////
673
674class InputStreamTest : public OpenStreamTest<IStreamIn> {
Kevin Rocardf0357882017-02-10 16:19:28 -0800675 virtual void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700676 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800677 address.device = AudioDevice::IN_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800678 const AudioConfig& config = GetParam();
Kevin Rocard72e50e22017-05-05 14:02:55 -0700679 AudioInputFlag flags =
680 AudioInputFlag::NONE; // TODO: test all flag combination
681 AudioSource source =
682 AudioSource::DEFAULT; // TODO: test all flag combination
683 testOpen(
684 [&](AudioIoHandle handle, AudioConfig config, auto cb) {
685 return device->openInputStream(handle, address, config, flags,
686 source, cb);
687 },
688 config);
Kevin Rocardf0357882017-02-10 16:19:28 -0800689 }
690};
691
692TEST_P(InputStreamTest, OpenInputStreamTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700693 doc::test(
694 "Check that input streams can be open with the required and "
695 "recommended config");
Kevin Rocardf0357882017-02-10 16:19:28 -0800696 // Open done in setup
697}
698INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700699 RequiredInputStreamConfigSupport, InputStreamTest,
700 ::testing::ValuesIn(
701 AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
702 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800703INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700704 SupportedInputStreamConfig, InputStreamTest,
705 ::testing::ValuesIn(
706 AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
707 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800708
709INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700710 RecommendedInputStreamConfigSupport, InputStreamTest,
711 ::testing::ValuesIn(
712 AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
713 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800714
715//////////////////////////////////////////////////////////////////////////////
716////////////////////////////// IStream getters ///////////////////////////////
717//////////////////////////////////////////////////////////////////////////////
718
719/** Unpack the provided result.
720 * If the result is not OK, register a failure and return an undefined value. */
721template <class R>
722static R extract(Return<R> ret) {
723 if (!ret.isOk()) {
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700724 EXPECT_IS_OK(ret);
Kevin Rocardf0357882017-02-10 16:19:28 -0800725 return R{};
726 }
727 return ret;
728}
729
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800730/* Could not find a way to write a test for two parametrized class fixure
731 * thus use this macro do duplicate tests for Input and Output stream */
732#define TEST_IO_STREAM(test_name, documentation, code) \
Kevin Rocard72e50e22017-05-05 14:02:55 -0700733 TEST_P(InputStreamTest, test_name) { \
734 doc::test(documentation); \
735 code; \
736 } \
737 TEST_P(OutputStreamTest, test_name) { \
738 doc::test(documentation); \
739 code; \
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800740 }
741
Kevin Rocard72e50e22017-05-05 14:02:55 -0700742TEST_IO_STREAM(
743 GetFrameCount,
744 "Check that the stream frame count == the one it was opened with",
745 ASSERT_EQ(audioConfig.frameCount, extract(stream->getFrameCount())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800746
Kevin Rocard72e50e22017-05-05 14:02:55 -0700747TEST_IO_STREAM(
748 GetSampleRate,
749 "Check that the stream sample rate == the one it was opened with",
750 ASSERT_EQ(audioConfig.sampleRateHz, extract(stream->getSampleRate())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800751
Kevin Rocard72e50e22017-05-05 14:02:55 -0700752TEST_IO_STREAM(
753 GetChannelMask,
754 "Check that the stream channel mask == the one it was opened with",
755 ASSERT_EQ(audioConfig.channelMask, extract(stream->getChannelMask())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800756
Kevin Rocard72e50e22017-05-05 14:02:55 -0700757TEST_IO_STREAM(GetFormat,
758 "Check that the stream format == the one it was opened with",
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800759 ASSERT_EQ(audioConfig.format, extract(stream->getFormat())))
760
761// TODO: for now only check that the framesize is not incoherent
Kevin Rocard72e50e22017-05-05 14:02:55 -0700762TEST_IO_STREAM(GetFrameSize,
763 "Check that the stream frame size == the one it was opened with",
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800764 ASSERT_GT(extract(stream->getFrameSize()), 0U))
765
Kevin Rocard72e50e22017-05-05 14:02:55 -0700766TEST_IO_STREAM(GetBufferSize,
767 "Check that the stream buffer size== the one it was opened with",
768 ASSERT_GE(extract(stream->getBufferSize()),
769 extract(stream->getFrameSize())));
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800770
Kevin Rocardf0357882017-02-10 16:19:28 -0800771template <class Property, class CapabilityGetter, class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700772static void testCapabilityGetter(const string& name, IStream* stream,
773 Property currentValue,
774 CapabilityGetter capablityGetter,
775 Getter getter, Setter setter) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800776 hidl_vec<Property> capabilities;
777 ASSERT_OK((stream->*capablityGetter)(returnIn(capabilities)));
778 if (capabilities.size() == 0) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700779 // The default hal should probably return a NOT_SUPPORTED if the hal
780 // does not expose
781 // capability retrieval. For now it returns an empty list if not
782 // implemented
Kevin Rocardf0357882017-02-10 16:19:28 -0800783 doc::partialTest(name + " is not supported");
784 return;
785 };
Kevin Rocard72e50e22017-05-05 14:02:55 -0700786 // TODO: This code has never been tested on a hal that supports
787 // getSupportedSampleRates
Kevin Rocardf0357882017-02-10 16:19:28 -0800788 EXPECT_NE(std::find(capabilities.begin(), capabilities.end(), currentValue),
Kevin Rocard72e50e22017-05-05 14:02:55 -0700789 capabilities.end())
Kevin Rocardf0357882017-02-10 16:19:28 -0800790 << "current " << name << " is not in the list of the supported ones "
791 << toString(capabilities);
792
793 // Check that all declared supported values are indeed supported
794 for (auto capability : capabilities) {
795 ASSERT_OK((stream->*setter)(capability));
796 ASSERT_EQ(capability, extract((stream->*getter)()));
797 }
798}
799
Kevin Rocard72e50e22017-05-05 14:02:55 -0700800TEST_IO_STREAM(SupportedSampleRate,
801 "Check that the stream sample rate is declared as supported",
802 testCapabilityGetter("getSupportedSampleRate", stream.get(),
803 extract(stream->getSampleRate()),
804 &IStream::getSupportedSampleRates,
805 &IStream::getSampleRate,
806 &IStream::setSampleRate))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800807
Kevin Rocard72e50e22017-05-05 14:02:55 -0700808TEST_IO_STREAM(SupportedChannelMask,
809 "Check that the stream channel mask is declared as supported",
810 testCapabilityGetter("getSupportedChannelMask", stream.get(),
811 extract(stream->getChannelMask()),
812 &IStream::getSupportedChannelMasks,
813 &IStream::getChannelMask,
814 &IStream::setChannelMask))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800815
Kevin Rocard72e50e22017-05-05 14:02:55 -0700816TEST_IO_STREAM(SupportedFormat,
817 "Check that the stream format is declared as supported",
818 testCapabilityGetter("getSupportedFormat", stream.get(),
819 extract(stream->getFormat()),
820 &IStream::getSupportedFormats,
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800821 &IStream::getFormat, &IStream::setFormat))
822
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800823static void testGetDevice(IStream* stream, AudioDevice expectedDevice) {
Kevin Rocard8f8730c2017-05-02 19:34:29 -0700824 // Unfortunately the interface does not allow the implementation to return
825 // NOT_SUPPORTED
826 // Thus allow NONE as signaling that the call is not supported.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800827 auto ret = stream->getDevice();
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700828 ASSERT_IS_OK(ret);
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800829 AudioDevice device = ret;
Kevin Rocard8f8730c2017-05-02 19:34:29 -0700830 ASSERT_TRUE(device == expectedDevice || device == AudioDevice::NONE)
831 << "Expected: " << ::testing::PrintToString(expectedDevice)
832 << "\n Actual: " << ::testing::PrintToString(device);
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800833}
834
Kevin Rocard72e50e22017-05-05 14:02:55 -0700835TEST_IO_STREAM(GetDevice,
836 "Check that the stream device == the one it was opened with",
837 areAudioPatchesSupported()
838 ? doc::partialTest("Audio patches are supported")
839 : testGetDevice(stream.get(), address.device))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800840
841static void testSetDevice(IStream* stream, const DeviceAddress& address) {
842 DeviceAddress otherAddress = address;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700843 otherAddress.device = (address.device & AudioDevice::BIT_IN) == 0
844 ? AudioDevice::OUT_SPEAKER
845 : AudioDevice::IN_BUILTIN_MIC;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800846 EXPECT_OK(stream->setDevice(otherAddress));
847
Kevin Rocard72e50e22017-05-05 14:02:55 -0700848 ASSERT_OK(stream->setDevice(address)); // Go back to the original value
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800849}
850
Kevin Rocard72e50e22017-05-05 14:02:55 -0700851TEST_IO_STREAM(
852 SetDevice,
853 "Check that the stream can be rerouted to SPEAKER or BUILTIN_MIC",
854 areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported")
855 : testSetDevice(stream.get(), address))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800856
Kevin Rocard72e50e22017-05-05 14:02:55 -0700857static void testGetAudioProperties(IStream* stream,
858 AudioConfig expectedConfig) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800859 uint32_t sampleRateHz;
860 AudioChannelMask mask;
861 AudioFormat format;
862
863 stream->getAudioProperties(returnIn(sampleRateHz, mask, format));
864
Kevin Rocard72e50e22017-05-05 14:02:55 -0700865 // FIXME: the qcom hal it does not currently negotiate the sampleRate &
866 // channel mask
Kevin Rocardd1e98ae2017-03-08 17:17:25 -0800867 EXPECT_EQ(expectedConfig.sampleRateHz, sampleRateHz);
868 EXPECT_EQ(expectedConfig.channelMask, mask);
Kevin Rocardf0357882017-02-10 16:19:28 -0800869 EXPECT_EQ(expectedConfig.format, format);
870}
871
Kevin Rocard72e50e22017-05-05 14:02:55 -0700872TEST_IO_STREAM(
873 GetAudioProperties,
874 "Check that the stream audio properties == the ones it was opened with",
875 testGetAudioProperties(stream.get(), audioConfig))
Kevin Rocardf0357882017-02-10 16:19:28 -0800876
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800877static void testConnectedState(IStream* stream) {
878 DeviceAddress address = {};
879 using AD = AudioDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700880 for (auto device :
881 {AD::OUT_HDMI, AD::OUT_WIRED_HEADPHONE, AD::IN_USB_HEADSET}) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800882 address.device = device;
883
884 ASSERT_OK(stream->setConnectedState(address, true));
885 ASSERT_OK(stream->setConnectedState(address, false));
886 }
887}
888TEST_IO_STREAM(SetConnectedState,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700889 "Check that the stream can be notified of device connection and "
890 "deconnection",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800891 testConnectedState(stream.get()))
892
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700893static auto invalidArgsOrNotSupportedOrOK = {Result::INVALID_ARGUMENTS,
894 Result::NOT_SUPPORTED, Result::OK};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800895TEST_IO_STREAM(SetHwAvSync, "Try to set hardware sync to an invalid value",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700896 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700897 stream->setHwAvSync(666)))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800898
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700899TEST_IO_STREAM(GetHwAvSync, "Get hardware sync can not fail",
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700900 ASSERT_IS_OK(device->getHwAvSync()));
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700901
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700902static void checkGetNoParameter(IStream* stream, hidl_vec<hidl_string> keys,
903 vector<Result> expectedResults) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800904 hidl_vec<ParameterValue> parameters;
905 Result res;
906 ASSERT_OK(stream->getParameters(keys, returnIn(res, parameters)));
907 ASSERT_RESULT(expectedResults, res);
908 if (res == Result::OK) {
Kevin Rocard4aefd1c2017-05-02 18:58:58 -0700909 for (auto& parameter : parameters) {
910 ASSERT_EQ(0U, parameter.value.size()) << toString(parameter);
911 }
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800912 }
913}
914
Kevin Rocard72e50e22017-05-05 14:02:55 -0700915/* Get/Set parameter is intended to be an opaque channel between vendors app and
916 * their HALs.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800917 * Thus can not be meaningfully tested.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800918 */
919TEST_IO_STREAM(getEmptySetParameter, "Retrieve the values of an empty set",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700920 checkGetNoParameter(stream.get(), {} /* keys */, {Result::OK}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800921
Kevin Rocard72e50e22017-05-05 14:02:55 -0700922TEST_IO_STREAM(getNonExistingParameter,
923 "Retrieve the values of an non existing parameter",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700924 checkGetNoParameter(stream.get(),
925 {"Non existing key"} /* keys */,
926 {Result::NOT_SUPPORTED}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800927
Kevin Rocard72e50e22017-05-05 14:02:55 -0700928TEST_IO_STREAM(setEmptySetParameter,
929 "Set the values of an empty set of parameters",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700930 ASSERT_RESULT(Result::OK, stream->setParameters({})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800931
Kevin Rocard72e50e22017-05-05 14:02:55 -0700932TEST_IO_STREAM(
933 setNonExistingParameter, "Set the values of an non existing parameter",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700934 // Unfortunately, the set_parameter legacy interface did not return any
935 // error code when a key is not supported.
936 // To allow implementation to just wrapped the legacy one, consider OK as a
937 // valid result for setting a non existing parameter.
938 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700939 stream->setParameters({{"non existing key", "0"}})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800940
Kevin Rocardb9031242017-03-13 12:20:54 -0700941TEST_IO_STREAM(DebugDump,
942 "Check that a stream can dump its state without error",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700943 testDebugDump([this](const auto& handle) {
944 return stream->debugDump(handle);
945 }))
Kevin Rocardb9031242017-03-13 12:20:54 -0700946
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700947TEST_IO_STREAM(DebugDumpInvalidArguments,
948 "Check that the stream dump doesn't crash on invalid arguments",
949 ASSERT_OK(stream->debugDump(hidl_handle())))
950
Kevin Rocardf0357882017-02-10 16:19:28 -0800951//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800952////////////////////////////// addRemoveEffect ///////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800953//////////////////////////////////////////////////////////////////////////////
954
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800955TEST_IO_STREAM(AddNonExistingEffect, "Adding a non existing effect should fail",
956 ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->addEffect(666)))
Kevin Rocard72e50e22017-05-05 14:02:55 -0700957TEST_IO_STREAM(RemoveNonExistingEffect,
958 "Removing a non existing effect should fail",
959 ASSERT_RESULT(Result::INVALID_ARGUMENTS,
960 stream->removeEffect(666)))
Kevin Rocardf0357882017-02-10 16:19:28 -0800961
Kevin Rocard72e50e22017-05-05 14:02:55 -0700962// TODO: positive tests
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800963
964//////////////////////////////////////////////////////////////////////////////
965/////////////////////////////// Control ////////////////////////////////
966//////////////////////////////////////////////////////////////////////////////
967
968TEST_IO_STREAM(standby, "Make sure the stream can be put in stanby",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700969 ASSERT_OK(stream->standby())) // can not fail
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800970
Kevin Rocard72e50e22017-05-05 14:02:55 -0700971static vector<Result> invalidStateOrNotSupported = {Result::INVALID_STATE,
972 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800973
Kevin Rocard72e50e22017-05-05 14:02:55 -0700974TEST_IO_STREAM(startNoMmap,
975 "Starting a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800976 ASSERT_RESULT(invalidStateOrNotSupported, stream->start()))
977
Kevin Rocard72e50e22017-05-05 14:02:55 -0700978TEST_IO_STREAM(stopNoMmap,
979 "Stopping a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800980 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
981
Kevin Rocard72e50e22017-05-05 14:02:55 -0700982TEST_IO_STREAM(getMmapPositionNoMmap,
983 "Get a stream Mmap position before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800984 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
985
Kevin Rocard72e50e22017-05-05 14:02:55 -0700986TEST_IO_STREAM(close, "Make sure a stream can be closed",
987 ASSERT_OK(closeStream()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800988TEST_IO_STREAM(closeTwice, "Make sure a stream can not be closed twice",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700989 ASSERT_OK(closeStream());
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800990 ASSERT_RESULT(Result::INVALID_STATE, closeStream()))
991
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700992static auto invalidArgsOrNotSupported = {Result::INVALID_ARGUMENTS,
993 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800994static void testCreateTooBigMmapBuffer(IStream* stream) {
995 MmapBufferInfo info;
996 Result res;
997 // Assume that int max is a value too big to be allocated
Kevin Rocard72e50e22017-05-05 14:02:55 -0700998 // This is true currently with a 32bit media server, but might not when it
999 // will run in 64 bit
Kevin Rocard8878b4b2017-03-10 18:47:37 -08001000 auto minSizeFrames = std::numeric_limits<int32_t>::max();
1001 ASSERT_OK(stream->createMmapBuffer(minSizeFrames, returnIn(res, info)));
1002 ASSERT_RESULT(invalidArgsOrNotSupported, res);
Kevin Rocardf0357882017-02-10 16:19:28 -08001003}
1004
Kevin Rocard8878b4b2017-03-10 18:47:37 -08001005TEST_IO_STREAM(CreateTooBigMmapBuffer, "Create mmap buffer too big should fail",
1006 testCreateTooBigMmapBuffer(stream.get()))
1007
Kevin Rocard8878b4b2017-03-10 18:47:37 -08001008static void testGetMmapPositionOfNonMmapedStream(IStream* stream) {
1009 Result res;
1010 MmapPosition position;
1011 ASSERT_OK(stream->getMmapPosition(returnIn(res, position)));
1012 ASSERT_RESULT(invalidArgsOrNotSupported, res);
1013}
1014
Kevin Rocard72e50e22017-05-05 14:02:55 -07001015TEST_IO_STREAM(
1016 GetMmapPositionOfNonMmapedStream,
1017 "Retrieving the mmap position of a non mmaped stream should fail",
1018 testGetMmapPositionOfNonMmapedStream(stream.get()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -08001019
Kevin Rocardf0357882017-02-10 16:19:28 -08001020//////////////////////////////////////////////////////////////////////////////
Kevin Rocardc9963522017-03-10 18:47:37 -08001021///////////////////////////////// StreamIn ///////////////////////////////////
1022//////////////////////////////////////////////////////////////////////////////
1023
1024TEST_P(InputStreamTest, GetAudioSource) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001025 doc::test(
1026 "Retrieving the audio source of an input stream should always succeed");
Kevin Rocardc9963522017-03-10 18:47:37 -08001027 AudioSource source;
1028 ASSERT_OK(stream->getAudioSource(returnIn(res, source)));
Kevin Rocard98390a62017-05-03 11:16:05 -07001029 if (res == Result::NOT_SUPPORTED) {
1030 doc::partialTest("getAudioSource is not supported");
1031 return;
1032 }
Kevin Rocardc9963522017-03-10 18:47:37 -08001033 ASSERT_OK(res);
1034 ASSERT_EQ(AudioSource::DEFAULT, source);
1035}
1036
Kevin Rocard72e50e22017-05-05 14:02:55 -07001037static void testUnitaryGain(std::function<Return<Result>(float)> setGain) {
Kevin Rocardc4f1b2f2017-05-03 11:19:25 -07001038 for (float value :
1039 (float[]){-INFINITY, -1.0, 1.0 + std::numeric_limits<float>::epsilon(),
1040 2.0, INFINITY, NAN}) {
Kevin Rocarda1d6ea42017-05-08 17:08:11 -07001041 EXPECT_RESULT(Result::INVALID_ARGUMENTS, setGain(value)) << "value="
1042 << value;
Kevin Rocardc9963522017-03-10 18:47:37 -08001043 }
Kevin Rocardc4f1b2f2017-05-03 11:19:25 -07001044 // Do not consider -0.0 as an invalid value as it is == with 0.0
1045 for (float value : {-0.0, 0.0, 0.01, 0.5, 0.09, 1.0 /* Restore volume*/}) {
Kevin Rocarda1d6ea42017-05-08 17:08:11 -07001046 EXPECT_OK(setGain(value)) << "value=" << value;
Kevin Rocardc4f1b2f2017-05-03 11:19:25 -07001047 }
Kevin Rocardc9963522017-03-10 18:47:37 -08001048}
1049
Kevin Rocard98390a62017-05-03 11:16:05 -07001050static void testOptionalUnitaryGain(
1051 std::function<Return<Result>(float)> setGain, string debugName) {
1052 auto result = setGain(1);
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001053 ASSERT_IS_OK(result);
Kevin Rocard98390a62017-05-03 11:16:05 -07001054 if (result == Result::NOT_SUPPORTED) {
1055 doc::partialTest(debugName + " is not supported");
1056 return;
1057 }
1058 testUnitaryGain(setGain);
1059}
1060
Kevin Rocardc9963522017-03-10 18:47:37 -08001061TEST_P(InputStreamTest, SetGain) {
1062 doc::test("The gain of an input stream should only be set between [0,1]");
Kevin Rocard98390a62017-05-03 11:16:05 -07001063 testOptionalUnitaryGain(
1064 [this](float volume) { return stream->setGain(volume); },
1065 "InputStream::setGain");
Kevin Rocardc9963522017-03-10 18:47:37 -08001066}
1067
Kevin Rocard72e50e22017-05-05 14:02:55 -07001068static void testPrepareForReading(IStreamIn* stream, uint32_t frameSize,
1069 uint32_t framesCount) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001070 Result res;
1071 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001072 ASSERT_OK(stream->prepareForReading(
1073 frameSize, framesCount,
1074 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocardfcf186b2017-05-03 11:16:05 -07001075 EXPECT_RESULT(Result::INVALID_ARGUMENTS, res);
Kevin Rocardc9963522017-03-10 18:47:37 -08001076}
1077
Kevin Rocard195205b2017-05-02 18:34:59 -07001078TEST_P(InputStreamTest, PrepareForReadingWithZeroBuffer) {
1079 doc::test(
1080 "Preparing a stream for reading with a 0 sized buffer should fail");
1081 testPrepareForReading(stream.get(), 0, 0);
1082}
1083
Kevin Rocardc9963522017-03-10 18:47:37 -08001084TEST_P(InputStreamTest, PrepareForReadingWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001085 doc::test(
1086 "Preparing a stream for reading with a 2^32 sized buffer should fail");
1087 testPrepareForReading(stream.get(), 1,
1088 std::numeric_limits<uint32_t>::max());
Kevin Rocardc9963522017-03-10 18:47:37 -08001089}
1090
1091TEST_P(InputStreamTest, PrepareForReadingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001092 doc::test(
1093 "Preparing a stream for reading with a overflowing sized buffer should "
1094 "fail");
Kevin Rocardc9963522017-03-10 18:47:37 -08001095 auto uintMax = std::numeric_limits<uint32_t>::max();
1096 testPrepareForReading(stream.get(), uintMax, uintMax);
1097}
1098
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001099TEST_P(InputStreamTest, GetInputFramesLost) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001100 doc::test(
1101 "The number of frames lost on a never started stream should be 0");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001102 auto ret = stream->getInputFramesLost();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001103 ASSERT_IS_OK(ret);
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001104 uint32_t framesLost{ret};
1105 ASSERT_EQ(0U, framesLost);
1106}
1107
Kevin Rocardc9963522017-03-10 18:47:37 -08001108TEST_P(InputStreamTest, getCapturePosition) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001109 doc::test(
1110 "The capture position of a non prepared stream should not be "
1111 "retrievable");
Kevin Rocardc9963522017-03-10 18:47:37 -08001112 uint64_t frames;
1113 uint64_t time;
1114 ASSERT_OK(stream->getCapturePosition(returnIn(res, frames, time)));
1115 ASSERT_RESULT(invalidStateOrNotSupported, res);
1116}
1117
1118//////////////////////////////////////////////////////////////////////////////
Kevin Rocard624800c2017-03-10 18:47:37 -08001119///////////////////////////////// StreamIn ///////////////////////////////////
1120//////////////////////////////////////////////////////////////////////////////
1121
1122TEST_P(OutputStreamTest, getLatency) {
1123 doc::test("Make sure latency is over 0");
1124 auto result = stream->getLatency();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001125 ASSERT_IS_OK(result);
Kevin Rocard624800c2017-03-10 18:47:37 -08001126 ASSERT_GT(result, 0U);
1127}
1128
1129TEST_P(OutputStreamTest, setVolume) {
1130 doc::test("Try to set the output volume");
Kevin Rocard98390a62017-05-03 11:16:05 -07001131 testOptionalUnitaryGain(
1132 [this](float volume) { return stream->setVolume(volume, volume); },
1133 "setVolume");
Kevin Rocard624800c2017-03-10 18:47:37 -08001134}
1135
Kevin Rocard72e50e22017-05-05 14:02:55 -07001136static void testPrepareForWriting(IStreamOut* stream, uint32_t frameSize,
1137 uint32_t framesCount) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001138 Result res;
1139 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001140 ASSERT_OK(stream->prepareForWriting(
1141 frameSize, framesCount,
1142 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocardfcf186b2017-05-03 11:16:05 -07001143 EXPECT_RESULT(Result::INVALID_ARGUMENTS, res);
Kevin Rocard624800c2017-03-10 18:47:37 -08001144}
1145
Kevin Rocard195205b2017-05-02 18:34:59 -07001146TEST_P(OutputStreamTest, PrepareForWriteWithZeroBuffer) {
1147 doc::test(
1148 "Preparing a stream for writing with a 0 sized buffer should fail");
1149 testPrepareForWriting(stream.get(), 0, 0);
1150}
1151
Kevin Rocard624800c2017-03-10 18:47:37 -08001152TEST_P(OutputStreamTest, PrepareForWriteWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001153 doc::test(
1154 "Preparing a stream for writing with a 2^32 sized buffer should fail");
1155 testPrepareForWriting(stream.get(), 1,
1156 std::numeric_limits<uint32_t>::max());
Kevin Rocard624800c2017-03-10 18:47:37 -08001157}
1158
1159TEST_P(OutputStreamTest, PrepareForWritingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001160 doc::test(
1161 "Preparing a stream for writing with a overflowing sized buffer should "
1162 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001163 auto uintMax = std::numeric_limits<uint32_t>::max();
1164 testPrepareForWriting(stream.get(), uintMax, uintMax);
1165}
1166
1167struct Capability {
1168 Capability(IStreamOut* stream) {
1169 EXPECT_OK(stream->supportsPauseAndResume(returnIn(pause, resume)));
1170 auto ret = stream->supportsDrain();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001171 EXPECT_IS_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001172 if (ret.isOk()) {
1173 drain = ret;
1174 }
1175 }
1176 bool pause = false;
1177 bool resume = false;
1178 bool drain = false;
1179};
1180
1181TEST_P(OutputStreamTest, SupportsPauseAndResumeAndDrain) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001182 doc::test(
1183 "Implementation must expose pause, resume and drain capabilities");
Kevin Rocard624800c2017-03-10 18:47:37 -08001184 Capability(stream.get());
1185}
1186
Kevin Rocard304b6c82017-05-03 10:57:06 -07001187template <class Value>
1188static void checkInvalidStateOr0(Result res, Value value) {
1189 switch (res) {
1190 case Result::INVALID_STATE:
1191 break;
1192 case Result::OK:
1193 ASSERT_EQ(0U, value);
1194 break;
1195 default:
1196 FAIL() << "Unexpected result " << toString(res);
1197 }
1198}
1199
Kevin Rocard624800c2017-03-10 18:47:37 -08001200TEST_P(OutputStreamTest, GetRenderPosition) {
Kevin Rocard304b6c82017-05-03 10:57:06 -07001201 doc::test("A new stream render position should be 0 or INVALID_STATE");
Kevin Rocard624800c2017-03-10 18:47:37 -08001202 uint32_t dspFrames;
1203 ASSERT_OK(stream->getRenderPosition(returnIn(res, dspFrames)));
1204 if (res == Result::NOT_SUPPORTED) {
1205 doc::partialTest("getRenderPosition is not supported");
1206 return;
1207 }
Kevin Rocard304b6c82017-05-03 10:57:06 -07001208 checkInvalidStateOr0(res, dspFrames);
Kevin Rocard624800c2017-03-10 18:47:37 -08001209}
1210
1211TEST_P(OutputStreamTest, GetNextWriteTimestamp) {
Kevin Rocard304b6c82017-05-03 10:57:06 -07001212 doc::test("A new stream next write timestamp should be 0 or INVALID_STATE");
Kevin Rocard624800c2017-03-10 18:47:37 -08001213 uint64_t timestampUs;
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001214 ASSERT_OK(stream->getNextWriteTimestamp(returnIn(res, timestampUs)));
Kevin Rocard624800c2017-03-10 18:47:37 -08001215 if (res == Result::NOT_SUPPORTED) {
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001216 doc::partialTest("getNextWriteTimestamp is not supported");
Kevin Rocard624800c2017-03-10 18:47:37 -08001217 return;
1218 }
Kevin Rocard304b6c82017-05-03 10:57:06 -07001219 checkInvalidStateOr0(res, timestampUs);
Kevin Rocard624800c2017-03-10 18:47:37 -08001220}
1221
1222/** Stub implementation of out stream callback. */
1223class MockOutCallbacks : public IStreamOutCallback {
1224 Return<void> onWriteReady() override { return {}; }
1225 Return<void> onDrainReady() override { return {}; }
1226 Return<void> onError() override { return {}; }
1227};
1228
Kevin Rocard72e50e22017-05-05 14:02:55 -07001229static bool isAsyncModeSupported(IStreamOut* stream) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001230 auto res = stream->setCallback(new MockOutCallbacks);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001231 stream->clearCallback(); // try to restore the no callback state, ignore
1232 // any error
1233 auto okOrNotSupported = {Result::OK, Result::NOT_SUPPORTED};
Kevin Rocard624800c2017-03-10 18:47:37 -08001234 EXPECT_RESULT(okOrNotSupported, res);
1235 return res.isOk() ? res == Result::OK : false;
1236}
1237
1238TEST_P(OutputStreamTest, SetCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001239 doc::test(
1240 "If supported, registering callback for async operation should never "
1241 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001242 if (!isAsyncModeSupported(stream.get())) {
1243 doc::partialTest("The stream does not support async operations");
1244 return;
1245 }
1246 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1247 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1248}
1249
1250TEST_P(OutputStreamTest, clearCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001251 doc::test(
1252 "If supported, clearing a callback to go back to sync operation should "
1253 "not fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001254 if (!isAsyncModeSupported(stream.get())) {
1255 doc::partialTest("The stream does not support async operations");
1256 return;
1257 }
1258 // TODO: Clarify if clearing a non existing callback should fail
1259 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1260 ASSERT_OK(stream->clearCallback());
1261}
1262
1263TEST_P(OutputStreamTest, Resume) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001264 doc::test(
1265 "If supported, a stream should fail to resume if not previously "
1266 "paused");
Kevin Rocard624800c2017-03-10 18:47:37 -08001267 if (!Capability(stream.get()).resume) {
1268 doc::partialTest("The output stream does not support resume");
1269 return;
1270 }
1271 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1272}
1273
1274TEST_P(OutputStreamTest, Pause) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001275 doc::test(
1276 "If supported, a stream should fail to pause if not previously "
1277 "started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001278 if (!Capability(stream.get()).pause) {
1279 doc::partialTest("The output stream does not support pause");
1280 return;
1281 }
1282 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1283}
1284
Kevin Rocard72e50e22017-05-05 14:02:55 -07001285static void testDrain(IStreamOut* stream, AudioDrain type) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001286 if (!Capability(stream).drain) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001287 doc::partialTest("The output stream does not support drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001288 return;
1289 }
1290 ASSERT_RESULT(Result::OK, stream->drain(type));
1291}
1292
1293TEST_P(OutputStreamTest, DrainAll) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001294 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001295 testDrain(stream.get(), AudioDrain::ALL);
1296}
1297
1298TEST_P(OutputStreamTest, DrainEarlyNotify) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001299 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001300 testDrain(stream.get(), AudioDrain::EARLY_NOTIFY);
1301}
1302
1303TEST_P(OutputStreamTest, FlushStop) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001304 doc::test("If supported, a stream should always succeed to flush");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001305 auto ret = stream->flush();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001306 ASSERT_IS_OK(ret);
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001307 if (ret == Result::NOT_SUPPORTED) {
1308 doc::partialTest("Flush is not supported");
1309 return;
1310 }
1311 ASSERT_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001312}
1313
1314TEST_P(OutputStreamTest, GetPresentationPositionStop) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001315 doc::test(
1316 "If supported, a stream should always succeed to retrieve the "
1317 "presentation position");
Kevin Rocard624800c2017-03-10 18:47:37 -08001318 uint64_t frames;
1319 TimeSpec mesureTS;
1320 ASSERT_OK(stream->getPresentationPosition(returnIn(res, frames, mesureTS)));
1321 if (res == Result::NOT_SUPPORTED) {
1322 doc::partialTest("getpresentationPosition is not supported");
1323 return;
1324 }
1325 ASSERT_EQ(0U, frames);
1326
Kevin Rocard476e38f2017-05-03 10:52:43 -07001327 if (mesureTS.tvNSec == 0 && mesureTS.tvSec == 0) {
1328 // As the stream has never written a frame yet,
1329 // the timestamp does not really have a meaning, allow to return 0
1330 return;
1331 }
1332
1333 // Make sure the return measure is not more than 1s old.
Kevin Rocard624800c2017-03-10 18:47:37 -08001334 struct timespec currentTS;
1335 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currentTS)) << errno;
1336
Kevin Rocard72e50e22017-05-05 14:02:55 -07001337 auto toMicroSec = [](uint64_t sec, auto nsec) {
1338 return sec * 1e+6 + nsec / 1e+3;
1339 };
Kevin Rocard624800c2017-03-10 18:47:37 -08001340 auto currentTime = toMicroSec(currentTS.tv_sec, currentTS.tv_nsec);
1341 auto mesureTime = toMicroSec(mesureTS.tvSec, mesureTS.tvNSec);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001342 ASSERT_PRED2([](auto c, auto m) { return c - m < 1e+6; }, currentTime,
1343 mesureTime);
Kevin Rocard624800c2017-03-10 18:47:37 -08001344}
1345
Kevin Rocard624800c2017-03-10 18:47:37 -08001346//////////////////////////////////////////////////////////////////////////////
Kevin Rocard3c405a72017-03-08 16:46:51 -08001347/////////////////////////////// PrimaryDevice ////////////////////////////////
1348//////////////////////////////////////////////////////////////////////////////
1349
1350TEST_F(AudioPrimaryHidlTest, setVoiceVolume) {
1351 doc::test("Make sure setVoiceVolume only succeed if volume is in [0,1]");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001352 testUnitaryGain(
1353 [this](float volume) { return device->setVoiceVolume(volume); });
Kevin Rocard3c405a72017-03-08 16:46:51 -08001354}
1355
1356TEST_F(AudioPrimaryHidlTest, setMode) {
Kevin Rocard04364ed2017-05-02 18:16:00 -07001357 doc::test(
1358 "Make sure setMode always succeeds if mode is valid "
1359 "and fails otherwise");
1360 // Test Invalid values
1361 for (AudioMode mode :
1362 {AudioMode::INVALID, AudioMode::CURRENT, AudioMode::CNT}) {
1363 SCOPED_TRACE("mode=" + toString(mode));
1364 ASSERT_RESULT(Result::INVALID_ARGUMENTS, device->setMode(mode));
1365 }
1366 // Test valid values
Kevin Rocard72e50e22017-05-05 14:02:55 -07001367 for (AudioMode mode :
1368 {AudioMode::IN_CALL, AudioMode::IN_COMMUNICATION, AudioMode::RINGTONE,
Kevin Rocard72e50e22017-05-05 14:02:55 -07001369 AudioMode::NORMAL /* Make sure to leave the test in normal mode */}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -08001370 SCOPED_TRACE("mode=" + toString(mode));
1371 ASSERT_OK(device->setMode(mode));
1372 }
Kevin Rocard3c405a72017-03-08 16:46:51 -08001373}
1374
Kevin Rocard3c405a72017-03-08 16:46:51 -08001375TEST_F(BoolAccessorPrimaryHidlTest, BtScoNrecEnabled) {
1376 doc::test("Query and set the BT SCO NR&EC state");
1377 testOptionalAccessors("BtScoNrecEnabled", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001378 &IPrimaryDevice::setBtScoNrecEnabled,
1379 &IPrimaryDevice::getBtScoNrecEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001380}
1381
1382TEST_F(BoolAccessorPrimaryHidlTest, setGetBtScoWidebandEnabled) {
1383 doc::test("Query and set the SCO whideband state");
1384 testOptionalAccessors("BtScoWideband", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001385 &IPrimaryDevice::setBtScoWidebandEnabled,
1386 &IPrimaryDevice::getBtScoWidebandEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001387}
1388
1389using TtyModeAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<TtyMode>;
1390TEST_F(TtyModeAccessorPrimaryHidlTest, setGetTtyMode) {
1391 doc::test("Query and set the TTY mode state");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001392 testOptionalAccessors(
1393 "TTY mode", {TtyMode::OFF, TtyMode::HCO, TtyMode::VCO, TtyMode::FULL},
1394 &IPrimaryDevice::setTtyMode, &IPrimaryDevice::getTtyMode);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001395}
1396
1397TEST_F(BoolAccessorPrimaryHidlTest, setGetHac) {
1398 doc::test("Query and set the HAC state");
Kevin Rocard98390a62017-05-03 11:16:05 -07001399 testOptionalAccessors("HAC", {true, false, true},
1400 &IPrimaryDevice::setHacEnabled,
1401 &IPrimaryDevice::getHacEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001402}
1403
1404//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -08001405//////////////////// Clean caches on global tear down ////////////////////////
1406//////////////////////////////////////////////////////////////////////////////
1407
1408int main(int argc, char** argv) {
1409 environment = new Environment;
1410 ::testing::AddGlobalTestEnvironment(environment);
1411 ::testing::InitGoogleTest(&argc, argv);
1412 int status = RUN_ALL_TESTS();
1413 LOG(INFO) << "Test result = " << status;
1414 return status;
1415}