Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
[Android] Add test case for onShow/HideCustomView.
Browse files Browse the repository at this point in the history
This patch is to add test for OnShowCustomView and onHideCustomView.
Some code was ported from android webview.
  • Loading branch information
wuhengzhi committed Nov 13, 2014
1 parent 9d5ed53 commit df2644f
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.chromium.base.ApplicationStatus;
import org.chromium.base.ApplicationStatus.ActivityStateListener;
import org.chromium.base.CommandLine;
import org.chromium.content.browser.ContentViewCore;
import org.xwalk.core.internal.extension.BuiltinXWalkExtensions;

/**
Expand Down Expand Up @@ -1095,4 +1096,9 @@ private File createImageFile() throws IOException {
);
return imageFile;
}

// For instrumentation test.
public ContentViewCore getXWalkContentForTest() {
return mContent.getContentViewCoreForTest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) 2014 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.core.internal.xwview.test;

import android.test.suitebuilder.annotation.MediumTest;

import junit.framework.Assert;

import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content.browser.test.util.DOMUtils;
import org.chromium.content.browser.test.util.TouchCommon;
import org.xwalk.core.internal.xwview.test.util.VideoTestWebServer;

/**
* Tests XWalkWebChromeClient::onShow/onHideCustomView.
*/
public class OnShowOnHideCustomViewTest extends XWalkViewInternalTestBase {
private VideoTestWebServer mWebServer;
private TestXWalkWebChromeClientBase mWebChromeClient;
private ContentViewCore mContentViewCore;

@Override
protected void setUp() throws Exception {
super.setUp();
mWebChromeClient = new TestXWalkWebChromeClientBase();
setXWalkWebChromeClient(mWebChromeClient);
mContentViewCore = getContentViewCore();
mWebServer = new VideoTestWebServer(getInstrumentation().getContext());
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
if (mWebServer != null) mWebServer.getTestWebServer().shutdown();
}

@MediumTest
@Feature({"onShow/onHideCustomView"})
public void testOnShowAndHideCustomViewWithCallback() throws Throwable {
doOnShowAndHideCustomViewTest(new Runnable() {
@Override
public void run() {
mWebChromeClient.getExitCallback().onCustomViewHidden();
}
});
}

@MediumTest
@Feature({"onShow/onHideCustomView"})
public void testOnShowAndHideCustomViewWithJavascript() throws Throwable {
doOnShowAndHideCustomViewTest(new Runnable() {
@Override
public void run() {
DOMUtils.exitFullscreen(mContentViewCore);
}
});
}

@MediumTest
@Feature({"onShow/onHideCustomView"})
public void testOnShowCustomViewAndPlayWithHtmlControl() throws Throwable {
doOnShowCustomViewTest();
Assert.assertFalse(DOMUtils.hasVideoEnded(mContentViewCore, VideoTestWebServer.VIDEO_ID));

// Click the html play button that is rendered above the video right in the middle
// of the custom view. Note that we're not able to get the precise location of the
// control since it is a shadow element, so this test might break if the location
// ever moves.
TouchCommon touchCommon = new TouchCommon(OnShowOnHideCustomViewTest.this);
touchCommon.singleClickView(mWebChromeClient.getCustomView());

Assert.assertTrue(DOMUtils.waitForEndOfVideo(
mContentViewCore, VideoTestWebServer.VIDEO_ID));
}

private void doOnShowAndHideCustomViewTest(final Runnable existFullscreen) throws Throwable {
doOnShowCustomViewTest();
getInstrumentation().runOnMainSync(existFullscreen);
mWebChromeClient.waitForCustomViewHidden();
}

private void doOnShowCustomViewTest() throws Exception {
loadTestPageAndClickFullscreen();
mWebChromeClient.waitForCustomViewShown();
}

private void loadTestPageAndClickFullscreen() throws Exception {
loadUrlSync(mWebServer.getFullScreenVideoTestURL());

// Click the button in full_screen_video_test.html to enter fullscreen.
TouchCommon touchCommon = new TouchCommon(this);
touchCommon.singleClickView(getXWalkView());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@
import android.content.Context;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebResourceResponse;
import android.widget.FrameLayout;

import java.io.InputStream;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.chromium.content.browser.ContentViewCore;
import org.chromium.content.browser.test.util.CallbackHelper;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
Expand All @@ -30,6 +37,7 @@
import org.xwalk.core.internal.XWalkSettings;
import org.xwalk.core.internal.XWalkUIClientInternal;
import org.xwalk.core.internal.XWalkViewInternal;
import org.xwalk.core.internal.XWalkWebChromeClient;

public class XWalkViewInternalTestBase
extends ActivityInstrumentationTestCase2<XWalkViewInternalTestRunnerActivity> {
Expand Down Expand Up @@ -98,6 +106,61 @@ public TestXWalkResourceClient() {
}
}

class TestXWalkWebChromeClientBase extends XWalkWebChromeClient {
private CallbackHelper mOnShowCustomViewCallbackHelper = new CallbackHelper();
private CallbackHelper mOnHideCustomViewCallbackHelper = new CallbackHelper();

private Activity mActivity = getActivity();
private View mCustomView;
private XWalkWebChromeClient.CustomViewCallback mExitCallback;

public TestXWalkWebChromeClientBase() {
super(mXWalkViewInternal);
}

@Override
public void onShowCustomView(View view, XWalkWebChromeClient.CustomViewCallback callback) {
mCustomView = view;
mExitCallback = callback;
mActivity.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

mActivity.getWindow().addContentView(view,
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
Gravity.CENTER));
mOnShowCustomViewCallbackHelper.notifyCalled();
}

@Override
public void onHideCustomView() {
mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
mOnHideCustomViewCallbackHelper.notifyCalled();
}

public XWalkWebChromeClient.CustomViewCallback getExitCallback() {
return mExitCallback;
}

public View getCustomView() {
return mCustomView;
}

public boolean wasCustomViewShownCalled() {
return mOnShowCustomViewCallbackHelper.getCallCount() > 0;
}

public void waitForCustomViewShown() throws TimeoutException, InterruptedException {
mOnShowCustomViewCallbackHelper.waitForCallback(0, 1, WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
}

public void waitForCustomViewHidden() throws InterruptedException, TimeoutException {
mOnHideCustomViewCallbackHelper.waitForCallback(0, 1, WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
}

void setUIClient(final XWalkUIClientInternal client) {
getInstrumentation().runOnMainSync(new Runnable() {
@Override
Expand All @@ -116,6 +179,15 @@ public void run() {
});
}

void setXWalkWebChromeClient(final TestXWalkWebChromeClientBase client) {
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mXWalkViewInternal.setXWalkWebChromeClient(client);
}
});
}

static class ViewPair {
private final XWalkViewInternal view0;
private final TestHelperBridge client0;
Expand Down Expand Up @@ -542,4 +614,13 @@ public String call() throws Exception {
}
});
}

