Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 1 | /* |
| 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 Ma | 161b564 | 2017-03-10 13:44:22 -0800 | [diff] [blame] | 29 | #include <VtsHalHidlTargetTestBase.h> |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 30 | |
| 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 Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 35 | #include <android/hardware/audio/2.0/IPrimaryDevice.h> |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 36 | #include <android/hardware/audio/2.0/types.h> |
| 37 | #include <android/hardware/audio/common/2.0/types.h> |
| 38 | |
| 39 | #include "utility/ReturnIn.h" |
| 40 | #include "utility/AssertOk.h" |
| 41 | #include "utility/PrettyPrintAudioTypes.h" |
| 42 | |
| 43 | using std::string; |
| 44 | using std::to_string; |
| 45 | using std::vector; |
| 46 | |
| 47 | using ::android::sp; |
| 48 | using ::android::hardware::Return; |
| 49 | using ::android::hardware::hidl_handle; |
| 50 | using ::android::hardware::hidl_string; |
| 51 | using ::android::hardware::hidl_vec; |
Kevin Rocard | c996352 | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 52 | using ::android::hardware::MQDescriptorSync; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 53 | using ::android::hardware::audio::V2_0::DeviceAddress; |
| 54 | using ::android::hardware::audio::V2_0::IDevice; |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 55 | using ::android::hardware::audio::V2_0::IPrimaryDevice; |
| 56 | using TtyMode = ::android::hardware::audio::V2_0::IPrimaryDevice::TtyMode; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 57 | using ::android::hardware::audio::V2_0::IDevicesFactory; |
| 58 | using ::android::hardware::audio::V2_0::IStream; |
| 59 | using ::android::hardware::audio::V2_0::IStreamIn; |
Kevin Rocard | c996352 | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 60 | using ReadParameters = ::android::hardware::audio::V2_0::IStreamIn::ReadParameters; |
| 61 | using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 62 | using ::android::hardware::audio::V2_0::IStreamOut; |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 63 | using ::android::hardware::audio::V2_0::MmapBufferInfo; |
| 64 | using ::android::hardware::audio::V2_0::MmapPosition; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 65 | using ::android::hardware::audio::V2_0::ParameterValue; |
| 66 | using ::android::hardware::audio::V2_0::Result; |
| 67 | using ::android::hardware::audio::common::V2_0::AudioChannelMask; |
| 68 | using ::android::hardware::audio::common::V2_0::AudioConfig; |
| 69 | using ::android::hardware::audio::common::V2_0::AudioDevice; |
| 70 | using ::android::hardware::audio::common::V2_0::AudioFormat; |
| 71 | using ::android::hardware::audio::common::V2_0::AudioHandleConsts; |
| 72 | using ::android::hardware::audio::common::V2_0::AudioInputFlag; |
| 73 | using ::android::hardware::audio::common::V2_0::AudioIoHandle; |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 74 | using ::android::hardware::audio::common::V2_0::AudioMode; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 75 | using ::android::hardware::audio::common::V2_0::AudioOffloadInfo; |
| 76 | using ::android::hardware::audio::common::V2_0::AudioOutputFlag; |
| 77 | using ::android::hardware::audio::common::V2_0::AudioSource; |
Kevin Rocard | c996352 | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 78 | using ::android::hardware::audio::common::V2_0::ThreadInfo; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 79 | |
| 80 | using utility::returnIn; |
| 81 | |
| 82 | namespace doc { |
| 83 | /** Document the current test case. |
| 84 | * Eg: calling `doc::test("Dump the state of the hal")` in the "debugDump" test will output: |
| 85 | * <testcase name="debugDump" status="run" time="6" classname="AudioPrimaryHidlTest" |
| 86 | description="Dump the state of the hal." /> |
| 87 | * see https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#logging-additional-information |
| 88 | */ |
| 89 | void test(const std::string& testCaseDocumentation) { |
| 90 | ::testing::Test::RecordProperty("description", testCaseDocumentation); |
| 91 | } |
| 92 | |
| 93 | /** Document why a test was not fully run. Usually due to an optional feature not implemented. */ |
| 94 | void partialTest(const std::string& reason) { |
| 95 | ::testing::Test::RecordProperty("partialyRunTest", reason); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Register callback for static object destruction |
| 100 | // Avoid destroying static objects after main return. |
| 101 | // Post main return destruction leads to incorrect gtest timing measurements as well as harder |
| 102 | // debuging if anything goes wrong during destruction. |
| 103 | class Environment : public ::testing::Environment { |
| 104 | public: |
| 105 | using TearDownFunc = std::function<void ()>; |
| 106 | void registerTearDown(TearDownFunc&& tearDown) { |
| 107 | tearDowns.push_back(std::move(tearDown)); |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | void TearDown() override { |
| 112 | // Call the tear downs in reverse order of insertion |
| 113 | for (auto& tearDown : tearDowns) { |
| 114 | tearDown(); |
| 115 | } |
| 116 | } |
| 117 | std::list<TearDownFunc> tearDowns; |
| 118 | }; |
| 119 | // Instance to register global tearDown |
| 120 | static Environment* environment; |
| 121 | |
Yuexi Ma | 161b564 | 2017-03-10 13:44:22 -0800 | [diff] [blame] | 122 | class HidlTest : public ::testing::VtsHalHidlTargetTestBase { |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 123 | protected: |
| 124 | // Convenient member to store results |
| 125 | Result res; |
| 126 | }; |
| 127 | |
| 128 | ////////////////////////////////////////////////////////////////////////////// |
| 129 | ////////////////////// getService audio_devices_factory ////////////////////// |
| 130 | ////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | // Test all audio devices |
| 133 | class AudioHidlTest : public HidlTest { |
| 134 | public: |
| 135 | void SetUp() override { |
| 136 | ASSERT_NO_FATAL_FAILURE(HidlTest::SetUp()); // setup base |
| 137 | |
| 138 | if (devicesFactory == nullptr) { |
| 139 | environment->registerTearDown([]{ devicesFactory.clear(); }); |
Yuexi Ma | 161b564 | 2017-03-10 13:44:22 -0800 | [diff] [blame] | 140 | devicesFactory = ::testing::VtsHalHidlTargetTestBase::getService<IDevicesFactory>(); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 141 | } |
| 142 | ASSERT_TRUE(devicesFactory != nullptr); |
| 143 | } |
| 144 | |
| 145 | protected: |
| 146 | // Cache the devicesFactory retrieval to speed up each test by ~0.5s |
| 147 | static sp<IDevicesFactory> devicesFactory; |
| 148 | }; |
| 149 | sp<IDevicesFactory> AudioHidlTest::devicesFactory; |
| 150 | |
| 151 | TEST_F(AudioHidlTest, GetAudioDevicesFactoryService) { |
| 152 | doc::test("test the getService (called in SetUp)"); |
| 153 | } |
| 154 | |
| 155 | ////////////////////////////////////////////////////////////////////////////// |
| 156 | /////////////////////////////// openDevice primary /////////////////////////// |
| 157 | ////////////////////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | // Test the primary device |
| 160 | class AudioPrimaryHidlTest : public AudioHidlTest { |
| 161 | public: |
| 162 | /** Primary HAL test are NOT thread safe. */ |
| 163 | void SetUp() override { |
| 164 | ASSERT_NO_FATAL_FAILURE(AudioHidlTest::SetUp()); // setup base |
| 165 | |
| 166 | if (device == nullptr) { |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 167 | IDevicesFactory::Result result; |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 168 | sp<IDevice> baseDevice; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 169 | ASSERT_OK(devicesFactory->openDevice(IDevicesFactory::Device::PRIMARY, |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 170 | returnIn(result, baseDevice))); |
| 171 | ASSERT_TRUE(baseDevice != nullptr); |
| 172 | |
| 173 | environment->registerTearDown([]{ device.clear(); }); |
| 174 | device = IPrimaryDevice::castFrom(baseDevice); |
| 175 | ASSERT_TRUE(device != nullptr); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 176 | } |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | protected: |
| 180 | // Cache the device opening to speed up each test by ~0.5s |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 181 | static sp<IPrimaryDevice> device; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 182 | }; |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 183 | sp<IPrimaryDevice> AudioPrimaryHidlTest::device; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 184 | |
| 185 | TEST_F(AudioPrimaryHidlTest, OpenPrimaryDevice) { |
| 186 | doc::test("Test the openDevice (called in SetUp)"); |
| 187 | } |
| 188 | |
| 189 | TEST_F(AudioPrimaryHidlTest, Init) { |
| 190 | doc::test("Test that the audio primary hal initialized correctly"); |
| 191 | ASSERT_OK(device->initCheck()); |
| 192 | } |
| 193 | |
| 194 | ////////////////////////////////////////////////////////////////////////////// |
Kevin Rocard | 92ce35d | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 195 | ///////////////////// {set,get}{Master,Mic}{Mute,Volume} ///////////////////// |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 196 | ////////////////////////////////////////////////////////////////////////////// |
| 197 | |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 198 | template <class Property> |
| 199 | class AccessorPrimaryHidlTest : public AudioPrimaryHidlTest { |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 200 | protected: |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 201 | |
| 202 | /** Test a property getter and setter. */ |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 203 | template <class Getter, class Setter> |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 204 | void testAccessors(const string& propertyName, const vector<Property>& valuesToTest, |
Kevin Rocard | 92ce35d | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 205 | Setter setter, Getter getter, |
| 206 | const vector<Property>& invalidValues = {}) { |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 207 | |
| 208 | Property initialValue; // Save initial value to restore it at the end of the test |
| 209 | ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue))); |
| 210 | ASSERT_OK(res); |
| 211 | |
| 212 | for (Property setValue : valuesToTest) { |
Kevin Rocard | 92ce35d | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 213 | SCOPED_TRACE("Test " + propertyName + " getter and setter for " + |
| 214 | testing::PrintToString(setValue)); |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 215 | ASSERT_OK((device.get()->*setter)(setValue)); |
| 216 | Property getValue; |
| 217 | // Make sure the getter returns the same value just set |
| 218 | ASSERT_OK((device.get()->*getter)(returnIn(res, getValue))); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 219 | ASSERT_OK(res); |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 220 | EXPECT_EQ(setValue, getValue); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 221 | } |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 222 | |
Kevin Rocard | 92ce35d | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 223 | for (Property invalidValue : invalidValues) { |
| 224 | SCOPED_TRACE("Try to set " + propertyName + " with the invalid value " + |
| 225 | testing::PrintToString(invalidValue)); |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 226 | EXPECT_RESULT(Result::INVALID_ARGUMENTS, (device.get()->*setter)(invalidValue)); |
Kevin Rocard | 92ce35d | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 229 | ASSERT_OK((device.get()->*setter)(initialValue)); // restore initial value |
| 230 | } |
| 231 | |
| 232 | /** Test the getter and setter of an optional feature. */ |
| 233 | template <class Getter, class Setter> |
| 234 | void testOptionalAccessors(const string& propertyName, const vector<Property>& valuesToTest, |
Kevin Rocard | 92ce35d | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 235 | Setter setter, Getter getter, |
| 236 | const vector<Property>& invalidValues = {}) { |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 237 | doc::test("Test the optional " + propertyName + " getters and setter"); |
| 238 | { |
| 239 | SCOPED_TRACE("Test feature support by calling the getter"); |
| 240 | Property initialValue; |
| 241 | ASSERT_OK((device.get()->*getter)(returnIn(res, initialValue))); |
| 242 | if (res == Result::NOT_SUPPORTED) { |
| 243 | doc::partialTest(propertyName + " getter is not supported"); |
| 244 | return; |
| 245 | } |
| 246 | ASSERT_OK(res); // If it is supported it must succeed |
| 247 | } |
| 248 | // The feature is supported, test it |
Kevin Rocard | 92ce35d | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 249 | testAccessors(propertyName, valuesToTest, setter, getter, invalidValues); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 250 | } |
| 251 | }; |
| 252 | |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 253 | using BoolAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<bool>; |
| 254 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 255 | TEST_F(BoolAccessorPrimaryHidlTest, MicMuteTest) { |
| 256 | doc::test("Check that the mic can be muted and unmuted"); |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 257 | testAccessors("mic mute", {true, false, true}, &IDevice::setMicMute, &IDevice::getMicMute); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 258 | // TODO: check that the mic is really muted (all sample are 0) |
| 259 | } |
| 260 | |
| 261 | TEST_F(BoolAccessorPrimaryHidlTest, MasterMuteTest) { |
| 262 | doc::test("If master mute is supported, try to mute and unmute the master output"); |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 263 | testOptionalAccessors("master mute", {true, false, true}, |
| 264 | &IDevice::setMasterMute, &IDevice::getMasterMute); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 265 | // TODO: check that the master volume is really muted |
| 266 | } |
| 267 | |
Kevin Rocard | 92ce35d | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 268 | using FloatAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<float>; |
| 269 | TEST_F(FloatAccessorPrimaryHidlTest, MasterVolumeTest) { |
| 270 | doc::test("Test the master volume if supported"); |
| 271 | testOptionalAccessors("master volume", {0, 0.5, 1}, |
| 272 | &IDevice::setMasterVolume, &IDevice::getMasterVolume, |
| 273 | {-0.1, 1.1, NAN, INFINITY, -INFINITY, |
| 274 | 1 + std::numeric_limits<float>::epsilon()}); |
| 275 | // TODO: check that the master volume is really changed |
| 276 | } |
| 277 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 278 | ////////////////////////////////////////////////////////////////////////////// |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 279 | //////////////////////////////// AudioPatches //////////////////////////////// |
| 280 | ////////////////////////////////////////////////////////////////////////////// |
| 281 | |
| 282 | class AudioPatchPrimaryHidlTest : public AudioPrimaryHidlTest { |
| 283 | protected: |
| 284 | bool areAudioPatchesSupported() { |
| 285 | auto result = device->supportsAudioPatches(); |
| 286 | EXPECT_TRUE(result.isOk()); |
| 287 | return result; |
| 288 | } |
| 289 | }; |
| 290 | |
| 291 | TEST_F(AudioPatchPrimaryHidlTest, AudioPatches) { |
| 292 | doc::test("Test if audio patches are supported"); |
| 293 | if (!areAudioPatchesSupported()) { |
| 294 | doc::partialTest("Audio patches are not supported"); |
| 295 | return; |
| 296 | } |
| 297 | // TODO: test audio patches |
| 298 | } |
| 299 | |
| 300 | |
| 301 | ////////////////////////////////////////////////////////////////////////////// |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 302 | //////////////// Required and recommended audio format support /////////////// |
| 303 | // From: https://source.android.com/compatibility/android-cdd.html#5_4_audio_recording |
| 304 | // From: https://source.android.com/compatibility/android-cdd.html#5_5_audio_playback |
| 305 | /////////// TODO: move to the beginning of the file for easier update //////// |
| 306 | ////////////////////////////////////////////////////////////////////////////// |
| 307 | |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 308 | class AudioConfigPrimaryTest : public AudioPatchPrimaryHidlTest { |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 309 | public: |
| 310 | // Cache result ? |
| 311 | static const vector<AudioConfig> getRequiredSupportPlaybackAudioConfig() { |
| 312 | return combineAudioConfig({AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO}, |
| 313 | {8000, 11025, 16000, 22050, 32000, 44100}, |
| 314 | {AudioFormat::PCM_16_BIT}); |
| 315 | } |
| 316 | |
| 317 | static const vector<AudioConfig> getRecommendedSupportPlaybackAudioConfig() { |
| 318 | return combineAudioConfig({AudioChannelMask::OUT_STEREO, AudioChannelMask::OUT_MONO}, |
| 319 | {24000, 48000}, |
| 320 | {AudioFormat::PCM_16_BIT}); |
| 321 | } |
| 322 | |
| 323 | static const vector<AudioConfig> getSupportedPlaybackAudioConfig() { |
| 324 | // TODO: retrieve audio config supported by the platform |
| 325 | // as declared in the policy configuration |
| 326 | return {}; |
| 327 | } |
| 328 | |
| 329 | static const vector<AudioConfig> getRequiredSupportCaptureAudioConfig() { |
| 330 | return combineAudioConfig({AudioChannelMask::IN_MONO}, |
| 331 | {8000, 11025, 16000, 44100}, |
| 332 | {AudioFormat::PCM_16_BIT}); |
| 333 | } |
| 334 | static const vector<AudioConfig> getRecommendedSupportCaptureAudioConfig() { |
| 335 | return combineAudioConfig({AudioChannelMask::IN_STEREO}, |
| 336 | {22050, 48000}, |
| 337 | {AudioFormat::PCM_16_BIT}); |
| 338 | } |
| 339 | static const vector<AudioConfig> getSupportedCaptureAudioConfig() { |
| 340 | // TODO: retrieve audio config supported by the platform |
| 341 | // as declared in the policy configuration |
| 342 | return {}; |
| 343 | } |
| 344 | private: |
| 345 | static const vector<AudioConfig> combineAudioConfig( |
| 346 | vector<AudioChannelMask> channelMasks, |
| 347 | vector<uint32_t> sampleRates, |
| 348 | vector<AudioFormat> formats) { |
| 349 | vector<AudioConfig> configs; |
| 350 | for (auto channelMask: channelMasks) { |
| 351 | for (auto sampleRate : sampleRates) { |
| 352 | for (auto format : formats) { |
| 353 | AudioConfig config{}; |
| 354 | // leave offloadInfo to 0 |
| 355 | config.channelMask = channelMask; |
| 356 | config.sampleRateHz = sampleRate; |
| 357 | config.format = format; |
| 358 | // FIXME: leave frameCount to 0 ? |
| 359 | configs.push_back(config); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | return configs; |
| 364 | } |
| 365 | }; |
| 366 | |
Kevin Rocard | 9c36914 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 367 | /** Generate a test name based on an audio config. |
| 368 | * |
| 369 | * As the only parameter changing are channel mask and sample rate, |
| 370 | * only print those ones in the test name. |
| 371 | */ |
| 372 | static string generateTestName(const testing::TestParamInfo<AudioConfig>& info) { |
| 373 | const AudioConfig& config = info.param; |
| 374 | return to_string(info.index) + "__" + to_string(config.sampleRateHz)+ "_" + |
| 375 | // "MONO" is more clear than "FRONT_LEFT" |
| 376 | ((config.channelMask == AudioChannelMask::OUT_MONO || |
| 377 | config.channelMask == AudioChannelMask::IN_MONO) ? |
| 378 | "MONO" : toString(config.channelMask)); |
| 379 | } |
| 380 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 381 | ////////////////////////////////////////////////////////////////////////////// |
| 382 | ///////////////////////////// getInputBufferSize ///////////////////////////// |
| 383 | ////////////////////////////////////////////////////////////////////////////// |
| 384 | |
| 385 | // FIXME: execute input test only if platform declares android.hardware.microphone |
| 386 | // how to get this value ? is it a property ??? |
| 387 | |
| 388 | class AudioCaptureConfigPrimaryTest : public AudioConfigPrimaryTest, |
| 389 | public ::testing::WithParamInterface<AudioConfig> { |
| 390 | protected: |
| 391 | void inputBufferSizeTest(const AudioConfig& audioConfig, bool supportRequired) { |
| 392 | uint64_t bufferSize; |
| 393 | ASSERT_OK(device->getInputBufferSize(audioConfig, returnIn(res, bufferSize))); |
| 394 | |
| 395 | switch (res) { |
| 396 | case Result::INVALID_ARGUMENTS: |
| 397 | EXPECT_FALSE(supportRequired); |
| 398 | break; |
| 399 | case Result::OK: |
| 400 | // Check that the buffer is of a sane size |
| 401 | // For now only that it is > 0 |
| 402 | EXPECT_GT(bufferSize, uint64_t(0)); |
| 403 | break; |
| 404 | default: |
| 405 | FAIL() << "Invalid return status: " << ::testing::PrintToString(res); |
| 406 | } |
| 407 | } |
| 408 | }; |
| 409 | |
| 410 | // Test that the required capture config and those declared in the policy are indeed supported |
| 411 | class RequiredInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {}; |
| 412 | TEST_P(RequiredInputBufferSizeTest, RequiredInputBufferSizeTest) { |
| 413 | doc::test("Input buffer size must be retrievable for a format with required support."); |
| 414 | inputBufferSizeTest(GetParam(), true); |
| 415 | } |
| 416 | INSTANTIATE_TEST_CASE_P( |
| 417 | RequiredInputBufferSize, RequiredInputBufferSizeTest, |
Kevin Rocard | 9c36914 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 418 | ::testing::ValuesIn(AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()), |
| 419 | &generateTestName); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 420 | INSTANTIATE_TEST_CASE_P( |
| 421 | SupportedInputBufferSize, RequiredInputBufferSizeTest, |
Kevin Rocard | 9c36914 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 422 | ::testing::ValuesIn(AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()), |
| 423 | &generateTestName); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 424 | |
| 425 | // Test that the recommended capture config are supported or lead to a INVALID_ARGUMENTS return |
| 426 | class OptionalInputBufferSizeTest : public AudioCaptureConfigPrimaryTest {}; |
| 427 | TEST_P(OptionalInputBufferSizeTest, OptionalInputBufferSizeTest) { |
| 428 | doc::test("Input buffer size should be retrievable for a format with recommended support."); |
| 429 | inputBufferSizeTest(GetParam(), false); |
| 430 | } |
| 431 | INSTANTIATE_TEST_CASE_P( |
| 432 | RecommendedCaptureAudioConfigSupport, OptionalInputBufferSizeTest, |
Kevin Rocard | 9c36914 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 433 | ::testing::ValuesIn(AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()), |
| 434 | &generateTestName); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 435 | |
| 436 | ////////////////////////////////////////////////////////////////////////////// |
| 437 | /////////////////////////////// setScreenState /////////////////////////////// |
| 438 | ////////////////////////////////////////////////////////////////////////////// |
| 439 | |
| 440 | TEST_F(AudioPrimaryHidlTest, setScreenState) { |
| 441 | doc::test("Check that the hal can receive the screen state"); |
| 442 | for (bool turnedOn : {false, true, true, false, false}) { |
| 443 | auto ret = device->setScreenState(turnedOn); |
| 444 | ASSERT_TRUE(ret.isOk()); |
| 445 | Result result = ret; |
| 446 | ASSERT_TRUE(result == Result::OK || result == Result::NOT_SUPPORTED) << toString(result); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | ////////////////////////////////////////////////////////////////////////////// |
| 451 | //////////////////////////// {get,set}Parameters ///////////////////////////// |
| 452 | ////////////////////////////////////////////////////////////////////////////// |
| 453 | |
| 454 | TEST_F(AudioPrimaryHidlTest, getParameters) { |
| 455 | doc::test("Check that the hal can set and get parameters"); |
| 456 | hidl_vec<hidl_string> keys; |
| 457 | hidl_vec<ParameterValue> values; |
| 458 | ASSERT_OK(device->getParameters(keys, returnIn(res, values))); |
| 459 | ASSERT_OK(device->setParameters(values)); |
| 460 | values.resize(0); |
| 461 | ASSERT_OK(device->setParameters(values)); |
| 462 | } |
| 463 | |
| 464 | ////////////////////////////////////////////////////////////////////////////// |
| 465 | //////////////////////////////// debugDebug ////////////////////////////////// |
| 466 | ////////////////////////////////////////////////////////////////////////////// |
| 467 | |
Kevin Rocard | b903124 | 2017-03-13 12:20:54 -0700 | [diff] [blame] | 468 | template <class DebugDump> |
| 469 | static void testDebugDump(DebugDump debugDump) { |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 470 | FILE* file = tmpfile(); |
| 471 | ASSERT_NE(nullptr, file) << errno; |
| 472 | |
| 473 | auto* nativeHandle = native_handle_create(1, 0); |
| 474 | ASSERT_NE(nullptr, nativeHandle); |
| 475 | nativeHandle->data[0] = fileno(file); |
| 476 | |
| 477 | hidl_handle handle; |
| 478 | handle.setTo(nativeHandle, true /*take ownership*/); |
| 479 | |
Kevin Rocard | ee771e9 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 480 | // TODO: debugDump does not return a Result. |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 481 | // This mean that the hal can not report that it not implementing the function. |
Kevin Rocard | b903124 | 2017-03-13 12:20:54 -0700 | [diff] [blame] | 482 | ASSERT_OK(debugDump(handle)); |
Kevin Rocard | ee771e9 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 483 | |
| 484 | rewind(file); // can not fail |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 485 | |
| 486 | // Check that at least one bit was written by the hal |
| 487 | char buff; |
Kevin Rocard | ee771e9 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 488 | ASSERT_EQ(size_t{1}, fread(&buff, sizeof(buff), 1, file)); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 489 | EXPECT_EQ(0, fclose(file)) << errno; |
| 490 | } |
| 491 | |
Kevin Rocard | b903124 | 2017-03-13 12:20:54 -0700 | [diff] [blame] | 492 | TEST_F(AudioPrimaryHidlTest, debugDump) { |
| 493 | doc::test("Check that the hal can dump its state without error"); |
| 494 | testDebugDump([this](const auto& handle){ return device->debugDump(handle); }); |
| 495 | } |
| 496 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 497 | ////////////////////////////////////////////////////////////////////////////// |
| 498 | ////////////////////////// open{Output,Input}Stream ////////////////////////// |
| 499 | ////////////////////////////////////////////////////////////////////////////// |
| 500 | |
| 501 | template <class Stream> |
| 502 | class OpenStreamTest : public AudioConfigPrimaryTest, |
| 503 | public ::testing::WithParamInterface<AudioConfig> { |
| 504 | protected: |
| 505 | template <class Open> |
| 506 | void testOpen(Open openStream, const AudioConfig& config) { |
| 507 | // FIXME: Open a stream without an IOHandle |
| 508 | // This is not required to be accepted by hal implementations |
| 509 | AudioIoHandle ioHandle = (AudioIoHandle)AudioHandleConsts::AUDIO_IO_HANDLE_NONE; |
| 510 | AudioConfig suggestedConfig{}; |
| 511 | ASSERT_OK(openStream(ioHandle, config, returnIn(res, stream, suggestedConfig))); |
| 512 | |
| 513 | // TODO: only allow failure for RecommendedPlaybackAudioConfig |
| 514 | switch (res) { |
| 515 | case Result::OK: |
| 516 | ASSERT_TRUE(stream != nullptr); |
| 517 | audioConfig = config; |
| 518 | break; |
| 519 | case Result::INVALID_ARGUMENTS: |
| 520 | ASSERT_TRUE(stream == nullptr); |
| 521 | AudioConfig suggestedConfigRetry; |
| 522 | // Could not open stream with config, try again with the suggested one |
| 523 | ASSERT_OK(openStream(ioHandle, suggestedConfig, |
| 524 | returnIn(res, stream, suggestedConfigRetry))); |
| 525 | // This time it must succeed |
| 526 | ASSERT_OK(res); |
| 527 | ASSERT_TRUE(stream == nullptr); |
| 528 | audioConfig = suggestedConfig; |
| 529 | break; |
| 530 | default: |
| 531 | FAIL() << "Invalid return status: " << ::testing::PrintToString(res); |
| 532 | } |
| 533 | open = true; |
| 534 | } |
| 535 | |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 536 | Return<Result> closeStream() { |
| 537 | open = false; |
| 538 | return stream->close(); |
| 539 | } |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 540 | private: |
| 541 | void TearDown() override { |
| 542 | if (open) { |
| 543 | ASSERT_OK(stream->close()); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | protected: |
| 548 | |
| 549 | AudioConfig audioConfig; |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 550 | DeviceAddress address = {}; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 551 | sp<Stream> stream; |
| 552 | bool open = false; |
| 553 | }; |
| 554 | |
| 555 | ////////////////////////////// openOutputStream ////////////////////////////// |
| 556 | |
| 557 | class OutputStreamTest : public OpenStreamTest<IStreamOut> { |
| 558 | virtual void SetUp() override { |
| 559 | ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 560 | address.device = AudioDevice::OUT_DEFAULT; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 561 | const AudioConfig& config = GetParam(); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 562 | AudioOutputFlag flags = AudioOutputFlag::NONE; // TODO: test all flag combination |
| 563 | testOpen([&](AudioIoHandle handle, AudioConfig config, auto cb) |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 564 | { return device->openOutputStream(handle, address, config, flags, cb); }, |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 565 | config); |
| 566 | } |
| 567 | }; |
| 568 | TEST_P(OutputStreamTest, OpenOutputStreamTest) { |
| 569 | doc::test("Check that output streams can be open with the required and recommended config"); |
| 570 | // Open done in SetUp |
| 571 | } |
| 572 | INSTANTIATE_TEST_CASE_P( |
| 573 | RequiredOutputStreamConfigSupport, OutputStreamTest, |
Kevin Rocard | 9c36914 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 574 | ::testing::ValuesIn(AudioConfigPrimaryTest::getRequiredSupportPlaybackAudioConfig()), |
| 575 | &generateTestName); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 576 | INSTANTIATE_TEST_CASE_P( |
| 577 | SupportedOutputStreamConfig, OutputStreamTest, |
Kevin Rocard | 9c36914 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 578 | ::testing::ValuesIn(AudioConfigPrimaryTest::getSupportedPlaybackAudioConfig()), |
| 579 | &generateTestName); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 580 | |
| 581 | INSTANTIATE_TEST_CASE_P( |
| 582 | RecommendedOutputStreamConfigSupport, OutputStreamTest, |
Kevin Rocard | 9c36914 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 583 | ::testing::ValuesIn(AudioConfigPrimaryTest::getRecommendedSupportPlaybackAudioConfig()), |
| 584 | &generateTestName); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 585 | |
| 586 | ////////////////////////////// openInputStream ////////////////////////////// |
| 587 | |
| 588 | class InputStreamTest : public OpenStreamTest<IStreamIn> { |
| 589 | |
| 590 | virtual void SetUp() override { |
| 591 | ASSERT_NO_FATAL_FAILURE(OpenStreamTest::SetUp()); // setup base |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 592 | address.device = AudioDevice::IN_DEFAULT; |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 593 | const AudioConfig& config = GetParam(); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 594 | AudioInputFlag flags = AudioInputFlag::NONE; // TODO: test all flag combination |
| 595 | AudioSource source = AudioSource::DEFAULT; // TODO: test all flag combination |
| 596 | testOpen([&](AudioIoHandle handle, AudioConfig config, auto cb) |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 597 | { return device->openInputStream(handle, address, config, flags, source, cb); }, |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 598 | config); |
| 599 | } |
| 600 | }; |
| 601 | |
| 602 | TEST_P(InputStreamTest, OpenInputStreamTest) { |
| 603 | doc::test("Check that input streams can be open with the required and recommended config"); |
| 604 | // Open done in setup |
| 605 | } |
| 606 | INSTANTIATE_TEST_CASE_P( |
| 607 | RequiredInputStreamConfigSupport, InputStreamTest, |
Kevin Rocard | 9c36914 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 608 | ::testing::ValuesIn(AudioConfigPrimaryTest::getRequiredSupportCaptureAudioConfig()), |
| 609 | &generateTestName); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 610 | INSTANTIATE_TEST_CASE_P( |
| 611 | SupportedInputStreamConfig, InputStreamTest, |
Kevin Rocard | 9c36914 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 612 | ::testing::ValuesIn(AudioConfigPrimaryTest::getSupportedCaptureAudioConfig()), |
| 613 | &generateTestName); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 614 | |
| 615 | INSTANTIATE_TEST_CASE_P( |
| 616 | RecommendedInputStreamConfigSupport, InputStreamTest, |
Kevin Rocard | 9c36914 | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 617 | ::testing::ValuesIn(AudioConfigPrimaryTest::getRecommendedSupportCaptureAudioConfig()), |
| 618 | &generateTestName); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 619 | |
| 620 | ////////////////////////////////////////////////////////////////////////////// |
| 621 | ////////////////////////////// IStream getters /////////////////////////////// |
| 622 | ////////////////////////////////////////////////////////////////////////////// |
| 623 | |
| 624 | /** Unpack the provided result. |
| 625 | * If the result is not OK, register a failure and return an undefined value. */ |
| 626 | template <class R> |
| 627 | static R extract(Return<R> ret) { |
| 628 | if (!ret.isOk()) { |
| 629 | ADD_FAILURE(); |
| 630 | return R{}; |
| 631 | } |
| 632 | return ret; |
| 633 | } |
| 634 | |
Kevin Rocard | a7df7fc | 2017-03-10 18:37:46 -0800 | [diff] [blame] | 635 | /* Could not find a way to write a test for two parametrized class fixure |
| 636 | * thus use this macro do duplicate tests for Input and Output stream */ |
| 637 | #define TEST_IO_STREAM(test_name, documentation, code) \ |
| 638 | TEST_P(InputStreamTest, test_name) { \ |
| 639 | doc::test(documentation); \ |
| 640 | code; \ |
| 641 | } \ |
| 642 | TEST_P(OutputStreamTest, test_name) { \ |
| 643 | doc::test(documentation); \ |
| 644 | code; \ |
| 645 | } |
| 646 | |
| 647 | TEST_IO_STREAM(GetFrameCount, "Check that the stream frame count == the one it was opened with", |
| 648 | ASSERT_EQ(audioConfig.frameCount, extract(stream->getFrameCount()))) |
| 649 | |
| 650 | TEST_IO_STREAM(GetSampleRate, "Check that the stream sample rate == the one it was opened with", |
| 651 | ASSERT_EQ(audioConfig.sampleRateHz, extract(stream->getSampleRate()))) |
| 652 | |
| 653 | TEST_IO_STREAM(GetChannelMask, "Check that the stream channel mask == the one it was opened with", |
| 654 | ASSERT_EQ(audioConfig.channelMask, extract(stream->getChannelMask()))) |
| 655 | |
| 656 | TEST_IO_STREAM(GetFormat, "Check that the stream format == the one it was opened with", |
| 657 | ASSERT_EQ(audioConfig.format, extract(stream->getFormat()))) |
| 658 | |
| 659 | // TODO: for now only check that the framesize is not incoherent |
| 660 | TEST_IO_STREAM(GetFrameSize, "Check that the stream frame size == the one it was opened with", |
| 661 | ASSERT_GT(extract(stream->getFrameSize()), 0U)) |
| 662 | |
| 663 | TEST_IO_STREAM(GetBufferSize, "Check that the stream buffer size== the one it was opened with", |
| 664 | ASSERT_GE(extract(stream->getBufferSize()), \ |
| 665 | extract(stream->getFrameSize()))); |
| 666 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 667 | template <class Property, class CapabilityGetter, class Getter, class Setter> |
Kevin Rocard | a7df7fc | 2017-03-10 18:37:46 -0800 | [diff] [blame] | 668 | static void testCapabilityGetter(const string& name, IStream* stream, Property currentValue, |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 669 | CapabilityGetter capablityGetter, Getter getter, Setter setter) { |
| 670 | hidl_vec<Property> capabilities; |
| 671 | ASSERT_OK((stream->*capablityGetter)(returnIn(capabilities))); |
| 672 | if (capabilities.size() == 0) { |
| 673 | // The default hal should probably return a NOT_SUPPORTED if the hal does not expose |
| 674 | // capability retrieval. For now it returns an empty list if not implemented |
| 675 | doc::partialTest(name + " is not supported"); |
| 676 | return; |
| 677 | }; |
| 678 | // TODO: This code has never been tested on a hal that supports getSupportedSampleRates |
| 679 | EXPECT_NE(std::find(capabilities.begin(), capabilities.end(), currentValue), |
| 680 | capabilities.end()) |
| 681 | << "current " << name << " is not in the list of the supported ones " |
| 682 | << toString(capabilities); |
| 683 | |
| 684 | // Check that all declared supported values are indeed supported |
| 685 | for (auto capability : capabilities) { |
| 686 | ASSERT_OK((stream->*setter)(capability)); |
| 687 | ASSERT_EQ(capability, extract((stream->*getter)())); |
| 688 | } |
| 689 | } |
| 690 | |
Kevin Rocard | a7df7fc | 2017-03-10 18:37:46 -0800 | [diff] [blame] | 691 | TEST_IO_STREAM(SupportedSampleRate, "Check that the stream sample rate is declared as supported", |
| 692 | testCapabilityGetter("getSupportedSampleRate", stream.get(), \ |
| 693 | extract(stream->getSampleRate()), \ |
| 694 | &IStream::getSupportedSampleRates, \ |
| 695 | &IStream::getSampleRate, &IStream::setSampleRate)) |
| 696 | |
| 697 | TEST_IO_STREAM(SupportedChannelMask, "Check that the stream channel mask is declared as supported", |
| 698 | testCapabilityGetter("getSupportedChannelMask", stream.get(), \ |
| 699 | extract(stream->getChannelMask()), \ |
| 700 | &IStream::getSupportedChannelMasks, \ |
| 701 | &IStream::getChannelMask, &IStream::setChannelMask)) |
| 702 | |
| 703 | TEST_IO_STREAM(SupportedFormat, "Check that the stream format is declared as supported", |
| 704 | testCapabilityGetter("getSupportedFormat", stream.get(), \ |
| 705 | extract(stream->getFormat()), \ |
| 706 | &IStream::getSupportedFormats, \ |
| 707 | &IStream::getFormat, &IStream::setFormat)) |
| 708 | |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 709 | static void testGetDevice(IStream* stream, AudioDevice expectedDevice) { |
| 710 | auto ret = stream->getDevice(); |
| 711 | ASSERT_TRUE(ret.isOk()); |
| 712 | AudioDevice device = ret; |
| 713 | ASSERT_EQ(expectedDevice, device); |
| 714 | } |
| 715 | |
| 716 | TEST_IO_STREAM(GetDevice, "Check that the stream device == the one it was opened with", |
| 717 | areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported") : \ |
| 718 | testGetDevice(stream.get(), address.device)) |
| 719 | |
| 720 | static void testSetDevice(IStream* stream, const DeviceAddress& address) { |
| 721 | DeviceAddress otherAddress = address; |
| 722 | otherAddress.device = (address.device & AudioDevice::BIT_IN) == 0 ? |
| 723 | AudioDevice::OUT_SPEAKER : AudioDevice::IN_BUILTIN_MIC; |
| 724 | EXPECT_OK(stream->setDevice(otherAddress)); |
| 725 | |
| 726 | ASSERT_OK(stream->setDevice(address)); // Go back to the original value |
| 727 | } |
| 728 | |
| 729 | TEST_IO_STREAM(SetDevice, "Check that the stream can be rerouted to SPEAKER or BUILTIN_MIC", |
| 730 | areAudioPatchesSupported() ? doc::partialTest("Audio patches are supported") : \ |
| 731 | testSetDevice(stream.get(), address)) |
| 732 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 733 | static void testGetAudioProperties(IStream* stream, AudioConfig expectedConfig) { |
| 734 | uint32_t sampleRateHz; |
| 735 | AudioChannelMask mask; |
| 736 | AudioFormat format; |
| 737 | |
| 738 | stream->getAudioProperties(returnIn(sampleRateHz, mask, format)); |
| 739 | |
| 740 | // FIXME: the qcom hal it does not currently negotiate the sampleRate & channel mask |
Kevin Rocard | d1e98ae | 2017-03-08 17:17:25 -0800 | [diff] [blame] | 741 | EXPECT_EQ(expectedConfig.sampleRateHz, sampleRateHz); |
| 742 | EXPECT_EQ(expectedConfig.channelMask, mask); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 743 | EXPECT_EQ(expectedConfig.format, format); |
| 744 | } |
| 745 | |
Kevin Rocard | a7df7fc | 2017-03-10 18:37:46 -0800 | [diff] [blame] | 746 | TEST_IO_STREAM(GetAudioProperties, |
| 747 | "Check that the stream audio properties == the ones it was opened with", |
| 748 | testGetAudioProperties(stream.get(), audioConfig)) |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 749 | |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 750 | static void testConnectedState(IStream* stream) { |
| 751 | DeviceAddress address = {}; |
| 752 | using AD = AudioDevice; |
| 753 | for (auto device : {AD::OUT_HDMI, AD::OUT_WIRED_HEADPHONE, AD::IN_USB_HEADSET}) { |
| 754 | address.device = device; |
| 755 | |
| 756 | ASSERT_OK(stream->setConnectedState(address, true)); |
| 757 | ASSERT_OK(stream->setConnectedState(address, false)); |
| 758 | } |
| 759 | } |
| 760 | TEST_IO_STREAM(SetConnectedState, |
| 761 | "Check that the stream can be notified of device connection and deconnection", |
| 762 | testConnectedState(stream.get())) |
| 763 | |
| 764 | |
| 765 | static auto invalidArgsOrNotSupported = {Result::INVALID_ARGUMENTS, Result::NOT_SUPPORTED}; |
| 766 | TEST_IO_STREAM(SetHwAvSync, "Try to set hardware sync to an invalid value", |
| 767 | ASSERT_RESULT(invalidArgsOrNotSupported, stream->setHwAvSync(666))) |
| 768 | |
| 769 | static void checkGetParameter(IStream* stream, hidl_vec<hidl_string> keys, |
| 770 | vector<Result> expectedResults) { |
| 771 | hidl_vec<ParameterValue> parameters; |
| 772 | Result res; |
| 773 | ASSERT_OK(stream->getParameters(keys, returnIn(res, parameters))); |
| 774 | ASSERT_RESULT(expectedResults, res); |
| 775 | if (res == Result::OK) { |
| 776 | ASSERT_EQ(0U, parameters.size()); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /* Get/Set parameter is intended to be an opaque channel between vendors app and their HALs. |
| 781 | * Thus can not be meaningfully tested. |
| 782 | * TODO: Doc missing. Should asking for an empty set of params raise an error ? |
| 783 | */ |
| 784 | TEST_IO_STREAM(getEmptySetParameter, "Retrieve the values of an empty set", |
| 785 | checkGetParameter(stream.get(), {} /* keys */, |
| 786 | {Result::OK, Result::INVALID_ARGUMENTS})) |
| 787 | |
| 788 | |
| 789 | TEST_IO_STREAM(getNonExistingParameter, "Retrieve the values of an non existing parameter", |
| 790 | checkGetParameter(stream.get(), {"Non existing key"} /* keys */, |
| 791 | {Result::INVALID_ARGUMENTS})) |
| 792 | |
| 793 | static vector<Result> okOrNotSupported = {Result::OK, Result::INVALID_ARGUMENTS}; |
| 794 | TEST_IO_STREAM(setEmptySetParameter, "Set the values of an empty set of parameters", |
| 795 | ASSERT_RESULT(okOrNotSupported, stream->setParameters({}))) |
| 796 | |
| 797 | TEST_IO_STREAM(setNonExistingParameter, "Set the values of an non existing parameter", |
| 798 | ASSERT_RESULT(Result::INVALID_ARGUMENTS, |
| 799 | stream->setParameters({{"non existing key", "0"}}))) |
| 800 | |
Kevin Rocard | b903124 | 2017-03-13 12:20:54 -0700 | [diff] [blame] | 801 | TEST_IO_STREAM(DebugDump, |
| 802 | "Check that a stream can dump its state without error", |
| 803 | testDebugDump([this](const auto& handle){ return stream->debugDump(handle); })) |
| 804 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 805 | ////////////////////////////////////////////////////////////////////////////// |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 806 | ////////////////////////////// addRemoveEffect /////////////////////////////// |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 807 | ////////////////////////////////////////////////////////////////////////////// |
| 808 | |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 809 | TEST_IO_STREAM(AddNonExistingEffect, "Adding a non existing effect should fail", |
| 810 | ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->addEffect(666))) |
| 811 | TEST_IO_STREAM(RemoveNonExistingEffect, "Removing a non existing effect should fail", |
| 812 | ASSERT_RESULT(Result::INVALID_ARGUMENTS, stream->removeEffect(666))) |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 813 | |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 814 | //TODO: positive tests |
| 815 | |
| 816 | ////////////////////////////////////////////////////////////////////////////// |
| 817 | /////////////////////////////// Control //////////////////////////////// |
| 818 | ////////////////////////////////////////////////////////////////////////////// |
| 819 | |
| 820 | TEST_IO_STREAM(standby, "Make sure the stream can be put in stanby", |
| 821 | ASSERT_OK(stream->standby())) // can not fail |
| 822 | |
| 823 | static vector<Result> invalidStateOrNotSupported = {Result::INVALID_STATE, Result::NOT_SUPPORTED}; |
| 824 | |
| 825 | TEST_IO_STREAM(startNoMmap, "Starting a mmaped stream before mapping it should fail", |
| 826 | ASSERT_RESULT(invalidStateOrNotSupported, stream->start())) |
| 827 | |
| 828 | TEST_IO_STREAM(stopNoMmap, "Stopping a mmaped stream before mapping it should fail", |
| 829 | ASSERT_RESULT(invalidStateOrNotSupported, stream->stop())) |
| 830 | |
| 831 | TEST_IO_STREAM(getMmapPositionNoMmap, "Get a stream Mmap position before mapping it should fail", |
| 832 | ASSERT_RESULT(invalidStateOrNotSupported, stream->stop())) |
| 833 | |
| 834 | TEST_IO_STREAM(close, "Make sure a stream can be closed", ASSERT_OK(closeStream())) |
| 835 | TEST_IO_STREAM(closeTwice, "Make sure a stream can not be closed twice", |
| 836 | ASSERT_OK(closeStream()); \ |
| 837 | ASSERT_RESULT(Result::INVALID_STATE, closeStream())) |
| 838 | |
| 839 | static void testCreateTooBigMmapBuffer(IStream* stream) { |
| 840 | MmapBufferInfo info; |
| 841 | Result res; |
| 842 | // Assume that int max is a value too big to be allocated |
| 843 | // This is true currently with a 32bit media server, but might not when it will run in 64 bit |
| 844 | auto minSizeFrames = std::numeric_limits<int32_t>::max(); |
| 845 | ASSERT_OK(stream->createMmapBuffer(minSizeFrames, returnIn(res, info))); |
| 846 | ASSERT_RESULT(invalidArgsOrNotSupported, res); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 847 | } |
| 848 | |
Kevin Rocard | 8878b4b | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 849 | TEST_IO_STREAM(CreateTooBigMmapBuffer, "Create mmap buffer too big should fail", |
| 850 | testCreateTooBigMmapBuffer(stream.get())) |
| 851 | |
| 852 | |
| 853 | static void testGetMmapPositionOfNonMmapedStream(IStream* stream) { |
| 854 | Result res; |
| 855 | MmapPosition position; |
| 856 | ASSERT_OK(stream->getMmapPosition(returnIn(res, position))); |
| 857 | ASSERT_RESULT(invalidArgsOrNotSupported, res); |
| 858 | } |
| 859 | |
| 860 | TEST_IO_STREAM(GetMmapPositionOfNonMmapedStream, |
| 861 | "Retrieving the mmap position of a non mmaped stream should fail", |
| 862 | testGetMmapPositionOfNonMmapedStream(stream.get())) |
| 863 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 864 | ////////////////////////////////////////////////////////////////////////////// |
Kevin Rocard | c996352 | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 865 | ///////////////////////////////// StreamIn /////////////////////////////////// |
| 866 | ////////////////////////////////////////////////////////////////////////////// |
| 867 | |
| 868 | TEST_P(InputStreamTest, GetAudioSource) { |
| 869 | doc::test("Retrieving the audio source of an input stream should always succeed"); |
| 870 | AudioSource source; |
| 871 | ASSERT_OK(stream->getAudioSource(returnIn(res, source))); |
| 872 | ASSERT_OK(res); |
| 873 | ASSERT_EQ(AudioSource::DEFAULT, source); |
| 874 | } |
| 875 | |
| 876 | static void testUnitaryGain(std::function<Return<Result> (float)> setGain) { |
| 877 | for (float value : {0.0, 0.01, 0.5, 0.09, 1.0}) { |
| 878 | SCOPED_TRACE("value=" + to_string(value)); |
| 879 | ASSERT_OK(setGain(value)); |
| 880 | } |
| 881 | for (float value : (float[]){-INFINITY,-1.0, -0.0, |
| 882 | 1.0 + std::numeric_limits<float>::epsilon(), 2.0, INFINITY, |
| 883 | NAN}) { |
| 884 | SCOPED_TRACE("value=" + to_string(value)); |
| 885 | // FIXME: NAN should never be accepted |
| 886 | // FIXME: Missing api doc. What should the impl do if the volume is outside [0,1] ? |
| 887 | ASSERT_RESULT(Result::INVALID_ARGUMENTS, setGain(value)); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | TEST_P(InputStreamTest, SetGain) { |
| 892 | doc::test("The gain of an input stream should only be set between [0,1]"); |
| 893 | testUnitaryGain([this](float volume) { return stream->setGain(volume); }); |
| 894 | } |
| 895 | |
| 896 | static void testPrepareForReading(IStreamIn* stream, uint32_t frameSize, uint32_t framesCount) { |
| 897 | Result res; |
| 898 | // Ignore output parameters as the call should fail |
| 899 | ASSERT_OK(stream->prepareForReading(frameSize, framesCount, |
| 900 | [&res](auto r, auto&, auto&, auto&, auto&) { res = r; })); |
| 901 | EXPECT_RESULT(invalidArgsOrNotSupported, res); |
| 902 | } |
| 903 | |
| 904 | TEST_P(InputStreamTest, PrepareForReadingWithHugeBuffer) { |
| 905 | doc::test("Preparing a stream for reading with a 2^32 sized buffer should fail"); |
| 906 | testPrepareForReading(stream.get(), 1, std::numeric_limits<uint32_t>::max()); |
| 907 | } |
| 908 | |
| 909 | TEST_P(InputStreamTest, PrepareForReadingCheckOverflow) { |
| 910 | doc::test("Preparing a stream for reading with a overflowing sized buffer should fail"); |
| 911 | auto uintMax = std::numeric_limits<uint32_t>::max(); |
| 912 | testPrepareForReading(stream.get(), uintMax, uintMax); |
| 913 | } |
| 914 | |
| 915 | TEST_P(InputStreamTest, getCapturePosition) { |
| 916 | doc::test("The capture position of a non prepared stream should not be retrievable"); |
| 917 | uint64_t frames; |
| 918 | uint64_t time; |
| 919 | ASSERT_OK(stream->getCapturePosition(returnIn(res, frames, time))); |
| 920 | ASSERT_RESULT(invalidStateOrNotSupported, res); |
| 921 | } |
| 922 | |
| 923 | ////////////////////////////////////////////////////////////////////////////// |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 924 | /////////////////////////////// PrimaryDevice //////////////////////////////// |
| 925 | ////////////////////////////////////////////////////////////////////////////// |
| 926 | |
Kevin Rocard | c996352 | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 927 | |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 928 | TEST_F(AudioPrimaryHidlTest, setVoiceVolume) { |
| 929 | doc::test("Make sure setVoiceVolume only succeed if volume is in [0,1]"); |
Kevin Rocard | c996352 | 2017-03-10 18:47:37 -0800 | [diff] [blame] | 930 | testUnitaryGain([this](float volume) { return device->setVoiceVolume(volume); }); |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | TEST_F(AudioPrimaryHidlTest, setMode) { |
| 934 | doc::test("Make sure setMode always succeeds if mode is valid"); |
| 935 | for (AudioMode mode : {AudioMode::IN_CALL, AudioMode::IN_COMMUNICATION, |
| 936 | AudioMode::RINGTONE, AudioMode::CURRENT, |
| 937 | AudioMode::NORMAL /* Make sure to leave the test in normal mode */ }) { |
| 938 | SCOPED_TRACE("mode=" + toString(mode)); |
| 939 | ASSERT_OK(device->setMode(mode)); |
| 940 | } |
| 941 | |
| 942 | // FIXME: Missing api doc. What should the impl do if the mode is invalid ? |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 943 | ASSERT_RESULT(Result::INVALID_ARGUMENTS, device->setMode(AudioMode::INVALID)); |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | |
| 947 | TEST_F(BoolAccessorPrimaryHidlTest, BtScoNrecEnabled) { |
| 948 | doc::test("Query and set the BT SCO NR&EC state"); |
| 949 | testOptionalAccessors("BtScoNrecEnabled", {true, false, true}, |
| 950 | &IPrimaryDevice::setBtScoNrecEnabled, |
| 951 | &IPrimaryDevice::getBtScoNrecEnabled); |
| 952 | } |
| 953 | |
| 954 | TEST_F(BoolAccessorPrimaryHidlTest, setGetBtScoWidebandEnabled) { |
| 955 | doc::test("Query and set the SCO whideband state"); |
| 956 | testOptionalAccessors("BtScoWideband", {true, false, true}, |
| 957 | &IPrimaryDevice::setBtScoWidebandEnabled, |
| 958 | &IPrimaryDevice::getBtScoWidebandEnabled); |
| 959 | } |
| 960 | |
| 961 | using TtyModeAccessorPrimaryHidlTest = AccessorPrimaryHidlTest<TtyMode>; |
| 962 | TEST_F(TtyModeAccessorPrimaryHidlTest, setGetTtyMode) { |
| 963 | doc::test("Query and set the TTY mode state"); |
| 964 | testOptionalAccessors("TTY mode", {TtyMode::OFF, TtyMode::HCO, TtyMode::VCO, TtyMode::FULL}, |
| 965 | &IPrimaryDevice::setTtyMode, &IPrimaryDevice::getTtyMode); |
| 966 | } |
| 967 | |
| 968 | TEST_F(BoolAccessorPrimaryHidlTest, setGetHac) { |
| 969 | doc::test("Query and set the HAC state"); |
| 970 | testAccessors("HAC", {true, false, true}, |
| 971 | &IPrimaryDevice::setHacEnabled, |
| 972 | &IPrimaryDevice::getHacEnabled); |
| 973 | } |
| 974 | |
| 975 | ////////////////////////////////////////////////////////////////////////////// |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 976 | //////////////////// Clean caches on global tear down //////////////////////// |
| 977 | ////////////////////////////////////////////////////////////////////////////// |
| 978 | |
| 979 | int main(int argc, char** argv) { |
| 980 | environment = new Environment; |
| 981 | ::testing::AddGlobalTestEnvironment(environment); |
| 982 | ::testing::InitGoogleTest(&argc, argv); |
| 983 | int status = RUN_ALL_TESTS(); |
| 984 | LOG(INFO) << "Test result = " << status; |
| 985 | return status; |
| 986 | } |