create_file_operation_unittest.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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
11namespace drive {
12namespace file_system {
13
14typedef OperationTestBase CreateFileOperationTest;
15
16TEST_F(CreateFileOperationTest, CreateFile) {
17  CreateFileOperation operation(blocking_task_runner(),
18                                observer(),
19                                scheduler(),
20                                metadata(),
21                                cache());
22
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"));
31
32  // Create fails if is_exclusive = true and a file exists.
33  FileError error = FILE_ERROR_FAILED;
34  operation.CreateFile(
35      kExistingFile,
36      true,  // is_exclusive
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);
41
42  // Create succeeds if is_exclusive = false and a file exists.
43  operation.CreateFile(
44      kExistingFile,
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);
50
51  // Create fails if a directory existed even when is_exclusive = false.
52  operation.CreateFile(
53      kExistingDirectory,
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);
59
60  // Create succeeds if no entry exists.
61  operation.CreateFile(
62      kNonExistingFile,
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_OK, error);
68
69  // Create fails if the parent directory does not exist.
70  operation.CreateFile(
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);
77}
78
79TEST_F(CreateFileOperationTest, CreateFileMimeType) {
80  CreateFileOperation operation(blocking_task_runner(),
81                                observer(),
82                                scheduler(),
83                                metadata(),
84                                cache());
85
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");
90
91  FileError error = FILE_ERROR_FAILED;
92  operation.CreateFile(
93      kPng1,
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);
99
100  // If no mime type is specified, it is guessed from the file name.
101  ResourceEntry entry;
102  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPng1, &entry));
103  EXPECT_EQ("image/png", entry.file_specific_info().content_mime_type());
104
105  error = FILE_ERROR_FAILED;
106  operation.CreateFile(
107      kPng2,
108      false,  // is_exclusive
109      kSpecialMimeType,
110      google_apis::test_util::CreateCopyResultCallback(&error));
111  test_util::RunBlockingPoolTask();
112  EXPECT_EQ(FILE_ERROR_OK, error);
113
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());
117
118  error = FILE_ERROR_FAILED;
119  operation.CreateFile(
120      kUnknown,
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);
126
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());
131}
132
133
134}  // namespace file_system
135}  // namespace drive
136