d6e42528a0da9a0196bb115a47befe86943a79c7
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKitCocoa / Download.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 #import "config.h"
27 #import <WebKit/WKFoundation.h>
28
29 #if WK_API_ENABLED
30 #if PLATFORM(MAC) // No downloading on iOS
31
32 #import "PlatformUtilities.h"
33 #import "Test.h"
34 #import "TestProtocol.h"
35 #import "TestWKWebView.h"
36 #import <WebCore/FileSystem.h>
37 #import <WebKit/_WKDownload.h>
38 #import <WebKit/_WKDownloadDelegate.h>
39 #import <WebKit/WKNavigationDelegatePrivate.h>
40 #import <WebKit/WKProcessPoolPrivate.h>
41 #import <WebKit/WKUIDelegatePrivate.h>
42 #import <WebKit/WKWebView.h>
43 #import <WebKit/WKWebViewConfiguration.h>
44 #import <wtf/RetainPtr.h>
45 #import <wtf/mac/AppKitCompatibilityDeclarations.h>
46 #import <wtf/text/WTFString.h>
47
48 static bool isDone;
49 static unsigned redirectCount = 0;
50 static bool hasReceivedResponse;
51 static NSURL *sourceURL = [[NSBundle mainBundle] URLForResource:@"simple" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"];
52 static WKWebView* expectedOriginatingWebView;
53
54 @interface DownloadDelegate : NSObject <_WKDownloadDelegate>
55 @end
56
57 @implementation DownloadDelegate {
58     RetainPtr<_WKDownload> _download;
59     String _destinationPath;
60     long long _expectedContentLength;
61     uint64_t _receivedContentLength;
62 }
63
64 - (void)_downloadDidStart:(_WKDownload *)download
65 {
66     EXPECT_NULL(_download);
67     EXPECT_NOT_NULL(download);
68     EXPECT_TRUE([[[[download request] URL] path] isEqualToString:[sourceURL path]]);
69     _download = download;
70 }
71
72 - (void)_download:(_WKDownload *)download didReceiveResponse:(NSURLResponse *)response
73 {
74     hasReceivedResponse = true;
75     EXPECT_EQ(_download, download);
76     EXPECT_TRUE(_expectedContentLength == 0);
77     EXPECT_TRUE(_receivedContentLength == 0);
78     EXPECT_TRUE([[[response URL] path] isEqualToString:[sourceURL path]]);
79     _expectedContentLength = [response expectedContentLength];
80 }
81
82 - (void)_download:(_WKDownload *)download didReceiveData:(uint64_t)length
83 {
84     EXPECT_EQ(_download, download);
85     _receivedContentLength += length;
86 }
87
88 - (NSString *)_download:(_WKDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename allowOverwrite:(BOOL *)allowOverwrite
89 {
90     EXPECT_TRUE(hasReceivedResponse);
91     EXPECT_EQ(_download, download);
92
93     WebCore::PlatformFileHandle fileHandle;
94     _destinationPath = WebCore::openTemporaryFile("TestWebKitAPI", fileHandle);
95     EXPECT_TRUE(fileHandle != WebCore::invalidPlatformFileHandle);
96     WebCore::closeFile(fileHandle);
97
98     *allowOverwrite = YES;
99     return _destinationPath;
100 }
101
102 - (void)_downloadDidFinish:(_WKDownload *)download
103 {
104     EXPECT_EQ(_download, download);
105     EXPECT_TRUE(_expectedContentLength == NSURLResponseUnknownLength || static_cast<uint64_t>(_expectedContentLength) == _receivedContentLength);
106     EXPECT_TRUE([[NSFileManager defaultManager] contentsEqualAtPath:_destinationPath andPath:[sourceURL path]]);
107     WebCore::deleteFile(_destinationPath);
108     isDone = true;
109 }
110
111 @end
112
113 TEST(_WKDownload, DownloadDelegate)
114 {
115     RetainPtr<WKProcessPool> processPool = adoptNS([[WKProcessPool alloc] init]);
116     DownloadDelegate *downloadDelegate = [[DownloadDelegate alloc] init];
117     [processPool _setDownloadDelegate:downloadDelegate];
118
119     @autoreleasepool {
120         EXPECT_EQ(downloadDelegate, [processPool _downloadDelegate]);
121     }
122
123     [downloadDelegate release];
124     EXPECT_NULL([processPool _downloadDelegate]);
125 }
126
127 static void runTest(id <WKNavigationDelegate> navigationDelegate, id <_WKDownloadDelegate> downloadDelegate, NSURL *url)
128 {
129     RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
130     [webView setNavigationDelegate:navigationDelegate];
131     [[[webView configuration] processPool] _setDownloadDelegate:downloadDelegate];
132
133     isDone = false;
134     hasReceivedResponse = false;
135     [webView loadRequest:[NSURLRequest requestWithURL:url]];
136     TestWebKitAPI::Util::run(&isDone);
137 }
138
139 @interface DownloadNavigationDelegate : NSObject <WKNavigationDelegate>
140 @end
141
142 @implementation DownloadNavigationDelegate
143 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
144 {
145     decisionHandler(_WKNavigationActionPolicyDownload);
146 }
147 @end
148
149 TEST(_WKDownload, DownloadRequest)
150 {
151     runTest(adoptNS([[DownloadNavigationDelegate alloc] init]).get(), adoptNS([[DownloadDelegate alloc] init]).get(), sourceURL);
152 }
153
154 @interface ConvertResponseToDownloadNavigationDelegate : NSObject <WKNavigationDelegate>
155 @end
156
157 @implementation ConvertResponseToDownloadNavigationDelegate
158 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
159 {
160     decisionHandler(_WKNavigationResponsePolicyBecomeDownload);
161 }
162 @end
163
164 TEST(_WKDownload, ConvertResponseToDownload)
165 {
166     runTest(adoptNS([[ConvertResponseToDownloadNavigationDelegate alloc] init]).get(), adoptNS([[DownloadDelegate alloc] init]).get(), sourceURL);
167 }
168
169 @interface FailingDownloadDelegate : NSObject <_WKDownloadDelegate>
170 @end
171
172 @implementation FailingDownloadDelegate
173
174 - (void)_downloadDidFinish:(_WKDownload *)download
175 {
176     EXPECT_TRUE(false);
177     isDone = true;
178 }
179
180 - (void)_download:(_WKDownload *)download didFailWithError:(NSError *)error
181 {
182     isDone = true;
183 }
184
185 - (void)_downloadDidCancel:(_WKDownload *)download
186 {
187     EXPECT_TRUE(false);
188     isDone = true;
189 }
190
191 @end
192
193 TEST(_WKDownload, DownloadMissingResource)
194 {
195     runTest(adoptNS([[DownloadNavigationDelegate alloc] init]).get(), adoptNS([[FailingDownloadDelegate alloc] init]).get(), [NSURL URLWithString:@"non-existant-scheme://"]);
196 }
197
198 @interface CancelledDownloadDelegate : NSObject <_WKDownloadDelegate>
199 @end
200
201 @implementation CancelledDownloadDelegate
202
203 - (void)_downloadDidStart:(_WKDownload *)download
204 {
205     [download cancel];
206 }
207
208 - (void)_downloadDidFinish:(_WKDownload *)download
209 {
210     EXPECT_TRUE(false);
211     isDone = true;
212 }
213
214 - (void)_download:(_WKDownload *)download didFailWithError:(NSError *)error
215 {
216     EXPECT_TRUE(false);
217     isDone = true;
218 }
219
220 - (void)_downloadDidCancel:(_WKDownload *)download
221 {
222     isDone = true;
223 }
224
225 @end
226
227 TEST(_WKDownload, CancelDownload)
228 {
229     runTest(adoptNS([[DownloadNavigationDelegate alloc] init]).get(), adoptNS([[CancelledDownloadDelegate alloc] init]).get(), sourceURL);
230 }
231
232 @interface OriginatingWebViewDownloadDelegate : NSObject <_WKDownloadDelegate>
233 - (instancetype)initWithWebView:(WKWebView *)webView;
234 @end
235
236 @implementation OriginatingWebViewDownloadDelegate {
237     RetainPtr<WKWebView> _webView;
238 }
239
240 - (instancetype)initWithWebView:(WKWebView *)webView
241 {
242     if (!(self = [super init]))
243         return nil;
244
245     _webView = webView;
246     return self;
247 }
248
249 - (void)_downloadDidStart:(_WKDownload *)download
250 {
251     @autoreleasepool {
252         EXPECT_EQ([download originatingWebView], _webView);
253     }
254
255     _webView = nullptr;
256     EXPECT_NULL([download originatingWebView]);
257     isDone = true;
258 }
259
260 @end
261
262 TEST(_WKDownload, OriginatingWebView)
263 {
264     RetainPtr<DownloadNavigationDelegate> navigationDelegate = adoptNS([[DownloadNavigationDelegate alloc] init]);                 
265     RetainPtr<OriginatingWebViewDownloadDelegate> downloadDelegate;
266     @autoreleasepool {
267         RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
268         [webView setNavigationDelegate:navigationDelegate.get()];
269         downloadDelegate = adoptNS([[OriginatingWebViewDownloadDelegate alloc] initWithWebView:webView.get()]);
270         [[[webView configuration] processPool] _setDownloadDelegate:downloadDelegate.get()];
271         [webView loadRequest:[NSURLRequest requestWithURL:sourceURL]];
272     }
273
274     isDone = false;
275     TestWebKitAPI::Util::run(&isDone);
276 }
277
278 @interface DownloadRequestOriginalURLDelegate : NSObject <_WKDownloadDelegate>
279 - (instancetype)initWithExpectedOriginalURL:(NSURL *)expectOriginalURL;
280 @end
281
282 @implementation DownloadRequestOriginalURLDelegate {
283     NSURL *_expectedOriginalURL;
284 }
285
286 - (instancetype)initWithExpectedOriginalURL:(NSURL *)expectedOriginalURL
287 {
288     if (!(self = [super init]))
289         return nil;
290
291     _expectedOriginalURL = expectedOriginalURL;
292     return self;
293 }
294
295 - (void)_downloadDidStart:(_WKDownload *)download
296 {
297     if ([_expectedOriginalURL isEqual:sourceURL])
298         EXPECT_TRUE(!download.request.mainDocumentURL);
299     else
300         EXPECT_TRUE([_expectedOriginalURL isEqual:download.request.mainDocumentURL]);
301     isDone = true;
302 }
303
304 @end
305
306 @interface DownloadRequestOriginalURLNavigationDelegate : NSObject <WKNavigationDelegate>
307 @end
308
309 @implementation DownloadRequestOriginalURLNavigationDelegate
310 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
311 {
312     if ([navigationAction.request.URL isEqual:sourceURL])
313         decisionHandler(_WKNavigationActionPolicyDownload);
314     else
315         decisionHandler(WKNavigationActionPolicyAllow);
316 }
317 @end
318
319 TEST(_WKDownload, DownloadRequestOriginalURL)
320 {
321     NSURL *originalURL = [[NSBundle mainBundle] URLForResource:@"DownloadRequestOriginalURL" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"];
322     runTest(adoptNS([[DownloadRequestOriginalURLNavigationDelegate alloc] init]).get(), adoptNS([[DownloadRequestOriginalURLDelegate alloc] initWithExpectedOriginalURL:originalURL]).get(), originalURL);
323 }
324
325 TEST(_WKDownload, DownloadRequestOriginalURLFrame)
326 {
327     NSURL *originalURL = [[NSBundle mainBundle] URLForResource:@"DownloadRequestOriginalURL2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"];
328     runTest(adoptNS([[DownloadRequestOriginalURLNavigationDelegate alloc] init]).get(), adoptNS([[DownloadRequestOriginalURLDelegate alloc] initWithExpectedOriginalURL:originalURL]).get(), originalURL);
329 }
330
331 TEST(_WKDownload, DownloadRequestOriginalURLDirectDownload)
332 {
333     runTest(adoptNS([[DownloadRequestOriginalURLNavigationDelegate alloc] init]).get(), adoptNS([[DownloadRequestOriginalURLDelegate alloc] initWithExpectedOriginalURL:sourceURL]).get(), sourceURL);
334 }
335
336 TEST(_WKDownload, DownloadRequestOriginalURLDirectDownloadWithLoadedContent)
337 {
338     auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
339     [webView setNavigationDelegate:[[DownloadRequestOriginalURLNavigationDelegate alloc] init]];
340     [[[webView configuration] processPool] _setDownloadDelegate:[[DownloadRequestOriginalURLDelegate alloc] initWithExpectedOriginalURL:sourceURL]];
341
342     NSURL *contentURL = [[NSBundle mainBundle] URLForResource:@"simple2" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"];
343     // Here is to test if the original URL can be set correctly when the current document
344     // is completely unrelated to the download.
345     [webView loadRequest:[NSURLRequest requestWithURL:contentURL]];
346     [webView loadRequest:[NSURLRequest requestWithURL:sourceURL]];
347     isDone = false;
348     TestWebKitAPI::Util::run(&isDone);
349 }
350
351 @interface BlobDownloadDelegate : NSObject <_WKDownloadDelegate>
352 @end
353
354 @implementation BlobDownloadDelegate {
355     RetainPtr<_WKDownload> _download;
356     String _destinationPath;
357     long long _expectedContentLength;
358     uint64_t _receivedContentLength;
359 }
360
361 - (void)_downloadDidStart:(_WKDownload *)download
362 {
363     EXPECT_NULL(_download);
364     EXPECT_NOT_NULL(download);
365     EXPECT_TRUE([[[[download request] URL] scheme] isEqualToString:@"blob"]);
366     _download = download;
367 }
368
369 - (void)_download:(_WKDownload *)download didReceiveResponse:(NSURLResponse *)response
370 {
371     hasReceivedResponse = true;
372     EXPECT_EQ(_download, download);
373     EXPECT_EQ(_expectedContentLength, 0U);
374     EXPECT_EQ(_receivedContentLength, 0U);
375     EXPECT_TRUE([[[response URL] scheme] isEqualToString:@"blob"]);
376     _expectedContentLength = [response expectedContentLength];
377 }
378
379 - (void)_download:(_WKDownload *)download didReceiveData:(uint64_t)length
380 {
381     EXPECT_EQ(_download, download);
382     _receivedContentLength += length;
383 }
384
385 - (NSString *)_download:(_WKDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename allowOverwrite:(BOOL *)allowOverwrite
386 {
387     EXPECT_TRUE(hasReceivedResponse);
388     EXPECT_EQ(_download, download);
389
390     WebCore::PlatformFileHandle fileHandle;
391     _destinationPath = WebCore::openTemporaryFile("TestWebKitAPI", fileHandle);
392     EXPECT_TRUE(fileHandle != WebCore::invalidPlatformFileHandle);
393     WebCore::closeFile(fileHandle);
394
395     *allowOverwrite = YES;
396     return _destinationPath;
397 }
398
399 - (void)_downloadDidFinish:(_WKDownload *)download
400 {
401     EXPECT_EQ(_download, download);
402     EXPECT_TRUE(_expectedContentLength == NSURLResponseUnknownLength || static_cast<uint64_t>(_expectedContentLength) == _receivedContentLength);
403     NSString* expectedContent = @"{\"x\":42,\"s\":\"hello, world\"}";
404     NSData* expectedData = [expectedContent dataUsingEncoding:NSUTF8StringEncoding];
405     EXPECT_TRUE([[[NSFileManager defaultManager] contentsAtPath:_destinationPath] isEqualToData:expectedData]);
406     WebCore::deleteFile(_destinationPath);
407     isDone = true;
408 }
409
410 @end
411
412 @interface DownloadBlobURLNavigationDelegate : NSObject <WKNavigationDelegate>
413 @end
414
415 @implementation DownloadBlobURLNavigationDelegate
416 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
417 {
418     if ([navigationAction.request.URL.scheme isEqualToString:@"blob"])
419         decisionHandler(_WKNavigationActionPolicyDownload);
420     else
421         decisionHandler(WKNavigationActionPolicyAllow);
422 }
423 @end
424
425 TEST(_WKDownload, DownloadRequestBlobURL)
426 {
427     NSURL *originalURL = [[NSBundle mainBundle] URLForResource:@"DownloadRequestBlobURL" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"];
428     runTest(adoptNS([[DownloadBlobURLNavigationDelegate alloc] init]).get(), adoptNS([[BlobDownloadDelegate alloc] init]).get(), originalURL);
429 }
430
431 @interface UIDownloadAsFileTestDelegate : NSObject <WKUIDelegatePrivate>
432 @end
433
434 @implementation UIDownloadAsFileTestDelegate
435
436 - (NSMenu *)_webView:(WKWebView *)webView contextMenu:(NSMenu *)menu forElement:(_WKContextMenuElementInfo *)element
437 {
438     static const long downloadLinkedFileTag = 2;
439     auto index = [menu indexOfItemWithTag:downloadLinkedFileTag];
440     [menu performActionForItemAtIndex:index];
441     return nil;
442 }
443
444 @end
445
446 @interface RedirectedDownloadDelegate : NSObject <_WKDownloadDelegate>
447 @end
448
449 @implementation RedirectedDownloadDelegate {
450     String _destinationPath;
451 }
452
453 - (void)_downloadDidStart:(_WKDownload *)download
454 {
455     EXPECT_NOT_NULL(download);
456     EXPECT_EQ(expectedOriginatingWebView, download.originatingWebView);
457 }
458
459 - (NSString *)_download:(_WKDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename allowOverwrite:(BOOL *)allowOverwrite
460 {
461     WebCore::PlatformFileHandle fileHandle;
462     _destinationPath = WebCore::openTemporaryFile("TestWebKitAPI", fileHandle);
463     EXPECT_TRUE(fileHandle != WebCore::invalidPlatformFileHandle);
464     WebCore::closeFile(fileHandle);
465     *allowOverwrite = YES;
466     return _destinationPath;
467 }
468
469 - (void)_download:(_WKDownload *)download didReceiveServerRedirectToURL:(NSURL *)url
470 {
471     if (!redirectCount)
472         EXPECT_STREQ("http://redirect/?pass", [url.absoluteString UTF8String]);
473     else
474         EXPECT_STREQ("http://pass/", [url.absoluteString UTF8String]);
475     ++redirectCount = true;
476 }
477
478 - (void)_downloadDidFinish:(_WKDownload *)download
479 {
480     NSArray<NSURL *> *redirectChain = download.redirectChain;
481     EXPECT_EQ(3U, redirectChain.count);
482     if (redirectChain.count > 0)
483         EXPECT_STREQ("http://redirect/?redirect/?pass", [redirectChain[0].absoluteString UTF8String]);
484     if (redirectChain.count > 1)
485         EXPECT_STREQ("http://redirect/?pass", [redirectChain[1].absoluteString UTF8String]);
486     if (redirectChain.count > 2)
487         EXPECT_STREQ("http://pass/", [redirectChain[2].absoluteString UTF8String]);
488
489     WebCore::deleteFile(_destinationPath);
490     isDone = true;
491 }
492
493 @end
494
495 TEST(_WKDownload, RedirectedDownload)
496 {
497     [TestProtocol registerWithScheme:@"http"];
498
499     redirectCount = 0;
500     isDone = false;
501
502     auto delegate = adoptNS([[UIDownloadAsFileTestDelegate alloc] init]);
503     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
504     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600) configuration:configuration.get()]);
505     [webView setUIDelegate:delegate.get()];
506     auto downloadDelegate = adoptNS([[RedirectedDownloadDelegate alloc] init]);
507     [[[webView configuration] processPool] _setDownloadDelegate:downloadDelegate.get()];
508
509     auto window = adoptNS([[NSWindow alloc] initWithContentRect:[webView frame] styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered defer:YES]);
510     [[window contentView] addSubview:webView.get()];
511
512     // Do 2 loads in the same view to make sure the redirect chain is properly cleared between loads.
513     [webView synchronouslyLoadHTMLString:@"<div>First load</div>"];
514     [webView synchronouslyLoadHTMLString:@"<a style='display: block; height: 100%; width: 100%' href='http://redirect/?redirect/?pass'>test</a>"];
515
516     expectedOriginatingWebView = webView.get();
517     NSPoint clickPoint = NSMakePoint(100, 100);
518     [[webView hitTest:clickPoint] mouseDown:[NSEvent mouseEventWithType:NSEventTypeRightMouseDown location:clickPoint modifierFlags:0 timestamp:0 windowNumber:[window windowNumber] context:nil eventNumber:0 clickCount:1 pressure:1]];
519     [[webView hitTest:clickPoint] mouseUp:[NSEvent mouseEventWithType:NSEventTypeRightMouseUp location:clickPoint modifierFlags:0 timestamp:0 windowNumber:[window windowNumber] context:nil eventNumber:0 clickCount:1 pressure:1]];
520
521     isDone = false;
522     TestWebKitAPI::Util::run(&isDone);
523     EXPECT_EQ(1U, redirectCount);
524
525     [TestProtocol unregister];
526 }
527
528 TEST(_WKDownload, RedirectedLoadConvertedToDownload)
529 {
530     [TestProtocol registerWithScheme:@"http"];
531
532     auto navigationDelegate = adoptNS([[ConvertResponseToDownloadNavigationDelegate alloc] init]);
533     auto downloadDelegate = adoptNS([[RedirectedDownloadDelegate alloc] init]);
534
535     auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
536     [webView setNavigationDelegate:navigationDelegate.get()];
537     [[[webView configuration] processPool] _setDownloadDelegate:downloadDelegate.get()];
538
539     expectedOriginatingWebView = webView.get();
540     isDone = false;
541     redirectCount = 0;
542     hasReceivedResponse = false;
543     [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://redirect/?redirect/?pass"]]];
544     TestWebKitAPI::Util::run(&isDone);
545     EXPECT_EQ(0U, redirectCount);
546
547     [TestProtocol unregister];
548 }
549
550 TEST(_WKDownload, RedirectedSubframeLoadConvertedToDownload)
551 {
552     [TestProtocol registerWithScheme:@"http"];
553
554     auto navigationDelegate = adoptNS([[ConvertResponseToDownloadNavigationDelegate alloc] init]);
555     auto downloadDelegate = adoptNS([[RedirectedDownloadDelegate alloc] init]);
556
557     auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
558     [webView setNavigationDelegate:navigationDelegate.get()];
559     [[[webView configuration] processPool] _setDownloadDelegate:downloadDelegate.get()];
560
561     expectedOriginatingWebView = webView.get();
562     isDone = false;
563     redirectCount = 0;
564     hasReceivedResponse = false;
565     [webView loadHTMLString:@"<body><iframe src='http://redirect/?redirect/?pass'></iframe></body>" baseURL:nil];
566     TestWebKitAPI::Util::run(&isDone);
567     EXPECT_EQ(0U, redirectCount);
568
569     [TestProtocol unregister];
570 }
571
572 #endif
573 #endif