From 8f6bdc9eb50c9f5a08bfd4d2f1393be5e489e2b8 Mon Sep 17 00:00:00 2001 From: Spencer Low <CompareAndSwap@gmail.com> Date: Fri, 31 Aug 2018 19:49:46 -0700 Subject: [PATCH] adb: win32: test USB kick on resume from sleep/hibernation Unittest for a5b06b0ff8d7c99ce2678f61536cbf1430783dba. Works by simulating resume from sleep/hibernation by sending a Windows message. Test: python3 -m unittest test_adb.PowerTest Change-Id: I78510f30c012f68eda39764da522dbf8d03f2576 Signed-off-by: Spencer Low <CompareAndSwap@gmail.com> --- adb/test_adb.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/adb/test_adb.py b/adb/test_adb.py index cde2b22ea2..7e73818df7 100755 --- a/adb/test_adb.py +++ b/adb/test_adb.py @@ -490,6 +490,58 @@ class DisconnectionTest(unittest.TestCase): self.assertEqual(_devices(server_port), []) +@unittest.skipUnless(sys.platform == "win32", "requires Windows") +class PowerTest(unittest.TestCase): + def test_resume_usb_kick(self): + """Resuming from sleep/hibernate should kick USB devices.""" + try: + usb_serial = subprocess.check_output(["adb", "-d", "get-serialno"]).strip() + except subprocess.CalledProcessError: + # If there are multiple USB devices, we don't have a way to check whether the selected + # device is USB. + raise unittest.SkipTest('requires single USB device') + + try: + serial = subprocess.check_output(["adb", "get-serialno"]).strip() + except subprocess.CalledProcessError: + # Did you forget to select a device with $ANDROID_SERIAL? + raise unittest.SkipTest('requires $ANDROID_SERIAL set to a USB device') + + # Test only works with USB devices because adb _power_notification_thread does not kick + # non-USB devices on resume event. + if serial != usb_serial: + raise unittest.SkipTest('requires USB device') + + # Run an adb shell command in the background that takes a while to complete. + proc = subprocess.Popen(['adb', 'shell', 'sleep', '5']) + + # Wait for startup of adb server's _power_notification_thread. + time.sleep(0.1) + + # Simulate resuming from sleep/hibernation by sending Windows message. + import ctypes + from ctypes import wintypes + HWND_BROADCAST = 0xffff + WM_POWERBROADCAST = 0x218 + PBT_APMRESUMEAUTOMATIC = 0x12 + + PostMessageW = ctypes.windll.user32.PostMessageW + PostMessageW.restype = wintypes.BOOL + PostMessageW.argtypes = (wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM) + result = PostMessageW(HWND_BROADCAST, WM_POWERBROADCAST, PBT_APMRESUMEAUTOMATIC, 0) + if not result: + raise ctypes.WinError() + + # Wait for connection to adb shell to be broken by _power_notification_thread detecting the + # Windows message. + start = time.time() + proc.wait() + end = time.time() + + # If the power event was detected, the adb shell command should be broken very quickly. + self.assertLess(end - start, 2) + + def main(): """Main entrypoint.""" random.seed(0) -- GitLab