Merge "simpleperf: improve test.py."
am: 12bc7ee848

Change-Id: I9bab0ca51e3272b0aed5a5b305d2474e8f1e86e3
diff --git a/simpleperf/scripts/app_profiler.py b/simpleperf/scripts/app_profiler.py
index 48b0aef..a724e2d 100644
--- a/simpleperf/scripts/app_profiler.py
+++ b/simpleperf/scripts/app_profiler.py
@@ -116,21 +116,9 @@
 
     def _get_device_environment(self):
         self.is_root_device = self.adb.switch_to_root()
-
-        # Get android version.
-        build_version = self.adb.get_property('ro.build.version.release')
-        if build_version:
-            if not build_version[0].isdigit():
-                c = build_version[0].upper()
-                if c < 'L':
-                    self.android_version = 0
-                else:
-                    self.android_version = ord(c) - ord('L') + 5
-            else:
-                strs = build_version.split('.')
-                if strs:
-                    self.android_version = int(strs[0])
-
+        self.android_version = self.adb.get_android_version()
+        if self.android_version < 7:
+            log_warning("app_profiler.py is not tested prior Android N, please switch to use cmdline interface.")
         self.device_arch = self.adb.get_device_arch()
 
 
@@ -173,9 +161,8 @@
             else:
                 self.config['launch_activity'] = '.MainActivity'
 
-        pid = self._find_app_process()
-        if pid is not None:
-            self.run_in_app_dir(['kill', '-9', str(pid)])
+        self.adb.check_run(['shell', 'am', 'force-stop', self.config['app_package_name']])
+        while self._find_app_process():
             time.sleep(1)
 
         if self.config['profile_from_launch']:
@@ -195,8 +182,8 @@
                 log_exit("Can't start instrumentation test  %s" % self.config['launch_inst_test'])
 
         for i in range(10):
-            pid = self._find_app_process()
-            if pid is not None:
+            self.app_pid = self._find_app_process()
+            if self.app_pid is not None:
                 return
             time.sleep(1)
             log_info('Wait for the app process for %d seconds' % (i + 1))
diff --git a/simpleperf/scripts/test.py b/simpleperf/scripts/test.py
index ea66fee..bbee65a 100644
--- a/simpleperf/scripts/test.py
+++ b/simpleperf/scripts/test.py
@@ -68,7 +68,6 @@
         support_trace_offcpu = 'trace-offcpu' in output
     return support_trace_offcpu
 
-
 def build_testdata():
     """ Collect testdata from ../testdata and ../demo. """
     from_testdata_path = os.path.join('..', 'testdata')
@@ -481,7 +480,7 @@
         self.common_test_inferno()
         self.run_app_profiler()
         self.run_cmd([inferno_script, "-sc"])
-        self.check_inferno_report_html([('BusyLoopThread', 80)])
+        self.check_inferno_report_html([('BusyLoopThread', 20)])
 
 
 class TestExampleWithNativeRoot(TestExampleBase):
@@ -807,7 +806,11 @@
 
 
 def main():
+    os.chdir(get_script_dir())
     build_testdata()
+    if AdbHelper().get_android_version() < 7:
+        log_info("Skip tests on Android version < N.")
+        sys.exit(0)
     test_program = unittest.main(failfast=True, exit=False)
     if test_program.result.wasSuccessful():
         TestExampleBase.cleanupTestFiles()
diff --git a/simpleperf/scripts/utils.py b/simpleperf/scripts/utils.py
index a28008e..77f9f72 100644
--- a/simpleperf/scripts/utils.py
+++ b/simpleperf/scripts/utils.py
@@ -265,6 +265,21 @@
         log_fatal('unsupported architecture: %s' % output.strip())
 
 
+    def get_android_version(self):
+        build_version = self.get_property('ro.build.version.release')
+        android_version = 0
+        if build_version:
+            if not build_version[0].isdigit():
+                c = build_version[0].upper()
+                if c.isupper() and c >= 'L':
+                    android_version = ord(c) - ord('L') + 5
+            else:
+                strs = build_version.split('.')
+                if strs:
+                    android_version = int(strs[0])
+        return android_version
+
+
 def flatten_arg_list(arg_list):
     res = []
     if arg_list: