Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: ppapi/tests/test_directory_reader.cc

Issue 6899055: PPAPI: Force async callback invocation option. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 return rv; 69 return rv;
70 } 70 }
71 71
72 } // namespace 72 } // namespace
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_ASYNC_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 failed", 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 failed", 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 failed", 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 failed", 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)
brettw 2011/06/21 22:56:41 Nit: needs {} according to Chrome style since it's
polina 2011/06/21 23:40:01 Done.
156 return ReportError("DirectoryReader::GetNextEntry force_async failed",
157 rv);
147 if (rv == PP_OK_COMPLETIONPENDING) 158 if (rv == PP_OK_COMPLETIONPENDING)
148 rv = callback.WaitForResult(); 159 rv = callback.WaitForResult();
149 if (rv != PP_OK) 160 if (rv != PP_OK)
150 return ReportError("DirectoryReader::GetNextEntry", rv); 161 return ReportError("DirectoryReader::GetNextEntry", rv);
151 if (!entry.is_null()) 162 if (!entry.is_null())
152 entries.push_back(entry); 163 entries.push_back(entry);
153 } while (!entry.is_null()); 164 } while (!entry.is_null());
154 165
155 size_t sum = expected_file_names.size() + expected_dir_names.size(); 166 size_t sum = expected_file_names.size() + expected_dir_names.size();
156 if (entries.size() != sum) 167 if (entries.size() != sum)
(...skipping 24 matching lines...) Expand all
181 } 192 }
182 193
183 // Test cancellation of asynchronous |GetNextEntry()|. 194 // Test cancellation of asynchronous |GetNextEntry()|.
184 { 195 {
185 // Declaring |entry| here prevents memory corruption for some failure modes 196 // Declaring |entry| here prevents memory corruption for some failure modes
186 // and lets us to detect some other failures. 197 // and lets us to detect some other failures.
187 pp::DirectoryEntry_Dev entry; 198 pp::DirectoryEntry_Dev entry;
188 callback.reset_run_count(); 199 callback.reset_run_count();
189 // Note that the directory reader will be deleted immediately. 200 // Note that the directory reader will be deleted immediately.
190 rv = pp::DirectoryReader_Dev(test_dir).GetNextEntry(&entry, callback); 201 rv = pp::DirectoryReader_Dev(test_dir).GetNextEntry(&entry, callback);
202 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
brettw 2011/06/21 22:56:41 Ditto
polina 2011/06/21 23:40:01 Done.
203 return ReportError("DirectoryReader::GetNextEntry force_async failed",
204 rv);
191 if (callback.run_count() > 0) 205 if (callback.run_count() > 0)
192 return "DirectoryReader::GetNextEntry ran callback synchronously."; 206 return "DirectoryReader::GetNextEntry ran callback synchronously.";
193 207
194 // If |GetNextEntry()| is completing asynchronously, the callback should be 208 // If |GetNextEntry()| is completing asynchronously, the callback should be
195 // aborted (i.e., called with |PP_ERROR_ABORTED| from the message loop) 209 // aborted (i.e., called with |PP_ERROR_ABORTED| from the message loop)
196 // since the resource was destroyed. 210 // since the resource was destroyed.
197 if (rv == PP_OK_COMPLETIONPENDING) { 211 if (rv == PP_OK_COMPLETIONPENDING) {
198 // |GetNextEntry()| *may* have written to |entry| (e.g., synchronously, 212 // |GetNextEntry()| *may* have written to |entry| (e.g., synchronously,
199 // before the resource was destroyed), but it must not write to it after 213 // before the resource was destroyed), but it must not write to it after
200 // destruction. 214 // destruction.
201 bool entry_is_null = entry.is_null(); 215 bool entry_is_null = entry.is_null();
202 rv = callback.WaitForResult(); 216 rv = callback.WaitForResult();
203 if (rv != PP_ERROR_ABORTED) 217 if (rv != PP_ERROR_ABORTED)
204 return "DirectoryReader::GetNextEntry not aborted."; 218 return "DirectoryReader::GetNextEntry not aborted.";
205 if (entry.is_null() != entry_is_null) 219 if (entry.is_null() != entry_is_null)
206 return "DirectoryReader::GetNextEntry wrote result after destruction."; 220 return "DirectoryReader::GetNextEntry wrote result after destruction.";
207 } else if (rv != PP_OK) { 221 } else if (rv != PP_OK) {
208 return ReportError("DirectoryReader::GetNextEntry", rv); 222 return ReportError("DirectoryReader::GetNextEntry", rv);
209 } 223 }
210 } 224 }
211 225
212 PASS(); 226 PASS();
213 } 227 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698