API test URLSchemeHandler.Leaks1 is timing out on iOS simulator debug
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKitCocoa / WKURLSchemeHandler-leaks.mm
1 /*
2  * Copyright (C) 2017 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 #ifndef NDEBUG
29
30 #import "PlatformUtilities.h"
31 #import "Test.h"
32 #import <WebKit/WKProcessPoolPrivate.h>
33 #import <WebKit/WKTestingSupport.h>
34 #import <WebKit/WKURLSchemeHandler.h>
35 #import <WebKit/WKURLSchemeTaskPrivate.h>
36 #import <WebKit/WKWebViewConfigurationPrivate.h>
37 #import <WebKit/WKWebViewPrivate.h>
38 #import <WebKit/WebKit.h>
39 #import <wtf/RetainPtr.h>
40 #import <wtf/Vector.h>
41
42 #if WK_API_ENABLED
43
44 @interface WKWebView (TestingSecrets)
45 + (size_t)_webURLSchemeTaskInstanceCount;
46 + (size_t)_apiURLSchemeTaskInstanceCount;
47 @end
48
49 @interface LeakSchemeHandler : NSObject <WKURLSchemeHandler>
50 @end
51
52 @implementation LeakSchemeHandler
53
54 - (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)task
55 {
56     RetainPtr<NSURLResponse> response = adoptNS([[NSURLResponse alloc] initWithURL:task.request.URL MIMEType:@"text/html" expectedContentLength:1 textEncodingName:nil]);
57     [task didReceiveResponse:response.get()];
58     [task didReceiveData:[NSData dataWithBytes:"1" length:1]];
59
60     // Don't finish these tasks, as we're exploring their leaks
61 }
62
63 - (void)webView:(WKWebView *)webView stopURLSchemeTask:(id <WKURLSchemeTask>)task
64 {
65 }
66
67 @end
68
69 static void runUntilTasksInFlight(size_t count)
70 {
71     while (true) {
72         EXPECT_EQ(WKGetAPIURLSchemeTaskInstanceCount(), WKGetWebURLSchemeTaskInstanceCount());
73
74         if (WKGetAPIURLSchemeTaskInstanceCount() == count)
75             return;
76
77         TestWebKitAPI::Util::spinRunLoop(10);
78     }
79 }
80
81 auto e = EPERM;
82
83 #if PLATFORM(IOS)
84 // This test is sometimes timing out on iOS (rdar://problem/33665676).
85 TEST(URLSchemeHandler, DISABLED_Leaks1)
86 #else
87 TEST(URLSchemeHandler, Leaks1)
88 #endif
89 {
90     RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
91     RetainPtr<LeakSchemeHandler> handler = adoptNS([[LeakSchemeHandler alloc] init]);
92     [configuration setURLSchemeHandler:handler.get() forURLScheme:@"testing"];
93
94     @autoreleasepool {
95     RetainPtr<WKWebView> webView1 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
96         @autoreleasepool {
97         RetainPtr<WKWebView> webView2 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
98             @autoreleasepool {
99                 RetainPtr<WKWebView> webView3 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
100
101                 handler = nil;
102                 configuration = nil;
103
104                 [webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main1"]]];
105                 [webView2 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main2"]]];
106                 [webView3 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main3"]]];
107
108                 runUntilTasksInFlight(3);
109             }
110             runUntilTasksInFlight(2);
111         }
112         runUntilTasksInFlight(1);
113     }
114     runUntilTasksInFlight(0);
115 }
116
117 TEST(URLSchemeHandler, Leaks2)
118 {
119     RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
120     RetainPtr<LeakSchemeHandler> handler = adoptNS([[LeakSchemeHandler alloc] init]);
121     [configuration setURLSchemeHandler:handler.get() forURLScheme:@"testing"];
122
123     RetainPtr<WKWebView> webView1 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
124     RetainPtr<WKWebView> webView2 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
125
126     handler = nil;
127     configuration = nil;
128
129     [webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main1"]]];
130     [webView2 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main2"]]];
131
132     runUntilTasksInFlight(2);
133
134     kill([webView1 _webProcessIdentifier], SIGKILL);
135     kill([webView2 _webProcessIdentifier], SIGKILL);
136
137     runUntilTasksInFlight(0);
138 }
139
140 TEST(URLSchemeHandler, Leaks3)
141 {
142     RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
143     RetainPtr<LeakSchemeHandler> handler = adoptNS([[LeakSchemeHandler alloc] init]);
144     [configuration setURLSchemeHandler:handler.get() forURLScheme:@"testing"];
145
146     RetainPtr<WKWebView> webView1 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
147     RetainPtr<WKWebView> webView2 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
148
149     handler = nil;
150     configuration = nil;
151
152     [webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main1"]]];
153     [webView2 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"testing:main2"]]];
154
155     runUntilTasksInFlight(2);
156
157     [webView1.get().configuration.processPool _terminateNetworkProcess];
158
159     runUntilTasksInFlight(0);
160 }
161
162 #endif // WK_API_ENABLED
163 #endif // NDEBUG