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