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