diff --git a/mobly/controllers/android_device.py b/mobly/controllers/android_device.py index c0e4845a..b14d6d82 100644 --- a/mobly/controllers/android_device.py +++ b/mobly/controllers/android_device.py @@ -1042,15 +1042,15 @@ def take_bug_report( if begin_time is None: begin_time = mobly_logger.get_log_file_timestamp() - new_br = True + # In case device restarted, wait for boot completion before querying + # device properties or taking the bug report. + self.wait_for_boot_completion() + + # 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') @@ -1060,8 +1060,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..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) @@ -1297,6 +1303,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'),