• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import time
5import utils
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.cros.enterprise import enterprise_policy_base
9from telemetry.core.exceptions import EvaluateException
10
11
12class policy_DefaultJavaScriptSetting(
13        enterprise_policy_base.EnterprisePolicyTest):
14    """
15    Tests the DefaultJavaScriptSetting policy in Chrome OS.
16
17    If the DefaultJavaScriptSetting policy is set to Allow, going to the
18    local test page should display YES. If the policy is set to Do Not Allow
19    going to the local test page should display NO.
20
21    """
22    version = 1
23
24    def initialize(self, **kwargs):
25        """Initialize this test."""
26        super(policy_DefaultJavaScriptSetting, self).initialize(**kwargs)
27        self.TEST_FILE = 'js_test.html'
28        self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE)
29        self.POLICY_NAME = 'DefaultJavaScriptSetting'
30        self.POLICIES = {}
31        self.TEST_CASES = {
32            'Allow': 1,
33            'Do Not Allow': 2}
34        self.start_webserver()
35
36    def _can_execute_javascript(self):
37        """
38        Determine whether JavaScript is allowed to run on the given page.
39
40        @param tab: browser tab containing JavaScript to run.
41
42        Note: This test does not use self.navigate_to_url(), because it can
43        not depend on methods that evaluate or execute JavaScript.
44
45        """
46        tab = self.cr.browser.tabs.New()
47        tab.Activate()
48        tab.Navigate(self.TEST_URL)
49        time.sleep(1)
50
51        utils.poll_for_condition(
52            lambda: tab.url == self.TEST_URL,
53            exception=error.TestError('Test page is not ready.'))
54
55        try:
56            utils.poll_for_condition(
57                lambda: tab.EvaluateJavaScript('jsAllowed', timeout=2),
58                exception=error.TestError('Test page is not ready.'))
59            return True
60        except (EvaluateException, utils.TimeoutError):
61            return False
62
63    def run_once(self, case):
64        """
65        Setup and run the test configured for the specified test case.
66
67        @param case: Name of the test case to run.
68
69        """
70        case_value = self.TEST_CASES[case]
71        self.POLICIES[self.POLICY_NAME] = case_value
72        self.setup_case(user_policies=self.POLICIES)
73
74        if case == 'Allow':
75            if not self._can_execute_javascript():
76                raise error.TestFail('JavaScript disabled, should be enabled.')
77        else:
78            if self._can_execute_javascript():
79                raise error.TestFail('JavaScript enabled, should be disabled.')
80