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.bin import utils
5from autotest_lib.client.common_lib import error
6from autotest_lib.client.cros.enterprise import enterprise_policy_base
7
8from telemetry.core import exceptions
9
10
11class policy_PromptForDownloadLocation(
12        enterprise_policy_base.EnterprisePolicyTest):
13    """
14    Test effect of policy_PromptForDownloadLocation policy on Chrome OS.
15
16    This test will set the policy, navigate to a test download link, then
17    check if the file is downloaded or not.
18
19    """
20    version = 1
21
22    POLICY_NAME = 'PromptForDownloadLocation'
23    _DOWNLOAD_BASE = ('http://commondatastorage.googleapis.com/'
24                      'chromiumos-test-assets-public/audio_power/')
25
26    def run_once(self, case=None):
27        """
28        Setup and run the test configured for the specified test case.
29
30        @param case: Name of the test case to run.
31
32        """
33
34        self.setup_case(user_policies={self.POLICY_NAME: case})
35
36        try:
37            self.navigate_to_url(self._DOWNLOAD_BASE)
38
39        #  The url is a test URL and doesn't actually load anything, causing
40        #  a Timeout. This is expected and OK.
41        except exceptions.TimeoutException:
42            pass
43
44        #  Check the downloads directory for the file.
45        files = utils.system_output('ls /home/chronos/user/Downloads/')
46
47        if case:
48            if 'download' in files:
49                raise error.TestError(
50                    'Download did not prompt for location when it should have.')
51        else:
52            if 'download' not in files:
53                raise error.TestError(
54                    'Download prompted for location when it should not have.')
55