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.
4import time
5
6from autotest_lib.client.common_lib import error
7from autotest_lib.client.cros.enterprise import enterprise_policy_base
8
9
10class policy_DeviceAllowBluetooth(
11        enterprise_policy_base.EnterprisePolicyTest):
12    """
13    Test for the DeviceAllowBluetooth policy.
14
15    If the policy is set to True/None then bluetooth button is available in
16    status tray menu. If the policy is set to False then bluetooth button
17    is not available.
18
19    """
20    version = 1
21    _POLICY = 'DeviceAllowBluetooth'
22
23    def _is_bluetooth_button_present(self, ext):
24        bt_present = ext.EvaluateJavaScript("""
25        var root;
26        chrome.automation.getDesktop(r => root = r);
27        bt = root.find({attributes: {role: "button", name: /Bluetooth/}});
28        bt;
29        """)
30        if bt_present is None:
31            return False
32        return True
33
34    def bluetooth_check(self, case):
35        # Click the status tray button in bottom right.
36        ext = self.cr.autotest_ext
37        ext.ExecuteJavaScript("""
38        chrome.automation.getDesktop(root => {
39            var button_to_click = root.find(
40                {attributes: {
41                    role: "button", name: /Status tray/}}).doDefault();
42        });
43        """)
44        time.sleep(1)
45
46        bluetooth_button = self._is_bluetooth_button_present(ext)
47
48        if case is False:
49            if bluetooth_button:
50                raise error.TestFail(
51                    'Bluetooth option is available and it should not be')
52        else:
53            if not bluetooth_button:
54                raise error.TestFail(
55                    'Bluetooth option should be available but it is not.')
56
57    def run_once(self, case):
58        """
59        Entry point of this test.
60
61        @param case: True, False, or None for the value of the policy.
62
63        """
64        self.setup_case(
65                device_policies={self._POLICY: case},
66                enroll=True)
67        self.bluetooth_check(case)
68