| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/tests/test_directory_reader.h" | 5 #include "ppapi/tests/test_directory_reader.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 73 |
| 74 bool TestDirectoryReader::Init() { | 74 bool TestDirectoryReader::Init() { |
| 75 return InitTestingInterface() && EnsureRunningOverHTTP(); | 75 return InitTestingInterface() && EnsureRunningOverHTTP(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void TestDirectoryReader::RunTest() { | 78 void TestDirectoryReader::RunTest() { |
| 79 RUN_TEST(GetNextFile); | 79 RUN_TEST(GetNextFile); |
| 80 } | 80 } |
| 81 | 81 |
| 82 std::string TestDirectoryReader::TestGetNextFile() { | 82 std::string TestDirectoryReader::TestGetNextFile() { |
| 83 TestCompletionCallback callback(instance_->pp_instance()); | 83 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 84 pp::FileSystem_Dev file_system( | 84 pp::FileSystem_Dev file_system( |
| 85 instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | 85 instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); |
| 86 int32_t rv = file_system.Open(1024, callback); | 86 int32_t rv = file_system.Open(1024, callback); |
| 87 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 88 return ReportError("FileSystem::Open force_async", rv); |
| 87 if (rv == PP_OK_COMPLETIONPENDING) | 89 if (rv == PP_OK_COMPLETIONPENDING) |
| 88 rv = callback.WaitForResult(); | 90 rv = callback.WaitForResult(); |
| 89 if (rv != PP_OK) | 91 if (rv != PP_OK) |
| 90 return ReportError("FileSystem::Open", rv); | 92 return ReportError("FileSystem::Open", rv); |
| 91 | 93 |
| 92 // Setup testing directories and files. | 94 // Setup testing directories and files. |
| 93 const char* test_dir_name = "/test_get_next_file"; | 95 const char* test_dir_name = "/test_get_next_file"; |
| 94 const char* file_prefix = "file_"; | 96 const char* file_prefix = "file_"; |
| 95 const char* dir_prefix = "dir_"; | 97 const char* dir_prefix = "dir_"; |
| 96 | 98 |
| 97 pp::FileRef_Dev test_dir(file_system, test_dir_name); | 99 pp::FileRef_Dev test_dir(file_system, test_dir_name); |
| 98 rv = DeleteDirectoryRecursively(&test_dir, &callback); | 100 rv = DeleteDirectoryRecursively(&test_dir, &callback); |
| 99 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) | 101 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) |
| 100 return ReportError("DeleteDirectoryRecursively", rv); | 102 return ReportError("DeleteDirectoryRecursively", rv); |
| 101 | 103 |
| 102 rv = test_dir.MakeDirectory(callback); | 104 rv = test_dir.MakeDirectory(callback); |
| 105 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 106 return ReportError("FileRef::MakeDirectory force_async", rv); |
| 103 if (rv == PP_OK_COMPLETIONPENDING) | 107 if (rv == PP_OK_COMPLETIONPENDING) |
| 104 rv = callback.WaitForResult(); | 108 rv = callback.WaitForResult(); |
| 105 if (rv != PP_OK) | 109 if (rv != PP_OK) |
| 106 return ReportError("FileRef::MakeDirectory", rv); | 110 return ReportError("FileRef::MakeDirectory", rv); |
| 107 | 111 |
| 108 std::set<std::string> expected_file_names; | 112 std::set<std::string> expected_file_names; |
| 109 for (int i = 1; i < 4; ++i) { | 113 for (int i = 1; i < 4; ++i) { |
| 110 char buffer[40]; | 114 char buffer[40]; |
| 111 sprintf(buffer, "%s/%s%d", test_dir_name, file_prefix, i); | 115 sprintf(buffer, "%s/%s%d", test_dir_name, file_prefix, i); |
| 112 pp::FileRef_Dev file_ref(file_system, buffer); | 116 pp::FileRef_Dev file_ref(file_system, buffer); |
| 113 | 117 |
| 114 pp::FileIO_Dev file_io(instance_); | 118 pp::FileIO_Dev file_io(instance_); |
| 115 rv = file_io.Open(file_ref, PP_FILEOPENFLAG_CREATE, callback); | 119 rv = file_io.Open(file_ref, PP_FILEOPENFLAG_CREATE, callback); |
| 120 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 121 return ReportError("FileIO::Open force_async", rv); |
| 116 if (rv == PP_OK_COMPLETIONPENDING) | 122 if (rv == PP_OK_COMPLETIONPENDING) |
| 117 rv = callback.WaitForResult(); | 123 rv = callback.WaitForResult(); |
| 118 if (rv != PP_OK) | 124 if (rv != PP_OK) |
| 119 return ReportError("FileIO::Open", rv); | 125 return ReportError("FileIO::Open", rv); |
| 120 | 126 |
| 121 expected_file_names.insert(buffer); | 127 expected_file_names.insert(buffer); |
| 122 } | 128 } |
| 123 | 129 |
| 124 std::set<std::string> expected_dir_names; | 130 std::set<std::string> expected_dir_names; |
| 125 for (int i = 1; i < 4; ++i) { | 131 for (int i = 1; i < 4; ++i) { |
| 126 char buffer[40]; | 132 char buffer[40]; |
| 127 sprintf(buffer, "%s/%s%d", test_dir_name, dir_prefix, i); | 133 sprintf(buffer, "%s/%s%d", test_dir_name, dir_prefix, i); |
| 128 pp::FileRef_Dev file_ref(file_system, buffer); | 134 pp::FileRef_Dev file_ref(file_system, buffer); |
| 129 | 135 |
| 130 rv = file_ref.MakeDirectory(callback); | 136 rv = file_ref.MakeDirectory(callback); |
| 137 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 138 return ReportError("FileRef::MakeDirectory force_async", rv); |
| 131 if (rv == PP_OK_COMPLETIONPENDING) | 139 if (rv == PP_OK_COMPLETIONPENDING) |
| 132 rv = callback.WaitForResult(); | 140 rv = callback.WaitForResult(); |
| 133 if (rv != PP_OK) | 141 if (rv != PP_OK) |
| 134 return ReportError("FileRef::MakeDirectory", rv); | 142 return ReportError("FileRef::MakeDirectory", rv); |
| 135 | 143 |
| 136 expected_dir_names.insert(buffer); | 144 expected_dir_names.insert(buffer); |
| 137 } | 145 } |
| 138 | 146 |
| 139 // Test that |GetNextEntry()| is able to enumerate all directories and files | 147 // Test that |GetNextEntry()| is able to enumerate all directories and files |
| 140 // that we created. | 148 // that we created. |
| 141 { | 149 { |
| 142 pp::DirectoryReader_Dev directory_reader(test_dir); | 150 pp::DirectoryReader_Dev directory_reader(test_dir); |
| 143 std::vector<pp::DirectoryEntry_Dev> entries; | 151 std::vector<pp::DirectoryEntry_Dev> entries; |
| 144 pp::DirectoryEntry_Dev entry; | 152 pp::DirectoryEntry_Dev entry; |
| 145 do { | 153 do { |
| 146 rv = directory_reader.GetNextEntry(&entry, callback); | 154 rv = directory_reader.GetNextEntry(&entry, callback); |
| 155 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 156 return ReportError("DirectoryReader::GetNextEntry force_async", rv); |
| 147 if (rv == PP_OK_COMPLETIONPENDING) | 157 if (rv == PP_OK_COMPLETIONPENDING) |
| 148 rv = callback.WaitForResult(); | 158 rv = callback.WaitForResult(); |
| 149 if (rv != PP_OK) | 159 if (rv != PP_OK) |
| 150 return ReportError("DirectoryReader::GetNextEntry", rv); | 160 return ReportError("DirectoryReader::GetNextEntry", rv); |
| 151 if (!entry.is_null()) | 161 if (!entry.is_null()) |
| 152 entries.push_back(entry); | 162 entries.push_back(entry); |
| 153 } while (!entry.is_null()); | 163 } while (!entry.is_null()); |
| 154 | 164 |
| 155 size_t sum = expected_file_names.size() + expected_dir_names.size(); | 165 size_t sum = expected_file_names.size() + expected_dir_names.size(); |
| 156 if (entries.size() != sum) | 166 if (entries.size() != sum) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 181 } | 191 } |
| 182 | 192 |
| 183 // Test cancellation of asynchronous |GetNextEntry()|. | 193 // Test cancellation of asynchronous |GetNextEntry()|. |
| 184 { | 194 { |
| 185 // Declaring |entry| here prevents memory corruption for some failure modes | 195 // Declaring |entry| here prevents memory corruption for some failure modes |
| 186 // and lets us to detect some other failures. | 196 // and lets us to detect some other failures. |
| 187 pp::DirectoryEntry_Dev entry; | 197 pp::DirectoryEntry_Dev entry; |
| 188 callback.reset_run_count(); | 198 callback.reset_run_count(); |
| 189 // Note that the directory reader will be deleted immediately. | 199 // Note that the directory reader will be deleted immediately. |
| 190 rv = pp::DirectoryReader_Dev(test_dir).GetNextEntry(&entry, callback); | 200 rv = pp::DirectoryReader_Dev(test_dir).GetNextEntry(&entry, callback); |
| 201 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 202 return ReportError("DirectoryReader::GetNextEntry force_async", rv); |
| 191 if (callback.run_count() > 0) | 203 if (callback.run_count() > 0) |
| 192 return "DirectoryReader::GetNextEntry ran callback synchronously."; | 204 return "DirectoryReader::GetNextEntry ran callback synchronously."; |
| 193 | 205 |
| 194 // If |GetNextEntry()| is completing asynchronously, the callback should be | 206 // If |GetNextEntry()| is completing asynchronously, the callback should be |
| 195 // aborted (i.e., called with |PP_ERROR_ABORTED| from the message loop) | 207 // aborted (i.e., called with |PP_ERROR_ABORTED| from the message loop) |
| 196 // since the resource was destroyed. | 208 // since the resource was destroyed. |
| 197 if (rv == PP_OK_COMPLETIONPENDING) { | 209 if (rv == PP_OK_COMPLETIONPENDING) { |
| 198 // |GetNextEntry()| *may* have written to |entry| (e.g., synchronously, | 210 // |GetNextEntry()| *may* have written to |entry| (e.g., synchronously, |
| 199 // before the resource was destroyed), but it must not write to it after | 211 // before the resource was destroyed), but it must not write to it after |
| 200 // destruction. | 212 // destruction. |
| 201 bool entry_is_null = entry.is_null(); | 213 bool entry_is_null = entry.is_null(); |
| 202 rv = callback.WaitForResult(); | 214 rv = callback.WaitForResult(); |
| 203 if (rv != PP_ERROR_ABORTED) | 215 if (rv != PP_ERROR_ABORTED) |
| 204 return "DirectoryReader::GetNextEntry not aborted."; | 216 return "DirectoryReader::GetNextEntry not aborted."; |
| 205 if (entry.is_null() != entry_is_null) | 217 if (entry.is_null() != entry_is_null) |
| 206 return "DirectoryReader::GetNextEntry wrote result after destruction."; | 218 return "DirectoryReader::GetNextEntry wrote result after destruction."; |
| 207 } else if (rv != PP_OK) { | 219 } else if (rv != PP_OK) { |
| 208 return ReportError("DirectoryReader::GetNextEntry", rv); | 220 return ReportError("DirectoryReader::GetNextEntry", rv); |
| 209 } | 221 } |
| 210 } | 222 } |
| 211 | 223 |
| 212 PASS(); | 224 PASS(); |
| 213 } | 225 } |
| OLD | NEW |