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