2ca1ddd459ab2b8948f65617bbaaff2e1b4e8d01
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKitCocoa / NowPlayingControlsTests.mm
1 /*
2  * Copyright (C) 2016 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 "PlatformUtilities.h"
29 #import "TestWKWebView.h"
30
31 #import <WebKit/WKWebViewConfigurationPrivate.h>
32 #import <WebKit/WKWebViewPrivate.h>
33
34 #if WK_API_ENABLED && (PLATFORM(IOS) || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101201)
35
36 @interface NowPlayingTestWebView : TestWKWebView
37 @property (nonatomic, readonly) BOOL hasActiveNowPlayingSession;
38 @property (readonly) NSString *lastUpdatedTitle;
39 @property (readonly) double lastUpdatedDuration;
40 @property (readonly) double lastUpdatedElapsedTime;
41 @end
42
43 @implementation NowPlayingTestWebView {
44     bool _receivedNowPlayingInfoResponse;
45     BOOL _hasActiveNowPlayingSession;
46 }
47 - (BOOL)hasActiveNowPlayingSession
48 {
49     _receivedNowPlayingInfoResponse = false;
50     [self _requestActiveNowPlayingSessionInfo];
51     TestWebKitAPI::Util::run(&_receivedNowPlayingInfoResponse);
52
53     return _hasActiveNowPlayingSession;
54 }
55
56 - (void)expectHasActiveNowPlayingSession:(BOOL)hasActiveNowPlayingSession
57 {
58     bool finishedWaiting = false;
59     while (!finishedWaiting) {
60         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
61         finishedWaiting = self.hasActiveNowPlayingSession == hasActiveNowPlayingSession;
62     }
63 }
64
65 - (void)waitForNowPlayingInfoToChange
66 {
67     BOOL initialHasActiveNowPlayingSession = self.hasActiveNowPlayingSession;
68     NSString *initialTitle = self.lastUpdatedTitle;
69     double initialDuration = self.lastUpdatedDuration;
70     double initialElapsedTime = self.lastUpdatedElapsedTime;
71     while ([[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]]) {
72         BOOL currentlyHasActiveNowPlayingSession = self.hasActiveNowPlayingSession;
73         if (initialHasActiveNowPlayingSession != currentlyHasActiveNowPlayingSession)
74             break;
75
76         if (initialDuration != self.lastUpdatedDuration)
77             break;
78
79         if (initialElapsedTime != self.lastUpdatedElapsedTime)
80             break;
81
82         if (![initialTitle isEqualToString:self.lastUpdatedTitle] && self.lastUpdatedTitle != initialTitle)
83             break;
84     }
85 }
86
87 - (void)_handleActiveNowPlayingSessionInfoResponse:(BOOL)hasActiveSession title:(NSString *)title duration:(double)duration elapsedTime:(double)elapsedTime
88 {
89     _hasActiveNowPlayingSession = hasActiveSession;
90     _lastUpdatedTitle = [title copy];
91     _lastUpdatedDuration = duration;
92     _lastUpdatedElapsedTime = elapsedTime;
93
94     _receivedNowPlayingInfoResponse = true;
95 }
96
97 - (void)setWindowVisible:(BOOL)isVisible
98 {
99 #if PLATFORM(MAC)
100     [self.window setIsVisible:isVisible];
101 #else
102     self.window.hidden = !isVisible;
103 #endif
104 }
105 @end
106
107 namespace TestWebKitAPI {
108
109 #if PLATFORM(MAC)
110 TEST(NowPlayingControlsTests, NowPlayingControlsDoNotShowForForegroundPage)
111 {
112     WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
113     configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
114     NowPlayingTestWebView *webView = [[NowPlayingTestWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration];
115     [webView loadTestPageNamed:@"large-video-test-now-playing"];
116     [webView waitForMessage:@"playing"];
117
118     [webView setWindowVisible:YES];
119     [webView.window makeKeyWindow];
120     [webView expectHasActiveNowPlayingSession:NO];
121
122     ASSERT_STREQ("foo", webView.lastUpdatedTitle.UTF8String);
123     ASSERT_EQ(10, webView.lastUpdatedDuration);
124     ASSERT_GE(webView.lastUpdatedElapsedTime, 0);
125 }
126
127 TEST(NowPlayingControlsTests, NowPlayingControlsShowForBackgroundPage)
128 {
129     WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
130     configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
131     NowPlayingTestWebView *webView = [[NowPlayingTestWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration];
132     [webView loadTestPageNamed:@"large-video-test-now-playing"];
133     [webView waitForMessage:@"playing"];
134
135     [webView setWindowVisible:NO];
136     [webView.window resignKeyWindow];
137     [webView expectHasActiveNowPlayingSession:YES];
138
139     ASSERT_STREQ("foo", webView.lastUpdatedTitle.UTF8String);
140     ASSERT_EQ(10, webView.lastUpdatedDuration);
141     ASSERT_GE(webView.lastUpdatedElapsedTime, 0);
142 }
143
144 TEST(NowPlayingControlsTests, NowPlayingControlsHideAfterShowingKeepsSessionActive)
145 {
146     WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
147     configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
148     NowPlayingTestWebView *webView = [[NowPlayingTestWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration];
149     [webView loadTestPageNamed:@"large-video-test-now-playing"];
150     [webView waitForMessage:@"playing"];
151
152     [webView setWindowVisible:NO];
153     [webView.window resignKeyWindow];
154
155     [webView expectHasActiveNowPlayingSession:YES];
156
157     [webView setWindowVisible:YES];
158     [webView.window makeKeyWindow];
159
160     [webView expectHasActiveNowPlayingSession:NO];
161
162     ASSERT_STREQ("foo", webView.lastUpdatedTitle.UTF8String);
163     ASSERT_EQ(10, webView.lastUpdatedDuration);
164     ASSERT_GE(webView.lastUpdatedElapsedTime, 0);
165 }
166
167 TEST(NowPlayingControlsTests, NowPlayingControlsClearInfoAfterSessionIsNoLongerValid)
168 {
169     WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
170     configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
171     NowPlayingTestWebView *webView = [[NowPlayingTestWebView alloc] initWithFrame:NSMakeRect(0, 0, 480, 320) configuration:configuration];
172     [webView loadTestPageNamed:@"large-video-test-now-playing"];
173     [webView waitForMessage:@"playing"];
174
175     [webView stringByEvaluatingJavaScript:@"document.querySelector('video').muted = true"];
176     [webView setWindowVisible:NO];
177     [webView.window resignKeyWindow];
178
179     [webView waitForNowPlayingInfoToChange];
180
181     ASSERT_STREQ("", webView.lastUpdatedTitle.UTF8String);
182     ASSERT_TRUE(isnan(webView.lastUpdatedDuration));
183     ASSERT_TRUE(isnan(webView.lastUpdatedElapsedTime));
184 }
185 #endif // PLATFORM(MAC)
186
187 #if PLATFORM(IOS)
188 // FIXME: Re-enable this test once <webkit.org/b/175204> is resolved.
189 TEST(NowPlayingControlsTests, DISABLED_NowPlayingControlsIOS)
190 {
191     WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
192     configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
193     NowPlayingTestWebView *webView = [[NowPlayingTestWebView alloc] initWithFrame:NSMakeRect(0, 0, 480, 320) configuration:configuration];
194     [webView loadTestPageNamed:@"large-video-test-now-playing"];
195     [webView waitForMessage:@"playing"];
196
197     [webView expectHasActiveNowPlayingSession:YES];
198     ASSERT_STREQ("foo", webView.lastUpdatedTitle.UTF8String);
199     ASSERT_EQ(10, webView.lastUpdatedDuration);
200     ASSERT_GE(webView.lastUpdatedElapsedTime, 0);
201 }
202 #endif
203
204 } // namespace TestWebKitAPI
205
206 #endif // WK_API_ENABLED && (PLATFORM(IOS) || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101201)