From 6c4e6c930f76065d305f18923c3b7169e47c7227 Mon Sep 17 00:00:00 2001 From: Ang Li Date: Wed, 11 Nov 2026 20:12:11 +0000 Subject: [PATCH 1/2] Wait for boot completion before probing bugreportz in take_bug_report AndroidDevice.take_bug_report previously ran 'adb shell bugreportz -v' before calling wait_for_boot_completion(), causing recently rebooted devices to fail the probe and fall back to legacy 'adb bugreport'. --- mobly/controllers/android_device.py | 6 ++- .../mobly/controllers/android_device_test.py | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/mobly/controllers/android_device.py b/mobly/controllers/android_device.py index c0e4845a..043379b0 100644 --- a/mobly/controllers/android_device.py +++ b/mobly/controllers/android_device.py @@ -1042,6 +1042,10 @@ def take_bug_report( if begin_time is None: begin_time = mobly_logger.get_log_file_timestamp() + # In case device restarted, wait for boot completion before probing + # bugreportz or querying device properties. + self.wait_for_boot_completion() + new_br = True try: stdout = self.adb.shell('bugreportz -v').decode('utf-8') @@ -1060,8 +1064,6 @@ def take_bug_report( if new_br: filename = filename.replace('.txt', '.zip') full_out_path = os.path.join(br_path, filename) - # in case device restarted, wait for adb interface to return - self.wait_for_boot_completion() self.log.debug('Start taking bugreport.') if new_br: out = self.adb.shell('bugreportz', timeout=timeout).decode('utf-8') diff --git a/tests/mobly/controllers/android_device_test.py b/tests/mobly/controllers/android_device_test.py index 1486d590..b5c98d98 100755 --- a/tests/mobly/controllers/android_device_test.py +++ b/tests/mobly/controllers/android_device_test.py @@ -1297,6 +1297,55 @@ def test_AndroidDevice_take_bug_report_fallback( ), ) + @mock.patch( + 'mobly.controllers.android_device_lib.adb.AdbProxy', + return_value=mock_android_device.MockAdbProxy('1'), + ) + @mock.patch( + 'mobly.controllers.android_device_lib.fastboot.FastbootProxy', + return_value=mock_android_device.MockFastbootProxy('1'), + ) + @mock.patch('mobly.utils.create_dir') + def test_AndroidDevice_take_bug_report_waits_for_boot_before_probing( + self, create_dir_mock, FastbootProxy, MockAdbProxy + ): + """Verifies take_bug_report waits for boot completion before bugreportz.""" + ad = android_device.AndroidDevice(serial='1') + booted = False + orig_shell = ad.adb.shell + + def _wait_for_boot(): + nonlocal booted + booted = True + + def _shell_requiring_boot(params, timeout=None): + if not booted: + raise adb.AdbError( + ['adb', 'shell', params], b'', b'device offline', 1, '1' + ) + return orig_shell(params, timeout=timeout) + + with ( + mock.patch.object( + ad, 'wait_for_boot_completion', side_effect=_wait_for_boot + ) as mock_wait, + mock.patch.object(ad.adb, 'shell', side_effect=_shell_requiring_boot), + ): + output_path = ad.take_bug_report( + test_name='test_something', begin_time='sometime' + ) + + mock_wait.assert_called_once_with() + expected_path = os.path.join( + logging.log_path, 'AndroidDevice%s' % ad.serial, 'BugReports' + ) + self.assertEqual( + output_path, + os.path.join( + expected_path, 'bugreport,test_something,1,fakemodel,sometime.zip' + ), + ) + @mock.patch( 'mobly.controllers.android_device_lib.adb.AdbProxy', return_value=mock_android_device.MockAdbProxy('1'), From 044086d8ab8bbaaa2210782ac4601d2ba105609e Mon Sep 17 00:00:00 2001 From: Ang Li Date: Thu, 3 Dec 2026 03:08:14 +0000 Subject: [PATCH 2/2] Use build_version_sdk >= 24 instead of probing bugreportz -v --- mobly/controllers/android_device.py | 16 ++++++---------- tests/mobly/controllers/android_device_test.py | 10 ++++++++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/mobly/controllers/android_device.py b/mobly/controllers/android_device.py index 043379b0..b14d6d82 100644 --- a/mobly/controllers/android_device.py +++ b/mobly/controllers/android_device.py @@ -1042,19 +1042,15 @@ def take_bug_report( if begin_time is None: begin_time = mobly_logger.get_log_file_timestamp() - # In case device restarted, wait for boot completion before probing - # bugreportz or querying device properties. + # In case device restarted, wait for boot completion before querying + # device properties or taking the bug report. self.wait_for_boot_completion() - new_br = True + # bugreportz was introduced in Android 7.0 (API 24). try: - stdout = self.adb.shell('bugreportz -v').decode('utf-8') - # This check is necessary for builds before N, where adb shell's ret - # code and stderr are not propagated properly. - if 'not found' in stdout: - new_br = False - except adb.AdbError: - new_br = False + new_br = int(self.build_info['build_version_sdk']) >= 24 + except (KeyError, TypeError, ValueError): + new_br = True if destination is None: destination = os.path.join(self.log_path, 'BugReports') diff --git a/tests/mobly/controllers/android_device_test.py b/tests/mobly/controllers/android_device_test.py index b5c98d98..45641344 100755 --- a/tests/mobly/controllers/android_device_test.py +++ b/tests/mobly/controllers/android_device_test.py @@ -1268,7 +1268,13 @@ def test_AndroidDevice_take_bug_report_with_destination( @mock.patch( 'mobly.controllers.android_device_lib.adb.AdbProxy', - return_value=mock_android_device.MockAdbProxy('1', fail_br_before_N=True), + return_value=mock_android_device.MockAdbProxy( + '1', + mock_properties={ + **mock_android_device.DEFAULT_MOCK_PROPERTIES, + 'ro.build.version.sdk': '23', + }, + ), ) @mock.patch( 'mobly.controllers.android_device_lib.fastboot.FastbootProxy', @@ -1279,7 +1285,7 @@ def test_AndroidDevice_take_bug_report_fallback( self, create_dir_mock, FastbootProxy, MockAdbProxy ): """Verifies AndroidDevice.take_bug_report falls back to traditional - bugreport on builds that do not have bugreportz. + bugreport on builds before Android N (API 24). """ mock_serial = '1' ad = android_device.AndroidDevice(serial=mock_serial)