summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2023-08-10 18:39:36 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2023-08-10 18:39:36 +0000
commit2dc37afe5ea43cf347f8ad2b8cde997514b6390c (patch)
tree5c16677e163d0703e8a87904f33579a2c2be9539
parent84fe033656e6f87d7f929bc2044d3fce49b1afe3 (diff)
parent64c607b6100d63b714cec99c7ec6cc484c480636 (diff)
downloadextras-2dc37afe5ea43cf347f8ad2b8cde997514b6390c.tar.gz
Merge "simpleperf: Check device connection in app_profiler.py" into main
-rwxr-xr-xsimpleperf/scripts/app_profiler.py6
-rw-r--r--simpleperf/scripts/test/app_profiler_test.py9
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):