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(),
23 const base::FilePath
kExistingFile(
24 FILE_PATH_LITERAL("drive/root/File 1.txt"));
25 const base::FilePath
kExistingDirectory(
26 FILE_PATH_LITERAL("drive/root/Directory 1"));
27 const base::FilePath
kNonExistingFile(
28 FILE_PATH_LITERAL("drive/root/Directory 1/not exist.png"));
29 const base::FilePath
kFileInNonExistingDirectory(
30 FILE_PATH_LITERAL("drive/root/not exist/not exist.png"));
32 // Create fails if is_exclusive = true and a file exists.
33 FileError error
= FILE_ERROR_FAILED
;
37 std::string(), // no predetermined mime type
38 google_apis::test_util::CreateCopyResultCallback(&error
));
39 test_util::RunBlockingPoolTask();
40 EXPECT_EQ(FILE_ERROR_EXISTS
, error
);
42 // Create succeeds if is_exclusive = false and a file exists.
45 false, // is_exclusive
46 std::string(), // no predetermined mime type
47 google_apis::test_util::CreateCopyResultCallback(&error
));
48 test_util::RunBlockingPoolTask();
49 EXPECT_EQ(FILE_ERROR_OK
, error
);
51 // Create fails if a directory existed even when is_exclusive = false.
54 false, // is_exclusive
55 std::string(), // no predetermined mime type
56 google_apis::test_util::CreateCopyResultCallback(&error
));
57 test_util::RunBlockingPoolTask();
58 EXPECT_EQ(FILE_ERROR_EXISTS
, error
);
60 // Create succeeds if no entry exists.
64 std::string(), // no predetermined mime type
65 google_apis::test_util::CreateCopyResultCallback(&error
));
66 test_util::RunBlockingPoolTask();
67 EXPECT_EQ(FILE_ERROR_OK
, error
);
69 // Create fails if the parent directory does not exist.
71 kFileInNonExistingDirectory
,
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_NOT_A_DIRECTORY
, error
);
79 TEST_F(CreateFileOperationTest
, CreateFileMimeType
) {
80 CreateFileOperation
operation(blocking_task_runner(),
86 const base::FilePath
kPng1(FILE_PATH_LITERAL("drive/root/1.png"));
87 const base::FilePath
kPng2(FILE_PATH_LITERAL("drive/root/2.png"));
88 const base::FilePath
kUnknown(FILE_PATH_LITERAL("drive/root/3.unknown"));
89 const std::string
kSpecialMimeType("application/x-createfile-test");
91 FileError error
= FILE_ERROR_FAILED
;
94 false, // is_exclusive
95 std::string(), // no predetermined mime type
96 google_apis::test_util::CreateCopyResultCallback(&error
));
97 test_util::RunBlockingPoolTask();
98 EXPECT_EQ(FILE_ERROR_OK
, error
);
100 // If no mime type is specified, it is guessed from the file name.
102 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(kPng1
, &entry
));
103 EXPECT_EQ("image/png", entry
.file_specific_info().content_mime_type());
105 error
= FILE_ERROR_FAILED
;
106 operation
.CreateFile(
108 false, // is_exclusive
110 google_apis::test_util::CreateCopyResultCallback(&error
));
111 test_util::RunBlockingPoolTask();
112 EXPECT_EQ(FILE_ERROR_OK
, error
);
114 // If the mime type is explicitly set, respect it.
115 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(kPng2
, &entry
));
116 EXPECT_EQ(kSpecialMimeType
, entry
.file_specific_info().content_mime_type());
118 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 test_util::RunBlockingPoolTask();
125 EXPECT_EQ(FILE_ERROR_OK
, error
);
127 // If the mime type is not set and unknown, default to octet-stream.
128 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(kUnknown
, &entry
));
129 EXPECT_EQ("application/octet-stream",
130 entry
.file_specific_info().content_mime_type());
134 } // namespace file_system