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

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

Issue 9937001: PPAPI: Refactor ppapi test callbacks to ease testing blocking callbacks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync Created 8 years, 8 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
« no previous file with comments | « ppapi/tests/test_network_monitor_private.cc ('k') | ppapi/tests/test_url_request.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_url_loader.h" 5 #include "ppapi/tests/test_url_loader.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <string.h> 8 #include <string.h>
9 #include <string> 9 #include <string>
10 10
(...skipping 22 matching lines...) Expand all
33 int32_t WriteEntireBuffer(PP_Instance instance, 33 int32_t WriteEntireBuffer(PP_Instance instance,
34 pp::FileIO* file_io, 34 pp::FileIO* file_io,
35 int32_t offset, 35 int32_t offset,
36 const std::string& data) { 36 const std::string& data) {
37 TestCompletionCallback callback(instance); 37 TestCompletionCallback callback(instance);
38 int32_t write_offset = offset; 38 int32_t write_offset = offset;
39 const char* buf = data.c_str(); 39 const char* buf = data.c_str();
40 int32_t size = data.size(); 40 int32_t size = data.size();
41 41
42 while (write_offset < offset + size) { 42 while (write_offset < offset + size) {
43 int32_t rv = file_io->Write(write_offset, &buf[write_offset - offset], 43 callback.WaitForResult(file_io->Write(write_offset,
44 size - write_offset + offset, callback); 44 &buf[write_offset - offset],
45 if (rv == PP_OK_COMPLETIONPENDING) 45 size - write_offset + offset,
46 rv = callback.WaitForResult(); 46 callback));
47 if (rv < 0) 47 if (callback.result() < 0)
48 return rv; 48 return callback.result();
49 if (rv == 0) 49 if (callback.result() == 0)
50 return PP_ERROR_FAILED; 50 return PP_ERROR_FAILED;
51 write_offset += rv; 51 write_offset += callback.result();
52 } 52 }
53 53
54 return PP_OK; 54 return PP_OK;
55 } 55 }
56 56
57 } // namespace 57 } // namespace
58 58
59 TestURLLoader::TestURLLoader(TestingInstance* instance) 59 TestURLLoader::TestURLLoader(TestingInstance* instance)
60 : TestCase(instance), 60 : TestCase(instance),
61 file_io_trusted_interface_(NULL), 61 file_io_trusted_interface_(NULL),
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 RUN_TEST_FORCEASYNC_AND_NOT(TrustedHttpRequests, filter); 115 RUN_TEST_FORCEASYNC_AND_NOT(TrustedHttpRequests, filter);
116 RUN_TEST_FORCEASYNC_AND_NOT(FollowURLRedirect, filter); 116 RUN_TEST_FORCEASYNC_AND_NOT(FollowURLRedirect, filter);
117 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect, filter); 117 RUN_TEST_FORCEASYNC_AND_NOT(AuditURLRedirect, filter);
118 RUN_TEST_FORCEASYNC_AND_NOT(AbortCalls, filter); 118 RUN_TEST_FORCEASYNC_AND_NOT(AbortCalls, filter);
119 RUN_TEST_FORCEASYNC_AND_NOT(UntendedLoad, filter); 119 RUN_TEST_FORCEASYNC_AND_NOT(UntendedLoad, filter);
120 RUN_TEST_FORCEASYNC_AND_NOT(PrefetchBufferThreshold, filter); 120 RUN_TEST_FORCEASYNC_AND_NOT(PrefetchBufferThreshold, filter);
121 } 121 }
122 122
123 std::string TestURLLoader::ReadEntireFile(pp::FileIO* file_io, 123 std::string TestURLLoader::ReadEntireFile(pp::FileIO* file_io,
124 std::string* data) { 124 std::string* data) {
125 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 125 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
126 char buf[256]; 126 char buf[256];
127 int64_t offset = 0; 127 int64_t offset = 0;
128 128
129 for (;;) { 129 for (;;) {
130 int32_t rv = file_io->Read(offset, buf, sizeof(buf), callback); 130 callback.WaitForResult(file_io->Read(offset, buf, sizeof(buf), callback));
131 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 131 if (callback.result() < 0)
132 return ReportError("FileIO::Read force_async", rv); 132 return ReportError("FileIO::Read", callback.result());
133 if (rv == PP_OK_COMPLETIONPENDING) 133 if (callback.result() == 0)
134 rv = callback.WaitForResult();
135 if (rv < 0)
136 return ReportError("FileIO::Read", rv);
137 if (rv == 0)
138 break; 134 break;
139 offset += rv; 135 offset += callback.result();
140 data->append(buf, rv); 136 data->append(buf, callback.result());
141 } 137 }
142 138
143 PASS(); 139 PASS();
144 } 140 }
145 141
146 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader, 142 std::string TestURLLoader::ReadEntireResponseBody(pp::URLLoader* loader,
147 std::string* body) { 143 std::string* body) {
148 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 144 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
149 char buf[2]; // Small so that multiple reads are needed. 145 char buf[2]; // Small so that multiple reads are needed.
150 146
151 for (;;) { 147 for (;;) {
152 int32_t rv = loader->ReadResponseBody(buf, sizeof(buf), callback); 148 callback.WaitForResult(
153 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 149 loader->ReadResponseBody(buf, sizeof(buf), callback));
154 return ReportError("URLLoader::ReadResponseBody force_async", rv); 150 if (callback.result() < 0)
155 if (rv == PP_OK_COMPLETIONPENDING) 151 return ReportError("URLLoader::ReadResponseBody", callback.result());
156 rv = callback.WaitForResult(); 152 if (callback.result() == 0)
157 if (rv < 0)
158 return ReportError("URLLoader::ReadResponseBody", rv);
159 if (rv == 0)
160 break; 153 break;
161 body->append(buf, rv); 154 body->append(buf, callback.result());
162 } 155 }
163 156
164 PASS(); 157 PASS();
165 } 158 }
166 159
167 std::string TestURLLoader::LoadAndCompareBody( 160 std::string TestURLLoader::LoadAndCompareBody(
168 const pp::URLRequestInfo& request, 161 const pp::URLRequestInfo& request,
169 const std::string& expected_body) { 162 const std::string& expected_body) {
170 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 163 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
171 164
172 pp::URLLoader loader(instance_); 165 pp::URLLoader loader(instance_);
173 int32_t rv = loader.Open(request, callback); 166 callback.WaitForResult(loader.Open(request, callback));
174 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 167 CHECK_CALLBACK_BEHAVIOR(callback);
175 return ReportError("URLLoader::Open force_async", rv); 168 ASSERT_EQ(PP_OK, callback.result());
176 if (rv == PP_OK_COMPLETIONPENDING)
177 rv = callback.WaitForResult();
178 if (rv != PP_OK)
179 return ReportError("URLLoader::Open", rv);
180 169
181 pp::URLResponseInfo response_info(loader.GetResponseInfo()); 170 pp::URLResponseInfo response_info(loader.GetResponseInfo());
182 if (response_info.is_null()) 171 if (response_info.is_null())
183 return "URLLoader::GetResponseInfo returned null"; 172 return "URLLoader::GetResponseInfo returned null";
184 int32_t status_code = response_info.GetStatusCode(); 173 int32_t status_code = response_info.GetStatusCode();
185 if (status_code != 200) 174 if (status_code != 200)
186 return "Unexpected HTTP status code"; 175 return "Unexpected HTTP status code";
187 176
188 std::string body; 177 std::string body;
189 std::string error = ReadEntireResponseBody(&loader, &body); 178 std::string error = ReadEntireResponseBody(&loader, &body);
190 if (!error.empty()) 179 if (!error.empty())
191 return error; 180 return error;
192 181
193 if (body.size() != expected_body.size()) 182 if (body.size() != expected_body.size())
194 return "URLLoader::ReadResponseBody returned unexpected content length"; 183 return "URLLoader::ReadResponseBody returned unexpected content length";
195 if (body != expected_body) 184 if (body != expected_body)
196 return "URLLoader::ReadResponseBody returned unexpected content"; 185 return "URLLoader::ReadResponseBody returned unexpected content";
197 186
198 PASS(); 187 PASS();
199 } 188 }
200 189
201 int32_t TestURLLoader::OpenFileSystem(pp::FileSystem* file_system, 190 int32_t TestURLLoader::OpenFileSystem(pp::FileSystem* file_system,
202 std::string* message) { 191 std::string* message) {
203 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 192 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
204 int32_t rv = file_system->Open(1024, callback); 193 callback.WaitForResult(file_system->Open(1024, callback));
205 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) { 194 if (callback.failed()) {
206 message->assign("FileSystem::Open force_async"); 195 message->assign(callback.errors());
207 return rv; 196 return callback.result();
208 } 197 }
209 if (rv == PP_OK_COMPLETIONPENDING) 198 if (callback.result() != PP_OK) {
210 rv = callback.WaitForResult();
211 if (rv != PP_OK) {
212 message->assign("FileSystem::Open"); 199 message->assign("FileSystem::Open");
213 return rv; 200 return callback.result();
214 } 201 }
215 return rv; 202 return callback.result();
216 } 203 }
217 204
218 int32_t TestURLLoader::PrepareFileForPost( 205 int32_t TestURLLoader::PrepareFileForPost(
219 const pp::FileRef& file_ref, 206 const pp::FileRef& file_ref,
220 const std::string& data, 207 const std::string& data,
221 std::string* message) { 208 std::string* message) {
222 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 209 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
223 pp::FileIO file_io(instance_); 210 pp::FileIO file_io(instance_);
224 int32_t rv = file_io.Open(file_ref, 211 callback.WaitForResult(file_io.Open(file_ref,
225 PP_FILEOPENFLAG_CREATE | 212 PP_FILEOPENFLAG_CREATE |
226 PP_FILEOPENFLAG_TRUNCATE | 213 PP_FILEOPENFLAG_TRUNCATE |
227 PP_FILEOPENFLAG_WRITE, 214 PP_FILEOPENFLAG_WRITE,
228 callback); 215 callback));
229 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) { 216 if (callback.failed()) {
230 message->assign("FileIO::Open force_async"); 217 message->assign(callback.errors());
231 return rv; 218 return callback.result();
232 } 219 }
233 if (rv == PP_OK_COMPLETIONPENDING) 220 if (callback.result() != PP_OK) {
234 rv = callback.WaitForResult(); 221 message->assign("FileIO::Open failed.");
222 return callback.result();
223 }
224
225 int32_t rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, data);
235 if (rv != PP_OK) { 226 if (rv != PP_OK) {
236 message->assign("FileIO::Open"); 227 message->assign("FileIO::Write failed.");
237 return rv; 228 return rv;
238 } 229 }
239 230
240 rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, data);
241 if (rv != PP_OK) {
242 message->assign("FileIO::Write");
243 return rv;
244 }
245
246 return rv; 231 return rv;
247 } 232 }
248 233
249 std::string TestURLLoader::GetReachableAbsoluteURL( 234 std::string TestURLLoader::GetReachableAbsoluteURL(
250 const std::string& file_name) { 235 const std::string& file_name) {
251 // Get the absolute page URL and replace the test case file name 236 // Get the absolute page URL and replace the test case file name
252 // with the given one. 237 // with the given one.
253 pp::Var document_url( 238 pp::Var document_url(
254 pp::PASS_REF, 239 pp::PASS_REF,
255 testing_interface_->GetDocumentURL(instance_->pp_instance(), 240 testing_interface_->GetDocumentURL(instance_->pp_instance(),
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 287
303 int32_t TestURLLoader::OpenTrusted(const pp::URLRequestInfo& request) { 288 int32_t TestURLLoader::OpenTrusted(const pp::URLRequestInfo& request) {
304 return Open(request, true); 289 return Open(request, true);
305 } 290 }
306 291
307 int32_t TestURLLoader::Open(const pp::URLRequestInfo& request, 292 int32_t TestURLLoader::Open(const pp::URLRequestInfo& request,
308 bool trusted) { 293 bool trusted) {
309 pp::URLLoader loader(instance_); 294 pp::URLLoader loader(instance_);
310 if (trusted) 295 if (trusted)
311 url_loader_trusted_interface_->GrantUniversalAccess(loader.pp_resource()); 296 url_loader_trusted_interface_->GrantUniversalAccess(loader.pp_resource());
312 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 297 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
313 int32_t rv = loader.Open(request, callback); 298 callback.WaitForResult(loader.Open(request, callback));
314 299 return callback.result();
315 if (rv == PP_OK_COMPLETIONPENDING)
316 rv = callback.WaitForResult();
317 else if (force_async_)
318 ReportError("URLLoader::Open force_async", rv);
319
320 return rv;
321 } 300 }
322 301
323 std::string TestURLLoader::TestBasicGET() { 302 std::string TestURLLoader::TestBasicGET() {
324 pp::URLRequestInfo request(instance_); 303 pp::URLRequestInfo request(instance_);
325 request.SetURL("test_url_loader_data/hello.txt"); 304 request.SetURL("test_url_loader_data/hello.txt");
326 return LoadAndCompareBody(request, "hello\n"); 305 return LoadAndCompareBody(request, "hello\n");
327 } 306 }
328 307
329 std::string TestURLLoader::TestBasicPOST() { 308 std::string TestURLLoader::TestBasicPOST() {
330 pp::URLRequestInfo request(instance_); 309 pp::URLRequestInfo request(instance_);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 "Untrusted request with bogus Content-Length restriction", rv); 409 "Untrusted request with bogus Content-Length restriction", rv);
431 410
432 PASS(); 411 PASS();
433 } 412 }
434 413
435 std::string TestURLLoader::TestStreamToFile() { 414 std::string TestURLLoader::TestStreamToFile() {
436 pp::URLRequestInfo request(instance_); 415 pp::URLRequestInfo request(instance_);
437 request.SetURL("test_url_loader_data/hello.txt"); 416 request.SetURL("test_url_loader_data/hello.txt");
438 request.SetStreamToFile(true); 417 request.SetStreamToFile(true);
439 418
440 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 419 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
441 420
442 pp::URLLoader loader(instance_); 421 pp::URLLoader loader(instance_);
443 int32_t rv = loader.Open(request, callback); 422 callback.WaitForResult(loader.Open(request, callback));
444 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 423 CHECK_CALLBACK_BEHAVIOR(callback);
445 return ReportError("URLLoader::Open force_async", rv); 424 ASSERT_EQ(PP_OK, callback.result());
446 if (rv == PP_OK_COMPLETIONPENDING)
447 rv = callback.WaitForResult();
448 if (rv != PP_OK)
449 return ReportError("URLLoader::Open", rv);
450 425
451 pp::URLResponseInfo response_info(loader.GetResponseInfo()); 426 pp::URLResponseInfo response_info(loader.GetResponseInfo());
452 if (response_info.is_null()) 427 if (response_info.is_null())
453 return "URLLoader::GetResponseInfo returned null"; 428 return "URLLoader::GetResponseInfo returned null";
454 int32_t status_code = response_info.GetStatusCode(); 429 int32_t status_code = response_info.GetStatusCode();
455 if (status_code != 200) 430 if (status_code != 200)
456 return "Unexpected HTTP status code"; 431 return "Unexpected HTTP status code";
457 432
458 pp::FileRef body(response_info.GetBodyAsFileRef()); 433 pp::FileRef body(response_info.GetBodyAsFileRef());
459 if (body.is_null()) 434 if (body.is_null())
460 return "URLResponseInfo::GetBody returned null"; 435 return "URLResponseInfo::GetBody returned null";
461 436
462 rv = loader.FinishStreamingToFile(callback); 437 callback.WaitForResult(loader.FinishStreamingToFile(callback));
463 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 438 CHECK_CALLBACK_BEHAVIOR(callback);
464 return ReportError("URLLoader::FinishStreamingToFile force_async", rv); 439 ASSERT_EQ(PP_OK, callback.result());
465 if (rv == PP_OK_COMPLETIONPENDING)
466 rv = callback.WaitForResult();
467 if (rv != PP_OK)
468 return ReportError("URLLoader::FinishStreamingToFile", rv);
469 440
470 // FileIO is not yet supported by ppapi/proxy.
471 pp::FileIO reader(instance_); 441 pp::FileIO reader(instance_);
472 rv = reader.Open(body, PP_FILEOPENFLAG_READ, callback); 442 callback.WaitForResult(reader.Open(body, PP_FILEOPENFLAG_READ, callback));
473 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 443 CHECK_CALLBACK_BEHAVIOR(callback);
474 return ReportError("FileIO::Open force_async", rv); 444 ASSERT_EQ(PP_OK, callback.result());
475 if (rv == PP_OK_COMPLETIONPENDING)
476 rv = callback.WaitForResult();
477 if (rv != PP_OK)
478 return ReportError("FileIO::Open", rv);
479 445
480 std::string data; 446 std::string data;
481 std::string error = ReadEntireFile(&reader, &data); 447 std::string error = ReadEntireFile(&reader, &data);
482 if (!error.empty()) 448 if (!error.empty())
483 return error; 449 return error;
484 450
485 std::string expected_body = "hello\n"; 451 std::string expected_body = "hello\n";
486 if (data.size() != expected_body.size()) 452 if (data.size() != expected_body.size())
487 return "ReadEntireFile returned unexpected content length"; 453 return "ReadEntireFile returned unexpected content length";
488 if (data != expected_body) 454 if (data != expected_body)
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 pp::URLRequestInfo request(instance_); 685 pp::URLRequestInfo request(instance_);
720 // This path will cause the server to return a 301 redirect. 686 // This path will cause the server to return a 301 redirect.
721 // This prefix causes the test server to return a 301 redirect. 687 // This prefix causes the test server to return a 301 redirect.
722 std::string redirect_prefix("/server-redirect?"); 688 std::string redirect_prefix("/server-redirect?");
723 // We need an absolute path for the redirect to actually work. 689 // We need an absolute path for the redirect to actually work.
724 std::string redirect_url = 690 std::string redirect_url =
725 GetReachableAbsoluteURL("test_url_loader_data/hello.txt"); 691 GetReachableAbsoluteURL("test_url_loader_data/hello.txt");
726 request.SetURL(redirect_prefix.append(redirect_url)); 692 request.SetURL(redirect_prefix.append(redirect_url));
727 request.SetFollowRedirects(false); 693 request.SetFollowRedirects(false);
728 694
729 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 695 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
730 696
731 pp::URLLoader loader(instance_); 697 pp::URLLoader loader(instance_);
732 int32_t rv = loader.Open(request, callback); 698 callback.WaitForResult(loader.Open(request, callback));
733 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 699 CHECK_CALLBACK_BEHAVIOR(callback);
734 return ReportError("URLLoader::Open force_async", rv); 700 ASSERT_EQ(PP_OK, callback.result());
735 if (rv == PP_OK_COMPLETIONPENDING)
736 rv = callback.WaitForResult();
737 if (rv != PP_OK)
738 return ReportError("URLLoader::Open", rv);
739 701
740 // Checks that the response indicates a redirect, and that the URL 702 // Checks that the response indicates a redirect, and that the URL
741 // is correct. 703 // is correct.
742 pp::URLResponseInfo response_info(loader.GetResponseInfo()); 704 pp::URLResponseInfo response_info(loader.GetResponseInfo());
743 if (response_info.is_null()) 705 if (response_info.is_null())
744 return "URLLoader::GetResponseInfo returned null"; 706 return "URLLoader::GetResponseInfo returned null";
745 int32_t status_code = response_info.GetStatusCode(); 707 int32_t status_code = response_info.GetStatusCode();
746 if (status_code != 301) 708 if (status_code != 301)
747 return "Response status should be 301"; 709 return "Response status should be 301";
748 710
749 // Test that the paused loader can be resumed. 711 // Test that the paused loader can be resumed.
750 TestCompletionCallback redirect_callback(instance_->pp_instance(), 712 callback.WaitForResult(loader.FollowRedirect(callback));
751 force_async_); 713 CHECK_CALLBACK_BEHAVIOR(callback);
752 rv = loader.FollowRedirect(redirect_callback); 714 ASSERT_EQ(PP_OK, callback.result());
753 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
754 return ReportError("URLLoader::FollowRedirect force_async", rv);
755 if (rv == PP_OK_COMPLETIONPENDING)
756 rv = redirect_callback.WaitForResult();
757 if (rv != PP_OK)
758 return ReportError("URLLoader::FollowRedirect", rv);
759 std::string body; 715 std::string body;
760 std::string error = ReadEntireResponseBody(&loader, &body); 716 std::string error = ReadEntireResponseBody(&loader, &body);
761 if (!error.empty()) 717 if (!error.empty())
762 return error; 718 return error;
763 719
764 if (body != "hello\n") 720 if (body != "hello\n")
765 return "URLLoader::FollowRedirect failed"; 721 return "URLLoader::FollowRedirect failed";
766 722
767 PASS(); 723 PASS();
768 } 724 }
769 725
770 std::string TestURLLoader::TestAbortCalls() { 726 std::string TestURLLoader::TestAbortCalls() {
771 pp::URLRequestInfo request(instance_); 727 pp::URLRequestInfo request(instance_);
772 request.SetURL("test_url_loader_data/hello.txt"); 728 request.SetURL("test_url_loader_data/hello.txt");
773 729
774 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 730 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
775 int32_t rv; 731 int32_t rv;
776 732
777 // Abort |Open()|. 733 // Abort |Open()|.
778 { 734 {
779 callback.reset_run_count();
780 rv = pp::URLLoader(instance_).Open(request, callback); 735 rv = pp::URLLoader(instance_).Open(request, callback);
781 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
782 return ReportError("URLLoader::Open force_async", rv);
783 if (callback.run_count() > 0)
784 return "URLLoader::Open ran callback synchronously.";
785 if (rv == PP_OK_COMPLETIONPENDING) {
786 rv = callback.WaitForResult();
787 if (rv != PP_ERROR_ABORTED)
788 return "URLLoader::Open not aborted.";
789 } else if (rv != PP_OK) {
790 return ReportError("URLLoader::Open", rv);
791 }
792 } 736 }
737 callback.WaitForAbortResult(rv);
738 CHECK_CALLBACK_BEHAVIOR(callback);
793 739
794 // Abort |ReadResponseBody()|. 740 // Abort |ReadResponseBody()|.
795 { 741 {
796 char buf[2] = { 0 }; 742 char buf[2] = { 0 };
797 { 743 {
798 pp::URLLoader loader(instance_); 744 pp::URLLoader loader(instance_);
799 rv = loader.Open(request, callback); 745 callback.WaitForResult(loader.Open(request, callback));
800 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 746 CHECK_CALLBACK_BEHAVIOR(callback);
801 return ReportError("URLLoader::Open force_async", rv); 747 ASSERT_EQ(PP_OK, callback.result());
802 if (rv == PP_OK_COMPLETIONPENDING)
803 rv = callback.WaitForResult();
804 if (rv != PP_OK)
805 return ReportError("URLLoader::Open", rv);
806 748
807 callback.reset_run_count();
808 rv = loader.ReadResponseBody(buf, sizeof(buf), callback); 749 rv = loader.ReadResponseBody(buf, sizeof(buf), callback);
809 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
810 return ReportError("URLLoader::ReadResponseBody force_async", rv);
811 } // Destroy |loader|. 750 } // Destroy |loader|.
751 callback.WaitForAbortResult(rv);
752 CHECK_CALLBACK_BEHAVIOR(callback);
812 if (rv == PP_OK_COMPLETIONPENDING) { 753 if (rv == PP_OK_COMPLETIONPENDING) {
813 // Save a copy and make sure |buf| doesn't get written to. 754 if (buf[0] || buf[1]) {
814 char buf_copy[2];
815 memcpy(&buf_copy, &buf, sizeof(buf));
816 rv = callback.WaitForResult();
817 if (rv != PP_ERROR_ABORTED)
818 return "URLLoader::ReadResponseBody not aborted.";
819 if (memcmp(&buf_copy, &buf, sizeof(buf)) != 0)
820 return "URLLoader::ReadResponseBody wrote data after resource " 755 return "URLLoader::ReadResponseBody wrote data after resource "
821 "destruction."; 756 "destruction.";
822 } else if (rv != PP_OK) { 757 }
823 return ReportError("URLLoader::ReadResponseBody", rv);
824 } 758 }
825 } 759 }
826 760
827 // TODO(viettrungluu): More abort tests (but add basic tests first). 761 // TODO(viettrungluu): More abort tests (but add basic tests first).
828 // Also test that Close() aborts properly. crbug.com/69457 762 // Also test that Close() aborts properly. crbug.com/69457
829 763
830 PASS(); 764 PASS();
831 } 765 }
832 766
833 std::string TestURLLoader::TestUntendedLoad() { 767 std::string TestURLLoader::TestUntendedLoad() {
834 pp::URLRequestInfo request(instance_); 768 pp::URLRequestInfo request(instance_);
835 request.SetURL("test_url_loader_data/hello.txt"); 769 request.SetURL("test_url_loader_data/hello.txt");
836 request.SetRecordDownloadProgress(true); 770 request.SetRecordDownloadProgress(true);
837 TestCompletionCallback callback(instance_->pp_instance(), force_async_); 771 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
838 772
839 pp::URLLoader loader(instance_); 773 pp::URLLoader loader(instance_);
840 int32_t rv = loader.Open(request, callback); 774 callback.WaitForResult(loader.Open(request, callback));
841 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) 775 CHECK_CALLBACK_BEHAVIOR(callback);
842 return ReportError("URLLoader::Open force_async", rv); 776 ASSERT_EQ(PP_OK, callback.result());
843 if (rv == PP_OK_COMPLETIONPENDING)
844 rv = callback.WaitForResult();
845 if (rv != PP_OK)
846 return ReportError("URLLoader::Open", rv);
847 777
848 // We received the response callback. Yield until the network code has called 778 // We received the response callback. Yield until the network code has called
849 // the loader's didReceiveData and didFinishLoading methods before we give it 779 // the loader's didReceiveData and didFinishLoading methods before we give it
850 // another callback function, to make sure the loader works with no callback. 780 // another callback function, to make sure the loader works with no callback.
851 int64_t bytes_received = 0; 781 int64_t bytes_received = 0;
852 int64_t total_bytes_to_be_received = 0; 782 int64_t total_bytes_to_be_received = 0;
853 while (true) { 783 while (true) {
854 loader.GetDownloadProgress(&bytes_received, &total_bytes_to_be_received); 784 loader.GetDownloadProgress(&bytes_received, &total_bytes_to_be_received);
855 if (total_bytes_to_be_received <= 0) 785 if (total_bytes_to_be_received <= 0)
856 return ReportError("URLLoader::GetDownloadProgress total size", 786 return ReportError("URLLoader::GetDownloadProgress total size",
857 total_bytes_to_be_received); 787 total_bytes_to_be_received);
858 if (bytes_received == total_bytes_to_be_received) 788 if (bytes_received == total_bytes_to_be_received)
859 break; 789 break;
860 pp::Module::Get()->core()->CallOnMainThread(10, callback); 790 pp::Module::Get()->core()->CallOnMainThread(10, callback);
861 callback.WaitForResult(); 791 callback.WaitForResult();
862 } 792 }
863 793
864 // The loader should now have the data and have finished successfully. 794 // The loader should now have the data and have finished successfully.
865 std::string body; 795 std::string body;
866 std::string error = ReadEntireResponseBody(&loader, &body); 796 std::string error = ReadEntireResponseBody(&loader, &body);
867 if (!error.empty()) 797 if (!error.empty())
868 return error; 798 return error;
869 if (body != "hello\n") 799 if (body != "hello\n")
870 return ReportError("Couldn't read data", rv); 800 return ReportError("Couldn't read data", callback.result());
871 801
872 PASS(); 802 PASS();
873 } 803 }
874 804
875 int32_t TestURLLoader::OpenWithPrefetchBufferThreshold(int32_t lower, 805 int32_t TestURLLoader::OpenWithPrefetchBufferThreshold(int32_t lower,
876 int32_t upper) { 806 int32_t upper) {
877 pp::URLRequestInfo request(instance_); 807 pp::URLRequestInfo request(instance_);
878 request.SetURL("test_url_loader_data/hello.txt"); 808 request.SetURL("test_url_loader_data/hello.txt");
879 request.SetPrefetchBufferLowerThreshold(lower); 809 request.SetPrefetchBufferLowerThreshold(lower);
880 request.SetPrefetchBufferUpperThreshold(upper); 810 request.SetPrefetchBufferUpperThreshold(upper);
(...skipping 18 matching lines...) Expand all
899 if (rv != PP_ERROR_FAILED) { 829 if (rv != PP_ERROR_FAILED) {
900 return ReportError("The lower buffer value was higher than the upper but " 830 return ReportError("The lower buffer value was higher than the upper but "
901 "the URLLoader did not fail.", rv); 831 "the URLLoader did not fail.", rv);
902 } 832 }
903 833
904 PASS(); 834 PASS();
905 } 835 }
906 836
907 // TODO(viettrungluu): Add tests for Get{Upload,Download}Progress, Close 837 // TODO(viettrungluu): Add tests for Get{Upload,Download}Progress, Close
908 // (including abort tests if applicable). 838 // (including abort tests if applicable).
OLDNEW
« no previous file with comments | « ppapi/tests/test_network_monitor_private.cc ('k') | ppapi/tests/test_url_request.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698