create_directory_operation_unittest.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
1// Copyright (c) 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_directory_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
14class CreateDirectoryOperationTest : public OperationTestBase {
15 protected:
16  // Returns FILE_ERROR_OK if a directory is found at |path|.
17  FileError FindDirectory(const base::FilePath& path) {
18    ResourceEntry entry;
19    FileError error = GetLocalResourceEntry(path, &entry);
20    if (error == FILE_ERROR_OK && !entry.file_info().is_directory())
21      error = FILE_ERROR_NOT_A_DIRECTORY;
22    return error;
23  }
24};
25
26TEST_F(CreateDirectoryOperationTest, CreateDirectory) {
27  CreateDirectoryOperation operation(blocking_task_runner(),
28                                     observer(),
29                                     scheduler(),
30                                     metadata());
31
32  const base::FilePath kExistingFile(
33      FILE_PATH_LITERAL("drive/root/File 1.txt"));
34  const base::FilePath kExistingDirectory(
35      FILE_PATH_LITERAL("drive/root/Directory 1"));
36  const base::FilePath kNewDirectory1(
37      FILE_PATH_LITERAL("drive/root/New Directory"));
38  const base::FilePath kNewDirectory2 =
39      kNewDirectory1.AppendASCII("New Directory 2/a/b/c");
40
41  // Create a new directory, not recursively.
42  EXPECT_EQ(FILE_ERROR_NOT_FOUND, FindDirectory(kNewDirectory1));
43
44  FileError error = FILE_ERROR_FAILED;
45  operation.CreateDirectory(
46      kNewDirectory1,
47      true, // is_exclusive
48      false,  // is_recursive
49      google_apis::test_util::CreateCopyResultCallback(&error));
50  google_apis::test_util::RunBlockingPoolTask();
51  EXPECT_EQ(FILE_ERROR_OK, error);
52  EXPECT_EQ(FILE_ERROR_OK, FindDirectory(kNewDirectory1));
53  EXPECT_EQ(1U, observer()->get_changed_paths().size());
54  EXPECT_EQ(1U,
55            observer()->get_changed_paths().count(kNewDirectory1.DirName()));
56
57  // Create a new directory recursively.
58  EXPECT_EQ(FILE_ERROR_NOT_FOUND, FindDirectory(kNewDirectory2));
59  operation.CreateDirectory(
60      kNewDirectory2,
61      true, // is_exclusive
62      false,  // is_recursive
63      google_apis::test_util::CreateCopyResultCallback(&error));
64  google_apis::test_util::RunBlockingPoolTask();
65  EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
66  EXPECT_EQ(FILE_ERROR_NOT_FOUND, FindDirectory(kNewDirectory2));
67
68  operation.CreateDirectory(
69      kNewDirectory2,
70      true, // is_exclusive
71      true,  // is_recursive
72      google_apis::test_util::CreateCopyResultCallback(&error));
73  google_apis::test_util::RunBlockingPoolTask();
74  EXPECT_EQ(FILE_ERROR_OK, error);
75  EXPECT_EQ(FILE_ERROR_OK, FindDirectory(kNewDirectory2));
76
77  // Try to create an existing directory.
78  operation.CreateDirectory(
79      kExistingDirectory,
80      true, // is_exclusive
81      false,  // is_recursive
82      google_apis::test_util::CreateCopyResultCallback(&error));
83  google_apis::test_util::RunBlockingPoolTask();
84  EXPECT_EQ(FILE_ERROR_EXISTS, error);
85
86  operation.CreateDirectory(
87      kExistingDirectory,
88      false, // is_exclusive
89      false,  // is_recursive
90      google_apis::test_util::CreateCopyResultCallback(&error));
91  google_apis::test_util::RunBlockingPoolTask();
92  EXPECT_EQ(FILE_ERROR_OK, error);
93
94  // Try to create a directory with a path for an existing file.
95  operation.CreateDirectory(
96      kExistingFile,
97      false, // is_exclusive
98      true,  // is_recursive
99      google_apis::test_util::CreateCopyResultCallback(&error));
100  google_apis::test_util::RunBlockingPoolTask();
101  EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
102
103  // Try to create a directory under a file.
104  operation.CreateDirectory(
105      kExistingFile.AppendASCII("New Directory"),
106      false, // is_exclusive
107      true,  // is_recursive
108      google_apis::test_util::CreateCopyResultCallback(&error));
109  google_apis::test_util::RunBlockingPoolTask();
110  EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
111}
112
113}  // namespace file_system
114}  // namespace drive
115