Install required APKs for UI tests before tests start.

UI tests used to install APKs programmatically during tests,
which is not stable as observed due to security issues.
Besides, that way also deviates from manual test cases.
Instead, we will install APKs before tests and deprecate
the programmatical method to install an apk in test runtime.

Change-Id: I059265ffcd9da7ac949506cf7467779b8503cf46
diff --git a/emu_test/test_ui/test_ui.py b/emu_test/test_ui/test_ui.py
index fb8cc11..25e3afd 100644
--- a/emu_test/test_ui/test_ui.py
+++ b/emu_test/test_ui/test_ui.py
@@ -69,6 +69,17 @@
         self.launch_emu_and_wait(avd)
         self.m_logger.info('System image UI tests (%s) start.' % self._testMethodName)
         uitest_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', 'system_image_uitests')
+
+        # install APKs required in UI tests
+        uitest_assets_dir = os.path.join(uitest_dir, 'app', 'src', 'main', 'assets')
+        for filename in os.listdir(uitest_assets_dir):
+            if filename.endswith('.apk'):
+                p = psutil.Popen(['adb', 'install', '-r', filename], cwd=uitest_assets_dir, stdout=PIPE, stderr=PIPE)
+                (out, err) = p.communicate()
+                self.m_logger.info('Install APK, stdout: %s, stderr: %s', out, err)
+                self.assertTrue(p.poll() == 0, "Failed to install %s." % filename)
+
+        # run tests using gradle script
         proc = self._launch_ui_test_with_avd_configs(uitest_dir, avd)
         (output, err) = proc.communicate()
         self.m_logger.info('gradle_stdout:\n' + output)