1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.android_webview.test.util;
7 import org.chromium.android_webview.AwContents;
8 import org.chromium.android_webview.AwSettings;
9 import org.chromium.android_webview.test.AwTestBase;
10 import org.chromium.android_webview.test.TestAwContentsClient;
13 * Code shared between the various video tests.
15 public class VideoTestUtil {
18 * @param testCase the test case instance we're going to run the test in.
19 * @param requiredUserGesture the settings of MediaPlaybackRequiresUserGesture.
20 * @return true if the event happened,
21 * @throws Throwable throw exception if timeout.
23 public static boolean runVideoTest(final AwTestBase testCase, final boolean requiredUserGesture,
24 long waitTime) throws Throwable {
25 final JavascriptEventObserver observer = new JavascriptEventObserver();
26 TestAwContentsClient client = new TestAwContentsClient();
27 final AwContents awContents =
28 testCase.createAwTestContainerViewOnMainSync(client).getAwContents();
29 testCase.getInstrumentation().runOnMainSync(new Runnable() {
32 AwSettings awSettings = awContents.getSettings();
33 awSettings.setJavaScriptEnabled(true);
34 awSettings.setMediaPlaybackRequiresUserGesture(requiredUserGesture);
35 observer.register(awContents.getContentViewCore(), "javaObserver");
38 VideoTestWebServer webServer = new VideoTestWebServer(testCase.getActivity());
40 String data = "<html><head><script>" +
41 "addEventListener('DOMContentLoaded', function() { " +
42 " document.getElementById('video').addEventListener('play', function() { " +
43 " javaObserver.notifyJava(); " +
46 "</script></head><body>" +
47 "<video id='video' autoplay control src='" +
48 webServer.getOnePixelOneFrameWebmURL() + "' /> </body></html>";
49 testCase.loadDataAsync(awContents, data, "text/html", false);
50 return observer.waitForEvent(waitTime);
52 if (webServer != null && webServer.getTestWebServer() != null)
53 webServer.getTestWebServer().shutdown();