blob: 0bb0769dd052f1560f5710ad627a931a3e0061ae [file] [log] [blame]
Kevin Rocardf0357882017-02-10 16:19:28 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "VtsHalAudioV2_0TargetTest"
18
19#include <algorithm>
20#include <cmath>
21#include <cstddef>
22#include <cstdio>
23#include <limits>
24#include <list>
25#include <string>
26#include <type_traits>
27#include <vector>
28
Yuexi Ma161b5642017-03-10 13:44:22 -080029#include <VtsHalHidlTargetTestBase.h>
Kevin Rocardf0357882017-02-10 16:19:28 -080030
31#include <android-base/logging.h>
32
33#include <android/hardware/audio/2.0/IDevice.h>
34#include <android/hardware/audio/2.0/IDevicesFactory.h>
Kevin Rocard3c405a72017-03-08 16:46:51 -080035#include <android/hardware/audio/2.0/IPrimaryDevice.h>
Kevin Rocardf0357882017-02-10 16:19:28 -080036#include <android/hardware/audio/2.0/types.h>
37#include <android/hardware/audio/common/2.0/types.h>
38
Kevin Rocardf0357882017-02-10 16:19:28 -080039#include "utility/AssertOk.h"
40#include "utility/PrettyPrintAudioTypes.h"
Kevin Rocard72e50e22017-05-05 14:02:55 -070041#include "utility/ReturnIn.h"
Kevin Rocardf0357882017-02-10 16:19:28 -080042
43using std::string;
44using std::to_string;
45using std::vector;
46
47using ::android::sp;
48using ::android::hardware::Return;
49using ::android::hardware::hidl_handle;
50using ::android::hardware::hidl_string;
51using ::android::hardware::hidl_vec;
Kevin Rocardc9963522017-03-10 18:47:37 -080052using ::android::hardware::MQDescriptorSync;
Kevin Rocard624800c2017-03-10 18:47:37 -080053using ::android::hardware::audio::V2_0::AudioDrain;
Kevin Rocardf0357882017-02-10 16:19:28 -080054using ::android::hardware::audio::V2_0::DeviceAddress;
55using ::android::hardware::audio::V2_0::IDevice;
Kevin Rocard3c405a72017-03-08 16:46:51 -080056using ::android::hardware::audio::V2_0::IPrimaryDevice;
57using TtyMode = ::android::hardware::audio::V2_0::IPrimaryDevice::TtyMode;
Kevin Rocardf0357882017-02-10 16:19:28 -080058using ::android::hardware::audio::V2_0::IDevicesFactory;
59using ::android::hardware::audio::V2_0::IStream;
60using ::android::hardware::audio::V2_0::IStreamIn;
Kevin Rocard624800c2017-03-10 18:47:37 -080061using ::android::hardware::audio::V2_0::TimeSpec;
Kevin Rocard72e50e22017-05-05 14:02:55 -070062using ReadParameters =
63 ::android::hardware::audio::V2_0::IStreamIn::ReadParameters;
Kevin Rocardc9963522017-03-10 18:47:37 -080064using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus;
Kevin Rocardf0357882017-02-10 16:19:28 -080065using ::android::hardware::audio::V2_0::IStreamOut;
Kevin Rocard624800c2017-03-10 18:47:37 -080066using ::android::hardware::audio::V2_0::IStreamOutCallback;
Kevin Rocard8878b4b2017-03-10 18:47:37 -080067using ::android::hardware::audio::V2_0::MmapBufferInfo;
68using ::android::hardware::audio::V2_0::MmapPosition;
Kevin Rocardf0357882017-02-10 16:19:28 -080069using ::android::hardware::audio::V2_0::ParameterValue;
70using ::android::hardware::audio::V2_0::Result;
71using ::android::hardware::audio::common::V2_0::AudioChannelMask;
72using ::android::hardware::audio::common::V2_0::AudioConfig;
73using ::android::hardware::audio::common::V2_0::AudioDevice;
74using ::android::hardware::audio::common::V2_0::AudioFormat;
75using ::android::hardware::audio::common::V2_0::AudioHandleConsts;
76using ::android::hardware::audio::common::V2_0::AudioInputFlag;
77using ::android::hardware::audio::common::V2_0::AudioIoHandle;
Kevin Rocard3c405a72017-03-08 16:46:51 -080078using ::android::hardware::audio::common::V2_0::AudioMode;
Kevin Rocardf0357882017-02-10 16:19:28 -080079using ::android::hardware::audio::common::V2_0::AudioOffloadInfo;
80using ::android::hardware::audio::common::V2_0::AudioOutputFlag;
81using ::android::hardware::audio::common::V2_0::AudioSource;
Kevin Rocardc9963522017-03-10 18:47:37 -080082using ::android::hardware::audio::common::V2_0::ThreadInfo;
Kevin Rocardf0357882017-02-10 16:19:28 -080083
84using utility::returnIn;
85
86namespace doc {
87/** Document the current test case.
Kevin Rocard72e50e22017-05-05 14:02:55 -070088 * Eg: calling `doc::test("Dump the state of the hal")` in the "debugDump" test
89 * will output:
90 * <testcase name="debugDump" status="run" time="6"
91 * classname="AudioPrimaryHidlTest"
92 description="Dump the state of the hal." />
93 * see
94 https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#logging-additional-information
Kevin Rocardf0357882017-02-10 16:19:28 -080095 */
96void test(const std::string& testCaseDocumentation) {
97 ::testing::Test::RecordProperty("description", testCaseDocumentation);
98}
99
Kevin Rocard72e50e22017-05-05 14:02:55 -0700100/** Document why a test was not fully run. Usually due to an optional feature
101 * not implemented. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800102void partialTest(const std::string& reason) {
103 ::testing::Test::RecordProperty("partialyRunTest", reason);
104}
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700105
106/** Add a note to the test. */
107void note(const std::string& note) {
108 ::testing::Test::RecordProperty("note", note);
109}
Kevin Rocardf0357882017-02-10 16:19:28 -0800110}
111
112// Register callback for static object destruction
113// Avoid destroying static objects after main return.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700114// Post main return destruction leads to incorrect gtest timing measurements as
115// well as harder
Kevin Rocardf0357882017-02-10 16:19:28 -0800116// debuging if anything goes wrong during destruction.
117class Environment : public ::testing::Environment {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700118 public:
119 using TearDownFunc = std::function<void()>;
120 void registerTearDown(TearDownFunc&& tearDown) {
121 tearDowns.push_back(std::move(tearDown));
Kevin Rocardf0357882017-02-10 16:19:28 -0800122 }
123
Kevin Rocard72e50e22017-05-05 14:02:55 -0700124 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800125 void TearDown() override {
126 // Call the tear downs in reverse order of insertion
127 for (auto& tearDown : tearDowns) {
128 tearDown();
129 }
130 }
131 std::list<TearDownFunc> tearDowns;
132};
133// Instance to register global tearDown
134static Environment* environment;
135
Yuexi Ma161b5642017-03-10 13:44:22 -0800136class HidlTest : public ::testing::VtsHalHidlTargetTestBase {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700137 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800138 // Convenient member to store results
139 Result res;
140};
141
142//////////////////////////////////////////////////////////////////////////////
143////////////////////// getService audio_devices_factory //////////////////////
144//////////////////////////////////////////////////////////////////////////////
145
146// Test all audio devices
147class AudioHidlTest : public HidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700148 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800149 void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700150 ASSERT_NO_FATAL_FAILURE(HidlTest::SetUp()); // setup base
Kevin Rocardf0357882017-02-10 16:19:28 -0800151
152 if (devicesFactory == nullptr) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700153 environment->registerTearDown([] { devicesFactory.clear(); });
154 devicesFactory = ::testing::VtsHalHidlTargetTestBase::getService<
155 IDevicesFactory>();
Kevin Rocardf0357882017-02-10 16:19:28 -0800156 }
157 ASSERT_TRUE(devicesFactory != nullptr);
158 }
159
Kevin Rocard72e50e22017-05-05 14:02:55 -0700160 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800161 // Cache the devicesFactory retrieval to speed up each test by ~0.5s
162 static sp<IDevicesFactory> devicesFactory;
163};
164sp<IDevicesFactory> AudioHidlTest::devicesFactory;
165
166TEST_F(AudioHidlTest, GetAudioDevicesFactoryService) {
167 doc::test("test the getService (called in SetUp)");
168}
169
Mikhail Naganov8604a732017-04-24 09:29:22 -0700170TEST_F(AudioHidlTest, OpenDeviceInvalidParameter) {
171 doc::test("test passing an invalid parameter to openDevice");
172 IDevicesFactory::Result result;
173 sp<IDevice> device;
174 ASSERT_OK(devicesFactory->openDevice(IDevicesFactory::Device(-1),
175 returnIn(result, device)));
176 ASSERT_EQ(IDevicesFactory::Result::INVALID_ARGUMENTS, result);
177 ASSERT_TRUE(device == nullptr);
178}
179
Kevin Rocardf0357882017-02-10 16:19:28 -0800180//////////////////////////////////////////////////////////////////////////////
181/////////////////////////////// openDevice primary ///////////////////////////
182//////////////////////////////////////////////////////////////////////////////
183
184// Test the primary device
185class AudioPrimaryHidlTest : public AudioHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700186 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800187 /** Primary HAL test are NOT thread safe. */
188 void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700189 ASSERT_NO_FATAL_FAILURE(AudioHidlTest::SetUp()); // setup base
Kevin Rocardf0357882017-02-10 16:19:28 -0800190
191 if (device == nullptr) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800192 IDevicesFactory::Result result;
Kevin Rocard3c405a72017-03-08 16:46:51 -0800193 sp<IDevice> baseDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700194 ASSERT_OK(
195 devicesFactory->openDevice(IDevicesFactory::Device::PRIMARY,
196 returnIn(result, baseDevice)));
Kevin Rocardfba442a2017-03-31 19:34:41 -0700197 ASSERT_OK(result);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800198 ASSERT_TRUE(baseDevice != nullptr);
199
Kevin Rocard72e50e22017-05-05 14:02:55 -0700200 environment->registerTearDown([] { device.clear(); });
Kevin Rocard3c405a72017-03-08 16:46:51 -0800201 device = IPrimaryDevice::castFrom(baseDevice);
202 ASSERT_TRUE(device != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800203 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800204 }
205
Kevin Rocard72e50e22017-05-05 14:02:55 -0700206 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800207 // Cache the device opening to speed up each test by ~0.5s
Kevin Rocard3c405a72017-03-08 16:46:51 -0800208 static sp<IPrimaryDevice> device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800209};
Kevin Rocard3c405a72017-03-08 16:46:51 -0800210sp<IPrimaryDevice> AudioPrimaryHidlTest::device;
Kevin Rocardf0357882017-02-10 16:19:28 -0800211
212TEST_F(AudioPrimaryHidlTest, OpenPrimaryDevice) {
213 doc::test("Test the openDevice (called in SetUp)");
214}
215
216TEST_F(AudioPrimaryHidlTest, Init) {
217 doc::test("Test that the audio primary hal initialized correctly");
218 ASSERT_OK(device->initCheck());
219}
220
221//////////////////////////////////////////////////////////////////////////////
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800222///////////////////// {set,get}{Master,Mic}{Mute,Volume} /////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800223//////////////////////////////////////////////////////////////////////////////
224
Kevin Rocard3c405a72017-03-08 16:46:51 -0800225template <class Property>
226class AccessorPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700227 protected:
Kevin Rocard3c405a72017-03-08 16:46:51 -0800228 /** Test a property getter and setter. */
Kevin Rocardf0357882017-02-10 16:19:28 -0800229 template <class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700230 void testAccessors(const string& propertyName,
231 const vector<Property>& valuesToTest, Setter setter,
232 Getter getter,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800233 const vector<Property>& invalidValues = {}) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700234 Property initialValue; // Save initial value to restore it at the end
235 // of the test
Kevin Rocard3c405a72017-03-08 16:46:51 -0800236 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
237 ASSERT_OK(res);
238
239 for (Property setValue : valuesToTest) {
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800240 SCOPED_TRACE("Test " + propertyName + " getter and setter for " +
241 testing::PrintToString(setValue));
Kevin Rocard3c405a72017-03-08 16:46:51 -0800242 ASSERT_OK((device.get()->*setter)(setValue));
243 Property getValue;
244 // Make sure the getter returns the same value just set
245 ASSERT_OK((device.get()->*getter)(returnIn(res, getValue)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800246 ASSERT_OK(res);
Kevin Rocard3c405a72017-03-08 16:46:51 -0800247 EXPECT_EQ(setValue, getValue);
Kevin Rocardf0357882017-02-10 16:19:28 -0800248 }
Kevin Rocard3c405a72017-03-08 16:46:51 -0800249
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800250 for (Property invalidValue : invalidValues) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700251 SCOPED_TRACE("Try to set " + propertyName +
252 " with the invalid value " +
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800253 testing::PrintToString(invalidValue));
Kevin Rocard72e50e22017-05-05 14:02:55 -0700254 EXPECT_RESULT(Result::INVALID_ARGUMENTS,
255 (device.get()->*setter)(invalidValue));
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800256 }
257
Kevin Rocard72e50e22017-05-05 14:02:55 -0700258 ASSERT_OK(
259 (device.get()->*setter)(initialValue)); // restore initial value
Kevin Rocard3c405a72017-03-08 16:46:51 -0800260 }
261
262 /** Test the getter and setter of an optional feature. */
263 template <class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700264 void testOptionalAccessors(const string& propertyName,
265 const vector<Property>& valuesToTest,
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800266 Setter setter, Getter getter,
267 const vector<Property>& invalidValues = {}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -0800268 doc::test("Test the optional " + propertyName + " getters and setter");
269 {
270 SCOPED_TRACE("Test feature support by calling the getter");
271 Property initialValue;
272 ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue)));
273 if (res == Result::NOT_SUPPORTED) {
274 doc::partialTest(propertyName + " getter is not supported");
275 return;
276 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700277 ASSERT_OK(res); // If it is supported it must succeed
Kevin Rocard3c405a72017-03-08 16:46:51 -0800278 }
279 // The feature is supported, test it
Kevin Rocard72e50e22017-05-05 14:02:55 -0700280 testAccessors(propertyName, valuesToTest, setter, getter,
281 invalidValues);
Kevin Rocardf0357882017-02-10 16:19:28 -0800282 }
283};
284
Kevin Rocard3c405a72017-03-08 16:46:51 -0800285using BoolAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<bool>;
286
Kevin Rocardf0357882017-02-10 16:19:28 -0800287TEST_F(BoolAccessorPrimaryHidlTest, MicMuteTest) {
288 doc::test("Check that the mic can be muted and unmuted");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700289 testAccessors("mic mute", {true, false, true}, &IDevice::setMicMute,
290 &IDevice::getMicMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800291 // TODO: check that the mic is really muted (all sample are 0)
292}
293
294TEST_F(BoolAccessorPrimaryHidlTest, MasterMuteTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700295 doc::test(
296 "If master mute is supported, try to mute and unmute the master "
297 "output");
Kevin Rocard3c405a72017-03-08 16:46:51 -0800298 testOptionalAccessors("master mute", {true, false, true},
299 &IDevice::setMasterMute, &IDevice::getMasterMute);
Kevin Rocardf0357882017-02-10 16:19:28 -0800300 // TODO: check that the master volume is really muted
301}
302
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800303using FloatAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<float>;
304TEST_F(FloatAccessorPrimaryHidlTest, MasterVolumeTest) {
305 doc::test("Test the master volume if supported");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700306 testOptionalAccessors("master volume", {0, 0.5, 1},
Kevin Rocard92ce35d2017-03-08 17:17:25 -0800307 &IDevice::setMasterVolume, &IDevice::getMasterVolume,
308 {-0.1, 1.1, NAN, INFINITY, -INFINITY,
309 1 + std::numeric_limits<float>::epsilon()});
310 // TODO: check that the master volume is really changed
311}
312
Kevin Rocardf0357882017-02-10 16:19:28 -0800313//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800314//////////////////////////////// AudioPatches ////////////////////////////////
315//////////////////////////////////////////////////////////////////////////////
316
317class AudioPatchPrimaryHidlTest : public AudioPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700318 protected:
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800319 bool areAudioPatchesSupported() {
320 auto result = device->supportsAudioPatches();
321 EXPECT_TRUE(result.isOk());
322 return result;
323 }
324};
325
326TEST_F(AudioPatchPrimaryHidlTest, AudioPatches) {
327 doc::test("Test if audio patches are supported");
328 if (!areAudioPatchesSupported()) {
329 doc::partialTest("Audio patches are not supported");
330 return;
331 }
332 // TODO: test audio patches
333}
334
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800335//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800336//////////////// Required and recommended audio format support ///////////////
Kevin Rocard72e50e22017-05-05 14:02:55 -0700337// From:
338// https://source.android.com/compatibility/android-cdd.html#5_4_audio_recording
339// From:
340// https://source.android.com/compatibility/android-cdd.html#5_5_audio_playback
Kevin Rocardf0357882017-02-10 16:19:28 -0800341/////////// TODO: move to the beginning of the file for easier update ////////
342//////////////////////////////////////////////////////////////////////////////
343
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800344class AudioConfigPrimaryTest : public AudioPatchPrimaryHidlTest {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700345 public:
Kevin Rocardf0357882017-02-10 16:19:28 -0800346 // Cache result ?
347 static const vector<AudioConfig> getRequiredSupportPlaybackAudioConfig() {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700348 return combineAudioConfig(
349 {AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
350 {8000, 11025, 16000, 22050, 32000, 44100},
351 {AudioFormat::PCM_16_BIT});
Kevin Rocardf0357882017-02-10 16:19:28 -0800352 }
353
Kevin Rocard72e50e22017-05-05 14:02:55 -0700354 static const vector<AudioConfig>
355 getRecommendedSupportPlaybackAudioConfig() {
356 return combineAudioConfig(
357 {AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO},
358 {24000, 48000}, {AudioFormat::PCM_16_BIT});
Kevin Rocardf0357882017-02-10 16:19:28 -0800359 }
360
361 static const vector<AudioConfig> getSupportedPlaybackAudioConfig() {
362 // TODO: retrieve audio config supported by the platform
363 // as declared in the policy configuration
364 return {};
365 }
366
367 static const vector<AudioConfig> getRequiredSupportCaptureAudioConfig() {
368 return combineAudioConfig({AudioChannelMask::IN_MONO},
369 {8000, 11025, 16000, 44100},
370 {AudioFormat::PCM_16_BIT});
371 }
372 static const vector<AudioConfig> getRecommendedSupportCaptureAudioConfig() {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700373 return combineAudioConfig({AudioChannelMask::IN_STEREO}, {22050, 48000},
Kevin Rocardf0357882017-02-10 16:19:28 -0800374 {AudioFormat::PCM_16_BIT});
375 }
376 static const vector<AudioConfig> getSupportedCaptureAudioConfig() {
377 // TODO: retrieve audio config supported by the platform
378 // as declared in the policy configuration
379 return {};
380 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700381
382 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800383 static const vector<AudioConfig> combineAudioConfig(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700384 vector<AudioChannelMask> channelMasks, vector<uint32_t> sampleRates,
385 vector<AudioFormat> formats) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800386 vector<AudioConfig> configs;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700387 for (auto channelMask : channelMasks) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800388 for (auto sampleRate : sampleRates) {
389 for (auto format : formats) {
390 AudioConfig config{};
391 // leave offloadInfo to 0
392 config.channelMask = channelMask;
393 config.sampleRateHz = sampleRate;
394 config.format = format;
395 // FIXME: leave frameCount to 0 ?
396 configs.push_back(config);
397 }
398 }
399 }
400 return configs;
401 }
402};
403
Kevin Rocard9c369142017-03-08 17:17:25 -0800404/** Generate a test name based on an audio config.
405 *
406 * As the only parameter changing are channel mask and sample rate,
407 * only print those ones in the test name.
408 */
Kevin Rocard72e50e22017-05-05 14:02:55 -0700409static string generateTestName(
410 const testing::TestParamInfo<AudioConfig>& info) {
Kevin Rocard9c369142017-03-08 17:17:25 -0800411 const AudioConfig& config = info.param;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700412 return to_string(info.index) + "__" + to_string(config.sampleRateHz) + "_" +
413 // "MONO" is more clear than "FRONT_LEFT"
414 ((config.channelMask == AudioChannelMask::OUT_MONO ||
415 config.channelMask == AudioChannelMask::IN_MONO)
416 ? "MONO"
417 : toString(config.channelMask));
Kevin Rocard9c369142017-03-08 17:17:25 -0800418}
419
Kevin Rocardf0357882017-02-10 16:19:28 -0800420//////////////////////////////////////////////////////////////////////////////
421///////////////////////////// getInputBufferSize /////////////////////////////
422//////////////////////////////////////////////////////////////////////////////
423
Kevin Rocard72e50e22017-05-05 14:02:55 -0700424// FIXME: execute input test only if platform declares
425// android.hardware.microphone
Kevin Rocardf0357882017-02-10 16:19:28 -0800426// how to get this value ? is it a property ???
427
Kevin Rocard72e50e22017-05-05 14:02:55 -0700428class AudioCaptureConfigPrimaryTest
429 : public AudioConfigPrimaryTest,
430 public ::testing::WithParamInterface<AudioConfig> {
431 protected:
432 void inputBufferSizeTest(const AudioConfig& audioConfig,
433 bool supportRequired) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800434 uint64_t bufferSize;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700435 ASSERT_OK(
436 device->getInputBufferSize(audioConfig, returnIn(res, bufferSize)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800437
438 switch (res) {
439 case Result::INVALID_ARGUMENTS:
440 EXPECT_FALSE(supportRequired);
441 break;
442 case Result::OK:
443 // Check that the buffer is of a sane size
444 // For now only that it is > 0
445 EXPECT_GT(bufferSize, uint64_t(0));
446 break;
447 default:
Kevin Rocard72e50e22017-05-05 14:02:55 -0700448 FAIL() << "Invalid return status: "
449 << ::testing::PrintToString(res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800450 }
451 }
452};
453
Kevin Rocard72e50e22017-05-05 14:02:55 -0700454// Test that the required capture config and those declared in the policy are
455// indeed supported
Kevin Rocardf0357882017-02-10 16:19:28 -0800456class RequiredInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
457TEST_P(RequiredInputBufferSizeTest, RequiredInputBufferSizeTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700458 doc::test(
459 "Input buffer size must be retrievable for a format with required "
460 "support.");
Kevin Rocardf0357882017-02-10 16:19:28 -0800461 inputBufferSizeTest(GetParam(), true);
462}
463INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700464 RequiredInputBufferSize, RequiredInputBufferSizeTest,
465 ::testing::ValuesIn(
466 AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
467 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800468INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700469 SupportedInputBufferSize, RequiredInputBufferSizeTest,
470 ::testing::ValuesIn(
471 AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
472 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800473
Kevin Rocard72e50e22017-05-05 14:02:55 -0700474// Test that the recommended capture config are supported or lead to a
475// INVALID_ARGUMENTS return
Kevin Rocardf0357882017-02-10 16:19:28 -0800476class OptionalInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {};
477TEST_P(OptionalInputBufferSizeTest, OptionalInputBufferSizeTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700478 doc::test(
479 "Input buffer size should be retrievable for a format with recommended "
480 "support.");
Kevin Rocardf0357882017-02-10 16:19:28 -0800481 inputBufferSizeTest(GetParam(), false);
482}
483INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700484 RecommendedCaptureAudioConfigSupport, OptionalInputBufferSizeTest,
485 ::testing::ValuesIn(
486 AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
487 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800488
489//////////////////////////////////////////////////////////////////////////////
490/////////////////////////////// setScreenState ///////////////////////////////
491//////////////////////////////////////////////////////////////////////////////
492
493TEST_F(AudioPrimaryHidlTest, setScreenState) {
494 doc::test("Check that the hal can receive the screen state");
495 for (bool turnedOn : {false, true, true, false, false}) {
496 auto ret = device->setScreenState(turnedOn);
497 ASSERT_TRUE(ret.isOk());
498 Result result = ret;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700499 ASSERT_TRUE(result == Result::OK || result == Result::NOT_SUPPORTED)
500 << toString(result);
Kevin Rocardf0357882017-02-10 16:19:28 -0800501 }
502}
503
504//////////////////////////////////////////////////////////////////////////////
505//////////////////////////// {get,set}Parameters /////////////////////////////
506//////////////////////////////////////////////////////////////////////////////
507
508TEST_F(AudioPrimaryHidlTest, getParameters) {
509 doc::test("Check that the hal can set and get parameters");
510 hidl_vec<hidl_string> keys;
511 hidl_vec<ParameterValue> values;
512 ASSERT_OK(device->getParameters(keys, returnIn(res, values)));
513 ASSERT_OK(device->setParameters(values));
514 values.resize(0);
515 ASSERT_OK(device->setParameters(values));
516}
517
518//////////////////////////////////////////////////////////////////////////////
519//////////////////////////////// debugDebug //////////////////////////////////
520//////////////////////////////////////////////////////////////////////////////
521
Kevin Rocardb9031242017-03-13 12:20:54 -0700522template <class DebugDump>
523static void testDebugDump(DebugDump debugDump) {
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700524 // Dump in a temporary file
525 // Note that SELinux must be deactivate for this test to work
Kevin Rocardf0357882017-02-10 16:19:28 -0800526 FILE* file = tmpfile();
527 ASSERT_NE(nullptr, file) << errno;
528
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700529 // Wrap the temporary file file descriptor in a native handle
Kevin Rocardf0357882017-02-10 16:19:28 -0800530 auto* nativeHandle = native_handle_create(1, 0);
531 ASSERT_NE(nullptr, nativeHandle);
532 nativeHandle->data[0] = fileno(file);
533
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700534 // Wrap this native handle in a hidl handle
Kevin Rocardf0357882017-02-10 16:19:28 -0800535 hidl_handle handle;
536 handle.setTo(nativeHandle, true /*take ownership*/);
537
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700538 ASSERT_OK(debugDump(handle));
539
540 // Check that at least one bit was written by the hal
Kevin Rocardee771e92017-03-08 17:17:25 -0800541 // TODO: debugDump does not return a Result.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700542 // This mean that the hal can not report that it not implementing the
543 // function.
Kevin Rocard72e50e22017-05-05 14:02:55 -0700544 rewind(file); // can not fail
Kevin Rocardf0357882017-02-10 16:19:28 -0800545 char buff;
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700546 if (fread(&buff, sizeof(buff), 1, file) != 1) {
547 doc::note("debugDump does not seem implemented");
548 }
Kevin Rocardf0357882017-02-10 16:19:28 -0800549 EXPECT_EQ(0, fclose(file)) << errno;
550}
551
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700552TEST_F(AudioPrimaryHidlTest, DebugDump) {
Kevin Rocardb9031242017-03-13 12:20:54 -0700553 doc::test("Check that the hal can dump its state without error");
Kevin Rocard72e50e22017-05-05 14:02:55 -0700554 testDebugDump(
555 [this](const auto& handle) { return device->debugDump(handle); });
Kevin Rocardb9031242017-03-13 12:20:54 -0700556}
557
Kevin Rocard5e5783d2017-05-02 18:41:46 -0700558TEST_F(AudioPrimaryHidlTest, DebugDumpInvalidArguments) {
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700559 doc::test("Check that the hal dump doesn't crash on invalid arguments");
560 ASSERT_OK(device->debugDump(hidl_handle()));
561}
562
Kevin Rocardf0357882017-02-10 16:19:28 -0800563//////////////////////////////////////////////////////////////////////////////
564////////////////////////// open{Output,Input}Stream //////////////////////////
565//////////////////////////////////////////////////////////////////////////////
566
567template <class Stream>
568class OpenStreamTest : public AudioConfigPrimaryTest,
569 public ::testing::WithParamInterface<AudioConfig> {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700570 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800571 template <class Open>
572 void testOpen(Open openStream, const AudioConfig& config) {
573 // FIXME: Open a stream without an IOHandle
574 // This is not required to be accepted by hal implementations
Kevin Rocard72e50e22017-05-05 14:02:55 -0700575 AudioIoHandle ioHandle =
576 (AudioIoHandle)AudioHandleConsts::AUDIO_IO_HANDLE_NONE;
Kevin Rocardf0357882017-02-10 16:19:28 -0800577 AudioConfig suggestedConfig{};
Kevin Rocard72e50e22017-05-05 14:02:55 -0700578 ASSERT_OK(openStream(ioHandle, config,
579 returnIn(res, stream, suggestedConfig)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800580
581 // TODO: only allow failure for RecommendedPlaybackAudioConfig
582 switch (res) {
583 case Result::OK:
584 ASSERT_TRUE(stream != nullptr);
585 audioConfig = config;
586 break;
587 case Result::INVALID_ARGUMENTS:
588 ASSERT_TRUE(stream == nullptr);
589 AudioConfig suggestedConfigRetry;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700590 // Could not open stream with config, try again with the
591 // suggested one
592 ASSERT_OK(
593 openStream(ioHandle, suggestedConfig,
594 returnIn(res, stream, suggestedConfigRetry)));
Kevin Rocardf0357882017-02-10 16:19:28 -0800595 // This time it must succeed
596 ASSERT_OK(res);
597 ASSERT_TRUE(stream == nullptr);
598 audioConfig = suggestedConfig;
599 break;
600 default:
Kevin Rocard72e50e22017-05-05 14:02:55 -0700601 FAIL() << "Invalid return status: "
602 << ::testing::PrintToString(res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800603 }
604 open = true;
605 }
606
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800607 Return<Result> closeStream() {
608 open = false;
609 return stream->close();
610 }
Kevin Rocard72e50e22017-05-05 14:02:55 -0700611
612 private:
Kevin Rocardf0357882017-02-10 16:19:28 -0800613 void TearDown() override {
614 if (open) {
615 ASSERT_OK(stream->close());
616 }
617 }
618
Kevin Rocard72e50e22017-05-05 14:02:55 -0700619 protected:
Kevin Rocardf0357882017-02-10 16:19:28 -0800620 AudioConfig audioConfig;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800621 DeviceAddress address = {};
Kevin Rocardf0357882017-02-10 16:19:28 -0800622 sp<Stream> stream;
623 bool open = false;
624};
625
626////////////////////////////// openOutputStream //////////////////////////////
627
628class OutputStreamTest : public OpenStreamTest<IStreamOut> {
629 virtual void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700630 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800631 address.device = AudioDevice::OUT_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800632 const AudioConfig& config = GetParam();
Kevin Rocard72e50e22017-05-05 14:02:55 -0700633 AudioOutputFlag flags =
634 AudioOutputFlag::NONE; // TODO: test all flag combination
635 testOpen(
636 [&](AudioIoHandle handle, AudioConfig config, auto cb) {
637 return device->openOutputStream(handle, address, config, flags,
638 cb);
639 },
640 config);
Kevin Rocardf0357882017-02-10 16:19:28 -0800641 }
642};
643TEST_P(OutputStreamTest, OpenOutputStreamTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700644 doc::test(
645 "Check that output streams can be open with the required and "
646 "recommended config");
Kevin Rocardf0357882017-02-10 16:19:28 -0800647 // Open done in SetUp
648}
649INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700650 RequiredOutputStreamConfigSupport, OutputStreamTest,
651 ::testing::ValuesIn(
652 AudioConfigPrimaryTest::getRequiredSupportPlaybackAudioConfig()),
653 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800654INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700655 SupportedOutputStreamConfig, OutputStreamTest,
656 ::testing::ValuesIn(
657 AudioConfigPrimaryTest::getSupportedPlaybackAudioConfig()),
658 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800659
660INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700661 RecommendedOutputStreamConfigSupport, OutputStreamTest,
662 ::testing::ValuesIn(
663 AudioConfigPrimaryTest::getRecommendedSupportPlaybackAudioConfig()),
664 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800665
666////////////////////////////// openInputStream //////////////////////////////
667
668class InputStreamTest : public OpenStreamTest<IStreamIn> {
Kevin Rocardf0357882017-02-10 16:19:28 -0800669 virtual void SetUp() override {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700670 ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800671 address.device = AudioDevice::IN_DEFAULT;
Kevin Rocardf0357882017-02-10 16:19:28 -0800672 const AudioConfig& config = GetParam();
Kevin Rocard72e50e22017-05-05 14:02:55 -0700673 AudioInputFlag flags =
674 AudioInputFlag::NONE; // TODO: test all flag combination
675 AudioSource source =
676 AudioSource::DEFAULT; // TODO: test all flag combination
677 testOpen(
678 [&](AudioIoHandle handle, AudioConfig config, auto cb) {
679 return device->openInputStream(handle, address, config, flags,
680 source, cb);
681 },
682 config);
Kevin Rocardf0357882017-02-10 16:19:28 -0800683 }
684};
685
686TEST_P(InputStreamTest, OpenInputStreamTest) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700687 doc::test(
688 "Check that input streams can be open with the required and "
689 "recommended config");
Kevin Rocardf0357882017-02-10 16:19:28 -0800690 // Open done in setup
691}
692INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700693 RequiredInputStreamConfigSupport, InputStreamTest,
694 ::testing::ValuesIn(
695 AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()),
696 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800697INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700698 SupportedInputStreamConfig, InputStreamTest,
699 ::testing::ValuesIn(
700 AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()),
701 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800702
703INSTANTIATE_TEST_CASE_P(
Kevin Rocard72e50e22017-05-05 14:02:55 -0700704 RecommendedInputStreamConfigSupport, InputStreamTest,
705 ::testing::ValuesIn(
706 AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()),
707 &generateTestName);
Kevin Rocardf0357882017-02-10 16:19:28 -0800708
709//////////////////////////////////////////////////////////////////////////////
710////////////////////////////// IStream getters ///////////////////////////////
711//////////////////////////////////////////////////////////////////////////////
712
713/** Unpack the provided result.
714 * If the result is not OK, register a failure and return an undefined value. */
715template <class R>
716static R extract(Return<R> ret) {
717 if (!ret.isOk()) {
718 ADD_FAILURE();
719 return R{};
720 }
721 return ret;
722}
723
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800724/* Could not find a way to write a test for two parametrized class fixure
725 * thus use this macro do duplicate tests for Input and Output stream */
726#define TEST_IO_STREAM(test_name, documentation, code) \
Kevin Rocard72e50e22017-05-05 14:02:55 -0700727 TEST_P(InputStreamTest, test_name) { \
728 doc::test(documentation); \
729 code; \
730 } \
731 TEST_P(OutputStreamTest, test_name) { \
732 doc::test(documentation); \
733 code; \
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800734 }
735
Kevin Rocard72e50e22017-05-05 14:02:55 -0700736TEST_IO_STREAM(
737 GetFrameCount,
738 "Check that the stream frame count == the one it was opened with",
739 ASSERT_EQ(audioConfig.frameCount, extract(stream->getFrameCount())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800740
Kevin Rocard72e50e22017-05-05 14:02:55 -0700741TEST_IO_STREAM(
742 GetSampleRate,
743 "Check that the stream sample rate == the one it was opened with",
744 ASSERT_EQ(audioConfig.sampleRateHz, extract(stream->getSampleRate())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800745
Kevin Rocard72e50e22017-05-05 14:02:55 -0700746TEST_IO_STREAM(
747 GetChannelMask,
748 "Check that the stream channel mask == the one it was opened with",
749 ASSERT_EQ(audioConfig.channelMask, extract(stream->getChannelMask())))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800750
Kevin Rocard72e50e22017-05-05 14:02:55 -0700751TEST_IO_STREAM(GetFormat,
752 "Check that the stream format == the one it was opened with",
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800753 ASSERT_EQ(audioConfig.format, extract(stream->getFormat())))
754
755// TODO: for now only check that the framesize is not incoherent
Kevin Rocard72e50e22017-05-05 14:02:55 -0700756TEST_IO_STREAM(GetFrameSize,
757 "Check that the stream frame size == the one it was opened with",
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800758 ASSERT_GT(extract(stream->getFrameSize()), 0U))
759
Kevin Rocard72e50e22017-05-05 14:02:55 -0700760TEST_IO_STREAM(GetBufferSize,
761 "Check that the stream buffer size== the one it was opened with",
762 ASSERT_GE(extract(stream->getBufferSize()),
763 extract(stream->getFrameSize())));
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800764
Kevin Rocardf0357882017-02-10 16:19:28 -0800765template <class Property, class CapabilityGetter, class Getter, class Setter>
Kevin Rocard72e50e22017-05-05 14:02:55 -0700766static void testCapabilityGetter(const string& name, IStream* stream,
767 Property currentValue,
768 CapabilityGetter capablityGetter,
769 Getter getter, Setter setter) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800770 hidl_vec<Property> capabilities;
771 ASSERT_OK((stream->*capablityGetter)(returnIn(capabilities)));
772 if (capabilities.size() == 0) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700773 // The default hal should probably return a NOT_SUPPORTED if the hal
774 // does not expose
775 // capability retrieval. For now it returns an empty list if not
776 // implemented
Kevin Rocardf0357882017-02-10 16:19:28 -0800777 doc::partialTest(name + " is not supported");
778 return;
779 };
Kevin Rocard72e50e22017-05-05 14:02:55 -0700780 // TODO: This code has never been tested on a hal that supports
781 // getSupportedSampleRates
Kevin Rocardf0357882017-02-10 16:19:28 -0800782 EXPECT_NE(std::find(capabilities.begin(), capabilities.end(), currentValue),
Kevin Rocard72e50e22017-05-05 14:02:55 -0700783 capabilities.end())
Kevin Rocardf0357882017-02-10 16:19:28 -0800784 << "current " << name << " is not in the list of the supported ones "
785 << toString(capabilities);
786
787 // Check that all declared supported values are indeed supported
788 for (auto capability : capabilities) {
789 ASSERT_OK((stream->*setter)(capability));
790 ASSERT_EQ(capability, extract((stream->*getter)()));
791 }
792}
793
Kevin Rocard72e50e22017-05-05 14:02:55 -0700794TEST_IO_STREAM(SupportedSampleRate,
795 "Check that the stream sample rate is declared as supported",
796 testCapabilityGetter("getSupportedSampleRate", stream.get(),
797 extract(stream->getSampleRate()),
798 &IStream::getSupportedSampleRates,
799 &IStream::getSampleRate,
800 &IStream::setSampleRate))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800801
Kevin Rocard72e50e22017-05-05 14:02:55 -0700802TEST_IO_STREAM(SupportedChannelMask,
803 "Check that the stream channel mask is declared as supported",
804 testCapabilityGetter("getSupportedChannelMask", stream.get(),
805 extract(stream->getChannelMask()),
806 &IStream::getSupportedChannelMasks,
807 &IStream::getChannelMask,
808 &IStream::setChannelMask))
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800809
Kevin Rocard72e50e22017-05-05 14:02:55 -0700810TEST_IO_STREAM(SupportedFormat,
811 "Check that the stream format is declared as supported",
812 testCapabilityGetter("getSupportedFormat", stream.get(),
813 extract(stream->getFormat()),
814 &IStream::getSupportedFormats,
Kevin Rocarda7df7fc2017-03-10 18:37:46 -0800815 &IStream::getFormat, &IStream::setFormat))
816
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800817static void testGetDevice(IStream* stream, AudioDevice expectedDevice) {
818 auto ret = stream->getDevice();
819 ASSERT_TRUE(ret.isOk());
820 AudioDevice device = ret;
821 ASSERT_EQ(expectedDevice, device);
822}
823
Kevin Rocard72e50e22017-05-05 14:02:55 -0700824TEST_IO_STREAM(GetDevice,
825 "Check that the stream device == the one it was opened with",
826 areAudioPatchesSupported()
827 ? doc::partialTest("Audio patches are supported")
828 : testGetDevice(stream.get(), address.device))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800829
830static void testSetDevice(IStream* stream, const DeviceAddress& address) {
831 DeviceAddress otherAddress = address;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700832 otherAddress.device = (address.device & AudioDevice::BIT_IN) == 0
833 ? AudioDevice::OUT_SPEAKER
834 : AudioDevice::IN_BUILTIN_MIC;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800835 EXPECT_OK(stream->setDevice(otherAddress));
836
Kevin Rocard72e50e22017-05-05 14:02:55 -0700837 ASSERT_OK(stream->setDevice(address)); // Go back to the original value
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800838}
839
Kevin Rocard72e50e22017-05-05 14:02:55 -0700840TEST_IO_STREAM(
841 SetDevice,
842 "Check that the stream can be rerouted to SPEAKER or BUILTIN_MIC",
843 areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported")
844 : testSetDevice(stream.get(), address))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800845
Kevin Rocard72e50e22017-05-05 14:02:55 -0700846static void testGetAudioProperties(IStream* stream,
847 AudioConfig expectedConfig) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800848 uint32_t sampleRateHz;
849 AudioChannelMask mask;
850 AudioFormat format;
851
852 stream->getAudioProperties(returnIn(sampleRateHz, mask, format));
853
Kevin Rocard72e50e22017-05-05 14:02:55 -0700854 // FIXME: the qcom hal it does not currently negotiate the sampleRate &
855 // channel mask
Kevin Rocardd1e98ae2017-03-08 17:17:25 -0800856 EXPECT_EQ(expectedConfig.sampleRateHz, sampleRateHz);
857 EXPECT_EQ(expectedConfig.channelMask, mask);
Kevin Rocardf0357882017-02-10 16:19:28 -0800858 EXPECT_EQ(expectedConfig.format, format);
859}
860
Kevin Rocard72e50e22017-05-05 14:02:55 -0700861TEST_IO_STREAM(
862 GetAudioProperties,
863 "Check that the stream audio properties == the ones it was opened with",
864 testGetAudioProperties(stream.get(), audioConfig))
Kevin Rocardf0357882017-02-10 16:19:28 -0800865
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800866static void testConnectedState(IStream* stream) {
867 DeviceAddress address = {};
868 using AD = AudioDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700869 for (auto device :
870 {AD::OUT_HDMI, AD::OUT_WIRED_HEADPHONE, AD::IN_USB_HEADSET}) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800871 address.device = device;
872
873 ASSERT_OK(stream->setConnectedState(address, true));
874 ASSERT_OK(stream->setConnectedState(address, false));
875 }
876}
877TEST_IO_STREAM(SetConnectedState,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700878 "Check that the stream can be notified of device connection and "
879 "deconnection",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800880 testConnectedState(stream.get()))
881
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700882static auto invalidArgsOrNotSupportedOrOK = {Result::INVALID_ARGUMENTS,
883 Result::NOT_SUPPORTED, Result::OK};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800884TEST_IO_STREAM(SetHwAvSync, "Try to set hardware sync to an invalid value",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700885 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700886 stream->setHwAvSync(666)))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800887
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700888TEST_IO_STREAM(GetHwAvSync, "Get hardware sync can not fail",
889 ASSERT_TRUE(device->getHwAvSync().isOk()))
890
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700891static void checkGetNoParameter(IStream* stream, hidl_vec<hidl_string> keys,
892 vector<Result> expectedResults) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800893 hidl_vec<ParameterValue> parameters;
894 Result res;
895 ASSERT_OK(stream->getParameters(keys, returnIn(res, parameters)));
896 ASSERT_RESULT(expectedResults, res);
897 if (res == Result::OK) {
898 ASSERT_EQ(0U, parameters.size());
899 }
900}
901
Kevin Rocard72e50e22017-05-05 14:02:55 -0700902/* Get/Set parameter is intended to be an opaque channel between vendors app and
903 * their HALs.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800904 * Thus can not be meaningfully tested.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800905 */
906TEST_IO_STREAM(getEmptySetParameter, "Retrieve the values of an empty set",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700907 checkGetNoParameter(stream.get(), {} /* keys */, {Result::OK}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800908
Kevin Rocard72e50e22017-05-05 14:02:55 -0700909TEST_IO_STREAM(getNonExistingParameter,
910 "Retrieve the values of an non existing parameter",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700911 checkGetNoParameter(stream.get(),
912 {"Non existing key"} /* keys */,
913 {Result::NOT_SUPPORTED}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800914
Kevin Rocard72e50e22017-05-05 14:02:55 -0700915TEST_IO_STREAM(setEmptySetParameter,
916 "Set the values of an empty set of parameters",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700917 ASSERT_RESULT(Result::OK, stream->setParameters({})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800918
Kevin Rocard72e50e22017-05-05 14:02:55 -0700919TEST_IO_STREAM(
920 setNonExistingParameter, "Set the values of an non existing parameter",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700921 // Unfortunately, the set_parameter legacy interface did not return any
922 // error code when a key is not supported.
923 // To allow implementation to just wrapped the legacy one, consider OK as a
924 // valid result for setting a non existing parameter.
925 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700926 stream->setParameters({{"non existing key", "0"}})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800927
Kevin Rocardb9031242017-03-13 12:20:54 -0700928TEST_IO_STREAM(DebugDump,
929 "Check that a stream can dump its state without error",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700930 testDebugDump([this](const auto& handle) {
931 return stream->debugDump(handle);
932 }))
Kevin Rocardb9031242017-03-13 12:20:54 -0700933
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700934TEST_IO_STREAM(DebugDumpInvalidArguments,
935 "Check that the stream dump doesn't crash on invalid arguments",
936 ASSERT_OK(stream->debugDump(hidl_handle())))
937
Kevin Rocardf0357882017-02-10 16:19:28 -0800938//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800939////////////////////////////// addRemoveEffect ///////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800940//////////////////////////////////////////////////////////////////////////////
941
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800942TEST_IO_STREAM(AddNonExistingEffect, "Adding a non existing effect should fail",
943 ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->addEffect(666)))
Kevin Rocard72e50e22017-05-05 14:02:55 -0700944TEST_IO_STREAM(RemoveNonExistingEffect,
945 "Removing a non existing effect should fail",
946 ASSERT_RESULT(Result::INVALID_ARGUMENTS,
947 stream->removeEffect(666)))
Kevin Rocardf0357882017-02-10 16:19:28 -0800948
Kevin Rocard72e50e22017-05-05 14:02:55 -0700949// TODO: positive tests
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800950
951//////////////////////////////////////////////////////////////////////////////
952/////////////////////////////// Control ////////////////////////////////
953//////////////////////////////////////////////////////////////////////////////
954
955TEST_IO_STREAM(standby, "Make sure the stream can be put in stanby",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700956 ASSERT_OK(stream->standby())) // can not fail
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800957
Kevin Rocard72e50e22017-05-05 14:02:55 -0700958static vector<Result> invalidStateOrNotSupported = {Result::INVALID_STATE,
959 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800960
Kevin Rocard72e50e22017-05-05 14:02:55 -0700961TEST_IO_STREAM(startNoMmap,
962 "Starting a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800963 ASSERT_RESULT(invalidStateOrNotSupported, stream->start()))
964
Kevin Rocard72e50e22017-05-05 14:02:55 -0700965TEST_IO_STREAM(stopNoMmap,
966 "Stopping a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800967 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
968
Kevin Rocard72e50e22017-05-05 14:02:55 -0700969TEST_IO_STREAM(getMmapPositionNoMmap,
970 "Get a stream Mmap position before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800971 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
972
Kevin Rocard72e50e22017-05-05 14:02:55 -0700973TEST_IO_STREAM(close, "Make sure a stream can be closed",
974 ASSERT_OK(closeStream()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800975TEST_IO_STREAM(closeTwice, "Make sure a stream can not be closed twice",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700976 ASSERT_OK(closeStream());
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800977 ASSERT_RESULT(Result::INVALID_STATE, closeStream()))
978
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700979static auto invalidArgsOrNotSupported = {Result::INVALID_ARGUMENTS,
980 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800981static void testCreateTooBigMmapBuffer(IStream* stream) {
982 MmapBufferInfo info;
983 Result res;
984 // Assume that int max is a value too big to be allocated
Kevin Rocard72e50e22017-05-05 14:02:55 -0700985 // This is true currently with a 32bit media server, but might not when it
986 // will run in 64 bit
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800987 auto minSizeFrames = std::numeric_limits<int32_t>::max();
988 ASSERT_OK(stream->createMmapBuffer(minSizeFrames, returnIn(res, info)));
989 ASSERT_RESULT(invalidArgsOrNotSupported, res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800990}
991
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800992TEST_IO_STREAM(CreateTooBigMmapBuffer, "Create mmap buffer too big should fail",
993 testCreateTooBigMmapBuffer(stream.get()))
994
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800995static void testGetMmapPositionOfNonMmapedStream(IStream* stream) {
996 Result res;
997 MmapPosition position;
998 ASSERT_OK(stream->getMmapPosition(returnIn(res, position)));
999 ASSERT_RESULT(invalidArgsOrNotSupported, res);
1000}
1001
Kevin Rocard72e50e22017-05-05 14:02:55 -07001002TEST_IO_STREAM(
1003 GetMmapPositionOfNonMmapedStream,
1004 "Retrieving the mmap position of a non mmaped stream should fail",
1005 testGetMmapPositionOfNonMmapedStream(stream.get()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -08001006
Kevin Rocardf0357882017-02-10 16:19:28 -08001007//////////////////////////////////////////////////////////////////////////////
Kevin Rocardc9963522017-03-10 18:47:37 -08001008///////////////////////////////// StreamIn ///////////////////////////////////
1009//////////////////////////////////////////////////////////////////////////////
1010
1011TEST_P(InputStreamTest, GetAudioSource) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001012 doc::test(
1013 "Retrieving the audio source of an input stream should always succeed");
Kevin Rocardc9963522017-03-10 18:47:37 -08001014 AudioSource source;
1015 ASSERT_OK(stream->getAudioSource(returnIn(res, source)));
1016 ASSERT_OK(res);
1017 ASSERT_EQ(AudioSource::DEFAULT, source);
1018}
1019
Kevin Rocard72e50e22017-05-05 14:02:55 -07001020static void testUnitaryGain(std::function<Return<Result>(float)> setGain) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001021 for (float value : {0.0, 0.01, 0.5, 0.09, 1.0}) {
1022 SCOPED_TRACE("value=" + to_string(value));
1023 ASSERT_OK(setGain(value));
1024 }
Kevin Rocard72e50e22017-05-05 14:02:55 -07001025 for (float value : (float[]){-INFINITY, -1.0, -0.0,
1026 1.0 + std::numeric_limits<float>::epsilon(),
1027 2.0, INFINITY, NAN}) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001028 SCOPED_TRACE("value=" + to_string(value));
1029 // FIXME: NAN should never be accepted
Kevin Rocard72e50e22017-05-05 14:02:55 -07001030 // FIXME: Missing api doc. What should the impl do if the volume is
1031 // outside [0,1] ?
Kevin Rocardc9963522017-03-10 18:47:37 -08001032 ASSERT_RESULT(Result::INVALID_ARGUMENTS, setGain(value));
1033 }
1034}
1035
1036TEST_P(InputStreamTest, SetGain) {
1037 doc::test("The gain of an input stream should only be set between [0,1]");
1038 testUnitaryGain([this](float volume) { return stream->setGain(volume); });
1039}
1040
Kevin Rocard72e50e22017-05-05 14:02:55 -07001041static void testPrepareForReading(IStreamIn* stream, uint32_t frameSize,
1042 uint32_t framesCount) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001043 Result res;
1044 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001045 ASSERT_OK(stream->prepareForReading(
1046 frameSize, framesCount,
1047 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocardc9963522017-03-10 18:47:37 -08001048 EXPECT_RESULT(invalidArgsOrNotSupported, res);
1049}
1050
Kevin Rocard195205b2017-05-02 18:34:59 -07001051TEST_P(InputStreamTest, PrepareForReadingWithZeroBuffer) {
1052 doc::test(
1053 "Preparing a stream for reading with a 0 sized buffer should fail");
1054 testPrepareForReading(stream.get(), 0, 0);
1055}
1056
Kevin Rocardc9963522017-03-10 18:47:37 -08001057TEST_P(InputStreamTest, PrepareForReadingWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001058 doc::test(
1059 "Preparing a stream for reading with a 2^32 sized buffer should fail");
1060 testPrepareForReading(stream.get(), 1,
1061 std::numeric_limits<uint32_t>::max());
Kevin Rocardc9963522017-03-10 18:47:37 -08001062}
1063
1064TEST_P(InputStreamTest, PrepareForReadingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001065 doc::test(
1066 "Preparing a stream for reading with a overflowing sized buffer should "
1067 "fail");
Kevin Rocardc9963522017-03-10 18:47:37 -08001068 auto uintMax = std::numeric_limits<uint32_t>::max();
1069 testPrepareForReading(stream.get(), uintMax, uintMax);
1070}
1071
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001072TEST_P(InputStreamTest, GetInputFramesLost) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001073 doc::test(
1074 "The number of frames lost on a never started stream should be 0");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001075 auto ret = stream->getInputFramesLost();
1076 ASSERT_TRUE(ret.isOk());
1077 uint32_t framesLost{ret};
1078 ASSERT_EQ(0U, framesLost);
1079}
1080
Kevin Rocardc9963522017-03-10 18:47:37 -08001081TEST_P(InputStreamTest, getCapturePosition) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001082 doc::test(
1083 "The capture position of a non prepared stream should not be "
1084 "retrievable");
Kevin Rocardc9963522017-03-10 18:47:37 -08001085 uint64_t frames;
1086 uint64_t time;
1087 ASSERT_OK(stream->getCapturePosition(returnIn(res, frames, time)));
1088 ASSERT_RESULT(invalidStateOrNotSupported, res);
1089}
1090
1091//////////////////////////////////////////////////////////////////////////////
Kevin Rocard624800c2017-03-10 18:47:37 -08001092///////////////////////////////// StreamIn ///////////////////////////////////
1093//////////////////////////////////////////////////////////////////////////////
1094
1095TEST_P(OutputStreamTest, getLatency) {
1096 doc::test("Make sure latency is over 0");
1097 auto result = stream->getLatency();
1098 ASSERT_TRUE(result.isOk());
1099 ASSERT_GT(result, 0U);
1100}
1101
1102TEST_P(OutputStreamTest, setVolume) {
1103 doc::test("Try to set the output volume");
1104 auto result = stream->setVolume(1, 1);
1105 ASSERT_TRUE(result.isOk());
1106 if (result == Result::NOT_SUPPORTED) {
1107 doc::partialTest("setVolume is not supported");
1108 return;
1109 }
Kevin Rocard72e50e22017-05-05 14:02:55 -07001110 testUnitaryGain(
1111 [this](float volume) { return stream->setVolume(volume, volume); });
Kevin Rocard624800c2017-03-10 18:47:37 -08001112}
1113
Kevin Rocard72e50e22017-05-05 14:02:55 -07001114static void testPrepareForWriting(IStreamOut* stream, uint32_t frameSize,
1115 uint32_t framesCount) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001116 Result res;
1117 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001118 ASSERT_OK(stream->prepareForWriting(
1119 frameSize, framesCount,
1120 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocard624800c2017-03-10 18:47:37 -08001121 EXPECT_RESULT(invalidArgsOrNotSupported, res);
1122}
1123
Kevin Rocard195205b2017-05-02 18:34:59 -07001124TEST_P(OutputStreamTest, PrepareForWriteWithZeroBuffer) {
1125 doc::test(
1126 "Preparing a stream for writing with a 0 sized buffer should fail");
1127 testPrepareForWriting(stream.get(), 0, 0);
1128}
1129
Kevin Rocard624800c2017-03-10 18:47:37 -08001130TEST_P(OutputStreamTest, PrepareForWriteWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001131 doc::test(
1132 "Preparing a stream for writing with a 2^32 sized buffer should fail");
1133 testPrepareForWriting(stream.get(), 1,
1134 std::numeric_limits<uint32_t>::max());
Kevin Rocard624800c2017-03-10 18:47:37 -08001135}
1136
1137TEST_P(OutputStreamTest, PrepareForWritingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001138 doc::test(
1139 "Preparing a stream for writing with a overflowing sized buffer should "
1140 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001141 auto uintMax = std::numeric_limits<uint32_t>::max();
1142 testPrepareForWriting(stream.get(), uintMax, uintMax);
1143}
1144
1145struct Capability {
1146 Capability(IStreamOut* stream) {
1147 EXPECT_OK(stream->supportsPauseAndResume(returnIn(pause, resume)));
1148 auto ret = stream->supportsDrain();
1149 EXPECT_TRUE(ret.isOk());
1150 if (ret.isOk()) {
1151 drain = ret;
1152 }
1153 }
1154 bool pause = false;
1155 bool resume = false;
1156 bool drain = false;
1157};
1158
1159TEST_P(OutputStreamTest, SupportsPauseAndResumeAndDrain) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001160 doc::test(
1161 "Implementation must expose pause, resume and drain capabilities");
Kevin Rocard624800c2017-03-10 18:47:37 -08001162 Capability(stream.get());
1163}
1164
1165TEST_P(OutputStreamTest, GetRenderPosition) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001166 doc::test("The render position should be 0 on a not started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001167 uint32_t dspFrames;
1168 ASSERT_OK(stream->getRenderPosition(returnIn(res, dspFrames)));
1169 if (res == Result::NOT_SUPPORTED) {
1170 doc::partialTest("getRenderPosition is not supported");
1171 return;
1172 }
1173 ASSERT_OK(res);
1174 ASSERT_EQ(0U, dspFrames);
1175}
1176
1177TEST_P(OutputStreamTest, GetNextWriteTimestamp) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001178 doc::test("The render position of a stream just created should be 0");
Kevin Rocard624800c2017-03-10 18:47:37 -08001179 uint64_t timestampUs;
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001180 ASSERT_OK(stream->getNextWriteTimestamp(returnIn(res, timestampUs)));
Kevin Rocard624800c2017-03-10 18:47:37 -08001181 if (res == Result::NOT_SUPPORTED) {
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001182 doc::partialTest("getNextWriteTimestamp is not supported");
Kevin Rocard624800c2017-03-10 18:47:37 -08001183 return;
1184 }
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001185 ASSERT_EQ(Result::INVALID_STATE, res);
Kevin Rocard624800c2017-03-10 18:47:37 -08001186}
1187
1188/** Stub implementation of out stream callback. */
1189class MockOutCallbacks : public IStreamOutCallback {
1190 Return<void> onWriteReady() override { return {}; }
1191 Return<void> onDrainReady() override { return {}; }
1192 Return<void> onError() override { return {}; }
1193};
1194
Kevin Rocard72e50e22017-05-05 14:02:55 -07001195static bool isAsyncModeSupported(IStreamOut* stream) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001196 auto res = stream->setCallback(new MockOutCallbacks);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001197 stream->clearCallback(); // try to restore the no callback state, ignore
1198 // any error
1199 auto okOrNotSupported = {Result::OK, Result::NOT_SUPPORTED};
Kevin Rocard624800c2017-03-10 18:47:37 -08001200 EXPECT_RESULT(okOrNotSupported, res);
1201 return res.isOk() ? res == Result::OK : false;
1202}
1203
1204TEST_P(OutputStreamTest, SetCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001205 doc::test(
1206 "If supported, registering callback for async operation should never "
1207 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001208 if (!isAsyncModeSupported(stream.get())) {
1209 doc::partialTest("The stream does not support async operations");
1210 return;
1211 }
1212 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1213 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1214}
1215
1216TEST_P(OutputStreamTest, clearCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001217 doc::test(
1218 "If supported, clearing a callback to go back to sync operation should "
1219 "not fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001220 if (!isAsyncModeSupported(stream.get())) {
1221 doc::partialTest("The stream does not support async operations");
1222 return;
1223 }
1224 // TODO: Clarify if clearing a non existing callback should fail
1225 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1226 ASSERT_OK(stream->clearCallback());
1227}
1228
1229TEST_P(OutputStreamTest, Resume) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001230 doc::test(
1231 "If supported, a stream should fail to resume if not previously "
1232 "paused");
Kevin Rocard624800c2017-03-10 18:47:37 -08001233 if (!Capability(stream.get()).resume) {
1234 doc::partialTest("The output stream does not support resume");
1235 return;
1236 }
1237 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1238}
1239
1240TEST_P(OutputStreamTest, Pause) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001241 doc::test(
1242 "If supported, a stream should fail to pause if not previously "
1243 "started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001244 if (!Capability(stream.get()).pause) {
1245 doc::partialTest("The output stream does not support pause");
1246 return;
1247 }
1248 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1249}
1250
Kevin Rocard72e50e22017-05-05 14:02:55 -07001251static void testDrain(IStreamOut* stream, AudioDrain type) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001252 if (!Capability(stream).drain) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001253 doc::partialTest("The output stream does not support drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001254 return;
1255 }
1256 ASSERT_RESULT(Result::OK, stream->drain(type));
1257}
1258
1259TEST_P(OutputStreamTest, DrainAll) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001260 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001261 testDrain(stream.get(), AudioDrain::ALL);
1262}
1263
1264TEST_P(OutputStreamTest, DrainEarlyNotify) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001265 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001266 testDrain(stream.get(), AudioDrain::EARLY_NOTIFY);
1267}
1268
1269TEST_P(OutputStreamTest, FlushStop) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001270 doc::test("If supported, a stream should always succeed to flush");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001271 auto ret = stream->flush();
1272 ASSERT_TRUE(ret.isOk());
1273 if (ret == Result::NOT_SUPPORTED) {
1274 doc::partialTest("Flush is not supported");
1275 return;
1276 }
1277 ASSERT_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001278}
1279
1280TEST_P(OutputStreamTest, GetPresentationPositionStop) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001281 doc::test(
1282 "If supported, a stream should always succeed to retrieve the "
1283 "presentation position");
Kevin Rocard624800c2017-03-10 18:47:37 -08001284 uint64_t frames;
1285 TimeSpec mesureTS;
1286 ASSERT_OK(stream->getPresentationPosition(returnIn(res, frames, mesureTS)));
1287 if (res == Result::NOT_SUPPORTED) {
1288 doc::partialTest("getpresentationPosition is not supported");
1289 return;
1290 }
1291 ASSERT_EQ(0U, frames);
1292
1293 struct timespec currentTS;
1294 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currentTS)) << errno;
1295
Kevin Rocard72e50e22017-05-05 14:02:55 -07001296 auto toMicroSec = [](uint64_t sec, auto nsec) {
1297 return sec * 1e+6 + nsec / 1e+3;
1298 };
Kevin Rocard624800c2017-03-10 18:47:37 -08001299 auto currentTime = toMicroSec(currentTS.tv_sec, currentTS.tv_nsec);
1300 auto mesureTime = toMicroSec(mesureTS.tvSec, mesureTS.tvNSec);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001301 ASSERT_PRED2([](auto c, auto m) { return c - m < 1e+6; }, currentTime,
1302 mesureTime);
Kevin Rocard624800c2017-03-10 18:47:37 -08001303}
1304
Kevin Rocard624800c2017-03-10 18:47:37 -08001305//////////////////////////////////////////////////////////////////////////////
Kevin Rocard3c405a72017-03-08 16:46:51 -08001306/////////////////////////////// PrimaryDevice ////////////////////////////////
1307//////////////////////////////////////////////////////////////////////////////
1308
1309TEST_F(AudioPrimaryHidlTest, setVoiceVolume) {
1310 doc::test("Make sure setVoiceVolume only succeed if volume is in [0,1]");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001311 testUnitaryGain(
1312 [this](float volume) { return device->setVoiceVolume(volume); });
Kevin Rocard3c405a72017-03-08 16:46:51 -08001313}
1314
1315TEST_F(AudioPrimaryHidlTest, setMode) {
Kevin Rocard04364ed2017-05-02 18:16:00 -07001316 doc::test(
1317 "Make sure setMode always succeeds if mode is valid "
1318 "and fails otherwise");
1319 // Test Invalid values
1320 for (AudioMode mode :
1321 {AudioMode::INVALID, AudioMode::CURRENT, AudioMode::CNT}) {
1322 SCOPED_TRACE("mode=" + toString(mode));
1323 ASSERT_RESULT(Result::INVALID_ARGUMENTS, device->setMode(mode));
1324 }
1325 // Test valid values
Kevin Rocard72e50e22017-05-05 14:02:55 -07001326 for (AudioMode mode :
1327 {AudioMode::IN_CALL, AudioMode::IN_COMMUNICATION, AudioMode::RINGTONE,
Kevin Rocard72e50e22017-05-05 14:02:55 -07001328 AudioMode::NORMAL /* Make sure to leave the test in normal mode */}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -08001329 SCOPED_TRACE("mode=" + toString(mode));
1330 ASSERT_OK(device->setMode(mode));
1331 }
Kevin Rocard3c405a72017-03-08 16:46:51 -08001332}
1333
Kevin Rocard3c405a72017-03-08 16:46:51 -08001334TEST_F(BoolAccessorPrimaryHidlTest, BtScoNrecEnabled) {
1335 doc::test("Query and set the BT SCO NR&EC state");
1336 testOptionalAccessors("BtScoNrecEnabled", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001337 &IPrimaryDevice::setBtScoNrecEnabled,
1338 &IPrimaryDevice::getBtScoNrecEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001339}
1340
1341TEST_F(BoolAccessorPrimaryHidlTest, setGetBtScoWidebandEnabled) {
1342 doc::test("Query and set the SCO whideband state");
1343 testOptionalAccessors("BtScoWideband", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001344 &IPrimaryDevice::setBtScoWidebandEnabled,
1345 &IPrimaryDevice::getBtScoWidebandEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001346}
1347
1348using TtyModeAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<TtyMode>;
1349TEST_F(TtyModeAccessorPrimaryHidlTest, setGetTtyMode) {
1350 doc::test("Query and set the TTY mode state");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001351 testOptionalAccessors(
1352 "TTY mode", {TtyMode::OFF, TtyMode::HCO, TtyMode::VCO, TtyMode::FULL},
1353 &IPrimaryDevice::setTtyMode, &IPrimaryDevice::getTtyMode);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001354}
1355
1356TEST_F(BoolAccessorPrimaryHidlTest, setGetHac) {
1357 doc::test("Query and set the HAC state");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001358 testAccessors("HAC", {true, false, true}, &IPrimaryDevice::setHacEnabled,
1359 &IPrimaryDevice::getHacEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001360}
1361
1362//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -08001363//////////////////// Clean caches on global tear down ////////////////////////
1364//////////////////////////////////////////////////////////////////////////////
1365
1366int main(int argc, char** argv) {
1367 environment = new Environment;
1368 ::testing::AddGlobalTestEnvironment(environment);
1369 ::testing::InitGoogleTest(&argc, argv);
1370 int status = RUN_ALL_TESTS();
1371 LOG(INFO) << "Test result = " << status;
1372 return status;
1373}