Improve stability of cheets_AppCompatTest.

I have added a retry flow around the core functionality of the test.
Each run will have its own logcat and screenshots.

BUG=None
TEST=Many of the tests locally and in the lab. No failures.

Change-Id: I727d2f2901e644945c3863b6590edf34a9c1bd92
Reviewed-on: https://chromium-review.googlesource.com/c/1401295
Reviewed-by: David Haddock <dhaddock@chromium.org>
Commit-Queue: David Haddock <dhaddock@chromium.org>
Tested-by: David Haddock <dhaddock@chromium.org>
Trybot-Ready: David Haddock <dhaddock@chromium.org>
(cherry picked from commit 17b572600066ec784894ef67b5baa0d1ec851167)
Reviewed-on: https://chromium-review.googlesource.com/c/1427362
diff --git a/client/site_tests/cheets_AppCompatTest/cheets_AppCompatTest.py b/client/site_tests/cheets_AppCompatTest/cheets_AppCompatTest.py
index 800d7a9..0cc3bcc 100644
--- a/client/site_tests/cheets_AppCompatTest/cheets_AppCompatTest.py
+++ b/client/site_tests/cheets_AppCompatTest/cheets_AppCompatTest.py
@@ -4,11 +4,11 @@
 
 import logging
 import os
-import time
 
 from autotest_lib.client.bin import utils
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.common_lib.cros import arc
+from autotest_lib.client.cros.graphics import graphics_utils
 
 
 class cheets_AppCompatTest(arc.ArcTest):
@@ -124,10 +124,11 @@
             arc.adb_shell('rm -r %s' % screenshot, ignore_status=True)
 
 
-    def _save_logcat(self):
+    def _save_logcat(self, iteration):
         """Saves the logcat for the test run to the resultsdir."""
+        self._logcat = 'logcat%d.txt' % iteration
         arc.adb_cmd('logcat -d > %s' % os.path.join(self.resultsdir,
-                                                    'logcat.txt'))
+                                                    self._logcat))
 
 
     def _parse_results(self):
@@ -144,13 +145,13 @@
                   self._pkg_name, self._app_version, passed, failed, nt,
                   blocked, skipped, ft))
         logging.info(result)
-        if int(failed) > 0:
-            raise error.TestFail(result)
+        pass_status = int(failed) == 0 and int(passed) > 0
+        return pass_status, result
 
 
     def _get_log_entry_count(self, entry):
         """Get the total number of times a string appears in logcat."""
-        logcat = os.path.join(self.resultsdir, 'logcat.txt')
+        logcat = os.path.join(self.resultsdir, self._logcat)
         return utils.run('grep "%s" %s | grep -c "%s"' %
                         (self._LOG_TAG, logcat, entry),
                          ignore_status=True).stdout.strip()
@@ -158,7 +159,7 @@
 
     def _get_failed_test_cases(self):
         """Get the list of test cases that failed from logcat."""
-        logcat = os.path.join(self.resultsdir, 'logcat.txt')
+        logcat = os.path.join(self.resultsdir, self._logcat)
         failed_tests = []
         failed_tests_list = utils.run('grep "%s" %s | grep ",FAIL,"' %
                             (self._LOG_TAG, logcat),
@@ -173,6 +174,11 @@
         arc.adb_cmd('logcat -G 16M')
 
 
+    def _clear_logcat_buffer(self):
+        """Clear logcat buffer between runs."""
+        arc.adb_cmd('logcat -c')
+
+
     def _get_app_version(self):
         """Grab the version of the application we are testing."""
         self._app_version = arc.adb_shell('dumpsys package %s | grep '
@@ -180,13 +186,32 @@
                                           self._pkg_name)
 
 
-    def run_once(self):
+    def run_once(self, retries=3):
         self._increase_logcat_buffer()
         self._copy_resources_to_dut()
         self._grant_storage_permission()
-        self._start_test()
-        self._get_app_version()
-        self._capture_bugreport()
-        self._grab_screenshots()
-        self._save_logcat()
-        self._parse_results()
+
+        for trial in range(retries):
+            logging.info('Iteration %d: Trying to launch play store' % trial)
+
+            # Bring Play Store to front.
+            arc.adb_shell('am start %s' % self._PLAY_STORE_ACTIVITY)
+            graphics_utils.take_screenshot(self.resultsdir,
+                                           'test_start')
+            self._start_test()
+            logging.info('Iteration %d: Test finished' % trial)
+            graphics_utils.take_screenshot(self.resultsdir,
+                                           'test_end')
+            self._get_app_version()
+            self._capture_bugreport()
+            self._grab_screenshots()
+            self._save_logcat(trial)
+            passed, result = self._parse_results()
+            if passed:
+                return
+            elif trial + 1 >= retries:
+                raise error.TestFail(result)
+
+            # Kill playstore and clear logcat for next iteration.
+            arc.adb_shell('am force-stop %s' % self._PLAY_STORE_ACTIVITY)
+            self._clear_logcat_buffer()