/* * Copyright (C) 2013 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #import "config.h" #import "PlatformUtilities.h" #import #import #import #import #import #if JSC_OBJC_API_ENABLED @class MyConsole; static bool didFinishLoad = false; static bool didCompleteTestSuccessfully = false; static bool didCallWindowCallback = false; static bool didFindMyCustomProperty = false; static bool didInsertMyCustomProperty = true; static bool didReportException = false; @protocol MyConsole - (void)log:(NSString *)s; - (void)printHelloWorld; - (int)add:(int)a to:(int)b; @end @interface MyConsole : NSObject @end @implementation MyConsole - (void)log:(NSString *)s { NSLog(@"%@", s); } - (void)printHelloWorld { NSLog(@"Hello, World!"); } - (int)add:(int)a to:(int)b { return a + b; } @end @interface DidCreateJavaScriptContextFrameLoadDelegate : NSObject @end @implementation DidCreateJavaScriptContextFrameLoadDelegate - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { didFinishLoad = true; } - (void)webView:(WebView *)webView didCreateJavaScriptContext:(JSContext *)context forFrame:(WebFrame *)frame { MyConsole *myConsole = [[MyConsole alloc] init]; context[@"myConsole"] = myConsole; context.exceptionHandler = nil; [myConsole release]; context[@"windowCallback"] = ^(JSValue *thisObject){ didCallWindowCallback = true; }; context[@"didCompleteTestSuccessfully"] = ^{ didCompleteTestSuccessfully = true; }; context[@"callMeBack"] = ^(JSValue *functionValue) { [functionValue callWithArguments:[NSArray array]]; }; context[@"checkForMyCustomProperty"] = ^(JSValue *element) { if ([element hasProperty:@"myCustomProperty"] && [[element valueForProperty:@"myCustomProperty"] toInt32] == 42) didFindMyCustomProperty = true; else NSLog(@"ERROR: Did not find myCustomProperty."); }; context[@"insertMyCustomProperty"] = ^(JSValue *element) { JSValue *fortyTwo = [JSValue valueWithInt32:42 inContext:[JSContext currentContext]]; [element setValue:fortyTwo forProperty:@"myCustomProperty"]; didInsertMyCustomProperty = true; }; context[@"testReportException"] = ^{ JSContext* context = [JSContext currentContext]; [context evaluateScript:@"function doThrow() { throw 'TestError'; }"]; JSGlobalContextRef globalContext = [context JSGlobalContextRef]; JSObjectRef globalObject = JSContextGetGlobalObject(globalContext); JSStringRef jsString = JSStringCreateWithUTF8CString("doThrow"); JSValueRef function = JSObjectGetProperty(globalContext, globalObject, jsString, nullptr); JSStringRelease(jsString); const JSValueRef arguments[] = { }; JSValueRef exception = 0; JSObjectCallAsFunction(globalContext, (JSObjectRef)function, globalObject, 0, arguments, &exception); if (exception) { [WebView _reportException:exception inContext:globalContext]; didReportException = true; } }; } @end namespace TestWebKitAPI { TEST(WebKit1, DidCreateJavaScriptContextSanity1) { didFinishLoad = false; @autoreleasepool { RetainPtr webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]); RetainPtr frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]); webView.get().frameLoadDelegate = frameLoadDelegate.get(); WebFrame *mainFrame = webView.get().mainFrame; NSString *bodyString = @" \ \ "; NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"]; [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL]; Util::run(&didCompleteTestSuccessfully); } } TEST(WebKit1, DidCreateJavaScriptContextSanity2) { didCallWindowCallback = false; @autoreleasepool { RetainPtr webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]); RetainPtr frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]); webView.get().frameLoadDelegate = frameLoadDelegate.get(); WebFrame *mainFrame = webView.get().mainFrame; NSString *bodyString = @" \ \ "; NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"]; [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL]; Util::run(&didCallWindowCallback); } } TEST(WebKit1, DidCreateJavaScriptContextCallJSFunctionFromObjCCallbackTest) { @autoreleasepool { RetainPtr webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]); RetainPtr frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]); webView.get().frameLoadDelegate = frameLoadDelegate.get(); WebFrame *mainFrame = webView.get().mainFrame; NSString *bodyString = @" \ \ "; NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"]; [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL]; Util::run(&didCompleteTestSuccessfully); } } TEST(WebKit1, DidCreateJavaScriptContextAddCustomPropertiesFromJSTest) { didFindMyCustomProperty = false; @autoreleasepool { RetainPtr webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]); RetainPtr frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]); webView.get().frameLoadDelegate = frameLoadDelegate.get(); WebFrame *mainFrame = webView.get().mainFrame; NSString *bodyString = @" \
\ \ "; NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"]; [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL]; Util::run(&didFindMyCustomProperty); } } TEST(WebKit1, DidCreateJavaScriptContextAddCustomPropertiesFromObjCTest) { didFindMyCustomProperty = false; @autoreleasepool { RetainPtr webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]); RetainPtr frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]); webView.get().frameLoadDelegate = frameLoadDelegate.get(); WebFrame *mainFrame = webView.get().mainFrame; NSString *bodyString = @" \
\ \ "; NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"]; [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL]; Util::run(&didFindMyCustomProperty); } } TEST(WebKit1, DidCreateJavaScriptContextBackForwardCacheTest) { didInsertMyCustomProperty = false; didFindMyCustomProperty = false; didCompleteTestSuccessfully = false; @autoreleasepool { RetainPtr webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]); RetainPtr frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]); [[webView.get() preferences] setCacheModel:WebCacheModelDocumentBrowser]; webView.get().frameLoadDelegate = frameLoadDelegate.get(); WebFrame *mainFrame = webView.get().mainFrame; NSURL *url1 = [[NSBundle mainBundle] URLForResource:@"JSContextBackForwardCache1" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; [mainFrame loadRequest:[NSURLRequest requestWithURL:url1]]; Util::run(&didInsertMyCustomProperty); NSURL *url2 = [[NSBundle mainBundle] URLForResource:@"JSContextBackForwardCache2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]; [mainFrame loadRequest:[NSURLRequest requestWithURL:url2]]; Util::run(&didCompleteTestSuccessfully); didCompleteTestSuccessfully = false; [[mainFrame javaScriptContext] evaluateScript: @"var testDiv = document.getElementById(\"test-div\"); \ if (!testDiv.myCustomProperty) { \ didCompleteTestSuccessfully(); \ }"]; EXPECT_TRUE(didCompleteTestSuccessfully); if ([webView.get() goBack]) { [[mainFrame javaScriptContext] evaluateScript: @"var testDiv = document.getElementById(\"test-div\"); \ checkForMyCustomProperty(testDiv);"]; EXPECT_TRUE(didFindMyCustomProperty); } else EXPECT_TRUE(false); } } TEST(WebKit1, ReportExceptionTest) { didReportException = false; @autoreleasepool { RetainPtr webView = adoptNS([[WebView alloc] initWithFrame:NSMakeRect(0, 0, 120, 200) frameName:nil groupName:nil]); RetainPtr frameLoadDelegate = adoptNS([[DidCreateJavaScriptContextFrameLoadDelegate alloc] init]); webView.get().frameLoadDelegate = frameLoadDelegate.get(); WebFrame *mainFrame = webView.get().mainFrame; NSString *bodyString = @" \ \ "; NSURL *aboutBlankURL = [NSURL URLWithString:@"about:blank"]; [mainFrame loadHTMLString:bodyString baseURL:aboutBlankURL]; Util::run(&didReportException); } } } // namespace TestWebKitAPI #endif // ENABLE(JSC_OBJC_API)