<file_sep>package com.sebamaty.easyuitests;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.assertion.ViewAssertions;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.espresso.matcher.ViewMatchers;

import org.hamcrest.Matchers;

public class UiTestsHelper {

    public static void clickOnView(int viewId) {
        Espresso.onView(ViewMatchers.withId(viewId)).perform(ViewActions.click());
    }

    public static void clickOnViewWithText(String text) {
        Espresso.onView(ViewMatchers.withText(text)).perform(ViewActions.click());
    }

    public static void scrollToAndClickOnView(int viewId) {
        Espresso.onView(ViewMatchers.withId(viewId)).perform(ViewActions.scrollTo(), ViewActions.click());
    }

    public static void scrollToAndClickOnViewWithText(String text) {
        Espresso.onView(ViewMatchers.withText(text)).perform(ViewActions.scrollTo(), ViewActions.click());
    }

    public static void checkIfViewIsDisplayed(int viewId) {
        Espresso.onView(ViewMatchers.withId(viewId)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    }

    public static void checkIfViewIsNotDisplayed(int viewId) {
        Espresso.onView(ViewMatchers.withId(viewId)).check(ViewAssertions.matches(Matchers.not(ViewMatchers.isDisplayed())));
    }

    public static void checkIfViewWithTextIsDisplayed(String text) {
        Espresso.onView(ViewMatchers.withText(text)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    }

    public static void checkIfViewWithTextIsNotDisplayed(String text) {
        Espresso.onView(ViewMatchers.withText(text)).check(ViewAssertions.matches(Matchers.not(ViewMatchers.isDisplayed())));
    }

    public static void assertTextOnViewWithId(int viewId, String text) {
        Espresso.onView(ViewMatchers.withId(viewId)).check(ViewAssertions.matches(ViewMatchers.withText(text)));
    }

    public static void replaceTextOnView(int viewId, String text) {
        Espresso.onView(ViewMatchers.withId(viewId)).perform(ViewActions.replaceText(text));
    }

    public static void clickOnItemInListView(int listViewId, int itemPosition) {
        Espresso.onData(Matchers.anything())
                .inAdapterView(ViewMatchers.withId(listViewId))
                .atPosition(itemPosition)
                .perform(ViewActions.click());
    }

    public static void assertTextOnListViewItem(String text) {
        Espresso.onData(Matchers.allOf(Matchers.is(Matchers.instanceOf(String.class)), Matchers.is(text))).check(ViewAssertions.matches(ViewMatchers.withText(text)));
    }

    public static void assertTextOnRecyclerViewItem(int recyclerViewId, int position, int childViewId, String text) {
        Espresso.onView(withRecyclerView(recyclerViewId)
                .atPositionOnView(position, childViewId))
                .check(ViewAssertions.matches(ViewMatchers.withText(text)));
    }

    public static void clickOnItemInRecyclerView(int recyclerViewId, int position) {
        Espresso.onView(Matchers.allOf(ViewMatchers.withId(recyclerViewId), ViewMatchers.isCompletelyDisplayed()))
                .perform(RecyclerViewActions.actionOnItemAtPosition(position, ViewActions.click()));
    }

    public static void scrollToPositionInRecyclerView(int recyclerViewId, int position) {
        Espresso.onView(Matchers.allOf(ViewMatchers.withId(recyclerViewId), ViewMatchers.isCompletelyDisplayed()))
                .perform(RecyclerViewActions.actionOnItemAtPosition(position, ViewActions.scrollTo()));
    }

    public static void clickOnItemChildInRecyclerView(int recyclerViewId, int position, int childViewId) {
        Espresso.onView(Matchers.allOf(ViewMatchers.withId(recyclerViewId), ViewMatchers.isCompletelyDisplayed()))
                .perform(RecyclerViewActions.actionOnItemAtPosition(position, ChildViewAction.clickChildViewWithId(childViewId)));
    }

    private static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) {
        return new RecyclerViewMatcher(recyclerViewId);
    }

    public static void isButtonSelected(int buttonId) {
        Espresso.onView(ViewMatchers.withId(buttonId)).check(ViewAssertions.matches(ViewMatchers.isSelected()));
    }
}
<file_sep># easy-ui-tests
A helper library for Android that reduces the boilerplate code when writing UI tests with Espresso.

# Usage example
Add the following code to a UI test class:
```java
@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

@Test
public void assertWelcomeText() throws Exception {
    UiTestsHelper.assertTextOnViewWithId(R.id.welcome_text_view, "Hello World!");
}
```
# What can you test with it?
All methods below are static and void
```java
assertTextOnListViewItem(String text)

assertTextOnRecyclerViewItem(int recyclerViewId, int position, int childViewId, String text)

assertTextOnViewWithId(int viewId, String text)

checkIfViewIsDisplayed(int viewId)

checkIfViewIsNotDisplayed(int viewId)

checkIfViewWithTextIsDisplayed(String text)

checkIfViewWithTextIsNotDisplayed(String text)

clickOnItemChildInRecyclerView(int recyclerViewId, int position, int childViewId)

clickOnItemInListView(int listViewId, int itemPosition)

clickOnItemInRecyclerView(int recyclerViewId, int position)

clickOnView(int viewId)

clickOnViewWithText(String text)

isButtonSelected(int buttonId)

replaceTextOnView(int viewId, String text)

scrollToAndClickOnView(int viewId)

scrollToAndClickOnViewWithText(String text)

scrollToPositionInRecyclerView(int recyclerViewId, int position)
```
# Download
```gradle
androidTestCompile 'com.sebamaty:easyuitests:1.0.6'
```
Make sure you have added [JUnit][junit] dependency. Android Studio won't find @Rule annotation and ActivityTestRule class otherwise.
[junit]: https://github.com/junit-team/junit4/wiki/Use-with-Gradle

# License
```
Copyright 2016 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
<file_sep>include ':app', ':easyuitests'
<file_sep>package com.sebamaty.easyuitestsexample;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.sebamaty.easyuitests.UiTestsHelper;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    @Rule
    public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void assertWelcomeText() throws Exception {
        UiTestsHelper.assertTextOnViewWithId(R.id.welcome_text_view, "Hello World!");
    }
}

