Commit 2c657c64 authored by yolandyan's avatar yolandyan Committed by Commit bot

Auto convert content shell tests to JUnit4

This uses auto convert script to convert content instrumentation tests to JUnit4,
assisted with some manual clean up. For more on JUnit4 migration, check
src/testing/android/docs/junit4.md

BUG=640116

Review-Url: https://codereview.chromium.org/2708243004
Cr-Commit-Position: refs/heads/master@{#456227}
parent fead74a8
...@@ -6,16 +6,27 @@ package org.chromium.content.browser; ...@@ -6,16 +6,27 @@ package org.chromium.content.browser;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
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.CommandLine; import org.chromium.base.CommandLine;
import org.chromium.base.annotations.SuppressFBWarnings; import org.chromium.base.annotations.SuppressFBWarnings;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.NativeLibraryTestBase; import org.chromium.content.browser.test.NativeLibraryTestRule;
import org.chromium.content_shell_apk.ContentShellApplication; import org.chromium.content_shell_apk.ContentShellApplication;
/** /**
* Test class for command lines. * Test class for command lines.
*/ */
public class ContentCommandLineTest extends NativeLibraryTestBase { @RunWith(BaseJUnit4ClassRunner.class)
public class ContentCommandLineTest {
@Rule
public NativeLibraryTestRule mActivityTestRule = new NativeLibraryTestRule();
// A reference command line. Note that switch2 is [brea\d], switch3 is [and "butter"], // A reference command line. Note that switch2 is [brea\d], switch3 is [and "butter"],
// and switch4 is [a "quoted" 'food'!] // and switch4 is [a "quoted" 'food'!]
static final String INIT_SWITCHES[] = { "init_command", "--switch", "Arg", static final String INIT_SWITCHES[] = { "init_command", "--switch", "Arg",
...@@ -33,65 +44,65 @@ public class ContentCommandLineTest extends NativeLibraryTestBase { ...@@ -33,65 +44,65 @@ public class ContentCommandLineTest extends NativeLibraryTestBase {
static final String CL_ADDED_SWITCH_2 = "username"; static final String CL_ADDED_SWITCH_2 = "username";
static final String CL_ADDED_VALUE_2 = "bozo"; static final String CL_ADDED_VALUE_2 = "bozo";
@Override @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
CommandLine.reset(); CommandLine.reset();
} }
void loadJni() { void loadJni() {
assertFalse(CommandLine.getInstance().isNativeImplementation()); Assert.assertFalse(CommandLine.getInstance().isNativeImplementation());
loadNativeLibraryNoBrowserProcess(); mActivityTestRule.loadNativeLibraryNoBrowserProcess();
assertTrue(CommandLine.getInstance().isNativeImplementation()); Assert.assertTrue(CommandLine.getInstance().isNativeImplementation());
} }
void checkInitSwitches() { void checkInitSwitches() {
CommandLine cl = CommandLine.getInstance(); CommandLine cl = CommandLine.getInstance();
assertFalse(cl.hasSwitch("init_command")); Assert.assertFalse(cl.hasSwitch("init_command"));
assertTrue(cl.hasSwitch("switch")); Assert.assertTrue(cl.hasSwitch("switch"));
assertFalse(cl.hasSwitch("--switch")); Assert.assertFalse(cl.hasSwitch("--switch"));
assertFalse(cl.hasSwitch("arg")); Assert.assertFalse(cl.hasSwitch("arg"));
assertFalse(cl.hasSwitch("actually_an_arg")); Assert.assertFalse(cl.hasSwitch("actually_an_arg"));
assertEquals("brea\\d", cl.getSwitchValue("switch2")); Assert.assertEquals("brea\\d", cl.getSwitchValue("switch2"));
assertEquals("and \"butter\"", cl.getSwitchValue("switch3")); Assert.assertEquals("and \"butter\"", cl.getSwitchValue("switch3"));
assertEquals("a \"quoted\" 'food'!", cl.getSwitchValue("switch4")); Assert.assertEquals("a \"quoted\" 'food'!", cl.getSwitchValue("switch4"));
assertNull(cl.getSwitchValue("switch")); Assert.assertNull(cl.getSwitchValue("switch"));
assertNull(cl.getSwitchValue("non-existant")); Assert.assertNull(cl.getSwitchValue("non-existant"));
} }
void checkSettingThenGetting() { void checkSettingThenGetting() {
CommandLine cl = CommandLine.getInstance(); CommandLine cl = CommandLine.getInstance();
// Add a plain switch. // Add a plain switch.
assertFalse(cl.hasSwitch(CL_ADDED_SWITCH)); Assert.assertFalse(cl.hasSwitch(CL_ADDED_SWITCH));
cl.appendSwitch(CL_ADDED_SWITCH); cl.appendSwitch(CL_ADDED_SWITCH);
assertTrue(cl.hasSwitch(CL_ADDED_SWITCH)); Assert.assertTrue(cl.hasSwitch(CL_ADDED_SWITCH));
// Add a switch paired with a value. // Add a switch paired with a value.
assertFalse(cl.hasSwitch(CL_ADDED_SWITCH_2)); Assert.assertFalse(cl.hasSwitch(CL_ADDED_SWITCH_2));
assertNull(cl.getSwitchValue(CL_ADDED_SWITCH_2)); Assert.assertNull(cl.getSwitchValue(CL_ADDED_SWITCH_2));
cl.appendSwitchWithValue(CL_ADDED_SWITCH_2, CL_ADDED_VALUE_2); cl.appendSwitchWithValue(CL_ADDED_SWITCH_2, CL_ADDED_VALUE_2);
assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2))); Assert.assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2)));
// Append a few new things. // Append a few new things.
final String switchesAndArgs[] = { "dummy", "--superfast", "--speed=turbo" }; final String switchesAndArgs[] = { "dummy", "--superfast", "--speed=turbo" };
assertFalse(cl.hasSwitch("dummy")); Assert.assertFalse(cl.hasSwitch("dummy"));
assertFalse(cl.hasSwitch("superfast")); Assert.assertFalse(cl.hasSwitch("superfast"));
assertNull(cl.getSwitchValue("speed")); Assert.assertNull(cl.getSwitchValue("speed"));
cl.appendSwitchesAndArguments(switchesAndArgs); cl.appendSwitchesAndArguments(switchesAndArgs);
assertFalse(cl.hasSwitch("dummy")); Assert.assertFalse(cl.hasSwitch("dummy"));
assertFalse(cl.hasSwitch("command")); Assert.assertFalse(cl.hasSwitch("command"));
assertTrue(cl.hasSwitch("superfast")); Assert.assertTrue(cl.hasSwitch("superfast"));
assertTrue("turbo".equals(cl.getSwitchValue("speed"))); Assert.assertTrue("turbo".equals(cl.getSwitchValue("speed")));
} }
void checkAppendedSwitchesPassedThrough() { void checkAppendedSwitchesPassedThrough() {
CommandLine cl = CommandLine.getInstance(); CommandLine cl = CommandLine.getInstance();
assertTrue(cl.hasSwitch(CL_ADDED_SWITCH)); Assert.assertTrue(cl.hasSwitch(CL_ADDED_SWITCH));
assertTrue(cl.hasSwitch(CL_ADDED_SWITCH_2)); Assert.assertTrue(cl.hasSwitch(CL_ADDED_SWITCH_2));
assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2))); Assert.assertTrue(CL_ADDED_VALUE_2.equals(cl.getSwitchValue(CL_ADDED_SWITCH_2)));
} }
@Test
@MediumTest @MediumTest
@Feature({"Android-AppBase"}) @Feature({"Android-AppBase"})
public void testJavaNativeTransition() { public void testJavaNativeTransition() {
...@@ -102,6 +113,7 @@ public class ContentCommandLineTest extends NativeLibraryTestBase { ...@@ -102,6 +113,7 @@ public class ContentCommandLineTest extends NativeLibraryTestBase {
checkSettingThenGetting(); checkSettingThenGetting();
} }
@Test
@MediumTest @MediumTest
@Feature({"Android-AppBase"}) @Feature({"Android-AppBase"})
public void testJavaNativeTransitionAfterAppends() { public void testJavaNativeTransitionAfterAppends() {
...@@ -113,6 +125,7 @@ public class ContentCommandLineTest extends NativeLibraryTestBase { ...@@ -113,6 +125,7 @@ public class ContentCommandLineTest extends NativeLibraryTestBase {
checkAppendedSwitchesPassedThrough(); checkAppendedSwitchesPassedThrough();
} }
@Test
@MediumTest @MediumTest
@Feature({"Android-AppBase"}) @Feature({"Android-AppBase"})
public void testNativeInitialization() { public void testNativeInitialization() {
...@@ -126,6 +139,7 @@ public class ContentCommandLineTest extends NativeLibraryTestBase { ...@@ -126,6 +139,7 @@ public class ContentCommandLineTest extends NativeLibraryTestBase {
checkSettingThenGetting(); checkSettingThenGetting();
} }
@Test
@SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME") @SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
@MediumTest @MediumTest
@Feature({"Android-AppBase"}) @Feature({"Android-AppBase"})
......
...@@ -4,15 +4,24 @@ ...@@ -4,15 +4,24 @@
package org.chromium.content.browser; package org.chromium.content.browser;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
import org.junit.After;
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.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.util.Criteria; import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper; import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer; import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper; import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper;
import org.chromium.content_public.browser.LoadUrlParams; import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_shell_apk.ContentShellTestBase; import org.chromium.content_shell_apk.ContentShellActivityTestRule;
import org.chromium.device.geolocation.LocationProviderFactory; import org.chromium.device.geolocation.LocationProviderFactory;
import org.chromium.device.geolocation.MockLocationProvider; import org.chromium.device.geolocation.MockLocationProvider;
...@@ -23,44 +32,48 @@ import java.util.concurrent.Callable; ...@@ -23,44 +32,48 @@ import java.util.concurrent.Callable;
* with ContentView APIs - e.g. that it's started and stopped as the * with ContentView APIs - e.g. that it's started and stopped as the
* ContentView is hidden or shown. * ContentView is hidden or shown.
*/ */
public class ContentViewLocationTest extends ContentShellTestBase { @RunWith(BaseJUnit4ClassRunner.class)
public class ContentViewLocationTest {
@Rule
public ContentShellActivityTestRule mActivityTestRule = new ContentShellActivityTestRule();
private TestCallbackHelperContainer mTestCallbackHelperContainer; private TestCallbackHelperContainer mTestCallbackHelperContainer;
private TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper mJavascriptHelper; private TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper mJavascriptHelper;
private MockLocationProvider mMockLocationProvider; private MockLocationProvider mMockLocationProvider;
private void hideContentViewOnUiThread() { private void hideContentViewOnUiThread() {
getInstrumentation().runOnMainSync(new Runnable() { InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override @Override
public void run() { public void run() {
getContentViewCore().onHide(); mActivityTestRule.getContentViewCore().onHide();
} }
}); });
} }
private void showContentViewOnUiThread() { private void showContentViewOnUiThread() {
getInstrumentation().runOnMainSync(new Runnable() { InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override @Override
public void run() { public void run() {
getContentViewCore().onShow(); mActivityTestRule.getContentViewCore().onShow();
} }
}); });
} }
private void pollForPositionCallback() throws Throwable { private void pollForPositionCallback() throws Throwable {
mJavascriptHelper.evaluateJavaScriptForTests(getWebContents(), mJavascriptHelper.evaluateJavaScriptForTests(
"positionCount = 0"); mActivityTestRule.getWebContents(), "positionCount = 0");
mJavascriptHelper.waitUntilHasValue(); mJavascriptHelper.waitUntilHasValue();
assertEquals(0, Integer.parseInt(mJavascriptHelper.getJsonResultAndClear())); Assert.assertEquals(0, Integer.parseInt(mJavascriptHelper.getJsonResultAndClear()));
CriteriaHelper.pollInstrumentationThread(new Criteria() { CriteriaHelper.pollInstrumentationThread(new Criteria() {
@Override @Override
public boolean isSatisfied() { public boolean isSatisfied() {
mJavascriptHelper.evaluateJavaScriptForTests(getWebContents(), "positionCount"); mJavascriptHelper.evaluateJavaScriptForTests(
mActivityTestRule.getWebContents(), "positionCount");
try { try {
mJavascriptHelper.waitUntilHasValue(); mJavascriptHelper.waitUntilHasValue();
} catch (Exception e) { } catch (Exception e) {
fail(); Assert.fail();
} }
return Integer.parseInt(mJavascriptHelper.getJsonResultAndClear()) > 0; return Integer.parseInt(mJavascriptHelper.getJsonResultAndClear()) > 0;
} }
...@@ -68,8 +81,8 @@ public class ContentViewLocationTest extends ContentShellTestBase { ...@@ -68,8 +81,8 @@ public class ContentViewLocationTest extends ContentShellTestBase {
} }
private void startGeolocationWatchPosition() throws Throwable { private void startGeolocationWatchPosition() throws Throwable {
mJavascriptHelper.evaluateJavaScriptForTests(getWebContents(), mJavascriptHelper.evaluateJavaScriptForTests(
"initiate_watchPosition();"); mActivityTestRule.getWebContents(), "initiate_watchPosition();");
mJavascriptHelper.waitUntilHasValue(); mJavascriptHelper.waitUntilHasValue();
} }
...@@ -82,35 +95,34 @@ public class ContentViewLocationTest extends ContentShellTestBase { ...@@ -82,35 +95,34 @@ public class ContentViewLocationTest extends ContentShellTestBase {
})); }));
} }
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
mMockLocationProvider = new MockLocationProvider(); mMockLocationProvider = new MockLocationProvider();
LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider); LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider);
try { try {
startActivityWithTestUrl("content/test/data/android/geolocation.html"); mActivityTestRule.launchContentShellWithUrlSync(
"content/test/data/android/geolocation.html");
} catch (Throwable t) { } catch (Throwable t) {
fail(); Assert.fail();
} }
mTestCallbackHelperContainer = new TestCallbackHelperContainer(getContentViewCore()); mTestCallbackHelperContainer =
new TestCallbackHelperContainer(mActivityTestRule.getContentViewCore());
mJavascriptHelper = new OnEvaluateJavaScriptResultHelper(); mJavascriptHelper = new OnEvaluateJavaScriptResultHelper();
ensureGeolocationRunning(false); ensureGeolocationRunning(false);
} }
@Override @After
protected void tearDown() throws Exception { public void tearDown() throws Exception {
mMockLocationProvider.stopUpdates(); mMockLocationProvider.stopUpdates();
super.tearDown();
} }
@Test
@MediumTest @MediumTest
@Feature({"Location"}) @Feature({"Location"})
public void testWatchHideShowStop() throws Throwable { public void testWatchHideShowStop() throws Throwable {
startGeolocationWatchPosition(); startGeolocationWatchPosition();
pollForPositionCallback(); pollForPositionCallback();
ensureGeolocationRunning(true); ensureGeolocationRunning(true);
...@@ -119,8 +131,8 @@ public class ContentViewLocationTest extends ContentShellTestBase { ...@@ -119,8 +131,8 @@ public class ContentViewLocationTest extends ContentShellTestBase {
hideContentViewOnUiThread(); hideContentViewOnUiThread();
ensureGeolocationRunning(false); ensureGeolocationRunning(false);
mJavascriptHelper.evaluateJavaScriptForTests(getWebContents(), mJavascriptHelper.evaluateJavaScriptForTests(
"positionCount = 0"); mActivityTestRule.getWebContents(), "positionCount = 0");
mJavascriptHelper.waitUntilHasValue(); mJavascriptHelper.waitUntilHasValue();
// Show the ContentView again and ensure that geolocation starts again. // Show the ContentView again and ensure that geolocation starts again.
...@@ -129,11 +141,13 @@ public class ContentViewLocationTest extends ContentShellTestBase { ...@@ -129,11 +141,13 @@ public class ContentViewLocationTest extends ContentShellTestBase {
ensureGeolocationRunning(true); ensureGeolocationRunning(true);
// Navigate away and ensure that geolocation stops. // Navigate away and ensure that geolocation stops.
loadUrl(getContentViewCore().getWebContents().getNavigationController(), mActivityTestRule.loadUrl(
mActivityTestRule.getContentViewCore().getWebContents().getNavigationController(),
mTestCallbackHelperContainer, new LoadUrlParams("about:blank")); mTestCallbackHelperContainer, new LoadUrlParams("about:blank"));
ensureGeolocationRunning(false); ensureGeolocationRunning(false);
} }
@Test
@MediumTest @MediumTest
@Feature({"Location"}) @Feature({"Location"})
public void testHideWatchResume() throws Throwable { public void testHideWatchResume() throws Throwable {
...@@ -146,6 +160,7 @@ public class ContentViewLocationTest extends ContentShellTestBase { ...@@ -146,6 +160,7 @@ public class ContentViewLocationTest extends ContentShellTestBase {
ensureGeolocationRunning(true); ensureGeolocationRunning(true);
} }
@Test
@MediumTest @MediumTest
@Feature({"Location"}) @Feature({"Location"})
public void testWatchHideNewWatchShow() throws Throwable { public void testWatchHideNewWatchShow() throws Throwable {
...@@ -165,6 +180,7 @@ public class ContentViewLocationTest extends ContentShellTestBase { ...@@ -165,6 +180,7 @@ public class ContentViewLocationTest extends ContentShellTestBase {
ensureGeolocationRunning(true); ensureGeolocationRunning(true);
} }
@Test
@MediumTest @MediumTest
@Feature({"Location"}) @Feature({"Location"})
public void testHideWatchStopShow() throws Throwable { public void testHideWatchStopShow() throws Throwable {
...@@ -172,7 +188,8 @@ public class ContentViewLocationTest extends ContentShellTestBase { ...@@ -172,7 +188,8 @@ public class ContentViewLocationTest extends ContentShellTestBase {
startGeolocationWatchPosition(); startGeolocationWatchPosition();
ensureGeolocationRunning(false); ensureGeolocationRunning(false);
loadUrl(getContentViewCore().getWebContents().getNavigationController(), mActivityTestRule.loadUrl(
mActivityTestRule.getContentViewCore().getWebContents().getNavigationController(),
mTestCallbackHelperContainer, new LoadUrlParams("about:blank")); mTestCallbackHelperContainer, new LoadUrlParams("about:blank"));
showContentViewOnUiThread(); showContentViewOnUiThread();
ensureGeolocationRunning(false); ensureGeolocationRunning(false);
......
...@@ -4,26 +4,36 @@ ...@@ -4,26 +4,36 @@
package org.chromium.content.browser; package org.chromium.content.browser;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.RetryOnFailure; import org.chromium.base.test.util.RetryOnFailure;
import org.chromium.base.test.util.UrlUtils; import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.test.util.Criteria; import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper; import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.content.browser.test.util.DOMUtils; import org.chromium.content.browser.test.util.DOMUtils;
import org.chromium.content_shell_apk.ContentShellTestBase; import org.chromium.content_shell_apk.ContentShellActivityTestRule;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
/** /**
* Class which provides test coverage for Popup Zoomer. * Class which provides test coverage for Popup Zoomer.
*/ */
@RunWith(BaseJUnit4ClassRunner.class)
@RetryOnFailure @RetryOnFailure
public class ContentViewPopupZoomerTest extends ContentShellTestBase { public class ContentViewPopupZoomerTest {
@Rule
public ContentShellActivityTestRule mActivityTestRule = new ContentShellActivityTestRule();
private static PopupZoomer findPopupZoomer(ViewGroup view) { private static PopupZoomer findPopupZoomer(ViewGroup view) {
assert view != null; assert view != null;
for (int i = 0; i < view.getChildCount(); i++) { for (int i = 0; i < view.getChildCount(); i++) {
...@@ -85,13 +95,14 @@ public class ContentViewPopupZoomerTest extends ContentShellTestBase { ...@@ -85,13 +95,14 @@ public class ContentViewPopupZoomerTest extends ContentShellTestBase {
/** /**
* Tests that shows a zoomer popup and makes sure it has valid dimensions. * Tests that shows a zoomer popup and makes sure it has valid dimensions.
*/ */
@Test
@MediumTest @MediumTest
@Feature({"Browser"}) @Feature({"Browser"})
public void testPopupZoomerShowsUp() throws InterruptedException, TimeoutException { public void testPopupZoomerShowsUp() throws InterruptedException, TimeoutException {
launchContentShellWithUrl(generateTestUrl(100, 15, "clickme")); mActivityTestRule.launchContentShellWithUrl(generateTestUrl(100, 15, "clickme"));
waitForActiveShellToBeDoneLoading(); mActivityTestRule.waitForActiveShellToBeDoneLoading();
final ContentViewCore viewCore = getContentViewCore(); final ContentViewCore viewCore = mActivityTestRule.getContentViewCore();
final ViewGroup view = viewCore.getContainerView(); final ViewGroup view = viewCore.getContainerView();
// The popup should be hidden before the click. // The popup should be hidden before the click.
...@@ -108,20 +119,21 @@ public class ContentViewPopupZoomerTest extends ContentShellTestBase { ...@@ -108,20 +119,21 @@ public class ContentViewPopupZoomerTest extends ContentShellTestBase {
/** /**
* Tests Popup zoomer hides when device back key is pressed. * Tests Popup zoomer hides when device back key is pressed.
*/ */
@Test
@MediumTest @MediumTest
@Feature({"Browser"}) @Feature({"Browser"})
@RetryOnFailure @RetryOnFailure
public void testBackKeyDismissesPopupZoomer() throws InterruptedException, TimeoutException { public void testBackKeyDismissesPopupZoomer() throws InterruptedException, TimeoutException {
launchContentShellWithUrl(generateTestUrl(100, 15, "clickme")); mActivityTestRule.launchContentShellWithUrl(generateTestUrl(100, 15, "clickme"));
waitForActiveShellToBeDoneLoading(); mActivityTestRule.waitForActiveShellToBeDoneLoading();
final ContentViewCore viewCore = getContentViewCore(); final ContentViewCore viewCore = mActivityTestRule.getContentViewCore();
final ViewGroup view = viewCore.getContainerView(); final ViewGroup view = viewCore.getContainerView();
CriteriaHelper.pollInstrumentationThread(new PopupShowingCriteria(view, false)); CriteriaHelper.pollInstrumentationThread(new PopupShowingCriteria(view, false));
DOMUtils.clickNode(viewCore, "clickme"); DOMUtils.clickNode(viewCore, "clickme");
CriteriaHelper.pollInstrumentationThread(new PopupShowingCriteria(view, true)); CriteriaHelper.pollInstrumentationThread(new PopupShowingCriteria(view, true));
sendKeys(KeyEvent.KEYCODE_BACK); InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
// When device key is pressed, popup zoomer should hide if already showing. // When device key is pressed, popup zoomer should hide if already showing.
CriteriaHelper.pollInstrumentationThread(new PopupShowingCriteria(view, false)); CriteriaHelper.pollInstrumentationThread(new PopupShowingCriteria(view, false));
} }
......
...@@ -9,16 +9,26 @@ import android.support.test.filters.SmallTest; ...@@ -9,16 +9,26 @@ import android.support.test.filters.SmallTest;
import android.view.InputDevice; import android.view.InputDevice;
import android.view.MotionEvent; import android.view.MotionEvent;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.UrlUtils; import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.input.AnimationIntervalProvider; import org.chromium.content.browser.input.AnimationIntervalProvider;
import org.chromium.content.browser.input.JoystickZoomProvider; import org.chromium.content.browser.input.JoystickZoomProvider;
import org.chromium.content_shell_apk.ContentShellTestBase; import org.chromium.content_shell_apk.ContentShellActivityTestRule;
/** /**
* Tests that ContentView running inside ContentShell can be zoomed using gamepad joystick. * Tests that ContentView running inside ContentShell can be zoomed using gamepad joystick.
*/ */
public class ContentViewZoomingTest extends ContentShellTestBase { @RunWith(BaseJUnit4ClassRunner.class)
public class ContentViewZoomingTest {
@Rule
public ContentShellActivityTestRule mActivityTestRule = new ContentShellActivityTestRule();
private static final String LARGE_PAGE = UrlUtils.encodeHtmlDataUri("<html><head>" private static final String LARGE_PAGE = UrlUtils.encodeHtmlDataUri("<html><head>"
+ "<meta name=\"viewport\" content=\"width=device-width, " + "<meta name=\"viewport\" content=\"width=device-width, "
+ "initial-scale=2.0, minimum-scale=2.0, maximum-scale=5.0\" />" + "initial-scale=2.0, minimum-scale=2.0, maximum-scale=5.0\" />"
...@@ -48,7 +58,7 @@ public class ContentViewZoomingTest extends ContentShellTestBase { ...@@ -48,7 +58,7 @@ public class ContentViewZoomingTest extends ContentShellTestBase {
public void animateZoomTest(final MotionEvent joystickZoomEvent, final long animationTicks) public void animateZoomTest(final MotionEvent joystickZoomEvent, final long animationTicks)
throws Throwable { throws Throwable {
runTestOnUiThread(new Runnable() { mActivityTestRule.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
onMotion(joystickZoomEvent); onMotion(joystickZoomEvent);
...@@ -77,67 +87,68 @@ public class ContentViewZoomingTest extends ContentShellTestBase { ...@@ -77,67 +87,68 @@ public class ContentViewZoomingTest extends ContentShellTestBase {
return joystickMotionEvent; return joystickMotionEvent;
} }
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); mActivityTestRule.launchContentShellWithUrl(LARGE_PAGE);
launchContentShellWithUrl(LARGE_PAGE); mActivityTestRule.waitForActiveShellToBeDoneLoading();
waitForActiveShellToBeDoneLoading(); mActivityTestRule.assertWaitForPageScaleFactorMatch(2.0f);
assertWaitForPageScaleFactorMatch(2.0f);
} }
@Test
@SmallTest @SmallTest
@Feature({"JoystickZoom"}) @Feature({"JoystickZoom"})
public void testJoystickZoomIn() throws Throwable { public void testJoystickZoomIn() throws Throwable {
MotionEvent rTriggerEvent; MotionEvent rTriggerEvent;
AnimationIntervalProvider intervalProvider = new TestAnimationIntervalProvider(); AnimationIntervalProvider intervalProvider = new TestAnimationIntervalProvider();
TestJoystickZoomProvider rtJoystickZoomProvider = TestJoystickZoomProvider rtJoystickZoomProvider = new TestJoystickZoomProvider(
new TestJoystickZoomProvider(getContentViewCore(), intervalProvider); mActivityTestRule.getContentViewCore(), intervalProvider);
// Verify page does not zoom-in if trigger motion falls in deadzone. // Verify page does not zoom-in if trigger motion falls in deadzone.
rTriggerEvent = simulateJoystickEvent(0.1f, true); rTriggerEvent = simulateJoystickEvent(0.1f, true);
rtJoystickZoomProvider.animateZoomTest(rTriggerEvent, 20); rtJoystickZoomProvider.animateZoomTest(rTriggerEvent, 20);
assertWaitForPageScaleFactorMatch(2.0f); mActivityTestRule.assertWaitForPageScaleFactorMatch(2.0f);
rTriggerEvent = simulateJoystickEvent(0.3f, true); rTriggerEvent = simulateJoystickEvent(0.3f, true);
rtJoystickZoomProvider.animateZoomTest(rTriggerEvent, 20); rtJoystickZoomProvider.animateZoomTest(rTriggerEvent, 20);
assertWaitForPageScaleFactorMatch(2.2018466f); mActivityTestRule.assertWaitForPageScaleFactorMatch(2.2018466f);
rTriggerEvent = simulateJoystickEvent(0.5f, true); rTriggerEvent = simulateJoystickEvent(0.5f, true);
rtJoystickZoomProvider.animateZoomTest(rTriggerEvent, 40); rtJoystickZoomProvider.animateZoomTest(rTriggerEvent, 40);
assertWaitForPageScaleFactorMatch(3.033731f); mActivityTestRule.assertWaitForPageScaleFactorMatch(3.033731f);
rTriggerEvent = simulateJoystickEvent(0.75f, true); rTriggerEvent = simulateJoystickEvent(0.75f, true);
rtJoystickZoomProvider.animateZoomTest(rTriggerEvent, 50); rtJoystickZoomProvider.animateZoomTest(rTriggerEvent, 50);
assertWaitForPageScaleFactorMatch(5.0f); mActivityTestRule.assertWaitForPageScaleFactorMatch(5.0f);
} }
@Test
@SmallTest @SmallTest
@Feature({"JoystickZoom"}) @Feature({"JoystickZoom"})
public void testJoystickZoomOut() throws Throwable { public void testJoystickZoomOut() throws Throwable {
MotionEvent lTriggerEvent; MotionEvent lTriggerEvent;
AnimationIntervalProvider intervalProvider = new TestAnimationIntervalProvider(); AnimationIntervalProvider intervalProvider = new TestAnimationIntervalProvider();
TestJoystickZoomProvider ltJoystickZoomProvider = TestJoystickZoomProvider ltJoystickZoomProvider = new TestJoystickZoomProvider(
new TestJoystickZoomProvider(getContentViewCore(), intervalProvider); mActivityTestRule.getContentViewCore(), intervalProvider);
// Zoom page to max size. // Zoom page to max size.
lTriggerEvent = simulateJoystickEvent(1.0f, true); lTriggerEvent = simulateJoystickEvent(1.0f, true);
ltJoystickZoomProvider.animateZoomTest(lTriggerEvent, 60); ltJoystickZoomProvider.animateZoomTest(lTriggerEvent, 60);
assertWaitForPageScaleFactorMatch(5.0f); mActivityTestRule.assertWaitForPageScaleFactorMatch(5.0f);
// Verify page does not zoom-out if trigger motion falls in deadzone. // Verify page does not zoom-out if trigger motion falls in deadzone.
lTriggerEvent = simulateJoystickEvent(0.1f, false); lTriggerEvent = simulateJoystickEvent(0.1f, false);
ltJoystickZoomProvider.animateZoomTest(lTriggerEvent, 20); ltJoystickZoomProvider.animateZoomTest(lTriggerEvent, 20);
assertWaitForPageScaleFactorMatch(5.0f); mActivityTestRule.assertWaitForPageScaleFactorMatch(5.0f);
lTriggerEvent = simulateJoystickEvent(0.3f, false); lTriggerEvent = simulateJoystickEvent(0.3f, false);
ltJoystickZoomProvider.animateZoomTest(lTriggerEvent, 40); ltJoystickZoomProvider.animateZoomTest(lTriggerEvent, 40);
assertWaitForPageScaleFactorMatch(4.125306f); mActivityTestRule.assertWaitForPageScaleFactorMatch(4.125306f);
lTriggerEvent = simulateJoystickEvent(0.5f, false); lTriggerEvent = simulateJoystickEvent(0.5f, false);
ltJoystickZoomProvider.animateZoomTest(lTriggerEvent, 50); ltJoystickZoomProvider.animateZoomTest(lTriggerEvent, 50);
assertWaitForPageScaleFactorMatch(2.7635581f); mActivityTestRule.assertWaitForPageScaleFactorMatch(2.7635581f);
lTriggerEvent = simulateJoystickEvent(0.75f, false); lTriggerEvent = simulateJoystickEvent(0.75f, false);
ltJoystickZoomProvider.animateZoomTest(lTriggerEvent, 60); ltJoystickZoomProvider.animateZoomTest(lTriggerEvent, 60);
assertWaitForPageScaleFactorMatch(2.0f); mActivityTestRule.assertWaitForPageScaleFactorMatch(2.0f);
} }
} }
...@@ -5,19 +5,24 @@ ...@@ -5,19 +5,24 @@
package org.chromium.content.browser; package org.chromium.content.browser;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.test.InstrumentationTestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.UrlUtils; import org.chromium.base.test.util.UrlUtils;
import java.net.URI; import java.net.URI;
import java.net.URLDecoder; import java.net.URLDecoder;
public class EncodeHtmlDataUriTest extends InstrumentationTestCase { @RunWith(BaseJUnit4ClassRunner.class)
public class EncodeHtmlDataUriTest {
private static final String DATA_URI_PREFIX = "data:text/html;utf-8,"; private static final String DATA_URI_PREFIX = "data:text/html;utf-8,";
private String getData(String dataUri) { private String getData(String dataUri) {
assertNotNull("Data URI is null", dataUri); Assert.assertNotNull("Data URI is null", dataUri);
assertTrue("Incorrect HTML Data URI prefix", dataUri.startsWith(DATA_URI_PREFIX)); Assert.assertTrue("Incorrect HTML Data URI prefix", dataUri.startsWith(DATA_URI_PREFIX));
return dataUri.substring(DATA_URI_PREFIX.length()); return dataUri.substring(DATA_URI_PREFIX.length());
} }
...@@ -26,30 +31,35 @@ public class EncodeHtmlDataUriTest extends InstrumentationTestCase { ...@@ -26,30 +31,35 @@ public class EncodeHtmlDataUriTest extends InstrumentationTestCase {
return URLDecoder.decode(data, "UTF-8"); return URLDecoder.decode(data, "UTF-8");
} }
@Test
@SmallTest @SmallTest
public void testDelimitersEncoding() throws java.io.UnsupportedEncodingException { public void testDelimitersEncoding() throws java.io.UnsupportedEncodingException {
String testString = "><#%\"'"; String testString = "><#%\"'";
String encodedUri = UrlUtils.encodeHtmlDataUri(testString); String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
String decodedUri = decode(encodedUri); String decodedUri = decode(encodedUri);
assertEquals("Delimiters are not properly encoded", decodedUri, testString); Assert.assertEquals("Delimiters are not properly encoded", decodedUri, testString);
} }
@Test
@SmallTest @SmallTest
public void testUnwiseCharactersEncoding() throws java.io.UnsupportedEncodingException { public void testUnwiseCharactersEncoding() throws java.io.UnsupportedEncodingException {
String testString = "{}|\\^[]`"; String testString = "{}|\\^[]`";
String encodedUri = UrlUtils.encodeHtmlDataUri(testString); String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
String decodedUri = decode(encodedUri); String decodedUri = decode(encodedUri);
assertEquals("Unwise characters are not properly encoded", decodedUri, testString); Assert.assertEquals("Unwise characters are not properly encoded", decodedUri, testString);
} }
@Test
@SmallTest @SmallTest
public void testWhitespaceEncoding() throws java.io.UnsupportedEncodingException { public void testWhitespaceEncoding() throws java.io.UnsupportedEncodingException {
String testString = " \n\t"; String testString = " \n\t";
String encodedUri = UrlUtils.encodeHtmlDataUri(testString); String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
String decodedUri = decode(encodedUri); String decodedUri = decode(encodedUri);
assertEquals("Whitespace characters are not properly encoded", decodedUri, testString); Assert.assertEquals(
"Whitespace characters are not properly encoded", decodedUri, testString);
} }
@Test
@SmallTest @SmallTest
public void testReturnsValidUri() public void testReturnsValidUri()
throws java.net.URISyntaxException, java.io.UnsupportedEncodingException { throws java.net.URISyntaxException, java.io.UnsupportedEncodingException {
...@@ -59,6 +69,6 @@ public class EncodeHtmlDataUriTest extends InstrumentationTestCase { ...@@ -59,6 +69,6 @@ public class EncodeHtmlDataUriTest extends InstrumentationTestCase {
// Verify that the encoded URI is valid. // Verify that the encoded URI is valid.
new URI(encodedUri); new URI(encodedUri);
// Verify that something sensible was encoded. // Verify that something sensible was encoded.
assertEquals("Simple HTML is not properly encoded", decodedUri, testString); Assert.assertEquals("Simple HTML is not properly encoded", decodedUri, testString);
} }
} }
...@@ -6,10 +6,16 @@ package org.chromium.content.browser; ...@@ -6,10 +6,16 @@ package org.chromium.content.browser;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.LargeTest; import android.support.test.filters.LargeTest;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.RetryOnFailure; import org.chromium.base.test.util.RetryOnFailure;
import org.chromium.base.test.util.UrlUtils; import org.chromium.base.test.util.UrlUtils;
...@@ -19,7 +25,7 @@ import org.chromium.content.browser.test.util.DOMUtils; ...@@ -19,7 +25,7 @@ import org.chromium.content.browser.test.util.DOMUtils;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer; import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper; import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
import org.chromium.content_public.browser.LoadUrlParams; import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_shell_apk.ContentShellTestBase; import org.chromium.content_shell_apk.ContentShellActivityTestRule;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -27,7 +33,11 @@ import java.util.concurrent.TimeUnit; ...@@ -27,7 +33,11 @@ import java.util.concurrent.TimeUnit;
* Provides test environment for Gesture Detector Reset for Content Shell. * Provides test environment for Gesture Detector Reset for Content Shell.
* This is a helper class for Content Shell tests. * This is a helper class for Content Shell tests.
*/ */
public class GestureDetectorResetTest extends ContentShellTestBase { @RunWith(BaseJUnit4ClassRunner.class)
public class GestureDetectorResetTest {
@Rule
public ContentShellActivityTestRule mActivityTestRule = new ContentShellActivityTestRule();
private static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(2); private static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(2);
private static final String CLICK_TEST_URL = UrlUtils.encodeHtmlDataUri("<html><body>" private static final String CLICK_TEST_URL = UrlUtils.encodeHtmlDataUri("<html><body>"
+ "<button id=\"button\" " + "<button id=\"button\" "
...@@ -90,16 +100,17 @@ public class GestureDetectorResetTest extends ContentShellTestBase { ...@@ -90,16 +100,17 @@ public class GestureDetectorResetTest extends ContentShellTestBase {
* Tests that showing a select popup and having the page reload while the popup is showing does * Tests that showing a select popup and having the page reload while the popup is showing does
* not assert. * not assert.
*/ */
@Test
@LargeTest @LargeTest
@Feature({"Browser"}) @Feature({"Browser"})
@RetryOnFailure @RetryOnFailure
public void testSeparateClicksAreRegisteredOnReload() public void testSeparateClicksAreRegisteredOnReload()
throws InterruptedException, Exception, Throwable { throws InterruptedException, Exception, Throwable {
// Load the test page. // Load the test page.
launchContentShellWithUrl(CLICK_TEST_URL); mActivityTestRule.launchContentShellWithUrl(CLICK_TEST_URL);
waitForActiveShellToBeDoneLoading(); mActivityTestRule.waitForActiveShellToBeDoneLoading();
final ContentViewCore viewCore = getContentViewCore(); final ContentViewCore viewCore = mActivityTestRule.getContentViewCore();
final TestCallbackHelperContainer viewClient = final TestCallbackHelperContainer viewClient =
new TestCallbackHelperContainer(viewCore); new TestCallbackHelperContainer(viewCore);
final OnPageFinishedHelper onPageFinishedHelper = final OnPageFinishedHelper onPageFinishedHelper =
...@@ -110,10 +121,10 @@ public class GestureDetectorResetTest extends ContentShellTestBase { ...@@ -110,10 +121,10 @@ public class GestureDetectorResetTest extends ContentShellTestBase {
// Reload the test page. // Reload the test page.
int currentCallCount = onPageFinishedHelper.getCallCount(); int currentCallCount = onPageFinishedHelper.getCallCount();
getInstrumentation().runOnMainSync(new Runnable() { InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override @Override
public void run() { public void run() {
getActivity().getActiveShell().loadUrl(CLICK_TEST_URL); mActivityTestRule.getActivity().getActiveShell().loadUrl(CLICK_TEST_URL);
} }
}); });
onPageFinishedHelper.waitForCallback(currentCallCount, 1, onPageFinishedHelper.waitForCallback(currentCallCount, 1,
...@@ -124,12 +135,15 @@ public class GestureDetectorResetTest extends ContentShellTestBase { ...@@ -124,12 +135,15 @@ public class GestureDetectorResetTest extends ContentShellTestBase {
// Directly navigate to the test page. // Directly navigate to the test page.
currentCallCount = onPageFinishedHelper.getCallCount(); currentCallCount = onPageFinishedHelper.getCallCount();
getInstrumentation().runOnMainSync(new Runnable() { InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override @Override
public void run() { public void run() {
getActivity().getActiveShell().getContentViewCore().getWebContents() mActivityTestRule.getActivity()
.getNavigationController().loadUrl( .getActiveShell()
new LoadUrlParams(CLICK_TEST_URL)); .getContentViewCore()
.getWebContents()
.getNavigationController()
.loadUrl(new LoadUrlParams(CLICK_TEST_URL));
} }
}); });
onPageFinishedHelper.waitForCallback(currentCallCount, 1, onPageFinishedHelper.waitForCallback(currentCallCount, 1,
......
...@@ -4,11 +4,19 @@ ...@@ -4,11 +4,19 @@
package org.chromium.content.browser; package org.chromium.content.browser;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
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.ImportantFileWriterAndroid; import org.chromium.base.ImportantFileWriterAndroid;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.NativeLibraryTestBase; import org.chromium.content.browser.test.NativeLibraryTestRule;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.File; import java.io.File;
...@@ -23,62 +31,62 @@ import java.io.IOException; ...@@ -23,62 +31,62 @@ import java.io.IOException;
* work, so is not attempting to test that writes are atomic. Instead it is just * work, so is not attempting to test that writes are atomic. Instead it is just
* testing that the Java code is calling the native code correctly. * testing that the Java code is calling the native code correctly.
*/ */
public class ImportantFileWriterAndroidTest extends NativeLibraryTestBase { @RunWith(BaseJUnit4ClassRunner.class)
public class ImportantFileWriterAndroidTest {
@Rule
public NativeLibraryTestRule mActivityTestRule = new NativeLibraryTestRule();
private void checkFile(File testFile, byte[] data) { private void checkFile(File testFile, byte[] data) {
assertTrue(testFile.exists()); Assert.assertTrue(testFile.exists());
try { try {
byte[] fileData = new byte[(int) testFile.length()]; byte[] fileData = new byte[(int) testFile.length()];
DataInputStream dis = DataInputStream dis =
new DataInputStream(new FileInputStream(testFile)); new DataInputStream(new FileInputStream(testFile));
dis.readFully(fileData); dis.readFully(fileData);
dis.close(); dis.close();
assertEquals("Data length wrong", data.length, fileData.length); Assert.assertEquals("Data length wrong", data.length, fileData.length);
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
assertEquals("Data byte wrong", data[i], fileData[i]); Assert.assertEquals("Data byte wrong", data[i], fileData[i]);
} }
} catch (IOException e) { } catch (IOException e) {
fail("Failed to read file"); Assert.fail("Failed to read file");
} }
} }
@Test
@SmallTest @SmallTest
@Feature({"Android-AppBase"}) @Feature({"Android-AppBase"})
public void testAtomicWrite() { public void testAtomicWrite() {
// Try writing a file that can't be created. // Try writing a file that can't be created.
byte[] data1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; byte[] data1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
assertFalse("Writing bad file succeded", Assert.assertFalse("Writing bad file succeded",
ImportantFileWriterAndroid.writeFileAtomically( ImportantFileWriterAndroid.writeFileAtomically("/junk/junk", data1));
"/junk/junk", data1)); File dir = InstrumentationRegistry.getInstrumentation().getTargetContext().getFilesDir();
File dir = getInstrumentation().getTargetContext().getFilesDir();
File testFile = new File(dir, "ImportantFileTest"); File testFile = new File(dir, "ImportantFileTest");
// Make sure the file doesn't already exist // Make sure the file doesn't already exist
if (testFile.exists()) { if (testFile.exists()) {
assertTrue(testFile.delete()); Assert.assertTrue(testFile.delete());
} }
// Write a new file // Write a new file
assertTrue("Writing new file failed", Assert.assertTrue("Writing new file failed",
ImportantFileWriterAndroid.writeFileAtomically( ImportantFileWriterAndroid.writeFileAtomically(testFile.getAbsolutePath(), data1));
testFile.getAbsolutePath(), data1));
checkFile(testFile, data1); checkFile(testFile, data1);
byte[] data2 = {10, 20, 30, 40, 50, 60, 70, 80}; byte[] data2 = {10, 20, 30, 40, 50, 60, 70, 80};
// Overwrite an existing file // Overwrite an existing file
assertTrue("Writing exiting file failed", Assert.assertTrue("Writing exiting file failed",
ImportantFileWriterAndroid.writeFileAtomically( ImportantFileWriterAndroid.writeFileAtomically(testFile.getAbsolutePath(), data2));
testFile.getAbsolutePath(), data2));
checkFile(testFile, data2); checkFile(testFile, data2);
// Done, tidy up // Done, tidy up
assertTrue(testFile.delete()); Assert.assertTrue(testFile.delete());
} }
@Override @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); mActivityTestRule.loadNativeLibraryNoBrowserProcess();
loadNativeLibraryNoBrowserProcess();
} }
} }
...@@ -6,7 +6,14 @@ package org.chromium.content.browser; ...@@ -6,7 +6,14 @@ package org.chromium.content.browser;
import android.support.test.filters.LargeTest; import android.support.test.filters.LargeTest;
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.ThreadUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.RetryOnFailure; import org.chromium.base.test.util.RetryOnFailure;
import org.chromium.base.test.util.UrlUtils; import org.chromium.base.test.util.UrlUtils;
...@@ -16,7 +23,7 @@ import org.chromium.content.browser.test.util.TouchCommon; ...@@ -16,7 +23,7 @@ import org.chromium.content.browser.test.util.TouchCommon;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.WebContentsObserver; import org.chromium.content_public.browser.WebContentsObserver;
import org.chromium.content_shell_apk.ContentShellActivity; import org.chromium.content_shell_apk.ContentShellActivity;
import org.chromium.content_shell_apk.ContentShellTestBase; import org.chromium.content_shell_apk.ContentShellActivityTestRule;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
...@@ -24,7 +31,10 @@ import java.util.concurrent.ExecutionException; ...@@ -24,7 +31,10 @@ import java.util.concurrent.ExecutionException;
/** /**
* Tests for interstitial pages. * Tests for interstitial pages.
*/ */
public class InterstitialPageTest extends ContentShellTestBase { @RunWith(BaseJUnit4ClassRunner.class)
public class InterstitialPageTest {
@Rule
public ContentShellActivityTestRule mActivityTestRule = new ContentShellActivityTestRule();
private static final String URL = UrlUtils.encodeHtmlDataUri( private static final String URL = UrlUtils.encodeHtmlDataUri(
"<html><head></head><body>test</body></html>"); "<html><head></head><body>test</body></html>");
...@@ -56,12 +66,11 @@ public class InterstitialPageTest extends ContentShellTestBase { ...@@ -56,12 +66,11 @@ public class InterstitialPageTest extends ContentShellTestBase {
} }
} }
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); ContentShellActivity activity = mActivityTestRule.launchContentShellWithUrl(URL);
ContentShellActivity activity = launchContentShellWithUrl(URL); Assert.assertNotNull(activity);
assertNotNull(activity); mActivityTestRule.waitForActiveShellToBeDoneLoading();
waitForActiveShellToBeDoneLoading();
} }
private void waitForInterstitial(final boolean shouldBeShown) { private void waitForInterstitial(final boolean shouldBeShown) {
...@@ -69,7 +78,7 @@ public class InterstitialPageTest extends ContentShellTestBase { ...@@ -69,7 +78,7 @@ public class InterstitialPageTest extends ContentShellTestBase {
Criteria.equals(shouldBeShown, new Callable<Boolean>() { Criteria.equals(shouldBeShown, new Callable<Boolean>() {
@Override @Override
public Boolean call() { public Boolean call() {
return getWebContents().isShowingInterstitialPage(); return mActivityTestRule.getWebContents().isShowingInterstitialPage();
} }
})); }));
} }
...@@ -77,6 +86,7 @@ public class InterstitialPageTest extends ContentShellTestBase { ...@@ -77,6 +86,7 @@ public class InterstitialPageTest extends ContentShellTestBase {
/** /**
* Tests that showing and hiding an interstitial works. * Tests that showing and hiding an interstitial works.
*/ */
@Test
@LargeTest @LargeTest
@Feature({"Navigation"}) @Feature({"Navigation"})
@RetryOnFailure @RetryOnFailure
...@@ -100,7 +110,7 @@ public class InterstitialPageTest extends ContentShellTestBase { ...@@ -100,7 +110,7 @@ public class InterstitialPageTest extends ContentShellTestBase {
new InterstitialPageDelegateAndroid(htmlContent) { new InterstitialPageDelegateAndroid(htmlContent) {
@Override @Override
protected void commandReceived(String command) { protected void commandReceived(String command) {
assertEquals(command, proceedCommand); Assert.assertEquals(command, proceedCommand);
proceed(); proceed();
} }
}; };
...@@ -108,17 +118,19 @@ public class InterstitialPageTest extends ContentShellTestBase { ...@@ -108,17 +118,19 @@ public class InterstitialPageTest extends ContentShellTestBase {
new Callable<TestWebContentsObserver>() { new Callable<TestWebContentsObserver>() {
@Override @Override
public TestWebContentsObserver call() throws Exception { public TestWebContentsObserver call() throws Exception {
getWebContents().showInterstitialPage(URL, delegate.getNative()); mActivityTestRule.getWebContents().showInterstitialPage(
return new TestWebContentsObserver(getWebContents()); URL, delegate.getNative());
return new TestWebContentsObserver(mActivityTestRule.getWebContents());
} }
}); });
waitForInterstitial(true); waitForInterstitial(true);
assertTrue("WebContentsObserver not notified of interstitial showing", Assert.assertTrue("WebContentsObserver not notified of interstitial showing",
observer.isInterstitialShowing()); observer.isInterstitialShowing());
TouchCommon.singleClickView(getContentViewCore().getContainerView(), 10, 10); TouchCommon.singleClickView(
mActivityTestRule.getContentViewCore().getContainerView(), 10, 10);
waitForInterstitial(false); waitForInterstitial(false);
assertTrue("WebContentsObserver not notified of interstitial hiding", Assert.assertTrue("WebContentsObserver not notified of interstitial hiding",
!observer.isInterstitialShowing()); !observer.isInterstitialShowing());
} }
} }
...@@ -6,33 +6,44 @@ package org.chromium.content.browser; ...@@ -6,33 +6,44 @@ package org.chromium.content.browser;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
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.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.UrlUtils; import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer; import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper; import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper; import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
import org.chromium.content_shell_apk.ContentShellTestBase; import org.chromium.content_shell_apk.ContentShellActivityTestRule;
/** /**
* Common functionality for testing the Java Bridge. * Common functionality for testing the Java Bridge.
*/ */
public class JavaBridgeBareboneTest extends ContentShellTestBase { @RunWith(BaseJUnit4ClassRunner.class)
public class JavaBridgeBareboneTest {
@Rule
public ContentShellActivityTestRule mActivityTestRule = new ContentShellActivityTestRule();
private TestCallbackHelperContainer mTestCallbackHelperContainer; private TestCallbackHelperContainer mTestCallbackHelperContainer;
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); mActivityTestRule.launchContentShellWithUrl(
launchContentShellWithUrl(
UrlUtils.encodeHtmlDataUri("<html><head></head><body>test</body></html>")); UrlUtils.encodeHtmlDataUri("<html><head></head><body>test</body></html>"));
waitForActiveShellToBeDoneLoading(); mActivityTestRule.waitForActiveShellToBeDoneLoading();
mTestCallbackHelperContainer = new TestCallbackHelperContainer(getContentViewCore()); mTestCallbackHelperContainer =
new TestCallbackHelperContainer(mActivityTestRule.getContentViewCore());
} }
private void injectDummyObject(final String name) throws Throwable { private void injectDummyObject(final String name) throws Throwable {
runTestOnUiThread(new Runnable() { mActivityTestRule.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
getContentViewCore().addPossiblyUnsafeJavascriptInterface( mActivityTestRule.getContentViewCore().addPossiblyUnsafeJavascriptInterface(
new Object(), name, null); new Object(), name, null);
} }
}); });
...@@ -40,7 +51,7 @@ public class JavaBridgeBareboneTest extends ContentShellTestBase { ...@@ -40,7 +51,7 @@ public class JavaBridgeBareboneTest extends ContentShellTestBase {
private String evaluateJsSync(String jsCode) throws Exception { private String evaluateJsSync(String jsCode) throws Exception {
OnEvaluateJavaScriptResultHelper javascriptHelper = new OnEvaluateJavaScriptResultHelper(); OnEvaluateJavaScriptResultHelper javascriptHelper = new OnEvaluateJavaScriptResultHelper();
javascriptHelper.evaluateJavaScriptForTests(getWebContents(), jsCode); javascriptHelper.evaluateJavaScriptForTests(mActivityTestRule.getWebContents(), jsCode);
javascriptHelper.waitUntilHasValue(); javascriptHelper.waitUntilHasValue();
return javascriptHelper.getJsonResultAndClear(); return javascriptHelper.getJsonResultAndClear();
} }
...@@ -49,10 +60,10 @@ public class JavaBridgeBareboneTest extends ContentShellTestBase { ...@@ -49,10 +60,10 @@ public class JavaBridgeBareboneTest extends ContentShellTestBase {
OnPageFinishedHelper pageFinishedHelper = OnPageFinishedHelper pageFinishedHelper =
mTestCallbackHelperContainer.getOnPageFinishedHelper(); mTestCallbackHelperContainer.getOnPageFinishedHelper();
int currentCallCount = pageFinishedHelper.getCallCount(); int currentCallCount = pageFinishedHelper.getCallCount();
runTestOnUiThread(new Runnable() { mActivityTestRule.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
getWebContents().getNavigationController().reload(true); mActivityTestRule.getWebContents().getNavigationController().reload(true);
} }
}); });
pageFinishedHelper.waitForCallback(currentCallCount); pageFinishedHelper.waitForCallback(currentCallCount);
...@@ -69,36 +80,40 @@ public class JavaBridgeBareboneTest extends ContentShellTestBase { ...@@ -69,36 +80,40 @@ public class JavaBridgeBareboneTest extends ContentShellTestBase {
// webView.addJavascriptInterface(new Foo(), "foo"); // webView.addJavascriptInterface(new Foo(), "foo");
// webView.loadUrl("javascript:foo.bar();void(0);"); // webView.loadUrl("javascript:foo.bar();void(0);");
// //
@Test
@SmallTest @SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"}) @Feature({"AndroidWebView", "Android-JavaBridge"})
public void testImmediateAddition() throws Throwable { public void testImmediateAddition() throws Throwable {
injectDummyObject("testObject"); injectDummyObject("testObject");
assertEquals("\"object\"", evaluateJsSync("typeof testObject")); Assert.assertEquals("\"object\"", evaluateJsSync("typeof testObject"));
} }
// Now here, we evaluate some JS before injecting the object, and this behaves as // Now here, we evaluate some JS before injecting the object, and this behaves as
// expected. // expected.
@Test
@SmallTest @SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"}) @Feature({"AndroidWebView", "Android-JavaBridge"})
public void testNoImmediateAdditionAfterJSEvaluation() throws Throwable { public void testNoImmediateAdditionAfterJSEvaluation() throws Throwable {
evaluateJsSync("true"); evaluateJsSync("true");
injectDummyObject("testObject"); injectDummyObject("testObject");
assertEquals("\"undefined\"", evaluateJsSync("typeof testObject")); Assert.assertEquals("\"undefined\"", evaluateJsSync("typeof testObject"));
} }
@Test
@SmallTest @SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"}) @Feature({"AndroidWebView", "Android-JavaBridge"})
public void testImmediateAdditionAfterReload() throws Throwable { public void testImmediateAdditionAfterReload() throws Throwable {
reloadSync(); reloadSync();
injectDummyObject("testObject"); injectDummyObject("testObject");
assertEquals("\"object\"", evaluateJsSync("typeof testObject")); Assert.assertEquals("\"object\"", evaluateJsSync("typeof testObject"));
} }
@Test
@SmallTest @SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"}) @Feature({"AndroidWebView", "Android-JavaBridge"})
public void testReloadAfterAddition() throws Throwable { public void testReloadAfterAddition() throws Throwable {
injectDummyObject("testObject"); injectDummyObject("testObject");
reloadSync(); reloadSync();
assertEquals("\"object\"", evaluateJsSync("typeof testObject")); Assert.assertEquals("\"object\"", evaluateJsSync("typeof testObject"));
} }
} }
...@@ -6,7 +6,14 @@ package org.chromium.content.browser; ...@@ -6,7 +6,14 @@ package org.chromium.content.browser;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
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.annotations.SuppressFBWarnings; import org.chromium.base.annotations.SuppressFBWarnings;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.JavaBridgeTestCommon.Controller; import org.chromium.content.browser.JavaBridgeTestCommon.Controller;
...@@ -14,7 +21,11 @@ import org.chromium.content.browser.JavaBridgeTestCommon.Controller; ...@@ -14,7 +21,11 @@ import org.chromium.content.browser.JavaBridgeTestCommon.Controller;
* Part of the test suite for the Java Bridge. This test tests the * Part of the test suite for the Java Bridge. This test tests the
* use of fields. * use of fields.
*/ */
public class JavaBridgeFieldsTest extends JavaBridgeTestBase { @RunWith(BaseJUnit4ClassRunner.class)
public class JavaBridgeFieldsTest {
@Rule
public JavaBridgeActivityTestRule mActivityTestRule = new JavaBridgeActivityTestRule();
@SuppressFBWarnings({"CHROMIUM_SYNCHRONIZED_METHOD", "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"}) @SuppressFBWarnings({"CHROMIUM_SYNCHRONIZED_METHOD", "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"})
private static class TestObject extends Controller { private static class TestObject extends Controller {
private String mStringValue; private String mStringValue;
...@@ -48,45 +59,45 @@ public class JavaBridgeFieldsTest extends JavaBridgeTestBase { ...@@ -48,45 +59,45 @@ public class JavaBridgeFieldsTest extends JavaBridgeTestBase {
TestObject mTestObject; TestObject mTestObject;
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
mTestObject = new TestObject(); mTestObject = new TestObject();
injectObjectAndReload(mTestObject, "testObject"); mActivityTestRule.injectObjectAndReload(mTestObject, "testObject");
} }
// Note that this requires that we can pass a JavaScript string to Java. // Note that this requires that we can pass a JavaScript string to Java.
protected String executeJavaScriptAndGetStringResult(String script) throws Throwable { protected String executeJavaScriptAndGetStringResult(String script) throws Throwable {
executeJavaScript("testObject.setStringValue(" + script + ");"); mActivityTestRule.executeJavaScript("testObject.setStringValue(" + script + ");");
return mTestObject.waitForStringValue(); return mTestObject.waitForStringValue();
} }
// The Java bridge does not provide access to fields. // The Java bridge does not provide access to fields.
// FIXME: Consider providing support for this. See See b/4408210. // FIXME: Consider providing support for this. See See b/4408210.
@Test
@SmallTest @SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"}) @Feature({"AndroidWebView", "Android-JavaBridge"})
public void testFieldTypes() throws Throwable { public void testFieldTypes() throws Throwable {
assertEquals("undefined", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.booleanField")); "undefined", executeJavaScriptAndGetStringResult("typeof testObject.booleanField"));
assertEquals("undefined", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.byteField")); "undefined", executeJavaScriptAndGetStringResult("typeof testObject.byteField"));
assertEquals("undefined", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.charField")); "undefined", executeJavaScriptAndGetStringResult("typeof testObject.charField"));
assertEquals("undefined", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.shortField")); "undefined", executeJavaScriptAndGetStringResult("typeof testObject.shortField"));
assertEquals("undefined", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.intField")); "undefined", executeJavaScriptAndGetStringResult("typeof testObject.intField"));
assertEquals("undefined", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.longField")); "undefined", executeJavaScriptAndGetStringResult("typeof testObject.longField"));
assertEquals("undefined", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.floatField")); "undefined", executeJavaScriptAndGetStringResult("typeof testObject.floatField"));
assertEquals("undefined", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.doubleField")); "undefined", executeJavaScriptAndGetStringResult("typeof testObject.doubleField"));
assertEquals("undefined", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.objectField")); "undefined", executeJavaScriptAndGetStringResult("typeof testObject.objectField"));
assertEquals("undefined", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.stringField")); "undefined", executeJavaScriptAndGetStringResult("typeof testObject.stringField"));
assertEquals("undefined", Assert.assertEquals("undefined",
executeJavaScriptAndGetStringResult("typeof testObject.customTypeField")); executeJavaScriptAndGetStringResult("typeof testObject.customTypeField"));
} }
} }
...@@ -6,7 +6,14 @@ package org.chromium.content.browser; ...@@ -6,7 +6,14 @@ package org.chromium.content.browser;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
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.annotations.SuppressFBWarnings; import org.chromium.base.annotations.SuppressFBWarnings;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.JavaBridgeTestCommon.Controller; import org.chromium.content.browser.JavaBridgeTestCommon.Controller;
...@@ -21,7 +28,11 @@ import org.chromium.content.browser.JavaBridgeTestCommon.Controller; ...@@ -21,7 +28,11 @@ import org.chromium.content.browser.JavaBridgeTestCommon.Controller;
* FIXME: Consider making our implementation more compliant, if it will not * FIXME: Consider making our implementation more compliant, if it will not
* break backwards-compatibility. See b/4408210. * break backwards-compatibility. See b/4408210.
*/ */
public class JavaBridgeReturnValuesTest extends JavaBridgeTestBase { @RunWith(BaseJUnit4ClassRunner.class)
public class JavaBridgeReturnValuesTest {
@Rule
public JavaBridgeActivityTestRule mActivityTestRule = new JavaBridgeActivityTestRule();
// An instance of this class is injected into the page to test returning // An instance of this class is injected into the page to test returning
// Java values to JavaScript. // Java values to JavaScript.
@SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD")
...@@ -105,88 +116,93 @@ public class JavaBridgeReturnValuesTest extends JavaBridgeTestBase { ...@@ -105,88 +116,93 @@ public class JavaBridgeReturnValuesTest extends JavaBridgeTestBase {
TestObject mTestObject; TestObject mTestObject;
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
mTestObject = new TestObject(); mTestObject = new TestObject();
injectObjectAndReload(mTestObject, "testObject"); mActivityTestRule.injectObjectAndReload(mTestObject, "testObject");
} }
// Note that this requires that we can pass a JavaScript string to Java. // Note that this requires that we can pass a JavaScript string to Java.
protected String executeJavaScriptAndGetStringResult(String script) throws Throwable { protected String executeJavaScriptAndGetStringResult(String script) throws Throwable {
executeJavaScript("testObject.setStringResult(" + script + ");"); mActivityTestRule.executeJavaScript("testObject.setStringResult(" + script + ");");
return mTestObject.waitForStringResult(); return mTestObject.waitForStringResult();
} }
// Note that this requires that we can pass a JavaScript boolean to Java. // Note that this requires that we can pass a JavaScript boolean to Java.
private boolean executeJavaScriptAndGetBooleanResult(String script) throws Throwable { private boolean executeJavaScriptAndGetBooleanResult(String script) throws Throwable {
executeJavaScript("testObject.setBooleanResult(" + script + ");"); mActivityTestRule.executeJavaScript("testObject.setBooleanResult(" + script + ");");
return mTestObject.waitForBooleanResult(); return mTestObject.waitForBooleanResult();
} }
@Test
@SmallTest @SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"}) @Feature({"AndroidWebView", "Android-JavaBridge"})
public void testMethodReturnTypes() throws Throwable { public void testMethodReturnTypes() throws Throwable {
assertEquals("boolean", Assert.assertEquals("boolean",
executeJavaScriptAndGetStringResult("typeof testObject.getBooleanValue()")); executeJavaScriptAndGetStringResult("typeof testObject.getBooleanValue()"));
assertEquals("number", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.getByteValue()")); "number", executeJavaScriptAndGetStringResult("typeof testObject.getByteValue()"));
// char values are returned to JavaScript as numbers. // char values are returned to JavaScript as numbers.
assertEquals("number", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.getCharValue()")); "number", executeJavaScriptAndGetStringResult("typeof testObject.getCharValue()"));
assertEquals("number", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.getShortValue()")); "number", executeJavaScriptAndGetStringResult("typeof testObject.getShortValue()"));
assertEquals("number", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.getIntValue()")); "number", executeJavaScriptAndGetStringResult("typeof testObject.getIntValue()"));
assertEquals("number", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.getLongValue()")); "number", executeJavaScriptAndGetStringResult("typeof testObject.getLongValue()"));
assertEquals("number", Assert.assertEquals(
executeJavaScriptAndGetStringResult("typeof testObject.getFloatValue()")); "number", executeJavaScriptAndGetStringResult("typeof testObject.getFloatValue()"));
assertEquals("number", Assert.assertEquals("number",
executeJavaScriptAndGetStringResult("typeof testObject.getFloatValueNoDecimal()")); executeJavaScriptAndGetStringResult("typeof testObject.getFloatValueNoDecimal()"));
assertEquals("number", Assert.assertEquals("number",
executeJavaScriptAndGetStringResult("typeof testObject.getDoubleValue()")); executeJavaScriptAndGetStringResult("typeof testObject.getDoubleValue()"));
assertEquals("number", Assert.assertEquals("number",
executeJavaScriptAndGetStringResult("typeof testObject.getDoubleValueNoDecimal()")); executeJavaScriptAndGetStringResult("typeof testObject.getDoubleValueNoDecimal()"));
assertEquals("string", Assert.assertEquals("string",
executeJavaScriptAndGetStringResult("typeof testObject.getStringValue()")); executeJavaScriptAndGetStringResult("typeof testObject.getStringValue()"));
assertEquals("string", Assert.assertEquals("string",
executeJavaScriptAndGetStringResult("typeof testObject.getEmptyStringValue()")); executeJavaScriptAndGetStringResult("typeof testObject.getEmptyStringValue()"));
// LIVECONNECT_COMPLIANCE: This should have type object. // LIVECONNECT_COMPLIANCE: This should have type object.
assertEquals("undefined", Assert.assertEquals("undefined",
executeJavaScriptAndGetStringResult("typeof testObject.getNullStringValue()")); executeJavaScriptAndGetStringResult("typeof testObject.getNullStringValue()"));
assertEquals("object", Assert.assertEquals("object",
executeJavaScriptAndGetStringResult("typeof testObject.getObjectValue()")); executeJavaScriptAndGetStringResult("typeof testObject.getObjectValue()"));
assertEquals("object", Assert.assertEquals("object",
executeJavaScriptAndGetStringResult("typeof testObject.getNullObjectValue()")); executeJavaScriptAndGetStringResult("typeof testObject.getNullObjectValue()"));
assertEquals("object", Assert.assertEquals("object",
executeJavaScriptAndGetStringResult("typeof testObject.getCustomTypeValue()")); executeJavaScriptAndGetStringResult("typeof testObject.getCustomTypeValue()"));
assertEquals("undefined", Assert.assertEquals("undefined",
executeJavaScriptAndGetStringResult("typeof testObject.getVoidValue()")); executeJavaScriptAndGetStringResult("typeof testObject.getVoidValue()"));
} }
@Test
@SmallTest @SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"}) @Feature({"AndroidWebView", "Android-JavaBridge"})
public void testMethodReturnValues() throws Throwable { public void testMethodReturnValues() throws Throwable {
// We do the string comparison in JavaScript, to avoid relying on the // We do the string comparison in JavaScript, to avoid relying on the
// coercion algorithm from JavaScript to Java. // coercion algorithm from JavaScript to Java.
assertTrue(executeJavaScriptAndGetBooleanResult("testObject.getBooleanValue()")); Assert.assertTrue(executeJavaScriptAndGetBooleanResult("testObject.getBooleanValue()"));
assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getByteValue()")); Assert.assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getByteValue()"));
// char values are returned to JavaScript as numbers. // char values are returned to JavaScript as numbers.
assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getCharValue()")); Assert.assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getCharValue()"));
assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getShortValue()")); Assert.assertTrue(
assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getIntValue()")); executeJavaScriptAndGetBooleanResult("42 === testObject.getShortValue()"));
assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getLongValue()")); Assert.assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getIntValue()"));
assertTrue(executeJavaScriptAndGetBooleanResult( Assert.assertTrue(executeJavaScriptAndGetBooleanResult("42 === testObject.getLongValue()"));
Assert.assertTrue(executeJavaScriptAndGetBooleanResult(
"Math.abs(42.1 - testObject.getFloatValue()) < 0.001")); "Math.abs(42.1 - testObject.getFloatValue()) < 0.001"));
assertTrue(executeJavaScriptAndGetBooleanResult( Assert.assertTrue(executeJavaScriptAndGetBooleanResult(
"42.0 === testObject.getFloatValueNoDecimal()")); "42.0 === testObject.getFloatValueNoDecimal()"));
assertTrue(executeJavaScriptAndGetBooleanResult( Assert.assertTrue(executeJavaScriptAndGetBooleanResult(
"Math.abs(42.1 - testObject.getDoubleValue()) < 0.001")); "Math.abs(42.1 - testObject.getDoubleValue()) < 0.001"));
assertTrue(executeJavaScriptAndGetBooleanResult( Assert.assertTrue(executeJavaScriptAndGetBooleanResult(
"42.0 === testObject.getDoubleValueNoDecimal()")); "42.0 === testObject.getDoubleValueNoDecimal()"));
assertEquals("foo", executeJavaScriptAndGetStringResult("testObject.getStringValue()")); Assert.assertEquals(
assertEquals("", executeJavaScriptAndGetStringResult("testObject.getEmptyStringValue()")); "foo", executeJavaScriptAndGetStringResult("testObject.getStringValue()"));
assertTrue(executeJavaScriptAndGetBooleanResult("undefined === testObject.getVoidValue()")); Assert.assertEquals(
"", executeJavaScriptAndGetStringResult("testObject.getEmptyStringValue()"));
Assert.assertTrue(
executeJavaScriptAndGetBooleanResult("undefined === testObject.getVoidValue()"));
} }
} }
...@@ -6,16 +6,26 @@ package org.chromium.content.browser; ...@@ -6,16 +6,26 @@ package org.chromium.content.browser;
import android.support.test.filters.LargeTest; import android.support.test.filters.LargeTest;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.UrlUtils; import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.test.util.DOMUtils; import org.chromium.content.browser.test.util.DOMUtils;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import org.chromium.content_shell_apk.ContentShellTestBase; import org.chromium.content_shell_apk.ContentShellActivityTestRule;
/** /**
* Integration tests for JavaScript execution. * Integration tests for JavaScript execution.
*/ */
public class TestsJavaScriptEvalTest extends ContentShellTestBase { @RunWith(BaseJUnit4ClassRunner.class)
public class TestsJavaScriptEvalTest {
@Rule
public ContentShellActivityTestRule mActivityTestRule = new ContentShellActivityTestRule();
private static final String JSTEST_URL = UrlUtils.encodeHtmlDataUri("<html><head><script>" private static final String JSTEST_URL = UrlUtils.encodeHtmlDataUri("<html><head><script>"
+ " function foobar() { return 'foobar'; }" + " function foobar() { return 'foobar'; }"
+ "</script></head>" + "</script></head>"
...@@ -28,22 +38,23 @@ public class TestsJavaScriptEvalTest extends ContentShellTestBase { ...@@ -28,22 +38,23 @@ public class TestsJavaScriptEvalTest extends ContentShellTestBase {
* Tests that evaluation of JavaScript for test purposes (using JavaScriptUtils, DOMUtils etc) * Tests that evaluation of JavaScript for test purposes (using JavaScriptUtils, DOMUtils etc)
* works even in presence of "background" (non-test-initiated) JavaScript evaluation activity. * works even in presence of "background" (non-test-initiated) JavaScript evaluation activity.
*/ */
@Test
@LargeTest @LargeTest
@Feature({"Browser"}) @Feature({"Browser"})
public void testJavaScriptEvalIsCorrectlyOrdered() public void testJavaScriptEvalIsCorrectlyOrdered()
throws InterruptedException, Exception, Throwable { throws InterruptedException, Exception, Throwable {
launchContentShellWithUrl(JSTEST_URL); mActivityTestRule.launchContentShellWithUrl(JSTEST_URL);
waitForActiveShellToBeDoneLoading(); mActivityTestRule.waitForActiveShellToBeDoneLoading();
final WebContents webContents = getWebContents(); final WebContents webContents = mActivityTestRule.getWebContents();
for (int i = 0; i < 30; ++i) { for (int i = 0; i < 30; ++i) {
for (int j = 0; j < 10; ++j) { for (int j = 0; j < 10; ++j) {
// Start evaluation of a JavaScript script -- we don't need a result. // Start evaluation of a JavaScript script -- we don't need a result.
webContents.evaluateJavaScriptForTests("foobar();", null); webContents.evaluateJavaScriptForTests("foobar();", null);
} }
// DOMUtils does need to evaluate a JavaScript and get its result to get DOM bounds. // DOMUtils does need to evaluate a JavaScript and get its result to get DOM bounds.
assertNotNull("Failed to get bounds", Assert.assertNotNull(
DOMUtils.getNodeBounds(webContents, "test")); "Failed to get bounds", DOMUtils.getNodeBounds(webContents, "test"));
} }
} }
} }
// Copyright 2017 The Chromium Authors. All rights reserved. // Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
......
// Copyright 2017 The Chromium Authors. All rights reserved. // Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
......
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