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 "content/public/test/test_utils.h"
9#include "google_apis/drive/test_util.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace drive {
13namespace file_system {
14
15class CreateDirectoryOperationTest : public OperationTestBase {
16 protected:
17  // Returns FILE_ERROR_OK if a directory is found at |path|.
18  FileError FindDirectory(const base::FilePath& path) {
19    ResourceEntry entry;
20    FileError error = GetLocalResourceEntry(path, &entry);
21    if (error == FILE_ERROR_OK && !entry.file_info().is_directory())
22      error = FILE_ERROR_NOT_A_DIRECTORY;
23    return error;
24  }
25};
26
27TEST_F(CreateDirectoryOperationTest, CreateDirectory) {
28  CreateDirectoryOperation operation(blocking_task_runner(),
29                                     delegate(),
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  content::RunAllBlockingPoolTasksUntilIdle();
51  EXPECT_EQ(FILE_ERROR_OK, error);
52  EXPECT_EQ(FILE_ERROR_OK, FindDirectory(kNewDirectory1));
53  EXPECT_EQ(1U, delegate()->get_changed_files().size());
54  EXPECT_EQ(
55      1U,
56      delegate()->get_changed_files().CountDirectory(kNewDirectory1.DirName()));
57
58  ResourceEntry entry;
59  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kNewDirectory1, &entry));
60  EXPECT_EQ(ResourceEntry::DIRTY, entry.metadata_edit_state());
61  EXPECT_TRUE(entry.file_info().is_directory());
62  EXPECT_FALSE(base::Time::FromInternalValue(
63      entry.file_info().last_modified()).is_null());
64  EXPECT_FALSE(base::Time::FromInternalValue(
65      entry.file_info().last_accessed()).is_null());
66  EXPECT_EQ(1U, delegate()->updated_local_ids().size());
67  EXPECT_EQ(1U, delegate()->updated_local_ids().count(entry.local_id()));
68
69  // Create a new directory recursively.
70  EXPECT_EQ(FILE_ERROR_NOT_FOUND, FindDirectory(kNewDirectory2));
71  operation.CreateDirectory(
72      kNewDirectory2,
73      true, // is_exclusive
74      false,  // is_recursive
75      google_apis::test_util::CreateCopyResultCallback(&error));
76  content::RunAllBlockingPoolTasksUntilIdle();
77  EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
78  EXPECT_EQ(FILE_ERROR_NOT_FOUND, FindDirectory(kNewDirectory2));
79
80  operation.CreateDirectory(
81      kNewDirectory2,
82      true, // is_exclusive
83      true,  // is_recursive
84      google_apis::test_util::CreateCopyResultCallback(&error));
85  content::RunAllBlockingPoolTasksUntilIdle();
86  EXPECT_EQ(FILE_ERROR_OK, error);
87  EXPECT_EQ(FILE_ERROR_OK, FindDirectory(kNewDirectory2));
88
89  // Try to create an existing directory.
90  operation.CreateDirectory(
91      kExistingDirectory,
92      true, // is_exclusive
93      false,  // is_recursive
94      google_apis::test_util::CreateCopyResultCallback(&error));
95  content::RunAllBlockingPoolTasksUntilIdle();
96  EXPECT_EQ(FILE_ERROR_EXISTS, error);
97
98  operation.CreateDirectory(
99      kExistingDirectory,
100      false, // is_exclusive
101      false,  // is_recursive
102      google_apis::test_util::CreateCopyResultCallback(&error));
103  content::RunAllBlockingPoolTasksUntilIdle();
104  EXPECT_EQ(FILE_ERROR_OK, error);
105
106  // Try to create a directory with a path for an existing file.
107  operation.CreateDirectory(
108      kExistingFile,
109      false, // is_exclusive
110      true,  // is_recursive
111      google_apis::test_util::CreateCopyResultCallback(&error));
112  content::RunAllBlockingPoolTasksUntilIdle();
113  EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error);
114
115  // Try to create a directory under a file.
116  operation.CreateDirectory(
117      kExistingFile.AppendASCII("New Directory"),
118      false, // is_exclusive
119      true,  // is_recursive
120      google_apis::test_util::CreateCopyResultCallback(&error));
121  content::RunAllBlockingPoolTasksUntilIdle();
122  EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error);
123}
124
125}  // namespace file_system
126}  // namespace drive
127