From 13a9f53d989d27ffe6ab5cc7ce89030a62cec759 Mon Sep 17 00:00:00 2001 From: Jimi Chen Date: Tue, 15 Oct 2024 07:09:29 +0000 Subject: [PATCH] Refactor `is_packet_capture_supported()` by `expect_throws()` Test: atest NetworkStaticLibHostPythonTests Change-Id: Ibbc249e3d6ef182207ba3fe37c47bab7e099f2b0 --- staticlibs/testutils/host/python/apf_utils.py | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/staticlibs/testutils/host/python/apf_utils.py b/staticlibs/testutils/host/python/apf_utils.py index 7fe60bd3b7..55ac860982 100644 --- a/staticlibs/testutils/host/python/apf_utils.py +++ b/staticlibs/testutils/host/python/apf_utils.py @@ -182,22 +182,23 @@ def is_packet_capture_supported( ad: android_device.AndroidDevice, ) -> bool: - # Invoke the shell command with empty argument and see how NetworkStack respond. - # If supported, an IllegalArgumentException with help page will be printed. - functions_with_args = ( - # list all functions and args with (func, *args) tuple - (start_capture_packets, (ad, "")), - (stop_capture_packets, (ad, "")), - (get_matched_packet_counts, (ad, "", "")) - ) - - for func, args in functions_with_args: - try: - func(*args) - except UnsupportedOperationException: - return False - except Exception: - continue + try: + # Invoke the shell command with empty argument and see how NetworkStack respond. + # If supported, an IllegalArgumentException with help page will be printed. + assert_utils.expect_throws( + lambda: start_capture_packets(ad, ""), + assert_utils.UnexpectedBehaviorError + ) + assert_utils.expect_throws( + lambda: stop_capture_packets(ad, ""), + assert_utils.UnexpectedBehaviorError + ) + assert_utils.expect_throws( + lambda: get_matched_packet_counts(ad, "", ""), + assert_utils.UnexpectedBehaviorError + ) + except assert_utils.UnexpectedExceptionError: + return False # If no UnsupportOperationException is thrown, regard it as supported return True