ExternalVideoSurfaceContainerTest.java revision 010d83a9304c5a91596085d917d248abff47903a
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
5package org.chromium.android_webview.test;
6
7import android.graphics.RectF;
8import android.test.suitebuilder.annotation.SmallTest;
9
10import org.chromium.android_webview.ExternalVideoSurfaceContainer;
11import org.chromium.android_webview.test.util.VideoTestUtil;
12import org.chromium.base.CommandLine;
13import org.chromium.base.test.util.DisabledTest;
14import org.chromium.base.test.util.Feature;
15import org.chromium.content.browser.ContentViewCore;
16import org.chromium.content.browser.test.util.CallbackHelper;
17
18/**
19 * A test suite for ExternalVideoSurfaceContainerTest.
20 */
21public class ExternalVideoSurfaceContainerTest extends AwTestBase {
22
23    // Callback helper to track the position/size of the external surface.
24    private static class OnExternalVideoSurfacePositionChanged extends CallbackHelper {
25        private RectF mRect;
26
27        public RectF getRectF() {
28            return mRect;
29        }
30
31        public void notifyCalled(RectF rect) {
32            mRect = rect;
33            notifyCalled();
34        }
35    }
36    private OnExternalVideoSurfacePositionChanged mOnExternalVideoSurfacePositionChanged =
37            new OnExternalVideoSurfacePositionChanged();
38    private CallbackHelper mOnRequestExternalVideoSurface = new CallbackHelper();
39
40    private static void waitForVideoSizeChangeTo(OnExternalVideoSurfacePositionChanged helper,
41            int callCount, float widthCss, float heightCss) throws Exception {
42        final int maxSizeChangeNotificationToWaitFor = 5;
43        final float epsilon = 0.000001f;
44        for (int i = 1; i <= maxSizeChangeNotificationToWaitFor; ++i) {
45            helper.waitForCallback(callCount, i);
46            RectF rect = helper.getRectF();
47            if (Math.abs(rect.width() - widthCss) < epsilon
48                    && Math.abs(rect.height() - heightCss) < epsilon) {
49                break;
50            }
51            assertTrue(i < maxSizeChangeNotificationToWaitFor);
52        }
53    }
54
55    private class MockExternalVideoSurfaceContainer extends ExternalVideoSurfaceContainer {
56        private int mPlayerId = INVALID_PLAYER_ID;
57
58        public MockExternalVideoSurfaceContainer(
59                long nativeExternalVideoSurfaceContainer, ContentViewCore contentViewCore) {
60            super(nativeExternalVideoSurfaceContainer, contentViewCore);
61        }
62
63        @Override
64        protected void requestExternalVideoSurface(int playerId) {
65            mPlayerId = playerId;
66            mOnRequestExternalVideoSurface.notifyCalled();
67        }
68
69        @Override
70        protected void releaseExternalVideoSurface(int playerId) {
71            mPlayerId = INVALID_PLAYER_ID;
72        }
73
74        @Override
75        protected void destroy() {}
76
77        @Override
78        protected void onExternalVideoSurfacePositionChanged(
79                int playerId, float left, float top, float right, float bottom) {
80            if (mPlayerId != playerId) {
81                return;
82            }
83            mOnExternalVideoSurfacePositionChanged.notifyCalled(
84                    new RectF(left, top, right, bottom));
85        }
86
87        @Override
88        protected void onFrameInfoUpdated() {}
89    }
90
91    private void setUpMockExternalVideoSurfaceContainer() {
92        ExternalVideoSurfaceContainer.setFactory(new ExternalVideoSurfaceContainer.Factory() {
93            @Override
94            public ExternalVideoSurfaceContainer create(
95                    long nativeContainer, ContentViewCore contentViewCore) {
96                return new MockExternalVideoSurfaceContainer(nativeContainer, contentViewCore);
97            }
98        });
99    }
100
101    /*
102    @SmallTest
103    @Feature({"AndroidWebView"})
104    TODO(ycheo): Enable this after turing on the flag 'video_hole=1'.
105    */
106    @DisabledTest
107    public void testEnableVideoOverlayForEmbeddedVideo() throws Throwable {
108        setUpMockExternalVideoSurfaceContainer();
109
110        CommandLine.getInstance().appendSwitch("force-use-overlay-embedded-video");
111
112        int onRequestCallCount = mOnRequestExternalVideoSurface.getCallCount();
113        int onPositionChangedCallCount = mOnExternalVideoSurfacePositionChanged.getCallCount();
114
115        assertTrue(VideoTestUtil.runVideoTest(this, false, WAIT_TIMEOUT_MS));
116
117        mOnRequestExternalVideoSurface.waitForCallback(onRequestCallCount);
118        waitForVideoSizeChangeTo(mOnExternalVideoSurfacePositionChanged,
119                                 onPositionChangedCallCount, 150.0f, 150.0f);
120    }
121
122    @SmallTest
123    @Feature({"AndroidWebView"})
124    public void testDisableVideoOverlayForEmbeddedVideo() throws Throwable {
125        setUpMockExternalVideoSurfaceContainer();
126
127        assertTrue(VideoTestUtil.runVideoTest(this, false, WAIT_TIMEOUT_MS));
128
129        assertEquals(0, mOnRequestExternalVideoSurface.getCallCount());
130        assertEquals(0, mOnExternalVideoSurfacePositionChanged.getCallCount());
131    }
132}
133