Chromium Code Reviews| 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 if (url_loader == kInvalidResource) { | |
| 108 return "Failed to create URLLoader"; | |
|
dmichael (off chromium)
2014/09/19 16:05:12
ASSERT_NE(kInvalidResource, url_loader);
?
| |
| 109 } | |
| 110 ASSERT_NE(PP_TRUE, ppb_url_request_interface_->IsURLRequestInfo(url_loader)); | |
| 111 ppb_url_loader_interface_->Close(url_loader); | |
| 112 ppb_core_interface_->ReleaseResource(url_loader); | |
| 110 | 113 |
| 111 // IsURLRequestInfo: Current URLRequestInfo resource -> true. | 114 // IsURLRequestInfo: Current URLRequestInfo resource -> true. |
| 112 std::string error; | 115 std::string error; |
| 113 if (PP_FALSE == ppb_url_request_interface_->IsURLRequestInfo(url_request)) | 116 if (PP_FALSE == ppb_url_request_interface_->IsURLRequestInfo(url_request)) |
| 114 error = "IsURLRequestInfo() failed with a current URLRequestInfo resource"; | 117 error = "IsURLRequestInfo() failed with a current URLRequestInfo resource"; |
| 115 | 118 |
| 116 // IsURLRequestInfo: Released URLRequestInfo resource -> false. | 119 // IsURLRequestInfo: Released URLRequestInfo resource -> false. |
| 117 ppb_core_interface_->ReleaseResource(url_request); | 120 ppb_core_interface_->ReleaseResource(url_request); |
| 118 ASSERT_NE(PP_TRUE, ppb_url_request_interface_->IsURLRequestInfo(url_request)); | 121 ASSERT_NE(PP_TRUE, ppb_url_request_interface_->IsURLRequestInfo(url_request)); |
| 119 | 122 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 ppb_var_interface_->Release(test_data[i].var); | 285 ppb_var_interface_->Release(test_data[i].var); |
| 283 } | 286 } |
| 284 | 287 |
| 285 ppb_core_interface_->ReleaseResource(url_request); | 288 ppb_core_interface_->ReleaseResource(url_request); |
| 286 return error; // == PASS() if empty. | 289 return error; // == PASS() if empty. |
| 287 } | 290 } |
| 288 | 291 |
| 289 std::string TestURLRequest::LoadAndCompareBody( | 292 std::string TestURLRequest::LoadAndCompareBody( |
| 290 PP_Resource url_request, const std::string& expected_body) { | 293 PP_Resource url_request, const std::string& expected_body) { |
| 291 TestCompletionCallback callback(instance_->pp_instance(), PP_REQUIRED); | 294 TestCompletionCallback callback(instance_->pp_instance(), PP_REQUIRED); |
| 295 | |
| 296 PP_Resource url_loader = | |
| 297 ppb_url_loader_interface_->Create(instance_->pp_instance()); | |
| 298 if (url_loader == kInvalidResource) { | |
| 299 return "Failed to create URLLoader"; | |
|
dmichael (off chromium)
2014/09/19 16:05:12
ditto
| |
| 300 } | |
| 292 callback.WaitForResult(ppb_url_loader_interface_->Open( | 301 callback.WaitForResult(ppb_url_loader_interface_->Open( |
| 293 url_loader_, url_request, | 302 url_loader, url_request, |
| 294 callback.GetCallback().pp_completion_callback())); | 303 callback.GetCallback().pp_completion_callback())); |
| 295 CHECK_CALLBACK_BEHAVIOR(callback); | 304 CHECK_CALLBACK_BEHAVIOR(callback); |
| 296 ASSERT_EQ(PP_OK, callback.result()); | 305 ASSERT_EQ(PP_OK, callback.result()); |
| 297 | 306 |
| 298 std::string error; | 307 std::string error; |
| 299 PP_Resource url_response = | 308 PP_Resource url_response = |
| 300 ppb_url_loader_interface_->GetResponseInfo(url_loader_); | 309 ppb_url_loader_interface_->GetResponseInfo(url_loader); |
| 301 if (url_response == kInvalidResource) { | 310 if (url_response == kInvalidResource) { |
| 302 error = "PPB_URLLoader::GetResponseInfo() returned invalid resource"; | 311 error = "PPB_URLLoader::GetResponseInfo() returned invalid resource"; |
| 303 } else { | 312 } else { |
| 304 PP_Var status = ppb_url_response_interface_->GetProperty( | 313 PP_Var status = ppb_url_response_interface_->GetProperty( |
| 305 url_response, PP_URLRESPONSEPROPERTY_STATUSCODE); | 314 url_response, PP_URLRESPONSEPROPERTY_STATUSCODE); |
| 306 if (status.type != PP_VARTYPE_INT32 && status.value.as_int != 200) | 315 if (status.type != PP_VARTYPE_INT32 && status.value.as_int != 200) |
| 307 error = ReportError("PPB_URLLoader::Open() status", status.value.as_int); | 316 error = ReportError("PPB_URLLoader::Open() status", status.value.as_int); |
| 308 | 317 |
| 309 std::string actual_body; | 318 std::string actual_body; |
| 310 for (; error.empty();) { // Read the entire body in this loop. | 319 for (; error.empty();) { // Read the entire body in this loop. |
| 311 const size_t kBufferSize = 32; | 320 const size_t kBufferSize = 32; |
| 312 char buf[kBufferSize]; | 321 char buf[kBufferSize]; |
| 313 callback.WaitForResult(ppb_url_loader_interface_->ReadResponseBody( | 322 callback.WaitForResult(ppb_url_loader_interface_->ReadResponseBody( |
| 314 url_loader_, buf, kBufferSize, | 323 url_loader, buf, kBufferSize, |
| 315 callback.GetCallback().pp_completion_callback())); | 324 callback.GetCallback().pp_completion_callback())); |
| 316 if (callback.failed()) | 325 if (callback.failed()) |
| 317 error.assign(callback.errors()); | 326 error.assign(callback.errors()); |
| 318 else if (callback.result() < PP_OK) | 327 else if (callback.result() < PP_OK) |
| 319 error.assign(ReportError("PPB_URLLoader::ReadResponseBody()", | 328 error.assign(ReportError("PPB_URLLoader::ReadResponseBody()", |
| 320 callback.result())); | 329 callback.result())); |
| 321 if (callback.result() <= PP_OK || callback.failed()) | 330 if (callback.result() <= PP_OK || callback.failed()) |
| 322 break; | 331 break; |
| 323 actual_body.append(buf, callback.result()); | 332 actual_body.append(buf, callback.result()); |
| 324 } | 333 } |
| 325 if (actual_body != expected_body) | 334 if (actual_body != expected_body) |
| 326 error = "PPB_URLLoader::ReadResponseBody() read unexpected response."; | 335 error = "PPB_URLLoader::ReadResponseBody() read unexpected response."; |
| 327 } | 336 } |
| 328 ppb_core_interface_->ReleaseResource(url_response); | 337 ppb_core_interface_->ReleaseResource(url_response); |
| 329 | 338 |
| 330 ppb_url_loader_interface_->Close(url_loader_); | 339 ppb_url_loader_interface_->Close(url_loader); |
| 340 ppb_core_interface_->ReleaseResource(url_loader); | |
| 331 return error; | 341 return error; |
| 332 } | 342 } |
| 333 | 343 |
| 334 // Tests | 344 // Tests |
| 335 // PP_Bool AppendDataToBody( | 345 // PP_Bool AppendDataToBody( |
| 336 // PP_Resource request, const void* data, uint32_t len); | 346 // PP_Resource request, const void* data, uint32_t len); |
| 337 std::string TestURLRequest::TestAppendDataToBody() { | 347 std::string TestURLRequest::TestAppendDataToBody() { |
| 338 PP_Resource url_request = ppb_url_request_interface_->Create( | 348 PP_Resource url_request = ppb_url_request_interface_->Create( |
| 339 instance_->pp_instance()); | 349 instance_->pp_instance()); |
| 340 ASSERT_NE(url_request, kInvalidResource); | 350 ASSERT_NE(url_request, kInvalidResource); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 451 } | 461 } |
| 452 } | 462 } |
| 453 for (int i = 0; i < num_created; i++) { | 463 for (int i = 0; i < num_created; i++) { |
| 454 ppb_core_interface_->ReleaseResource(url_request_info[i]); | 464 ppb_core_interface_->ReleaseResource(url_request_info[i]); |
| 455 if (PP_TRUE == | 465 if (PP_TRUE == |
| 456 ppb_url_request_interface_->IsURLRequestInfo(url_request_info[i])) | 466 ppb_url_request_interface_->IsURLRequestInfo(url_request_info[i])) |
| 457 error = "IsURLREquestInfo() succeeded after release"; | 467 error = "IsURLREquestInfo() succeeded after release"; |
| 458 } | 468 } |
| 459 return error; // == PASS() if empty. | 469 return error; // == PASS() if empty. |
| 460 } | 470 } |
| OLD | NEW |