blob: f90ff05e3ce7a9ef214a8b7abe6ed9cbd2e73a58 [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);
Kevin Rocard4aefd1c2017-05-02 18:58:58 -0700597 ASSERT_TRUE(stream != nullptr);
Kevin Rocardf0357882017-02-10 16:19:28 -0800598 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) {
Kevin Rocard8f8730c2017-05-02 19:34:29 -0700818 // Unfortunately the interface does not allow the implementation to return
819 // NOT_SUPPORTED
820 // Thus allow NONE as signaling that the call is not supported.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800821 auto ret = stream->getDevice();
822 ASSERT_TRUE(ret.isOk());
823 AudioDevice device = ret;
Kevin Rocard8f8730c2017-05-02 19:34:29 -0700824 ASSERT_TRUE(device == expectedDevice || device == AudioDevice::NONE)
825 << "Expected: " << ::testing::PrintToString(expectedDevice)
826 << "\n Actual: " << ::testing::PrintToString(device);
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800827}
828
Kevin Rocard72e50e22017-05-05 14:02:55 -0700829TEST_IO_STREAM(GetDevice,
830 "Check that the stream device == the one it was opened with",
831 areAudioPatchesSupported()
832 ? doc::partialTest("Audio patches are supported")
833 : testGetDevice(stream.get(), address.device))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800834
835static void testSetDevice(IStream* stream, const DeviceAddress& address) {
836 DeviceAddress otherAddress = address;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700837 otherAddress.device = (address.device & AudioDevice::BIT_IN) == 0
838 ? AudioDevice::OUT_SPEAKER
839 : AudioDevice::IN_BUILTIN_MIC;
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800840 EXPECT_OK(stream->setDevice(otherAddress));
841
Kevin Rocard72e50e22017-05-05 14:02:55 -0700842 ASSERT_OK(stream->setDevice(address)); // Go back to the original value
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800843}
844
Kevin Rocard72e50e22017-05-05 14:02:55 -0700845TEST_IO_STREAM(
846 SetDevice,
847 "Check that the stream can be rerouted to SPEAKER or BUILTIN_MIC",
848 areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported")
849 : testSetDevice(stream.get(), address))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800850
Kevin Rocard72e50e22017-05-05 14:02:55 -0700851static void testGetAudioProperties(IStream* stream,
852 AudioConfig expectedConfig) {
Kevin Rocardf0357882017-02-10 16:19:28 -0800853 uint32_t sampleRateHz;
854 AudioChannelMask mask;
855 AudioFormat format;
856
857 stream->getAudioProperties(returnIn(sampleRateHz, mask, format));
858
Kevin Rocard72e50e22017-05-05 14:02:55 -0700859 // FIXME: the qcom hal it does not currently negotiate the sampleRate &
860 // channel mask
Kevin Rocardd1e98ae2017-03-08 17:17:25 -0800861 EXPECT_EQ(expectedConfig.sampleRateHz, sampleRateHz);
862 EXPECT_EQ(expectedConfig.channelMask, mask);
Kevin Rocardf0357882017-02-10 16:19:28 -0800863 EXPECT_EQ(expectedConfig.format, format);
864}
865
Kevin Rocard72e50e22017-05-05 14:02:55 -0700866TEST_IO_STREAM(
867 GetAudioProperties,
868 "Check that the stream audio properties == the ones it was opened with",
869 testGetAudioProperties(stream.get(), audioConfig))
Kevin Rocardf0357882017-02-10 16:19:28 -0800870
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800871static void testConnectedState(IStream* stream) {
872 DeviceAddress address = {};
873 using AD = AudioDevice;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700874 for (auto device :
875 {AD::OUT_HDMI, AD::OUT_WIRED_HEADPHONE, AD::IN_USB_HEADSET}) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800876 address.device = device;
877
878 ASSERT_OK(stream->setConnectedState(address, true));
879 ASSERT_OK(stream->setConnectedState(address, false));
880 }
881}
882TEST_IO_STREAM(SetConnectedState,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700883 "Check that the stream can be notified of device connection and "
884 "deconnection",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800885 testConnectedState(stream.get()))
886
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700887static auto invalidArgsOrNotSupportedOrOK = {Result::INVALID_ARGUMENTS,
888 Result::NOT_SUPPORTED, Result::OK};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800889TEST_IO_STREAM(SetHwAvSync, "Try to set hardware sync to an invalid value",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700890 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700891 stream->setHwAvSync(666)))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800892
Kevin Rocarde9a8fb72017-03-21 11:39:55 -0700893TEST_IO_STREAM(GetHwAvSync, "Get hardware sync can not fail",
894 ASSERT_TRUE(device->getHwAvSync().isOk()))
895
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700896static void checkGetNoParameter(IStream* stream, hidl_vec<hidl_string> keys,
897 vector<Result> expectedResults) {
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800898 hidl_vec<ParameterValue> parameters;
899 Result res;
900 ASSERT_OK(stream->getParameters(keys, returnIn(res, parameters)));
901 ASSERT_RESULT(expectedResults, res);
902 if (res == Result::OK) {
Kevin Rocard4aefd1c2017-05-02 18:58:58 -0700903 for (auto& parameter : parameters) {
904 ASSERT_EQ(0U, parameter.value.size()) << toString(parameter);
905 }
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800906 }
907}
908
Kevin Rocard72e50e22017-05-05 14:02:55 -0700909/* Get/Set parameter is intended to be an opaque channel between vendors app and
910 * their HALs.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800911 * Thus can not be meaningfully tested.
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800912 */
913TEST_IO_STREAM(getEmptySetParameter, "Retrieve the values of an empty set",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700914 checkGetNoParameter(stream.get(), {} /* keys */, {Result::OK}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800915
Kevin Rocard72e50e22017-05-05 14:02:55 -0700916TEST_IO_STREAM(getNonExistingParameter,
917 "Retrieve the values of an non existing parameter",
Kevin Rocardfa3b4a92017-05-02 17:38:34 -0700918 checkGetNoParameter(stream.get(),
919 {"Non existing key"} /* keys */,
920 {Result::NOT_SUPPORTED}))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800921
Kevin Rocard72e50e22017-05-05 14:02:55 -0700922TEST_IO_STREAM(setEmptySetParameter,
923 "Set the values of an empty set of parameters",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700924 ASSERT_RESULT(Result::OK, stream->setParameters({})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800925
Kevin Rocard72e50e22017-05-05 14:02:55 -0700926TEST_IO_STREAM(
927 setNonExistingParameter, "Set the values of an non existing parameter",
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700928 // Unfortunately, the set_parameter legacy interface did not return any
929 // error code when a key is not supported.
930 // To allow implementation to just wrapped the legacy one, consider OK as a
931 // valid result for setting a non existing parameter.
932 ASSERT_RESULT(invalidArgsOrNotSupportedOrOK,
Kevin Rocard72e50e22017-05-05 14:02:55 -0700933 stream->setParameters({{"non existing key", "0"}})))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800934
Kevin Rocardb9031242017-03-13 12:20:54 -0700935TEST_IO_STREAM(DebugDump,
936 "Check that a stream can dump its state without error",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700937 testDebugDump([this](const auto& handle) {
938 return stream->debugDump(handle);
939 }))
Kevin Rocardb9031242017-03-13 12:20:54 -0700940
Mikhail Naganov3e6fe752017-04-24 10:44:08 -0700941TEST_IO_STREAM(DebugDumpInvalidArguments,
942 "Check that the stream dump doesn't crash on invalid arguments",
943 ASSERT_OK(stream->debugDump(hidl_handle())))
944
Kevin Rocardf0357882017-02-10 16:19:28 -0800945//////////////////////////////////////////////////////////////////////////////
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800946////////////////////////////// addRemoveEffect ///////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -0800947//////////////////////////////////////////////////////////////////////////////
948
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800949TEST_IO_STREAM(AddNonExistingEffect, "Adding a non existing effect should fail",
950 ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->addEffect(666)))
Kevin Rocard72e50e22017-05-05 14:02:55 -0700951TEST_IO_STREAM(RemoveNonExistingEffect,
952 "Removing a non existing effect should fail",
953 ASSERT_RESULT(Result::INVALID_ARGUMENTS,
954 stream->removeEffect(666)))
Kevin Rocardf0357882017-02-10 16:19:28 -0800955
Kevin Rocard72e50e22017-05-05 14:02:55 -0700956// TODO: positive tests
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800957
958//////////////////////////////////////////////////////////////////////////////
959/////////////////////////////// Control ////////////////////////////////
960//////////////////////////////////////////////////////////////////////////////
961
962TEST_IO_STREAM(standby, "Make sure the stream can be put in stanby",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700963 ASSERT_OK(stream->standby())) // can not fail
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800964
Kevin Rocard72e50e22017-05-05 14:02:55 -0700965static vector<Result> invalidStateOrNotSupported = {Result::INVALID_STATE,
966 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800967
Kevin Rocard72e50e22017-05-05 14:02:55 -0700968TEST_IO_STREAM(startNoMmap,
969 "Starting a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800970 ASSERT_RESULT(invalidStateOrNotSupported, stream->start()))
971
Kevin Rocard72e50e22017-05-05 14:02:55 -0700972TEST_IO_STREAM(stopNoMmap,
973 "Stopping a mmaped stream before mapping it should fail",
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800974 ASSERT_RESULT(invalidStateOrNotSupported, stream->stop()))
975
Kevin Rocard72e50e22017-05-05 14:02:55 -0700976TEST_IO_STREAM(getMmapPositionNoMmap,
977 "Get a stream Mmap position 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(close, "Make sure a stream can be closed",
981 ASSERT_OK(closeStream()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800982TEST_IO_STREAM(closeTwice, "Make sure a stream can not be closed twice",
Kevin Rocard72e50e22017-05-05 14:02:55 -0700983 ASSERT_OK(closeStream());
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800984 ASSERT_RESULT(Result::INVALID_STATE, closeStream()))
985
Kevin Rocardf8500dc2017-05-03 10:43:21 -0700986static auto invalidArgsOrNotSupported = {Result::INVALID_ARGUMENTS,
987 Result::NOT_SUPPORTED};
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800988static void testCreateTooBigMmapBuffer(IStream* stream) {
989 MmapBufferInfo info;
990 Result res;
991 // Assume that int max is a value too big to be allocated
Kevin Rocard72e50e22017-05-05 14:02:55 -0700992 // This is true currently with a 32bit media server, but might not when it
993 // will run in 64 bit
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800994 auto minSizeFrames = std::numeric_limits<int32_t>::max();
995 ASSERT_OK(stream->createMmapBuffer(minSizeFrames, returnIn(res, info)));
996 ASSERT_RESULT(invalidArgsOrNotSupported, res);
Kevin Rocardf0357882017-02-10 16:19:28 -0800997}
998
Kevin Rocard8878b4b2017-03-10 18:47:37 -0800999TEST_IO_STREAM(CreateTooBigMmapBuffer, "Create mmap buffer too big should fail",
1000 testCreateTooBigMmapBuffer(stream.get()))
1001
Kevin Rocard8878b4b2017-03-10 18:47:37 -08001002static void testGetMmapPositionOfNonMmapedStream(IStream* stream) {
1003 Result res;
1004 MmapPosition position;
1005 ASSERT_OK(stream->getMmapPosition(returnIn(res, position)));
1006 ASSERT_RESULT(invalidArgsOrNotSupported, res);
1007}
1008
Kevin Rocard72e50e22017-05-05 14:02:55 -07001009TEST_IO_STREAM(
1010 GetMmapPositionOfNonMmapedStream,
1011 "Retrieving the mmap position of a non mmaped stream should fail",
1012 testGetMmapPositionOfNonMmapedStream(stream.get()))
Kevin Rocard8878b4b2017-03-10 18:47:37 -08001013
Kevin Rocardf0357882017-02-10 16:19:28 -08001014//////////////////////////////////////////////////////////////////////////////
Kevin Rocardc9963522017-03-10 18:47:37 -08001015///////////////////////////////// StreamIn ///////////////////////////////////
1016//////////////////////////////////////////////////////////////////////////////
1017
1018TEST_P(InputStreamTest, GetAudioSource) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001019 doc::test(
1020 "Retrieving the audio source of an input stream should always succeed");
Kevin Rocardc9963522017-03-10 18:47:37 -08001021 AudioSource source;
1022 ASSERT_OK(stream->getAudioSource(returnIn(res, source)));
Kevin Rocard98390a62017-05-03 11:16:05 -07001023 if (res == Result::NOT_SUPPORTED) {
1024 doc::partialTest("getAudioSource is not supported");
1025 return;
1026 }
Kevin Rocardc9963522017-03-10 18:47:37 -08001027 ASSERT_OK(res);
1028 ASSERT_EQ(AudioSource::DEFAULT, source);
1029}
1030
Kevin Rocard72e50e22017-05-05 14:02:55 -07001031static void testUnitaryGain(std::function<Return<Result>(float)> setGain) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001032 for (float value : {0.0, 0.01, 0.5, 0.09, 1.0}) {
1033 SCOPED_TRACE("value=" + to_string(value));
1034 ASSERT_OK(setGain(value));
1035 }
Kevin Rocard72e50e22017-05-05 14:02:55 -07001036 for (float value : (float[]){-INFINITY, -1.0, -0.0,
1037 1.0 + std::numeric_limits<float>::epsilon(),
1038 2.0, INFINITY, NAN}) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001039 SCOPED_TRACE("value=" + to_string(value));
1040 // FIXME: NAN should never be accepted
Kevin Rocard72e50e22017-05-05 14:02:55 -07001041 // FIXME: Missing api doc. What should the impl do if the volume is
1042 // outside [0,1] ?
Kevin Rocardc9963522017-03-10 18:47:37 -08001043 ASSERT_RESULT(Result::INVALID_ARGUMENTS, setGain(value));
1044 }
1045}
1046
Kevin Rocard98390a62017-05-03 11:16:05 -07001047static void testOptionalUnitaryGain(
1048 std::function<Return<Result>(float)> setGain, string debugName) {
1049 auto result = setGain(1);
1050 ASSERT_TRUE(result.isOk());
1051 if (result == Result::NOT_SUPPORTED) {
1052 doc::partialTest(debugName + " is not supported");
1053 return;
1054 }
1055 testUnitaryGain(setGain);
1056}
1057
Kevin Rocardc9963522017-03-10 18:47:37 -08001058TEST_P(InputStreamTest, SetGain) {
1059 doc::test("The gain of an input stream should only be set between [0,1]");
Kevin Rocard98390a62017-05-03 11:16:05 -07001060 testOptionalUnitaryGain(
1061 [this](float volume) { return stream->setGain(volume); },
1062 "InputStream::setGain");
Kevin Rocardc9963522017-03-10 18:47:37 -08001063}
1064
Kevin Rocard72e50e22017-05-05 14:02:55 -07001065static void testPrepareForReading(IStreamIn* stream, uint32_t frameSize,
1066 uint32_t framesCount) {
Kevin Rocardc9963522017-03-10 18:47:37 -08001067 Result res;
1068 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001069 ASSERT_OK(stream->prepareForReading(
1070 frameSize, framesCount,
1071 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocardc9963522017-03-10 18:47:37 -08001072 EXPECT_RESULT(invalidArgsOrNotSupported, res);
1073}
1074
Kevin Rocard195205b2017-05-02 18:34:59 -07001075TEST_P(InputStreamTest, PrepareForReadingWithZeroBuffer) {
1076 doc::test(
1077 "Preparing a stream for reading with a 0 sized buffer should fail");
1078 testPrepareForReading(stream.get(), 0, 0);
1079}
1080
Kevin Rocardc9963522017-03-10 18:47:37 -08001081TEST_P(InputStreamTest, PrepareForReadingWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001082 doc::test(
1083 "Preparing a stream for reading with a 2^32 sized buffer should fail");
1084 testPrepareForReading(stream.get(), 1,
1085 std::numeric_limits<uint32_t>::max());
Kevin Rocardc9963522017-03-10 18:47:37 -08001086}
1087
1088TEST_P(InputStreamTest, PrepareForReadingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001089 doc::test(
1090 "Preparing a stream for reading with a overflowing sized buffer should "
1091 "fail");
Kevin Rocardc9963522017-03-10 18:47:37 -08001092 auto uintMax = std::numeric_limits<uint32_t>::max();
1093 testPrepareForReading(stream.get(), uintMax, uintMax);
1094}
1095
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001096TEST_P(InputStreamTest, GetInputFramesLost) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001097 doc::test(
1098 "The number of frames lost on a never started stream should be 0");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001099 auto ret = stream->getInputFramesLost();
1100 ASSERT_TRUE(ret.isOk());
1101 uint32_t framesLost{ret};
1102 ASSERT_EQ(0U, framesLost);
1103}
1104
Kevin Rocardc9963522017-03-10 18:47:37 -08001105TEST_P(InputStreamTest, getCapturePosition) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001106 doc::test(
1107 "The capture position of a non prepared stream should not be "
1108 "retrievable");
Kevin Rocardc9963522017-03-10 18:47:37 -08001109 uint64_t frames;
1110 uint64_t time;
1111 ASSERT_OK(stream->getCapturePosition(returnIn(res, frames, time)));
1112 ASSERT_RESULT(invalidStateOrNotSupported, res);
1113}
1114
1115//////////////////////////////////////////////////////////////////////////////
Kevin Rocard624800c2017-03-10 18:47:37 -08001116///////////////////////////////// StreamIn ///////////////////////////////////
1117//////////////////////////////////////////////////////////////////////////////
1118
1119TEST_P(OutputStreamTest, getLatency) {
1120 doc::test("Make sure latency is over 0");
1121 auto result = stream->getLatency();
1122 ASSERT_TRUE(result.isOk());
1123 ASSERT_GT(result, 0U);
1124}
1125
1126TEST_P(OutputStreamTest, setVolume) {
1127 doc::test("Try to set the output volume");
Kevin Rocard98390a62017-05-03 11:16:05 -07001128 testOptionalUnitaryGain(
1129 [this](float volume) { return stream->setVolume(volume, volume); },
1130 "setVolume");
Kevin Rocard624800c2017-03-10 18:47:37 -08001131}
1132
Kevin Rocard72e50e22017-05-05 14:02:55 -07001133static void testPrepareForWriting(IStreamOut* stream, uint32_t frameSize,
1134 uint32_t framesCount) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001135 Result res;
1136 // Ignore output parameters as the call should fail
Kevin Rocard72e50e22017-05-05 14:02:55 -07001137 ASSERT_OK(stream->prepareForWriting(
1138 frameSize, framesCount,
1139 [&res](auto r, auto&, auto&, auto&, auto&) { res = r; }));
Kevin Rocard624800c2017-03-10 18:47:37 -08001140 EXPECT_RESULT(invalidArgsOrNotSupported, res);
1141}
1142
Kevin Rocard195205b2017-05-02 18:34:59 -07001143TEST_P(OutputStreamTest, PrepareForWriteWithZeroBuffer) {
1144 doc::test(
1145 "Preparing a stream for writing with a 0 sized buffer should fail");
1146 testPrepareForWriting(stream.get(), 0, 0);
1147}
1148
Kevin Rocard624800c2017-03-10 18:47:37 -08001149TEST_P(OutputStreamTest, PrepareForWriteWithHugeBuffer) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001150 doc::test(
1151 "Preparing a stream for writing with a 2^32 sized buffer should fail");
1152 testPrepareForWriting(stream.get(), 1,
1153 std::numeric_limits<uint32_t>::max());
Kevin Rocard624800c2017-03-10 18:47:37 -08001154}
1155
1156TEST_P(OutputStreamTest, PrepareForWritingCheckOverflow) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001157 doc::test(
1158 "Preparing a stream for writing with a overflowing sized buffer should "
1159 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001160 auto uintMax = std::numeric_limits<uint32_t>::max();
1161 testPrepareForWriting(stream.get(), uintMax, uintMax);
1162}
1163
1164struct Capability {
1165 Capability(IStreamOut* stream) {
1166 EXPECT_OK(stream->supportsPauseAndResume(returnIn(pause, resume)));
1167 auto ret = stream->supportsDrain();
1168 EXPECT_TRUE(ret.isOk());
1169 if (ret.isOk()) {
1170 drain = ret;
1171 }
1172 }
1173 bool pause = false;
1174 bool resume = false;
1175 bool drain = false;
1176};
1177
1178TEST_P(OutputStreamTest, SupportsPauseAndResumeAndDrain) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001179 doc::test(
1180 "Implementation must expose pause, resume and drain capabilities");
Kevin Rocard624800c2017-03-10 18:47:37 -08001181 Capability(stream.get());
1182}
1183
1184TEST_P(OutputStreamTest, GetRenderPosition) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001185 doc::test("The render position should be 0 on a not started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001186 uint32_t dspFrames;
1187 ASSERT_OK(stream->getRenderPosition(returnIn(res, dspFrames)));
1188 if (res == Result::NOT_SUPPORTED) {
1189 doc::partialTest("getRenderPosition is not supported");
1190 return;
1191 }
1192 ASSERT_OK(res);
1193 ASSERT_EQ(0U, dspFrames);
1194}
1195
1196TEST_P(OutputStreamTest, GetNextWriteTimestamp) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001197 doc::test("The render position of a stream just created should be 0");
Kevin Rocard624800c2017-03-10 18:47:37 -08001198 uint64_t timestampUs;
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001199 ASSERT_OK(stream->getNextWriteTimestamp(returnIn(res, timestampUs)));
Kevin Rocard624800c2017-03-10 18:47:37 -08001200 if (res == Result::NOT_SUPPORTED) {
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001201 doc::partialTest("getNextWriteTimestamp is not supported");
Kevin Rocard624800c2017-03-10 18:47:37 -08001202 return;
1203 }
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001204 ASSERT_EQ(Result::INVALID_STATE, res);
Kevin Rocard624800c2017-03-10 18:47:37 -08001205}
1206
1207/** Stub implementation of out stream callback. */
1208class MockOutCallbacks : public IStreamOutCallback {
1209 Return<void> onWriteReady() override { return {}; }
1210 Return<void> onDrainReady() override { return {}; }
1211 Return<void> onError() override { return {}; }
1212};
1213
Kevin Rocard72e50e22017-05-05 14:02:55 -07001214static bool isAsyncModeSupported(IStreamOut* stream) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001215 auto res = stream->setCallback(new MockOutCallbacks);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001216 stream->clearCallback(); // try to restore the no callback state, ignore
1217 // any error
1218 auto okOrNotSupported = {Result::OK, Result::NOT_SUPPORTED};
Kevin Rocard624800c2017-03-10 18:47:37 -08001219 EXPECT_RESULT(okOrNotSupported, res);
1220 return res.isOk() ? res == Result::OK : false;
1221}
1222
1223TEST_P(OutputStreamTest, SetCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001224 doc::test(
1225 "If supported, registering callback for async operation should never "
1226 "fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001227 if (!isAsyncModeSupported(stream.get())) {
1228 doc::partialTest("The stream does not support async operations");
1229 return;
1230 }
1231 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1232 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1233}
1234
1235TEST_P(OutputStreamTest, clearCallback) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001236 doc::test(
1237 "If supported, clearing a callback to go back to sync operation should "
1238 "not fail");
Kevin Rocard624800c2017-03-10 18:47:37 -08001239 if (!isAsyncModeSupported(stream.get())) {
1240 doc::partialTest("The stream does not support async operations");
1241 return;
1242 }
1243 // TODO: Clarify if clearing a non existing callback should fail
1244 ASSERT_OK(stream->setCallback(new MockOutCallbacks));
1245 ASSERT_OK(stream->clearCallback());
1246}
1247
1248TEST_P(OutputStreamTest, Resume) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001249 doc::test(
1250 "If supported, a stream should fail to resume if not previously "
1251 "paused");
Kevin Rocard624800c2017-03-10 18:47:37 -08001252 if (!Capability(stream.get()).resume) {
1253 doc::partialTest("The output stream does not support resume");
1254 return;
1255 }
1256 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1257}
1258
1259TEST_P(OutputStreamTest, Pause) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001260 doc::test(
1261 "If supported, a stream should fail to pause if not previously "
1262 "started");
Kevin Rocard624800c2017-03-10 18:47:37 -08001263 if (!Capability(stream.get()).pause) {
1264 doc::partialTest("The output stream does not support pause");
1265 return;
1266 }
1267 ASSERT_RESULT(Result::INVALID_STATE, stream->resume());
1268}
1269
Kevin Rocard72e50e22017-05-05 14:02:55 -07001270static void testDrain(IStreamOut* stream, AudioDrain type) {
Kevin Rocard624800c2017-03-10 18:47:37 -08001271 if (!Capability(stream).drain) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001272 doc::partialTest("The output stream does not support drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001273 return;
1274 }
1275 ASSERT_RESULT(Result::OK, stream->drain(type));
1276}
1277
1278TEST_P(OutputStreamTest, DrainAll) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001279 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001280 testDrain(stream.get(), AudioDrain::ALL);
1281}
1282
1283TEST_P(OutputStreamTest, DrainEarlyNotify) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001284 doc::test("If supported, a stream should always succeed to drain");
Kevin Rocard624800c2017-03-10 18:47:37 -08001285 testDrain(stream.get(), AudioDrain::EARLY_NOTIFY);
1286}
1287
1288TEST_P(OutputStreamTest, FlushStop) {
Kevin Rocard6f226802017-04-03 10:29:34 -07001289 doc::test("If supported, a stream should always succeed to flush");
Kevin Rocarde9a8fb72017-03-21 11:39:55 -07001290 auto ret = stream->flush();
1291 ASSERT_TRUE(ret.isOk());
1292 if (ret == Result::NOT_SUPPORTED) {
1293 doc::partialTest("Flush is not supported");
1294 return;
1295 }
1296 ASSERT_OK(ret);
Kevin Rocard624800c2017-03-10 18:47:37 -08001297}
1298
1299TEST_P(OutputStreamTest, GetPresentationPositionStop) {
Kevin Rocard72e50e22017-05-05 14:02:55 -07001300 doc::test(
1301 "If supported, a stream should always succeed to retrieve the "
1302 "presentation position");
Kevin Rocard624800c2017-03-10 18:47:37 -08001303 uint64_t frames;
1304 TimeSpec mesureTS;
1305 ASSERT_OK(stream->getPresentationPosition(returnIn(res, frames, mesureTS)));
1306 if (res == Result::NOT_SUPPORTED) {
1307 doc::partialTest("getpresentationPosition is not supported");
1308 return;
1309 }
1310 ASSERT_EQ(0U, frames);
1311
Kevin Rocard476e38f2017-05-03 10:52:43 -07001312 if (mesureTS.tvNSec == 0 && mesureTS.tvSec == 0) {
1313 // As the stream has never written a frame yet,
1314 // the timestamp does not really have a meaning, allow to return 0
1315 return;
1316 }
1317
1318 // Make sure the return measure is not more than 1s old.
Kevin Rocard624800c2017-03-10 18:47:37 -08001319 struct timespec currentTS;
1320 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &currentTS)) << errno;
1321
Kevin Rocard72e50e22017-05-05 14:02:55 -07001322 auto toMicroSec = [](uint64_t sec, auto nsec) {
1323 return sec * 1e+6 + nsec / 1e+3;
1324 };
Kevin Rocard624800c2017-03-10 18:47:37 -08001325 auto currentTime = toMicroSec(currentTS.tv_sec, currentTS.tv_nsec);
1326 auto mesureTime = toMicroSec(mesureTS.tvSec, mesureTS.tvNSec);
Kevin Rocard72e50e22017-05-05 14:02:55 -07001327 ASSERT_PRED2([](auto c, auto m) { return c - m < 1e+6; }, currentTime,
1328 mesureTime);
Kevin Rocard624800c2017-03-10 18:47:37 -08001329}
1330
Kevin Rocard624800c2017-03-10 18:47:37 -08001331//////////////////////////////////////////////////////////////////////////////
Kevin Rocard3c405a72017-03-08 16:46:51 -08001332/////////////////////////////// PrimaryDevice ////////////////////////////////
1333//////////////////////////////////////////////////////////////////////////////
1334
1335TEST_F(AudioPrimaryHidlTest, setVoiceVolume) {
1336 doc::test("Make sure setVoiceVolume only succeed if volume is in [0,1]");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001337 testUnitaryGain(
1338 [this](float volume) { return device->setVoiceVolume(volume); });
Kevin Rocard3c405a72017-03-08 16:46:51 -08001339}
1340
1341TEST_F(AudioPrimaryHidlTest, setMode) {
Kevin Rocard04364ed2017-05-02 18:16:00 -07001342 doc::test(
1343 "Make sure setMode always succeeds if mode is valid "
1344 "and fails otherwise");
1345 // Test Invalid values
1346 for (AudioMode mode :
1347 {AudioMode::INVALID, AudioMode::CURRENT, AudioMode::CNT}) {
1348 SCOPED_TRACE("mode=" + toString(mode));
1349 ASSERT_RESULT(Result::INVALID_ARGUMENTS, device->setMode(mode));
1350 }
1351 // Test valid values
Kevin Rocard72e50e22017-05-05 14:02:55 -07001352 for (AudioMode mode :
1353 {AudioMode::IN_CALL, AudioMode::IN_COMMUNICATION, AudioMode::RINGTONE,
Kevin Rocard72e50e22017-05-05 14:02:55 -07001354 AudioMode::NORMAL /* Make sure to leave the test in normal mode */}) {
Kevin Rocard3c405a72017-03-08 16:46:51 -08001355 SCOPED_TRACE("mode=" + toString(mode));
1356 ASSERT_OK(device->setMode(mode));
1357 }
Kevin Rocard3c405a72017-03-08 16:46:51 -08001358}
1359
Kevin Rocard3c405a72017-03-08 16:46:51 -08001360TEST_F(BoolAccessorPrimaryHidlTest, BtScoNrecEnabled) {
1361 doc::test("Query and set the BT SCO NR&EC state");
1362 testOptionalAccessors("BtScoNrecEnabled", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001363 &IPrimaryDevice::setBtScoNrecEnabled,
1364 &IPrimaryDevice::getBtScoNrecEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001365}
1366
1367TEST_F(BoolAccessorPrimaryHidlTest, setGetBtScoWidebandEnabled) {
1368 doc::test("Query and set the SCO whideband state");
1369 testOptionalAccessors("BtScoWideband", {true, false, true},
Kevin Rocard72e50e22017-05-05 14:02:55 -07001370 &IPrimaryDevice::setBtScoWidebandEnabled,
1371 &IPrimaryDevice::getBtScoWidebandEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001372}
1373
1374using TtyModeAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<TtyMode>;
1375TEST_F(TtyModeAccessorPrimaryHidlTest, setGetTtyMode) {
1376 doc::test("Query and set the TTY mode state");
Kevin Rocard72e50e22017-05-05 14:02:55 -07001377 testOptionalAccessors(
1378 "TTY mode", {TtyMode::OFF, TtyMode::HCO, TtyMode::VCO, TtyMode::FULL},
1379 &IPrimaryDevice::setTtyMode, &IPrimaryDevice::getTtyMode);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001380}
1381
1382TEST_F(BoolAccessorPrimaryHidlTest, setGetHac) {
1383 doc::test("Query and set the HAC state");
Kevin Rocard98390a62017-05-03 11:16:05 -07001384 testOptionalAccessors("HAC", {true, false, true},
1385 &IPrimaryDevice::setHacEnabled,
1386 &IPrimaryDevice::getHacEnabled);
Kevin Rocard3c405a72017-03-08 16:46:51 -08001387}
1388
1389//////////////////////////////////////////////////////////////////////////////
Kevin Rocardf0357882017-02-10 16:19:28 -08001390//////////////////// Clean caches on global tear down ////////////////////////
1391//////////////////////////////////////////////////////////////////////////////
1392
1393int main(int argc, char** argv) {
1394 environment = new Environment;
1395 ::testing::AddGlobalTestEnvironment(environment);
1396 ::testing::InitGoogleTest(&argc, argv);
1397 int status = RUN_ALL_TESTS();
1398 LOG(INFO) << "Test result = " << status;
1399 return status;
1400}