Bug 889185 - Part 1: Test OrderedBroadcast token handling. r=rnewman

This commit is contained in:
Nick Alexander 2013-07-08 13:55:20 -07:00
parent d9dc390346
commit e599e9ae61
3 changed files with 57 additions and 12 deletions

View File

@ -73,7 +73,7 @@ public final class OrderedBroadcastHelper
}
// It's fine if the caller-provided token is missing or null.
final Object token = (message.has("token") && !message.isNull("token")) ?
final JSONObject token = (message.has("token") && !message.isNull("token")) ?
message.getJSONObject("token") : null;
// And a missing or null permission means no permission.
@ -105,6 +105,12 @@ public final class OrderedBroadcastHelper
};
Intent intent = new Intent(action);
// OrderedBroadcast.jsm adds its callback ID to the caller's token;
// this unwraps that wrapping.
if (token != null && token.has("data")) {
intent.putExtra("token", token.getString("data"));
}
mContext.sendOrderedBroadcast(intent,
permission,
resultReceiver,

View File

@ -30,19 +30,26 @@ public class testOrderedBroadcast extends JavascriptTest {
mAsserter.dumpLog("Registering org.mozilla.gecko.test.receiver broadcast receiver");
JSONObject o = new JSONObject();
o.put("c", "efg");
o.put("d", 456);
final String data = o.toString();
IntentFilter filter = new IntentFilter();
filter.addAction("org.mozilla.gecko.test.receiver");
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
setResultCode(Activity.RESULT_OK);
setResultData(data);
try {
JSONObject o = new JSONObject();
o.put("c", "efg");
o.put("d", 456);
// Feed the received token back to the sender.
o.put("token", intent.getStringExtra("token"));
String data = o.toString();
setResultCode(Activity.RESULT_OK);
setResultData(data);
} catch (JSONException e) {
setResultCode(Activity.RESULT_CANCELED);
setResultData(null);
}
}
};

View File

@ -31,8 +31,9 @@ add_task(function test_send() {
let observer = makeObserver();
let token = { a: "bcde", b: 1234 };
sendOrderedBroadcast("org.mozilla.gecko.test.receiver",
{ a: "bcde", b: 1234 }, observer.callback);
token, observer.callback);
let value = yield observer.promise;
@ -40,15 +41,19 @@ add_task(function test_send() {
// We get back the correct action and token.
do_check_neq(value, null);
do_check_neq(value.token, null);
do_check_eq(value.token.a, "bcde");
do_check_eq(value.token.b, 1234);
do_check_matches(value.token, token);
do_check_eq(value.action, "org.mozilla.gecko.test.receiver");
// Data is provided by testOrderedBroadcast.java.in.
do_check_neq(value.data, null);
do_check_eq(value.data.c, "efg");
do_check_eq(value.data.d, 456);
// And the provided token is returned to us (as a string) by
// testOrderedBroadcast.java.in.
do_check_neq(value.data.token, null);
do_check_eq(typeof(value.data.token), "string");
do_check_matches(JSON.parse(value.data.token), token);
});
add_task(function test_null_token() {
@ -74,6 +79,33 @@ add_task(function test_null_token() {
do_check_eq(value.data.d, 456);
});
add_task(function test_string_token() {
let deferred = Promise.defer();
let observer = makeObserver();
let token = "string_token";
let permission = null; // means any receiver can listen for our intent
sendOrderedBroadcast("org.mozilla.gecko.test.receiver",
token, observer.callback, permission);
let value = yield observer.promise;
do_check_eq(observer.count, 1);
// We get back the correct action and token.
do_check_neq(value, null);
do_check_eq(value.token, token);
do_check_eq(typeof(value.token), "string");
do_check_eq(value.action, "org.mozilla.gecko.test.receiver");
// Data is provided by testOrderedBroadcast.java.in.
do_check_neq(value.data, null);
do_check_eq(value.data.c, "efg");
do_check_eq(value.data.d, 456);
do_check_eq(value.data.token, token);
});
add_task(function test_permission() {
let deferred = Promise.defer();