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 11 12VERIFY_VIDEO_NOT_LOADED_CMD = ("document.getElementById" 13 "('error-screen').innerText;") 14RESTRICTED_ONLY_ON_STRICT = 'https://www.youtube.com/watch?v=Fmwfmee2ZTE' 15RESTRICTED_ON_MODERATE = 'https://www.youtube.com/watch?v=feM0xeOLcYI' 16SEARCH_QUERY = 'https://www.youtube.com/results?search_query=adult' 17RESTRICTED_MODE_TOGGLE_ENABLED = 'aria-disabled="false"' 18RESTRICTED_MODE_TOGGLE_DISABLED = 'aria-disabled="true"' 19RESTRICTED_MODE_TOGGLE_SCRAPE_CMD = ("document.querySelector('* /deep/ " 20 "#restricted-mode').innerHTML;") 21BURGER_MENU_CLICK = ("document.querySelector('* /deep/ #masthead-container /de" 22 "ep/ #end /deep/ ytd-topbar-menu-button-renderer:last-of-type').click();") 23BURGER_MENU = ("document.querySelector('* /deep/ #masthead-container /deep/" 24 " #end /deep/ ytd-topbar-menu-button-renderer:last-of-type').innerHTML;") 25RESTRICTED_MENU_CLICK = ("document.querySelector('* /deep/ ytd-app /deep/" 26 " ytd-account-settings /deep/ #restricted').click();") 27 28 29class policy_ForceYouTubeRestrict( 30 enterprise_policy_base.EnterprisePolicyTest): 31 """ 32 Tests the ForceYouTubeRestrict policy in Chrome OS. 33 34 If the policy is set to strict than the user will not be able to view any 35 restricted videos on YouTube. If the policy is set to moderate than the 36 user will not be able to watch some restricted videos. In both cases 37 the user will not be able to toggle restricted settings on the website. 38 39 Note: This test doesn't cover the ARC++ app. 40 41 """ 42 version = 1 43 44 POLICY_NAME = 'ForceYouTubeRestrict' 45 TEST_CASES = { 46 'Strict': 2, 47 'Moderate': 1, 48 'Disabled': 0, 49 'NotSet': None} 50 51 52 def _get_content(self, restriction_type): 53 """ 54 Checks the contents of the watch page. 55 56 @param restriction_type: URL with either strict or moderate content. 57 58 @returns text content of the element with video status. 59 60 """ 61 active_tab = self.navigate_to_url(restriction_type) 62 utils.poll_for_condition( 63 lambda: self.check_page_readiness( 64 active_tab, VERIFY_VIDEO_NOT_LOADED_CMD), 65 exception=error.TestFail('Page is not ready.'), 66 timeout=5, 67 sleep_interval=1) 68 return active_tab.EvaluateJavaScript(VERIFY_VIDEO_NOT_LOADED_CMD) 69 70 71 def _check_restricted_mode(self, case): 72 """ 73 Checks restricted settings by verifying that user is unable to play 74 certain videos as well as toggle restricted settings. 75 76 @param case: policy value expected. 77 78 """ 79 # Navigates to the search page and verifies the status of the toggle 80 # button. 81 search_tab = self.navigate_to_url(SEARCH_QUERY) 82 utils.poll_for_condition( 83 lambda: self.check_page_readiness(search_tab, BURGER_MENU), 84 exception=error.TestFail('Page is not ready.'), 85 timeout=5, 86 sleep_interval=1) 87 search_tab.EvaluateJavaScript(BURGER_MENU_CLICK) 88 time.sleep(1) 89 search_tab.EvaluateJavaScript(RESTRICTED_MENU_CLICK) 90 time.sleep(1) 91 restricted_mode_toggle_status = search_tab.EvaluateJavaScript( 92 RESTRICTED_MODE_TOGGLE_SCRAPE_CMD) 93 94 if case == 'Strict' or case == 'Moderate': 95 if case == 'Strict': 96 strict_content = self._get_content(RESTRICTED_ONLY_ON_STRICT) 97 if 'restricted' not in strict_content: 98 raise error.TestFail("Restricted mode is not on, " 99 "user can view restricted video.") 100 elif case == 'Moderate': 101 moderate_content = self._get_content(RESTRICTED_ON_MODERATE) 102 if 'restricted' not in moderate_content: 103 raise error.TestFail("Restricted mode is not on, " 104 "user can view restricted video.") 105 if RESTRICTED_MODE_TOGGLE_ENABLED in restricted_mode_toggle_status: 106 raise error.TestFail("User is able to toggle restricted mode.") 107 else: 108 strict_content = self._get_content(RESTRICTED_ONLY_ON_STRICT) 109 moderate_content = self._get_content(RESTRICTED_ON_MODERATE) 110 if ('restricted' in strict_content or 111 'restricted' in moderate_content): 112 raise error.TestFail("Restricted mode is on and it " 113 "shouldn't be.") 114 if (RESTRICTED_MODE_TOGGLE_DISABLED in 115 restricted_mode_toggle_status): 116 raise error.TestFail("User is not able to " 117 "toggle restricted mode.") 118 119 120 def run_once(self, case): 121 """ 122 Setup and run the test configured for the specified test case. 123 124 @param case: Name of the test case to run. 125 126 """ 127 self.POLICIES = {self.POLICY_NAME: self.TEST_CASES[case]} 128 self.setup_case(user_policies=self.POLICIES) 129 self._check_restricted_mode(case)