| 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 |
| 11 #include "ppapi/c/pp_errors.h" | 11 #include "ppapi/c/pp_errors.h" |
| 12 #include "ppapi/c/dev/ppb_file_io_dev.h" | 12 #include "ppapi/c/dev/ppb_file_io_dev.h" |
| 13 #include "ppapi/cpp/dev/directory_entry_dev.h" | 13 #include "ppapi/cpp/dev/directory_entry_dev.h" |
| 14 #include "ppapi/cpp/dev/directory_reader_dev.h" | 14 #include "ppapi/cpp/dev/directory_reader_dev.h" |
| 15 #include "ppapi/cpp/dev/file_io_dev.h" | 15 #include "ppapi/cpp/file_io.h" |
| 16 #include "ppapi/cpp/dev/file_ref_dev.h" | 16 #include "ppapi/cpp/file_ref.h" |
| 17 #include "ppapi/cpp/dev/file_system_dev.h" | 17 #include "ppapi/cpp/file_system.h" |
| 18 #include "ppapi/cpp/instance.h" | 18 #include "ppapi/cpp/instance.h" |
| 19 #include "ppapi/tests/test_utils.h" | 19 #include "ppapi/tests/test_utils.h" |
| 20 #include "ppapi/tests/testing_instance.h" | 20 #include "ppapi/tests/testing_instance.h" |
| 21 | 21 |
| 22 REGISTER_TEST_CASE(DirectoryReader); | 22 REGISTER_TEST_CASE(DirectoryReader); |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 std::string IntegerToString(int value) { | 26 std::string IntegerToString(int value) { |
| 27 char result[12]; | 27 char result[12]; |
| 28 sprintf(result, "%d", value); | 28 sprintf(result, "%d", value); |
| 29 return result; | 29 return result; |
| 30 } | 30 } |
| 31 | 31 |
| 32 int32_t DeleteDirectoryRecursively(pp::FileRef_Dev* dir, | 32 int32_t DeleteDirectoryRecursively(pp::FileRef* dir, |
| 33 TestCompletionCallback* callback) { | 33 TestCompletionCallback* callback) { |
| 34 if (!dir || !callback) | 34 if (!dir || !callback) |
| 35 return PP_ERROR_BADARGUMENT; | 35 return PP_ERROR_BADARGUMENT; |
| 36 | 36 |
| 37 int32_t rv = PP_OK; | 37 int32_t rv = PP_OK; |
| 38 pp::DirectoryReader_Dev directory_reader(*dir); | 38 pp::DirectoryReader_Dev directory_reader(*dir); |
| 39 std::vector<pp::DirectoryEntry_Dev> entries; | 39 std::vector<pp::DirectoryEntry_Dev> entries; |
| 40 pp::DirectoryEntry_Dev entry; | 40 pp::DirectoryEntry_Dev entry; |
| 41 do { | 41 do { |
| 42 rv = directory_reader.GetNextEntry(&entry, *callback); | 42 rv = directory_reader.GetNextEntry(&entry, *callback); |
| 43 if (rv == PP_OK_COMPLETIONPENDING) | 43 if (rv == PP_OK_COMPLETIONPENDING) |
| 44 rv = callback->WaitForResult(); | 44 rv = callback->WaitForResult(); |
| 45 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) | 45 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) |
| 46 return rv; | 46 return rv; |
| 47 if (!entry.is_null()) | 47 if (!entry.is_null()) |
| 48 entries.push_back(entry); | 48 entries.push_back(entry); |
| 49 } while (!entry.is_null()); | 49 } while (!entry.is_null()); |
| 50 | 50 |
| 51 for (std::vector<pp::DirectoryEntry_Dev>::const_iterator it = entries.begin(); | 51 for (std::vector<pp::DirectoryEntry_Dev>::const_iterator it = entries.begin(); |
| 52 it != entries.end(); ++it) { | 52 it != entries.end(); ++it) { |
| 53 pp::FileRef_Dev file_ref = it->file_ref(); | 53 pp::FileRef file_ref = it->file_ref(); |
| 54 if (it->file_type() == PP_FILETYPE_DIRECTORY) { | 54 if (it->file_type() == PP_FILETYPE_DIRECTORY) { |
| 55 rv = DeleteDirectoryRecursively(&file_ref, callback); | 55 rv = DeleteDirectoryRecursively(&file_ref, callback); |
| 56 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) | 56 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) |
| 57 return rv; | 57 return rv; |
| 58 } else { | 58 } else { |
| 59 rv = file_ref.Delete(*callback); | 59 rv = file_ref.Delete(*callback); |
| 60 if (rv == PP_OK_COMPLETIONPENDING) | 60 if (rv == PP_OK_COMPLETIONPENDING) |
| 61 rv = callback->WaitForResult(); | 61 rv = callback->WaitForResult(); |
| 62 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) | 62 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) |
| 63 return rv; | 63 return rv; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 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(), force_async_); | 83 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 84 pp::FileSystem_Dev file_system( | 84 pp::FileSystem 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) | 87 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 88 return ReportError("FileSystem::Open force_async", rv); | 88 return ReportError("FileSystem::Open force_async", rv); |
| 89 if (rv == PP_OK_COMPLETIONPENDING) | 89 if (rv == PP_OK_COMPLETIONPENDING) |
| 90 rv = callback.WaitForResult(); | 90 rv = callback.WaitForResult(); |
| 91 if (rv != PP_OK) | 91 if (rv != PP_OK) |
| 92 return ReportError("FileSystem::Open", rv); | 92 return ReportError("FileSystem::Open", rv); |
| 93 | 93 |
| 94 // Setup testing directories and files. | 94 // Setup testing directories and files. |
| 95 const char* test_dir_name = "/test_get_next_file"; | 95 const char* test_dir_name = "/test_get_next_file"; |
| 96 const char* file_prefix = "file_"; | 96 const char* file_prefix = "file_"; |
| 97 const char* dir_prefix = "dir_"; | 97 const char* dir_prefix = "dir_"; |
| 98 | 98 |
| 99 pp::FileRef_Dev test_dir(file_system, test_dir_name); | 99 pp::FileRef test_dir(file_system, test_dir_name); |
| 100 rv = DeleteDirectoryRecursively(&test_dir, &callback); | 100 rv = DeleteDirectoryRecursively(&test_dir, &callback); |
| 101 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) | 101 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) |
| 102 return ReportError("DeleteDirectoryRecursively", rv); | 102 return ReportError("DeleteDirectoryRecursively", rv); |
| 103 | 103 |
| 104 rv = test_dir.MakeDirectory(callback); | 104 rv = test_dir.MakeDirectory(callback); |
| 105 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | 105 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 106 return ReportError("FileRef::MakeDirectory force_async", rv); | 106 return ReportError("FileRef::MakeDirectory force_async", rv); |
| 107 if (rv == PP_OK_COMPLETIONPENDING) | 107 if (rv == PP_OK_COMPLETIONPENDING) |
| 108 rv = callback.WaitForResult(); | 108 rv = callback.WaitForResult(); |
| 109 if (rv != PP_OK) | 109 if (rv != PP_OK) |
| 110 return ReportError("FileRef::MakeDirectory", rv); | 110 return ReportError("FileRef::MakeDirectory", rv); |
| 111 | 111 |
| 112 std::set<std::string> expected_file_names; | 112 std::set<std::string> expected_file_names; |
| 113 for (int i = 1; i < 4; ++i) { | 113 for (int i = 1; i < 4; ++i) { |
| 114 char buffer[40]; | 114 char buffer[40]; |
| 115 sprintf(buffer, "%s/%s%d", test_dir_name, file_prefix, i); | 115 sprintf(buffer, "%s/%s%d", test_dir_name, file_prefix, i); |
| 116 pp::FileRef_Dev file_ref(file_system, buffer); | 116 pp::FileRef file_ref(file_system, buffer); |
| 117 | 117 |
| 118 pp::FileIO_Dev file_io(instance_); | 118 pp::FileIO file_io(instance_); |
| 119 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) | 120 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 121 return ReportError("FileIO::Open force_async", rv); | 121 return ReportError("FileIO::Open force_async", rv); |
| 122 if (rv == PP_OK_COMPLETIONPENDING) | 122 if (rv == PP_OK_COMPLETIONPENDING) |
| 123 rv = callback.WaitForResult(); | 123 rv = callback.WaitForResult(); |
| 124 if (rv != PP_OK) | 124 if (rv != PP_OK) |
| 125 return ReportError("FileIO::Open", rv); | 125 return ReportError("FileIO::Open", rv); |
| 126 | 126 |
| 127 expected_file_names.insert(buffer); | 127 expected_file_names.insert(buffer); |
| 128 } | 128 } |
| 129 | 129 |
| 130 std::set<std::string> expected_dir_names; | 130 std::set<std::string> expected_dir_names; |
| 131 for (int i = 1; i < 4; ++i) { | 131 for (int i = 1; i < 4; ++i) { |
| 132 char buffer[40]; | 132 char buffer[40]; |
| 133 sprintf(buffer, "%s/%s%d", test_dir_name, dir_prefix, i); | 133 sprintf(buffer, "%s/%s%d", test_dir_name, dir_prefix, i); |
| 134 pp::FileRef_Dev file_ref(file_system, buffer); | 134 pp::FileRef file_ref(file_system, buffer); |
| 135 | 135 |
| 136 rv = file_ref.MakeDirectory(callback); | 136 rv = file_ref.MakeDirectory(callback); |
| 137 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | 137 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) |
| 138 return ReportError("FileRef::MakeDirectory force_async", rv); | 138 return ReportError("FileRef::MakeDirectory force_async", rv); |
| 139 if (rv == PP_OK_COMPLETIONPENDING) | 139 if (rv == PP_OK_COMPLETIONPENDING) |
| 140 rv = callback.WaitForResult(); | 140 rv = callback.WaitForResult(); |
| 141 if (rv != PP_OK) | 141 if (rv != PP_OK) |
| 142 return ReportError("FileRef::MakeDirectory", rv); | 142 return ReportError("FileRef::MakeDirectory", rv); |
| 143 | 143 |
| 144 expected_dir_names.insert(buffer); | 144 expected_dir_names.insert(buffer); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 162 entries.push_back(entry); | 162 entries.push_back(entry); |
| 163 } while (!entry.is_null()); | 163 } while (!entry.is_null()); |
| 164 | 164 |
| 165 size_t sum = expected_file_names.size() + expected_dir_names.size(); | 165 size_t sum = expected_file_names.size() + expected_dir_names.size(); |
| 166 if (entries.size() != sum) | 166 if (entries.size() != sum) |
| 167 return "Expected " + IntegerToString(sum) + " entries, got " + | 167 return "Expected " + IntegerToString(sum) + " entries, got " + |
| 168 IntegerToString(entries.size()); | 168 IntegerToString(entries.size()); |
| 169 | 169 |
| 170 for (std::vector<pp::DirectoryEntry_Dev>::const_iterator it = | 170 for (std::vector<pp::DirectoryEntry_Dev>::const_iterator it = |
| 171 entries.begin(); it != entries.end(); ++it) { | 171 entries.begin(); it != entries.end(); ++it) { |
| 172 pp::FileRef_Dev file_ref = it->file_ref(); | 172 pp::FileRef file_ref = it->file_ref(); |
| 173 std::string file_path = file_ref.GetPath().AsString(); | 173 std::string file_path = file_ref.GetPath().AsString(); |
| 174 std::set<std::string>::iterator found = | 174 std::set<std::string>::iterator found = |
| 175 expected_file_names.find(file_path); | 175 expected_file_names.find(file_path); |
| 176 if (found != expected_file_names.end()) { | 176 if (found != expected_file_names.end()) { |
| 177 if (it->file_type() != PP_FILETYPE_REGULAR) | 177 if (it->file_type() != PP_FILETYPE_REGULAR) |
| 178 return file_path + " should have been a regular file."; | 178 return file_path + " should have been a regular file."; |
| 179 expected_file_names.erase(found); | 179 expected_file_names.erase(found); |
| 180 } else { | 180 } else { |
| 181 found = expected_dir_names.find(file_path); | 181 found = expected_dir_names.find(file_path); |
| 182 if (found == expected_dir_names.end()) | 182 if (found == expected_dir_names.end()) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 return "DirectoryReader::GetNextEntry not aborted."; | 216 return "DirectoryReader::GetNextEntry not aborted."; |
| 217 if (entry.is_null() != entry_is_null) | 217 if (entry.is_null() != entry_is_null) |
| 218 return "DirectoryReader::GetNextEntry wrote result after destruction."; | 218 return "DirectoryReader::GetNextEntry wrote result after destruction."; |
| 219 } else if (rv != PP_OK) { | 219 } else if (rv != PP_OK) { |
| 220 return ReportError("DirectoryReader::GetNextEntry", rv); | 220 return ReportError("DirectoryReader::GetNextEntry", rv); |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 | 223 |
| 224 PASS(); | 224 PASS(); |
| 225 } | 225 } |
| OLD | NEW |