Bug 1253381 - Move existing JavascriptBridge tests to JavascriptBridgeTest. r=sebastian

MozReview-Commit-ID: 7UQfbpzpGAg

--HG--
extra : rebase_source : 2a49038bcbf11de915fb85b2631c6f68bf30d3c7
This commit is contained in:
Michael Comella 2016-03-03 13:34:39 -08:00
parent 1f8aa986b9
commit b900750aa2
4 changed files with 39 additions and 94 deletions

View File

@ -6,8 +6,6 @@ package org.mozilla.gecko.tests;
import static org.mozilla.gecko.tests.helpers.AssertionHelper.*;
import org.mozilla.gecko.tests.helpers.*;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.util.BundleEventListener;
import org.mozilla.gecko.util.EventCallback;
@ -26,7 +24,7 @@ import org.json.JSONObject;
* Tests the proper operation of EventDispatcher,
* including associated NativeJSObject objects.
*/
public class testEventDispatcher extends UITest
public class testEventDispatcher extends JavascriptBridgeTest
implements BundleEventListener, GeckoEventListener, NativeEventListener {
private static final String TEST_JS = "testEventDispatcher.js";
@ -42,7 +40,6 @@ public class testEventDispatcher extends UITest
private static final long WAIT_FOR_BUNDLE_EVENT_TIMEOUT_MILLIS = 20000; // 20 seconds
private JavascriptBridge js;
private NativeJSObject savedMessage;
private boolean handledGeckoEvent;
@ -52,7 +49,6 @@ public class testEventDispatcher extends UITest
@Override
public void setUp() throws Exception {
super.setUp();
js = new JavascriptBridge(this);
EventDispatcher.getInstance().registerGeckoThreadListener(
(GeckoEventListener) this, GECKO_EVENT, GECKO_RESPONSE_EVENT);
@ -77,7 +73,6 @@ public class testEventDispatcher extends UITest
EventDispatcher.getInstance().unregisterBackgroundThreadListener(
this, BACKGROUND_EVENT, BACKGROUND_RESPONSE_EVENT);
js.disconnect();
super.tearDown();
}
@ -103,22 +98,21 @@ public class testEventDispatcher extends UITest
}
public void testEventDispatcher() {
GeckoHelper.blockForReady();
NavigationHelper.enterAndLoadUrl(mStringHelper.getHarnessUrlForJavascript(TEST_JS));
blockForReadyAndLoadJS(TEST_JS);
js.syncCall("send_test_message", GECKO_EVENT);
getJS().syncCall("send_test_message", GECKO_EVENT);
fAssertTrue("Should have handled Gecko event synchronously", handledGeckoEvent);
js.syncCall("send_message_for_response", GECKO_RESPONSE_EVENT, "success");
js.syncCall("send_message_for_response", GECKO_RESPONSE_EVENT, "error");
getJS().syncCall("send_message_for_response", GECKO_RESPONSE_EVENT, "success");
getJS().syncCall("send_message_for_response", GECKO_RESPONSE_EVENT, "error");
js.syncCall("send_test_message", NATIVE_EVENT);
getJS().syncCall("send_test_message", NATIVE_EVENT);
fAssertTrue("Should have handled native event synchronously", handledNativeEvent);
js.syncCall("send_message_for_response", NATIVE_RESPONSE_EVENT, "success");
js.syncCall("send_message_for_response", NATIVE_RESPONSE_EVENT, "error");
getJS().syncCall("send_message_for_response", NATIVE_RESPONSE_EVENT, "success");
getJS().syncCall("send_message_for_response", NATIVE_RESPONSE_EVENT, "error");
js.syncCall("send_test_message", NATIVE_EXCEPTION_EVENT);
getJS().syncCall("send_test_message", NATIVE_EXCEPTION_EVENT);
fAssertNotSame("Should have saved a message", null, savedMessage);
try {
savedMessage.toString();
@ -126,25 +120,25 @@ public class testEventDispatcher extends UITest
} catch (final NullPointerException e) {
}
js.syncCall("send_test_message", UI_EVENT);
getJS().syncCall("send_test_message", UI_EVENT);
waitForAsyncEvent();
js.syncCall("send_message_for_response", UI_RESPONSE_EVENT, "success");
getJS().syncCall("send_message_for_response", UI_RESPONSE_EVENT, "success");
waitForAsyncEvent();
js.syncCall("send_message_for_response", UI_RESPONSE_EVENT, "error");
getJS().syncCall("send_message_for_response", UI_RESPONSE_EVENT, "error");
waitForAsyncEvent();
js.syncCall("send_test_message", BACKGROUND_EVENT);
getJS().syncCall("send_test_message", BACKGROUND_EVENT);
waitForAsyncEvent();
js.syncCall("send_message_for_response", BACKGROUND_RESPONSE_EVENT, "success");
getJS().syncCall("send_message_for_response", BACKGROUND_RESPONSE_EVENT, "success");
waitForAsyncEvent();
js.syncCall("send_message_for_response", BACKGROUND_RESPONSE_EVENT, "error");
getJS().syncCall("send_message_for_response", BACKGROUND_RESPONSE_EVENT, "error");
waitForAsyncEvent();
js.syncCall("finish_test");
getJS().syncCall("finish_test");
}
@Override

