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