• 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
5import time
6
7from autotest_lib.client.bin import utils
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros.enterprise import enterprise_policy_base
10
11class policy_ForceYouTubeRestrict(
12        enterprise_policy_base.EnterprisePolicyTest):
13    """
14    Tests the ForceYouTubeRestrict policy in Chrome OS.
15
16    If the policy is set to strict than the user will not be able to view any
17    restricted videos on YouTube. If the policy is set to moderate than the
18    user will not be able to watch some restricted videos. In both cases
19    the user will not be able to toggle restricted settings on the website.
20
21    Note: This test doesn't cover the ARC++ app.
22
23    """
24    version = 1
25
26    POLICY_NAME = 'ForceYouTubeRestrict'
27    TEST_CASES = {
28        'Strict': 2,
29        'Moderate': 1,
30        'Disabled': 0,
31        'NotSet': None}
32
33    def _search_for_adult_content(self):
34        SEARCH_QUERY = 'https://www.youtube.com/results?search_query=adult'
35        BURGER_MENU = (
36                "document.querySelector('* /deep/ #masthead-container /deep/"
37                " #end /deep/ ytd-topbar-menu-button-renderer:last-of-type').innerHTML;"
38        )
39        self.search_tab = self.navigate_to_url(SEARCH_QUERY)
40        utils.poll_for_condition(lambda: self.check_page_readiness(
41                self.search_tab, BURGER_MENU),
42                                 exception=error.TestFail(
43                                         'Page is not ready.'),
44                                 timeout=5,
45                                 sleep_interval=1)
46
47    def _open_restricted_menu(self):
48        BURGER_MENU_CLICK = (
49                "document.querySelector('* /deep/ #masthead-container /de"
50                "ep/ #end /deep/ ytd-topbar-menu-button-renderer:last-of-type').click();"
51        )
52        RESTRICTED_MENU_CLICK = """
53buttons=document.querySelectorAll('a#endpoint.yt-simple-endpoint.style-scope.ytd-compact-link-renderer');
54for (let i = 0; i < buttons.length; i++) {
55  button=buttons[i];
56  if (button.innerText.startsWith("Restricted Mode:")) {
57     button.click();
58     break;
59  }
60}
61"""
62        self.search_tab.EvaluateJavaScript(BURGER_MENU_CLICK)
63        time.sleep(1)
64        self.search_tab.EvaluateJavaScript(RESTRICTED_MENU_CLICK)
65        time.sleep(1)
66
67    def _restricted_mode_by_policy(self):
68        RESTRICTED_MODE_SELECTOR = "document.querySelector('ytd-text-header-renderer.style-scope.ytd-section-list-renderer').innerText"
69        return self.search_tab.EvaluateJavaScript(RESTRICTED_MODE_SELECTOR)
70
71    def _restricted_mode_by_policy_strict(self):
72        return "Restricted Mode is enabled by your network administrator" in \
73           self._restricted_mode_by_policy()
74
75    def _restricted_mode_by_policy_moderate(self):
76        return self._restricted_mode_by_policy_strict()
77
78    def _get_content(self, restriction_type_url):
79        """
80        Checks the contents of the watch page.
81
82        @param restriction_type: URL with either strict or moderate content.
83
84        @returns text content of the element with video status.
85
86        """
87        VERIFY_VIDEO_NOT_LOADED_CMD = ("document.getElementById"
88                                       "('error-screen').innerText;")
89        active_tab = self.navigate_to_url(restriction_type_url)
90        utils.poll_for_condition(
91            lambda: self.check_page_readiness(
92                active_tab, VERIFY_VIDEO_NOT_LOADED_CMD),
93            exception=error.TestFail('Page is not ready.'),
94            timeout=5,
95            sleep_interval=1)
96        return active_tab.EvaluateJavaScript(VERIFY_VIDEO_NOT_LOADED_CMD)
97
98    def _strict_content(self):
99        RESTRICTED_ONLY_ON_STRICT = 'https://www.youtube.com/watch?v=Fmwfmee2ZTE'
100        return self._get_content(RESTRICTED_ONLY_ON_STRICT)
101
102    def _moderated_content(self):
103        RESTRICTED_ON_MODERATE = 'https://www.youtube.com/watch?v=yR79oLrI1g4'
104        return self._get_content(RESTRICTED_ON_MODERATE)
105
106    def _check_restricted_mode(self, case):
107        """
108        Checks restricted settings by verifying that user is unable to play
109        certain videos as well as toggle restricted settings.
110
111        @param case: policy value expected.
112
113        """
114        # Navigates to the search page to search for adult content
115        self._search_for_adult_content()
116        # We could check for the status shown in restricted menu but
117        # unfortunately this is broken for the policy and therefore
118        # doesn't add value to the test
119        #self._open_restricted_menu()
120
121        if case == 'Strict':
122            if 'restricted' in self._strict_content() \
123               and self._restricted_mode_by_policy_strict():
124                return True
125            raise error.TestFail(
126                    "Restricted mode is not on, user can view restricted video."
127            )
128        elif case == 'Moderate':
129            if 'restricted' in self._moderated_content() \
130               and self._restricted_mode_by_policy_moderate():
131                return True
132            raise error.TestFail(
133                    "Restricted mode is not on, user can view restricted video."
134            )
135        else:
136            return True
137
138
139    def run_once(self, case):
140        """
141        Setup and run the test configured for the specified test case.
142
143        @param case: Name of the test case to run.
144
145        """
146        self.POLICIES = {self.POLICY_NAME: self.TEST_CASES[case]}
147        self.setup_case(user_policies=self.POLICIES)
148        self._check_restricted_mode(case)
149