1# Copyright 2016 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 logging 6import utils 7 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.cros.enterprise import enterprise_policy_base 10 11 12class policy_NotificationsAllowedForUrls( 13 enterprise_policy_base.EnterprisePolicyTest): 14 """Test NotificationsAllowedForUrls policy effect on CrOS behavior. 15 16 This test verifies the behavior of Chrome OS with a set of valid values 17 for the NotificationsAllowedForUrls user policy, when 18 DefaultNotificationSetting=2 (i.e., do not allow notifications, except on 19 sites listed in NotificationsAllowedForUrls). These valid values are 20 covered by 3 test cases: SiteAllowed_Show, SiteNotAllowed_Block, 21 NotSet_Block. 22 23 When the policy value is None (as in case NotSet_Block), then notifications 24 are blocked on every site. When the value is set to one or more URLs (as 25 in SiteAllowed_Show and SiteNotAllowed_Block), notifications are blocked 26 on every site except for those sites whose domain matches any of the 27 listed URLs. 28 29 A related test, policy_NotificationsBlockedForUrls, has 30 DefaultNotificationsSetting=1 i.e., allow display of notifications by 31 default, except on sites in domains listed in NotificationsBlockedForUrls). 32 """ 33 version = 1 34 35 def initialize(self, **kwargs): 36 """Initialize this test.""" 37 self._initialize_test_constants() 38 super(policy_NotificationsAllowedForUrls, self).initialize(**kwargs) 39 self.start_webserver() 40 41 42 def _initialize_test_constants(self): 43 """Initialize test-specific constants, some from class constants.""" 44 self.POLICY_NAME = 'NotificationsAllowedForUrls' 45 self.TEST_FILE = 'notification_test_page.html' 46 self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE) 47 self.INCLUDES_ALLOWED_URL = ['http://www.bing.com', self.WEB_HOST, 48 'https://www.yahoo.com'] 49 self.EXCLUDES_ALLOWED_URL = ['http://www.bing.com', 50 'https://www.irs.gov/', 51 'https://www.yahoo.com'] 52 self.TEST_CASES = { 53 'SiteAllowed_Show': self.INCLUDES_ALLOWED_URL, 54 'SiteNotAllowed_Block': self.EXCLUDES_ALLOWED_URL, 55 'NotSet_Block': None 56 } 57 self.STARTUP_URLS = ['chrome://policy', 'chrome://settings'] 58 self.SUPPORTING_POLICIES = { 59 'DefaultNotificationsSetting': 2, 60 'BookmarkBarEnabled': True, 61 'EditBookmarksEnabled': True, 62 'RestoreOnStartupURLs': self.STARTUP_URLS, 63 'RestoreOnStartup': 4 64 } 65 66 67 def _wait_for_page_ready(self, tab): 68 """Wait for JavaScript on page in |tab| to set the pageReady flag. 69 70 @param tab: browser tab with page to load. 71 """ 72 utils.poll_for_condition( 73 lambda: tab.EvaluateJavaScript('pageReady'), 74 exception=error.TestError('Test page is not ready.')) 75 76 77 def _are_notifications_allowed(self, tab): 78 """Check if Notifications are allowed. 79 80 @param: chrome tab which has test page loaded. 81 @returns True if Notifications are allowed, else returns False. 82 """ 83 notification_permission = tab.EvaluateJavaScript( 84 'Notification.permission') 85 if notification_permission not in ['granted', 'denied', 'default']: 86 error.TestFail('Unable to capture Notification Setting.') 87 return notification_permission == 'granted' 88 89 90 def _test_notifications_allowed_for_urls(self, policy_value): 91 """Verify CrOS enforces the NotificationsAllowedForUrls policy. 92 93 When NotificationsAllowedForUrls is undefined, notifications shall be 94 blocked on all pages. When NotificationsAllowedForUrls contains one or 95 more URLs, notifications shall be allowed only on the pages whose 96 domain matches any of the listed URLs. 97 98 @param policy_value: policy value for this case. 99 100 """ 101 tab = self.navigate_to_url(self.TEST_URL) 102 self._wait_for_page_ready(tab) 103 notifications_allowed = self._are_notifications_allowed(tab) 104 logging.info('Notifications are allowed: %r', notifications_allowed) 105 106 # String |WEB_HOST| will be found in string |policy_value| for 107 # cases that expect the Notifications to be displayed. 108 if policy_value is not None and self.WEB_HOST in policy_value: 109 if not notifications_allowed: 110 raise error.TestFail('Notifications should be shown.') 111 else: 112 if notifications_allowed: 113 raise error.TestFail('Notifications should be blocked.') 114 tab.Close() 115 116 117 def run_once(self, case): 118 """Setup and run the test configured for the specified test case. 119 120 @param case: Name of the test case to run. 121 """ 122 case_value = self.TEST_CASES[case] 123 self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value 124 self.setup_case(user_policies=self.SUPPORTING_POLICIES) 125 self._test_notifications_allowed_for_urls(case_value) 126