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_change.h"
8 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
9 #include "content/public/test/test_utils.h"
10 #include "google_apis/drive/test_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
14 namespace file_system
{
16 typedef OperationTestBase CreateFileOperationTest
;
18 TEST_F(CreateFileOperationTest
, CreateFile
) {
19 CreateFileOperation
operation(blocking_task_runner(),
23 const base::FilePath
kFilePath(FILE_PATH_LITERAL("drive/root/New File.txt"));
24 FileError error
= FILE_ERROR_FAILED
;
28 std::string(), // no predetermined mime type
29 google_apis::test_util::CreateCopyResultCallback(&error
));
30 content::RunAllBlockingPoolTasksUntilIdle();
31 EXPECT_EQ(FILE_ERROR_OK
, error
);
34 EXPECT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(kFilePath
, &entry
));
35 EXPECT_EQ(ResourceEntry::DIRTY
, entry
.metadata_edit_state());
36 EXPECT_FALSE(base::Time::FromInternalValue(
37 entry
.file_info().last_modified()).is_null());
38 EXPECT_FALSE(base::Time::FromInternalValue(
39 entry
.file_info().last_accessed()).is_null());
41 EXPECT_EQ(1u, delegate()->get_changed_files().size());
42 EXPECT_EQ(1u, delegate()->get_changed_files().count(kFilePath
));
43 EXPECT_EQ(1u, delegate()->updated_local_ids().size());
44 EXPECT_EQ(1u, delegate()->updated_local_ids().count(entry
.local_id()));
47 TEST_F(CreateFileOperationTest
, CreateFileIsExclusive
) {
48 CreateFileOperation
operation(blocking_task_runner(),
52 const base::FilePath
kExistingFile(
53 FILE_PATH_LITERAL("drive/root/File 1.txt"));
54 const base::FilePath
kExistingDirectory(
55 FILE_PATH_LITERAL("drive/root/Directory 1"));
56 const base::FilePath
kNonExistingFile(
57 FILE_PATH_LITERAL("drive/root/Directory 1/not exist.png"));
58 const base::FilePath
kFileInNonExistingDirectory(
59 FILE_PATH_LITERAL("drive/root/not exist/not exist.png"));
61 // Create fails if is_exclusive = true and a file exists.
62 FileError error
= FILE_ERROR_FAILED
;
66 std::string(), // no predetermined mime type
67 google_apis::test_util::CreateCopyResultCallback(&error
));
68 content::RunAllBlockingPoolTasksUntilIdle();
69 EXPECT_EQ(FILE_ERROR_EXISTS
, error
);
71 // Create succeeds if is_exclusive = false and a file exists.
74 false, // is_exclusive
75 std::string(), // no predetermined mime type
76 google_apis::test_util::CreateCopyResultCallback(&error
));
77 content::RunAllBlockingPoolTasksUntilIdle();
78 EXPECT_EQ(FILE_ERROR_OK
, error
);
80 // Create fails if a directory existed even when is_exclusive = false.
83 false, // is_exclusive
84 std::string(), // no predetermined mime type
85 google_apis::test_util::CreateCopyResultCallback(&error
));
86 content::RunAllBlockingPoolTasksUntilIdle();
87 EXPECT_EQ(FILE_ERROR_EXISTS
, error
);
89 // Create succeeds if no entry exists.
93 std::string(), // no predetermined mime type
94 google_apis::test_util::CreateCopyResultCallback(&error
));
95 content::RunAllBlockingPoolTasksUntilIdle();
96 EXPECT_EQ(FILE_ERROR_OK
, error
);
98 // Create fails if the parent directory does not exist.
100 kFileInNonExistingDirectory
,
101 false, // is_exclusive
102 std::string(), // no predetermined mime type
103 google_apis::test_util::CreateCopyResultCallback(&error
));
104 content::RunAllBlockingPoolTasksUntilIdle();
105 EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY
, error
);
108 TEST_F(CreateFileOperationTest
, CreateFileMimeType
) {
109 CreateFileOperation
operation(blocking_task_runner(),
113 const base::FilePath
kPng1(FILE_PATH_LITERAL("drive/root/1.png"));
114 const base::FilePath
kPng2(FILE_PATH_LITERAL("drive/root/2.png"));
115 const base::FilePath
kUnknown(FILE_PATH_LITERAL("drive/root/3.unknown"));
116 const std::string
kSpecialMimeType("application/x-createfile-test");
118 FileError error
= FILE_ERROR_FAILED
;
119 operation
.CreateFile(
121 false, // is_exclusive
122 std::string(), // no predetermined mime type
123 google_apis::test_util::CreateCopyResultCallback(&error
));
124 content::RunAllBlockingPoolTasksUntilIdle();
125 EXPECT_EQ(FILE_ERROR_OK
, error
);
127 // If no mime type is specified, it is guessed from the file name.
129 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(kPng1
, &entry
));
130 EXPECT_EQ("image/png", entry
.file_specific_info().content_mime_type());
132 error
= FILE_ERROR_FAILED
;
133 operation
.CreateFile(
135 false, // is_exclusive
137 google_apis::test_util::CreateCopyResultCallback(&error
));
138 content::RunAllBlockingPoolTasksUntilIdle();
139 EXPECT_EQ(FILE_ERROR_OK
, error
);
141 // If the mime type is explicitly set, respect it.
142 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(kPng2
, &entry
));
143 EXPECT_EQ(kSpecialMimeType
, entry
.file_specific_info().content_mime_type());
145 error
= FILE_ERROR_FAILED
;
146 operation
.CreateFile(
148 false, // is_exclusive
149 std::string(), // no predetermined mime type
150 google_apis::test_util::CreateCopyResultCallback(&error
));
151 content::RunAllBlockingPoolTasksUntilIdle();
152 EXPECT_EQ(FILE_ERROR_OK
, error
);
154 // If the mime type is not set and unknown, default to octet-stream.
155 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(kUnknown
, &entry
));
156 EXPECT_EQ("application/octet-stream",
157 entry
.file_specific_info().content_mime_type());
160 } // namespace file_system