• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ImagesBlockedForUrls(enterprise_policy_base.EnterprisePolicyTest):
12    """Test ImagesBlockedForUrls 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 ImagesBlockedForUrls user policies. These values are covered by
16    four test cases, named: NotSet_Allow, 1Url_Block, 2Urls_Allow, and
17    3Urls_Block.
18
19    When the policy value is None (as in case=NotSet_Allow), then images are
20    not blocked on any page. When the value is set to a single domain (such as
21    case=1Url_Block), then images are blocked on any page with that domain.
22    When set to multiple domains (as in case=2Urls_Allow or 3Urls_Block),
23    then images are blocked on any page with a domain that matches any of the
24    listed domains.
25
26    Two test cases (1Url_Block, 3Urls_Block) are designed to block images
27    on the test page. The other two test cases (NotSet_Allow, 2Urls_Allow)
28    are designed to allow images to be shown on the test page.
29
30    Note this test has a dependency on the DefaultImagesSetting policy, which
31    is partially tested herein, and by the test policy_ImagesAllowedForUrls.
32    For this test, we set DefaultImagesSetting=1 (or null). This allows images
33    on all pages except those with a domain listed in ImagesBlockedForUrls.
34    For the test of ImagesAllowedForUrls, we set DefaultImagesSetting=2.
35    That blocks images on all pages except those with domains listed in
36    ImagesAllowedForUrls.
37
38    """
39    version = 1
40
41    def initialize(self, **kwargs):
42        self._initialize_test_constants()
43        super(policy_ImagesBlockedForUrls, self).initialize(**kwargs)
44        self.start_webserver()
45
46
47    def _initialize_test_constants(self):
48        """Initialize test-specific constants, some from class constants."""
49        self.POLICY_NAME = 'ImagesBlockedForUrls'
50        self.TEST_FILE = 'kittens.html'
51        self.TEST_URL = '%s/%s' % (self.WEB_HOST, self.TEST_FILE)
52        self.MINIMUM_IMAGE_WIDTH = 640
53        self.URL1_DATA = [self.WEB_HOST]
54        self.URL2_DATA = ['http://www.bing.com', 'https://www.yahoo.com']
55        self.URL3_DATA = ['http://www.bing.com', self.WEB_HOST,
56                          'https://www.yahoo.com']
57        self.TEST_CASES = {
58            'NotSet_Allow': None,
59            '1Url_Block': self.URL1_DATA,
60            '2Urls_Allow': self.URL2_DATA,
61            '3Urls_Block': self.URL3_DATA
62        }
63        self.STARTUP_URLS = ['chrome://policy', 'chrome://settings']
64        self.SUPPORTING_POLICIES = {
65            'DefaultImagesSetting': 1,
66            'BookmarkBarEnabled': False,
67            'RestoreOnStartupURLs': self.STARTUP_URLS,
68            'RestoreOnStartup': 4
69        }
70
71
72    def _wait_for_page_ready(self, tab):
73        utils.poll_for_condition(
74            lambda: tab.EvaluateJavaScript('pageReady'),
75            exception=error.TestError('Test page is not ready.'))
76
77
78    def _is_image_blocked(self, tab):
79        image_width = tab.EvaluateJavaScript(
80            "document.getElementById('kittens_id').naturalWidth")
81        return image_width < self.MINIMUM_IMAGE_WIDTH
82
83
84    def _test_images_blocked_for_urls(self, policy_value):
85        """Verify CrOS enforces the ImagesBlockedForUrls policy.
86
87        When ImagesBlockedForUrls is undefined, images shall be allowed on
88        all pages. When ImagesBlockedForUrls contains one or more URLs, images
89        shall be blocked on any page whose domain matches any of the listed
90        domains.
91
92        @param policy_value: policy value for this case.
93        """
94        tab = self.navigate_to_url(self.TEST_URL)
95        self._wait_for_page_ready(tab)
96        image_is_blocked = self._is_image_blocked(tab)
97
98        # String |WEB_HOST| shall be found in string |policy_value| for test
99        # cases 1Url_Block and 3Urls_Block, but not for NotSet_Allow and
100        # 2Urls_Allow.
101        if policy_value is not None and self.WEB_HOST in policy_value:
102            if not image_is_blocked:
103                raise error.TestFail('Image should be blocked.')
104        else:
105            if image_is_blocked:
106                raise error.TestFail('Image should not be blocked.')
107        tab.Close()
108
109
110    def run_test_case(self, case):
111        """Setup and run the test configured for the specified test case.
112
113        @param case: Name of the test case to run.
114        """
115        case_value = self.TEST_CASES[case]
116        self.setup_case(self.POLICY_NAME, case_value, self.SUPPORTING_POLICIES)
117        self._test_images_blocked_for_urls(case_value)
118