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