OLD | NEW |
| (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 #include "config.h" | |
27 #include "PlatformUtilities.h" | |
28 #include "PlatformWebView.h" | |
29 #include "Test.h" | |
30 #include <JavaScriptCore/JSContextRef.h> | |
31 #include <WebKit2/WKRetainPtr.h> | |
32 #include <WebKit2/WKSerializedScriptValue.h> | |
33 | |
34 namespace TestWebKitAPI { | |
35 | |
36 static bool testDone; | |
37 | |
38 static void didRunStep1StateChangeVisibleToHidden(WKSerializedScriptValueRef, WK
ErrorRef, void*); | |
39 static void didRunStep2StateChangeHiddenToPrerender(WKSerializedScriptValueRef,
WKErrorRef, void*); | |
40 static void didRunStep3StateChangePrerenderToPreview(WKSerializedScriptValueRef,
WKErrorRef, void*); | |
41 static void didRunStep4InStatePreview(WKSerializedScriptValueRef, WKErrorRef, vo
id*); | |
42 | |
43 static void setPageVisibilityStateWithEvalContinuation(PlatformWebView* webView,
WKPageVisibilityState visibilityState, WKPageRunJavaScriptFunction callback) | |
44 { | |
45 WKPageSetVisibilityState(webView->page(), visibilityState, false); | |
46 WKRetainPtr<WKStringRef> javaScriptString(AdoptWK, WKStringCreateWithUTF8CSt
ring("document.webkitVisibilityState")); | |
47 WKPageRunJavaScriptInMainFrame(webView->page(), javaScriptString.get(), stat
ic_cast<void*>(webView), callback); | |
48 } | |
49 | |
50 static void assertSerializedScriptValueIsStringValue(WKSerializedScriptValueRef
serializedValue, WKErrorRef error, const char *expected) | |
51 { | |
52 EXPECT_NULL(error); | |
53 EXPECT_NOT_NULL(serializedValue); | |
54 if (error || !serializedValue) | |
55 return; | |
56 | |
57 JSGlobalContextRef scriptContext = JSGlobalContextCreate(0); | |
58 JSValueRef scriptValue = WKSerializedScriptValueDeserialize(serializedValue,
scriptContext, 0); | |
59 EXPECT_TRUE(JSValueIsString(scriptContext, scriptValue)); | |
60 if (!JSValueIsString(scriptContext, scriptValue)) { | |
61 JSGlobalContextRelease(scriptContext); | |
62 return; | |
63 } | |
64 | |
65 JSStringRef expectedString = JSStringCreateWithUTF8CString(expected); | |
66 JSStringRef scriptString = JSValueToStringCopy(scriptContext, scriptValue, 0
); | |
67 EXPECT_TRUE(JSStringIsEqual(scriptString, expectedString)); | |
68 | |
69 JSStringRelease(scriptString); | |
70 JSStringRelease(expectedString); | |
71 JSGlobalContextRelease(scriptContext); | |
72 } | |
73 | |
74 static void didRunStep1StateChangeVisibleToHidden(WKSerializedScriptValueRef res
ultSerializedScriptValue, WKErrorRef error, void* context) | |
75 { | |
76 assertSerializedScriptValueIsStringValue(resultSerializedScriptValue, error,
"visible"); | |
77 setPageVisibilityStateWithEvalContinuation(static_cast<PlatformWebView*>(con
text), kWKPageVisibilityStateHidden, didRunStep2StateChangeHiddenToPrerender); | |
78 } | |
79 | |
80 static void didRunStep2StateChangeHiddenToPrerender(WKSerializedScriptValueRef r
esultSerializedScriptValue, WKErrorRef error, void* context) | |
81 { | |
82 assertSerializedScriptValueIsStringValue(resultSerializedScriptValue, error,
"hidden"); | |
83 setPageVisibilityStateWithEvalContinuation(static_cast<PlatformWebView*>(con
text), kWKPageVisibilityStatePrerender, didRunStep3StateChangePrerenderToPreview
); | |
84 } | |
85 | |
86 static void didRunStep3StateChangePrerenderToPreview(WKSerializedScriptValueRef
resultSerializedScriptValue, WKErrorRef error, void* context) | |
87 { | |
88 assertSerializedScriptValueIsStringValue(resultSerializedScriptValue, error,
"prerender"); | |
89 setPageVisibilityStateWithEvalContinuation(static_cast<PlatformWebView*>(con
text), kWKPageVisibilityStatePreview, didRunStep4InStatePreview); | |
90 } | |
91 | |
92 static void didRunStep4InStatePreview(WKSerializedScriptValueRef resultSerialize
dScriptValue, WKErrorRef error, void* context) | |
93 { | |
94 assertSerializedScriptValueIsStringValue(resultSerializedScriptValue, error,
"preview"); | |
95 testDone = true; | |
96 } | |
97 | |
98 TEST(WebKit2, PageVisibilityState) | |
99 { | |
100 WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate()); | |
101 | |
102 // Pass the PlatformWebView webView on as the context of the evals, so we ca
n continue to eval on it. | |
103 PlatformWebView webView(context.get()); | |
104 setPageVisibilityStateWithEvalContinuation(&webView, kWKPageVisibilityStateV
isible, didRunStep1StateChangeVisibleToHidden); | |
105 | |
106 Util::run(&testDone); | |
107 } | |
108 | |
109 } // namespace TestWebKitAPI | |
OLD | NEW |