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