Issue
I have an application that accepts deeplink.
Manifest.xml:
<activity
android:name=".activities.unsigned.MagicLink"
android:label="Some test">
<intent-filter android:label="Test">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.myapp" />
</intent-filter>
</activity>
<activity
android:name=".activities.unsigned.MainScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Activity:
public class MagicLink extends BusAppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent != null && intent.getAction() != null) {
Uri data = intent.getData();
ServicesApi servicesApi = ServicesApi.init(this);
servicesApi.setSessionId(data.getQueryParameter(HttpRemoteApi.SESSION_ID));
startActivity(new Intent(this, LoginActivity.class));
}
}
}
This thing works perfectly if user use it. Well I want to create a test for it now. So I write something like this:
androidTest:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginTest {
@Rule
public final ActivityTestRule<MainScreen> main = new ActivityTestRule<>(MainScreen.class);
@Test
public void checkSmth() {
clickMagicLink();
//...
}
private void clickMagicLink() {
String url = "com.myapp://login?session_id="+utils.getSessionId();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
main.launchActivity(i);
}
}
But instead of starting MagicLink
activity this thing starts MainScreen
activity (which is MAIN
). What do I do wrong?
P.s. I also saw something like this: new ActivityTestRule<>(MainScreen.class,true, false);
. But with this constructor my test start, but android app doesn't (I mean emulator starts but app doesn't)
Solution
ActivityTestRule.launchActivity()
always starts the activity being tested. You cannot use it to start any other activity. In this case, it will always start MainActivity
. The Intent
parameter is passed to the activity. This allows you to send extras during a test. The intent is not used to select which activity to launch.
Also note that the docs say
Don't call this method directly, unless you explicitly requested not to lazily launch the Activity manually using the launchActivity flag in
ActivityTestRule(Class, boolean, boolean)
.
If you want to test your MagicLink
activity, you can use ActivityTestRule<MagicLink>
:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MagicLinkTest {
@Rule
public final ActivityTestRule<MagicLink> main = new ActivityTestRule<>(MainScreen.class, false, false);
@Test
public void testMagicLink() {
String url = "com.myapp://login?session_id="+utils.getSessionId();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
main.launchActivity(i);
// assertions go here
}
}
You can also use ActivityTestRule<MainScreen>
but you have to simulate the exact same actions as an actual user.
Answered By - Code-Apprentice
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.