Issue
Let's say I have an app with an input field and a button. I create a new Activity when the user enters the correct number and presses the button. How can I test that the correct intent was fired and everything in the new Activity is in place using Espresso?
Solution
I understood that you have an EditText and a Button, when the user clicks the button, you will start a new activity just if the text in the edit text is equal to a specific text.
Here's a full working code with explanations:
activity_home.xml
Let's say the activity that has both the Button and the EditText is HomeActivity
Create its layout as the followings:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<EditText
android:id="@+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go" />
</LinearLayout>
HomeActivity.java
Create your java activity that achieves your logic as the followings:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class HomeActivity extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
editText = (EditText)findViewById(R.id.edit_text);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = editText.getText().toString();
if (text.equals("1")) {
Intent intent = new Intent(HomeActivity.this, ThirdActivity.class);
startActivity(intent);
}
}
});
}
}
Notice that What I am here is just checking if the text is equal to 1 then I start a new activity, which I call it ThirdActivity
HomeActivityText.java
Create a class named HomeActivityText inside your androidTest folder
import android.app.Activity;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import android.support.test.runner.lifecycle.Stage;
import junit.framework.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Collection;
@RunWith(AndroidJUnit4.class)
public class HomeActivityTest {
@Rule
public ActivityTestRule<HomeActivity> mActivityRule =
new ActivityTestRule<>(HomeActivity.class);
@Test
public void testHappyScenario() {
Espresso.onView(ViewMatchers.withId(R.id.edit_text)).perform(ViewActions.typeText("1"));
Espresso.onView(ViewMatchers.withId(R.id.button)).perform(ViewActions.click());
Assert.assertEquals(getActivityInstance().getClass(), ThirdActivity.class);
}
private Activity getActivityInstance() {
final Activity[] currentActivity = {null};
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
if (resumedActivities.iterator().hasNext()) {
currentActivity[0] = (Activity) resumedActivities.iterator().next();
}
}
});
return currentActivity[0];
}
}
The test will pass when you enter 1 as the value of the edit text, and will fail otherwise.
The code for the unit test is simulating the happy scenario, in which you set the value 1 to the edit text then you click the button then you check if the new activity was started
Notice that I developed a function to retrieve the last activity that is shown in the device and compare its class to the intended ThirdActivity class
Could you let me know please if there is something not clear?
Answered By - William Kinaan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.