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 "chrome/browser/chromeos/drive/file_system/create_file_operation.h"
7 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
8 #include "google_apis/drive/test_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
12 namespace file_system {
14 typedef OperationTestBase CreateFileOperationTest;
16 TEST_F(CreateFileOperationTest, CreateFile) {
17 CreateFileOperation operation(blocking_task_runner(),
21 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/New File.txt"));
22 FileError error = FILE_ERROR_FAILED;
26 std::string(), // no predetermined mime type
27 google_apis::test_util::CreateCopyResultCallback(&error));
28 test_util::RunBlockingPoolTask();
29 EXPECT_EQ(FILE_ERROR_OK, error);
32 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kFilePath, &entry));
33 EXPECT_EQ(ResourceEntry::DIRTY, entry.metadata_edit_state());
34 EXPECT_FALSE(base::Time::FromInternalValue(
35 entry.file_info().last_modified()).is_null());
36 EXPECT_FALSE(base::Time::FromInternalValue(
37 entry.file_info().last_accessed()).is_null());
39 EXPECT_EQ(1u, observer()->get_changed_paths().size());
40 EXPECT_EQ(1u, observer()->get_changed_paths().count(kFilePath.DirName()));
41 EXPECT_EQ(1u, observer()->updated_local_ids().size());
42 EXPECT_EQ(1u, observer()->updated_local_ids().count(entry.local_id()));
45 TEST_F(CreateFileOperationTest, CreateFileIsExclusive) {
46 CreateFileOperation operation(blocking_task_runner(),
50 const base::FilePath kExistingFile(
51 FILE_PATH_LITERAL("drive/root/File 1.txt"));
52 const base::FilePath kExistingDirectory(
53 FILE_PATH_LITERAL("drive/root/Directory 1"));
54 const base::FilePath kNonExistingFile(
55 FILE_PATH_LITERAL("drive/root/Directory 1/not exist.png"));
56 const base::FilePath kFileInNonExistingDirectory(
57 FILE_PATH_LITERAL("drive/root/not exist/not exist.png"));
59 // Create fails if is_exclusive = true and a file exists.
60 FileError error = FILE_ERROR_FAILED;
64 std::string(), // no predetermined mime type
65 google_apis::test_util::CreateCopyResultCallback(&error));
66 test_util::RunBlockingPoolTask();
67 EXPECT_EQ(FILE_ERROR_EXISTS, error);
69 // Create succeeds if is_exclusive = false and a file exists.
72 false, // is_exclusive
73 std::string(), // no predetermined mime type
74 google_apis::test_util::CreateCopyResultCallback(&error));
75 test_util::RunBlockingPoolTask();
76 EXPECT_EQ(FILE_ERROR_OK, error);
78 // Create fails if a directory existed even when is_exclusive = false.
81 false, // is_exclusive
82 std::string(), // no predetermined mime type
83 google_apis::test_util::CreateCopyResultCallback(&error));
84 test_util::RunBlockingPoolTask();
85 EXPECT_EQ(FILE_ERROR_EXISTS, error);
87 // Create succeeds if no entry exists.
91 std::string(), // no predetermined mime type
92 google_apis::test_util::CreateCopyResultCallback(&error));
93 test_util::RunBlockingPoolTask();
94 EXPECT_EQ(FILE_ERROR_OK, error);
96 // Create fails if the parent directory does not exist.
98 kFileInNonExistingDirectory,
99 false, // is_exclusive
100 std::string(), // no predetermined mime type
101 google_apis::test_util::CreateCopyResultCallback(&error));
102 test_util::RunBlockingPoolTask();
103 EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error);
106 TEST_F(CreateFileOperationTest, CreateFileMimeType) {
107 CreateFileOperation operation(blocking_task_runner(),
111 const base::FilePath kPng1(FILE_PATH_LITERAL("drive/root/1.png"));
112 const base::FilePath kPng2(FILE_PATH_LITERAL("drive/root/2.png"));
113 const base::FilePath kUnknown(FILE_PATH_LITERAL("drive/root/3.unknown"));
114 const std::string kSpecialMimeType("application/x-createfile-test");
116 FileError error = FILE_ERROR_FAILED;
117 operation.CreateFile(
119 false, // is_exclusive
120 std::string(), // no predetermined mime type
121 google_apis::test_util::CreateCopyResultCallback(&error));
122 test_util::RunBlockingPoolTask();
123 EXPECT_EQ(FILE_ERROR_OK, error);
125 // If no mime type is specified, it is guessed from the file name.
127 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPng1, &entry));
128 EXPECT_EQ("image/png", entry.file_specific_info().content_mime_type());
130 error = FILE_ERROR_FAILED;
131 operation.CreateFile(
133 false, // is_exclusive
135 google_apis::test_util::CreateCopyResultCallback(&error));
136 test_util::RunBlockingPoolTask();
137 EXPECT_EQ(FILE_ERROR_OK, error);
139 // If the mime type is explicitly set, respect it.
140 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPng2, &entry));
141 EXPECT_EQ(kSpecialMimeType, entry.file_specific_info().content_mime_type());
143 error = FILE_ERROR_FAILED;
144 operation.CreateFile(
146 false, // is_exclusive
147 std::string(), // no predetermined mime type
148 google_apis::test_util::CreateCopyResultCallback(&error));
149 test_util::RunBlockingPoolTask();
150 EXPECT_EQ(FILE_ERROR_OK, error);
152 // If the mime type is not set and unknown, default to octet-stream.
153 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kUnknown, &entry));
154 EXPECT_EQ("application/octet-stream",
155 entry.file_specific_info().content_mime_type());
158 } // namespace file_system