1# Copyright 2015 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 utils
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.cros.enterprise import enterprise_policy_base
9
10
11class policy_PopupsBlockedForUrls(enterprise_policy_base.EnterprisePolicyTest):
12    """Test PopupsBlockedForUrls policy effect on CrOS look & feel.
13
14    This test verifies the behavior of Chrome OS with a range of valid values
15    for the PopupsBlockedForUrls user policy, when DefaultPopupsSetting=1
16    (i.e., allow popups by default on all pages except those in domains listed
17    in PopupsBlockedForUrls). These valid values are covered by 4 test cases,
18    named: NotSet_Allowed, 1Url_Blocked, 2Urls_Allowed, and 3Urls_Blocked.
19
20    When the policy value is None (as in case NotSet_Allowed), then popups are
21    allowed on any page. When the value is set to one or more URLs (as in
22    1Url_Blocked, 2Urls_Allowed, and 3Urls_Blocked), popups are blocked only
23    on pages with a domain that matches any of the listed URLs, and allowed on
24    any of those that do not match.
25
26    As noted above, this test requires the DefaultPopupsSetting policy to be
27    set to 1. A related test, policy_PopupsAllowedForUrls, requires the value
28    to be set to 2. That value blocks popups on all pages except those with
29    domains listed in PopupsAllowedForUrls.
30
31    """
32    version = 1
33
34    def initialize(self, **kwargs):
35        """Initialize this test."""
36        self._initialize_test_constants()
37        super(policy_PopupsBlockedForUrls, 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 = 'PopupsBlockedForUrls'
44        self.TEST_FILE = 'popup_status.html'
45        self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE)
46
47        self.URL1_DATA = [self.WEB_HOST]
48        self.URL2_DATA = ['http://www.bing.com', 'https://www.yahoo.com']
49        self.URL3_DATA = ['http://www.bing.com', self.WEB_HOST,
50                          'https://www.yahoo.com']
51        self.TEST_CASES = {
52            'NotSet_Allow': None,
53            '1Url_Block': self.URL1_DATA,
54            '2Urls_Allow': self.URL2_DATA,
55            '3Urls_Block': self.URL3_DATA
56        }
57        self.STARTUP_URLS = ['chrome://policy', 'chrome://settings']
58        self.SUPPORTING_POLICIES = {
59            'DefaultPopupsSetting': 1,
60            'BookmarkBarEnabled': False,
61            'RestoreOnStartupURLs': self.STARTUP_URLS,
62            'RestoreOnStartup': 4
63        }
64
65    def _wait_for_page_ready(self, tab):
66        utils.poll_for_condition(
67            lambda: tab.EvaluateJavaScript('pageReady'),
68            exception=error.TestError('Test page is not ready.'))
69
70
71    def _test_popups_blocked_for_urls(self, policy_value):
72        """Verify CrOS enforces the PopupsBlockedForUrls policy.
73
74        When PopupsBlockedForUrls is undefined, popups shall be allowed on
75        all pages. When PopupsBlockedForUrls contains one or more URLs, popups
76        shall be blocked only on the pages whose domain matches any of the
77        listed URLs.
78
79        @param policy_value: policy value expected.
80        """
81        tab = self.navigate_to_url(self.TEST_URL)
82        self._wait_for_page_ready(tab)
83        is_blocked = tab.EvaluateJavaScript('isPopupBlocked();')
84
85        # String |WEB_HOST| will be found in string |policy_value| for
86        # test cases 1Url_Blocked and 3Urls_Blocked, but not for cases
87        # NotSet_Allowed and 2Urls_Allowed.
88        if policy_value is not None and self.WEB_HOST in policy_value:
89            if not is_blocked:
90                raise error.TestFail('Popups should be blocked.')
91        else:
92            if is_blocked:
93                raise error.TestFail('Popups should not be blocked.')
94        tab.Close()
95
96
97    def run_once(self, case):
98        """Setup and run the test configured for the specified test case.
99
100        @param case: Name of the test case to run.
101        """
102        case_value = self.TEST_CASES[case]
103        self.SUPPORTING_POLICIES[self.POLICY_NAME] = case_value
104        self.setup_case(user_policies=self.SUPPORTING_POLICIES)
105        self._test_popups_blocked_for_urls(case_value)
106