Closed fhsiao closed 2 years ago
Hi @fhsiao , could you help to test it on 4.7.3? The 4.5.1 is old now. If 4.7.3 also has this problem, you can attach a reproduce-app by following https://github.com/robolectric/robolectric/wiki/How-to-Report-Issues, and we can analyze this problem based on your provided sample.
Hi @utzcoz , I have tried 4.7.3 and the results are the same (observed in 4.4.1 and above). The issue should be reproducible by using the sample app linked above. One thing noticed is that "Robolectric currently supports APIs: 16, 17, 18, 19 and 21" but my app has to use minSdkVersion 29. Would that be an issue?
The issue can been seen using Shadows, however it seems the root cause is that the receiver didn't receive intents during testings.
List<Intent> intents = Shadows.shadowOf(context).getBroadcastIntents();
@fhsiao Looks like LocalBroadcastReceiver
manages broadcast internally(https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-viewpager2-release/localbroadcastmanager/src/main/java/androidx/localbroadcastmanager/content/LocalBroadcastManager.java#218, https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-viewpager2-release/localbroadcastmanager/src/main/java/androidx/localbroadcastmanager/content/LocalBroadcastManager.java#278), and it doesn't use system's mechanism to manage broadcast intents. I think if you use LocalBroadcastManager#sendBroadcastSync
you can receive onReceive
calling. If sendBroadcastSync
works for you, could you try to add @LooperMode(LooperMode.Mode.LEGACY)
to your test class to use legacy looper mode? Looks like Robolectric 4.4 started to use paused mode for looper default(https://github.com/robolectric/robolectric/releases/tag/robolectric-4.4), and it maybe affect LocalBroadcastManager
's mechanism to executing receivers' onReceive
method to send broadcast intent based on Handler
(https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-viewpager2-release/localbroadcastmanager/src/main/java/androidx/localbroadcastmanager/content/LocalBroadcastManager.java#280).
@utzcoz Thank you for the suggestions.
As mentioned, intents were received by using sendBroadcastSync
.
However @LooperMode(LooperMode.Mode.LEGACY)
has been deprecated.
https://github.com/robolectric/robolectric/pull/5490
Since currently we are using sendBroadcast
and there could be implications of using the sendBroadcastSync
instead such as the blocking natural, is there any other alternative solutions that could be implemented in the test cases to solve the issue?
Thank you!
@fhsiao Okay, your testing result confirmed by guess, and it is caused by new looper mode. You can follow http://robolectric.org/blog/2019/06/04/paused-looper/ to migrate your test code to work with new looper mode.
@utzcoz Thank you for looking into this issue. Its confirmed that the issue has been resolved by http://robolectric.org/blog/2019/06/04/paused-looper/ by design.
Description
Upgrading robolectric from 4.3.1 to 4.5.1 (also tested 4.4.1 and above) , LocalBroadcastReceiver registered receiver no longer receives intents when running test cases. On a physical phone, the intents were received.
Code: `public class MyActivity extends Activity {
}`
My testing code:
`@RunWith(RobolectricTestRunner.class) public class MyActivityTest {
}`
Settings: `apply plugin: 'com.android.application' android { compileSdkVersion 30 buildToolsVersion "30.0.2"
}
dependencies { testImplementation 'junit:junit:4.13.2' testImplementation 'com.squareup.assertj:assertj-android:1.1.0' testImplementation 'org.robolectric:robolectric:4.5.1' testImplementation 'org.mockito:mockito-inline:3.9.0' implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' }`
Testing failed and
onReceive(Context context, Intent intent)
is not called.Steps to Reproduce
When changing back to the version of 4.3.1,
onReceive(Context context, Intent intent)
will be called and intents will be received.With current testImplementation 'org.robolectric:robolectric:4.5.1':
Change to testImplementation 'org.robolectric:robolectric:4.3.1':
Robolectric & Android Version
Robolectric: 4.5.1 Android Q
Link to a public git repo demonstrating the problem:
https://github.com/fhsiao/robolectricbug