b2d41bdfaf3e36a17a5a8c5e0e2151d6135006be
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKit2Cocoa / Navigation.mm
1 /*
2  * Copyright (C) 2014 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
28 #import <WebKit2/WKNavigation.h>
29 #import <WebKit2/WKNavigationDelegate.h>
30 #import <WebKit2/WKWebView.h>
31 #import <wtf/RetainPtr.h>
32 #import "PlatformUtilities.h"
33 #import "Test.h"
34
35 #if WK_API_ENABLED
36
37 static bool isDone;
38 static RetainPtr<WKNavigation> currentNavigation;
39
40 @interface NavigationDelegate : NSObject <WKNavigationDelegate>
41 @end
42
43 @implementation NavigationDelegate
44
45 - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
46 {
47     EXPECT_EQ(currentNavigation, navigation);
48     EXPECT_NOT_NULL(navigation.request);
49 }
50
51 - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
52 {
53     EXPECT_EQ(currentNavigation, navigation);
54 }
55
56 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
57 {
58     EXPECT_EQ(currentNavigation, navigation);
59
60     isDone = true;
61 }
62
63 @end
64
65 TEST(WKNavigation, NavigationDelegate)
66 {
67     RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
68
69     NavigationDelegate *delegate = [[NavigationDelegate alloc] init];
70     [webView setNavigationDelegate:delegate];
71
72     @autoreleasepool {
73         EXPECT_EQ(delegate, [webView navigationDelegate]);
74     }
75
76     [delegate release];
77     EXPECT_NULL([webView navigationDelegate]);
78 }
79
80 TEST(WKNavigation, LoadRequest)
81 {
82     RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
83
84     RetainPtr<NavigationDelegate> delegate = adoptNS([[NavigationDelegate alloc] init]);
85     [webView setNavigationDelegate:delegate.get()];
86
87     NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
88
89     currentNavigation = [webView loadRequest:request];
90     ASSERT_NOT_NULL(currentNavigation);
91     ASSERT_TRUE([[currentNavigation request] isEqual:request]);
92
93     TestWebKitAPI::Util::run(&isDone);
94 }
95
96 @interface DidFailProvisionalNavigationDelegate : NSObject <WKNavigationDelegate>
97 @end
98
99 @implementation DidFailProvisionalNavigationDelegate
100
101 - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
102 {
103     EXPECT_EQ(currentNavigation, navigation);
104     EXPECT_NOT_NULL(navigation.request);
105 }
106
107 - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
108 {
109     EXPECT_EQ(currentNavigation, navigation);
110     EXPECT_NOT_NULL(navigation.request);
111
112     EXPECT_TRUE([error.domain isEqualToString:NSURLErrorDomain]);
113     EXPECT_EQ(NSURLErrorUnsupportedURL, error.code);
114
115     isDone = true;
116 }
117
118 @end
119
120 TEST(WKNavigation, DidFailProvisionalNavigation)
121 {
122     RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
123
124     RetainPtr<DidFailProvisionalNavigationDelegate> delegate = adoptNS([[DidFailProvisionalNavigationDelegate alloc] init]);
125     [webView setNavigationDelegate:delegate.get()];
126
127     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"non-existant-scheme://"]];
128
129     currentNavigation = [webView loadRequest:request];
130     ASSERT_NOT_NULL(currentNavigation);
131     ASSERT_TRUE([[currentNavigation request] isEqual:request]);
132
133     TestWebKitAPI::Util::run(&isDone);
134 }
135
136 #endif