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.
5 #include "components/drive/file_system/create_directory_operation.h"
7 #include "components/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"
13 namespace file_system
{
15 class CreateDirectoryOperationTest
: public OperationTestBase
{
17 // Returns FILE_ERROR_OK if a directory is found at |path|.
18 FileError
FindDirectory(const base::FilePath
& path
) {
20 FileError error
= GetLocalResourceEntry(path
, &entry
);
21 if (error
== FILE_ERROR_OK
&& !entry
.file_info().is_directory())
22 error
= FILE_ERROR_NOT_A_DIRECTORY
;
27 TEST_F(CreateDirectoryOperationTest
, CreateDirectory
) {
28 CreateDirectoryOperation
operation(blocking_task_runner(),
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");
41 // Create a new directory, not recursively.
42 EXPECT_EQ(FILE_ERROR_NOT_FOUND
, FindDirectory(kNewDirectory1
));
44 FileError error
= FILE_ERROR_FAILED
;
45 operation
.CreateDirectory(
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());
56 delegate()->get_changed_files().CountDirectory(kNewDirectory1
.DirName()));
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()));
69 // Create a new directory recursively.
70 EXPECT_EQ(FILE_ERROR_NOT_FOUND
, FindDirectory(kNewDirectory2
));
71 operation
.CreateDirectory(
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
));
80 operation
.CreateDirectory(
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
));
89 // Try to create an existing directory.
90 operation
.CreateDirectory(
93 false, // is_recursive
94 google_apis::test_util::CreateCopyResultCallback(&error
));
95 content::RunAllBlockingPoolTasksUntilIdle();
96 EXPECT_EQ(FILE_ERROR_EXISTS
, error
);
98 operation
.CreateDirectory(
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
);
106 // Try to create a directory with a path for an existing file.
107 operation
.CreateDirectory(
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
);
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
);
125 } // namespace file_system