Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / android_webview / javatests / src / org / chromium / android_webview / test / FullScreenVideoTestAwContentsClient.java
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
5 package org.chromium.android_webview.test;
6
7 import android.app.Activity;
8 import android.view.Gravity;
9 import android.view.KeyEvent;
10 import android.view.View;
11 import android.view.ViewGroup;
12 import android.view.WindowManager;
13 import android.webkit.WebChromeClient;
14 import android.widget.FrameLayout;
15
16 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
17
18 import org.chromium.content.browser.test.util.CallbackHelper;
19
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22
23 /**
24  * This class is a AwContentsClient for full screen video test.
25  */
26 public class FullScreenVideoTestAwContentsClient extends TestAwContentsClient {
27     public static final long WAITING_SECONDS = scaleTimeout(20);
28     private CallbackHelper mOnShowCustomViewCallbackHelper = new CallbackHelper();
29     private CallbackHelper mOnHideCustomViewCallbackHelper = new CallbackHelper();
30     private CallbackHelper mOnUnhandledKeyUpEventCallbackHelper = new CallbackHelper();
31
32     private final Activity mActivity;
33     private final boolean mAllowHardwareAcceleration;
34     private View mCustomView;
35     private WebChromeClient.CustomViewCallback mExitCallback;
36
37     public FullScreenVideoTestAwContentsClient(Activity activity,
38             boolean allowHardwareAcceleration) {
39         mActivity = activity;
40         mAllowHardwareAcceleration = allowHardwareAcceleration;
41     }
42
43     @Override
44     public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
45         mCustomView = view;
46         if (!mAllowHardwareAcceleration) {
47             // The hardware emulation in the testing infrastructure is not perfect, and this is
48             // required to work-around some of the limitations.
49             mCustomView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
50         }
51         mExitCallback = callback;
52         mActivity.getWindow().setFlags(
53                 WindowManager.LayoutParams.FLAG_FULLSCREEN,
54                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
55
56         mActivity.getWindow().addContentView(view,
57                 new FrameLayout.LayoutParams(
58                         ViewGroup.LayoutParams.MATCH_PARENT,
59                         ViewGroup.LayoutParams.MATCH_PARENT,
60                         Gravity.CENTER));
61         mOnShowCustomViewCallbackHelper.notifyCalled();
62     }
63
64     @Override
65     public void onHideCustomView() {
66         mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
67         mOnHideCustomViewCallbackHelper.notifyCalled();
68     }
69
70     public WebChromeClient.CustomViewCallback getExitCallback() {
71         return mExitCallback;
72     }
73
74     @Override
75     public void onUnhandledKeyEvent(KeyEvent event) {
76         if (event.getAction() == KeyEvent.ACTION_UP) {
77             mOnUnhandledKeyUpEventCallbackHelper.notifyCalled();
78         }
79     }
80
81     public boolean wasOnUnhandledKeyUpEventCalled() {
82         return mOnUnhandledKeyUpEventCallbackHelper.getCallCount() > 0;
83     }
84
85     public View getCustomView() {
86         return mCustomView;
87     }
88
89     public boolean wasCustomViewShownCalled() {
90         return mOnShowCustomViewCallbackHelper.getCallCount() > 0;
91     }
92
93     public void waitForCustomViewShown() throws TimeoutException, InterruptedException {
94         mOnShowCustomViewCallbackHelper.waitForCallback(0, 1, WAITING_SECONDS, TimeUnit.SECONDS);
95     }
96
97     public void waitForCustomViewHidden() throws InterruptedException, TimeoutException {
98         mOnHideCustomViewCallbackHelper.waitForCallback(0, 1, WAITING_SECONDS, TimeUnit.SECONDS);
99     }
100 }