Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / android_webview / javatests / src / org / chromium / android_webview / test / ExternalVideoSurfaceContainerTest.java
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;
6
7 import android.graphics.RectF;
8 import android.test.suitebuilder.annotation.SmallTest;
9
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;
16
17 /**
18  * A test suite for ExternalVideoSurfaceContainerTest.
19  */
20 public class ExternalVideoSurfaceContainerTest extends AwTestBase {
21
22     // Callback helper to track the position/size of the external surface.
23     private static class OnExternalVideoSurfacePositionChanged extends CallbackHelper {
24         private RectF mRect;
25
26         public RectF getRectF() {
27             return mRect;
28         }
29
30         public void notifyCalled(RectF rect) {
31             mRect = rect;
32             notifyCalled();
33         }
34     }
35     private OnExternalVideoSurfacePositionChanged mOnExternalVideoSurfacePositionChanged =
36             new OnExternalVideoSurfacePositionChanged();
37     private CallbackHelper mOnRequestExternalVideoSurface = new CallbackHelper();
38
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) {
48                 break;
49             }
50             assertTrue(i < maxSizeChangeNotificationToWaitFor);
51         }
52     }
53
54     private class MockExternalVideoSurfaceContainer extends ExternalVideoSurfaceContainer {
55         private int mPlayerId = INVALID_PLAYER_ID;
56
57         public MockExternalVideoSurfaceContainer(
58                 long nativeExternalVideoSurfaceContainer, ContentViewCore contentViewCore) {
59             super(nativeExternalVideoSurfaceContainer, contentViewCore);
60         }
61
62         @Override
63         protected void requestExternalVideoSurface(int playerId) {
64             mPlayerId = playerId;
65             mOnRequestExternalVideoSurface.notifyCalled();
66         }
67
68         @Override
69         protected void releaseExternalVideoSurface(int playerId) {
70             mPlayerId = INVALID_PLAYER_ID;
71         }
72
73         @Override
74         protected void destroy() {}
75
76         @Override
77         protected void onExternalVideoSurfacePositionChanged(
78                 int playerId, float left, float top, float right, float bottom) {
79             if (mPlayerId != playerId) {
80                 return;
81             }
82             mOnExternalVideoSurfacePositionChanged.notifyCalled(
83                     new RectF(left, top, right, bottom));
84         }
85
86         @Override
87         protected void onFrameInfoUpdated() {}
88     }
89
90     private void setUpMockExternalVideoSurfaceContainer() {
91         ExternalVideoSurfaceContainer.setFactory(new ExternalVideoSurfaceContainer.Factory() {
92             @Override
93             public ExternalVideoSurfaceContainer create(
94                     long nativeContainer, ContentViewCore contentViewCore) {
95                 return new MockExternalVideoSurfaceContainer(nativeContainer, contentViewCore);
96             }
97         });
98     }
99
100     @DisableHardwareAccelerationForTest
101     @SmallTest
102     @Feature({"AndroidWebView"})
103     public void testEnableVideoOverlayForEmbeddedVideo() throws Throwable {
104         setUpMockExternalVideoSurfaceContainer();
105
106         CommandLine.getInstance().appendSwitch("force-use-overlay-embedded-video");
107
108         int onRequestCallCount = mOnRequestExternalVideoSurface.getCallCount();
109         int onPositionChangedCallCount = mOnExternalVideoSurfacePositionChanged.getCallCount();
110
111         assertTrue(VideoTestUtil.runVideoTest(this, false, WAIT_TIMEOUT_MS));
112
113         mOnRequestExternalVideoSurface.waitForCallback(onRequestCallCount);
114         waitForVideoSizeChangeTo(mOnExternalVideoSurfacePositionChanged,
115                                  onPositionChangedCallCount, 150.0f, 150.0f);
116     }
117
118     @DisableHardwareAccelerationForTest
119     @SmallTest
120     @Feature({"AndroidWebView"})
121     public void testDisableVideoOverlayForEmbeddedVideo() throws Throwable {
122         setUpMockExternalVideoSurfaceContainer();
123
124         assertTrue(VideoTestUtil.runVideoTest(this, false, WAIT_TIMEOUT_MS));
125
126         assertEquals(0, mOnRequestExternalVideoSurface.getCallCount());
127         assertEquals(0, mOnExternalVideoSurfacePositionChanged.getCallCount());
128     }
129 }