protected ContentViewCore getContentViewCore() throws Exception {
return runTestOnUiThreadAndGetResult(new Callable<ContentViewCore>() {
@Override
public ContentViewCore call() throws Exception {
return mXWalkViewInternal.getXWalkContentForTest();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
// Copyright (c) 2014 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.core.internal.xwview.test.util;

import android.content.Context;
import android.util.Pair;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.chromium.net.test.util.TestWebServer;

/**
* This class is a WebServer provide video data.
*/
public class VideoTestWebServer {
// VIDEO_ID must be kept in sync with the id in full_screen_video_test.html.
public static final String VIDEO_ID = "video";
public static final String ONE_PIXEL_ONE_FRAME_WEBM_FILENAME = "one_pixel_one_frame.webm";
public static final String ONE_PIXEL_ONE_FRAME_WEBM_BASE64 =
"GkXfo0AgQoaBAUL3gQFC8oEEQvOBCEKCQAR3ZWJtQoeBAkKFgQIYU4BnQN8VSalmQCgq17FAAw9C" +
"QE2AQAZ3aGFtbXlXQUAGd2hhbW15RIlACECPQAAAAAAAFlSua0AxrkAu14EBY8WBAZyBACK1nEAD" +
"dW5khkAFVl9WUDglhohAA1ZQOIOBAeBABrCBlrqBlh9DtnVAdOeBAKNAboEAAIDyCACdASqWAJYA" +
"Pk0ci0WD+IBAAJiWlu4XdQTSq2H4MW0+sMO0gz8HMRe+0jRo0aNGjRo0aNGjRo0aNGjRo0aNGjRo" +
"0aNGjRo0aNGjRo0VAAD+/729RWRzH4mOZ9/O8Dl319afX4gsgAAA";

private String mOnePixelOneFrameWebmURL;
private String mFullScreenVideoTestURL;
private TestWebServer mTestWebServer;

public VideoTestWebServer(Context context) throws Exception {
mTestWebServer = TestWebServer.start();
List<Pair<String, String>> headers = getWebmHeaders(true);
mOnePixelOneFrameWebmURL = mTestWebServer.setResponseBase64("/" +
ONE_PIXEL_ONE_FRAME_WEBM_FILENAME,
ONE_PIXEL_ONE_FRAME_WEBM_BASE64, headers);
initFullScreenVideoTest(context);
}

/**
* @return the mOnePixelOneFrameWebmURL
*/
public String getOnePixelOneFrameWebmURL() {
return mOnePixelOneFrameWebmURL;
}

public String getFullScreenVideoTestURL() {
return mFullScreenVideoTestURL;
}

public TestWebServer getTestWebServer() {
return mTestWebServer;
}

private static List<Pair<String, String>> getWebmHeaders(boolean disableCache) {
return CommonResources.getContentTypeAndCacheHeaders("video/webm", disableCache);
}

private static List<Pair<String, String>> getHTMLHeaders(boolean disableCache) {
return CommonResources.getContentTypeAndCacheHeaders("text/html", disableCache);
}

private void initFullScreenVideoTest(Context context) throws IOException {
final String fullScreenVideoPath = "full_screen_video_test.html";
String data = loadAssetData(context, fullScreenVideoPath);
mFullScreenVideoTestURL = mTestWebServer.setResponse("/" + fullScreenVideoPath,
data.replace("VIDEO_FILE_URL", getOnePixelOneFrameWebmURL()),
getHTMLHeaders(false));
}

private String loadAssetData(Context context, String asset) throws IOException {
InputStream in = context.getAssets().open(asset);
ByteArrayOutputStream os = new ByteArrayOutputStream();
int bufferLength = 128;
byte[] buffer = new byte[bufferLength];
int len = in.read(buffer, 0, bufferLength);
while (len != -1) {
os.write(buffer, 0, len);
if (len < bufferLength) break;
len = in.read(buffer, 0, bufferLength);
}
return os.toString();
}
}
30 changes: 30 additions & 0 deletions test/android/data/full_screen_video_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<html>
<head>
<script>
function goFullscreen(id) {
var element = document.getElementById(id);
if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
}
}

addEventListener('DOMContentLoaded', function() {
document.getElementById('video').addEventListener('play', function() {
console.log('Video Played');
}, false);
document.addEventListener('webkitfullscreenerror', function() {
javaFullScreenErrorObserver.notifyJava();
}, false);
}, false);
</script>
</head>
<body>
</script></head><body>
<button autofocus style ='padding:1024px 1024px;' onclick="goFullscreen('video'); return false">Big enough you can't miss it</button>
<p></p>
<!-- The video id must be kept in sync with VideoTestWebServer.VIDEO_ID -->
<video style = 'width: 10px; height: 10px;' id='video' controls>
<source id="webm" src="VIDEO_FILE_URL" type="video/webm">
</video>
</body>
</html>
2 changes: 2 additions & 0 deletions xwalk_android_tests.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@
'<(PRODUCT_DIR)/xwalk_internal_xwview_test/assets/echo.html',
'<(PRODUCT_DIR)/xwalk_internal_xwview_test/assets/echoSync.html',
'<(PRODUCT_DIR)/xwalk_internal_xwview_test/assets/framesEcho.html',
'<(PRODUCT_DIR)/xwalk_internal_xwview_test/assets/full_screen_video_test.html',
'<(PRODUCT_DIR)/xwalk_internal_xwview_test/assets/geolocation.html',
'<(PRODUCT_DIR)/xwalk_internal_xwview_test/assets/index.html',
'<(PRODUCT_DIR)/xwalk_internal_xwview_test/assets/navigator.online.html',
Expand All @@ -665,6 +666,7 @@
'test/android/data/echo.html',
'test/android/data/echoSync.html',
'test/android/data/framesEcho.html',
'test/android/data/full_screen_video_test.html',
'test/android/data/geolocation.html',
'test/android/data/index.html',
'test/android/data/navigator.online.html',
Expand Down

0 comments on commit df2644f

Please sign in to comment.