Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / download / download_dir_policy_handler_unittest.cc
blobc0ad58692a79ed7c39738a091d4392f4adc424c7
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.
5 #include <string>
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"
24 #endif
26 namespace {
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)
39 .value();
42 #endif
44 } // namespace
46 class DownloadDirPolicyHandlerTest
47 : public policy::ConfigurationPolicyPrefStoreTest {
48 public:
49 void SetUp() override {
50 recommended_store_ = new policy::ConfigurationPolicyPrefStore(
51 policy_service_.get(),
52 &handler_list_,
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;
64 protected:
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()),
76 NULL);
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));
82 ASSERT_TRUE(value);
83 bool prompt_for_download = true;
84 bool result = value->GetAsBoolean(&prompt_for_download);
85 ASSERT_TRUE(result);
86 EXPECT_FALSE(prompt_for_download);
88 #endif
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),
99 NULL);
100 UpdateProviderPolicy(policy);
102 const base::Value* value = NULL;
103 bool prompt_for_download;
104 EXPECT_TRUE(store_->GetValue(prefs::kPromptForDownload, &value));
105 EXPECT_TRUE(value);
106 EXPECT_TRUE(value->GetAsBoolean(&prompt_for_download));
107 EXPECT_FALSE(prompt_for_download);
109 bool disable_drive;
110 EXPECT_TRUE(store_->GetValue(drive::prefs::kDisableDrive, &value));
111 EXPECT_TRUE(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));
117 EXPECT_TRUE(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),
125 NULL);
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),
134 NULL);
135 UpdateProviderPolicy(policy);
137 EXPECT_FALSE(recommended_store_->GetValue(prefs::kPromptForDownload, NULL));
138 EXPECT_FALSE(recommended_store_->GetValue(drive::prefs::kDisableDrive, NULL));
140 EXPECT_TRUE(
141 recommended_store_->GetValue(prefs::kDownloadDefaultDirectory, &value));
142 EXPECT_TRUE(value);
143 EXPECT_TRUE(value->GetAsString(&download_directory));
144 EXPECT_EQ(GetExpectedDownloadDirectory() + kRelativeToDriveRoot,
145 download_directory);
147 policy.Set(policy::key::kDownloadDirectory,
148 policy::POLICY_LEVEL_RECOMMENDED,
149 policy::POLICY_SCOPE_USER,
150 new base::StringValue(kUserIDHash),
151 NULL);
152 UpdateProviderPolicy(policy);
154 EXPECT_FALSE(recommended_store_->GetValue(prefs::kPromptForDownload, NULL));
155 EXPECT_FALSE(recommended_store_->GetValue(drive::prefs::kDisableDrive, NULL));
157 EXPECT_TRUE(
158 recommended_store_->GetValue(prefs::kDownloadDefaultDirectory, &value));
159 EXPECT_TRUE(value);
160 EXPECT_TRUE(value->GetAsString(&download_directory));
161 EXPECT_EQ(kUserIDHash, download_directory);
163 #endif