blob: 838aca2316d73fd804ec7747e195df96883aa0a3 [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
Tri Vo6c00ac32017-10-12 15:47:32 -070029#include <fcntl.h>
30#include <unistd.h>
31
Yuexi Ma161b5642017-03-10 13:44:22 -080032#include <VtsHalHidlTargetTestBase.h>
Kevin Rocardf0357882017-02-10 16:19:28 -080033
34#include <android-base/logging.h>
35
36#include <android/hardware/audio/2.0/IDevice.h>
37#include <android/hardware/audio/2.0/IDevicesFactory.h>
Kevin Rocard3c405a72017-03-08 16:46:51 -080038#include <android/hardware/audio/2.0/IPrimaryDevice.h>
Kevin Rocardf0357882017-02-10 16:19:28 -080039#include <android/hardware/audio/2.0/types.h>
40#include <android/hardware/audio/common/2.0/types.h>
41
Kevin Rocardf0357882017-02-10 16:19:28 -080042#include "utility/AssertOk.h"
43#include "utility/PrettyPrintAudioTypes.h"
Kevin Rocard72e50e22017-05-05 14:02:55 -070044#include "utility/ReturnIn.h"
Kevin Rocardf0357882017-02-10 16:19:28 -080045
46using std::string;
47using std::to_string;
48using std::vector;
49
50using ::android::sp;
51using ::android::hardware::Return;
52using ::android::hardware::hidl_handle;
53using ::android::hardware::hidl_string;
54using ::android::hardware::hidl_vec;
Kevin Rocardc9963522017-03-10 18:47:37 -080055using ::android::hardware::MQDescriptorSync;
Kevin Rocard624800c2017-03-10 18:47:37 -080056using ::android::hardware::audio::V2_0::AudioDrain;
Kevin Rocardf0357882017-02-10 16:19:28 -080057using ::android::hardware::audio::V2_0::DeviceAddress;
58using ::android::hardware::audio::V2_0::IDevice;
Kevin Rocard3c405a72017-03-08 16:46:51 -080059using ::android::hardware::audio::V2_0::IPrimaryDevice;
60using TtyMode = ::android::hardware::audio::V2_0::IPrimaryDevice::TtyMode;
Kevin Rocardf0357882017-02-10 16:19:28 -080061using ::android::hardware::audio::V2_0::IDevicesFactory;
62using ::android::hardware::audio::V2_0::IStream;
63using ::android::hardware::audio::V2_0::IStreamIn;
Kevin Rocard624800c2017-03-10 18:47:37 -080064using ::android::hardware::audio::V2_0::TimeSpec;
Kevin Rocard72e50e22017-05-05 14:02:55 -070065using ReadParameters =
66 ::android::hardware::audio::V2_0::IStreamIn::ReadParameters;
Kevin Rocardc9963522017-03-10 18:47:37 -080067using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus;
Kevin Rocardf0357882017-02-10 16:19:28 -080068using ::android::hardware::audio::V2_0::IStreamOut;
Kevin Rocard624800c2017-03-10 18:47:37 -080069using ::android::hardware::audio::V2_0::IStreamOutCallback;
Kevin Rocard8878b4b2017-03-10 18:47:37 -080070using ::android::hardware::audio::V2_0::MmapBufferInfo;
71using ::android::hardware::audio::V2_0::MmapPosition;
Kevin Rocardf0357882017-02-10 16:19:28 -080072using ::android::hardware::audio::V2_0::ParameterValue;
73using ::android::hardware::audio::V2_0::Result;
74using ::android::hardware::audio::common::V2_0::AudioChannelMask;
75using ::android::hardware::audio::common::V2_0::AudioConfig;
76using ::android::hardware::audio::common::V2_0::AudioDevice;
77using ::android::hardware::audio::common::V2_0::AudioFormat;
78using ::android::hardware::audio::common::V2_0::AudioHandleConsts;
79using ::android::hardware::audio::common::V2_0::AudioInputFlag;
80using ::android::hardware::audio::common::V2_0::AudioIoHandle;
Kevin Rocard3c405a72017-03-08 16:46:51 -080081using ::android::hardware::audio::common::V2_0::AudioMode;
Kevin Rocardf0357882017-02-10 16:19:28 -080082using ::android::hardware::audio::common::V2_0::AudioOffloadInfo;
83using ::android::hardware::audio::common::V2_0::AudioOutputFlag;
84using ::android::hardware::audio::common::V2_0::AudioSource;
Kevin Rocardc9963522017-03-10 18:47:37 -080085using ::android::hardware::audio::common::V2_0::ThreadInfo;
Kevin Rocardf0357882017-02-10 16:19:28 -080086
87using utility::returnIn;
88
Kevin Rocard96f46c42017-05-08 11:53:07 -070089const char* getTestName() {
90 return ::testing::UnitTest::GetInstance()->current_test_info()->name();
91}
92
Kevin Rocardf0357882017-02-10 16:19:28 -080093namespace doc {
94/** Document the current test case.
Kevin Rocard72e50e22017-05-05 14:02:55 -070095 * Eg: calling `doc::test("Dump the state of the hal")` in the "debugDump" test
96 * will output:
97 * <testcase name="debugDump" status="run" time="6"
98 * classname="AudioPrimaryHidlTest"
99 description="Dump the state of the hal." />
100 * see
101 https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#logging-additional-information
Kevin Rocardf0357882017-02-10 16:19:28 -0800102 */
103void test(const std::string& testCaseDocumentation) {
104 ::testing::Test::RecordProperty("description", testCaseDocumentation);
105}
106
Kevin Rocard72e50e22017-05-05 14:02:55 -0700107/** Document why a test was not fully run. Usually due to an optional feature
108 * not implemented. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800109void partialTest(const std::string& reason) {
Kevin Rocard96f46c42017-05-08 11:53:07 -0700110 LOG(INFO) << "Test " << getTestName() << " partially run: " << reason;
Kevin Rocardf0357882017-02-10 16:19:28 -0800111 ::testing::Test::RecordProperty("partialyRunTest", reason);
112}
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700113
114/** Add a note to the test. */
115void note(const std::string& note) {
Kevin Rocard96f46c42017-05-08 11:53:07 -0700116 LOG(INFO) << "Test " << getTestName() << " noted: " << note;
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700117 ::testing::Test::RecordProperty("note", note);
118}
Kevin Rocardf0357882017-02-10 16:19:28 -0800119}
120
121// Register callback for static object destruction
122// Avoid destroying static objects after main return.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700123// Post main return destruction leads to incorrect gtest timing measurements as
124// well as harder
Kevin Rocardf0357882017-02-10 16:19:28 -0800125// debuging if anything goes wrong during destruction.
126class Environment : public ::testing::Environment {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700127 public:
128 using TearDownFunc = std::function<void()>;
129 void registerTearDown(TearDownFunc&& tearDown) {
130 tearDowns.push_back(std::move(tearDown));
Kevin Rocardf0357882017-02-10 16:19:28 -0800131 }
132
Kevin Rocard72e50e22017-05-05 14:02:55 -0700133 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800134 void TearDown() override {
135 // Call the tear downs in reverse order of insertion
136 for (auto& tearDown : tearDowns) {
137 tearDown();
138 }
139 }
140 std::list<TearDownFunc> tearDowns;
141};
142// Instance to register global tearDown
143static Environment* environment;
144
Yuexi Ma161b5642017-03-10 13:44:22 -0800145class HidlTest : public ::testing::VtsHalHidlTargetTestBase {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700146 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800147 // Convenient member to store results
148 Result res;
149};
150
151//////////////////////////////////////////////////////////////////////////////
152////////////////////// getService audio_devices_factory //////////////////////
153//////////////////////////////////////////////////////////////////////////////
154
155// Test all audio devices
156class AudioHidlTest : public HidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700157 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800158 void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700159 ASSERT_NO_FATAL_FAILURE(HidlTest::SetUp()); // setup base
Kevin Rocardf0357882017-02-10 16:19:28 -0800160
161 if (devicesFactory == nullptr) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700162 environment->registerTearDown([] { devicesFactory.clear(); });
163 devicesFactory = ::testing::VtsHalHidlTargetTestBase::getService<
164 IDevicesFactory>();
Kevin Rocardf0357882017-02-10 16:19:28 -0800165 }
166 ASSERT_TRUE(devicesFactory != nullptr);
167 }
168
Kevin Rocard72e50e22017-05-05 14:02:55 -0700169 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800170 // Cache the devicesFactory retrieval to speed up each test by ~0.5s
171 static sp<IDevicesFactory> devicesFactory;
172};
173sp<IDevicesFactory> AudioHidlTest::devicesFactory;
174
175TEST_F(AudioHidlTest, GetAudioDevicesFactoryService) {
176 doc::test("test the getService (called in SetUp)");
177}
178
Mikhail Naganov8604a732017-04-24 09:29:22 -0700179TEST_F(AudioHidlTest, OpenDeviceInvalidParameter) {
180 doc::test("test passing an invalid parameter to openDevice");
181 IDevicesFactory::Result result;
182 sp<IDevice> device;
183 ASSERT_OK(devicesFactory->openDevice(IDevicesFactory::Device(-1),
184 returnIn(result, device)));
185 ASSERT_EQ(IDevicesFactory::Result::INVALID_ARGUMENTS, result);
186 ASSERT_TRUE(device == nullptr);
187}
188
Kevin Rocardf0357882017-02-10 16:19:28 -0800189//////////////////////////////////////////////////////////////////////////////
190/////////////////////////////// openDevice primary ///////////////////////////
191//////////////////////////////////////////////////////////////////////////////
192
193// Test the primary device
194class AudioPrimaryHidlTest : public AudioHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700195 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800196 /** Primary HAL test are NOT thread safe. */
197 void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700198 ASSERT_NO_FATAL_FAILURE(AudioHidlTest::SetUp()); // setup base
Kevin Rocardf0357882017-02-10 16:19:28 -0800199
200 if (device == nullptr) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800201 IDevicesFactory::Result result;
Kevin Rocard3c405a72017-03-08 16:46:51 -0800202 sp<IDevice> baseDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700203 ASSERT_OK(
204 devicesFactory->openDevice(IDevicesFactory::Device::PRIMARY,
205 returnIn(result, baseDevice)));
Kevin Rocardfba442a2017-03-31 19:34:41 -0700206 ASSERT_OK(result);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800207 ASSERT_TRUE(baseDevice != nullptr);
208
Kevin Rocard72e50e22017-05-05 14:02:55 -0700209 environment->registerTearDown([] { device.clear(); });
Kevin Rocard3c405a72017-03-08 16:46:51 -0800210 device = IPrimaryDevice::castFrom(baseDevice);
211 ASSERT_TRUE(device != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800212 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800213 }
214
Kevin Rocard72e50e22017-05-05 14:02:55 -0700215 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800216 // Cache the device opening to speed up each test by ~0.5s
Kevin Rocard3c405a72017-03-08 16:46:51 -0800217 static sp<IPrimaryDevice> device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800218};
Kevin Rocard3c405a72017-03-08 16:46:51 -0800219sp<IPrimaryDevice> AudioPrimaryHidlTest::device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800220
221TEST_F(AudioPrimaryHidlTest, OpenPrimaryDevice) {
222 doc::test("Test the openDevice (called in SetUp)");
223}
224
225TEST_F(AudioPrimaryHidlTest, Init) {
226 doc::test("Test that the audio primary hal initialized correctly");
227 ASSERT_OK(device->initCheck());
228}
229
230//////////////////////////////////////////////////////////////////////////////
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800231///////////////////// {set,get}{Master,Mic}{Mute,Volume} /////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800232//////////////////////////////////////////////////////////////////////////////
233
Kevin Rocard3c405a72017-03-08 16:46:51 -0800234template <class Property>
235class AccessorPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700236 protected:
Kevin Rocard3c405a72017-03-08 16:46:51 -0800237 /** Test a property getter and setter. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800238 template <class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700239 void testAccessors(const string& propertyName,
240 const vector<Property>& valuesToTest, Setter setter,
241 Getter getter,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800242 const vector<Property>& invalidValues = {}) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700243 Property initialValue; // Save initial value to restore it at the end
244 // of the test
Kevin Rocard3c405a72017-03-08 16:46:51 -0800245 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
246 ASSERT_OK(res);
247
248 for (Property setValue : valuesToTest) {
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800249 SCOPED_TRACE("Test " + propertyName + " getter and setter for " +
250 testing::PrintToString(setValue));
Kevin Rocard3c405a72017-03-08 16:46:51 -0800251 ASSERT_OK((device.get()->*setter)(setValue));
252 Property getValue;
253 // Make sure the getter returns the same value just set
254 ASSERT_OK((device.get()->*getter)(returnIn(res, getValue)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800255 ASSERT_OK(res);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800256 EXPECT_EQ(setValue, getValue);
Kevin Rocardf0357882017-02-10 16:19:28 -0800257 }
Kevin Rocard3c405a72017-03-08 16:46:51 -0800258
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800259 for (Property invalidValue : invalidValues) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700260 SCOPED_TRACE("Try to set " + propertyName +
261 " with the invalid value " +
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800262 testing::PrintToString(invalidValue));
Kevin Rocard72e50e22017-05-05 14:02:55 -0700263 EXPECT_RESULT(Result::INVALID_ARGUMENTS,
264 (device.get()->*setter)(invalidValue));
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800265 }
266
Kevin Rocard72e50e22017-05-05 14:02:55 -0700267 ASSERT_OK(
268 (device.get()->*setter)(initialValue)); // restore initial value
Kevin Rocard3c405a72017-03-08 16:46:51 -0800269 }
270
271 /** Test the getter and setter of an optional feature. */
272 template <class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700273 void testOptionalAccessors(const string& propertyName,
274 const vector<Property>& valuesToTest,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800275 Setter setter, Getter getter,
276 const vector<Property>& invalidValues = {}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -0800277 doc::test("Test the optional " + propertyName + " getters and setter");
278 {
279 SCOPED_TRACE("Test feature support by calling the getter");
280 Property initialValue;
281 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
282 if (res == Result::NOT_SUPPORTED) {
283 doc::partialTest(propertyName + " getter is not supported");
284 return;
285 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700286 ASSERT_OK(res); // If it is supported it must succeed
Kevin Rocard3c405a72017-03-08 16:46:51 -0800287 }
288 // The feature is supported, test it
Kevin Rocard72e50e22017-05-05 14:02:55 -0700289 testAccessors(propertyName, valuesToTest, setter, getter,
290 invalidValues);
Kevin Rocardf0357882017-02-10 16:19:28 -0800291 }
292};
293
Kevin Rocard3c405a72017-03-08 16:46:51 -0800294using BoolAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<bool>;
295
Kevin Rocardf0357882017-02-10 16:19:28 -0800296TEST_F(BoolAccessorPrimaryHidlTest, MicMuteTest) {
297 doc::test("Check that the mic can be muted and unmuted");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700298 testAccessors("mic mute", {true, false, true}, &IDevice::setMicMute,
299 &IDevice::getMicMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800300 // TODO: check that the mic is really muted (all sample are 0)
301}
302
303TEST_F(BoolAccessorPrimaryHidlTest, MasterMuteTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700304 doc::test(
305 "If master mute is supported, try to mute and unmute the master "
306 "output");
Kevin Rocard3c405a72017-03-08 16:46:51 -0800307 testOptionalAccessors("master mute", {true, false, true},
308 &IDevice::setMasterMute, &IDevice::getMasterMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800309 // TODO: check that the master volume is really muted
310}
311
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800312using FloatAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<float>;
313TEST_F(FloatAccessorPrimaryHidlTest, MasterVolumeTest) {
314 doc::test("Test the master volume if supported");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700315 testOptionalAccessors("master volume", {0, 0.5, 1},
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800316 &IDevice::setMasterVolume, &IDevice::getMasterVolume,
317 {-0.1, 1.1, NAN, INFINITY, -INFINITY,
318 1 + std::numeric_limits<float>::epsilon()});
319 // TODO: check that the master volume is really changed
320}
321
Kevin Rocardf0357882017-02-10 16:19:28 -0800322//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800323//////////////////////////////// AudioPatches ////////////////////////////////
324//////////////////////////////////////////////////////////////////////////////
325
326class AudioPatchPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700327 protected:
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800328 bool areAudioPatchesSupported() {
329 auto result = device->supportsAudioPatches();
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700330 EXPECT_IS_OK(result);
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800331 return result;
332 }
333};
334
335TEST_F(AudioPatchPrimaryHidlTest, AudioPatches) {
336 doc::test("Test if audio patches are supported");
337 if (!areAudioPatchesSupported()) {
338 doc::partialTest("Audio patches are not supported");
339 return;
340 }
341 // TODO: test audio patches
342}
343
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800344//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800345//////////////// Required and recommended audio format support ///////////////
Kevin Rocard72e50e22017-05-05 14:02:55 -0700346// From:
347// https://source.android.com/compatibility/android-cdd.html#5_4_audio_recording
348// From:
349// https://source.android.com/compatibility/android-cdd.html#5_5_audio_playback
Kevin Rocardf0357882017-02-10 16:19:28 -0800350/////////// TODO: move to the beginning of the file for easier update ////////
351//////////////////////////////////////////////////////////////////////////////
352
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800353class AudioConfigPrimaryTest : public AudioPatchPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700354 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800355 // Cache result ?
356 static const vector<AudioConfig> getRequiredSupportPlaybackAudioConfig() {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700357 return combineAudioConfig(
358 {AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
359 {8000, 11025, 16000, 22050, 32000, 44100},
360 {AudioFormat::PCM_16_BIT});
Kevin Rocardf0357882017-02-10 16:19:28 -0800361 }
362
Kevin Rocard72e50e22017-05-05 14:02:55 -0700363 static const vector<AudioConfig>
364 getRecommendedSupportPlaybackAudioConfig() {
365 return combineAudioConfig(
366 {AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
367 {24000, 48000}, {AudioFormat::PCM_16_BIT});
Kevin Rocardf0357882017-02-10 16:19:28 -0800368 }
369
370 static const vector<AudioConfig> getSupportedPlaybackAudioConfig() {
371 // TODO: retrieve audio config supported by the platform
372 // as declared in the policy configuration
373 return {};
374 }
375
376 static const vector<AudioConfig> getRequiredSupportCaptureAudioConfig() {
377 return combineAudioConfig({AudioChannelMask::IN_MONO},
378 {8000, 11025, 16000, 44100},
379 {AudioFormat::PCM_16_BIT});
380 }
381 static const vector<AudioConfig> getRecommendedSupportCaptureAudioConfig() {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700382 return combineAudioConfig({AudioChannelMask::IN_STEREO}, {22050, 48000},
Kevin Rocardf0357882017-02-10 16:19:28 -0800383 {AudioFormat::PCM_16_BIT});
384 }
385 static const vector<AudioConfig> getSupportedCaptureAudioConfig() {
386 // TODO: retrieve audio config supported by the platform
387 // as declared in the policy configuration
388 return {};
389 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700390
391 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800392 static const vector<AudioConfig> combineAudioConfig(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700393 vector<AudioChannelMask> channelMasks, vector<uint32_t> sampleRates,
394 vector<AudioFormat> formats) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800395 vector<AudioConfig> configs;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700396 for (auto channelMask : channelMasks) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800397 for (auto sampleRate : sampleRates) {
398 for (auto format : formats) {
399 AudioConfig config{};
400 // leave offloadInfo to 0
401 config.channelMask = channelMask;
402 config.sampleRateHz = sampleRate;
403 config.format = format;
404 // FIXME: leave frameCount to 0 ?
405 configs.push_back(config);
406 }
407 }
408 }
409 return configs;
410 }
411};
412
Kevin Rocard9c369142017-03-08 17:17:25 -0800413/** Generate a test name based on an audio config.
414 *
415 * As the only parameter changing are channel mask and sample rate,
416 * only print those ones in the test name.
417 */
Kevin Rocard72e50e22017-05-05 14:02:55 -0700418static string generateTestName(
419 const testing::TestParamInfo<AudioConfig>& info) {
Kevin Rocard9c369142017-03-08 17:17:25 -0800420 const AudioConfig& config = info.param;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700421 return to_string(info.index) + "__" + to_string(config.sampleRateHz) + "_" +
422 // "MONO" is more clear than "FRONT_LEFT"
423 ((config.channelMask == AudioChannelMask::OUT_MONO ||
424 config.channelMask == AudioChannelMask::IN_MONO)
425 ? "MONO"
426 : toString(config.channelMask));
Kevin Rocard9c369142017-03-08 17:17:25 -0800427}
428
Kevin Rocardf0357882017-02-10 16:19:28 -0800429//////////////////////////////////////////////////////////////////////////////
430///////////////////////////// getInputBufferSize /////////////////////////////
431//////////////////////////////////////////////////////////////////////////////
432
Kevin Rocard72e50e22017-05-05 14:02:55 -0700433// FIXME: execute input test only if platform declares
434// android.hardware.microphone
Kevin Rocardf0357882017-02-10 16:19:28 -0800435// how to get this value ? is it a property ???
436
Kevin Rocard72e50e22017-05-05 14:02:55 -0700437class AudioCaptureConfigPrimaryTest
438 : public AudioConfigPrimaryTest,
439 public ::testing::WithParamInterface<AudioConfig> {
440 protected:
441 void inputBufferSizeTest(const AudioConfig& audioConfig,
442 bool supportRequired) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800443 uint64_t bufferSize;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700444 ASSERT_OK(
445 device->getInputBufferSize(audioConfig, returnIn(res, bufferSize)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800446
447 switch (res) {
448 case Result::INVALID_ARGUMENTS:
449 EXPECT_FALSE(supportRequired);
450 break;
451 case Result::OK:
452 // Check that the buffer is of a sane size
453 // For now only that it is > 0
454 EXPECT_GT(bufferSize, uint64_t(0));
455 break;
456 default:
Kevin Rocard72e50e22017-05-05 14:02:55 -0700457 FAIL() << "Invalid return status: "
458 << ::testing::PrintToString(res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800459 }
460 }
461};
462
Kevin Rocard72e50e22017-05-05 14:02:55 -0700463// Test that the required capture config and those declared in the policy are
464// indeed supported
Kevin Rocardf0357882017-02-10 16:19:28 -0800465class RequiredInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
466TEST_P(RequiredInputBufferSizeTest, RequiredInputBufferSizeTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700467 doc::test(
468 "Input buffer size must be retrievable for a format with required "
469 "support.");
Kevin Rocardf0357882017-02-10 16:19:28 -0800470 inputBufferSizeTest(GetParam(), true);
471}
472INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700473 RequiredInputBufferSize, RequiredInputBufferSizeTest,
474 ::testing::ValuesIn(
475 AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
476 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800477INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700478 SupportedInputBufferSize, RequiredInputBufferSizeTest,
479 ::testing::ValuesIn(
480 AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
481 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800482
Kevin Rocard72e50e22017-05-05 14:02:55 -0700483// Test that the recommended capture config are supported or lead to a
484// INVALID_ARGUMENTS return
Kevin Rocardf0357882017-02-10 16:19:28 -0800485class OptionalInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
486TEST_P(OptionalInputBufferSizeTest, OptionalInputBufferSizeTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700487 doc::test(
488 "Input buffer size should be retrievable for a format with recommended "
489 "support.");
Kevin Rocardf0357882017-02-10 16:19:28 -0800490 inputBufferSizeTest(GetParam(), false);
491}
492INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700493 RecommendedCaptureAudioConfigSupport, OptionalInputBufferSizeTest,
494 ::testing::ValuesIn(
495 AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
496 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800497
498//////////////////////////////////////////////////////////////////////////////
499/////////////////////////////// setScreenState ///////////////////////////////
500//////////////////////////////////////////////////////////////////////////////
501
502TEST_F(AudioPrimaryHidlTest, setScreenState) {
503 doc::test("Check that the hal can receive the screen state");
504 for (bool turnedOn : {false, true, true, false, false}) {
505 auto ret = device->setScreenState(turnedOn);
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700506 ASSERT_IS_OK(ret);
Kevin Rocardf0357882017-02-10 16:19:28 -0800507 Result result = ret;
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700508 auto okOrNotSupported = {Result::OK, Result::NOT_SUPPORTED};
509 ASSERT_RESULT(okOrNotSupported, result);
Kevin Rocardf0357882017-02-10 16:19:28 -0800510 }
511}
512
513//////////////////////////////////////////////////////////////////////////////
514//////////////////////////// {get,set}Parameters /////////////////////////////
515//////////////////////////////////////////////////////////////////////////////
516
517TEST_F(AudioPrimaryHidlTest, getParameters) {
518 doc::test("Check that the hal can set and get parameters");
519 hidl_vec<hidl_string> keys;
520 hidl_vec<ParameterValue> values;
521 ASSERT_OK(device->getParameters(keys, returnIn(res, values)));
522 ASSERT_OK(device->setParameters(values));
523 values.resize(0);
524 ASSERT_OK(device->setParameters(values));
525}
526
527//////////////////////////////////////////////////////////////////////////////
528//////////////////////////////// debugDebug //////////////////////////////////
529//////////////////////////////////////////////////////////////////////////////
530
Kevin Rocardb9031242017-03-13 12:20:54 -0700531template <class DebugDump>
532static void testDebugDump(DebugDump debugDump) {
Tri Vo6c00ac32017-10-12 15:47:32 -0700533 // File descriptors to our pipe. fds[0] corresponds to the read end and
534 // fds[1] to the write end.
535 int fds[2];
536 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
Kevin Rocardf0357882017-02-10 16:19:28 -0800537
Tri Vo7d35a3d2017-10-17 11:07:29 -0700538 // Make sure that the pipe is at least 1 MB in size. The test process runs
539 // in su domain, so it should be safe to make this call.
540 fcntl(fds[0], F_SETPIPE_SZ, 1 << 20);
541
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700542 // Wrap the temporary file file descriptor in a native handle
Kevin Rocardf0357882017-02-10 16:19:28 -0800543 auto* nativeHandle = native_handle_create(1, 0);
544 ASSERT_NE(nullptr, nativeHandle);
Tri Vo6c00ac32017-10-12 15:47:32 -0700545 nativeHandle->data[0] = fds[1];
Kevin Rocardf0357882017-02-10 16:19:28 -0800546
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700547 // Wrap this native handle in a hidl handle
Kevin Rocardf0357882017-02-10 16:19:28 -0800548 hidl_handle handle;
Tri Vo6c00ac32017-10-12 15:47:32 -0700549 handle.setTo(nativeHandle, false /*take ownership*/);
Kevin Rocardf0357882017-02-10 16:19:28 -0800550
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700551 ASSERT_OK(debugDump(handle));
552
553 // Check that at least one bit was written by the hal
Kevin Rocardee771e92017-03-08 17:17:25 -0800554 // TODO: debugDump does not return a Result.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700555 // This mean that the hal can not report that it not implementing the
556 // function.
Kevin Rocardf0357882017-02-10 16:19:28 -0800557 char buff;
Tri Vo6c00ac32017-10-12 15:47:32 -0700558 if (read(fds[0], &buff, 1) != 1) {
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700559 doc::note("debugDump does not seem implemented");
560 }
Tri Vo6c00ac32017-10-12 15:47:32 -0700561 EXPECT_EQ(0, close(fds[0])) << errno;
562 EXPECT_EQ(0, close(fds[1])) << errno;
Kevin Rocardf0357882017-02-10 16:19:28 -0800563}
564
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700565TEST_F(AudioPrimaryHidlTest, DebugDump) {
Kevin Rocardb9031242017-03-13 12:20:54 -0700566 doc::test("Check that the hal can dump its state without error");
Chih-Hung Hsiehd3bc6812017-05-15 12:31:25 -0700567 testDebugDump([](const auto& handle) { return device->debugDump(handle); });
Kevin Rocardb9031242017-03-13 12:20:54 -0700568}
569
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700570TEST_F(AudioPrimaryHidlTest, DebugDumpInvalidArguments) {
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700571 doc::test("Check that the hal dump doesn't crash on invalid arguments");
572 ASSERT_OK(device->debugDump(hidl_handle()));
573}
574
Kevin Rocardf0357882017-02-10 16:19:28 -0800575//////////////////////////////////////////////////////////////////////////////
576////////////////////////// open{Output,Input}Stream //////////////////////////
577//////////////////////////////////////////////////////////////////////////////
578
579template <class Stream>
580class OpenStreamTest : public AudioConfigPrimaryTest,
581 public ::testing::WithParamInterface<AudioConfig> {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700582 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800583 template <class Open>
584 void testOpen(Open openStream, const AudioConfig& config) {
585 // FIXME: Open a stream without an IOHandle
586 // This is not required to be accepted by hal implementations
Kevin Rocard72e50e22017-05-05 14:02:55 -0700587 AudioIoHandle ioHandle =
588 (AudioIoHandle)AudioHandleConsts::AUDIO_IO_HANDLE_NONE;
Kevin Rocardf0357882017-02-10 16:19:28 -0800589 AudioConfig suggestedConfig{};
Kevin Rocard72e50e22017-05-05 14:02:55 -0700590 ASSERT_OK(openStream(ioHandle, config,
591 returnIn(res, stream, suggestedConfig)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800592
593 // TODO: only allow failure for RecommendedPlaybackAudioConfig
594 switch (res) {
595 case Result::OK:
596 ASSERT_TRUE(stream != nullptr);
597 audioConfig = config;
598 break;
599 case Result::INVALID_ARGUMENTS:
600 ASSERT_TRUE(stream == nullptr);
601 AudioConfig suggestedConfigRetry;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700602 // Could not open stream with config, try again with the
603 // suggested one
604 ASSERT_OK(
605 openStream(ioHandle, suggestedConfig,
606 returnIn(res, stream, suggestedConfigRetry)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800607 // This time it must succeed
608 ASSERT_OK(res);
Kevin Rocard4aefd1c2017-05-02 18:58:58 -0700609 ASSERT_TRUE(stream != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800610 audioConfig = suggestedConfig;
611 break;
612 default:
Kevin Rocard72e50e22017-05-05 14:02:55 -0700613 FAIL() << "Invalid return status: "
614 << ::testing::PrintToString(res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800615 }
616 open = true;
617 }
618
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800619 Return<Result> closeStream() {
620 open = false;
621 return stream->close();
622 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700623
624 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800625 void TearDown() override {
626 if (open) {
627 ASSERT_OK(stream->close());
628 }
629 }
630
Kevin Rocard72e50e22017-05-05 14:02:55 -0700631 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800632 AudioConfig audioConfig;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800633 DeviceAddress address = {};
Kevin Rocardf0357882017-02-10 16:19:28 -0800634 sp<Stream> stream;
635 bool open = false;
636};
637
638////////////////////////////// openOutputStream //////////////////////////////
639
640class OutputStreamTest : public OpenStreamTest<IStreamOut> {
641 virtual void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700642 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800643 address.device = AudioDevice::OUT_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800644 const AudioConfig& config = GetParam();
Kevin Rocard72e50e22017-05-05 14:02:55 -0700645 AudioOutputFlag flags =
646 AudioOutputFlag::NONE; // TODO: test all flag combination
647 testOpen(
648 [&](AudioIoHandle handle, AudioConfig config, auto cb) {
649 return device->openOutputStream(handle, address, config, flags,
650 cb);
651 },
652 config);
Kevin Rocardf0357882017-02-10 16:19:28 -0800653 }
654};
655TEST_P(OutputStreamTest, OpenOutputStreamTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700656 doc::test(
657 "Check that output streams can be open with the required and "
658 "recommended config");
Kevin Rocardf0357882017-02-10 16:19:28 -0800659 // Open done in SetUp
660}
661INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700662 RequiredOutputStreamConfigSupport, OutputStreamTest,
663 ::testing::ValuesIn(
664 AudioConfigPrimaryTest::getRequiredSupportPlaybackAudioConfig()),
665 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800666INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700667 SupportedOutputStreamConfig, OutputStreamTest,
668 ::testing::ValuesIn(
669 AudioConfigPrimaryTest::getSupportedPlaybackAudioConfig()),
670 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800671
672INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700673 RecommendedOutputStreamConfigSupport, OutputStreamTest,
674 ::testing::ValuesIn(
675 AudioConfigPrimaryTest::getRecommendedSupportPlaybackAudioConfig()),
676 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800677
678////////////////////////////// openInputStream //////////////////////////////
679
680class InputStreamTest : public OpenStreamTest<IStreamIn> {
Kevin Rocardf0357882017-02-10 16:19:28 -0800681 virtual void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700682 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800683 address.device = AudioDevice::IN_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800684 const AudioConfig& config = GetParam();
Kevin Rocard72e50e22017-05-05 14:02:55 -0700685 AudioInputFlag flags =
686 AudioInputFlag::NONE; // TODO: test all flag combination
687 AudioSource source =
688 AudioSource::DEFAULT; // TODO: test all flag combination
689 testOpen(
690 [&](AudioIoHandle handle, AudioConfig config, auto cb) {
691 return device->openInputStream(handle, address, config, flags,
692 source, cb);
693 },
694 config);
Kevin Rocardf0357882017-02-10 16:19:28 -0800695 }
696};
697
698TEST_P(InputStreamTest, OpenInputStreamTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700699 doc::test(
700 "Check that input streams can be open with the required and "
701 "recommended config");
Kevin Rocardf0357882017-02-10 16:19:28 -0800702 // Open done in setup
703}
704INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700705 RequiredInputStreamConfigSupport, InputStreamTest,
706 ::testing::ValuesIn(
707 AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
708 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800709INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700710 SupportedInputStreamConfig, InputStreamTest,
711 ::testing::ValuesIn(
712 AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
713 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800714
715INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700716 RecommendedInputStreamConfigSupport, InputStreamTest,
717 ::testing::ValuesIn(
718 AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
719 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800720
721//////////////////////////////////////////////////////////////////////////////
722////////////////////////////// IStream getters ///////////////////////////////
723//////////////////////////////////////////////////////////////////////////////
724
725/** Unpack the provided result.
726 * If the result is not OK, register a failure and return an undefined value. */
727template <class R>
728static R extract(Return<R> ret) {
729 if (!ret.isOk()) {
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700730 EXPECT_IS_OK(ret);
Kevin Rocardf0357882017-02-10 16:19:28 -0800731 return R{};
732 }
733 return ret;
734}
735
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800736/* Could not find a way to write a test for two parametrized class fixure
737 * thus use this macro do duplicate tests for Input and Output stream */
738#define TEST_IO_STREAM(test_name, documentation, code) \
Kevin Rocard72e50e22017-05-05 14:02:55 -0700739 TEST_P(InputStreamTest, test_name) { \
740 doc::test(documentation); \
741 code; \
742 } \
743 TEST_P(OutputStreamTest, test_name) { \
744 doc::test(documentation); \
745 code; \
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800746 }
747
Kevin Rocard72e50e22017-05-05 14:02:55 -0700748TEST_IO_STREAM(
749 GetFrameCount,
750 "Check that the stream frame count == the one it was opened with",
751 ASSERT_EQ(audioConfig.frameCount, extract(stream->getFrameCount())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800752
Kevin Rocard72e50e22017-05-05 14:02:55 -0700753TEST_IO_STREAM(
754 GetSampleRate,
755 "Check that the stream sample rate == the one it was opened with",
Kevin Rocardfd297c62017-05-12 15:25:09 -0700756 stream->getSampleRate())
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800757
Kevin Rocard72e50e22017-05-05 14:02:55 -0700758TEST_IO_STREAM(
759 GetChannelMask,
760 "Check that the stream channel mask == the one it was opened with",
Kevin Rocardfd297c62017-05-12 15:25:09 -0700761 stream->getChannelMask())
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800762
Kevin Rocard72e50e22017-05-05 14:02:55 -0700763TEST_IO_STREAM(GetFormat,
764 "Check that the stream format == the one it was opened with",
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800765 ASSERT_EQ(audioConfig.format, extract(stream->getFormat())))
766
767// TODO: for now only check that the framesize is not incoherent
Kevin Rocard72e50e22017-05-05 14:02:55 -0700768TEST_IO_STREAM(GetFrameSize,
769 "Check that the stream frame size == the one it was opened with",
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800770 ASSERT_GT(extract(stream->getFrameSize()), 0U))
771
Kevin Rocard72e50e22017-05-05 14:02:55 -0700772TEST_IO_STREAM(GetBufferSize,
773 "Check that the stream buffer size== the one it was opened with",
774 ASSERT_GE(extract(stream->getBufferSize()),
775 extract(stream->getFrameSize())));
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800776
Kevin Rocardf0357882017-02-10 16:19:28 -0800777template <class Property, class CapabilityGetter, class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700778static void testCapabilityGetter(const string& name, IStream* stream,
779 Property currentValue,
780 CapabilityGetter capablityGetter,
781 Getter getter, Setter setter) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800782 hidl_vec<Property> capabilities;
783 ASSERT_OK((stream->*capablityGetter)(returnIn(capabilities)));
784 if (capabilities.size() == 0) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700785 // The default hal should probably return a NOT_SUPPORTED if the hal
786 // does not expose
787 // capability retrieval. For now it returns an empty list if not
788 // implemented
Kevin Rocardf0357882017-02-10 16:19:28 -0800789 doc::partialTest(name + " is not supported");
790 return;
791 };
Kevin Rocard72e50e22017-05-05 14:02:55 -0700792 // TODO: This code has never been tested on a hal that supports
793 // getSupportedSampleRates
Kevin Rocardf0357882017-02-10 16:19:28 -0800794 EXPECT_NE(std::find(capabilities.begin(), capabilities.end(), currentValue),
Kevin Rocard72e50e22017-05-05 14:02:55 -0700795 capabilities.end())
Kevin Rocardf0357882017-02-10 16:19:28 -0800796 << "current " << name << " is not in the list of the supported ones "
797 << toString(capabilities);
798
799 // Check that all declared supported values are indeed supported
800 for (auto capability : capabilities) {
801 ASSERT_OK((stream->*setter)(capability));
802 ASSERT_EQ(capability, extract((stream->*getter)()));
803 }
804}
805
Kevin Rocard72e50e22017-05-05 14:02:55 -0700806TEST_IO_STREAM(SupportedSampleRate,
807 "Check that the stream sample rate is declared as supported",
808 testCapabilityGetter("getSupportedSampleRate", stream.get(),
809 extract(stream->getSampleRate()),
810 &IStream::getSupportedSampleRates,
811 &IStream::getSampleRate,
812 &IStream::setSampleRate))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800813
Kevin Rocard72e50e22017-05-05 14:02:55 -0700814TEST_IO_STREAM(SupportedChannelMask,
815 "Check that the stream channel mask is declared as supported",
816 testCapabilityGetter("getSupportedChannelMask", stream.get(),
817 extract(stream->getChannelMask()),
818 &IStream::getSupportedChannelMasks,
819 &IStream::getChannelMask,
820 &IStream::setChannelMask))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800821
Kevin Rocard72e50e22017-05-05 14:02:55 -0700822TEST_IO_STREAM(SupportedFormat,
823 "Check that the stream format is declared as supported",
824 testCapabilityGetter("getSupportedFormat", stream.get(),
825 extract(stream->getFormat()),
826 &IStream::getSupportedFormats,
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800827 &IStream::getFormat, &IStream::setFormat))
828
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800829static void testGetDevice(IStream* stream, AudioDevice expectedDevice) {
Kevin Rocard8f8730c2017-05-02 19:34:29 -0700830 // Unfortunately the interface does not allow the implementation to return
831 // NOT_SUPPORTED
832 // Thus allow NONE as signaling that the call is not supported.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800833 auto ret = stream->getDevice();
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700834 ASSERT_IS_OK(ret);
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800835 AudioDevice device = ret;
Kevin Rocard8f8730c2017-05-02 19:34:29 -0700836 ASSERT_TRUE(device == expectedDevice || device == AudioDevice::NONE)
837 << "Expected: " << ::testing::PrintToString(expectedDevice)
838 << "\n Actual: " << ::testing::PrintToString(device);
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800839}
840
Kevin Rocard72e50e22017-05-05 14:02:55 -0700841TEST_IO_STREAM(GetDevice,
842 "Check that the stream device == the one it was opened with",
843 areAudioPatchesSupported()
844 ? doc::partialTest("Audio patches are supported")
845 : testGetDevice(stream.get(), address.device))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800846
847static void testSetDevice(IStream* stream, const DeviceAddress& address) {
848 DeviceAddress otherAddress = address;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700849 otherAddress.device = (address.device & AudioDevice::BIT_IN) == 0
850 ? AudioDevice::OUT_SPEAKER
851 : AudioDevice::IN_BUILTIN_MIC;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800852 EXPECT_OK(stream->setDevice(otherAddress));
853
Kevin Rocard72e50e22017-05-05 14:02:55 -0700854 ASSERT_OK(stream->setDevice(address)); // Go back to the original value
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800855}
856
Kevin Rocard72e50e22017-05-05 14:02:55 -0700857TEST_IO_STREAM(
858 SetDevice,
859 "Check that the stream can be rerouted to SPEAKER or BUILTIN_MIC",
860 areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported")
861 : testSetDevice(stream.get(), address))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800862
Kevin Rocardfd297c62017-05-12 15:25:09 -0700863static void testGetAudioProperties(IStream* stream) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800864 uint32_t sampleRateHz;
865 AudioChannelMask mask;
866 AudioFormat format;
Kevin Rocardf0357882017-02-10 16:19:28 -0800867 stream->getAudioProperties(returnIn(sampleRateHz, mask, format));
Kevin Rocardf0357882017-02-10 16:19:28 -0800868}
869
Kevin Rocard72e50e22017-05-05 14:02:55 -0700870TEST_IO_STREAM(
871 GetAudioProperties,
872 "Check that the stream audio properties == the ones it was opened with",
Kevin Rocardfd297c62017-05-12 15:25:09 -0700873 testGetAudioProperties(stream.get()))
Kevin Rocardf0357882017-02-10 16:19:28 -0800874
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800875static void testConnectedState(IStream* stream) {
876 DeviceAddress address = {};
877 using AD = AudioDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700878 for (auto device :
879 {AD::OUT_HDMI, AD::OUT_WIRED_HEADPHONE, AD::IN_USB_HEADSET}) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800880 address.device = device;
881
882 ASSERT_OK(stream->setConnectedState(address, true));
883 ASSERT_OK(stream->setConnectedState(address, false));
884 }
885}
886TEST_IO_STREAM(SetConnectedState,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700887 "Check that the stream can be notified of device connection and "
888 "deconnection",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800889 testConnectedState(stream.get()))
890
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700891static auto invalidArgsOrNotSupportedOrOK = {Result::INVALID_ARGUMENTS,
892 Result::NOT_SUPPORTED, Result::OK};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800893TEST_IO_STREAM(SetHwAvSync, "Try to set hardware sync to an invalid value",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700894 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700895 stream->setHwAvSync(666)))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800896
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700897TEST_IO_STREAM(GetHwAvSync, "Get hardware sync can not fail",
Kevin Rocardf26f67a2017-05-02 19:21:58 -0700898 ASSERT_IS_OK(device->getHwAvSync()));
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700899
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700900static void checkGetNoParameter(IStream* stream, hidl_vec<hidl_string> keys,
901 vector<Result> expectedResults) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800902 hidl_vec<ParameterValue> parameters;
903 Result res;
904 ASSERT_OK(stream->getParameters(keys, returnIn(res, parameters)));
905 ASSERT_RESULT(expectedResults, res);
906 if (res == Result::OK) {
Kevin Rocard4aefd1c2017-05-02 18:58:58 -0700907 for (auto& parameter : parameters) {
908 ASSERT_EQ(0U, parameter.value.size()) << toString(parameter);
909 }
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800910 }
911}
912
Kevin Rocard72e50e22017-05-05 14:02:55 -0700913/* Get/Set parameter is intended to be an opaque channel between vendors app and
914 * their HALs.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800915 * Thus can not be meaningfully tested.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800916 */
917TEST_IO_STREAM(getEmptySetParameter, "Retrieve the values of an empty set",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700918 checkGetNoParameter(stream.get(), {} /* keys */, {Result::OK}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800919
Kevin Rocard72e50e22017-05-05 14:02:55 -0700920TEST_IO_STREAM(getNonExistingParameter,
921 "Retrieve the values of an non existing parameter",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700922 checkGetNoParameter(stream.get(),
923 {"Non existing key"} /* keys */,
924 {Result::NOT_SUPPORTED}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800925
Kevin Rocard72e50e22017-05-05 14:02:55 -0700926TEST_IO_STREAM(setEmptySetParameter,
927 "Set the values of an empty set of parameters",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700928 ASSERT_RESULT(Result::OK, stream->setParameters({})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800929
Kevin Rocard72e50e22017-05-05 14:02:55 -0700930TEST_IO_STREAM(
931 setNonExistingParameter, "Set the values of an non existing parameter",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700932 // Unfortunately, the set_parameter legacy interface did not return any
933 // error code when a key is not supported.
934 // To allow implementation to just wrapped the legacy one, consider OK as a
935 // valid result for setting a non existing parameter.
936 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700937 stream->setParameters({{"non existing key", "0"}})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800938
Kevin Rocardb9031242017-03-13 12:20:54 -0700939TEST_IO_STREAM(DebugDump,
940 "Check that a stream can dump its state without error",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700941 testDebugDump([this](const auto& handle) {
942 return stream->debugDump(handle);
943 }))
Kevin Rocardb9031242017-03-13 12:20:54 -0700944
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700945TEST_IO_STREAM(DebugDumpInvalidArguments,
946 "Check that the stream dump doesn't crash on invalid arguments",
947 ASSERT_OK(stream->debugDump(hidl_handle())))
948
Kevin Rocardf0357882017-02-10 16:19:28 -0800949//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800950////////////////////////////// addRemoveEffect ///////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800951//////////////////////////////////////////////////////////////////////////////
952
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800953TEST_IO_STREAM(AddNonExistingEffect, "Adding a non existing effect should fail",
954 ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->addEffect(666)))
Kevin Rocard72e50e22017-05-05 14:02:55 -0700955TEST_IO_STREAM(RemoveNonExistingEffect,
956 "Removing a non existing effect should fail",
957 ASSERT_RESULT(Result::INVALID_ARGUMENTS,
958 stream->removeEffect(666)))
Kevin Rocardf0357882017-02-10 16:19:28 -0800959
Kevin Rocard72e50e22017-05-05 14:02:55 -0700960// TODO: positive tests
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800961
962//////////////////////////////////////////////////////////////////////////////
963/////////////////////////////// Control ////////////////////////////////
964//////////////////////////////////////////////////////////////////////////////
965
966TEST_IO_STREAM(standby, "Make sure the stream can be put in stanby",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700967 ASSERT_OK(stream->standby())) // can not fail
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800968
Kevin Rocard72e50e22017-05-05 14:02:55 -0700969static vector<Result> invalidStateOrNotSupported = {Result::INVALID_STATE,
970 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800971
Kevin Rocard72e50e22017-05-05 14:02:55 -0700972TEST_IO_STREAM(startNoMmap,
973 "Starting a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800974 ASSERT_RESULT(invalidStateOrNotSupported, stream->start()))
975
Kevin Rocard72e50e22017-05-05 14:02:55 -0700976TEST_IO_STREAM(stopNoMmap,
977 "Stopping a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800978 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
979
Kevin Rocard72e50e22017-05-05 14:02:55 -0700980TEST_IO_STREAM(getMmapPositionNoMmap,
981 "Get a stream Mmap position before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800982 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
983
Kevin Rocard72e50e22017-05-05 14:02:55 -0700984TEST_IO_STREAM(close, "Make sure a stream can be closed",
985 ASSERT_OK(closeStream()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800986TEST_IO_STREAM(closeTwice, "Make sure a stream can not be closed twice",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700987 ASSERT_OK(closeStream());
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800988 ASSERT_RESULT(Result::INVALID_STATE, closeStream()))
989
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700990static auto invalidArgsOrNotSupported = {Result::INVALID_ARGUMENTS,
991 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800992static void testCreateTooBigMmapBuffer(IStream* stream) {
993 MmapBufferInfo info;
994 Result res;
995 // Assume that int max is a value too big to be allocated
Kevin Rocard72e50e22017-05-05 14:02:55 -0700996 // This is true currently with a 32bit media server, but might not when it
997 // will run in 64 bit
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800998 auto minSizeFrames = std::numeric_limits<int32_t>::max();
999 ASSERT_OK(stream->createMmapBuffer(minSizeFrames, returnIn(res, info)));
1000 ASSERT_RESULT(invalidArgsOrNotSupported, res);
Kevin Rocardf0357882017-02-10 16:19:28 -08001001}
1002
Kevin Rocard8878b4b2017-03-10 18:47:37 -08001003TEST_IO_STREAM(CreateTooBigMmapBuffer, "Create mmap buffer too big should fail",
1004 testCreateTooBigMmapBuffer(stream.get()))
1005
Kevin Rocard8878b4b2017-03-10 18:47:37 -08001006static void testGetMmapPositionOfNonMmapedStream(IStream* stream) {
1007 Result res;
1008 MmapPosition position;
1009 ASSERT_OK(stream->getMmapPosition(returnIn(res, position)));
1010 ASSERT_RESULT(invalidArgsOrNotSupported, res);
1011}
1012
Kevin Rocard72e50e22017-05-05 14:02:55 -07001013TEST_IO_STREAM(
1014 GetMmapPositionOfNonMmapedStream,
1015 "Retrieving the mmap position of a non mmaped stream should fail",
1016 testGetMmapPositionOfNonMmapedStream(stream.get()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -08001017
Kevin Rocardf0357882017-02-10 16:19:28 -08001018//////////////////////////////////////////////////////////////////////////////
Kevin Rocardc9963522017-03-10 18:47:37 -08001019///////////////////////////////// StreamIn ///////////////////////////////////
1020//////////////////////////////////////////////////////////////////////////////
1021
1022TEST_P(InputStreamTest, GetAudioSource) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001023 doc::test(
1024 "Retrieving the audio source of an input stream should always succeed");
Kevin Rocardc9963522017-03-10 18:47:37 -08001025 AudioSource source;
1026 ASSERT_OK(stream->getAudioSource(returnIn(res, source)));
Kevin Rocard98390a62017-05-03 11:16:05 -07001027 if (res == Result::NOT_SUPPORTED) {
1028 doc::partialTest("getAudioSource is not supported");
1029 return;
1030 }
Kevin Rocardc9963522017-03-10 18:47:37 -08001031 ASSERT_OK(res);
1032 ASSERT_EQ(AudioSource::DEFAULT, source);
1033}
1034
Kevin Rocard72e50e22017-05-05 14:02:55 -07001035static void testUnitaryGain(std::function<Return<Result>(float)> setGain) {
Kevin Rocardc4f1b2f2017-05-03 11:19:25 -07001036 for (float value :
1037 (float[]){-INFINITY, -1.0, 1.0 + std::numeric_limits<float>::epsilon(),
1038 2.0, INFINITY, NAN}) {
Kevin Rocarda1d6ea42017-05-08 17:08:11 -07001039 EXPECT_RESULT(Result::INVALID_ARGUMENTS, setGain(value)) << "value="
1040 << value;
Kevin Rocardc9963522017-03-10 18:47:37 -08001041 }
Kevin Rocardc4f1b2f2017-05-03 11:19:25 -07001042 // Do not consider -0.0 as an invalid value as it is == with 0.0
1043 for (float value : {-0.0, 0.0, 0.01, 0.5, 0.09, 1.0 /* Restore volume*/}) {
Kevin Rocarda1d6ea42017-05-08 17:08:11 -07001044 EXPECT_OK(setGain(value)) << "value=" << value;
Kevin Rocardc4f1b2f2017-05-03 11:19:25 -07001045 }
Kevin Rocardc9963522017-03-10 18:47:37 -08001046}
1047
Kevin Rocard98390a62017-05-03 11:16:05 -07001048static void testOptionalUnitaryGain(
1049 std::function<Return<Result>(float)> setGain, string debugName) {
1050 auto result = setGain(1);
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001051 ASSERT_IS_OK(result);
Kevin Rocard98390a62017-05-03 11:16:05 -07001052 if (result == Result::NOT_SUPPORTED) {
1053 doc::partialTest(debugName + " is not supported");
1054 return;
1055 }
1056 testUnitaryGain(setGain);
1057}
1058
Kevin Rocardc9963522017-03-10 18:47:37 -08001059TEST_P(InputStreamTest, SetGain) {
1060 doc::test("The gain of an input stream should only be set between [0,1]");
Kevin Rocard98390a62017-05-03 11:16:05 -07001061 testOptionalUnitaryGain(
1062 [this](float volume) { return stream->setGain(volume); },
1063 "InputStream::setGain");
Kevin Rocardc9963522017-03-10 18:47:37 -08001064}
1065
Kevin Rocard72e50e22017-05-05 14:02:55 -07001066static void testPrepareForReading(IStreamIn* stream, uint32_t frameSize,
1067 uint32_t framesCount) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001068 Result res;
1069 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001070 ASSERT_OK(stream->prepareForReading(
1071 frameSize, framesCount,
1072 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocardfcf186b2017-05-03 11:16:05 -07001073 EXPECT_RESULT(Result::INVALID_ARGUMENTS, res);
Kevin Rocardc9963522017-03-10 18:47:37 -08001074}
1075
Kevin Rocard195205b2017-05-02 18:34:59 -07001076TEST_P(InputStreamTest, PrepareForReadingWithZeroBuffer) {
1077 doc::test(
1078 "Preparing a stream for reading with a 0 sized buffer should fail");
1079 testPrepareForReading(stream.get(), 0, 0);
1080}
1081
Kevin Rocardc9963522017-03-10 18:47:37 -08001082TEST_P(InputStreamTest, PrepareForReadingWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001083 doc::test(
1084 "Preparing a stream for reading with a 2^32 sized buffer should fail");
1085 testPrepareForReading(stream.get(), 1,
1086 std::numeric_limits<uint32_t>::max());
Kevin Rocardc9963522017-03-10 18:47:37 -08001087}
1088
1089TEST_P(InputStreamTest, PrepareForReadingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001090 doc::test(
1091 "Preparing a stream for reading with a overflowing sized buffer should "
1092 "fail");
Kevin Rocardc9963522017-03-10 18:47:37 -08001093 auto uintMax = std::numeric_limits<uint32_t>::max();
1094 testPrepareForReading(stream.get(), uintMax, uintMax);
1095}
1096
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001097TEST_P(InputStreamTest, GetInputFramesLost) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001098 doc::test(
1099 "The number of frames lost on a never started stream should be 0");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001100 auto ret = stream->getInputFramesLost();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001101 ASSERT_IS_OK(ret);
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001102 uint32_t framesLost{ret};
1103 ASSERT_EQ(0U, framesLost);
1104}
1105
Kevin Rocardc9963522017-03-10 18:47:37 -08001106TEST_P(InputStreamTest, getCapturePosition) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001107 doc::test(
1108 "The capture position of a non prepared stream should not be "
1109 "retrievable");
Kevin Rocardc9963522017-03-10 18:47:37 -08001110 uint64_t frames;
1111 uint64_t time;
1112 ASSERT_OK(stream->getCapturePosition(returnIn(res, frames, time)));
1113 ASSERT_RESULT(invalidStateOrNotSupported, res);
1114}
1115
1116//////////////////////////////////////////////////////////////////////////////
Kevin Rocard624800c2017-03-10 18:47:37 -08001117///////////////////////////////// StreamIn ///////////////////////////////////
1118//////////////////////////////////////////////////////////////////////////////
1119
1120TEST_P(OutputStreamTest, getLatency) {
1121 doc::test("Make sure latency is over 0");
1122 auto result = stream->getLatency();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001123 ASSERT_IS_OK(result);
Kevin Rocard624800c2017-03-10 18:47:37 -08001124 ASSERT_GT(result, 0U);
1125}
1126
1127TEST_P(OutputStreamTest, setVolume) {
1128 doc::test("Try to set the output volume");
Kevin Rocard98390a62017-05-03 11:16:05 -07001129 testOptionalUnitaryGain(
1130 [this](float volume) { return stream->setVolume(volume, volume); },
1131 "setVolume");
Kevin Rocard624800c2017-03-10 18:47:37 -08001132}
1133
Kevin Rocard72e50e22017-05-05 14:02:55 -07001134static void testPrepareForWriting(IStreamOut* stream, uint32_t frameSize,
1135 uint32_t framesCount) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001136 Result res;
1137 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001138 ASSERT_OK(stream->prepareForWriting(
1139 frameSize, framesCount,
1140 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocardfcf186b2017-05-03 11:16:05 -07001141 EXPECT_RESULT(Result::INVALID_ARGUMENTS, res);
Kevin Rocard624800c2017-03-10 18:47:37 -08001142}
1143
Kevin Rocard195205b2017-05-02 18:34:59 -07001144TEST_P(OutputStreamTest, PrepareForWriteWithZeroBuffer) {
1145 doc::test(
1146 "Preparing a stream for writing with a 0 sized buffer should fail");
1147 testPrepareForWriting(stream.get(), 0, 0);
1148}
1149
Kevin Rocard624800c2017-03-10 18:47:37 -08001150TEST_P(OutputStreamTest, PrepareForWriteWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001151 doc::test(
1152 "Preparing a stream for writing with a 2^32 sized buffer should fail");
1153 testPrepareForWriting(stream.get(), 1,
1154 std::numeric_limits<uint32_t>::max());
Kevin Rocard624800c2017-03-10 18:47:37 -08001155}
1156
1157TEST_P(OutputStreamTest, PrepareForWritingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001158 doc::test(
1159 "Preparing a stream for writing with a overflowing sized buffer should "
1160 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001161 auto uintMax = std::numeric_limits<uint32_t>::max();
1162 testPrepareForWriting(stream.get(), uintMax, uintMax);
1163}
1164
1165struct Capability {
1166 Capability(IStreamOut* stream) {
1167 EXPECT_OK(stream->supportsPauseAndResume(returnIn(pause, resume)));
1168 auto ret = stream->supportsDrain();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001169 EXPECT_IS_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001170 if (ret.isOk()) {
1171 drain = ret;
1172 }
1173 }
1174 bool pause = false;
1175 bool resume = false;
1176 bool drain = false;
1177};
1178
1179TEST_P(OutputStreamTest, SupportsPauseAndResumeAndDrain) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001180 doc::test(
1181 "Implementation must expose pause, resume and drain capabilities");
Kevin Rocard624800c2017-03-10 18:47:37 -08001182 Capability(stream.get());
1183}
1184
Kevin Rocard304b6c82017-05-03 10:57:06 -07001185template <class Value>
1186static void checkInvalidStateOr0(Result res, Value value) {
1187 switch (res) {
1188 case Result::INVALID_STATE:
1189 break;
1190 case Result::OK:
1191 ASSERT_EQ(0U, value);
1192 break;
1193 default:
1194 FAIL() << "Unexpected result " << toString(res);
1195 }
1196}
1197
Kevin Rocard624800c2017-03-10 18:47:37 -08001198TEST_P(OutputStreamTest, GetRenderPosition) {
Kevin Rocard304b6c82017-05-03 10:57:06 -07001199 doc::test("A new stream render position should be 0 or INVALID_STATE");
Kevin Rocard624800c2017-03-10 18:47:37 -08001200 uint32_t dspFrames;
1201 ASSERT_OK(stream->getRenderPosition(returnIn(res, dspFrames)));
1202 if (res == Result::NOT_SUPPORTED) {
1203 doc::partialTest("getRenderPosition is not supported");
1204 return;
1205 }
Kevin Rocard304b6c82017-05-03 10:57:06 -07001206 checkInvalidStateOr0(res, dspFrames);
Kevin Rocard624800c2017-03-10 18:47:37 -08001207}
1208
1209TEST_P(OutputStreamTest, GetNextWriteTimestamp) {
Kevin Rocard304b6c82017-05-03 10:57:06 -07001210 doc::test("A new stream next write timestamp should be 0 or INVALID_STATE");
Kevin Rocard624800c2017-03-10 18:47:37 -08001211 uint64_t timestampUs;
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001212 ASSERT_OK(stream->getNextWriteTimestamp(returnIn(res, timestampUs)));
Kevin Rocard624800c2017-03-10 18:47:37 -08001213 if (res == Result::NOT_SUPPORTED) {
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001214 doc::partialTest("getNextWriteTimestamp is not supported");
Kevin Rocard624800c2017-03-10 18:47:37 -08001215 return;
1216 }
Kevin Rocard304b6c82017-05-03 10:57:06 -07001217 checkInvalidStateOr0(res, timestampUs);
Kevin Rocard624800c2017-03-10 18:47:37 -08001218}
1219
1220/** Stub implementation of out stream callback. */
1221class MockOutCallbacks : public IStreamOutCallback {
1222 Return<void> onWriteReady() override { return {}; }
1223 Return<void> onDrainReady() override { return {}; }
1224 Return<void> onError() override { return {}; }
1225};
1226
Kevin Rocard72e50e22017-05-05 14:02:55 -07001227static bool isAsyncModeSupported(IStreamOut* stream) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001228 auto res = stream->setCallback(new MockOutCallbacks);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001229 stream->clearCallback(); // try to restore the no callback state, ignore
1230 // any error
1231 auto okOrNotSupported = {Result::OK, Result::NOT_SUPPORTED};
Kevin Rocard624800c2017-03-10 18:47:37 -08001232 EXPECT_RESULT(okOrNotSupported, res);
1233 return res.isOk() ? res == Result::OK : false;
1234}
1235
1236TEST_P(OutputStreamTest, SetCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001237 doc::test(
1238 "If supported, registering callback for async operation should never "
1239 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001240 if (!isAsyncModeSupported(stream.get())) {
1241 doc::partialTest("The stream does not support async operations");
1242 return;
1243 }
1244 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1245 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1246}
1247
1248TEST_P(OutputStreamTest, clearCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001249 doc::test(
1250 "If supported, clearing a callback to go back to sync operation should "
1251 "not fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001252 if (!isAsyncModeSupported(stream.get())) {
1253 doc::partialTest("The stream does not support async operations");
1254 return;
1255 }
1256 // TODO: Clarify if clearing a non existing callback should fail
1257 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1258 ASSERT_OK(stream->clearCallback());
1259}
1260
1261TEST_P(OutputStreamTest, Resume) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001262 doc::test(
1263 "If supported, a stream should fail to resume if not previously "
1264 "paused");
Kevin Rocard624800c2017-03-10 18:47:37 -08001265 if (!Capability(stream.get()).resume) {
1266 doc::partialTest("The output stream does not support resume");
1267 return;
1268 }
1269 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1270}
1271
1272TEST_P(OutputStreamTest, Pause) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001273 doc::test(
1274 "If supported, a stream should fail to pause if not previously "
1275 "started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001276 if (!Capability(stream.get()).pause) {
1277 doc::partialTest("The output stream does not support pause");
1278 return;
1279 }
1280 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1281}
1282
Kevin Rocard72e50e22017-05-05 14:02:55 -07001283static void testDrain(IStreamOut* stream, AudioDrain type) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001284 if (!Capability(stream).drain) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001285 doc::partialTest("The output stream does not support drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001286 return;
1287 }
1288 ASSERT_RESULT(Result::OK, stream->drain(type));
1289}
1290
1291TEST_P(OutputStreamTest, DrainAll) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001292 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001293 testDrain(stream.get(), AudioDrain::ALL);
1294}
1295
1296TEST_P(OutputStreamTest, DrainEarlyNotify) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001297 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001298 testDrain(stream.get(), AudioDrain::EARLY_NOTIFY);
1299}
1300
1301TEST_P(OutputStreamTest, FlushStop) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001302 doc::test("If supported, a stream should always succeed to flush");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001303 auto ret = stream->flush();
Kevin Rocardf26f67a2017-05-02 19:21:58 -07001304 ASSERT_IS_OK(ret);
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001305 if (ret == Result::NOT_SUPPORTED) {
1306 doc::partialTest("Flush is not supported");
1307 return;
1308 }
1309 ASSERT_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001310}
1311
1312TEST_P(OutputStreamTest, GetPresentationPositionStop) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001313 doc::test(
1314 "If supported, a stream should always succeed to retrieve the "
1315 "presentation position");
Kevin Rocard624800c2017-03-10 18:47:37 -08001316 uint64_t frames;
1317 TimeSpec mesureTS;
1318 ASSERT_OK(stream->getPresentationPosition(returnIn(res, frames, mesureTS)));
1319 if (res == Result::NOT_SUPPORTED) {
1320 doc::partialTest("getpresentationPosition is not supported");
1321 return;
1322 }
1323 ASSERT_EQ(0U, frames);
1324
Kevin Rocard476e38f2017-05-03 10:52:43 -07001325 if (mesureTS.tvNSec == 0 && mesureTS.tvSec == 0) {
1326 // As the stream has never written a frame yet,
1327 // the timestamp does not really have a meaning, allow to return 0
1328 return;
1329 }
1330
1331 // Make sure the return measure is not more than 1s old.
Kevin Rocard624800c2017-03-10 18:47:37 -08001332 struct timespec currentTS;
1333 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currentTS)) << errno;
1334
Kevin Rocard72e50e22017-05-05 14:02:55 -07001335 auto toMicroSec = [](uint64_t sec, auto nsec) {
1336 return sec * 1e+6 + nsec / 1e+3;
1337 };
Kevin Rocard624800c2017-03-10 18:47:37 -08001338 auto currentTime = toMicroSec(currentTS.tv_sec, currentTS.tv_nsec);
1339 auto mesureTime = toMicroSec(mesureTS.tvSec, mesureTS.tvNSec);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001340 ASSERT_PRED2([](auto c, auto m) { return c - m < 1e+6; }, currentTime,
1341 mesureTime);
Kevin Rocard624800c2017-03-10 18:47:37 -08001342}
1343
Kevin Rocard624800c2017-03-10 18:47:37 -08001344//////////////////////////////////////////////////////////////////////////////
Kevin Rocard3c405a72017-03-08 16:46:51 -08001345/////////////////////////////// PrimaryDevice ////////////////////////////////
1346//////////////////////////////////////////////////////////////////////////////
1347
1348TEST_F(AudioPrimaryHidlTest, setVoiceVolume) {
1349 doc::test("Make sure setVoiceVolume only succeed if volume is in [0,1]");
Chih-Hung Hsiehd3bc6812017-05-15 12:31:25 -07001350 testUnitaryGain([](float volume) { return device->setVoiceVolume(volume); });
Kevin Rocard3c405a72017-03-08 16:46:51 -08001351}
1352
1353TEST_F(AudioPrimaryHidlTest, setMode) {
Kevin Rocard04364ed2017-05-02 18:16:00 -07001354 doc::test(
1355 "Make sure setMode always succeeds if mode is valid "
1356 "and fails otherwise");
1357 // Test Invalid values
1358 for (AudioMode mode :
1359 {AudioMode::INVALID, AudioMode::CURRENT, AudioMode::CNT}) {
1360 SCOPED_TRACE("mode=" + toString(mode));
1361 ASSERT_RESULT(Result::INVALID_ARGUMENTS, device->setMode(mode));
1362 }
1363 // Test valid values
Kevin Rocard72e50e22017-05-05 14:02:55 -07001364 for (AudioMode mode :
1365 {AudioMode::IN_CALL, AudioMode::IN_COMMUNICATION, AudioMode::RINGTONE,
Kevin Rocard72e50e22017-05-05 14:02:55 -07001366 AudioMode::NORMAL /* Make sure to leave the test in normal mode */}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -08001367 SCOPED_TRACE("mode=" + toString(mode));
1368 ASSERT_OK(device->setMode(mode));
1369 }
Kevin Rocard3c405a72017-03-08 16:46:51 -08001370}
1371
Kevin Rocard3c405a72017-03-08 16:46:51 -08001372TEST_F(BoolAccessorPrimaryHidlTest, BtScoNrecEnabled) {
1373 doc::test("Query and set the BT SCO NR&EC state");
1374 testOptionalAccessors("BtScoNrecEnabled", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001375 &IPrimaryDevice::setBtScoNrecEnabled,
1376 &IPrimaryDevice::getBtScoNrecEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001377}
1378
1379TEST_F(BoolAccessorPrimaryHidlTest, setGetBtScoWidebandEnabled) {
1380 doc::test("Query and set the SCO whideband state");
1381 testOptionalAccessors("BtScoWideband", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001382 &IPrimaryDevice::setBtScoWidebandEnabled,
1383 &IPrimaryDevice::getBtScoWidebandEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001384}
1385
1386using TtyModeAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<TtyMode>;
1387TEST_F(TtyModeAccessorPrimaryHidlTest, setGetTtyMode) {
1388 doc::test("Query and set the TTY mode state");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001389 testOptionalAccessors(
1390 "TTY mode", {TtyMode::OFF, TtyMode::HCO, TtyMode::VCO, TtyMode::FULL},
1391 &IPrimaryDevice::setTtyMode, &IPrimaryDevice::getTtyMode);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001392}
1393
1394TEST_F(BoolAccessorPrimaryHidlTest, setGetHac) {
1395 doc::test("Query and set the HAC state");
Kevin Rocard98390a62017-05-03 11:16:05 -07001396 testOptionalAccessors("HAC", {true, false, true},
1397 &IPrimaryDevice::setHacEnabled,
1398 &IPrimaryDevice::getHacEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001399}
1400
1401//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -08001402//////////////////// Clean caches on global tear down ////////////////////////
1403//////////////////////////////////////////////////////////////////////////////
1404
1405int main(int argc, char** argv) {
1406 environment = new Environment;
1407 ::testing::AddGlobalTestEnvironment(environment);
1408 ::testing::InitGoogleTest(&argc, argv);
1409 int status = RUN_ALL_TESTS();
1410 LOG(INFO) << "Test result = " << status;
1411 return status;
1412}