1// Copyright 2013 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.test.suitebuilder.annotation.MediumTest;
8
9import junit.framework.Assert;
10
11import org.chromium.android_webview.test.util.JavascriptEventObserver;
12import org.chromium.android_webview.test.util.VideoTestWebServer;
13import org.chromium.base.test.util.Feature;
14import org.chromium.content.browser.ContentViewCore;
15import org.chromium.content.browser.test.util.DOMUtils;
16import org.chromium.content.browser.test.util.TouchCommon;
17
18/**
19 * Test WebChromeClient::onShow/HideCustomView.
20 */
21public class AwContentsClientFullScreenVideoTest extends AwTestBase {
22    private FullScreenVideoTestAwContentsClient mContentsClient;
23    private ContentViewCore mContentViewCore;
24    private VideoTestWebServer mWebServer;
25    private AwTestContainerView mTestContainerView;
26
27    @Override
28    protected void setUp() throws Exception {
29        super.setUp();
30        mContentsClient = new FullScreenVideoTestAwContentsClient(getActivity());
31        mTestContainerView =
32                createAwTestContainerViewOnMainSync(mContentsClient);
33        mContentViewCore = mTestContainerView.getContentViewCore();
34        enableJavaScriptOnUiThread(mTestContainerView.getAwContents());
35        mTestContainerView.getAwContents().getSettings().setFullscreenSupported(true);
36        mWebServer = new VideoTestWebServer(
37                getInstrumentation().getTargetContext());
38    }
39
40    @Override
41    protected void tearDown() throws Exception {
42        super.tearDown();
43        if (mWebServer != null) mWebServer.getTestWebServer().shutdown();
44    }
45
46    @MediumTest
47    @Feature({"AndroidWebView"})
48    public void testOnShowAndHideCustomViewWithCallback() throws Throwable {
49        doOnShowAndHideCustomViewTest(new Runnable() {
50            @Override
51            public void run() {
52                mContentsClient.getExitCallback().onCustomViewHidden();
53            }
54        });
55    }
56
57    @MediumTest
58    @Feature({"AndroidWebView"})
59    public void testOnShowAndHideCustomViewWithJavascript() throws Throwable {
60        doOnShowAndHideCustomViewTest(new Runnable() {
61            @Override
62            public void run() {
63                DOMUtils.exitFullscreen(mContentViewCore);
64            }
65        });
66    }
67
68    @MediumTest
69    @Feature({"AndroidWebView"})
70    public void testOnShowCustomViewAndPlayWithHtmlControl() throws Throwable {
71        doOnShowCustomViewTest();
72        Assert.assertFalse(DOMUtils.hasVideoEnded(
73                mContentViewCore, VideoTestWebServer.VIDEO_ID));
74
75        // Click the html play button that is rendered above the video right in the middle
76        // of the custom view. Note that we're not able to get the precise location of the
77        // control since it is a shadow element, so this test might break if the location
78        // ever moves.
79        TouchCommon touchCommon = new TouchCommon(
80                AwContentsClientFullScreenVideoTest.this);
81        touchCommon.singleClickView(mContentsClient.getCustomView());
82
83        Assert.assertTrue(DOMUtils.waitForEndOfVideo(
84                mContentViewCore, VideoTestWebServer.VIDEO_ID));
85    }
86
87    @MediumTest
88    @Feature({"AndroidWebView"})
89    public void testFullscreenNotSupported() throws Throwable {
90        mTestContainerView.getAwContents().getSettings().setFullscreenSupported(false);
91
92        final JavascriptEventObserver fullScreenErrorObserver = new JavascriptEventObserver();
93        getInstrumentation().runOnMainSync(new Runnable() {
94            @Override
95            public void run() {
96                fullScreenErrorObserver.register(mContentViewCore, "javaFullScreenErrorObserver");
97            }
98        });
99
100        loadTestPageAndClickFullscreen();
101
102        Assert.assertTrue(fullScreenErrorObserver.waitForEvent(500));
103        Assert.assertFalse(mContentsClient.wasCustomViewShownCalled());
104    }
105
106    private void doOnShowAndHideCustomViewTest(final Runnable existFullscreen)
107            throws Throwable {
108        doOnShowCustomViewTest();
109        getInstrumentation().runOnMainSync(existFullscreen);
110        mContentsClient.waitForCustomViewHidden();
111    }
112
113    private void doOnShowCustomViewTest() throws Exception {
114        loadTestPageAndClickFullscreen();
115        mContentsClient.waitForCustomViewShown();
116    }
117
118    private void loadTestPageAndClickFullscreen() throws Exception {
119        loadUrlSync(mTestContainerView.getAwContents(),
120                mContentsClient.getOnPageFinishedHelper(),
121                mWebServer.getFullScreenVideoTestURL());
122
123        // Click the button in full_screen_video_test.html to enter fullscreen.
124        TouchCommon touchCommon = new TouchCommon(this);
125        touchCommon.singleClickView(mTestContainerView);
126    }
127}
128