Home | History | Annotate | Download | only in util
      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.
      4 
      5 package org.chromium.android_webview.test.util;
      6 
      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;
     11 
     12 /**
     13  * Code shared between the various video tests.
     14  */
     15 public class VideoTestUtil {
     16     /**
     17      * Run video test.
     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.
     22      */
     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() {
     30             @Override
     31             public void run() {
     32                 AwSettings awSettings = awContents.getSettings();
     33                 awSettings.setJavaScriptEnabled(true);
     34                 awSettings.setMediaPlaybackRequiresUserGesture(requiredUserGesture);
     35                 observer.register(awContents.getContentViewCore(), "javaObserver");
     36             }
     37         });
     38         VideoTestWebServer webServer = new VideoTestWebServer(testCase.getActivity());
     39         try {
     40             String data = "<html><head><script>" +
     41                 "addEventListener('DOMContentLoaded', function() { " +
     42                 "  document.getElementById('video').addEventListener('play', function() { " +
     43                 "    javaObserver.notifyJava(); " +
     44                 "  }, false); " +
     45                 "}, false); " +
     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);
     51         } finally {
     52             if (webServer != null && webServer.getTestWebServer() != null)
     53                 webServer.getTestWebServer().shutdown();
     54         }
     55     }
     56 }
     57