Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system / create_file_operation_unittest.cc
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.
4
5 #include "chrome/browser/chromeos/drive/file_system/create_file_operation.h"
6
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"
10
11 namespace drive {
12 namespace file_system {
13
14 typedef OperationTestBase CreateFileOperationTest;
15
16 TEST_F(CreateFileOperationTest, CreateFile) {
17   CreateFileOperation operation(blocking_task_runner(),
18                                 observer(),
19                                 metadata());
20
21   const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/New File.txt"));
22   FileError error = FILE_ERROR_FAILED;
23   operation.CreateFile(
24       kFilePath,
25       true,  // is_exclusive
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);
30
31   ResourceEntry entry;
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());
38
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()));
43 }
44
45 TEST_F(CreateFileOperationTest, CreateFileIsExclusive) {
46   CreateFileOperation operation(blocking_task_runner(),
47                                 observer(),
48                                 metadata());
49
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"));
58
59   // Create fails if is_exclusive = true and a file exists.
60   FileError error = FILE_ERROR_FAILED;
61   operation.CreateFile(
62       kExistingFile,
63       true,  // is_exclusive
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);
68
69   // Create succeeds if is_exclusive = false and a file exists.
70   operation.CreateFile(
71       kExistingFile,
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);
77
78   // Create fails if a directory existed even when is_exclusive = false.
79   operation.CreateFile(
80       kExistingDirectory,
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);
86
87   // Create succeeds if no entry exists.
88   operation.CreateFile(
89       kNonExistingFile,
90       true,   // is_exclusive
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);
95
96   // Create fails if the parent directory does not exist.
97   operation.CreateFile(
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);
104 }
105
106 TEST_F(CreateFileOperationTest, CreateFileMimeType) {
107   CreateFileOperation operation(blocking_task_runner(),
108                                 observer(),
109                                 metadata());
110
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");
115
116   FileError error = FILE_ERROR_FAILED;
117   operation.CreateFile(
118       kPng1,
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);
124
125   // If no mime type is specified, it is guessed from the file name.
126   ResourceEntry entry;
127   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPng1, &entry));
128   EXPECT_EQ("image/png", entry.file_specific_info().content_mime_type());
129
130   error = FILE_ERROR_FAILED;
131   operation.CreateFile(
132       kPng2,
133       false,  // is_exclusive
134       kSpecialMimeType,
135       google_apis::test_util::CreateCopyResultCallback(&error));
136   test_util::RunBlockingPoolTask();
137   EXPECT_EQ(FILE_ERROR_OK, error);
138
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());
142
143   error = FILE_ERROR_FAILED;
144   operation.CreateFile(
145       kUnknown,
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);
151
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());
156 }
157
158 }  // namespace file_system
159 }  // namespace drive