initial import
[vuplus_webkit] / Tools / TestWebKitAPI / mac / WebKitAgnosticTest.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 #include "config.h"
27 #include "WebKitAgnosticTest.h"
28
29 #include <WebKit2/WKURLCF.h>
30 #include <wtf/RetainPtr.h>
31
32 @interface FrameLoadDelegate : NSObject {
33     bool* _didFinishLoad;
34 }
35
36 - (id)initWithDidFinishLoadBoolean:(bool*)didFinishLoad;
37
38 @end
39
40 @implementation FrameLoadDelegate
41
42 - (id)initWithDidFinishLoadBoolean:(bool*)didFinishLoad
43 {
44     self = [super init];
45     if (!self)
46         return nil;
47
48     _didFinishLoad = didFinishLoad;
49     return self;
50 }
51
52 - (void)webView:(WebView *)webView didFinishLoadForFrame:(WebFrame *)webFrame
53 {
54     *_didFinishLoad = true;
55 }
56
57 @end
58
59 namespace TestWebKitAPI {
60
61 static void didFinishLoadForFrame(WKPageRef, WKFrameRef, WKTypeRef, const void* context)
62 {
63     *static_cast<bool*>(const_cast<void*>(context)) = true;
64 }
65
66 static void setPageLoaderClient(WKPageRef page, bool* didFinishLoad)
67 {
68     WKPageLoaderClient loaderClient;
69     memset(&loaderClient, 0, sizeof(loaderClient));
70     loaderClient.version = 0;
71     loaderClient.clientInfo = didFinishLoad;
72     loaderClient.didFinishLoadForFrame = didFinishLoadForFrame;
73
74     WKPageSetPageLoaderClient(page, &loaderClient);
75 }
76
77 WebKitAgnosticTest::WebKitAgnosticTest()
78     : viewFrame(NSMakeRect(0, 0, 800, 600))
79     , didFinishLoad(false)
80 {
81 }
82
83 void WebKitAgnosticTest::runWebKit1Test()
84 {
85     RetainPtr<WebView> webView(AdoptNS, [[WebView alloc] initWithFrame:viewFrame]);
86     RetainPtr<FrameLoadDelegate> delegate(AdoptNS, [[FrameLoadDelegate alloc] initWithDidFinishLoadBoolean:&didFinishLoad]);
87     [webView.get() setFrameLoadDelegate:delegate.get()];
88     initializeView(webView.get());
89
90     loadURL(webView.get(), url());
91     waitForLoadToFinish();
92     didLoadURL(webView.get());
93 }
94
95 void WebKitAgnosticTest::runWebKit2Test()
96 {
97     WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreate());
98     RetainPtr<WKView> view(AdoptNS, [[WKView alloc] initWithFrame:viewFrame contextRef:context.get()]);
99     setPageLoaderClient([view.get() pageRef], &didFinishLoad);
100     initializeView(view.get());
101
102     loadURL(view.get(), url());
103     waitForLoadToFinish();
104     didLoadURL(view.get());
105 }
106
107 void WebKitAgnosticTest::loadURL(WebView *webView, NSURL *url)
108 {
109     EXPECT_FALSE(didFinishLoad);
110     [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
111 }
112
113 void WebKitAgnosticTest::loadURL(WKView *view, NSURL *url)
114 {
115     EXPECT_FALSE(didFinishLoad);
116     WKPageLoadURL([view pageRef], adoptWK(WKURLCreateWithCFURL((CFURLRef)url)).get());
117 }
118
119 void WebKitAgnosticTest::goBack(WebView *webView)
120 {
121     EXPECT_FALSE(didFinishLoad);
122     [webView goBack];
123 }
124
125 void WebKitAgnosticTest::goBack(WKView *view)
126 {
127     EXPECT_FALSE(didFinishLoad);
128     WKPageGoBack([view pageRef]);
129 }
130
131 void WebKitAgnosticTest::waitForLoadToFinish()
132 {
133     Util::run(&didFinishLoad);
134     didFinishLoad = false;
135 }
136
137 } // namespace TestWebKitAPI