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