2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "JavaScriptTest.h"
28 #include "PlatformUtilities.h"
29 #include "PlatformWebView.h"
30 #include <WebKit/WKRetainPtr.h>
31 #include <WebKit/WKPreferencesPrivate.h>
33 namespace TestWebKitAPI {
35 static bool didFinishLoad;
36 static bool didNotHandleKeyDownEvent;
38 static void didFinishLoadForFrame(WKPageRef, WKFrameRef, WKTypeRef, const void*)
43 static void didNotHandleKeyEventCallback(WKPageRef, WKNativeEventPtr event, const void*)
45 if (Util::isKeyDown(event))
46 didNotHandleKeyDownEvent = true;
49 TEST(WebKit2, SpacebarScrolling)
51 WKRetainPtr<WKContextRef> context(AdoptWK, Util::createContextWithInjectedBundle());
53 // Turn off threaded scrolling; synchronously waiting for the main thread scroll position to
54 // update using WKPageForceRepaint would be better, but for some reason the test still fails occasionally.
55 WKRetainPtr<WKPageGroupRef> pageGroup(AdoptWK, WKPageGroupCreateWithIdentifier(Util::toWK("NoThreadedScrollingPageGroup").get()));
56 WKPreferencesRef preferences = WKPageGroupGetPreferences(pageGroup.get());
57 WKPreferencesSetThreadedScrollingEnabled(preferences, false);
59 PlatformWebView webView(context.get(), pageGroup.get());
61 WKPageLoaderClientV0 loaderClient;
62 memset(&loaderClient, 0, sizeof(loaderClient));
64 loaderClient.base.version = 0;
65 loaderClient.didFinishLoadForFrame = didFinishLoadForFrame;
67 WKPageSetPageLoaderClient(webView.page(), &loaderClient.base);
69 WKPageUIClientV0 uiClient;
70 memset(&uiClient, 0, sizeof(uiClient));
72 uiClient.base.version = 0;
73 uiClient.didNotHandleKeyEvent = didNotHandleKeyEventCallback;
75 WKPageSetPageUIClient(webView.page(), &uiClient.base);
77 WKRetainPtr<WKURLRef> url(AdoptWK, Util::createURLForResource("spacebar-scrolling", "html"));
78 WKPageLoadURL(webView.page(), url.get());
79 Util::run(&didFinishLoad);
81 EXPECT_JS_FALSE(webView.page(), "isDocumentScrolled()");
82 EXPECT_JS_FALSE(webView.page(), "textFieldContainsSpace()");
84 webView.simulateSpacebarKeyPress();
86 EXPECT_JS_FALSE(webView.page(), "isDocumentScrolled()");
87 EXPECT_JS_TRUE(webView.page(), "textFieldContainsSpace()");
89 // On Mac, a key down event represents both a raw key down and a key press.
90 // We expect the key press to be handled (because it inserts text into the text field),
91 // but the raw key down should not be handled.
93 EXPECT_FALSE(didNotHandleKeyDownEvent);
96 EXPECT_JS_EQ(webView.page(), "blurTextField()", "undefined");
98 didNotHandleKeyDownEvent = false;
99 webView.simulateSpacebarKeyPress();
101 EXPECT_JS_TRUE(webView.page(), "isDocumentScrolled()");
102 EXPECT_JS_TRUE(webView.page(), "textFieldContainsSpace()");
105 EXPECT_FALSE(didNotHandleKeyDownEvent);
109 } // namespace TestWebKitAPI