Skip to content

Instantly share code, notes, and snippets.

@danielmai
Created December 18, 2015 23:22
Show Gist options
  • Save danielmai/a0ff629047c47387d3a1 to your computer and use it in GitHub Desktop.
Save danielmai/a0ff629047c47387d3a1 to your computer and use it in GitHub Desktop.
Espresso test for Sunshine. Demo for the Android Testing webcast in the Android Developer Nanodegree.
package com.example.android.sunshine.app.ui;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import com.example.android.sunshine.app.MainActivity;
import com.example.android.sunshine.app.R;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.Matchers.anything;
/**
* For more information on how to use Espresso, check out the documentation and cheat sheet:
*
* - https://google.github.io/android-testing-support-library/docs/espresso/index.html
* - https://google.github.io/android-testing-support-library/docs/espresso/cheatsheet/index.html
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ForecastListToDetailTest {
@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity
.class);
@Test
public void testSelectForecast() {
// we're not displaying the detail view yet
onView(withId(R.id.detail_day_textview))
.check(doesNotExist());
// we are displaying the forecast list
onView(withId(R.id.listview_forecast))
.check(matches(isDisplayed()));
// click on today's forecast list item
onData(anything())
.inAdapterView(withId(R.id.listview_forecast))
.atPosition(0)
.perform(click());
// we're displaying the detail view now!
onView(withId(R.id.detail_day_textview))
.check(matches(isDisplayed()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment