OLD | NEW |
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 // Tests PPB_URLRequestInfo interface. | 5 // Tests PPB_URLRequestInfo interface. |
6 | 6 |
7 #include "ppapi/tests/test_url_request.h" | 7 #include "ppapi/tests/test_url_request.h" |
8 | 8 |
9 #include <string.h> | 9 #include <string.h> |
10 #include <string> | 10 #include <string> |
(...skipping 22 matching lines...) Expand all Loading... |
33 const PP_Instance kNotAnInstance = 0xFFFFF0; | 33 const PP_Instance kNotAnInstance = 0xFFFFF0; |
34 const PP_Resource kNotAResource = 0xAAAAA0; | 34 const PP_Resource kNotAResource = 0xAAAAA0; |
35 } | 35 } |
36 | 36 |
37 TestURLRequest::TestURLRequest(TestingInstance* instance) | 37 TestURLRequest::TestURLRequest(TestingInstance* instance) |
38 : TestCase(instance), | 38 : TestCase(instance), |
39 ppb_url_request_interface_(NULL), | 39 ppb_url_request_interface_(NULL), |
40 ppb_url_loader_interface_(NULL), | 40 ppb_url_loader_interface_(NULL), |
41 ppb_url_response_interface_(NULL), | 41 ppb_url_response_interface_(NULL), |
42 ppb_core_interface_(NULL), | 42 ppb_core_interface_(NULL), |
43 ppb_var_interface_(NULL), | 43 ppb_var_interface_(NULL) { |
44 url_loader_(kInvalidResource) { | |
45 } | 44 } |
46 | 45 |
47 bool TestURLRequest::Init() { | 46 bool TestURLRequest::Init() { |
48 ppb_url_request_interface_ = static_cast<const PPB_URLRequestInfo*>( | 47 ppb_url_request_interface_ = static_cast<const PPB_URLRequestInfo*>( |
49 pp::Module::Get()->GetBrowserInterface(PPB_URLREQUESTINFO_INTERFACE)); | 48 pp::Module::Get()->GetBrowserInterface(PPB_URLREQUESTINFO_INTERFACE)); |
50 ppb_url_loader_interface_ = static_cast<const PPB_URLLoader*>( | 49 ppb_url_loader_interface_ = static_cast<const PPB_URLLoader*>( |
51 pp::Module::Get()->GetBrowserInterface(PPB_URLLOADER_INTERFACE)); | 50 pp::Module::Get()->GetBrowserInterface(PPB_URLLOADER_INTERFACE)); |
52 ppb_url_response_interface_ = static_cast<const PPB_URLResponseInfo*>( | 51 ppb_url_response_interface_ = static_cast<const PPB_URLResponseInfo*>( |
53 pp::Module::Get()->GetBrowserInterface(PPB_URLRESPONSEINFO_INTERFACE)); | 52 pp::Module::Get()->GetBrowserInterface(PPB_URLRESPONSEINFO_INTERFACE)); |
54 ppb_core_interface_ = static_cast<const PPB_Core*>( | 53 ppb_core_interface_ = static_cast<const PPB_Core*>( |
55 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | 54 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); |
56 ppb_var_interface_ = static_cast<const PPB_Var*>( | 55 ppb_var_interface_ = static_cast<const PPB_Var*>( |
57 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | 56 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); |
58 if (!ppb_url_request_interface_) | 57 if (!ppb_url_request_interface_) |
59 instance_->AppendError("PPB_URLRequestInfo interface not available"); | 58 instance_->AppendError("PPB_URLRequestInfo interface not available"); |
60 if (!ppb_url_response_interface_) | 59 if (!ppb_url_response_interface_) |
61 instance_->AppendError("PPB_URLResponseInfo interface not available"); | 60 instance_->AppendError("PPB_URLResponseInfo interface not available"); |
62 if (!ppb_core_interface_) | 61 if (!ppb_core_interface_) |
63 instance_->AppendError("PPB_Core interface not available"); | 62 instance_->AppendError("PPB_Core interface not available"); |
64 if (!ppb_var_interface_) | 63 if (!ppb_var_interface_) |
65 instance_->AppendError("PPB_Var interface not available"); | 64 instance_->AppendError("PPB_Var interface not available"); |
66 if (!ppb_url_loader_interface_) { | 65 if (!ppb_url_loader_interface_) { |
67 instance_->AppendError("PPB_URLLoader interface not available"); | 66 instance_->AppendError("PPB_URLLoader interface not available"); |
68 } else { | |
69 url_loader_ = ppb_url_loader_interface_->Create(instance_->pp_instance()); | |
70 if (url_loader_ == kInvalidResource) | |
71 instance_->AppendError("Failed to create URLLoader"); | |
72 } | 67 } |
73 return EnsureRunningOverHTTP(); | 68 return EnsureRunningOverHTTP(); |
74 } | 69 } |
75 | 70 |
76 void TestURLRequest::RunTests(const std::string& filter) { | 71 void TestURLRequest::RunTests(const std::string& filter) { |
77 RUN_TEST(CreateAndIsURLRequestInfo, filter); | 72 RUN_TEST(CreateAndIsURLRequestInfo, filter); |
78 RUN_TEST(SetProperty, filter); | 73 RUN_TEST(SetProperty, filter); |
79 RUN_TEST(AppendDataToBody, filter); | 74 RUN_TEST(AppendDataToBody, filter); |
80 RUN_TEST(AppendFileToBody, filter); | 75 RUN_TEST(AppendFileToBody, filter); |
81 RUN_TEST(Stress, filter); | 76 RUN_TEST(Stress, filter); |
(...skipping 17 matching lines...) Expand all Loading... |
99 PP_Resource url_request = ppb_url_request_interface_->Create( | 94 PP_Resource url_request = ppb_url_request_interface_->Create( |
100 instance_->pp_instance()); | 95 instance_->pp_instance()); |
101 ASSERT_NE(url_request, kInvalidResource); | 96 ASSERT_NE(url_request, kInvalidResource); |
102 | 97 |
103 // IsURLRequestInfo: | 98 // IsURLRequestInfo: |
104 // Invalid / non-existent / non-URLRequestInfo resource -> false. | 99 // Invalid / non-existent / non-URLRequestInfo resource -> false. |
105 ASSERT_NE(PP_TRUE, | 100 ASSERT_NE(PP_TRUE, |
106 ppb_url_request_interface_->IsURLRequestInfo(kInvalidResource)); | 101 ppb_url_request_interface_->IsURLRequestInfo(kInvalidResource)); |
107 ASSERT_NE(PP_TRUE, | 102 ASSERT_NE(PP_TRUE, |
108 ppb_url_request_interface_->IsURLRequestInfo(kNotAResource)); | 103 ppb_url_request_interface_->IsURLRequestInfo(kNotAResource)); |
109 ASSERT_NE(PP_TRUE, ppb_url_request_interface_->IsURLRequestInfo(url_loader_)); | 104 |
| 105 PP_Resource url_loader = |
| 106 ppb_url_loader_interface_->Create(instance_->pp_instance()); |
| 107 ASSERT_NE(kInvalidResource, url_loader); |
| 108 |
| 109 ASSERT_NE(PP_TRUE, ppb_url_request_interface_->IsURLRequestInfo(url_loader)); |
| 110 ppb_url_loader_interface_->Close(url_loader); |
| 111 ppb_core_interface_->ReleaseResource(url_loader); |
110 | 112 |
111 // IsURLRequestInfo: Current URLRequestInfo resource -> true. | 113 // IsURLRequestInfo: Current URLRequestInfo resource -> true. |
112 std::string error; | 114 std::string error; |
113 if (PP_FALSE == ppb_url_request_interface_->IsURLRequestInfo(url_request)) | 115 if (PP_FALSE == ppb_url_request_interface_->IsURLRequestInfo(url_request)) |
114 error = "IsURLRequestInfo() failed with a current URLRequestInfo resource"; | 116 error = "IsURLRequestInfo() failed with a current URLRequestInfo resource"; |
115 | 117 |
116 // IsURLRequestInfo: Released URLRequestInfo resource -> false. | 118 // IsURLRequestInfo: Released URLRequestInfo resource -> false. |
117 ppb_core_interface_->ReleaseResource(url_request); | 119 ppb_core_interface_->ReleaseResource(url_request); |
118 ASSERT_NE(PP_TRUE, ppb_url_request_interface_->IsURLRequestInfo(url_request)); | 120 ASSERT_NE(PP_TRUE, ppb_url_request_interface_->IsURLRequestInfo(url_request)); |
119 | 121 |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 ppb_var_interface_->Release(test_data[i].var); | 284 ppb_var_interface_->Release(test_data[i].var); |
283 } | 285 } |
284 | 286 |
285 ppb_core_interface_->ReleaseResource(url_request); | 287 ppb_core_interface_->ReleaseResource(url_request); |
286 return error; // == PASS() if empty. | 288 return error; // == PASS() if empty. |
287 } | 289 } |
288 | 290 |
289 std::string TestURLRequest::LoadAndCompareBody( | 291 std::string TestURLRequest::LoadAndCompareBody( |
290 PP_Resource url_request, const std::string& expected_body) { | 292 PP_Resource url_request, const std::string& expected_body) { |
291 TestCompletionCallback callback(instance_->pp_instance(), PP_REQUIRED); | 293 TestCompletionCallback callback(instance_->pp_instance(), PP_REQUIRED); |
| 294 |
| 295 PP_Resource url_loader = |
| 296 ppb_url_loader_interface_->Create(instance_->pp_instance()); |
| 297 ASSERT_NE(kInvalidResource, url_loader); |
| 298 |
292 callback.WaitForResult(ppb_url_loader_interface_->Open( | 299 callback.WaitForResult(ppb_url_loader_interface_->Open( |
293 url_loader_, url_request, | 300 url_loader, url_request, |
294 callback.GetCallback().pp_completion_callback())); | 301 callback.GetCallback().pp_completion_callback())); |
295 CHECK_CALLBACK_BEHAVIOR(callback); | 302 CHECK_CALLBACK_BEHAVIOR(callback); |
296 ASSERT_EQ(PP_OK, callback.result()); | 303 ASSERT_EQ(PP_OK, callback.result()); |
297 | 304 |
298 std::string error; | 305 std::string error; |
299 PP_Resource url_response = | 306 PP_Resource url_response = |
300 ppb_url_loader_interface_->GetResponseInfo(url_loader_); | 307 ppb_url_loader_interface_->GetResponseInfo(url_loader); |
301 if (url_response == kInvalidResource) { | 308 if (url_response == kInvalidResource) { |
302 error = "PPB_URLLoader::GetResponseInfo() returned invalid resource"; | 309 error = "PPB_URLLoader::GetResponseInfo() returned invalid resource"; |
303 } else { | 310 } else { |
304 PP_Var status = ppb_url_response_interface_->GetProperty( | 311 PP_Var status = ppb_url_response_interface_->GetProperty( |
305 url_response, PP_URLRESPONSEPROPERTY_STATUSCODE); | 312 url_response, PP_URLRESPONSEPROPERTY_STATUSCODE); |
306 if (status.type != PP_VARTYPE_INT32 && status.value.as_int != 200) | 313 if (status.type != PP_VARTYPE_INT32 && status.value.as_int != 200) |
307 error = ReportError("PPB_URLLoader::Open() status", status.value.as_int); | 314 error = ReportError("PPB_URLLoader::Open() status", status.value.as_int); |
308 | 315 |
309 std::string actual_body; | 316 std::string actual_body; |
310 for (; error.empty();) { // Read the entire body in this loop. | 317 for (; error.empty();) { // Read the entire body in this loop. |
311 const size_t kBufferSize = 32; | 318 const size_t kBufferSize = 32; |
312 char buf[kBufferSize]; | 319 char buf[kBufferSize]; |
313 callback.WaitForResult(ppb_url_loader_interface_->ReadResponseBody( | 320 callback.WaitForResult(ppb_url_loader_interface_->ReadResponseBody( |
314 url_loader_, buf, kBufferSize, | 321 url_loader, buf, kBufferSize, |
315 callback.GetCallback().pp_completion_callback())); | 322 callback.GetCallback().pp_completion_callback())); |
316 if (callback.failed()) | 323 if (callback.failed()) |
317 error.assign(callback.errors()); | 324 error.assign(callback.errors()); |
318 else if (callback.result() < PP_OK) | 325 else if (callback.result() < PP_OK) |
319 error.assign(ReportError("PPB_URLLoader::ReadResponseBody()", | 326 error.assign(ReportError("PPB_URLLoader::ReadResponseBody()", |
320 callback.result())); | 327 callback.result())); |
321 if (callback.result() <= PP_OK || callback.failed()) | 328 if (callback.result() <= PP_OK || callback.failed()) |
322 break; | 329 break; |
323 actual_body.append(buf, callback.result()); | 330 actual_body.append(buf, callback.result()); |
324 } | 331 } |
325 if (actual_body != expected_body) | 332 if (actual_body != expected_body) |
326 error = "PPB_URLLoader::ReadResponseBody() read unexpected response."; | 333 error = "PPB_URLLoader::ReadResponseBody() read unexpected response."; |
327 } | 334 } |
328 ppb_core_interface_->ReleaseResource(url_response); | 335 ppb_core_interface_->ReleaseResource(url_response); |
329 | 336 |
330 ppb_url_loader_interface_->Close(url_loader_); | 337 ppb_url_loader_interface_->Close(url_loader); |
| 338 ppb_core_interface_->ReleaseResource(url_loader); |
331 return error; | 339 return error; |
332 } | 340 } |
333 | 341 |
334 // Tests | 342 // Tests |
335 // PP_Bool AppendDataToBody( | 343 // PP_Bool AppendDataToBody( |
336 // PP_Resource request, const void* data, uint32_t len); | 344 // PP_Resource request, const void* data, uint32_t len); |
337 std::string TestURLRequest::TestAppendDataToBody() { | 345 std::string TestURLRequest::TestAppendDataToBody() { |
338 PP_Resource url_request = ppb_url_request_interface_->Create( | 346 PP_Resource url_request = ppb_url_request_interface_->Create( |
339 instance_->pp_instance()); | 347 instance_->pp_instance()); |
340 ASSERT_NE(url_request, kInvalidResource); | 348 ASSERT_NE(url_request, kInvalidResource); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 } | 459 } |
452 } | 460 } |
453 for (int i = 0; i < num_created; i++) { | 461 for (int i = 0; i < num_created; i++) { |
454 ppb_core_interface_->ReleaseResource(url_request_info[i]); | 462 ppb_core_interface_->ReleaseResource(url_request_info[i]); |
455 if (PP_TRUE == | 463 if (PP_TRUE == |
456 ppb_url_request_interface_->IsURLRequestInfo(url_request_info[i])) | 464 ppb_url_request_interface_->IsURLRequestInfo(url_request_info[i])) |
457 error = "IsURLREquestInfo() succeeded after release"; | 465 error = "IsURLREquestInfo() succeeded after release"; |
458 } | 466 } |
459 return error; // == PASS() if empty. | 467 return error; // == PASS() if empty. |
460 } | 468 } |
OLD | NEW |