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/policy/core/browser/configuration_policy_handler_parameters.h"
14 #include "components/policy/core/browser/configuration_policy_pref_store.h"
15 #include "components/policy/core/browser/configuration_policy_pref_store_test.h"
16 #include "components/policy/core/common/policy_details.h"
17 #include "components/policy/core/common/policy_map.h"
18 #include "components/policy/core/common/policy_types.h"
19 #include "policy/policy_constants.h"
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/drive/file_system_util.h"
27 const char* kUserIDHash
= "deadbeef";
29 #if defined(OS_CHROMEOS)
30 const char* kDriveNamePolicyVariableName
= "${google_drive}";
31 const base::FilePath::CharType
* kRootRelativeToDriveMount
=
32 FILE_PATH_LITERAL("root");
33 const char* kRelativeToDriveRoot
= "/home/";
35 std::string
GetExpectedDownloadDirectory() {
36 return drive::util::GetDriveMountPointPathForUserIdHash(kUserIDHash
)
37 .Append(kRootRelativeToDriveMount
)
45 class DownloadDirPolicyHandlerTest
46 : public policy::ConfigurationPolicyPrefStoreTest
{
48 void SetUp() override
{
49 recommended_store_
= new policy::ConfigurationPolicyPrefStore(
50 policy_service_
.get(),
52 policy::POLICY_LEVEL_RECOMMENDED
);
53 handler_list_
.AddHandler(
54 make_scoped_ptr
<policy::ConfigurationPolicyHandler
>(
55 new DownloadDirPolicyHandler
));
58 void PopulatePolicyHandlerParameters(
59 policy::PolicyHandlerParameters
* parameters
) override
{
60 parameters
->user_id_hash
= kUserIDHash
;
64 scoped_refptr
<policy::ConfigurationPolicyPrefStore
> recommended_store_
;
67 #if !defined(OS_CHROMEOS)
68 TEST_F(DownloadDirPolicyHandlerTest
, SetDownloadDirectory
) {
69 policy::PolicyMap policy
;
70 EXPECT_FALSE(store_
->GetValue(prefs::kPromptForDownload
, NULL
));
71 policy
.Set(policy::key::kDownloadDirectory
,
72 policy::POLICY_LEVEL_MANDATORY
,
73 policy::POLICY_SCOPE_USER
,
74 new base::StringValue(std::string()),
76 UpdateProviderPolicy(policy
);
78 // Setting a DownloadDirectory should disable the PromptForDownload pref.
79 const base::Value
* value
= NULL
;
80 EXPECT_TRUE(store_
->GetValue(prefs::kPromptForDownload
, &value
));
82 bool prompt_for_download
= true;
83 bool result
= value
->GetAsBoolean(&prompt_for_download
);
85 EXPECT_FALSE(prompt_for_download
);
89 #if defined(OS_CHROMEOS)
90 TEST_F(DownloadDirPolicyHandlerTest
, SetDownloadToDrive
) {
91 EXPECT_FALSE(store_
->GetValue(prefs::kPromptForDownload
, NULL
));
93 policy::PolicyMap policy
;
94 policy
.Set(policy::key::kDownloadDirectory
,
95 policy::POLICY_LEVEL_MANDATORY
,
96 policy::POLICY_SCOPE_USER
,
97 new base::StringValue(kDriveNamePolicyVariableName
),
99 UpdateProviderPolicy(policy
);
101 const base::Value
* value
= NULL
;
102 bool prompt_for_download
;
103 EXPECT_TRUE(store_
->GetValue(prefs::kPromptForDownload
, &value
));
105 EXPECT_TRUE(value
->GetAsBoolean(&prompt_for_download
));
106 EXPECT_FALSE(prompt_for_download
);
109 EXPECT_TRUE(store_
->GetValue(prefs::kDisableDrive
, &value
));
111 EXPECT_TRUE(value
->GetAsBoolean(&disable_drive
));
112 EXPECT_FALSE(disable_drive
);
114 std::string download_directory
;
115 EXPECT_TRUE(store_
->GetValue(prefs::kDownloadDefaultDirectory
, &value
));
117 EXPECT_TRUE(value
->GetAsString(&download_directory
));
118 EXPECT_EQ(GetExpectedDownloadDirectory(), download_directory
);
120 policy
.Set(policy::key::kDownloadDirectory
,
121 policy::POLICY_LEVEL_MANDATORY
,
122 policy::POLICY_SCOPE_USER
,
123 new base::StringValue(kUserIDHash
),
125 UpdateProviderPolicy(policy
);
126 EXPECT_FALSE(recommended_store_
->GetValue(prefs::kDisableDrive
, NULL
));
128 policy
.Set(policy::key::kDownloadDirectory
,
129 policy::POLICY_LEVEL_RECOMMENDED
,
130 policy::POLICY_SCOPE_USER
,
131 new base::StringValue(std::string(kDriveNamePolicyVariableName
) +
132 kRelativeToDriveRoot
),
134 UpdateProviderPolicy(policy
);
136 EXPECT_FALSE(recommended_store_
->GetValue(prefs::kPromptForDownload
, NULL
));
137 EXPECT_FALSE(recommended_store_
->GetValue(prefs::kDisableDrive
, NULL
));
140 recommended_store_
->GetValue(prefs::kDownloadDefaultDirectory
, &value
));
142 EXPECT_TRUE(value
->GetAsString(&download_directory
));
143 EXPECT_EQ(GetExpectedDownloadDirectory() + kRelativeToDriveRoot
,
146 policy
.Set(policy::key::kDownloadDirectory
,
147 policy::POLICY_LEVEL_RECOMMENDED
,
148 policy::POLICY_SCOPE_USER
,
149 new base::StringValue(kUserIDHash
),
151 UpdateProviderPolicy(policy
);
153 EXPECT_FALSE(recommended_store_
->GetValue(prefs::kPromptForDownload
, NULL
));
154 EXPECT_FALSE(recommended_store_
->GetValue(prefs::kDisableDrive
, NULL
));
157 recommended_store_
->GetValue(prefs::kDownloadDefaultDirectory
, &value
));
159 EXPECT_TRUE(value
->GetAsString(&download_directory
));
160 EXPECT_EQ(kUserIDHash
, download_directory
);