• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 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_NotificationsBlockedForUrls(
13        enterprise_policy_base.EnterprisePolicyTest):
14    """Test NotificationsBlockedForUrls policy effect on CrOS behavior.
15
16    This test verifies the behavior of Chrome OS with a set of valid values
17    for the NotificationsBlockedForUrls user policy, when
18    DefaultNotificationSetting=1 (i.e., allow notifications, except on
19    sites listed in NotificationsBlockedForUrls). These valid values are
20    covered by 3 test cases: SiteBlocked_Block, SiteAllowed_Show,
21    NotSet_Show.
22
23    When the policy value is None (as in case NotSet_Show), then notifications
24    are displayed on every site. When the value is set to one or more URLs (as
25    in SiteBlocked_Block and SiteAllowed_Show), notifications are allowed
26    on every site except for those sites whose domain matches any of the
27    listed URLs.
28
29    A related test, policy_NotificationsAllowedForUrls, has
30    DefaultNotificationsSetting=2 i.e., do not allow display of notifications by
31    default, except on sites in domains listed in NotificationsAllowedForUrls).
32
33    """
34    version = 1
35
36    def initialize(self, **kwargs):
37        self._initialize_test_constants()
38        super(policy_NotificationsBlockedForUrls, 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 = 'NotificationsBlockedForUrls'
45        self.TEST_FILE = 'notification_test_page.html'
46        self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE)
47        self.INCLUDES_BLOCKED_URL = ['http://www.bing.com', self.WEB_HOST,
48                                     'https://www.yahoo.com']
49        self.EXCLUDES_BLOCKED_URL = ['http://www.bing.com',
50                                     'https://www.irs.gov/',
51                                     'https://www.yahoo.com']
52        self.TEST_CASES = {
53            'SiteBlocked_Block': self.INCLUDES_BLOCKED_URL,
54            'SiteAllowed_Show': self.EXCLUDES_BLOCKED_URL,
55            'NotSet_Show': None
56        }
57        self.STARTUP_URLS = ['chrome://policy', 'chrome://settings']
58        self.SUPPORTING_POLICIES = {
59            'DefaultNotificationsSetting': 1,
60            'BookmarkBarEnabled': True,
61            'EditBookmarkEnabled': 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        """
73        utils.poll_for_condition(
74            lambda: tab.EvaluateJavaScript('pageReady'),
75            exception=error.TestError('Test page is not ready.'))
76
77
78    def _are_notifications_blocked(self, tab):
79        """Check if Notifications are blocked.
80
81        @param: chrome tab which has test page loaded.
82
83        @returns True if Notifications are blocked, else returns False.
84
85        """
86        notification_permission = tab.EvaluateJavaScript(
87                'Notification.permission')
88        if notification_permission not in ['granted', 'denied', 'default']:
89            error.TestFail('Unable to capture Notification Setting.')
90        return notification_permission == 'denied'
91
92
93    def _test_notifications_blocked_for_urls(self, policy_value):
94        """Verify CrOS enforces the NotificationsBlockedForUrls policy.
95
96        When NotificationsBlockedForUrls is undefined, notifications shall be
97        allowed on all pages. When NotificationsBlockedForUrls contains one or
98        more URLs, notifications shall be blocked only on the pages whose
99        domain matches any of the listed URLs.
100
101        @param policy_value: policy value for this case.
102
103        """
104        tab = self.navigate_to_url(self.TEST_URL)
105        self._wait_for_page_ready(tab)
106        notifications_blocked = self._are_notifications_blocked(tab)
107        logging.info('Notifications are blocked: %r', notifications_blocked)
108
109        # String |WEB_HOST| will be found in string |policy_value| for
110        # cases that expect the Notifications to be blocked.
111        if policy_value is not None and self.WEB_HOST in policy_value:
112            if not notifications_blocked:
113                raise error.TestFail('Notifications should be blocked.')
114        else:
115            if notifications_blocked:
116                raise error.TestFail('Notifications should be allowed.')
117        tab.Close()
118
119
120    def run_test_case(self, case):
121        """Setup and run the test configured for the specified test case.
122
123        @param case: Name of the test case to run.
124
125        """
126        case_value = self.TEST_CASES[case]
127        self.setup_case(self.POLICY_NAME, case_value, self.SUPPORTING_POLICIES)
128        self._test_notifications_blocked_for_urls(case_value)
129