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