Skip to content

Commit 77deb38

Browse files
authored
Skip UiAutomation duplicate registration check if fail to get logcat (#81)
1 parent f6fb9e6 commit 77deb38

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

snippet_uiautomator/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def covert_to_millisecond(timeout: TimeUnit, ignore_error: bool = False) -> int:
5151
def get_latest_logcat_timestamp(ad: android_device.AndroidDevice) -> str:
5252
"""Gets the latest timestamp from logcat."""
5353
logcat = ad.adb.logcat(['-b', 'main', '-t', '1'])
54+
if not logcat:
55+
ad.log.error('Failed to get logcat, skip getting latest timestamp.')
56+
return ''
5457
last_line = logcat.splitlines()[-1]
5558
return re.findall(REGEX_LOGCAT_TIMESTAMP.encode(), last_line)[-1].decode()
5659

@@ -72,8 +75,12 @@ def is_uiautomator_service_registered(
7275
Args:
7376
ad: Mobly Android device controller.
7477
start_time: A timestamp that conforms to the REGEX_LOGCAT_TIMESTAMP format
75-
will only check the log after this time point.
78+
will only check the log after this time point. If empty, will skip the
79+
check.
7680
"""
81+
if not start_time:
82+
ad.log.error('Failed to get timestamp from logcat, skip checking.')
83+
return False
7784
logcat = ad.adb.logcat(['-d', '-s', 'AndroidRuntime:E'])
7885
runtime_errors = re.findall(
7986
REGEX_UIA_SERVICE_ALREADY_REGISTERED.encode(), logcat

tests/utils_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ def test_covert_to_millisecond_succeeds(timeout, expected):
3535
assert millisecond == expected
3636

3737

38+
def test_get_latest_logcat_timestamp_fails():
39+
mock_ad = mock.Mock()
40+
mock_ad.adb.logcat.return_value = b''
41+
42+
timestamp = utils.get_latest_logcat_timestamp(mock_ad)
43+
44+
assert timestamp == ''
45+
46+
3847
def test_get_latest_logcat_timestamp_succeeds():
3948
mock_ad = mock.Mock()
4049
mock_ad.adb.logcat.return_value = (
@@ -96,3 +105,13 @@ def test_is_uiautomator_service_registered_when_found_old_registered_error():
96105
is_registered = utils.is_uiautomator_service_registered(mock_ad, start_time)
97106

98107
assert not is_registered
108+
109+
110+
def test_is_uiautomator_service_registered_when_fail_to_get_logcat():
111+
start_time = ''
112+
mock_ad = mock.Mock()
113+
mock_ad.adb.logcat.return_value = b''
114+
115+
is_registered = utils.is_uiautomator_service_registered(mock_ad, start_time)
116+
117+
assert not is_registered

0 commit comments

Comments
 (0)