Don't peek messages in the MessagePumpForUI class when we receive our kMsgHaveWork...
[chromium-blink-merge.git] / components / filesystem / directory_impl_unittest.cc
blobf7058d45031dacb25712ea13a26a8b4123ac975e
1 // Copyright 2015 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 <map>
6 #include <string>
8 #include "components/filesystem/files_test_base.h"
10 namespace filesystem {
11 namespace {
13 using DirectoryImplTest = FilesTestBase;
15 TEST_F(DirectoryImplTest, Read) {
16 DirectoryPtr directory;
17 GetTemporaryRoot(&directory);
18 FileError error;
20 // Make some files.
21 const struct {
22 const char* name;
23 uint32_t open_flags;
24 } files_to_create[] = {
25 {"my_file1", kFlagRead | kFlagWrite | kFlagCreate},
26 {"my_file2", kFlagWrite | kFlagCreate | kFlagOpenAlways},
27 {"my_file3", kFlagWrite | kFlagCreate | kFlagAppend},
28 {"my_file4", kFlagWrite | kFlagCreate}};
29 for (size_t i = 0; i < arraysize(files_to_create); i++) {
30 error = FILE_ERROR_FAILED;
31 directory->OpenFile(files_to_create[i].name, nullptr,
32 files_to_create[i].open_flags, Capture(&error));
33 ASSERT_TRUE(directory.WaitForIncomingResponse());
34 EXPECT_EQ(FILE_ERROR_OK, error);
36 // Make a directory.
37 error = FILE_ERROR_FAILED;
38 directory->OpenDirectory(
39 "my_dir", nullptr, kFlagRead | kFlagWrite | kFlagCreate, Capture(&error));
40 ASSERT_TRUE(directory.WaitForIncomingResponse());
41 EXPECT_EQ(FILE_ERROR_OK, error);
43 error = FILE_ERROR_FAILED;
44 mojo::Array<DirectoryEntryPtr> directory_contents;
45 directory->Read(Capture(&error, &directory_contents));
46 ASSERT_TRUE(directory.WaitForIncomingResponse());
47 EXPECT_EQ(FILE_ERROR_OK, error);
49 // Expected contents of the directory.
50 std::map<std::string, FsFileType> expected_contents;
51 expected_contents["my_file1"] = FS_FILE_TYPE_REGULAR_FILE;
52 expected_contents["my_file2"] = FS_FILE_TYPE_REGULAR_FILE;
53 expected_contents["my_file3"] = FS_FILE_TYPE_REGULAR_FILE;
54 expected_contents["my_file4"] = FS_FILE_TYPE_REGULAR_FILE;
55 expected_contents["my_dir"] = FS_FILE_TYPE_DIRECTORY;
56 // Note: We don't expose ".." or ".".
58 EXPECT_EQ(expected_contents.size(), directory_contents.size());
59 for (size_t i = 0; i < directory_contents.size(); i++) {
60 ASSERT_TRUE(directory_contents[i]);
61 ASSERT_TRUE(directory_contents[i]->name);
62 auto it = expected_contents.find(directory_contents[i]->name.get());
63 ASSERT_TRUE(it != expected_contents.end());
64 EXPECT_EQ(it->second, directory_contents[i]->type);
65 expected_contents.erase(it);
69 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags).
71 TEST_F(DirectoryImplTest, BasicRenameDelete) {
72 DirectoryPtr directory;
73 GetTemporaryRoot(&directory);
74 FileError error;
76 // Create my_file.
77 error = FILE_ERROR_FAILED;
78 directory->OpenFile("my_file", nullptr, kFlagWrite | kFlagCreate,
79 Capture(&error));
80 ASSERT_TRUE(directory.WaitForIncomingResponse());
81 EXPECT_EQ(FILE_ERROR_OK, error);
83 // Opening my_file should succeed.
84 error = FILE_ERROR_FAILED;
85 directory->OpenFile("my_file", nullptr, kFlagRead | kFlagOpen,
86 Capture(&error));
87 ASSERT_TRUE(directory.WaitForIncomingResponse());
88 EXPECT_EQ(FILE_ERROR_OK, error);
90 // Rename my_file to my_new_file.
91 directory->Rename("my_file", "my_new_file", Capture(&error));
92 ASSERT_TRUE(directory.WaitForIncomingResponse());
93 EXPECT_EQ(FILE_ERROR_OK, error);
95 // Opening my_file should fail.
97 error = FILE_ERROR_FAILED;
98 directory->OpenFile("my_file", nullptr, kFlagRead | kFlagOpen,
99 Capture(&error));
100 ASSERT_TRUE(directory.WaitForIncomingResponse());
101 EXPECT_EQ(FILE_ERROR_FAILED, error);
103 // Opening my_new_file should succeed.
104 error = FILE_ERROR_FAILED;
105 directory->OpenFile("my_new_file", nullptr, kFlagRead | kFlagOpen,
106 Capture(&error));
107 ASSERT_TRUE(directory.WaitForIncomingResponse());
108 EXPECT_EQ(FILE_ERROR_OK, error);
110 // Delete my_new_file (no flags).
111 directory->Delete("my_new_file", 0, Capture(&error));
112 ASSERT_TRUE(directory.WaitForIncomingResponse());
113 EXPECT_EQ(FILE_ERROR_OK, error);
115 // Opening my_new_file should fail.
116 error = FILE_ERROR_FAILED;
117 directory->OpenFile("my_new_file", nullptr, kFlagRead | kFlagOpen,
118 Capture(&error));
119 ASSERT_TRUE(directory.WaitForIncomingResponse());
120 EXPECT_EQ(FILE_ERROR_FAILED, error);
123 TEST_F(DirectoryImplTest, CantOpenDirectoriesAsFiles) {
124 DirectoryPtr directory;
125 GetTemporaryRoot(&directory);
126 FileError error;
129 // Create a directory called 'my_file'
130 DirectoryPtr my_file_directory;
131 error = FILE_ERROR_FAILED;
132 directory->OpenDirectory(
133 "my_file", GetProxy(&my_file_directory),
134 kFlagRead | kFlagWrite | kFlagCreate,
135 Capture(&error));
136 ASSERT_TRUE(directory.WaitForIncomingResponse());
137 EXPECT_EQ(FILE_ERROR_OK, error);
141 // Attempt to open that directory as a file. This must fail!
142 FilePtr file;
143 error = FILE_ERROR_FAILED;
144 directory->OpenFile("my_file", GetProxy(&file), kFlagRead | kFlagOpen,
145 Capture(&error));
146 ASSERT_TRUE(directory.WaitForIncomingResponse());
147 EXPECT_EQ(FILE_ERROR_NOT_A_FILE, error);
152 // TODO(vtl): Test delete flags.
154 } // namespace
155 } // namespace filesystem