Delay WebProcess launch until a load is triggered in a Web view
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKit / mac / GetBackingScaleFactor.mm
1 /*
2  * Copyright (C) 2011 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
28 #import "PlatformUtilities.h"
29 #import "SyntheticBackingScaleFactorWindow.h"
30 #import "Test.h"
31 #import <WebKit/WKBrowsingContextController.h>
32 #import <WebKit/WKViewPrivate.h>
33 #import <wtf/RetainPtr.h>
34
35 namespace TestWebKitAPI {
36
37 static bool messageReceived;
38 static double backingScaleFactor;
39
40 static void didReceiveMessageFromInjectedBundle(WKContextRef context, WKStringRef messageName, WKTypeRef messageBody, const void*)
41 {
42     messageReceived = true;
43     EXPECT_WK_STREQ("DidGetBackingScaleFactor", messageName);
44     ASSERT_NOT_NULL(messageBody);
45     EXPECT_EQ(WKDoubleGetTypeID(), WKGetTypeID(messageBody));
46     backingScaleFactor = WKDoubleGetValue(static_cast<WKDoubleRef>(messageBody));
47 }
48
49 static void setInjectedBundleClient(WKContextRef context)
50 {
51     WKContextInjectedBundleClientV0 injectedBundleClient;
52     memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
53
54     injectedBundleClient.base.version = 0;
55     injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessageFromInjectedBundle;
56
57     WKContextSetInjectedBundleClient(context, &injectedBundleClient.base);
58 }
59
60 static RetainPtr<SyntheticBackingScaleFactorWindow> createWindow()
61 {
62     RetainPtr<SyntheticBackingScaleFactorWindow> window = adoptNS([[SyntheticBackingScaleFactorWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600) styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES]);
63     [window.get() setReleasedWhenClosed:NO];
64     return window;
65 }
66
67 TEST(WebKit, GetBackingScaleFactor)
68 {
69     WKRetainPtr<WKContextRef> context = adoptWK(Util::createContextForInjectedBundleTest("GetBackingScaleFactorTest"));
70     setInjectedBundleClient(context.get());
71     RetainPtr<WKView> view = adoptNS([[WKView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) contextRef:context.get() pageGroupRef:0]);
72     [[view browsingContextController] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
73
74     RetainPtr<SyntheticBackingScaleFactorWindow> window1 = createWindow();
75     [window1.get() setBackingScaleFactor:1];
76
77     [[window1.get() contentView] addSubview:view.get()];
78     WKContextPostMessageToInjectedBundle(context.get(), Util::toWK("GetBackingScaleFactor").get(), 0);
79     Util::run(&messageReceived);
80     messageReceived = false;
81     EXPECT_EQ(1, backingScaleFactor);
82
83     RetainPtr<SyntheticBackingScaleFactorWindow> window2 = createWindow();
84     [window2.get() setBackingScaleFactor:2];
85
86     [[window2.get() contentView] addSubview:view.get()];
87     WKContextPostMessageToInjectedBundle(context.get(), Util::toWK("GetBackingScaleFactor").get(), 0);
88     Util::run(&messageReceived);
89     messageReceived = false;
90     EXPECT_EQ(2, backingScaleFactor);
91
92     WKPageSetCustomBackingScaleFactor(view.get().pageRef, 3);
93     WKContextPostMessageToInjectedBundle(context.get(), Util::toWK("GetBackingScaleFactor").get(), 0);
94     Util::run(&messageReceived);
95     messageReceived = false;
96     EXPECT_EQ(3, backingScaleFactor);
97 }
98
99 } // namespace TestWebKitAPI