arc: Switch to using autotest API to enable Play Store

Need to discard using settings based approach once old
settings expires soon.

BUG=chromium:694081
TEST=Manually, together with crrev.com/2801173002

Change-Id: I906340935fd2f4faa9fc7f6783925f6b89728050
Reviewed-on: https://chromium-review.googlesource.com/470707
Commit-Ready: Yury Khmel <khmel@chromium.org>
Tested-by: Yury Khmel <khmel@chromium.org>
Reviewed-by: Luis Hector Chavez <lhchavez@chromium.org>
Reviewed-by: Yury Khmel <khmel@chromium.org>
diff --git a/client/common_lib/cros/arc_util.py b/client/common_lib/cros/arc_util.py
index 6ddb171..b0f7c22 100644
--- a/client/common_lib/cros/arc_util.py
+++ b/client/common_lib/cros/arc_util.py
@@ -14,6 +14,7 @@
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.common_lib import file_utils
 from autotest_lib.client.common_lib.cros import arc_common
+from telemetry.core import exceptions
 from telemetry.internal.browser import extension_page
 
 _ARC_SUPPORT_HOST_URL = 'chrome-extension://cnbgggchhmkkdmeppjobngjoejnihlei/'
@@ -143,47 +144,49 @@
     b_options.gaia_login = True
 
 
-def enable_arc_setting(browser):
+def enable_play_store(autotest_ext):
     """
-    Enable ARC++ via the settings page checkbox.
+    Enable ARC++ Play Store
 
     Do nothing if the account is managed.
 
-    @param browser: chrome.Chrome broswer object.
+    @param autotest_ext: autotest extension object.
 
     @returns: True if the opt-in should continue; else False.
 
     """
-    settings_tab = browser.tabs.New()
 
+    if autotest_ext is None:
+         raise error.TestFail(
+                 'Could not enable ARC because autotest API does not exist')
+
+    # Skip enabling for managed users, since value is policy enforced.
+    # Return early if a managed user has ArcEnabled set to false.
     try:
-        settings_tab.Navigate('chrome://settings-frame')
-        settings_tab.WaitForDocumentReadyStateToBeComplete()
-
-        try:
-            settings_tab.ExecuteJavaScript(
-                    'assert(document.getElementById("android-apps-enabled"))')
-        except Exception, e:
-            raise error.TestFail('Could not locate section in chrome://settings'
-                                 ' to enable arc. Make sure ARC is available.')
-
-        # Skip enabling for managed users, since value is policy enforced.
-        # Return early if a managed user has ArcEnabled set to false.
-        is_managed = settings_tab.EvaluateJavaScript(
-                'document.getElementById("android-apps-enabled").disabled')
+        autotest_ext.ExecuteJavaScript('''
+            chrome.autotestPrivate.getPlayStoreState(function(state) {
+              window.__play_store_state = state;
+            });
+        ''')
+        # Results must be available by the next invocation.
+        is_managed = autotest_ext.EvaluateJavaScript(
+            'window.__play_store_state.managed')
         if is_managed:
             logging.info('Determined that ARC is managed by user policy.')
-            policy_value = settings_tab.EvaluateJavaScript(
-                    'document.getElementById("android-apps-enabled").checked')
-            if not policy_value:
+            policy_enabled = autotest_ext.EvaluateJavaScript(
+                'window.__play_store_state.enabled')
+            if not policy_enabled:
                 logging.info(
-                        'Returning early since ARC is policy-enforced off.')
+                    'Returning early since ARC is policy-enforced off.')
                 return False
         else:
-            settings_tab.ExecuteJavaScript(
-                    'Preferences.setBooleanPref("arc.enabled", true, true)')
-    finally:
-        settings_tab.Close()
+            autotest_ext.ExecuteJavaScript('''
+                chrome.autotestPrivate.setPlayStoreEnabled(
+                    true, function(enabled) {});
+            ''')
+    except exceptions.EvaluateException as e:
+        raise error.TestFail(' Could not enable ARC via autotest API. "%s".'
+            % e)
 
     return True
 
@@ -268,19 +271,20 @@
                                  'seconds!' % SIGN_IN_TIMEOUT)
 
 
-def opt_in(browser):
+def opt_in(browser, autotest_ext):
     """
     Step through opt in and wait for it to complete.
 
     Return early if the arc_setting cannot be set True.
 
     @param browser: chrome.Chrome broswer object.
+    @param autotest_ext: autotest extension object.
 
     @raises: error.TestFail if opt in fails.
 
     """
     logging.info(_OPT_IN_BEGIN)
-    if not enable_arc_setting(browser):
+    if not enable_play_store(autotest_ext):
         return
     extension_main_page = find_opt_in_extension_page(browser)
     opt_in_and_wait_for_completion(extension_main_page)
diff --git a/client/common_lib/cros/chrome.py b/client/common_lib/cros/chrome.py
index 8d734c3..27a5f58 100644
--- a/client/common_lib/cros/chrome.py
+++ b/client/common_lib/cros/chrome.py
@@ -90,6 +90,12 @@
                                    to disable the arc opt in flow.
         """
         self._autotest_ext_path = None
+
+        # Force autotest extension if we need enable Play Store.
+        if (utils.is_arc_available() and (arc_util.should_start_arc(arc_mode)
+            or not disable_arc_opt_in)):
+            autotest_ext = True
+
         if autotest_ext:
             self._autotest_ext_path = os.path.join(os.path.dirname(__file__),
                                                    'autotest_private_ext')
@@ -159,9 +165,9 @@
                 if utils.is_arc_available():
                     if disable_arc_opt_in:
                         if arc_util.should_start_arc(arc_mode):
-                            arc_util.enable_arc_setting(self.browser)
+                            arc_util.enable_play_store(self.autotest_ext)
                     else:
-                        arc_util.opt_in(self.browser)
+                        arc_util.opt_in(self.browser, self.autotest_ext)
                     arc_util.post_processing_after_browser(self)
                 break
             except exceptions.LoginException as e: