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 policy::POLICY_SOURCE_CLOUD
,
76 new base::StringValue(std::string()),
78 UpdateProviderPolicy(policy
);
80 // Setting a DownloadDirectory should disable the PromptForDownload pref.
81 const base::Value
* value
= NULL
;
82 EXPECT_TRUE(store_
->GetValue(prefs::kPromptForDownload
, &value
));
84 bool prompt_for_download
= true;
85 bool result
= value
->GetAsBoolean(&prompt_for_download
);
87 EXPECT_FALSE(prompt_for_download
);
91 #if defined(OS_CHROMEOS)
92 TEST_F(DownloadDirPolicyHandlerTest
, SetDownloadToDrive
) {
93 EXPECT_FALSE(store_
->GetValue(prefs::kPromptForDownload
, NULL
));
95 policy::PolicyMap policy
;
96 policy
.Set(policy::key::kDownloadDirectory
,
97 policy::POLICY_LEVEL_MANDATORY
,
98 policy::POLICY_SCOPE_USER
,
99 policy::POLICY_SOURCE_CLOUD
,
100 new base::StringValue(kDriveNamePolicyVariableName
),
102 UpdateProviderPolicy(policy
);
104 const base::Value
* value
= NULL
;
105 bool prompt_for_download
;
106 EXPECT_TRUE(store_
->GetValue(prefs::kPromptForDownload
, &value
));
108 EXPECT_TRUE(value
->GetAsBoolean(&prompt_for_download
));
109 EXPECT_FALSE(prompt_for_download
);
112 EXPECT_TRUE(store_
->GetValue(drive::prefs::kDisableDrive
, &value
));
114 EXPECT_TRUE(value
->GetAsBoolean(&disable_drive
));
115 EXPECT_FALSE(disable_drive
);
117 std::string download_directory
;
118 EXPECT_TRUE(store_
->GetValue(prefs::kDownloadDefaultDirectory
, &value
));
120 EXPECT_TRUE(value
->GetAsString(&download_directory
));
121 EXPECT_EQ(GetExpectedDownloadDirectory(), download_directory
);
123 policy
.Set(policy::key::kDownloadDirectory
,
124 policy::POLICY_LEVEL_MANDATORY
,
125 policy::POLICY_SCOPE_USER
,
126 policy::POLICY_SOURCE_CLOUD
,
127 new base::StringValue(kUserIDHash
),
129 UpdateProviderPolicy(policy
);
130 EXPECT_FALSE(recommended_store_
->GetValue(drive::prefs::kDisableDrive
, NULL
));
132 policy
.Set(policy::key::kDownloadDirectory
,
133 policy::POLICY_LEVEL_RECOMMENDED
,
134 policy::POLICY_SCOPE_USER
,
135 policy::POLICY_SOURCE_CLOUD
,
136 new base::StringValue(std::string(kDriveNamePolicyVariableName
) +
137 kRelativeToDriveRoot
),
139 UpdateProviderPolicy(policy
);
141 EXPECT_FALSE(recommended_store_
->GetValue(prefs::kPromptForDownload
, NULL
));
142 EXPECT_FALSE(recommended_store_
->GetValue(drive::prefs::kDisableDrive
, NULL
));
145 recommended_store_
->GetValue(prefs::kDownloadDefaultDirectory
, &value
));
147 EXPECT_TRUE(value
->GetAsString(&download_directory
));
148 EXPECT_EQ(GetExpectedDownloadDirectory() + kRelativeToDriveRoot
,
151 policy
.Set(policy::key::kDownloadDirectory
,
152 policy::POLICY_LEVEL_RECOMMENDED
,
153 policy::POLICY_SCOPE_USER
,
154 policy::POLICY_SOURCE_CLOUD
,
155 new base::StringValue(kUserIDHash
),
157 UpdateProviderPolicy(policy
);
159 EXPECT_FALSE(recommended_store_
->GetValue(prefs::kPromptForDownload
, NULL
));
160 EXPECT_FALSE(recommended_store_
->GetValue(drive::prefs::kDisableDrive
, NULL
));
163 recommended_store_
->GetValue(prefs::kDownloadDefaultDirectory
, &value
));
165 EXPECT_TRUE(value
->GetAsString(&download_directory
));
166 EXPECT_EQ(kUserIDHash
, download_directory
);