| 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 "Test.h" | |
| 28 | |
| 29 #import "PlatformUtilities.h" | |
| 30 #import "TestBrowsingContextLoadDelegate.h" | |
| 31 #import <JavaScriptCore/JSRetainPtr.h> | |
| 32 #import <JavaScriptCore/JavaScriptCore.h> | |
| 33 #import <WebKit2/WKSerializedScriptValue.h> | |
| 34 #import <WebKit2/WKViewPrivate.h> | |
| 35 #import <WebKit2/WebKit2.h> | |
| 36 | |
| 37 | |
| 38 static bool testFinished = false; | |
| 39 static NSString *htmlString = @"<body style='background-color: red'>"; | |
| 40 static NSString *userStyleSheet = @"body { background-color: green !important; }
"; | |
| 41 static const char* backgroundColorScript = "window.getComputedStyle(document.bod
y, null).getPropertyValue('background-color')"; | |
| 42 static const char* greenInRGB = "rgb(0, 128, 0)"; | |
| 43 static const char* redInRGB = "rgb(255, 0, 0)"; | |
| 44 static const char* userScriptTestProperty = "window._userScriptInstalled"; | |
| 45 | |
| 46 namespace { | |
| 47 class WebKit2UserContentTest : public ::testing::Test { | |
| 48 public: | |
| 49 WKProcessGroup *processGroup; | |
| 50 WKBrowsingContextGroup *browsingContextGroup; | |
| 51 | |
| 52 WebKit2UserContentTest() | |
| 53 : processGroup(nil) | |
| 54 , browsingContextGroup(nil) | |
| 55 { | |
| 56 } | |
| 57 | |
| 58 virtual void SetUp() | |
| 59 { | |
| 60 processGroup = [[WKProcessGroup alloc] init]; | |
| 61 browsingContextGroup = [[WKBrowsingContextGroup alloc] initWithIdent
ifier:@"UserContentIdentifier"]; | |
| 62 } | |
| 63 | |
| 64 virtual void TearDown() | |
| 65 { | |
| 66 [browsingContextGroup release]; | |
| 67 [processGroup release]; | |
| 68 } | |
| 69 }; | |
| 70 } // namespace | |
| 71 | |
| 72 static void expectScriptValueIsString(WKSerializedScriptValueRef serializedScrip
tValue, const char* expectedValue) | |
| 73 { | |
| 74 JSGlobalContextRef scriptContext = JSGlobalContextCreate(0); | |
| 75 | |
| 76 JSValueRef scriptValue = WKSerializedScriptValueDeserialize(serializedScript
Value, scriptContext, 0); | |
| 77 EXPECT_TRUE(JSValueIsString(scriptContext, scriptValue)); | |
| 78 | |
| 79 JSRetainPtr<JSStringRef> scriptString(Adopt, JSValueToStringCopy(scriptConte
xt, scriptValue, 0)); | |
| 80 EXPECT_TRUE(JSStringIsEqualToUTF8CString(scriptString.get(), expectedValue))
; | |
| 81 | |
| 82 JSGlobalContextRelease(scriptContext); | |
| 83 } | |
| 84 | |
| 85 static void expectScriptValueIsBoolean(WKSerializedScriptValueRef serializedScri
ptValue, bool expectedValue) | |
| 86 { | |
| 87 JSGlobalContextRef scriptContext = JSGlobalContextCreate(0); | |
| 88 | |
| 89 JSValueRef scriptValue = WKSerializedScriptValueDeserialize(serializedScript
Value, scriptContext, 0); | |
| 90 EXPECT_TRUE(JSValueIsBoolean(scriptContext, scriptValue)); | |
| 91 EXPECT_EQ(JSValueToBoolean(scriptContext, scriptValue), expectedValue); | |
| 92 | |
| 93 JSGlobalContextRelease(scriptContext); | |
| 94 } | |
| 95 | |
| 96 static void expectScriptValueIsUndefined(WKSerializedScriptValueRef serializedSc
riptValue) | |
| 97 { | |
| 98 JSGlobalContextRef scriptContext = JSGlobalContextCreate(0); | |
| 99 | |
| 100 JSValueRef scriptValue = WKSerializedScriptValueDeserialize(serializedScript
Value, scriptContext, 0); | |
| 101 EXPECT_TRUE(JSValueIsUndefined(scriptContext, scriptValue)); | |
| 102 | |
| 103 JSGlobalContextRelease(scriptContext); | |
| 104 } | |
| 105 | |
| 106 TEST_F(WebKit2UserContentTest, AddUserStyleSheetBeforeCreatingView) | |
| 107 { | |
| 108 testFinished = false; | |
| 109 [browsingContextGroup addUserStyleSheet:userStyleSheet baseURL:nil whitelist
edURLPatterns:nil blacklistedURLPatterns:nil mainFrameOnly:YES]; | |
| 110 | |
| 111 WKView *wkView = [[WKView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) pr
ocessGroup:processGroup browsingContextGroup:browsingContextGroup]; | |
| 112 WKStringRef backgroundColorQuery = WKStringCreateWithUTF8CString(backgroundC
olorScript); | |
| 113 wkView.browsingContextController.loadDelegate = [[TestBrowsingContextLoadDel
egate alloc] initWithBlockToRunOnLoad:^(WKBrowsingContextController *sender) { | |
| 114 WKPageRunJavaScriptInMainFrame_b(wkView.pageRef, backgroundColorQuery, ^
(WKSerializedScriptValueRef serializedScriptValue, WKErrorRef error) { | |
| 115 expectScriptValueIsString(serializedScriptValue, greenInRGB); | |
| 116 testFinished = true; | |
| 117 WKRelease(backgroundColorQuery); | |
| 118 }); | |
| 119 }]; | |
| 120 | |
| 121 [wkView.browsingContextController loadHTMLString:htmlString baseURL:nil]; | |
| 122 | |
| 123 TestWebKitAPI::Util::run(&testFinished); | |
| 124 } | |
| 125 | |
| 126 TEST_F(WebKit2UserContentTest, AddUserStyleSheetAfterCreatingView) | |
| 127 { | |
| 128 testFinished = false; | |
| 129 | |
| 130 WKView *wkView = [[WKView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) pr
ocessGroup:processGroup browsingContextGroup:browsingContextGroup]; | |
| 131 WKStringRef backgroundColorQuery = WKStringCreateWithUTF8CString(backgroundC
olorScript); | |
| 132 wkView.browsingContextController.loadDelegate = [[TestBrowsingContextLoadDel
egate alloc] initWithBlockToRunOnLoad:^(WKBrowsingContextController *sender) { | |
| 133 WKPageRunJavaScriptInMainFrame_b(wkView.pageRef, backgroundColorQuery, ^
(WKSerializedScriptValueRef serializedScriptValue, WKErrorRef error) { | |
| 134 expectScriptValueIsString(serializedScriptValue, greenInRGB); | |
| 135 testFinished = true; | |
| 136 WKRelease(backgroundColorQuery); | |
| 137 }); | |
| 138 }]; | |
| 139 | |
| 140 [browsingContextGroup addUserStyleSheet:userStyleSheet baseURL:nil whitelist
edURLPatterns:nil blacklistedURLPatterns:nil mainFrameOnly:YES]; | |
| 141 | |
| 142 [wkView.browsingContextController loadHTMLString:htmlString baseURL:nil]; | |
| 143 | |
| 144 TestWebKitAPI::Util::run(&testFinished); | |
| 145 } | |
| 146 | |
| 147 TEST_F(WebKit2UserContentTest, RemoveAllUserStyleSheets) | |
| 148 { | |
| 149 testFinished = false; | |
| 150 [browsingContextGroup addUserStyleSheet:userStyleSheet baseURL:nil whitelist
edURLPatterns:nil blacklistedURLPatterns:nil mainFrameOnly:YES]; | |
| 151 | |
| 152 WKView *wkView = [[WKView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) pr
ocessGroup:processGroup browsingContextGroup:browsingContextGroup]; | |
| 153 WKStringRef backgroundColorQuery = WKStringCreateWithUTF8CString(backgroundC
olorScript); | |
| 154 wkView.browsingContextController.loadDelegate = [[TestBrowsingContextLoadDel
egate alloc] initWithBlockToRunOnLoad:^(WKBrowsingContextController *sender) { | |
| 155 WKPageRunJavaScriptInMainFrame_b(wkView.pageRef, backgroundColorQuery, ^
(WKSerializedScriptValueRef serializedScriptValue, WKErrorRef error) { | |
| 156 expectScriptValueIsString(serializedScriptValue, redInRGB); | |
| 157 testFinished = true; | |
| 158 WKRelease(backgroundColorQuery); | |
| 159 }); | |
| 160 }]; | |
| 161 | |
| 162 [browsingContextGroup removeAllUserStyleSheets]; | |
| 163 | |
| 164 [wkView.browsingContextController loadHTMLString:htmlString baseURL:nil]; | |
| 165 | |
| 166 TestWebKitAPI::Util::run(&testFinished); | |
| 167 } | |
| 168 | |
| 169 TEST_F(WebKit2UserContentTest, AddUserScriptBeforeCreatingView) | |
| 170 { | |
| 171 testFinished = false; | |
| 172 [browsingContextGroup addUserScript:[NSString stringWithFormat:@"%s = true;"
, userScriptTestProperty] baseURL:nil whitelistedURLPatterns:nil blacklistedURLP
atterns:nil injectionTime:kWKInjectAtDocumentStart mainFrameOnly:YES]; | |
| 173 | |
| 174 WKView *wkView = [[WKView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) pr
ocessGroup:processGroup browsingContextGroup:browsingContextGroup]; | |
| 175 WKStringRef userScriptTestPropertyString = WKStringCreateWithUTF8CString(use
rScriptTestProperty); | |
| 176 wkView.browsingContextController.loadDelegate = [[TestBrowsingContextLoadDel
egate alloc] initWithBlockToRunOnLoad:^(WKBrowsingContextController *sender) { | |
| 177 WKPageRunJavaScriptInMainFrame_b(wkView.pageRef, userScriptTestPropertyS
tring, ^(WKSerializedScriptValueRef serializedScriptValue, WKErrorRef error) { | |
| 178 expectScriptValueIsBoolean(serializedScriptValue, true); | |
| 179 testFinished = true; | |
| 180 WKRelease(userScriptTestPropertyString); | |
| 181 }); | |
| 182 }]; | |
| 183 | |
| 184 [wkView.browsingContextController loadHTMLString:@"" baseURL:nil]; | |
| 185 | |
| 186 TestWebKitAPI::Util::run(&testFinished); | |
| 187 } | |
| 188 | |
| 189 TEST_F(WebKit2UserContentTest, AddUserScriptAfterCreatingView) | |
| 190 { | |
| 191 testFinished = false; | |
| 192 | |
| 193 WKView *wkView = [[WKView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) pr
ocessGroup:processGroup browsingContextGroup:browsingContextGroup]; | |
| 194 WKStringRef userScriptTestPropertyString = WKStringCreateWithUTF8CString(use
rScriptTestProperty); | |
| 195 wkView.browsingContextController.loadDelegate = [[TestBrowsingContextLoadDel
egate alloc] initWithBlockToRunOnLoad:^(WKBrowsingContextController *sender) { | |
| 196 WKPageRunJavaScriptInMainFrame_b(wkView.pageRef, userScriptTestPropertyS
tring, ^(WKSerializedScriptValueRef serializedScriptValue, WKErrorRef error) { | |
| 197 expectScriptValueIsBoolean(serializedScriptValue, true); | |
| 198 testFinished = true; | |
| 199 WKRelease(userScriptTestPropertyString); | |
| 200 }); | |
| 201 }]; | |
| 202 | |
| 203 [browsingContextGroup addUserScript:[NSString stringWithFormat:@"%s = true;"
, userScriptTestProperty] baseURL:nil whitelistedURLPatterns:nil blacklistedURLP
atterns:nil injectionTime:kWKInjectAtDocumentStart mainFrameOnly:YES]; | |
| 204 | |
| 205 [wkView.browsingContextController loadHTMLString:@"" baseURL:nil]; | |
| 206 | |
| 207 TestWebKitAPI::Util::run(&testFinished); | |
| 208 } | |
| 209 | |
| 210 TEST_F(WebKit2UserContentTest, RemoveAllUserScripts) | |
| 211 { | |
| 212 testFinished = false; | |
| 213 [browsingContextGroup addUserScript:[NSString stringWithFormat:@"%s = true;"
, userScriptTestProperty] baseURL:nil whitelistedURLPatterns:nil blacklistedURLP
atterns:nil injectionTime:kWKInjectAtDocumentStart mainFrameOnly:YES]; | |
| 214 | |
| 215 WKView *wkView = [[WKView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) pr
ocessGroup:processGroup browsingContextGroup:browsingContextGroup]; | |
| 216 WKStringRef userScriptTestPropertyString = WKStringCreateWithUTF8CString(use
rScriptTestProperty); | |
| 217 wkView.browsingContextController.loadDelegate = [[TestBrowsingContextLoadDel
egate alloc] initWithBlockToRunOnLoad:^(WKBrowsingContextController *sender) { | |
| 218 WKPageRunJavaScriptInMainFrame_b(wkView.pageRef, userScriptTestPropertyS
tring, ^(WKSerializedScriptValueRef serializedScriptValue, WKErrorRef error) { | |
| 219 expectScriptValueIsUndefined(serializedScriptValue); | |
| 220 testFinished = true; | |
| 221 WKRelease(userScriptTestPropertyString); | |
| 222 }); | |
| 223 }]; | |
| 224 | |
| 225 [browsingContextGroup removeAllUserScripts]; | |
| 226 | |
| 227 [wkView.browsingContextController loadHTMLString:htmlString baseURL:nil]; | |
| 228 | |
| 229 TestWebKitAPI::Util::run(&testFinished); | |
| 230 } | |
| OLD | NEW |