1 // Copyright 2013 The Chromium 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.
7 #include "base/compiler_specific.h"
8 #include "base/files/file_path.h"
9 #include "base/values.h"
10 #include "chrome/browser/download/download_dir_policy_handler.h"
11 #include "chrome/browser/download/download_prefs.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/drive/drive_pref_names.h"
14 #include "components/policy/core/browser/configuration_policy_handler_parameters.h"
15 #include "components/policy/core/browser/configuration_policy_pref_store.h"
16 #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
17 #include "components/policy/core/common/policy_details.h"
18 #include "components/policy/core/common/policy_map.h"
19 #include "components/policy/core/common/policy_types.h"
20 #include "policy/policy_constants.h"
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/chromeos/drive/file_system_util.h"
28 const char* kUserIDHash
= "deadbeef";
30 #if defined(OS_CHROMEOS)
31 const char* kDriveNamePolicyVariableName
= "${google_drive}";
32 const base::FilePath::CharType
* kRootRelativeToDriveMount
=
33 FILE_PATH_LITERAL("root");
34 const char* kRelativeToDriveRoot
= "/home/";
36 std::string
GetExpectedDownloadDirectory() {
37 return drive::util::GetDriveMountPointPathForUserIdHash(kUserIDHash
)
38 .Append(kRootRelativeToDriveMount
)
46 class DownloadDirPolicyHandlerTest
47 : public policy::ConfigurationPolicyPrefStoreTest
{
49 void SetUp() override
{
50 recommended_store_
= new policy::ConfigurationPolicyPrefStore(
51 policy_service_
.get(),
53 policy::POLICY_LEVEL_RECOMMENDED
);
54 handler_list_
.AddHandler(
55 make_scoped_ptr
<policy::ConfigurationPolicyHandler
>(
56 new DownloadDirPolicyHandler
));
59 void PopulatePolicyHandlerParameters(
60 policy::PolicyHandlerParameters
* parameters
) override
{
61 parameters
->user_id_hash
= kUserIDHash
;
65 scoped_refptr
<policy::ConfigurationPolicyPrefStore
> recommended_store_
;
68 #if !defined(OS_CHROMEOS)
69 TEST_F(DownloadDirPolicyHandlerTest
, SetDownloadDirectory
) {
70 policy::PolicyMap policy
;
71 EXPECT_FALSE(store_
->GetValue(prefs::kPromptForDownload
, NULL
));
72 policy
.Set(policy::key::kDownloadDirectory
,
73 policy::POLICY_LEVEL_MANDATORY
,
74 policy::POLICY_SCOPE_USER
,
75 new base::StringValue(std::string()),
77 UpdateProviderPolicy(policy
);
79 // Setting a DownloadDirectory should disable the PromptForDownload pref.
80 const base::Value
* value
= NULL
;
81 EXPECT_TRUE(store_
->GetValue(prefs::kPromptForDownload
, &value
));
83 bool prompt_for_download
= true;
84 bool result
= value
->GetAsBoolean(&prompt_for_download
);
86 EXPECT_FALSE(prompt_for_download
);
90 #if defined(OS_CHROMEOS)
91 TEST_F(DownloadDirPolicyHandlerTest
, SetDownloadToDrive
) {
92 EXPECT_FALSE(store_
->GetValue(prefs::kPromptForDownload
, NULL
));
94 policy::PolicyMap policy
;
95 policy
.Set(policy::key::kDownloadDirectory
,
96 policy::POLICY_LEVEL_MANDATORY
,
97 policy::POLICY_SCOPE_USER
,
98 new base::StringValue(kDriveNamePolicyVariableName
),
100 UpdateProviderPolicy(policy
);
102 const base::Value
* value
= NULL
;
103 bool prompt_for_download
;
104 EXPECT_TRUE(store_
->GetValue(prefs::kPromptForDownload
, &value
));
106 EXPECT_TRUE(value
->GetAsBoolean(&prompt_for_download
));
107 EXPECT_FALSE(prompt_for_download
);
110 EXPECT_TRUE(store_
->GetValue(drive::prefs::kDisableDrive
, &value
));
112 EXPECT_TRUE(value
->GetAsBoolean(&disable_drive
));
113 EXPECT_FALSE(disable_drive
);
115 std::string download_directory
;
116 EXPECT_TRUE(store_
->GetValue(prefs::kDownloadDefaultDirectory
, &value
));
118 EXPECT_TRUE(value
->GetAsString(&download_directory
));
119 EXPECT_EQ(GetExpectedDownloadDirectory(), download_directory
);
121 policy
.Set(policy::key::kDownloadDirectory
,
122 policy::POLICY_LEVEL_MANDATORY
,
123 policy::POLICY_SCOPE_USER
,
124 new base::StringValue(kUserIDHash
),
126 UpdateProviderPolicy(policy
);
127 EXPECT_FALSE(recommended_store_
->GetValue(drive::prefs::kDisableDrive
, NULL
));
129 policy
.Set(policy::key::kDownloadDirectory
,
130 policy::POLICY_LEVEL_RECOMMENDED
,
131 policy::POLICY_SCOPE_USER
,
132 new base::StringValue(std::string(kDriveNamePolicyVariableName
) +
133 kRelativeToDriveRoot
),
135 UpdateProviderPolicy(policy
);
137 EXPECT_FALSE(recommended_store_
->GetValue(prefs::kPromptForDownload
, NULL
));
138 EXPECT_FALSE(recommended_store_
->GetValue(drive::prefs::kDisableDrive
, NULL
));
141 recommended_store_
->GetValue(prefs::kDownloadDefaultDirectory
, &value
));
143 EXPECT_TRUE(value
->GetAsString(&download_directory
));
144 EXPECT_EQ(GetExpectedDownloadDirectory() + kRelativeToDriveRoot
,
147 policy
.Set(policy::key::kDownloadDirectory
,
148 policy::POLICY_LEVEL_RECOMMENDED
,
149 policy::POLICY_SCOPE_USER
,
150 new base::StringValue(kUserIDHash
),
152 UpdateProviderPolicy(policy
);
154 EXPECT_FALSE(recommended_store_
->GetValue(prefs::kPromptForDownload
, NULL
));
155 EXPECT_FALSE(recommended_store_
->GetValue(drive::prefs::kDisableDrive
, NULL
));
158 recommended_store_
->GetValue(prefs::kDownloadDefaultDirectory
, &value
));
160 EXPECT_TRUE(value
->GetAsString(&download_directory
));
161 EXPECT_EQ(kUserIDHash
, download_directory
);