Fix API test after r241728
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKitCocoa / ExitFullscreenOnEnterPiP.mm
1 /*
2  * Copyright (C) 2018 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 #if (WK_API_ENABLED && PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR))
29
30 #import "PlatformUtilities.h"
31 #import "Test.h"
32 #import "TestWKWebView.h"
33 #import <WebKit/WKPreferencesPrivate.h>
34 #import <WebKit/WKUIDelegatePrivate.h>
35 #import <WebKit/_WKFullscreenDelegate.h>
36 #import <wtf/RetainPtr.h>
37 #import <wtf/Seconds.h>
38
39 static bool didEnterPiP;
40 static bool didExitPiP;
41 static bool didEnterFullscreen;
42 static bool didExitFullscreen;
43
44 @interface ExitFullscreenOnEnterPiPUIDelegate : NSObject <WKUIDelegate, _WKFullscreenDelegate>
45 @end
46
47 @implementation ExitFullscreenOnEnterPiPUIDelegate
48
49 - (void)_webView:(WKWebView *)webView hasVideoInPictureInPictureDidChange:(BOOL)value
50 {
51     if (value)
52         didEnterPiP = true;
53     else
54         didExitPiP = true;
55 }
56
57 - (void)_webViewDidEnterFullscreen:(WKWebView *)webView
58 {
59     didEnterFullscreen = true;
60 }
61
62 - (void)_webViewDidExitFullscreen:(WKWebView *)webView
63 {
64     didExitFullscreen = true;
65 }
66 @end
67
68 namespace TestWebKitAPI {
69
70 TEST(ExitFullscreenOnEnterPiP, VideoFullscreen)
71 {
72     RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
73     [configuration preferences]._fullScreenEnabled = YES;
74     [configuration preferences]._allowsPictureInPictureMediaPlayback = YES;
75     RetainPtr<TestWKWebView> webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get() addToWindow:YES]);
76     RetainPtr<ExitFullscreenOnEnterPiPUIDelegate> handler = adoptNS([[ExitFullscreenOnEnterPiPUIDelegate alloc] init]);
77     [webView setUIDelegate:handler.get()];
78     [webView _setFullscreenDelegate:handler.get()];
79
80     [webView synchronouslyLoadTestPageNamed:@"ExitFullscreenOnEnterPiP"];
81
82     didEnterFullscreen = false;
83     [webView evaluateJavaScript:@"document.getElementById('enter-video-fullscreen').click()" completionHandler: nil];
84     TestWebKitAPI::Util::run(&didEnterFullscreen);
85     ASSERT_TRUE(didEnterFullscreen);
86
87     didEnterPiP = false;
88     didExitFullscreen = false;
89     [webView evaluateJavaScript:@"document.getElementById('enter-pip').click()" completionHandler: nil];
90     TestWebKitAPI::Util::run(&didEnterPiP);
91     TestWebKitAPI::Util::run(&didExitFullscreen);
92
93     sleep(1_s); // Wait for PIPAgent to launch, or it won't call -pipDidClose: callback.
94
95     [webView evaluateJavaScript:@"document.getElementById('exit-pip').click()" completionHandler: nil];
96     TestWebKitAPI::Util::run(&didExitPiP);
97 }
98
99
100 TEST(ExitFullscreenOnEnterPiP, ElementFullscreen)
101 {
102     RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
103     [configuration preferences]._fullScreenEnabled = YES;
104     [configuration preferences]._allowsPictureInPictureMediaPlayback = YES;
105     RetainPtr<TestWKWebView> webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get() addToWindow:YES]);
106     RetainPtr<ExitFullscreenOnEnterPiPUIDelegate> handler = adoptNS([[ExitFullscreenOnEnterPiPUIDelegate alloc] init]);
107     [webView setUIDelegate:handler.get()];
108     [webView _setFullscreenDelegate:handler.get()];
109
110     [webView synchronouslyLoadTestPageNamed:@"ExitFullscreenOnEnterPiP"];
111
112     didEnterFullscreen = false;
113     [webView evaluateJavaScript:@"document.getElementById('enter-element-fullscreen').click()" completionHandler: nil];
114     TestWebKitAPI::Util::run(&didEnterFullscreen);
115     ASSERT_TRUE(didEnterFullscreen);
116
117     // Make the video the "main content" by playing with a user gesture.
118     __block bool didBeginPlaying = false;
119     [webView performAfterReceivingMessage:@"playing" action:^{ didBeginPlaying = true; }];
120     [webView evaluateJavaScript:@"document.getElementById('play').click()" completionHandler:nil];
121     TestWebKitAPI::Util::run(&didBeginPlaying);
122
123     didEnterPiP = false;
124     didExitFullscreen = false;
125     [webView evaluateJavaScript:@"document.getElementById('enter-pip').click()" completionHandler: nil];
126     TestWebKitAPI::Util::run(&didEnterPiP);
127     TestWebKitAPI::Util::run(&didExitFullscreen);
128
129     sleep(1_s); // Wait for PIPAgent to launch, or it won't call -pipDidClose: callback.
130
131     [webView evaluateJavaScript:@"document.getElementById('exit-pip').click()" completionHandler: nil];
132     TestWebKitAPI::Util::run(&didExitPiP);
133 }
134
135 } // namespace TestWebKitAPI
136
137 #endif