| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #import "ios/web/web_state/js/crw_js_post_request_loader.h" | 5 #import "ios/web/web_state/js/crw_js_post_request_loader.h" |
| 6 | 6 |
| 7 #import <WebKit/WebKit.h> | 7 #import <WebKit/WebKit.h> |
| 8 | 8 |
| 9 #import "base/mac/foundation_util.h" | 9 #import "base/mac/foundation_util.h" |
| 10 #import "base/mac/scoped_nsobject.h" | |
| 11 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 12 #import "base/test/ios/wait_util.h" | 11 #import "base/test/ios/wait_util.h" |
| 13 #include "ios/web/public/test/web_test.h" | 12 #include "ios/web/public/test/web_test.h" |
| 14 #import "ios/web/public/web_view_creation_util.h" | 13 #import "ios/web/public/web_view_creation_util.h" |
| 15 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" | 14 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" |
| 16 #import "testing/gtest_mac.h" | 15 #import "testing/gtest_mac.h" |
| 17 #import "third_party/ocmock/OCMock/OCMock.h" | 16 #import "third_party/ocmock/OCMock/OCMock.h" |
| 18 | 17 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 19 #error "This file requires ARC support." |
| 20 #endif |
| 21 |
| 19 namespace base { | 22 namespace base { |
| 20 namespace { | 23 namespace { |
| 21 | 24 |
| 22 typedef web::WebTest CRWJSPOSTRequestLoaderTest; | 25 typedef web::WebTest CRWJSPOSTRequestLoaderTest; |
| 23 | 26 |
| 24 // This script takes a JavaScript blob and converts it to a base64-encoded | 27 // This script takes a JavaScript blob and converts it to a base64-encoded |
| 25 // string asynchronously, then is sent to XHRSendHandler message handler. | 28 // string asynchronously, then is sent to XHRSendHandler message handler. |
| 26 NSString* const kBlobToBase64StringScript = | 29 NSString* const kBlobToBase64StringScript = |
| 27 @"var blobToBase64 = function(x) {" | 30 @"var blobToBase64 = function(x) {" |
| 28 " var reader = new window.FileReader();" | 31 " var reader = new window.FileReader();" |
| 29 " reader.readAsDataURL(x);" | 32 " reader.readAsDataURL(x);" |
| 30 " reader.onloadend = function() {" | 33 " reader.onloadend = function() {" |
| 31 " base64data = reader.result;" | 34 " base64data = reader.result;" |
| 32 " window.webkit.messageHandlers.XHRSendHandler.postMessage(base64data);" | 35 " window.webkit.messageHandlers.XHRSendHandler.postMessage(base64data);" |
| 33 " };" | 36 " };" |
| 34 "};"; | 37 "};"; |
| 35 | 38 |
| 36 // Tests that the POST request is correctly executed through XMLHttpRequest. | 39 // Tests that the POST request is correctly executed through XMLHttpRequest. |
| 37 // TODO(crbug.com/592034): This test is flaky on device. | 40 // TODO(crbug.com/592034): This test is flaky on device. |
| 38 #if TARGET_IPHONE_SIMULATOR | 41 #if TARGET_IPHONE_SIMULATOR |
| 39 #define MAYBE_LoadsCorrectHTML LoadsCorrectHTML | 42 #define MAYBE_LoadsCorrectHTML LoadsCorrectHTML |
| 40 #else | 43 #else |
| 41 #define MAYBE_LoadsCorrectHTML DISABLED_LoadsCorrectHTML | 44 #define MAYBE_LoadsCorrectHTML DISABLED_LoadsCorrectHTML |
| 42 #endif | 45 #endif |
| 46 |
| 43 TEST_F(CRWJSPOSTRequestLoaderTest, MAYBE_LoadsCorrectHTML) { | 47 TEST_F(CRWJSPOSTRequestLoaderTest, MAYBE_LoadsCorrectHTML) { |
| 44 // Set up necessary objects. | 48 // Set up necessary objects. |
| 45 scoped_nsobject<CRWJSPOSTRequestLoader> loader( | 49 CRWJSPOSTRequestLoader* loader = [[CRWJSPOSTRequestLoader alloc] init]; |
| 46 [[CRWJSPOSTRequestLoader alloc] init]); | |
| 47 WKWebView* web_view = web::BuildWKWebView(CGRectZero, GetBrowserState()); | 50 WKWebView* web_view = web::BuildWKWebView(CGRectZero, GetBrowserState()); |
| 48 WKUserContentController* contentController = | 51 WKUserContentController* contentController = |
| 49 web_view.configuration.userContentController; | 52 web_view.configuration.userContentController; |
| 50 scoped_nsobject<CRWWKScriptMessageRouter> messageRouter( | 53 CRWWKScriptMessageRouter* messageRouter = [[CRWWKScriptMessageRouter alloc] |
| 51 [[CRWWKScriptMessageRouter alloc] | 54 initWithUserContentController:contentController]; |
| 52 initWithUserContentController:contentController]); | |
| 53 | 55 |
| 54 // Override XMLHttpRequest.send() to call kBlobToBase64StringScript. | 56 // Override XMLHttpRequest.send() to call kBlobToBase64StringScript. |
| 55 __block BOOL overrideSuccessfull = NO; | 57 __block BOOL overrideSuccessfull = NO; |
| 56 NSString* JS = [kBlobToBase64StringScript stringByAppendingString:@";\ | 58 NSString* JS = [kBlobToBase64StringScript stringByAppendingString:@";\ |
| 57 XMLHttpRequest.prototype.send = function(x) { blobToBase64(x); };"]; | 59 XMLHttpRequest.prototype.send = function(x) { blobToBase64(x); };"]; |
| 58 [web_view evaluateJavaScript:JS | 60 [web_view evaluateJavaScript:JS |
| 59 completionHandler:^(id, NSError*) { | 61 completionHandler:^(id, NSError*) { |
| 60 overrideSuccessfull = YES; | 62 overrideSuccessfull = YES; |
| 61 }]; | 63 }]; |
| 62 base::test::ios::WaitUntilCondition(^bool { | 64 base::test::ios::WaitUntilCondition(^bool { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 return complete; | 100 return complete; |
| 99 }); | 101 }); |
| 100 | 102 |
| 101 // Clean up installed script handler. | 103 // Clean up installed script handler. |
| 102 [messageRouter removeScriptMessageHandlerForName:@"XHRSendHandler" | 104 [messageRouter removeScriptMessageHandlerForName:@"XHRSendHandler" |
| 103 webView:web_view]; | 105 webView:web_view]; |
| 104 } | 106 } |
| 105 | 107 |
| 106 } // namespace | 108 } // namespace |
| 107 } // namespace base | 109 } // namespace base |
| OLD | NEW |