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

Side by Side Diff: Tools/TestWebKitAPI/Tests/mac/WebViewDidCreateJavaScriptContext.mm

Issue 13602008: Remove non-chromium code from TestWebKitAPI (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 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
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #import "config.h"
27 #import "PlatformUtilities.h"
28 #import <JavaScriptCore/JSExport.h>
29 #import <JavaScriptCore/JSContext.h>
30 #import <WebKit/WebFrameLoadDelegatePrivate.h>
31 #import <wtf/RetainPtr.h>
32
33 #if JSC_OBJC_API_ENABLED
34
35 @class MyConsole;
36
37 static bool didFinishLoad = false;
38 static bool didCompleteTestSuccessfully = false;
39 static bool didCallWindowCallback = false;
40 static bool didFindMyCustomProperty = false;
41 static bool didInsertMyCustomProperty = true;
42
43 @protocol MyConsole<JSExport>
44 - (void)log:(NSString *)s;
45 - (void)printHelloWorld;
46 - (int)add:(int)a to:(int)b;
47 @end
48
49 @interface MyConsole : NSObject<MyConsole>
50 @end
51
52 @implementation MyConsole
53 - (void)log:(NSString *)s
54 {
55 NSLog(@"%@", s);
56 }
57
58 - (void)printHelloWorld
59 {
60 NSLog(@"Hello, World!");
61 }
62
63 - (int)add:(int)a to:(int)b
64 {
65 return a + b;
66 }
67 @end
68
69 @interface DidCreateJavaScriptContextFrameLoadDelegate : NSObject
70 @end
71
72 @implementation DidCreateJavaScriptContextFrameLoadDelegate
73
74 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
75 {
76 didFinishLoad = true;
77 }
78
79 - (void)webView:(WebView *)webView didCreateJavaScriptContext:(JSContext *)conte xt forFrame:(WebFrame *)frame
80 {
81 MyConsole *myConsole = [[MyConsole alloc] init];
82 context[@"myConsole"] = myConsole;
83 context.exceptionHandler = nil;
84 [myConsole release];
85
86 context[@"windowCallback"] = ^(JSValue *thisObject){
87 didCallWindowCallback = true;
88 };
89
90 context[@"didCompleteTestSuccessfully"] = ^{
91 didCompleteTestSuccessfully = true;
92 };
93
94 context[@"callMeBack"] = ^(JSValue *functionValue) {
95 [functionValue callWithArguments:[NSArray array]];
96 };
97
98 context[@"checkForMyCustomProperty"] = ^(JSValue *element) {
99 if ([element hasProperty:@"myCustomProperty"] && [[element valueForPrope rty:@"myCustomProperty"] toInt32] == 42)
100 didFindMyCustomProperty = true;
101 else
102 NSLog(@"ERROR: Did not find myCustomProperty.");
103 };
104
105 context[@"insertMyCustomProperty"] = ^(JSValue *element) {
106 JSValue *fortyTwo = [JSValue valueWithInt32:42 inContext:[JSContext curr entContext]];
107 [element setValue:fortyTwo forProperty:@"myCustomProperty"];
108 didInsertMyCustomProperty = true;
109 };
110 }
111
112 @end
113
114 namespace TestWebKitAPI {
115
116 TEST(WebKit1, DidCreateJavaScriptContextSanity1)
117 {
118 didFinishLoad = false;
119 @autoreleasepool {
120 RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMa keRect(0, 0, 120, 200) frameName:nil groupName:nil]);
121 RetainPtr<DidCreateJavaScriptContextFrameLoadDelegate> frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]);
122
123 webView.get().frameLoadDelegate = frameLoadDelegate.get();
124 WebFrame *mainFrame = webView.get().mainFrame;
125
126 NSString *bodyString =
127 @"<body> \
128 <script> \
129 myConsole.printHelloWorld(); \
130 myConsole.log(\"Loaded custom stuff.\"); \
131 myConsole.log(myConsole.addTo(40, 2)); \
132 didCompleteTestSuccessfully(); \
133 </script> \
134 </body>";
135 NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"];
136
137 [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL];
138 Util::run(&didCompleteTestSuccessfully);
139 }
140 }
141
142 TEST(WebKit1, DidCreateJavaScriptContextSanity2)
143 {
144 didCallWindowCallback = false;
145 @autoreleasepool {
146 RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMa keRect(0, 0, 120, 200) frameName:nil groupName:nil]);
147 RetainPtr<DidCreateJavaScriptContextFrameLoadDelegate> frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]);
148
149 webView.get().frameLoadDelegate = frameLoadDelegate.get();
150 WebFrame *mainFrame = webView.get().mainFrame;
151
152 NSString *bodyString =
153 @"<body> \
154 <script> \
155 setTimeout(windowCallback, 100); \
156 </script> \
157 </body>";
158 NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"];
159
160 [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL];
161 Util::run(&didCallWindowCallback);
162 }
163 }
164
165 TEST(WebKit1, DidCreateJavaScriptContextCallJSFunctionFromObjCCallbackTest)
166 {
167 @autoreleasepool {
168 RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMa keRect(0, 0, 120, 200) frameName:nil groupName:nil]);
169 RetainPtr<DidCreateJavaScriptContextFrameLoadDelegate> frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]);
170
171 webView.get().frameLoadDelegate = frameLoadDelegate.get();
172 WebFrame *mainFrame = webView.get().mainFrame;
173
174 NSString *bodyString =
175 @"<body> \
176 <script> \
177 callMeBack(function() { \
178 didCompleteTestSuccessfully(); \
179 }); \
180 </script> \
181 </body>";
182 NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"];
183
184 [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL];
185 Util::run(&didCompleteTestSuccessfully);
186 }
187 }
188
189 TEST(WebKit1, DidCreateJavaScriptContextAddCustomPropertiesFromJSTest)
190 {
191 didFindMyCustomProperty = false;
192 @autoreleasepool {
193 RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMa keRect(0, 0, 120, 200) frameName:nil groupName:nil]);
194 RetainPtr<DidCreateJavaScriptContextFrameLoadDelegate> frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]);
195
196 webView.get().frameLoadDelegate = frameLoadDelegate.get();
197 WebFrame *mainFrame = webView.get().mainFrame;
198
199 NSString *bodyString =
200 @"<body> \
201 <div id=\"test-div\"></div> \
202 <script> \
203 var testDiv = document.getElementById(\"test-div\"); \
204 testDiv.myCustomProperty = 42; \
205 checkForMyCustomProperty(testDiv); \
206 </script> \
207 </body>";
208 NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"];
209
210 [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL];
211 Util::run(&didFindMyCustomProperty);
212 }
213 }
214
215 TEST(WebKit1, DidCreateJavaScriptContextAddCustomPropertiesFromObjCTest)
216 {
217 didFindMyCustomProperty = false;
218 @autoreleasepool {
219 RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMa keRect(0, 0, 120, 200) frameName:nil groupName:nil]);
220 RetainPtr<DidCreateJavaScriptContextFrameLoadDelegate> frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]);
221
222 webView.get().frameLoadDelegate = frameLoadDelegate.get();
223 WebFrame *mainFrame = webView.get().mainFrame;
224
225 NSString *bodyString =
226 @"<body> \
227 <div id=\"test-div\"></div> \
228 <script> \
229 var testDiv = document.getElementById(\"test-div\"); \
230 insertMyCustomProperty(testDiv); \
231 if (testDiv.myCustomProperty === 42) { \
232 checkForMyCustomProperty(testDiv); \
233 } \
234 </script> \
235 </body>";
236 NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"];
237
238 [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL];
239 Util::run(&didFindMyCustomProperty);
240 }
241 }
242
243 TEST(WebKit1, DidCreateJavaScriptContextBackForwardCacheTest)
244 {
245 didInsertMyCustomProperty = false;
246 didFindMyCustomProperty = false;
247 didCompleteTestSuccessfully = false;
248 @autoreleasepool {
249 RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMa keRect(0, 0, 120, 200) frameName:nil groupName:nil]);
250 RetainPtr<DidCreateJavaScriptContextFrameLoadDelegate> frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]);
251
252 webView.get().frameLoadDelegate = frameLoadDelegate.get();
253 WebFrame *mainFrame = webView.get().mainFrame;
254
255 NSURL *url1 = [[NSBundle mainBundle] URLForResource:@"JSContextBackForwa rdCache1"
256 withExtension:@"html"
257 subdirectory:@"TestWebKitAPI.reso urces"];
258 [mainFrame loadRequest:[NSURLRequest requestWithURL:url1]];
259 Util::run(&didInsertMyCustomProperty);
260
261 NSURL *url2 = [[NSBundle mainBundle] URLForResource:@"JSContextBackForwa rdCache2"
262 withExtension:@"html"
263 subdirectory:@"TestWebKitAPI.reso urces"];
264 [mainFrame loadRequest:[NSURLRequest requestWithURL:url2]];
265 Util::run(&didCompleteTestSuccessfully);
266
267 didCompleteTestSuccessfully = false;
268 [[mainFrame javaScriptContext] evaluateScript:
269 @"var testDiv = document.getElementById(\"test-div\"); \
270 if (!testDiv.myCustomProperty) { \
271 didCompleteTestSuccessfully(); \
272 }"];
273 EXPECT_TRUE(didCompleteTestSuccessfully);
274
275 if ([webView.get() goBack]) {
276 [[mainFrame javaScriptContext] evaluateScript:
277 @"var testDiv = document.getElementById(\"test-div\"); \
278 checkForMyCustomProperty(testDiv);"];
279 EXPECT_TRUE(didFindMyCustomProperty);
280 } else
281 EXPECT_TRUE(false);
282 }
283 }
284
285 } // namespace TestWebKitAPI
286
287 #endif // ENABLE(JSC_OBJC_API)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698