|
1 package org.mozilla.gecko.tests; |
|
2 |
|
3 import org.json.JSONException; |
|
4 import org.json.JSONObject; |
|
5 |
|
6 import android.app.Activity; |
|
7 import android.content.BroadcastReceiver; |
|
8 import android.content.Context; |
|
9 import android.content.Intent; |
|
10 import android.content.IntentFilter; |
|
11 |
|
12 |
|
13 public class testOrderedBroadcast extends JavascriptTest { |
|
14 protected BroadcastReceiver mReceiver; |
|
15 |
|
16 public testOrderedBroadcast() { |
|
17 super("testOrderedBroadcast.js"); |
|
18 } |
|
19 |
|
20 @Override |
|
21 public void setUp() throws Exception { |
|
22 super.setUp(); |
|
23 |
|
24 mAsserter.dumpLog("Registering org.mozilla.gecko.test.receiver broadcast receiver"); |
|
25 |
|
26 IntentFilter filter = new IntentFilter(); |
|
27 filter.addAction("org.mozilla.gecko.test.receiver"); |
|
28 |
|
29 mReceiver = new BroadcastReceiver() { |
|
30 @Override |
|
31 public void onReceive(Context context, Intent intent) { |
|
32 try { |
|
33 JSONObject o = new JSONObject(); |
|
34 o.put("c", "efg"); |
|
35 o.put("d", 456); |
|
36 // Feed the received token back to the sender. |
|
37 o.put("token", intent.getStringExtra("token")); |
|
38 String data = o.toString(); |
|
39 |
|
40 setResultCode(Activity.RESULT_OK); |
|
41 setResultData(data); |
|
42 } catch (JSONException e) { |
|
43 setResultCode(Activity.RESULT_CANCELED); |
|
44 setResultData(null); |
|
45 } |
|
46 } |
|
47 }; |
|
48 |
|
49 // We must register the receiver in a Fennec context to avoid a |
|
50 // SecurityException. |
|
51 getActivity().getApplicationContext().registerReceiver(mReceiver, filter); |
|
52 } |
|
53 |
|
54 @Override |
|
55 public void tearDown() throws Exception { |
|
56 super.tearDown(); |
|
57 |
|
58 mAsserter.dumpLog("Unregistering org.mozilla.gecko.test.receiver broadcast receiver"); |
|
59 |
|
60 if (mReceiver != null) { |
|
61 getActivity().getApplicationContext().unregisterReceiver(mReceiver); |
|
62 mReceiver = null; |
|
63 } |
|
64 } |
|
65 } |