Commit 9385a899 authored by Yoland Yan's avatar Yoland Yan Committed by Commit Bot

Convert Android Overlay test to JUnit4

Bug: 640116
Change-Id: I74053b1b4bcb37553a72305ece82703453354586
Reviewed-on: https://chromium-review.googlesource.com/636171
Commit-Queue: Yoland Yan <yolandyan@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Reviewed-by: default avatarFrank Liberato <liberato@chromium.org>
Cr-Commit-Position: refs/heads/master@{#501484}
parent 17fcfc1b
......@@ -465,7 +465,7 @@ android_library("content_javatests") {
"javatests/src/org/chromium/content/browser/accessibility/captioning/CaptioningChangeDelegateTest.java",
"javatests/src/org/chromium/content/browser/androidoverlay/DialogOverlayImplPixelTest.java",
"javatests/src/org/chromium/content/browser/androidoverlay/DialogOverlayImplTest.java",
"javatests/src/org/chromium/content/browser/androidoverlay/DialogOverlayImplTestBase.java",
"javatests/src/org/chromium/content/browser/androidoverlay/DialogOverlayImplTestRule.java",
"javatests/src/org/chromium/content/browser/crypto/CipherFactoryTest.java",
"javatests/src/org/chromium/content/browser/input/CursorAnchorInfoControllerTest.java",
"javatests/src/org/chromium/content/browser/input/ImeLollipopTest.java",
......
......@@ -10,27 +10,39 @@ import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import android.view.Surface;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.MinAndroidSdkLevel;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content.browser.androidoverlay.DialogOverlayImplTestRule.Client;
import java.util.concurrent.Callable;
/**
* Pixel tests for DialogOverlayImpl. These use UiAutomation, so they only run in JB or above.
* TODO(liberato): Convert to junit4.
*/
@RunWith(BaseJUnit4ClassRunner.class)
@MinAndroidSdkLevel(Build.VERSION_CODES.JELLY_BEAN_MR2)
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class DialogOverlayImplPixelTest extends DialogOverlayImplTestBase {
public class DialogOverlayImplPixelTest {
// Color that we'll fill the overlay with.
@Rule
public DialogOverlayImplTestRule mActivityTestRule =
new DialogOverlayImplTestRule(TEST_PAGE_DATA_URL);
private static final int OVERLAY_FILL_COLOR = Color.BLUE;
// CSS coordinates of a div that we'll try to cover with an overlay.
......@@ -85,20 +97,14 @@ public class DialogOverlayImplPixelTest extends DialogOverlayImplTestBase {
// Screenshot of the test page, before we do anything.
Bitmap mInitialScreenshot;
@Override
protected String getInitialUrl() {
return TEST_PAGE_DATA_URL;
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
takeScreenshotOfBackground();
}
// Take a screenshot via UiAutomation, which captures all overlays.
Bitmap takeScreenshot() {
return getInstrumentation().getUiAutomation().takeScreenshot();
return InstrumentationRegistry.getInstrumentation().getUiAutomation().takeScreenshot();
}
// Fill |surface| with OVERLAY_FILL_COLOR and return a screenshot. Note that we have no idea
......@@ -113,7 +119,7 @@ public class DialogOverlayImplPixelTest extends DialogOverlayImplTestBase {
}
int convertCSSToScreenPixels(int css) {
ContentViewCore cvc = getContentViewCore();
ContentViewCore cvc = mActivityTestRule.getContentViewCore();
return (int) (css * cvc.getPageScaleFactor() * cvc.getDeviceScaleFactor());
}
......@@ -221,7 +227,7 @@ public class DialogOverlayImplPixelTest extends DialogOverlayImplTestBase {
// Wait for |overlay| to become ready, get its surface, and return it.
Surface waitForSurface(DialogOverlayImpl overlay) throws Exception {
Assert.assertNotNull(overlay);
final Client.Event event = getClient().nextEvent();
final Client.Event event = mActivityTestRule.getClient().nextEvent();
Assert.assertTrue(event.surfaceKey > 0);
return ThreadUtils.runOnUiThreadBlocking(new Callable<Surface>() {
@Override
......@@ -231,22 +237,24 @@ public class DialogOverlayImplPixelTest extends DialogOverlayImplTestBase {
});
}
@Test
@MediumTest
@Feature({"AndroidOverlay"})
public void testInitialPosition() throws Exception {
// Test that the initial position supplied for the overlay covers the <div> we created.
final DialogOverlayImpl overlay =
createOverlay(mDivXPx, mDivYPx, mDivWidthPx, mDivHeightPx);
mActivityTestRule.createOverlay(mDivXPx, mDivYPx, mDivWidthPx, mDivHeightPx);
Surface surface = waitForSurface(overlay);
assertDivIsExactlyCovered(surface);
}
@Test
@MediumTest
@Feature({"AndroidOverlay"})
public void testScheduleLayout() throws Exception {
// Test that scheduleLayout() moves the overlay to cover the <div>.
final DialogOverlayImpl overlay = createOverlay(0, 0, 10, 10);
final DialogOverlayImpl overlay = mActivityTestRule.createOverlay(0, 0, 10, 10);
Surface surface = waitForSurface(overlay);
final org.chromium.gfx.mojom.Rect rect = new org.chromium.gfx.mojom.Rect();
......
......@@ -7,37 +7,42 @@ package org.chromium.content.browser.androidoverlay;
import android.support.test.filters.SmallTest;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.androidoverlay.DialogOverlayImplTestRule.Client;
/**
* Tests for DialogOverlayImpl.
* TODO(liberato): Convert to junit4.
*/
public class DialogOverlayImplTest extends DialogOverlayImplTestBase {
@RunWith(BaseJUnit4ClassRunner.class)
public class DialogOverlayImplTest {
private static final String BLANK_URL = "about://blank";
@Override
protected String getInitialUrl() {
return BLANK_URL;
}
@Rule
public DialogOverlayImplTestRule mActivityTestRule =
new DialogOverlayImplTestRule(BLANK_URL);
@Test
@SmallTest
@Feature({"AndroidOverlay"})
public void testCreateDestroyOverlay() {
Assert.assertFalse(getClient().hasReceivedOverlayModeChange());
Assert.assertFalse(getClient().isUsingOverlayMode());
Assert.assertFalse(mActivityTestRule.getClient().hasReceivedOverlayModeChange());
Assert.assertFalse(mActivityTestRule.getClient().isUsingOverlayMode());
final DialogOverlayImpl overlay = createOverlay(0, 0, 10, 10);
final DialogOverlayImpl overlay = mActivityTestRule.createOverlay(0, 0, 10, 10);
// We should get a new overlay with a valid surface key.
Client.Event event = getClient().nextEvent();
Client.Event event = mActivityTestRule.getClient().nextEvent();
Assert.assertEquals(Client.SURFACE_READY, event.which);
Assert.assertTrue(event.surfaceKey > 0);
Assert.assertTrue(getClient().hasReceivedOverlayModeChange());
Assert.assertTrue(getClient().isUsingOverlayMode());
Assert.assertTrue(mActivityTestRule.getClient().hasReceivedOverlayModeChange());
Assert.assertTrue(mActivityTestRule.getClient().isUsingOverlayMode());
// Close the overlay, and make sure that the provider is notified.
// Note that we should not get a 'destroyed' message when we close it.
......@@ -47,67 +52,71 @@ public class DialogOverlayImplTest extends DialogOverlayImplTestBase {
overlay.close();
}
});
Assert.assertEquals(Client.RELEASED, getClient().nextEvent().which);
Assert.assertFalse(getClient().isUsingOverlayMode());
Assert.assertEquals(Client.RELEASED, mActivityTestRule.getClient().nextEvent().which);
Assert.assertFalse(mActivityTestRule.getClient().isUsingOverlayMode());
}
@Test
@SmallTest
@Feature({"AndroidOverlay"})
public void testCreateOverlayFailsIfUnknownRoutingToken() {
// Try to create an overlay with a bad routing token.
mRoutingToken.high++;
DialogOverlayImpl overlay = createOverlay(0, 0, 10, 10);
mActivityTestRule.incrementUnguessableTokenHigh();
DialogOverlayImpl overlay = mActivityTestRule.createOverlay(0, 0, 10, 10);
Assert.assertNotNull(overlay);
// We should be notified that the overlay is destroyed.
Client.Event event = getClient().nextEvent();
Client.Event event = mActivityTestRule.getClient().nextEvent();
Assert.assertEquals(Client.DESTROYED, event.which);
}
@Test
@SmallTest
@Feature({"AndroidOverlay"})
public void testCreateOverlayFailsIfWebContentsHidden() {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
getWebContents().onHide();
mActivityTestRule.getWebContents().onHide();
}
});
DialogOverlayImpl overlay = createOverlay(0, 0, 10, 10);
DialogOverlayImpl overlay = mActivityTestRule.createOverlay(0, 0, 10, 10);
Assert.assertNotNull(overlay);
// We should be notified that the overlay is destroyed.
Client.Event event = getClient().nextEvent();
Client.Event event = mActivityTestRule.getClient().nextEvent();
Assert.assertEquals(Client.DESTROYED, event.which);
}
@Test
@SmallTest
@Feature({"AndroidOverlay"})
public void testHiddingWebContentsDestroysOverlay() {
DialogOverlayImpl overlay = createOverlay(0, 0, 10, 10);
DialogOverlayImpl overlay = mActivityTestRule.createOverlay(0, 0, 10, 10);
Assert.assertNotNull(overlay);
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
getWebContents().onHide();
mActivityTestRule.getWebContents().onHide();
}
});
// We should be notified that the overlay is destroyed.
Client.Event event = getClient().nextEvent();
Client.Event event = mActivityTestRule.getClient().nextEvent();
Assert.assertEquals(Client.DESTROYED, event.which);
}
@Test
@SmallTest
@Feature({"AndroidOverlay"})
public void testScheduleLayoutDoesntCrash() {
// Make sure that we don't get any messages due to scheduleLayout, and we don't crash.
final DialogOverlayImpl overlay = createOverlay(0, 0, 10, 10);
final DialogOverlayImpl overlay = mActivityTestRule.createOverlay(0, 0, 10, 10);
// Wait for the surface.
Assert.assertEquals(Client.SURFACE_READY, getClient().nextEvent().which);
Assert.assertEquals(Client.SURFACE_READY, mActivityTestRule.getClient().nextEvent().which);
final org.chromium.gfx.mojom.Rect rect = new org.chromium.gfx.mojom.Rect();
rect.x = 100;
rect.y = 200;
......@@ -121,29 +130,31 @@ public class DialogOverlayImplTest extends DialogOverlayImplTestBase {
});
// No additional messages should have arrived.
Assert.assertTrue(getClient().isEmpty());
Assert.assertTrue(mActivityTestRule.getClient().isEmpty());
}
@Test
@SmallTest
@Feature({"AndroidOverlay"})
public void testCreateSecureSurface() {
// Test that creating a secure overlay creates an overlay. We can't really tell if it's
// secure or not, until we can do a screen shot test.
mSecure = true;
final DialogOverlayImpl overlay = createOverlay(0, 0, 10, 10);
mActivityTestRule.setSecure(true);
final DialogOverlayImpl overlay = mActivityTestRule.createOverlay(0, 0, 10, 10);
Assert.assertNotNull(overlay);
// We should get a new overlay with a valid surface key.
Client.Event event = getClient().nextEvent();
Client.Event event = mActivityTestRule.getClient().nextEvent();
Assert.assertEquals(Client.SURFACE_READY, event.which);
Assert.assertTrue(event.surfaceKey > 0);
}
@Test
@SmallTest
@Feature({"AndroidOverlay"})
public void testCloseOnlyClosesOnce() {
// Test that trying to close an overlay more than once doesn't actually do anything.
final DialogOverlayImpl overlay = createOverlay(0, 0, 10, 10);
final DialogOverlayImpl overlay = mActivityTestRule.createOverlay(0, 0, 10, 10);
// The first should generate RELEASED
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
......@@ -151,15 +162,15 @@ public class DialogOverlayImplTest extends DialogOverlayImplTestBase {
overlay.close();
}
});
Assert.assertEquals(Client.RELEASED, getClient().nextEvent().which);
Assert.assertEquals(Client.RELEASED, mActivityTestRule.getClient().nextEvent().which);
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
overlay.close();
getClient().injectMarkerEvent();
mActivityTestRule.getClient().injectMarkerEvent();
}
});
Assert.assertEquals(Client.TEST_MARKER, getClient().nextEvent().which);
Assert.assertEquals(Client.TEST_MARKER, mActivityTestRule.getClient().nextEvent().which);
}
}
......@@ -8,11 +8,13 @@ import android.os.Handler;
import android.os.HandlerThread;
import org.junit.Assert;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.chromium.base.Callback;
import org.chromium.base.ThreadUtils;
import org.chromium.content.browser.framehost.RenderFrameHostImpl;
import org.chromium.content_shell_apk.ContentShellTestBase;
import org.chromium.content_shell_apk.ContentShellActivityTestRule;
import org.chromium.media.mojom.AndroidOverlayClient;
import org.chromium.media.mojom.AndroidOverlayConfig;
import org.chromium.mojo.common.mojom.UnguessableToken;
......@@ -23,9 +25,9 @@ import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
/**
* Base class for tests for DialogOverlayImpl.
* TestRule for tests for DialogOverlayImpl.
*/
public abstract class DialogOverlayImplTestBase extends ContentShellTestBase {
public class DialogOverlayImplTestRule extends ContentShellActivityTestRule {
// overlay-ui thread.
private HandlerThread mOverlayUiThread;
private Handler mOverlayUiHandler;
......@@ -35,10 +37,12 @@ public abstract class DialogOverlayImplTestBase extends ContentShellTestBase {
// The routing token that we'll use to create overlays. This may be modified by the tests prior
// to calling createOverlay().
protected UnguessableToken mRoutingToken;
private UnguessableToken mRoutingToken;
// True if we should create a secure overlay.
protected boolean mSecure;
private boolean mSecure;
private String mInitialUrl;
/**
* AndroidOverlay client that supports waiting operations for callbacks. One may call
......@@ -152,57 +156,75 @@ public abstract class DialogOverlayImplTestBase extends ContentShellTestBase {
private Client mClient = new Client();
// Return the URL to start with.
protected abstract String getInitialUrl();
public DialogOverlayImplTestRule(String url) {
mInitialUrl = url;
}
protected Client getClient() {
public String getInitialUrl() {
return mInitialUrl;
}
public Client getClient() {
return mClient;
}
@Override
public void setUp() throws Exception {
super.setUp();
public void setSecure(boolean secure) {
mSecure = secure;
}
public void incrementUnguessableTokenHigh() {
mRoutingToken.high++;
}
launchContentShellWithUrl(getInitialUrl());
waitForActiveShellToBeDoneLoading(); // Do we need this?
// Fetch the routing token.
mRoutingToken =
ThreadUtils.runOnUiThreadBlockingNoException(new Callable<UnguessableToken>() {
@Override
public Statement apply(final Statement base, Description desc) {
return super.apply(new Statement() {
@Override
public void evaluate() throws Throwable {
launchContentShellWithUrl(getInitialUrl());
waitForActiveShellToBeDoneLoading(); // Do we need this?
// Fetch the routing token.
mRoutingToken = ThreadUtils.runOnUiThreadBlockingNoException(
new Callable<UnguessableToken>() {
@Override
public UnguessableToken call() {
RenderFrameHostImpl host =
(RenderFrameHostImpl) getWebContents().getMainFrame();
org.chromium.base.UnguessableToken routingToken =
host.getAndroidOverlayRoutingToken();
UnguessableToken mojoToken = new UnguessableToken();
mojoToken.high = routingToken.getHighForSerialization();
mojoToken.low = routingToken.getLowForSerialization();
return mojoToken;
}
});
// Set up the overlay UI thread
mOverlayUiThread = new HandlerThread("TestOverlayUI");
mOverlayUiThread.start();
mOverlayUiHandler = new Handler(mOverlayUiThread.getLooper());
// Just delegate to |mClient| when an overlay is released.
mReleasedRunnable = new Runnable() {
@Override
public UnguessableToken call() {
RenderFrameHostImpl host =
(RenderFrameHostImpl) getWebContents().getMainFrame();
org.chromium.base.UnguessableToken routingToken =
host.getAndroidOverlayRoutingToken();
UnguessableToken mojoToken = new UnguessableToken();
mojoToken.high = routingToken.getHighForSerialization();
mojoToken.low = routingToken.getLowForSerialization();
return mojoToken;
public void run() {
mClient.notifyReleased();
}
});
// Set up the overlay UI thread
mOverlayUiThread = new HandlerThread("TestOverlayUI");
mOverlayUiThread.start();
mOverlayUiHandler = new Handler(mOverlayUiThread.getLooper());
};
// Just delegate to |mClient| when an overlay is released.
mReleasedRunnable = new Runnable() {
@Override
public void run() {
mClient.notifyReleased();
}
};
Callback<Boolean> overlayModeChanged = new Callback<Boolean>() {
@Override
public void onResult(Boolean useOverlayMode) {
mClient.onOverlayModeChanged(useOverlayMode);
}
};
Callback<Boolean> overlayModeChanged = new Callback<Boolean>() {
@Override
public void onResult(Boolean useOverlayMode) {
mClient.onOverlayModeChanged(useOverlayMode);
getActivityForTestCommon().getActiveShell().setOverayModeChangedCallbackForTesting(
overlayModeChanged);
}
};
getActivityForTestCommon().getActiveShell().setOverayModeChangedCallbackForTesting(
overlayModeChanged);
}, desc);
}
// Create an overlay with the given parameters and return it.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment