Skip to content

Instantly share code, notes, and snippets.

@leviyehonatan
Last active February 17, 2016 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leviyehonatan/bd8730df455b48e63eec to your computer and use it in GitHub Desktop.
Save leviyehonatan/bd8730df455b48e63eec to your computer and use it in GitHub Desktop.
a reproduction of a scenario which produces a problem with robolectric ShadowBroadcastReceiver
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test">
<application android:allowBackup="true" android:label="@string/app_name">
<receiver android:name=".Receiver" >
<intent-filter>
<action android:name="com.test.message" />
</intent-filter>
</receiver>
</application>
</manifest>
java.lang.IllegalStateException: Broadcast already finished
at com.google.common.base.Preconditions.checkState(Preconditions.java:174)
at org.robolectric.shadows.ShadowBroadcastPendingResult.finish(ShadowBroadcastPendingResult.java:35)
at android.content.BroadcastReceiver$PendingResult.finish(BroadcastReceiver.java)
at org.robolectric.shadows.ShadowBroadcastReceiver.onReceive(ShadowBroadcastReceiver.java:40)
at org.robolectric.shadows.ShadowApplication$6$1.run(ShadowApplication.java:550)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:144)
at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:30)
at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:105)
at org.robolectric.util.Scheduler$ScheduledRunnable.run(Scheduler.java:335)
at org.robolectric.util.Scheduler.runOneTask(Scheduler.java:237)
at org.robolectric.util.Scheduler.advanceTo(Scheduler.java:218)
at org.robolectric.util.Scheduler.advanceBy(Scheduler.java:201)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:308)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:143)
at org.robolectric.shadows.ShadowMessageQueue.enqueueMessage(ShadowMessageQueue.java:126)
at android.os.MessageQueue.enqueueMessage(MessageQueue.java)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.post(Handler.java:326)
at org.robolectric.shadows.ShadowApplication.postIntent(ShadowApplication.java:492)
at org.robolectric.shadows.ShadowApplication.postToWrappers(ShadowApplication.java:504)
at org.robolectric.shadows.ShadowApplication.sendBroadcastWithPermission(ShadowApplication.java:594)
at org.robolectric.shadows.ShadowApplication.sendBroadcast(ShadowApplication.java:440)
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java)
at com.test.TestBroadcast.test(TestBroadcast.java:24)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:553)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:258)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:154)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code 255
package com.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class Receiver extends BroadcastReceiver {
final String TAG = "Test";
final String MESSAGE_INTENT = "com.test.message";
final String MESSAGE = "message";
final String OUTER = "outer";
final String INNER = "inner";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MESSAGE_INTENT)) {
String message = intent.getStringExtra(MESSAGE);
if (message.equals(OUTER)) {
Log.i(TAG, "OUTER");
context.sendOrderedBroadcast(new Intent(MESSAGE_INTENT).putExtra(MESSAGE, INNER), null, new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "result");
}
}, null, 0, null, null);
} else if (message.equals(INNER)) {
Log.i(TAG, "INNER");
}
}
}
}
package com.test;
import android.content.Intent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
/**
* Created by nickst on 17/02/2016.
*/
@RunWith(RobolectricTestRunner.class)
@Config(sdk= {21}, manifest = "src/test/java/AndroidManifest.xml")
public class TestBroadcast {
@Test
public void test() {
RuntimeEnvironment.application.sendBroadcast(new Intent("com.test.message").putExtra("message", "outer"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment