• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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.
4
5from autotest_lib.client.common_lib import error
6from autotest_lib.client.cros.enterprise import enterprise_policy_base
7from autotest_lib.client.cros.input_playback import keyboard
8from telemetry.core import exceptions
9
10class policy_DeveloperToolsAvailability(
11        enterprise_policy_base.EnterprisePolicyTest):
12    """
13    Tests the DeveloperToolsAvailable policy in Chrome OS.
14    If the policy is set to Available then the user will be able to open
15    a new Developer Tools console. If the policy is Disabled then the user
16    should not be able to open a new Developer Tools console. Forced is
17    not being tested.
18
19    """
20
21    version = 1
22
23
24    def initialize(self, **kwargs):
25        super(policy_DeveloperToolsAvailability, self).initialize(**kwargs)
26        self.keyboard = keyboard.Keyboard()
27        self.POLICY_NAME = 'DeveloperToolsAvailability'
28        self.POLICIES = {}
29        self.TEST_CASES = {
30            'NotSet': None,
31            'DisabledOnExtensions': 0,
32            'Available': 1,
33            'Disabled': 2}
34
35
36    def _check_developer_tools_availability(self, case):
37        """
38        Opens a new chrome://user-actions page and then tries to open Developer
39        Tools console. To see if the new window actually opened the test checks
40        what was recorded in user actions.
41
42        @param case: policy description.
43
44        """
45        page_scrape_cmd = (
46            'document.getElementById("user-actions-table").innerText;')
47        user_actions_tab = self.navigate_to_url('chrome://user-actions')
48
49        # The below shortcuts can be used to open Developer Tools, though in
50        # different tabs. The first one opens the Elements tab, the next two
51        # open the last used tab, and the final one opens the Console tab.
52
53        keys = ['ctrl+shift+c', 'ctrl+shift+i', 'f12', 'ctrl+shift+j']
54
55        for key in keys:
56            self.keyboard.press_key(key)
57
58        recorded_user_actions = (
59            user_actions_tab.EvaluateJavaScript(page_scrape_cmd))
60
61        if (case == 'Available' or case == 'DisabledOnExtensions'
62                or case == 'NotSet'):
63            if ('DevTools_ToggleWindow' not in recorded_user_actions and
64                'DevTools_ToggleConsole' not in recorded_user_actions):
65                    raise error.TestFail("Developer Tools didn't open, but"
66                        " should be allowed.")
67        elif case == 'Disabled':
68            if 'DevTools_ToggleWindow' in recorded_user_actions:
69                raise error.TestFail("Developer Tools opened and should "
70                    "have been disabled.")
71
72
73    def run_once(self, case):
74        """
75        Setup and run the test configured for the specified test case.
76
77        @param case: Name of the test case to run.
78
79        """
80        case_value = self.TEST_CASES[case]
81        self.POLICIES[self.POLICY_NAME] = case_value
82
83        try:
84            self.setup_case(user_policies=self.POLICIES)
85            self._check_developer_tools_availability(case)
86        except exceptions.TimeoutException:
87            if case != 'Disabled':
88                raise error.TestFail("Unexpected Timeout Exception")
89
90