1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/tests/testOrderedBroadcast.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,65 @@ 1.4 +package org.mozilla.gecko.tests; 1.5 + 1.6 +import org.json.JSONException; 1.7 +import org.json.JSONObject; 1.8 + 1.9 +import android.app.Activity; 1.10 +import android.content.BroadcastReceiver; 1.11 +import android.content.Context; 1.12 +import android.content.Intent; 1.13 +import android.content.IntentFilter; 1.14 + 1.15 + 1.16 +public class testOrderedBroadcast extends JavascriptTest { 1.17 + protected BroadcastReceiver mReceiver; 1.18 + 1.19 + public testOrderedBroadcast() { 1.20 + super("testOrderedBroadcast.js"); 1.21 + } 1.22 + 1.23 + @Override 1.24 + public void setUp() throws Exception { 1.25 + super.setUp(); 1.26 + 1.27 + mAsserter.dumpLog("Registering org.mozilla.gecko.test.receiver broadcast receiver"); 1.28 + 1.29 + IntentFilter filter = new IntentFilter(); 1.30 + filter.addAction("org.mozilla.gecko.test.receiver"); 1.31 + 1.32 + mReceiver = new BroadcastReceiver() { 1.33 + @Override 1.34 + public void onReceive(Context context, Intent intent) { 1.35 + try { 1.36 + JSONObject o = new JSONObject(); 1.37 + o.put("c", "efg"); 1.38 + o.put("d", 456); 1.39 + // Feed the received token back to the sender. 1.40 + o.put("token", intent.getStringExtra("token")); 1.41 + String data = o.toString(); 1.42 + 1.43 + setResultCode(Activity.RESULT_OK); 1.44 + setResultData(data); 1.45 + } catch (JSONException e) { 1.46 + setResultCode(Activity.RESULT_CANCELED); 1.47 + setResultData(null); 1.48 + } 1.49 + } 1.50 + }; 1.51 + 1.52 + // We must register the receiver in a Fennec context to avoid a 1.53 + // SecurityException. 1.54 + getActivity().getApplicationContext().registerReceiver(mReceiver, filter); 1.55 + } 1.56 + 1.57 + @Override 1.58 + public void tearDown() throws Exception { 1.59 + super.tearDown(); 1.60 + 1.61 + mAsserter.dumpLog("Unregistering org.mozilla.gecko.test.receiver broadcast receiver"); 1.62 + 1.63 + if (mReceiver != null) { 1.64 + getActivity().getApplicationContext().unregisterReceiver(mReceiver); 1.65 + mReceiver = null; 1.66 + } 1.67 + } 1.68 +}