• 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        """Initialize this test."""
38        self._initialize_test_constants()
39        super(policy_NotificationsBlockedForUrls, self).initialize(**kwargs)
40        self.start_webserver()
41
42
43    def _initialize_test_constants(self):
44        """Initialize test-specific constants, some from class constants."""
45        self.POLICY_NAME = 'NotificationsBlockedForUrls'
46        self.TEST_FILE = 'notification_test_page.html'
47        self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE)
48        self.INCLUDES_BLOCKED_URL = ['http://www.bing.com', self.WEB_HOST,
49                                     'https://www.yahoo.com']
50        self.EXCLUDES_BLOCKED_URL = ['http://www.bing.com',
51                                     'https://www.irs.gov/',
52                                     'https://www.yahoo.com']
53        self.TEST_CASES = {
54            'SiteBlocked_Block': self.INCLUDES_BLOCKED_URL,
55            'SiteAllowed_Show': self.EXCLUDES_BLOCKED_URL,
56            'NotSet_Show': None
57        }
58        self.STARTUP_URLS = ['chrome://policy', 'chrome://settings']
59        self.SUPPORTING_POLICIES = {
60            'DefaultNotificationsSetting': 1,
61            'BookmarkBarEnabled': True,
62            'EditBookmarksEnabled': True,
63            'RestoreOnStartupURLs': self.STARTUP_URLS,
64            'RestoreOnStartup': 4
65        }
66
67
68    def _wait_for_page_ready(self, tab):
69        """Wait for JavaScript on page in |tab| to set the pageReady flag.
70
71        @param tab: browser tab with page to load.
72
73        """
74        utils.poll_for_condition(
75            lambda: tab.EvaluateJavaScript('pageReady'),
76            exception=error.TestError('Test page is not ready.'))
77
78
79    def _are_notifications_blocked(self, tab):
80        """Check if Notifications are blocked.
81
82        @param: chrome tab which has test page loaded.
83
84        @returns True if Notifications are blocked, else returns False.
85
86        """
87        notification_permission = tab.EvaluateJavaScript(
88                'Notification.permission')
89        if notification_permission not in ['granted', 'denied', 'default']:
90            error.TestFail('Unable to capture Notification Setting.')
91        return notification_permission == 'denied'
92
93
94    def _test_notifications_blocked_for_urls(self, policy_value):
95        """Verify CrOS enforces the NotificationsBlockedForUrls policy.
96
97        When NotificationsBlockedForUrls is undefined, notifications shall be
98        allowed on all pages. When NotificationsBlockedForUrls contains one or
99        more URLs, notifications shall be blocked only on the pages whose
100        domain matches any of the listed URLs.
101
102        @param policy_value: policy value for this case.
103
104        """
105        tab = self.navigate_to_url(self.TEST_URL)
106        self._wait_for_page_ready(tab)
107        notifications_blocked = self._are_notifications_blocked(tab)
108        logging.info('Notifications are blocked: %r', notifications_blocked)
109
110        # String |WEB_HOST| will be found in string |policy_value| for
111        # cases that expect the Notifications to be blocked.
112        if policy_value is not None and self.WEB_HOST in policy_value:
113            if not notifications_blocked:
114                raise error.TestFail('Notifications should be blocked.')
115        else:
116            if notifications_blocked:
117                raise error.TestFail('Notifications should be allowed.')
118        tab.Close()
119
120
121    def run_once(self, case):
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        case_value = self.TEST_CASES[case]
128        self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value
129        self.setup_case(user_policies=self.SUPPORTING_POLICIES)
130        self._test_notifications_blocked_for_urls(case_value)
131