create_file_operation_unittest.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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 "chrome/browser/google_apis/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      google_apis::test_util::CreateCopyResultCallback(&error));
38  test_util::RunBlockingPoolTask();
39  EXPECT_EQ(FILE_ERROR_EXISTS, error);
40
41  // Create succeeds if is_exclusive = false and a file exists.
42  operation.CreateFile(
43      kExistingFile,
44      false,  // is_exclusive
45      google_apis::test_util::CreateCopyResultCallback(&error));
46  test_util::RunBlockingPoolTask();
47  EXPECT_EQ(FILE_ERROR_OK, error);
48
49  // Create fails if a directory existed even when is_exclusive = false.
50  operation.CreateFile(
51      kExistingDirectory,
52      false,  // is_exclusive
53      google_apis::test_util::CreateCopyResultCallback(&error));
54  test_util::RunBlockingPoolTask();
55  EXPECT_EQ(FILE_ERROR_EXISTS, error);
56
57  // Create succeeds if no entry exists.
58  operation.CreateFile(
59      kNonExistingFile,
60      true,   // is_exclusive
61      google_apis::test_util::CreateCopyResultCallback(&error));
62  test_util::RunBlockingPoolTask();
63  EXPECT_EQ(FILE_ERROR_OK, error);
64
65  // Create fails if the parent directory does not exist.
66  operation.CreateFile(
67      kFileInNonExistingDirectory,
68      false,  // is_exclusive
69      google_apis::test_util::CreateCopyResultCallback(&error));
70  test_util::RunBlockingPoolTask();
71  EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error);
72}
73
74}  // namespace file_system
75}  // namespace drive
76