robolectric / robolectric

Android Unit Testing Framework
http://robolectric.org
Other
5.9k stars 1.37k forks source link

Request code when starting AppCompatActivity gets corrupted #4051

Open alixwar opened 6 years ago

alixwar commented 6 years ago

Description

The request code changes value during the course of the test. The real value was 2 and was converted to 65538

Steps to Reproduce

Code:

final Intent newIntent = new Intent(context, FooActivity.class);
startActivityForResult(newIntent, 2);

Test code:

final ShadowActivity shadowActivity = Shadow.extract(activity);
assertThat(shadowActivity.peekNextStartedActivityForResult(), notNullValue());
final ShadowActivity.IntentForResult intentForResult = shadowActivity.getNextStartedActivityForResult();
assertThat(intentForResult.requestCode, is(2));

Robolectric & Android Version

Robolectric 4.0 Config: @Config(sdk = Build.VERSION_CODES.LOLLIPOP) @RunWith(AndroidJUnit4.class)

alixwar commented 6 years ago

Debugger:

When it's still OK in the call stack: still ok

When it's converted to something faulty (this will also be the end result in the assertion): not ok

alixwar commented 6 years ago

It looks like android.support.v4.app.FragmentActivity does some weird stuff with the request code:

ActivityCompat.startActivityForResult(this, intent, (requestIndex + 1 << 16) + (requestCode & '\uffff'), options);
alixwar commented 6 years ago

OK, I managed to avoid this situation:

Previously I launched the new activity from a fragment instead of from the parent activity. Launching the activity from the parent activity solved my problem.

Weird behavior anyway... So I leave this ticket open for someone to look into what this means for Robolectric...

mateot1 commented 5 years ago

Just ran into this issue trying out the new FragmentScenario api -- it would be great if the EmptyFragmentActivity's Shadow did something to record the original request code it was given rather than having to figure out what it is after the FragmentActivity processes it.