Upstream version 11.40.277.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_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"
12
13 namespace drive {
14 namespace file_system {
15
16 typedef OperationTestBase CreateFileOperationTest;
17
18 TEST_F(CreateFileOperationTest, CreateFile) {
19   CreateFileOperation operation(blocking_task_runner(),
20                                 delegate(),
21                                 metadata());
22
23   const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/New File.txt"));
24   FileError error = FILE_ERROR_FAILED;
25   operation.CreateFile(
26       kFilePath,
27       true,  // is_exclusive
28       std::string(),  // no predetermined mime type
29       google_apis::test_util::CreateCopyResultCallback(&error));
30   content::RunAllBlockingPoolTasksUntilIdle();
31   EXPECT_EQ(FILE_ERROR_OK, error);
32
33   ResourceEntry entry;
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());
40
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()));
45 }
46
47 TEST_F(CreateFileOperationTest, CreateFileIsExclusive) {
48   CreateFileOperation operation(blocking_task_runner(),
49                                 delegate(),
50                                 metadata());
51
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"));
60
61   // Create fails if is_exclusive = true and a file exists.
62   FileError error = FILE_ERROR_FAILED;
63   operation.CreateFile(
64       kExistingFile,
65       true,  // is_exclusive
66       std::string(),  // no predetermined mime type
67       google_apis::test_util::CreateCopyResultCallback(&error));
68   content::RunAllBlockingPoolTasksUntilIdle();
69   EXPECT_EQ(FILE_ERROR_EXISTS, error);
70
71   // Create succeeds if is_exclusive = false and a file exists.
72   operation.CreateFile(
73       kExistingFile,
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);
79
80   // Create fails if a directory existed even when is_exclusive = false.
81   operation.CreateFile(
82       kExistingDirectory,
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);
88
89   // Create succeeds if no entry exists.
90   operation.CreateFile(
91       kNonExistingFile,
92       true,   // is_exclusive
93       std::string(),  // no predetermined mime type
94       google_apis::test_util::CreateCopyResultCallback(&error));
95   content::RunAllBlockingPoolTasksUntilIdle();
96   EXPECT_EQ(FILE_ERROR_OK, error);
97
98   // Create fails if the parent directory does not exist.
99   operation.CreateFile(
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);
106 }
107
108 TEST_F(CreateFileOperationTest, CreateFileMimeType) {
109   CreateFileOperation operation(blocking_task_runner(),
110                                 delegate(),
111                                 metadata());
112
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");
117
118   FileError error = FILE_ERROR_FAILED;
119   operation.CreateFile(
120       kPng1,
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);
126
127   // If no mime type is specified, it is guessed from the file name.
128   ResourceEntry entry;
129   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPng1, &entry));
130   EXPECT_EQ("image/png", entry.file_specific_info().content_mime_type());
131
132   error = FILE_ERROR_FAILED;
133   operation.CreateFile(
134       kPng2,
135       false,  // is_exclusive
136       kSpecialMimeType,
137       google_apis::test_util::CreateCopyResultCallback(&error));
138   content::RunAllBlockingPoolTasksUntilIdle();
139   EXPECT_EQ(FILE_ERROR_OK, error);
140
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());
144
145   error = FILE_ERROR_FAILED;
146   operation.CreateFile(
147       kUnknown,
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);
153
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());
158 }
159
160 }  // namespace file_system
161 }  // namespace drive