OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "ppapi/tests/test_directory_reader.h" | |
6 | |
7 #include <stdio.h> | |
8 #include <set> | |
9 #include <vector> | |
10 | |
11 #include "ppapi/c/pp_errors.h" | |
12 #include "ppapi/c/ppb_file_io.h" | |
13 #include "ppapi/cpp/dev/directory_entry_dev.h" | |
14 #include "ppapi/cpp/dev/directory_reader_dev.h" | |
15 #include "ppapi/cpp/file_io.h" | |
16 #include "ppapi/cpp/file_ref.h" | |
17 #include "ppapi/cpp/file_system.h" | |
18 #include "ppapi/cpp/instance.h" | |
19 #include "ppapi/tests/test_utils.h" | |
20 #include "ppapi/tests/testing_instance.h" | |
21 | |
22 REGISTER_TEST_CASE(DirectoryReader); | |
23 | |
24 namespace { | |
25 | |
26 typedef std::vector<pp::DirectoryEntry_Dev> Entries; | |
27 | |
28 std::string IntegerToString(int value) { | |
29 char result[12]; | |
30 sprintf(result, "%d", value); | |
31 return result; | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 bool TestDirectoryReader::Init() { | |
37 return CheckTestingInterface() && EnsureRunningOverHTTP(); | |
38 } | |
39 | |
40 void TestDirectoryReader::RunTests(const std::string& filter) { | |
41 RUN_CALLBACK_TEST(TestDirectoryReader, ReadEntries, filter); | |
42 } | |
43 | |
44 int32_t TestDirectoryReader::DeleteDirectoryRecursively(pp::FileRef* dir) { | |
45 if (!dir) | |
46 return PP_ERROR_BADARGUMENT; | |
47 | |
48 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); | |
49 TestCompletionCallbackWithOutput<Entries> output_callback( | |
50 instance_->pp_instance(), callback_type()); | |
51 | |
52 pp::DirectoryReader_Dev directory_reader(*dir); | |
53 output_callback.WaitForResult( | |
54 directory_reader.ReadEntries(output_callback.GetCallback())); | |
55 int32_t rv = output_callback.result(); | |
56 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) | |
57 return rv; | |
58 | |
59 Entries entries = output_callback.output(); | |
60 for (Entries::const_iterator it = entries.begin(); | |
61 it != entries.end(); ++it) { | |
62 pp::FileRef file_ref = it->file_ref(); | |
63 if (it->file_type() == PP_FILETYPE_DIRECTORY) { | |
64 rv = DeleteDirectoryRecursively(&file_ref); | |
65 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) | |
66 return rv; | |
67 } else { | |
68 callback.WaitForResult(file_ref.Delete(callback.GetCallback())); | |
69 rv = callback.result(); | |
70 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) | |
71 return rv; | |
72 } | |
73 } | |
74 callback.WaitForResult(dir->Delete(callback.GetCallback())); | |
75 return callback.result(); | |
76 } | |
77 | |
78 std::string TestDirectoryReader::TestReadEntries() { | |
79 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); | |
80 pp::FileSystem file_system( | |
81 instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | |
82 callback.WaitForResult(file_system.Open(1024, callback.GetCallback())); | |
83 CHECK_CALLBACK_BEHAVIOR(callback); | |
84 ASSERT_EQ(PP_OK, callback.result()); | |
85 | |
86 // Setup testing directories and files. | |
87 const char* test_dir_name = "/test_get_next_file"; | |
88 const char* file_prefix = "file_"; | |
89 const char* dir_prefix = "dir_"; | |
90 | |
91 pp::FileRef test_dir(file_system, test_dir_name); | |
92 int32_t rv = DeleteDirectoryRecursively(&test_dir); | |
93 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) | |
94 return ReportError("DeleteDirectoryRecursively", rv); | |
95 | |
96 callback.WaitForResult(test_dir.MakeDirectory(callback.GetCallback())); | |
97 CHECK_CALLBACK_BEHAVIOR(callback); | |
98 ASSERT_EQ(PP_OK, callback.result()); | |
99 | |
100 std::set<std::string> expected_file_names; | |
101 for (int i = 1; i < 4; ++i) { | |
102 char buffer[40]; | |
103 sprintf(buffer, "%s/%s%d", test_dir_name, file_prefix, i); | |
104 pp::FileRef file_ref(file_system, buffer); | |
105 | |
106 pp::FileIO file_io(instance_); | |
107 callback.WaitForResult( | |
108 file_io.Open(file_ref, PP_FILEOPENFLAG_CREATE, callback.GetCallback())); | |
109 CHECK_CALLBACK_BEHAVIOR(callback); | |
110 ASSERT_EQ(PP_OK, callback.result()); | |
111 | |
112 expected_file_names.insert(buffer); | |
113 } | |
114 | |
115 std::set<std::string> expected_dir_names; | |
116 for (int i = 1; i < 4; ++i) { | |
117 char buffer[40]; | |
118 sprintf(buffer, "%s/%s%d", test_dir_name, dir_prefix, i); | |
119 pp::FileRef file_ref(file_system, buffer); | |
120 | |
121 callback.WaitForResult(file_ref.MakeDirectory(callback.GetCallback())); | |
122 CHECK_CALLBACK_BEHAVIOR(callback); | |
123 ASSERT_EQ(PP_OK, callback.result()); | |
124 | |
125 expected_dir_names.insert(buffer); | |
126 } | |
127 | |
128 // Test that |ReadEntries()| is able to fetch all directories and files that | |
129 // we created. | |
130 { | |
131 TestCompletionCallbackWithOutput<Entries> output_callback( | |
132 instance_->pp_instance(), callback_type()); | |
133 | |
134 pp::DirectoryReader_Dev directory_reader(test_dir); | |
135 output_callback.WaitForResult( | |
136 directory_reader.ReadEntries(output_callback.GetCallback())); | |
137 CHECK_CALLBACK_BEHAVIOR(output_callback); | |
138 ASSERT_EQ(PP_OK, output_callback.result()); | |
139 | |
140 Entries entries = output_callback.output(); | |
141 size_t sum = expected_file_names.size() + expected_dir_names.size(); | |
142 if (entries.size() != sum) | |
143 return "Expected " + IntegerToString(sum) + " entries, got " + | |
144 IntegerToString(entries.size()); | |
145 | |
146 for (Entries::const_iterator it = entries.begin(); | |
147 it != entries.end(); ++it) { | |
148 pp::FileRef file_ref = it->file_ref(); | |
149 std::string file_path = file_ref.GetPath().AsString(); | |
150 std::set<std::string>::iterator found = | |
151 expected_file_names.find(file_path); | |
152 if (found != expected_file_names.end()) { | |
153 if (it->file_type() != PP_FILETYPE_REGULAR) | |
154 return file_path + " should have been a regular file."; | |
155 expected_file_names.erase(found); | |
156 } else { | |
157 found = expected_dir_names.find(file_path); | |
158 if (found == expected_dir_names.end()) | |
159 return "Unexpected file path: " + file_path; | |
160 if (it->file_type() != PP_FILETYPE_DIRECTORY) | |
161 return file_path + " should have been a directory."; | |
162 expected_dir_names.erase(found); | |
163 } | |
164 } | |
165 if (!expected_file_names.empty() || !expected_dir_names.empty()) | |
166 return "Expected more file paths."; | |
167 } | |
168 | |
169 // Test cancellation of asynchronous |ReadEntries()|. | |
170 TestCompletionCallbackWithOutput<Entries> output_callback( | |
171 instance_->pp_instance(), callback_type()); | |
172 { | |
173 | |
174 // Note that the directory reader will be deleted immediately. | |
175 rv = pp::DirectoryReader_Dev(test_dir).ReadEntries( | |
176 output_callback.GetCallback()); | |
177 } | |
178 output_callback.WaitForAbortResult(rv); | |
179 CHECK_CALLBACK_BEHAVIOR(output_callback); | |
180 | |
181 PASS(); | |
182 } | |
OLD | NEW |