OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import "ios/web/web_state/js/crw_js_post_request_loader.h" | |
6 | |
7 #import <WebKit/WebKit.h> | |
8 | |
9 #import "base/strings/sys_string_conversions.h" | |
Eugene But (OOO till 7-30)
2015/12/02 17:06:15
s/import/include
stkhapugin
2015/12/03 15:43:02
Done.
| |
10 #import "base/test/ios/wait_util.h" | |
11 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" | |
12 #import "testing/gtest_mac.h" | |
13 #include "third_party/ocmock/OCMock/OCMock.h" | |
14 | |
15 namespace { | |
16 | |
17 typedef void (^SuccessCallback)(); | |
18 typedef void (^ErrorCallback)(NSError*); | |
19 | |
20 // The expected HTML to be load in WKWebView to execute a post request from | |
21 // LoadsCorrectHTML test. | |
22 std::string expectedHTML = | |
Eugene But (OOO till 7-30)
2015/12/02 17:06:15
FYI: Variables of class type with static storage d
stkhapugin
2015/12/03 15:43:02
Done.
| |
23 R"delimeter(<html><script>var __crPostRequestHack={runPostRequest:function(k ,l,m,n){var p=function(b,d,c,g){var e=new XMLHttpRequest;e.open("POST",b,!1);for (var a in d)d.hasOwnProperty(a)&&e.setRequestHeader(a,d[a]);b=atob(c);g=g||"";d= [];for(c=0;c<b.length;c+=512){a=b.slice(c,c+512);for(var h=Array(a.length),f=0;f <a.length;f++)h[f]=a.charCodeAt(f);a=new Uint8Array(h);d.push(a)}b=new Blob(d,{t ype:g});e.send(b);if(200!=e.status)throw e.status;return e.responseText};documen t.open();try{document.write(p(k,l,m,n)),window.webkit.messageHandlers.POSTSucces s.postMessage("")}catch(b){window.webkit.messageHandlers.POSTErrorHandler.postMe ssage(b)}document.close()}}; | |
Eugene But (OOO till 7-30)
2015/12/02 17:06:15
This is too fragile, and will be breaking very oft
stkhapugin
2015/12/03 15:43:02
Done.
| |
24 __crPostRequestHack.runPostRequest("http://google.com", {}, "", "")</script></ht ml>)delimeter"; | |
25 | |
26 // Tests that the POST request is correctly executed by loading a HTML string. | |
27 TEST(CRWJSPOSTRequestLoaderTest, LoadsCorrectHTML) { | |
28 CRWJSPOSTRequestLoader* loader = | |
29 [[[CRWJSPOSTRequestLoader alloc] init] autorelease]; | |
30 id mockWebView = [OCMockObject mockForClass:[WKWebView class]]; | |
31 NSURL* url = [NSURL URLWithString:@"http://google.com"]; | |
32 NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; | |
33 request.HTTPMethod = @"POST"; | |
34 id mockMessageRouter = | |
35 [OCMockObject niceMockForClass:[CRWWKScriptMessageRouter class]]; | |
36 NSString* expectedHTMLString = base::SysUTF8ToNSString(expectedHTML); | |
37 [(WKWebView*)[mockWebView expect] loadHTMLString:expectedHTMLString | |
38 baseURL:url]; | |
39 | |
40 [loader loadPOSTRequest:request | |
41 inWebView:mockWebView | |
42 messageRouter:mockMessageRouter | |
43 errorBlock:^(NSError* error) { | |
44 } | |
45 successBlock:^{ | |
46 }]; | |
47 | |
48 [mockWebView verify]; | |
49 } | |
50 | |
51 // Tests that success and error message handlers are installed and removed. | |
52 TEST(CRWJSPOSTRequestLoaderTest, installsCallbacks) { | |
Eugene But (OOO till 7-30)
2015/12/02 17:06:16
Installing and removing script message router is t
Eugene But (OOO till 7-30)
2015/12/02 17:06:16
s/installsCallbacks/InstallsCallbacks
stkhapugin
2015/12/03 15:43:02
Acknowledged.
stkhapugin
2015/12/03 15:43:02
OK, I'll remove this test for simplicity.
| |
53 CRWJSPOSTRequestLoader* loader = | |
54 [[[CRWJSPOSTRequestLoader alloc] init] autorelease]; | |
55 id mockWebView = [OCMockObject niceMockForClass:[WKWebView class]]; | |
56 NSURL* url = [NSURL URLWithString:@"http://google.com"]; | |
57 NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; | |
58 request.HTTPMethod = @"POST"; | |
59 id mockMessageRouter = | |
60 [OCMockObject mockForClass:[CRWWKScriptMessageRouter class]]; | |
61 | |
62 __block BOOL successBlockCalled = NO; | |
63 SuccessCallback successHandler = ^{ | |
64 EXPECT_FALSE(successBlockCalled); | |
65 successBlockCalled = YES; | |
66 }; | |
67 __block BOOL errorBlockCalled = NO; | |
68 ErrorCallback errorHandler = ^(NSError*) { | |
69 EXPECT_FALSE(errorBlockCalled); | |
70 errorBlockCalled = YES; | |
71 }; | |
72 | |
73 // Checks that successHandler is called and that handlers are removed. | |
74 id successArgChecker = | |
75 [OCMArg checkWithBlock:^BOOL(SuccessCallback actualSuccessHandler) { | |
76 [[mockMessageRouter expect] | |
77 removeScriptMessageHandlerForName:@"POSTErrorHandler" | |
78 webView:mockWebView]; | |
79 [[mockMessageRouter expect] | |
80 removeScriptMessageHandlerForName:@"POSTSuccess" | |
81 webView:mockWebView]; | |
82 actualSuccessHandler(); | |
83 base::test::ios::WaitUntilCondition(^{ | |
84 return successBlockCalled; | |
85 }); | |
86 return successBlockCalled; | |
87 }]; | |
88 | |
89 // Checks that errorHandler is called and that handlers are removed. | |
90 id errorArgChecker = | |
91 [OCMArg checkWithBlock:^BOOL(ErrorCallback actualErrorHandler) { | |
92 [[mockMessageRouter expect] | |
93 removeScriptMessageHandlerForName:@"POSTErrorHandler" | |
94 webView:mockWebView]; | |
95 [[mockMessageRouter expect] | |
96 removeScriptMessageHandlerForName:@"POSTSuccess" | |
97 webView:mockWebView]; | |
98 | |
99 actualErrorHandler(nil); | |
100 base::test::ios::WaitUntilCondition(^{ | |
101 return errorBlockCalled; | |
102 }); | |
103 return errorBlockCalled; | |
104 }]; | |
105 | |
106 [[mockMessageRouter expect] setScriptMessageHandler:successArgChecker | |
107 name:@"POSTSuccess" | |
108 webView:mockWebView]; | |
109 [[mockMessageRouter expect] setScriptMessageHandler:errorArgChecker | |
110 name:@"POSTErrorHandler" | |
111 webView:mockWebView]; | |
112 | |
113 [loader loadPOSTRequest:request | |
114 inWebView:mockWebView | |
115 messageRouter:mockMessageRouter | |
116 errorBlock:errorHandler | |
117 successBlock:successHandler]; | |
118 | |
119 [mockMessageRouter verify]; | |
120 } | |
121 | |
122 } // namespace | |
OLD | NEW |