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