OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 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 <WebKit/WebFramePrivate.h> | |
29 #import <WebKit/WebScriptWorld.h> | |
30 #import <JavaScriptCore/JSContextRef.h> | |
31 #import <JavaScriptCore/JSRetainPtr.h> | |
32 #import <JavaScriptCore/JSStringRef.h> | |
33 #import <JavaScriptCore/JSValueRef.h> | |
34 #import <wtf/RetainPtr.h> | |
35 | |
36 @interface JSWrapperForNodeFrameLoadDelegate : NSObject { | |
37 } | |
38 @end | |
39 | |
40 static bool didFinishLoad; | |
41 | |
42 @implementation JSWrapperForNodeFrameLoadDelegate | |
43 | |
44 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame | |
45 { | |
46 didFinishLoad = true; | |
47 } | |
48 | |
49 @end | |
50 | |
51 namespace TestWebKitAPI { | |
52 | |
53 TEST(WebKit1, JSWrapperForNode) | |
54 { | |
55 RetainPtr<WebView> webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRe
ct(0, 0, 120, 200) frameName:nil groupName:nil]); | |
56 RetainPtr<JSWrapperForNodeFrameLoadDelegate> frameLoadDelegate = adoptNS([[J
SWrapperForNodeFrameLoadDelegate alloc] init]); | |
57 | |
58 webView.get().frameLoadDelegate = frameLoadDelegate.get(); | |
59 WebFrame *mainFrame = webView.get().mainFrame; | |
60 [mainFrame loadHTMLString:@"<div id=\"target\"</div>" baseURL:[NSURL URLWith
String:@"about:blank"]]; | |
61 Util::run(&didFinishLoad); | |
62 DOMDocument *document = webView.get().mainFrameDocument; | |
63 DOMNode *target = [document getElementById:@"target"]; // This script object
is in standard world. | |
64 | |
65 // In an isolated script world, add a new property to the target node. | |
66 NSString *isolatedScriptString = @"var target = document.getElementById(\"ta
rget\"); target.isolatedProperty = true;"; | |
67 WebScriptWorld *isolatedWorld = [WebScriptWorld world]; | |
68 JSGlobalContextRef isolatedCtx = [mainFrame _globalContextForScriptWorld:iso
latedWorld]; | |
69 [mainFrame _stringByEvaluatingJavaScriptFromString:isolatedScriptString with
GlobalObject:JSContextGetGlobalObject(isolatedCtx) inScriptWorld:isolatedWorld]; | |
70 JSValueRef isolatedNodeJSValue = [mainFrame jsWrapperForNode:target inScript
World:isolatedWorld]; | |
71 ASSERT_TRUE(JSValueIsObject(isolatedCtx, isolatedNodeJSValue)); | |
72 JSObjectRef isolatedNodeJSObject = JSValueToObject(isolatedCtx, isolatedNode
JSValue, 0); | |
73 | |
74 // In the standard script world, add a different property to the target node | |
75 NSString *normalScriptString = @"var target = document.getElementById(\"targ
et\"); target.normalProperty = true;"; | |
76 WebScriptWorld *normalWorld = [WebScriptWorld standardWorld]; | |
77 JSGlobalContextRef normalCtx = [mainFrame _globalContextForScriptWorld:norma
lWorld]; | |
78 [mainFrame _stringByEvaluatingJavaScriptFromString:normalScriptString withGl
obalObject:JSContextGetGlobalObject(normalCtx) inScriptWorld:normalWorld]; | |
79 JSValueRef normalNodeJSValue = [mainFrame jsWrapperForNode:target inScriptWo
rld:normalWorld]; | |
80 ASSERT_TRUE(JSValueIsObject(normalCtx, normalNodeJSValue)); | |
81 JSObjectRef normalNodeJSObject = JSValueToObject(normalCtx, normalNodeJSValu
e, 0); | |
82 | |
83 JSRetainPtr<JSStringRef> isolatedPropertyJSString = JSStringCreateWithUTF8CS
tring("isolatedProperty"); | |
84 // Test for successful retrieval of the first property in the isolated scrip
t world | |
85 EXPECT_TRUE(JSValueIsBoolean(isolatedCtx, JSObjectGetProperty(isolatedCtx, i
solatedNodeJSObject, isolatedPropertyJSString.get(), 0))); | |
86 // Test for failed retrieval of the first property in the standard script wo
rld | |
87 EXPECT_TRUE(JSValueIsUndefined(normalCtx, JSObjectGetProperty(normalCtx, nor
malNodeJSObject, isolatedPropertyJSString.get(), 0))); | |
88 | |
89 JSRetainPtr<JSStringRef> normalPropertyJSString = JSStringCreateWithUTF8CStr
ing("normalProperty"); | |
90 // Test for successful retrieval of the second property in the standard scri
pt world | |
91 EXPECT_TRUE(JSValueIsBoolean(normalCtx, JSObjectGetProperty(normalCtx, norma
lNodeJSObject, normalPropertyJSString.get(), 0))); | |
92 // Test for failed retrieval of the second property in the isolated script w
orld | |
93 EXPECT_TRUE(JSValueIsUndefined(isolatedCtx, JSObjectGetProperty(isolatedCtx,
isolatedNodeJSObject, normalPropertyJSString.get(), 0))); | |
94 } | |
95 | |
96 } // namespace TestWebKitAPI | |
OLD | NEW |