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;
7 import android.graphics.RectF;
8 import android.test.suitebuilder.annotation.SmallTest;
10 import org.chromium.android_webview.ExternalVideoSurfaceContainer;
11 import org.chromium.android_webview.test.util.VideoTestUtil;
12 import org.chromium.base.CommandLine;
13 import org.chromium.base.test.util.Feature;
14 import org.chromium.content.browser.ContentViewCore;
15 import org.chromium.content.browser.test.util.CallbackHelper;
18 * A test suite for ExternalVideoSurfaceContainerTest.
20 public class ExternalVideoSurfaceContainerTest extends AwTestBase {
22 // Callback helper to track the position/size of the external surface.
23 private static class OnExternalVideoSurfacePositionChanged extends CallbackHelper {
26 public RectF getRectF() {
30 public void notifyCalled(RectF rect) {
35 private OnExternalVideoSurfacePositionChanged mOnExternalVideoSurfacePositionChanged =
36 new OnExternalVideoSurfacePositionChanged();
37 private CallbackHelper mOnRequestExternalVideoSurface = new CallbackHelper();
39 private static void waitForVideoSizeChangeTo(OnExternalVideoSurfacePositionChanged helper,
40 int callCount, float widthCss, float heightCss) throws Exception {
41 final int maxSizeChangeNotificationToWaitFor = 5;
42 final float epsilon = 0.000001f;
43 for (int i = 1; i <= maxSizeChangeNotificationToWaitFor; ++i) {
44 helper.waitForCallback(callCount, i);
45 RectF rect = helper.getRectF();
46 if (Math.abs(rect.width() - widthCss) < epsilon
47 && Math.abs(rect.height() - heightCss) < epsilon) {
50 assertTrue(i < maxSizeChangeNotificationToWaitFor);
54 private class MockExternalVideoSurfaceContainer extends ExternalVideoSurfaceContainer {
55 private int mPlayerId = INVALID_PLAYER_ID;
57 public MockExternalVideoSurfaceContainer(
58 long nativeExternalVideoSurfaceContainer, ContentViewCore contentViewCore) {
59 super(nativeExternalVideoSurfaceContainer, contentViewCore);
63 protected void requestExternalVideoSurface(int playerId) {
65 mOnRequestExternalVideoSurface.notifyCalled();
69 protected void releaseExternalVideoSurface(int playerId) {
70 mPlayerId = INVALID_PLAYER_ID;
74 protected void destroy() {}
77 protected void onExternalVideoSurfacePositionChanged(
78 int playerId, float left, float top, float right, float bottom) {
79 if (mPlayerId != playerId) {
82 mOnExternalVideoSurfacePositionChanged.notifyCalled(
83 new RectF(left, top, right, bottom));
87 protected void onFrameInfoUpdated() {}
90 private void setUpMockExternalVideoSurfaceContainer() {
91 ExternalVideoSurfaceContainer.setFactory(new ExternalVideoSurfaceContainer.Factory() {
93 public ExternalVideoSurfaceContainer create(
94 long nativeContainer, ContentViewCore contentViewCore) {
95 return new MockExternalVideoSurfaceContainer(nativeContainer, contentViewCore);
100 @DisableHardwareAccelerationForTest
102 @Feature({"AndroidWebView"})
103 public void testEnableVideoOverlayForEmbeddedVideo() throws Throwable {
104 setUpMockExternalVideoSurfaceContainer();
106 CommandLine.getInstance().appendSwitch("force-use-overlay-embedded-video");
108 int onRequestCallCount = mOnRequestExternalVideoSurface.getCallCount();
109 int onPositionChangedCallCount = mOnExternalVideoSurfacePositionChanged.getCallCount();
111 assertTrue(VideoTestUtil.runVideoTest(this, false, WAIT_TIMEOUT_MS));
113 mOnRequestExternalVideoSurface.waitForCallback(onRequestCallCount);
114 waitForVideoSizeChangeTo(mOnExternalVideoSurfacePositionChanged,
115 onPositionChangedCallCount, 150.0f, 150.0f);
118 @DisableHardwareAccelerationForTest
120 @Feature({"AndroidWebView"})
121 public void testDisableVideoOverlayForEmbeddedVideo() throws Throwable {
122 setUpMockExternalVideoSurfaceContainer();
124 assertTrue(VideoTestUtil.runVideoTest(this, false, WAIT_TIMEOUT_MS));
126 assertEquals(0, mOnRequestExternalVideoSurface.getCallCount());
127 assertEquals(0, mOnExternalVideoSurfacePositionChanged.getCallCount());