1# Copyright 2019 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.
4from autotest_lib.client.common_lib import error
5from autotest_lib.client.cros.enterprise import enterprise_policy_base
6
7
8class policy_DefaultGeolocationSetting(
9        enterprise_policy_base.EnterprisePolicyTest):
10    """
11    Test effect of DefaultGeolocationSetting policy on Chrome OS.
12
13    This test will set the policy, then will check the geolocation setting
14    to determine if the policy worked or not.
15
16    The policy sets the result to variable so its easier to call/return
17    via the EvaluateJavaScript function.
18
19    """
20    version = 1
21
22    POLICY_NAME = 'DefaultGeolocationSetting'
23    policy_setting = {'granted': 1,
24                      'denied': 2,
25                      'prompt': 3,
26                      'not_set': None}
27
28    def _notification_check(self, case):
29        """
30        Navigates to a page, and query the geolocation status.
31
32        @param case: the setting of the DefaultGeolocationSetting Policy
33
34        """
35        tab = self.navigate_to_url('chrome://policy')
36
37        geo_status = """navigator.permissions.query({name:'geolocation'})
38                          .then(function(permissionStatus) {
39                            geo_result = permissionStatus.state;
40                           });"""
41
42        tab.EvaluateJavaScript(geo_status)
43        content = tab.EvaluateJavaScript('geo_result')
44
45        # prompt is the default setting
46        if case == 'not_set':
47            case = 'prompt'
48        if content != case:
49            raise error.TestError('Geolocation Setting did not match setting.'
50                                  'Expected {e} but received {r}'
51                                  .format(e=case, r=content))
52
53    def run_once(self, case):
54        """
55        Setup and run the test configured for the specified test case.
56
57        @param case: Name of the test case to run.
58
59        """
60        self.setup_case(user_policies={
61            self.POLICY_NAME: self.policy_setting[case]})
62        self._notification_check(case)
63