c4eb1e91c1b7d4bd19665f30fd51709741b2463f
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKit / WKPageIsPlayingAudio.cpp
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 #if WK_HAVE_C_SPI
29
30 #include "PlatformUtilities.h"
31 #include "PlatformWebView.h"
32 #include "Test.h"
33 #include <JavaScriptCore/JSRetainPtr.h>
34 #include <JavaScriptCore/JavaScriptCore.h>
35 #include <WebKit/WKSerializedScriptValue.h>
36 #include <WebKit/WKPagePrivate.h>
37 #include <WebKit/WKPreferencesRef.h>
38 #include <WebKit/WKPreferencesRefPrivate.h>
39
40 // This test loads file-with-video.html. It first checks to make sure WKPageIsPlayingAudio() returns
41 // false for the page. Then it calls a JavaScript method to play the video, waits for
42 // WKPageUIClient::isPlayingAudioDidChange() to get called, and checks that WKPageIsPlayingAudio() now
43 // returns true for the page.
44
45 namespace TestWebKitAPI {
46
47 static bool isMSEEnabledChanged;
48 static bool isMSEEnabled;
49 static bool didFinishLoad;
50 static bool isPlayingAudioChanged;
51
52 static void nullJavaScriptCallback(WKSerializedScriptValueRef, WKErrorRef error, void*)
53 {
54 }
55
56 static void isMSEEnabledCallback(WKSerializedScriptValueRef serializedResultValue, WKErrorRef error, void*)
57 {
58     JSGlobalContextRef scriptContext = JSGlobalContextCreate(0);
59
60     JSValueRef resultValue = WKSerializedScriptValueDeserialize(serializedResultValue, scriptContext, 0);
61     EXPECT_TRUE(JSValueIsBoolean(scriptContext, resultValue));
62
63     isMSEEnabledChanged = true;
64     isMSEEnabled = JSValueToBoolean(scriptContext, resultValue);
65
66     JSGlobalContextRelease(scriptContext);
67 }
68
69 static void didFinishNavigation(WKPageRef page, WKNavigationRef, WKTypeRef, const void*)
70 {
71     didFinishLoad = true;
72 }
73
74 static void isPlayingAudioDidChangeCallback(WKPageRef page, const void*)
75 {
76     isPlayingAudioChanged = true;
77 }
78
79 static void setUpClients(WKPageRef page)
80 {
81     WKPageNavigationClientV0 loaderClient;
82     memset(&loaderClient, 0, sizeof(loaderClient));
83
84     loaderClient.base.version = 0;
85     loaderClient.didFinishNavigation = didFinishNavigation;
86
87     WKPageSetPageNavigationClient(page, &loaderClient.base);
88
89     WKPageUIClientV5 uiClient;
90     memset(&uiClient, 0, sizeof(uiClient));
91
92     uiClient.base.version = 5;
93     uiClient.isPlayingAudioDidChange = isPlayingAudioDidChangeCallback;
94
95     WKPageSetPageUIClient(page, &uiClient.base);
96 }
97
98 TEST(WebKit, WKPageIsPlayingAudio)
99 {
100     WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreate());
101
102     PlatformWebView webView(context.get());
103     setUpClients(webView.page());
104
105     WKRetainPtr<WKURLRef> url = adoptWK(Util::createURLForResource("file-with-video", "html"));
106     WKPageLoadURL(webView.page(), url.get());
107
108     Util::run(&didFinishLoad);
109
110     EXPECT_FALSE(WKPageIsPlayingAudio(webView.page()));
111     WKPageRunJavaScriptInMainFrame(webView.page(), Util::toWK("playVideo()").get(), 0, nullJavaScriptCallback);
112
113     Util::run(&isPlayingAudioChanged);
114     EXPECT_TRUE(WKPageIsPlayingAudio(webView.page()));
115 }
116
117 TEST(WebKit, MSEIsPlayingAudio)
118 {
119     WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreate());
120
121     WKRetainPtr<WKPageGroupRef> pageGroup(AdoptWK, WKPageGroupCreateWithIdentifier(Util::toWK("MSEIsPlayingAudioPageGroup").get()));
122     WKPreferencesRef preferences = WKPageGroupGetPreferences(pageGroup.get());
123     WKPreferencesSetMediaSourceEnabled(preferences, true);
124     WKPreferencesSetFileAccessFromFileURLsAllowed(preferences, true);
125
126     PlatformWebView webView(context.get(), pageGroup.get());
127     setUpClients(webView.page());
128
129     WKRetainPtr<WKURLRef> url = adoptWK(Util::createURLForResource("file-with-mse", "html"));
130     didFinishLoad = false;
131     WKPageLoadURL(webView.page(), url.get());
132
133     Util::run(&didFinishLoad);
134
135     // Bail out of the test early if the platform does not support MSE.
136     isMSEEnabledChanged = false;
137     WKPageRunJavaScriptInMainFrame(webView.page(), Util::toWK("window.MediaSource !== undefined").get(), 0, isMSEEnabledCallback);
138     Util::run(&isMSEEnabledChanged);
139     if (!isMSEEnabled)
140         return;
141
142     EXPECT_FALSE(WKPageIsPlayingAudio(webView.page()));
143     isPlayingAudioChanged = false;
144     WKPageRunJavaScriptInMainFrame(webView.page(), Util::toWK("playVideo()").get(), 0, nullJavaScriptCallback);
145
146     Util::run(&isPlayingAudioChanged);
147     EXPECT_TRUE(WKPageIsPlayingAudio(webView.page()));
148 }
149
150 } // namespace TestWebKitAPI
151
152 #endif