Merge "uninstall apps after finishing tests" am: 2df671b38d
am: 6f82a2ee2b

Change-Id: Ie8ec28d0e33e2fc52adc5566bfba4c08ea374800
diff --git a/emu_test/test_console/run_adb_shell.py b/emu_test/test_console/run_adb_shell.py
index 00d3240..48d562c 100644
--- a/emu_test/test_console/run_adb_shell.py
+++ b/emu_test/test_console/run_adb_shell.py
@@ -6,8 +6,7 @@
 
 from utils import util
 
-main_apk_package = 'com.android.devtools.server'
-launcher_class_name = '%s.Server' % main_apk_package
+launcher_class_name = '%s.Server' % util.MAIN_APK_PACKAGE
 instrumentation_runner = 'android.support.test.runner.AndroidJUnitRunner'
 
 num_trials = 1
@@ -21,7 +20,7 @@
                                          '-w', '-e' 'class',
                                          launcher_class_name,
                                          ('%s.test/%s'
-                                          % (main_apk_package,
+                                          % (util.MAIN_APK_PACKAGE,
                                              instrumentation_runner))])
     break
   except subprocess.CalledProcessError as err:
diff --git a/emu_test/test_console/testcase_call.py b/emu_test/test_console/testcase_call.py
index 8c169a1..71dbc68 100644
--- a/emu_test/test_console/testcase_call.py
+++ b/emu_test/test_console/testcase_call.py
@@ -37,6 +37,10 @@
   def setUpClass(cls):
     util.run_script_run_adb_shell(TESTCASE_CALL_DIR)
 
+  @classmethod
+  def tearDownClass(cls):
+    util.unstall_apps(TESTCASE_CALL_DIR)
+
   def _process_request_telephony_service(self, payload):
     r = requests.post(SERVLET_TELEPHONY, data=json.dumps(payload))
     if r.raise_for_status():
diff --git a/emu_test/test_console/testcase_geo.py b/emu_test/test_console/testcase_geo.py
index 36aa230..9302908 100644
--- a/emu_test/test_console/testcase_geo.py
+++ b/emu_test/test_console/testcase_geo.py
@@ -34,6 +34,10 @@
   def setUpClass(cls):
     util.run_script_run_adb_shell(TESTCASE_CALL_DIR)
 
+  @classmethod
+  def tearDownClass(cls):
+    util.unstall_apps(TESTCASE_CALL_DIR)
+
   def _process_request_geo_service(self, payload):
     """Processes post request to geo service.
 
diff --git a/emu_test/test_console/testcase_orientation.py b/emu_test/test_console/testcase_orientation.py
index a694627..d372242 100644
--- a/emu_test/test_console/testcase_orientation.py
+++ b/emu_test/test_console/testcase_orientation.py
@@ -38,6 +38,10 @@
   def setUpClass(cls):
     util.run_script_run_adb_shell(TESTCASE_CALL_DIR)
 
+  @classmethod
+  def tearDownClass(cls):
+    util.unstall_apps(TESTCASE_CALL_DIR)
+
   def _process_request_orientation_service(self, payload):
     """Processes post request to orientation service.
 
diff --git a/emu_test/test_console/testcase_sms.py b/emu_test/test_console/testcase_sms.py
index c693f16..764be97 100644
--- a/emu_test/test_console/testcase_sms.py
+++ b/emu_test/test_console/testcase_sms.py
@@ -37,6 +37,10 @@
   def setUpClass(cls):
     util.run_script_run_adb_shell(TESTCASE_CALL_DIR)
 
+  @classmethod
+  def tearDownClass(cls):
+    util.unstall_apps(TESTCASE_CALL_DIR)
+
   def _process_request_sms_service(self, payload):
     """Processes post request to sms service.
 
diff --git a/emu_test/test_console/uninstall_app.py b/emu_test/test_console/uninstall_app.py
new file mode 100644
index 0000000..847c1be
--- /dev/null
+++ b/emu_test/test_console/uninstall_app.py
@@ -0,0 +1,23 @@
+"""This script is to run adb to uninstall apps."""
+
+import subprocess
+import sys
+import time
+
+from utils import util
+
+test_apk_package = '%s.test' % util.MAIN_APK_PACKAGE
+
+num_trials = 1
+while True:
+  if num_trials is util.ADB_NUM_MAX_TRIALS:
+    sys.exit(-1)
+  try:
+    print ('Run adb shell to uninstall apps, trial num: %s' % str(num_trials))
+    subprocess.call(['adb', 'uninstall', util.MAIN_APK_PACKAGE])
+    subprocess.call(['adb', 'uninstall', test_apk_package])
+    break
+  except subprocess.CalledProcessError as err:
+    print 'Subprocess call error: {0}'.format(err)
+    time.sleep(util.ADB_TRIAL_WAIT_TIME_S)
+    num_trials += 1
diff --git a/emu_test/test_console/utils/util.py b/emu_test/test_console/utils/util.py
index 8899f48..984ee89 100644
--- a/emu_test/test_console/utils/util.py
+++ b/emu_test/test_console/utils/util.py
@@ -69,8 +69,10 @@
 CMD_EXIT = 'exit\n'
 SCRIPT_TO_INSTALL_APK = 'install_apk.py'
 SCRIPT_TO_RUN_ADB_SHELL = 'run_adb_shell.py'
+SCRIPT_TO_UNINSTALL_APP = 'uninstall_app.py'
 PYTHON_INTERPRETER = 'python'
 CMD_ROTATE = 'rotate\n'
+MAIN_APK_PACKAGE = 'com.android.devtools.server'
 
 
 def check_read_until(console_output):
@@ -304,3 +306,13 @@
   subprocess.call([PYTHON_INTERPRETER, script_install_apk])
   subprocess.Popen([PYTHON_INTERPRETER, script_run_adb_shell])
   time.sleep(SETUP_WAIT_TIMEOUT_S)
+
+def unstall_apps(testcase_call_dir):
+  """Run Python script to uninstall apps.
+
+  Args:
+    testcase_call_dir: The directory where the test case is called from.
+  """
+  subprocess.Popen([PYTHON_INTERPRETER,
+                    '%s/%s' % (testcase_call_dir, SCRIPT_TO_UNINSTALL_APP)])
+  time.sleep(SETUP_WAIT_TIMEOUT_S)