View File

@ -10,9 +10,6 @@ import com.jayway.android.robotium.solo.Condition;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.tests.helpers.AssertionHelper;
import org.mozilla.gecko.tests.helpers.GeckoHelper;
import org.mozilla.gecko.tests.helpers.JavascriptBridge;
import org.mozilla.gecko.tests.helpers.NavigationHelper;
import org.mozilla.gecko.tests.helpers.WaitHelper;
import org.mozilla.gecko.util.GeckoRequest;
import org.mozilla.gecko.util.NativeJSObject;
@ -20,51 +17,36 @@ import org.mozilla.gecko.util.NativeJSObject;
/**
* Tests sending and receiving Gecko requests using the GeckoRequest API.
*/
public class testGeckoRequest extends UITest {
public class testGeckoRequest extends JavascriptBridgeTest {
private static final String TEST_JS = "testGeckoRequest.js";
private static final String REQUEST_EVENT = "Robocop:GeckoRequest";
private static final String REQUEST_EXCEPTION_EVENT = "Robocop:GeckoRequestException";
private static final int MAX_WAIT_MS = 5000;
private JavascriptBridge js;
@Override
public void setUp() throws Exception {
super.setUp();
js = new JavascriptBridge(this);
}
@Override
public void tearDown() throws Exception {
js.disconnect();
super.tearDown();
}
public void testGeckoRequest() {
GeckoHelper.blockForReady();
NavigationHelper.enterAndLoadUrl(mStringHelper.getHarnessUrlForJavascript(TEST_JS));
blockForReadyAndLoadJS(TEST_JS);
// Register a listener for this request.
js.syncCall("add_request_listener", REQUEST_EVENT);
getJS().syncCall("add_request_listener", REQUEST_EVENT);
// Make sure we receive the expected response.
checkFooRequest();
// Try registering a second listener for this request, which should fail.
js.syncCall("add_second_request_listener", REQUEST_EVENT);
getJS().syncCall("add_second_request_listener", REQUEST_EVENT);
// Unregister the listener for this request.
js.syncCall("remove_request_listener", REQUEST_EVENT);
getJS().syncCall("remove_request_listener", REQUEST_EVENT);
// Make sure we don't receive a response after removing the listener.
checkUnregisteredRequest();
// Check that we still receive a response for listeners that throw.
js.syncCall("add_exception_listener", REQUEST_EXCEPTION_EVENT);
getJS().syncCall("add_exception_listener", REQUEST_EXCEPTION_EVENT);
checkExceptionRequest();
js.syncCall("remove_request_listener", REQUEST_EXCEPTION_EVENT);
getJS().syncCall("remove_request_listener", REQUEST_EXCEPTION_EVENT);
js.syncCall("finish_test");
getJS().syncCall("finish_test");
}
private void checkFooRequest() {

View File

@ -15,7 +15,6 @@ import static org.mozilla.gecko.tests.helpers.WaitHelper.waitFor;
import org.mozilla.gecko.tests.components.GeckoViewComponent.InputConnectionTest;
import org.mozilla.gecko.tests.helpers.GeckoHelper;
import org.mozilla.gecko.tests.helpers.JavascriptBridge;
import org.mozilla.gecko.tests.helpers.NavigationHelper;
import com.jayway.android.robotium.solo.Condition;
@ -27,24 +26,10 @@ import android.view.inputmethod.InputConnection;
/**
* Tests the proper operation of GeckoInputConnection
*/
public class testInputConnection extends UITest {
public class testInputConnection extends JavascriptBridgeTest {
private static final String INITIAL_TEXT = "foo";
private JavascriptBridge js;
@Override // UITest
public void setUp() throws Exception {
super.setUp();
js = new JavascriptBridge(this);
}
@Override // UITest
public void tearDown() throws Exception {
js.disconnect();
super.tearDown();
}
public void testInputConnection() throws InterruptedException {
GeckoHelper.blockForReady();
@ -53,18 +38,18 @@ public class testInputConnection extends UITest {
mToolbar.assertTitle(url);
// First run tests inside the normal input field.
js.syncCall("focus_input", INITIAL_TEXT);
getJS().syncCall("focus_input", INITIAL_TEXT);
mGeckoView.mTextInput
.waitForInputConnection()
.testInputConnection(new BasicInputConnectionTest());
// Then switch focus to the resetting input field, and run tests there.
js.syncCall("focus_resetting_input", "");
getJS().syncCall("focus_resetting_input", "");
mGeckoView.mTextInput
.waitForInputConnection()
.testInputConnection(new ResettingInputConnectionTest());
js.syncCall("finish_test");
getJS().syncCall("finish_test");
}
private class BasicInputConnectionTest extends InputConnectionTest {
@ -182,7 +167,7 @@ public class testInputConnection extends UITest {
// Bug 1051556, exception due to committing text changes during flushing.
ic.setComposingText("bad", 1);
assertTextAndSelectionAt("Can set the composing text", ic, "bad", 3);
js.asyncCall("test_reflush_changes");
getJS().asyncCall("test_reflush_changes");
// Wait for text change notifications to come in.
processGeckoEvents(ic);
assertTextAndSelectionAt("Can re-flush text changes", ic, "good", 4);
@ -197,7 +182,7 @@ public class testInputConnection extends UITest {
// Bug 1241558 - wrong selection due to ignoring selection notification.
ic.setComposingText("foobar", 1);
assertTextAndSelectionAt("Can set the composing text", ic, "foobar", 6);
js.asyncCall("test_set_selection");
getJS().asyncCall("test_set_selection");
// Wait for text change notifications to come in.
processGeckoEvents(ic);
assertTextAndSelectionAt("Can select after committing", ic, "foobar", 3);

View File

@ -6,8 +6,6 @@ package org.mozilla.gecko.tests;
import static org.mozilla.gecko.tests.helpers.AssertionHelper.*;
import org.mozilla.gecko.tests.helpers.*;
import org.json.JSONException;
import org.json.JSONObject;
@ -15,47 +13,33 @@ import org.json.JSONObject;
* Tests the proper operation of JavascriptBridge and JavaBridge,
* which are used by tests for communication between Java and JS.
*/
public class testJavascriptBridge extends UITest {
public class testJavascriptBridge extends JavascriptBridgeTest {
private static final String TEST_JS = "testJavascriptBridge.js";
private JavascriptBridge js;
private boolean syncCallReceived;
@Override
public void setUp() throws Exception {
super.setUp();
js = new JavascriptBridge(this);
}
@Override
public void tearDown() throws Exception {
js.disconnect();
super.tearDown();
}
public void testJavascriptBridge() {
GeckoHelper.blockForReady();
NavigationHelper.enterAndLoadUrl(mStringHelper.getHarnessUrlForJavascript(TEST_JS));
js.syncCall("check_js_int_arg", 1);
blockForReadyAndLoadJS(TEST_JS);
getJS().syncCall("check_js_int_arg", 1);
}
public void checkJavaIntArg(final int int2) {
// Async call from JS
fAssertEquals("Integer argument matches", 2, int2);
js.syncCall("check_js_double_arg", 3.0D);
getJS().syncCall("check_js_double_arg", 3.0D);
}
public void checkJavaDoubleArg(final double double4) {
// Async call from JS
fAssertEquals("Double argument matches", 4.0, double4);
js.syncCall("check_js_boolean_arg", false);
getJS().syncCall("check_js_boolean_arg", false);
}
public void checkJavaBooleanArg(final boolean booltrue) {
// Async call from JS
fAssertEquals("Boolean argument matches", true, booltrue);
js.syncCall("check_js_string_arg", "foo");
getJS().syncCall("check_js_string_arg", "foo");
}
public void checkJavaStringArg(final String stringbar) throws JSONException {
@ -63,19 +47,19 @@ public class testJavascriptBridge extends UITest {
fAssertEquals("String argument matches", "bar", stringbar);
final JSONObject obj = new JSONObject();
obj.put("caller", "java");
js.syncCall("check_js_object_arg", (JSONObject) obj);
getJS().syncCall("check_js_object_arg", (JSONObject) obj);
}
public void checkJavaObjectArg(final JSONObject obj) throws JSONException {
// Async call from JS
fAssertEquals("Object argument matches", "js", obj.getString("caller"));
js.syncCall("check_js_sync_call");
getJS().syncCall("check_js_sync_call");
}
public void doJSSyncCall() {
// Sync call from JS
syncCallReceived = true;
js.asyncCall("respond_to_js_sync_call");
getJS().asyncCall("respond_to_js_sync_call");
}
public void checkJSSyncCallReceived() {