Badhri Jagan Sridharan | aef9dec | 2021-12-27 14:02:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Probject |
| 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 "UsbAidlTest" |
| 18 | #include <android-base/logging.h> |
| 19 | |
| 20 | #include <aidl/android/hardware/usb/IUsb.h> |
| 21 | #include <aidl/android/hardware/usb/IUsbCallback.h> |
| 22 | #include <aidl/android/hardware/usb/BnUsbCallback.h> |
| 23 | #include <aidl/android/hardware/usb/PortDataRole.h> |
| 24 | #include <aidl/android/hardware/usb/PortMode.h> |
| 25 | #include <aidl/android/hardware/usb/PortPowerRole.h> |
| 26 | #include <aidl/android/hardware/usb/PortRole.h> |
| 27 | #include <aidl/android/hardware/usb/PortStatus.h> |
| 28 | #include <aidl/android/hardware/usb/Status.h> |
| 29 | #include <aidl/Vintf.h> |
| 30 | #include <aidl/Gtest.h> |
| 31 | |
| 32 | #include <android/binder_auto_utils.h> |
| 33 | #include <android/binder_manager.h> |
| 34 | #include <android/binder_process.h> |
| 35 | #include <gtest/gtest.h> |
| 36 | |
| 37 | #include <log/log.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <chrono> |
| 40 | #include <condition_variable> |
| 41 | #include <mutex> |
| 42 | |
| 43 | #define TIMEOUT_PERIOD 10 |
| 44 | |
RD Babiera | 6c94dde | 2022-11-09 22:55:47 +0000 | [diff] [blame] | 45 | using ::aidl::android::hardware::usb::AltModeData; |
Badhri Jagan Sridharan | aef9dec | 2021-12-27 14:02:56 -0800 | [diff] [blame] | 46 | using ::aidl::android::hardware::usb::BnUsbCallback; |
RD Babiera | 3a8b5ee | 2022-09-16 20:53:14 +0000 | [diff] [blame] | 47 | using ::aidl::android::hardware::usb::ComplianceWarning; |
RD Babiera | 6c94dde | 2022-11-09 22:55:47 +0000 | [diff] [blame] | 48 | using ::aidl::android::hardware::usb::DisplayPortAltModePinAssignment; |
| 49 | using ::aidl::android::hardware::usb::DisplayPortAltModeStatus; |
Badhri Jagan Sridharan | aef9dec | 2021-12-27 14:02:56 -0800 | [diff] [blame] | 50 | using ::aidl::android::hardware::usb::IUsb; |
| 51 | using ::aidl::android::hardware::usb::IUsbCallback; |
RD Babiera | 6c94dde | 2022-11-09 22:55:47 +0000 | [diff] [blame] | 52 | using ::aidl::android::hardware::usb::PlugOrientation; |
Badhri Jagan Sridharan | aef9dec | 2021-12-27 14:02:56 -0800 | [diff] [blame] | 53 | using ::aidl::android::hardware::usb::PortDataRole; |
| 54 | using ::aidl::android::hardware::usb::PortMode; |
| 55 | using ::aidl::android::hardware::usb::PortPowerRole; |
| 56 | using ::aidl::android::hardware::usb::PortRole; |
| 57 | using ::aidl::android::hardware::usb::PortStatus; |
| 58 | using ::aidl::android::hardware::usb::Status; |
Badhri Jagan Sridharan | a6aaccc | 2022-12-12 09:02:21 +0000 | [diff] [blame] | 59 | using ::aidl::android::hardware::usb::UsbDataStatus; |
Badhri Jagan Sridharan | aef9dec | 2021-12-27 14:02:56 -0800 | [diff] [blame] | 60 | |
| 61 | using ::ndk::ScopedAStatus; |
| 62 | using ::ndk::SpAIBinder; |
| 63 | using std::vector; |
| 64 | using std::shared_ptr; |
| 65 | using std::string; |
| 66 | |
| 67 | // The main test class for the USB aidl hal |
| 68 | class UsbAidlTest : public testing::TestWithParam<std::string> { |
| 69 | public: |
| 70 | // Callback class for the USB aidl hal. |
| 71 | // Usb Hal will call this object upon role switch or port query. |
| 72 | class UsbCallback : public BnUsbCallback { |
| 73 | UsbAidlTest& parent_; |
| 74 | int cookie; |
| 75 | |
| 76 | public: |
| 77 | UsbCallback(UsbAidlTest& parent, int cookie) |
| 78 | : parent_(parent), cookie(cookie){}; |
| 79 | |
| 80 | virtual ~UsbCallback() = default; |
| 81 | |
| 82 | // Callback method for the port status. |
| 83 | ScopedAStatus notifyPortStatusChange(const vector<PortStatus>& currentPortStatus, |
| 84 | Status retval) override { |
| 85 | if (retval == Status::SUCCESS && currentPortStatus.size() > 0) { |
| 86 | parent_.usb_last_port_status.portName = |
| 87 | currentPortStatus[0].portName.c_str(); |
| 88 | parent_.usb_last_port_status.currentDataRole = |
| 89 | currentPortStatus[0].currentDataRole; |
| 90 | parent_.usb_last_port_status.currentPowerRole = |
| 91 | currentPortStatus[0].currentPowerRole; |
| 92 | parent_.usb_last_port_status.currentMode = |
| 93 | currentPortStatus[0].currentMode; |
| 94 | } |
| 95 | parent_.usb_last_cookie = cookie; |
| 96 | return ScopedAStatus::ok(); |
| 97 | } |
| 98 | |
| 99 | // Callback method for the status of role switch operation. |
| 100 | ScopedAStatus notifyRoleSwitchStatus(const string& /*portName*/, const PortRole& newRole, |
| 101 | Status retval, int64_t transactionId) override { |
| 102 | parent_.usb_last_status = retval; |
| 103 | parent_.usb_last_cookie = cookie; |
| 104 | parent_.usb_last_port_role = newRole; |
| 105 | parent_.usb_role_switch_done = true; |
| 106 | parent_.last_transactionId = transactionId; |
| 107 | parent_.notify(); |
| 108 | return ScopedAStatus::ok(); |
| 109 | } |
| 110 | |
| 111 | // Callback method for the status of enableUsbData operation |
| 112 | ScopedAStatus notifyEnableUsbDataStatus(const string& /*portName*/, bool /*enable*/, |
| 113 | Status /*retval*/, int64_t transactionId) override { |
| 114 | parent_.last_transactionId = transactionId; |
| 115 | parent_.usb_last_cookie = cookie; |
| 116 | parent_.enable_usb_data_done = true; |
| 117 | parent_.notify(); |
| 118 | return ScopedAStatus::ok(); |
| 119 | } |
| 120 | |
Badhri Jagan Sridharan | f883235 | 2022-01-23 09:17:04 -0800 | [diff] [blame] | 121 | // Callback method for the status of enableUsbData operation |
| 122 | ScopedAStatus notifyEnableUsbDataWhileDockedStatus(const string& /*portName*/, |
| 123 | Status /*retval*/, |
| 124 | int64_t transactionId) override { |
| 125 | parent_.last_transactionId = transactionId; |
| 126 | parent_.usb_last_cookie = cookie; |
| 127 | parent_.enable_usb_data_while_docked_done = true; |
| 128 | parent_.notify(); |
| 129 | return ScopedAStatus::ok(); |
| 130 | } |
| 131 | |
Badhri Jagan Sridharan | aef9dec | 2021-12-27 14:02:56 -0800 | [diff] [blame] | 132 | // Callback method for the status of enableContaminantPresenceDetection |
| 133 | ScopedAStatus notifyContaminantEnabledStatus(const string& /*portName*/, bool /*enable*/, |
| 134 | Status /*retval*/, int64_t transactionId) override { |
| 135 | parent_.last_transactionId = transactionId; |
| 136 | parent_.usb_last_cookie = cookie; |
| 137 | parent_.enable_contaminant_done = true; |
| 138 | parent_.notify(); |
| 139 | return ScopedAStatus::ok(); |
| 140 | } |
| 141 | |
| 142 | // Callback method for the status of queryPortStatus operation |
| 143 | ScopedAStatus notifyQueryPortStatus(const string& /*portName*/, Status /*retval*/, |
| 144 | int64_t transactionId) override { |
| 145 | parent_.last_transactionId = transactionId; |
| 146 | parent_.notify(); |
| 147 | return ScopedAStatus::ok(); |
| 148 | } |
Badhri Jagan Sridharan | 6f67c56 | 2022-01-17 19:13:08 -0800 | [diff] [blame] | 149 | |
| 150 | // Callback method for the status of limitPowerTransfer operation |
| 151 | ScopedAStatus notifyLimitPowerTransferStatus(const string& /*portName*/, bool /*limit*/, |
| 152 | Status /*retval*/, int64_t transactionId) override { |
| 153 | parent_.last_transactionId = transactionId; |
| 154 | parent_.usb_last_cookie = cookie; |
| 155 | parent_.limit_power_transfer_done = true; |
| 156 | parent_.notify(); |
| 157 | return ScopedAStatus::ok(); |
| 158 | } |
Ricky Niu | a904040 | 2022-01-05 20:03:09 +0800 | [diff] [blame] | 159 | |
| 160 | // Callback method for the status of resetUsbPortStatus operation |
| 161 | ScopedAStatus notifyResetUsbPortStatus(const string& /*portName*/, Status /*retval*/, |
| 162 | int64_t transactionId) override { |
| 163 | ALOGI("enter notifyResetUsbPortStatus"); |
| 164 | parent_.last_transactionId = transactionId; |
| 165 | parent_.usb_last_cookie = cookie; |
| 166 | parent_.reset_usb_port_done = true; |
| 167 | parent_.notify(); |
| 168 | return ScopedAStatus::ok(); |
| 169 | } |
Badhri Jagan Sridharan | aef9dec | 2021-12-27 14:02:56 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | virtual void SetUp() override { |
| 173 | ALOGI("Setup"); |
| 174 | usb = IUsb::fromBinder( |
| 175 | SpAIBinder(AServiceManager_waitForService(GetParam().c_str()))); |
| 176 | ASSERT_NE(usb, nullptr); |
| 177 | |
| 178 | usb_cb_2 = ::ndk::SharedRefBase::make<UsbCallback>(*this, 2); |
| 179 | ASSERT_NE(usb_cb_2, nullptr); |
| 180 | const auto& ret = usb->setCallback(usb_cb_2); |
| 181 | ASSERT_TRUE(ret.isOk()); |
| 182 | } |
| 183 | |
| 184 | virtual void TearDown() override { ALOGI("Teardown"); } |
| 185 | |
| 186 | // Used as a mechanism to inform the test about data/event callback. |
| 187 | inline void notify() { |
| 188 | std::unique_lock<std::mutex> lock(usb_mtx); |
| 189 | usb_count++; |
| 190 | usb_cv.notify_one(); |
| 191 | } |
| 192 | |
| 193 | // Test code calls this function to wait for data/event callback. |
| 194 | inline std::cv_status wait() { |
| 195 | std::unique_lock<std::mutex> lock(usb_mtx); |
| 196 | |
| 197 | std::cv_status status = std::cv_status::no_timeout; |
| 198 | auto now = std::chrono::system_clock::now(); |
| 199 | while (usb_count == 0) { |
| 200 | status = |
| 201 | usb_cv.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD)); |
| 202 | if (status == std::cv_status::timeout) { |
| 203 | ALOGI("timeout"); |
| 204 | return status; |
| 205 | } |
| 206 | } |
| 207 | usb_count--; |
| 208 | return status; |
| 209 | } |
| 210 | |
| 211 | // USB aidl hal Proxy |
| 212 | shared_ptr<IUsb> usb; |
| 213 | |
| 214 | // Callback objects for usb aidl |
| 215 | // Methods of these objects are called to notify port status updates. |
| 216 | shared_ptr<IUsbCallback> usb_cb_1, usb_cb_2; |
| 217 | |
| 218 | // The last conveyed status of the USB ports. |
| 219 | // Stores information of currentt_data_role, power_role for all the USB ports |
| 220 | PortStatus usb_last_port_status; |
| 221 | |
| 222 | // Status of the last role switch operation. |
| 223 | Status usb_last_status; |
| 224 | |
| 225 | // Port role information of the last role switch operation. |
| 226 | PortRole usb_last_port_role; |
| 227 | |
| 228 | // Flag to indicate the invocation of role switch callback. |
| 229 | bool usb_role_switch_done; |
| 230 | |
| 231 | // Flag to indicate the invocation of notifyContaminantEnabledStatus callback. |
| 232 | bool enable_contaminant_done; |
| 233 | |
| 234 | // Flag to indicate the invocation of notifyEnableUsbDataStatus callback. |
| 235 | bool enable_usb_data_done; |
| 236 | |
Badhri Jagan Sridharan | f883235 | 2022-01-23 09:17:04 -0800 | [diff] [blame] | 237 | // Flag to indicate the invocation of notifyEnableUsbDataWhileDockedStatus callback. |
| 238 | bool enable_usb_data_while_docked_done; |
| 239 | |
Badhri Jagan Sridharan | 6f67c56 | 2022-01-17 19:13:08 -0800 | [diff] [blame] | 240 | // Flag to indicate the invocation of notifyLimitPowerTransferStatus callback. |
| 241 | bool limit_power_transfer_done; |
| 242 | |
Ricky Niu | a904040 | 2022-01-05 20:03:09 +0800 | [diff] [blame] | 243 | // Flag to indicate the invocation of notifyResetUsbPort callback. |
| 244 | bool reset_usb_port_done; |
| 245 | |
Badhri Jagan Sridharan | aef9dec | 2021-12-27 14:02:56 -0800 | [diff] [blame] | 246 | // Stores the cookie of the last invoked usb callback object. |
| 247 | int usb_last_cookie; |
| 248 | |
| 249 | // Last transaction ID that was recorded. |
| 250 | int64_t last_transactionId; |
| 251 | // synchronization primitives to coordinate between main test thread |
| 252 | // and the callback thread. |
| 253 | std::mutex usb_mtx; |
| 254 | std::condition_variable usb_cv; |
| 255 | int usb_count = 0; |
| 256 | }; |
| 257 | |
| 258 | /* |
| 259 | * Test to see if setCallback succeeds. |
| 260 | * Callback object is created and registered. |
| 261 | */ |
| 262 | TEST_P(UsbAidlTest, setCallback) { |
| 263 | ALOGI("UsbAidlTest setCallback start"); |
| 264 | usb_cb_1 = ::ndk::SharedRefBase::make<UsbCallback>(*this, 1); |
| 265 | ASSERT_NE(usb_cb_1, nullptr); |
| 266 | const auto& ret = usb->setCallback(usb_cb_1); |
| 267 | ASSERT_TRUE(ret.isOk()); |
| 268 | ALOGI("UsbAidlTest setCallback end"); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Check to see if querying type-c |
| 273 | * port status succeeds. |
| 274 | * The callback parameters are checked to see if the transaction id |
| 275 | * matches. |
| 276 | */ |
| 277 | TEST_P(UsbAidlTest, queryPortStatus) { |
| 278 | ALOGI("UsbAidlTest queryPortStatus start"); |
| 279 | int64_t transactionId = rand() % 10000; |
| 280 | const auto& ret = usb->queryPortStatus(transactionId); |
| 281 | ASSERT_TRUE(ret.isOk()); |
| 282 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 283 | EXPECT_EQ(2, usb_last_cookie); |
| 284 | EXPECT_EQ(transactionId, last_transactionId); |
| 285 | ALOGI("UsbAidlTest queryPortStatus end: %s", usb_last_port_status.portName.c_str()); |
| 286 | } |
| 287 | |
| 288 | /* |
Badhri Jagan Sridharan | a6aaccc | 2022-12-12 09:02:21 +0000 | [diff] [blame] | 289 | * Query port status to Check to see whether only one of DISABLED_DOCK, |
| 290 | * DISABLED_DOCK_DEVICE_MODE, DISABLED_DOCK_HOST_MODE is set at the most. |
| 291 | * The callback parameters are checked to see if the transaction id |
| 292 | * matches. |
| 293 | */ |
| 294 | TEST_P(UsbAidlTest, DisabledDataStatusCheck) { |
| 295 | int disabledCount = 0; |
| 296 | |
| 297 | ALOGI("UsbAidlTest DataStatusCheck start"); |
| 298 | int64_t transactionId = rand() % 10000; |
| 299 | const auto& ret = usb->queryPortStatus(transactionId); |
| 300 | ASSERT_TRUE(ret.isOk()); |
| 301 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 302 | EXPECT_EQ(2, usb_last_cookie); |
| 303 | EXPECT_EQ(transactionId, last_transactionId); |
| 304 | ALOGI("UsbAidlTest DataStatusCheck portName: %s", usb_last_port_status.portName.c_str()); |
| 305 | if (usb_last_port_status.usbDataStatus.size() > 1) { |
| 306 | for (UsbDataStatus dataStatus : usb_last_port_status.usbDataStatus) { |
| 307 | if (dataStatus == UsbDataStatus::DISABLED_DOCK || |
| 308 | dataStatus == UsbDataStatus::DISABLED_DOCK_DEVICE_MODE || |
| 309 | dataStatus == UsbDataStatus::DISABLED_DOCK_HOST_MODE) { |
| 310 | disabledCount++; |
| 311 | } |
| 312 | } |
| 313 | } |
Badhri Jagan Sridharan | 6dcb417 | 2023-01-04 22:45:03 +0000 | [diff] [blame] | 314 | EXPECT_GE(1, disabledCount); |
Badhri Jagan Sridharan | a6aaccc | 2022-12-12 09:02:21 +0000 | [diff] [blame] | 315 | ALOGI("UsbAidlTest DataStatusCheck end"); |
| 316 | } |
| 317 | |
| 318 | /* |
Badhri Jagan Sridharan | aef9dec | 2021-12-27 14:02:56 -0800 | [diff] [blame] | 319 | * Trying to switch a non-existent port should fail. |
| 320 | * This test case tried to switch the port with empty |
| 321 | * name which is expected to fail. |
| 322 | * The callback parameters are checked to see if the transaction id |
| 323 | * matches. |
| 324 | */ |
| 325 | TEST_P(UsbAidlTest, switchEmptyPort) { |
| 326 | ALOGI("UsbAidlTest switchEmptyPort start"); |
| 327 | PortRole role; |
| 328 | role.set<PortRole::powerRole>(PortPowerRole::SOURCE); |
| 329 | int64_t transactionId = rand() % 10000; |
| 330 | const auto& ret = usb->switchRole("", role, transactionId); |
| 331 | ASSERT_TRUE(ret.isOk()); |
| 332 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 333 | EXPECT_EQ(Status::ERROR, usb_last_status); |
| 334 | EXPECT_EQ(transactionId, last_transactionId); |
| 335 | EXPECT_EQ(2, usb_last_cookie); |
| 336 | ALOGI("UsbAidlTest switchEmptyPort end"); |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Test switching the power role of usb port. |
| 341 | * Test case queries the usb ports present in device. |
| 342 | * If there is at least one usb port, a power role switch |
| 343 | * to SOURCE is attempted for the port. |
| 344 | * The callback parameters are checked to see if the transaction id |
| 345 | * matches. |
| 346 | */ |
| 347 | TEST_P(UsbAidlTest, switchPowerRole) { |
| 348 | ALOGI("UsbAidlTest switchPowerRole start"); |
| 349 | PortRole role; |
| 350 | role.set<PortRole::powerRole>(PortPowerRole::SOURCE); |
| 351 | int64_t transactionId = rand() % 10000; |
| 352 | const auto& ret = usb->queryPortStatus(transactionId); |
| 353 | ASSERT_TRUE(ret.isOk()); |
| 354 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 355 | EXPECT_EQ(2, usb_last_cookie); |
| 356 | EXPECT_EQ(transactionId, last_transactionId); |
| 357 | |
| 358 | if (!usb_last_port_status.portName.empty()) { |
| 359 | string portBeingSwitched = usb_last_port_status.portName; |
| 360 | ALOGI("switchPower role portname:%s", portBeingSwitched.c_str()); |
| 361 | usb_role_switch_done = false; |
| 362 | transactionId = rand() % 10000; |
| 363 | const auto& ret = usb->switchRole(portBeingSwitched, role, transactionId); |
| 364 | ASSERT_TRUE(ret.isOk()); |
| 365 | |
| 366 | std::cv_status waitStatus = wait(); |
| 367 | while (waitStatus == std::cv_status::no_timeout && |
| 368 | usb_role_switch_done == false) |
| 369 | waitStatus = wait(); |
| 370 | |
| 371 | EXPECT_EQ(std::cv_status::no_timeout, waitStatus); |
| 372 | EXPECT_EQ(2, usb_last_cookie); |
| 373 | EXPECT_EQ(transactionId, last_transactionId); |
| 374 | } |
| 375 | ALOGI("UsbAidlTest switchPowerRole end"); |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Test switching the data role of usb port. |
| 380 | * Test case queries the usb ports present in device. |
| 381 | * If there is at least one usb port, a data role switch |
| 382 | * to device is attempted for the port. |
| 383 | * The callback parameters are checked to see if transaction id |
| 384 | * matches. |
| 385 | */ |
| 386 | TEST_P(UsbAidlTest, switchDataRole) { |
| 387 | ALOGI("UsbAidlTest switchDataRole start"); |
| 388 | PortRole role; |
| 389 | role.set<PortRole::dataRole>(PortDataRole::DEVICE); |
| 390 | int64_t transactionId = rand() % 10000; |
| 391 | const auto& ret = usb->queryPortStatus(transactionId); |
| 392 | ASSERT_TRUE(ret.isOk()); |
| 393 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 394 | EXPECT_EQ(2, usb_last_cookie); |
| 395 | EXPECT_EQ(transactionId, last_transactionId); |
| 396 | |
| 397 | if (!usb_last_port_status.portName.empty()) { |
| 398 | string portBeingSwitched = usb_last_port_status.portName; |
| 399 | ALOGI("portname:%s", portBeingSwitched.c_str()); |
| 400 | usb_role_switch_done = false; |
| 401 | transactionId = rand() % 10000; |
| 402 | const auto& ret = usb->switchRole(portBeingSwitched, role, transactionId); |
| 403 | ASSERT_TRUE(ret.isOk()); |
| 404 | |
| 405 | std::cv_status waitStatus = wait(); |
| 406 | while (waitStatus == std::cv_status::no_timeout && |
| 407 | usb_role_switch_done == false) |
| 408 | waitStatus = wait(); |
| 409 | |
| 410 | EXPECT_EQ(std::cv_status::no_timeout, waitStatus); |
| 411 | EXPECT_EQ(2, usb_last_cookie); |
| 412 | EXPECT_EQ(transactionId, last_transactionId); |
| 413 | } |
| 414 | ALOGI("UsbAidlTest switchDataRole end"); |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Test enabling contaminant presence detection of the port. |
| 419 | * Test case queries the usb ports present in device. |
| 420 | * If there is at least one usb port, enabling contaminant detection |
| 421 | * is attempted for the port. |
| 422 | * The callback parameters are checked to see if transaction id |
| 423 | * matches. |
| 424 | */ |
| 425 | TEST_P(UsbAidlTest, enableContaminantPresenceDetection) { |
| 426 | ALOGI("UsbAidlTest enableContaminantPresenceDetection start"); |
| 427 | int64_t transactionId = rand() % 10000; |
| 428 | const auto& ret = usb->queryPortStatus(transactionId); |
| 429 | ASSERT_TRUE(ret.isOk()); |
| 430 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 431 | EXPECT_EQ(2, usb_last_cookie); |
| 432 | EXPECT_EQ(transactionId, last_transactionId); |
| 433 | |
| 434 | if (!usb_last_port_status.portName.empty()) { |
| 435 | ALOGI("portname:%s", usb_last_port_status.portName.c_str()); |
| 436 | enable_contaminant_done = false; |
| 437 | transactionId = rand() % 10000; |
| 438 | const auto& ret = usb->enableContaminantPresenceDetection(usb_last_port_status.portName, |
| 439 | true, transactionId); |
| 440 | ASSERT_TRUE(ret.isOk()); |
| 441 | |
| 442 | std::cv_status waitStatus = wait(); |
| 443 | while (waitStatus == std::cv_status::no_timeout && |
| 444 | enable_contaminant_done == false) |
| 445 | waitStatus = wait(); |
| 446 | |
| 447 | EXPECT_EQ(std::cv_status::no_timeout, waitStatus); |
| 448 | EXPECT_EQ(2, usb_last_cookie); |
| 449 | EXPECT_EQ(transactionId, last_transactionId); |
| 450 | } |
| 451 | ALOGI("UsbAidlTest enableContaminantPresenceDetection end"); |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Test enabling Usb data of the port. |
| 456 | * Test case queries the usb ports present in device. |
| 457 | * If there is at least one usb port, enabling Usb data is attempted |
| 458 | * for the port. |
| 459 | * The callback parameters are checked to see if transaction id |
| 460 | * matches. |
| 461 | */ |
| 462 | TEST_P(UsbAidlTest, enableUsbData) { |
| 463 | ALOGI("UsbAidlTest enableUsbData start"); |
| 464 | int64_t transactionId = rand() % 10000; |
| 465 | const auto& ret = usb->queryPortStatus(transactionId); |
| 466 | ASSERT_TRUE(ret.isOk()); |
| 467 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 468 | EXPECT_EQ(2, usb_last_cookie); |
| 469 | EXPECT_EQ(transactionId, last_transactionId); |
| 470 | |
| 471 | if (!usb_last_port_status.portName.empty()) { |
| 472 | ALOGI("portname:%s", usb_last_port_status.portName.c_str()); |
| 473 | enable_usb_data_done = false; |
| 474 | transactionId = rand() % 10000; |
| 475 | const auto& ret = usb->enableUsbData(usb_last_port_status.portName, true, transactionId); |
| 476 | ASSERT_TRUE(ret.isOk()); |
| 477 | |
| 478 | std::cv_status waitStatus = wait(); |
| 479 | while (waitStatus == std::cv_status::no_timeout && |
| 480 | enable_usb_data_done == false) |
| 481 | waitStatus = wait(); |
| 482 | |
| 483 | EXPECT_EQ(std::cv_status::no_timeout, waitStatus); |
| 484 | EXPECT_EQ(2, usb_last_cookie); |
| 485 | EXPECT_EQ(transactionId, last_transactionId); |
| 486 | } |
| 487 | ALOGI("UsbAidlTest enableUsbData end"); |
| 488 | } |
| 489 | |
Badhri Jagan Sridharan | 6f67c56 | 2022-01-17 19:13:08 -0800 | [diff] [blame] | 490 | /* |
Badhri Jagan Sridharan | f883235 | 2022-01-23 09:17:04 -0800 | [diff] [blame] | 491 | * Test enabling Usb data while being docked. |
| 492 | * Test case queries the usb ports present in device. |
| 493 | * If there is at least one usb port, enabling Usb data while docked |
| 494 | * is attempted for the port. |
| 495 | * The callback parameters are checked to see if transaction id |
| 496 | * matches. |
| 497 | */ |
| 498 | TEST_P(UsbAidlTest, enableUsbDataWhileDocked) { |
| 499 | ALOGI("UsbAidlTest enableUsbDataWhileDocked start"); |
| 500 | int64_t transactionId = rand() % 10000; |
| 501 | const auto& ret = usb->queryPortStatus(transactionId); |
| 502 | ASSERT_TRUE(ret.isOk()); |
| 503 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 504 | EXPECT_EQ(2, usb_last_cookie); |
| 505 | EXPECT_EQ(transactionId, last_transactionId); |
| 506 | |
| 507 | if (!usb_last_port_status.portName.empty()) { |
| 508 | ALOGI("portname:%s", usb_last_port_status.portName.c_str()); |
| 509 | enable_usb_data_while_docked_done = false; |
| 510 | transactionId = rand() % 10000; |
| 511 | const auto& ret = usb->enableUsbDataWhileDocked(usb_last_port_status.portName, transactionId); |
| 512 | ASSERT_TRUE(ret.isOk()); |
| 513 | |
| 514 | std::cv_status waitStatus = wait(); |
| 515 | while (waitStatus == std::cv_status::no_timeout && |
| 516 | enable_usb_data_while_docked_done == false) |
| 517 | waitStatus = wait(); |
| 518 | |
| 519 | EXPECT_EQ(std::cv_status::no_timeout, waitStatus); |
| 520 | EXPECT_EQ(2, usb_last_cookie); |
| 521 | EXPECT_EQ(transactionId, last_transactionId); |
| 522 | } |
| 523 | ALOGI("UsbAidlTest enableUsbDataWhileDocked end"); |
| 524 | } |
| 525 | |
| 526 | /* |
Badhri Jagan Sridharan | 6f67c56 | 2022-01-17 19:13:08 -0800 | [diff] [blame] | 527 | * Test enabling Usb data of the port. |
| 528 | * Test case queries the usb ports present in device. |
| 529 | * If there is at least one usb port, relaxing limit power transfer |
| 530 | * is attempted for the port. |
| 531 | * The callback parameters are checked to see if transaction id |
| 532 | * matches. |
| 533 | */ |
| 534 | TEST_P(UsbAidlTest, limitPowerTransfer) { |
| 535 | ALOGI("UsbAidlTest limitPowerTransfer start"); |
| 536 | int64_t transactionId = rand() % 10000; |
| 537 | const auto& ret = usb->queryPortStatus(transactionId); |
| 538 | ASSERT_TRUE(ret.isOk()); |
| 539 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 540 | EXPECT_EQ(2, usb_last_cookie); |
| 541 | EXPECT_EQ(transactionId, last_transactionId); |
| 542 | |
| 543 | if (!usb_last_port_status.portName.empty()) { |
| 544 | ALOGI("portname:%s", usb_last_port_status.portName.c_str()); |
| 545 | limit_power_transfer_done = false; |
| 546 | transactionId = rand() % 10000; |
| 547 | const auto& ret = usb->limitPowerTransfer(usb_last_port_status.portName, false, transactionId); |
| 548 | ASSERT_TRUE(ret.isOk()); |
| 549 | |
| 550 | std::cv_status waitStatus = wait(); |
| 551 | while (waitStatus == std::cv_status::no_timeout && |
| 552 | limit_power_transfer_done == false) |
| 553 | waitStatus = wait(); |
| 554 | |
| 555 | EXPECT_EQ(std::cv_status::no_timeout, waitStatus); |
| 556 | EXPECT_EQ(2, usb_last_cookie); |
| 557 | EXPECT_EQ(transactionId, last_transactionId); |
| 558 | } |
| 559 | ALOGI("UsbAidlTest limitPowerTransfer end"); |
| 560 | } |
| 561 | |
Ricky Niu | a904040 | 2022-01-05 20:03:09 +0800 | [diff] [blame] | 562 | /* |
| 563 | * Test reset Usb data of the port. |
| 564 | * Test case queries the usb ports present in device. |
| 565 | * If there is at least one usb port, reset Usb data for the port. |
| 566 | * The callback parameters are checked to see if transaction id |
| 567 | * matches. |
| 568 | */ |
| 569 | TEST_P(UsbAidlTest, DISABLED_resetUsbPort) { |
| 570 | ALOGI("UsbAidlTest resetUsbPort start"); |
| 571 | int64_t transactionId = rand() % 10000; |
| 572 | const auto& ret = usb->queryPortStatus(transactionId); |
| 573 | ASSERT_TRUE(ret.isOk()); |
| 574 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 575 | EXPECT_EQ(2, usb_last_cookie); |
| 576 | EXPECT_EQ(transactionId, last_transactionId); |
| 577 | |
| 578 | if (!usb_last_port_status.portName.empty()) { |
| 579 | ALOGI("portname:%s", usb_last_port_status.portName.c_str()); |
| 580 | reset_usb_port_done = false; |
| 581 | transactionId = rand() % 10000; |
| 582 | const auto& ret = usb->resetUsbPort(usb_last_port_status.portName, transactionId); |
| 583 | ASSERT_TRUE(ret.isOk()); |
| 584 | ALOGI("UsbAidlTest resetUsbPort ret.isOk"); |
| 585 | |
| 586 | std::cv_status waitStatus = wait(); |
| 587 | while (waitStatus == std::cv_status::no_timeout && |
| 588 | reset_usb_port_done == false) |
| 589 | waitStatus = wait(); |
| 590 | |
| 591 | ALOGI("UsbAidlTest resetUsbPort wait()"); |
| 592 | EXPECT_EQ(std::cv_status::no_timeout, waitStatus); |
| 593 | EXPECT_EQ(2, usb_last_cookie); |
| 594 | EXPECT_EQ(transactionId, last_transactionId); |
| 595 | } |
| 596 | ALOGI("UsbAidlTest resetUsbPort end"); |
| 597 | } |
| 598 | |
RD Babiera | 3a8b5ee | 2022-09-16 20:53:14 +0000 | [diff] [blame] | 599 | /* |
| 600 | * Test charger compliance warning |
| 601 | * The test asserts that complianceWarnings is |
| 602 | * empty when the feature is not supported. i.e. |
| 603 | * supportsComplianceWarning is false. |
| 604 | */ |
| 605 | TEST_P(UsbAidlTest, nonCompliantChargerStatus) { |
| 606 | ALOGI("UsbAidlTest nonCompliantChargerStatus start"); |
| 607 | int64_t transactionId = rand() % 10000; |
| 608 | const auto& ret = usb->queryPortStatus(transactionId); |
| 609 | ASSERT_TRUE(ret.isOk()); |
| 610 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 611 | EXPECT_EQ(2, usb_last_cookie); |
| 612 | EXPECT_EQ(transactionId, last_transactionId); |
| 613 | |
| 614 | if (!usb_last_port_status.supportsComplianceWarnings) { |
| 615 | EXPECT_TRUE(usb_last_port_status.complianceWarnings.empty()); |
| 616 | } |
| 617 | |
| 618 | ALOGI("UsbAidlTest nonCompliantChargerStatus end"); |
| 619 | } |
| 620 | |
| 621 | /* |
| 622 | * Test charger compliance warning values |
| 623 | * The test asserts that complianceWarning values |
| 624 | * are valid. |
| 625 | */ |
| 626 | TEST_P(UsbAidlTest, nonCompliantChargerValues) { |
| 627 | ALOGI("UsbAidlTest nonCompliantChargerValues start"); |
| 628 | int64_t transactionId = rand() % 10000; |
| 629 | const auto& ret = usb->queryPortStatus(transactionId); |
| 630 | ASSERT_TRUE(ret.isOk()); |
| 631 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 632 | EXPECT_EQ(2, usb_last_cookie); |
| 633 | EXPECT_EQ(transactionId, last_transactionId); |
| 634 | |
| 635 | // Current compliance values range from [1, 4] |
| 636 | if (usb_last_port_status.supportsComplianceWarnings) { |
| 637 | for (auto warning : usb_last_port_status.complianceWarnings) { |
| 638 | EXPECT_TRUE((int)warning >= (int)ComplianceWarning::OTHER); |
| 639 | EXPECT_TRUE((int)warning <= (int)ComplianceWarning::MISSING_RP); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | ALOGI("UsbAidlTest nonCompliantChargerValues end"); |
| 644 | } |
| 645 | |
RD Babiera | 6c94dde | 2022-11-09 22:55:47 +0000 | [diff] [blame] | 646 | /* |
| 647 | * Test PlugOrientation Values are within range in PortStatus |
| 648 | */ |
| 649 | TEST_P(UsbAidlTest, plugOrientationValues) { |
| 650 | ALOGI("UsbAidlTest plugOrientationValues start"); |
| 651 | int64_t transactionId = rand() % 10000; |
| 652 | const auto& ret = usb->queryPortStatus(transactionId); |
| 653 | ASSERT_TRUE(ret.isOk()); |
| 654 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 655 | EXPECT_EQ(2, usb_last_cookie); |
| 656 | EXPECT_EQ(transactionId, last_transactionId); |
| 657 | |
| 658 | EXPECT_TRUE((int)usb_last_port_status.plugOrientation >= (int)PlugOrientation::UNKNOWN); |
| 659 | EXPECT_TRUE((int)usb_last_port_status.plugOrientation <= (int)PlugOrientation::PLUGGED_FLIPPED); |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | * Test DisplayPortAltMode Values when DisplayPort Alt Mode |
| 664 | * is active. |
| 665 | */ |
| 666 | TEST_P(UsbAidlTest, dpAltModeValues) { |
| 667 | ALOGI("UsbAidlTest dpAltModeValues start"); |
| 668 | int64_t transactionId = rand() % 10000; |
| 669 | const auto& ret = usb->queryPortStatus(transactionId); |
| 670 | ASSERT_TRUE(ret.isOk()); |
| 671 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 672 | EXPECT_EQ(2, usb_last_cookie); |
| 673 | EXPECT_EQ(transactionId, last_transactionId); |
| 674 | |
| 675 | // Discover DisplayPort Alt Mode |
| 676 | for (AltModeData altMode : usb_last_port_status.supportedAltModes) { |
| 677 | if (altMode.getTag() == AltModeData::displayPortAltModeData) { |
| 678 | AltModeData::DisplayPortAltModeData displayPortAltModeData = |
| 679 | altMode.get<AltModeData::displayPortAltModeData>(); |
| 680 | EXPECT_TRUE((int)displayPortAltModeData.partnerSinkStatus >= |
| 681 | (int)DisplayPortAltModeStatus::UNKNOWN); |
| 682 | EXPECT_TRUE((int)displayPortAltModeData.partnerSinkStatus <= |
| 683 | (int)DisplayPortAltModeStatus::ENABLED); |
| 684 | |
| 685 | EXPECT_TRUE((int)displayPortAltModeData.cableStatus >= |
| 686 | (int)DisplayPortAltModeStatus::UNKNOWN); |
| 687 | EXPECT_TRUE((int)displayPortAltModeData.cableStatus <= |
| 688 | (int)DisplayPortAltModeStatus::ENABLED); |
| 689 | |
| 690 | EXPECT_TRUE((int)displayPortAltModeData.pinAssignment >= |
| 691 | (int)DisplayPortAltModePinAssignment::NONE); |
| 692 | EXPECT_TRUE((int)displayPortAltModeData.pinAssignment <= |
| 693 | (int)DisplayPortAltModePinAssignment::F); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | ALOGI("UsbAidlTest dpAltModeValues end"); |
| 698 | } |
| 699 | |
Badhri Jagan Sridharan | aef9dec | 2021-12-27 14:02:56 -0800 | [diff] [blame] | 700 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(UsbAidlTest); |
| 701 | INSTANTIATE_TEST_SUITE_P( |
| 702 | PerInstance, UsbAidlTest, |
| 703 | testing::ValuesIn(::android::getAidlHalInstanceNames(IUsb::descriptor)), |
| 704 | ::android::PrintInstanceNameToString); |
| 705 | |
| 706 | int main(int argc, char** argv) { |
| 707 | ::testing::InitGoogleTest(&argc, argv); |
| 708 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 709 | ABinderProcess_startThreadPool(); |
| 710 | return RUN_ALL_TESTS(); |
| 711 | } |