diff options
author | Yabin Cui <yabinc@google.com> | 2023-08-10 10:55:50 -0700 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2023-08-10 10:55:50 -0700 |
commit | 64c607b6100d63b714cec99c7ec6cc484c480636 (patch) | |
tree | 5c16677e163d0703e8a87904f33579a2c2be9539 | |
parent | 84fe033656e6f87d7f929bc2044d3fce49b1afe3 (diff) | |
download | extras-64c607b6100d63b714cec99c7ec6cc484c480636.tar.gz |
simpleperf: Check device connection in app_profiler.py
Bug: 293819830
Test: run test.py -p *test_device_not_connected
Change-Id: I8306b2e536a8cac567265a92f7ddfcbd28922d74
-rwxr-xr-x | simpleperf/scripts/app_profiler.py | 6 | ||||
-rw-r--r-- | simpleperf/scripts/test/app_profiler_test.py | 9 |
2 files changed, 13 insertions, 2 deletions
diff --git a/simpleperf/scripts/app_profiler.py b/simpleperf/scripts/app_profiler.py index a6399ef1..1ab7e15a 100755 --- a/simpleperf/scripts/app_profiler.py +++ b/simpleperf/scripts/app_profiler.py @@ -194,6 +194,8 @@ class ProfilerBase(object): def __init__(self, args): self.args = args self.adb = AdbHelper(enable_switch_to_root=not args.disable_adb_root) + if not self.adb.is_device_available(): + log_exit('No Android device is connected via ADB.') self.is_root_device = self.adb.switch_to_root() self.android_version = self.adb.get_android_version() if self.android_version < 7: @@ -335,10 +337,10 @@ class AppProfiler(ProfilerBase): result, ps_output = self.adb.run_and_return_output( ['shell', 'ps', '-p', pid, '-o', 'USER']) if not result: - return None + return None uid = SHELL_PS_UID_PATTERN.search(ps_output).group(1) if uid == current_user.strip(): - return int(pid) + return int(pid) return None def run_in_app_dir(self, args): diff --git a/simpleperf/scripts/test/app_profiler_test.py b/simpleperf/scripts/test/app_profiler_test.py index 9fc4bcc5..b6b39ba8 100644 --- a/simpleperf/scripts/test/app_profiler_test.py +++ b/simpleperf/scripts/test/app_profiler_test.py @@ -16,6 +16,8 @@ from app_profiler import NativeLibDownloader import shutil +import subprocess +import sys from simpleperf_utils import str_to_bytes, bytes_to_str, remove from . test_utils import TestBase, TestHelper, INFERNO_SCRIPT @@ -53,6 +55,13 @@ class TestNativeProfiling(TestBase): return self.run_cmd(['app_profiler.py', '--system_wide', '-r', '--duration 1']) + def test_device_not_connected(self): + args = [sys.executable, TestHelper.script_path('app_profiler.py'), '-cmd', 'ls'] + proc = subprocess.run( + args, env={'ANDROID_SERIAL': 'not_exist_device'}, + stderr=subprocess.PIPE, text=True) + self.assertIn('No Android device is connected via ADB.', proc.stderr) + class TestNativeLibDownloader(TestBase): def setUp(self): |