Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / platform_util_unittest.cc
blob97c5b32471d602edd94ca49064cfb64985e67773
1 // Copyright 2015 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 "chrome/browser/platform_util.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "chrome/browser/platform_util_internal.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 #if defined(OS_CHROMEOS)
17 #include "base/json/json_string_value_serializer.h"
18 #include "base/values.h"
19 #include "chrome/browser/chrome_content_browser_client.h"
20 #include "chrome/browser/chromeos/file_manager/app_id.h"
21 #include "chrome/browser/chromeos/fileapi/file_system_backend.h"
22 #include "chrome/browser/extensions/extension_special_storage_policy.h"
23 #include "chrome/test/base/browser_with_test_window_test.h"
24 #include "content/public/browser/browser_context.h"
25 #include "content/public/common/content_client.h"
26 #include "content/public/test/mock_special_storage_policy.h"
27 #include "extensions/browser/extension_registry.h"
28 #include "extensions/common/extension.h"
29 #include "storage/browser/fileapi/external_mount_points.h"
30 #include "storage/common/fileapi/file_system_types.h"
31 #else
32 #include "content/public/test/test_browser_thread_bundle.h"
33 #endif
35 namespace platform_util {
37 namespace {
39 #if defined(OS_CHROMEOS)
41 // ChromeContentBrowserClient subclass that sets up a custom file system backend
42 // that allows the test to grant file access to the file manager extension ID
43 // without having to install the extension.
44 class PlatformUtilTestContentBrowserClient
45 : public chrome::ChromeContentBrowserClient {
46 public:
47 void GetAdditionalFileSystemBackends(
48 content::BrowserContext* browser_context,
49 const base::FilePath& storage_partition_path,
50 ScopedVector<storage::FileSystemBackend>* additional_backends) override {
51 storage::ExternalMountPoints* external_mount_points =
52 content::BrowserContext::GetMountPoints(browser_context);
54 // New FileSystemBackend that uses our MockSpecialStoragePolicy.
55 chromeos::FileSystemBackend* backend = new chromeos::FileSystemBackend(
56 nullptr, nullptr, nullptr, external_mount_points,
57 storage::ExternalMountPoints::GetSystemInstance());
58 additional_backends->push_back(backend);
62 // Base test fixture class to be used on Chrome OS.
63 class PlatformUtilTestBase : public BrowserWithTestWindowTest {
64 protected:
65 void SetUpPlatformFixture(const base::FilePath& test_directory) {
66 content_browser_client_.reset(new PlatformUtilTestContentBrowserClient());
67 old_content_browser_client_ =
68 content::SetBrowserClientForTesting(content_browser_client_.get());
70 // The test_directory needs to be mounted for it to be accessible.
71 content::BrowserContext::GetMountPoints(GetProfile())
72 ->RegisterFileSystem("test", storage::kFileSystemTypeNativeLocal,
73 storage::FileSystemMountOption(), test_directory);
75 // To test opening a file, we are going to register a mock extension that
76 // handles .txt files. The extension doesn't actually need to exist due to
77 // the DisableShellOperationsForTesting() call which prevents the extension
78 // from being invoked.
79 std::string error;
80 int error_code = 0;
82 std::string json_manifest =
83 "{"
84 " \"manifest_version\": 2,"
85 " \"name\": \"Test extension\","
86 " \"version\": \"0\","
87 " \"app\": { \"background\": { \"scripts\": [\"main.js\"] }},"
88 " \"file_handlers\": {"
89 " \"text\": {"
90 " \"extensions\": [ \"txt\" ],"
91 " \"title\": \"Text\""
92 " }"
93 " }"
94 "}";
95 JSONStringValueDeserializer json_string_deserializer(json_manifest);
96 scoped_ptr<base::Value> manifest(
97 json_string_deserializer.Deserialize(&error_code, &error));
98 base::DictionaryValue* manifest_dictionary;
100 manifest->GetAsDictionary(&manifest_dictionary);
101 ASSERT_TRUE(manifest_dictionary);
103 scoped_refptr<extensions::Extension> extension =
104 extensions::Extension::Create(
105 test_directory.AppendASCII("invalid-extension"),
106 extensions::Manifest::INVALID_LOCATION, *manifest_dictionary,
107 extensions::Extension::NO_FLAGS, &error);
108 ASSERT_TRUE(error.empty()) << error;
109 extensions::ExtensionRegistry::Get(GetProfile())->AddEnabled(extension);
112 void TearDown() override {
113 content::ContentBrowserClient* content_browser_client =
114 content::SetBrowserClientForTesting(old_content_browser_client_);
115 old_content_browser_client_ = nullptr;
116 DCHECK_EQ(static_cast<content::ContentBrowserClient*>(
117 content_browser_client_.get()),
118 content_browser_client)
119 << "ContentBrowserClient changed during test.";
120 BrowserWithTestWindowTest::TearDown();
123 private:
124 scoped_ptr<content::ContentBrowserClient> content_browser_client_;
125 content::ContentBrowserClient* old_content_browser_client_ = nullptr;
128 #else
130 // Test fixture used by all desktop platforms other than Chrome OS.
131 class PlatformUtilTestBase : public testing::Test {
132 protected:
133 Profile* GetProfile() { return nullptr; }
134 void SetUpPlatformFixture(const base::FilePath&) {}
136 private:
137 content::TestBrowserThreadBundle thread_bundle_;
140 #endif
142 class PlatformUtilTest : public PlatformUtilTestBase {
143 public:
144 void SetUp() override {
145 ASSERT_NO_FATAL_FAILURE(PlatformUtilTestBase::SetUp());
147 static const char kTestFileData[] = "Cow says moo!";
148 const int kTestFileDataLength = arraysize(kTestFileData) - 1;
150 // This prevents platfrom_util from invoking any shell or external APIs
151 // during tests. Doing so may result in external applications being launched
152 // and intefering with tests.
153 internal::DisableShellOperationsForTesting();
155 ASSERT_TRUE(directory_.CreateUniqueTempDir());
157 // A valid file.
158 existing_file_ = directory_.path().AppendASCII("test_file.txt");
159 ASSERT_EQ(
160 kTestFileDataLength,
161 base::WriteFile(existing_file_, kTestFileData, kTestFileDataLength));
163 // A valid folder.
164 existing_folder_ = directory_.path().AppendASCII("test_folder");
165 ASSERT_TRUE(base::CreateDirectory(existing_folder_));
167 // A non-existent path.
168 nowhere_ = directory_.path().AppendASCII("nowhere");
170 SetUpPlatformFixture(directory_.path());
173 OpenOperationResult CallOpenItem(const base::FilePath& path,
174 OpenItemType item_type) {
175 base::RunLoop run_loop;
176 OpenOperationResult result = OPEN_SUCCEEDED;
177 OpenOperationCallback callback =
178 base::Bind(&OnOpenOperationDone, run_loop.QuitClosure(), &result);
179 OpenItem(GetProfile(), path, item_type, callback);
180 run_loop.Run();
181 return result;
184 base::FilePath existing_file_;
185 base::FilePath existing_folder_;
186 base::FilePath nowhere_;
188 protected:
189 base::ScopedTempDir directory_;
191 private:
192 scoped_ptr<base::RunLoop> run_loop_;
194 static void OnOpenOperationDone(const base::Closure& closure,
195 OpenOperationResult* store_result,
196 OpenOperationResult result) {
197 *store_result = result;
198 closure.Run();
202 } // namespace
204 TEST_F(PlatformUtilTest, OpenFile) {
205 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(existing_file_, OPEN_FILE));
206 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
207 CallOpenItem(existing_folder_, OPEN_FILE));
208 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenItem(nowhere_, OPEN_FILE));
211 TEST_F(PlatformUtilTest, OpenFolder) {
212 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(existing_folder_, OPEN_FOLDER));
213 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
214 CallOpenItem(existing_file_, OPEN_FOLDER));
215 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND, CallOpenItem(nowhere_, OPEN_FOLDER));
218 #if defined(OS_POSIX)
219 // Symbolic links are currently only supported on Posix. Windows technically
220 // supports it as well, but not on Windows XP.
221 class PlatformUtilPosixTest : public PlatformUtilTest {
222 public:
223 void SetUp() override {
224 ASSERT_NO_FATAL_FAILURE(PlatformUtilTest::SetUp());
226 symlink_to_file_ = directory_.path().AppendASCII("l_file.txt");
227 ASSERT_TRUE(base::CreateSymbolicLink(existing_file_, symlink_to_file_));
228 symlink_to_folder_ = directory_.path().AppendASCII("l_folder");
229 ASSERT_TRUE(base::CreateSymbolicLink(existing_folder_, symlink_to_folder_));
230 symlink_to_nowhere_ = directory_.path().AppendASCII("l_nowhere");
231 ASSERT_TRUE(base::CreateSymbolicLink(nowhere_, symlink_to_nowhere_));
234 protected:
235 base::FilePath symlink_to_file_;
236 base::FilePath symlink_to_folder_;
237 base::FilePath symlink_to_nowhere_;
239 #endif // OS_POSIX
241 #if defined(OS_CHROMEOS)
242 // ChromeOS doesn't follow symbolic links in sandboxed filesystems. So all the
243 // symbolic link tests should return PATH_NOT_FOUND.
245 TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinksChromeOS) {
246 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
247 CallOpenItem(symlink_to_file_, OPEN_FILE));
248 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
249 CallOpenItem(symlink_to_folder_, OPEN_FILE));
250 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
251 CallOpenItem(symlink_to_nowhere_, OPEN_FILE));
254 TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinksChromeOS) {
255 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
256 CallOpenItem(symlink_to_folder_, OPEN_FOLDER));
257 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
258 CallOpenItem(symlink_to_file_, OPEN_FOLDER));
259 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
260 CallOpenItem(symlink_to_nowhere_, OPEN_FOLDER));
263 TEST_F(PlatformUtilTest, OpenFileWithUnhandledFileType) {
264 base::FilePath unhandled_file =
265 directory_.path().AppendASCII("myfile.filetype");
266 ASSERT_EQ(3, base::WriteFile(unhandled_file, "cat", 3));
267 EXPECT_EQ(OPEN_FAILED_NO_HANLDER_FOR_FILE_TYPE,
268 CallOpenItem(unhandled_file, OPEN_FILE));
270 #endif // OS_CHROMEOS
272 #if defined(OS_POSIX) && !defined(OS_CHROMEOS)
273 // On all other Posix platforms, the symbolic link tests should work as
274 // expected.
276 TEST_F(PlatformUtilPosixTest, OpenFileWithPosixSymlinks) {
277 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(symlink_to_file_, OPEN_FILE));
278 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
279 CallOpenItem(symlink_to_folder_, OPEN_FILE));
280 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
281 CallOpenItem(symlink_to_nowhere_, OPEN_FILE));
284 TEST_F(PlatformUtilPosixTest, OpenFolderWithPosixSymlinks) {
285 EXPECT_EQ(OPEN_SUCCEEDED, CallOpenItem(symlink_to_folder_, OPEN_FOLDER));
286 EXPECT_EQ(OPEN_FAILED_INVALID_TYPE,
287 CallOpenItem(symlink_to_file_, OPEN_FOLDER));
288 EXPECT_EQ(OPEN_FAILED_PATH_NOT_FOUND,
289 CallOpenItem(symlink_to_nowhere_, OPEN_FOLDER));
291 #endif // OS_POSIX && !OS_CHROMEOS
293 } // namespace platform_util