Skip to content

Commit

Permalink
Add lots of tests on the Barista methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rocboronat committed Dec 30, 2016
1 parent a77faf2 commit 36c3239
Show file tree
Hide file tree
Showing 18 changed files with 375 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/build
/captures
.externalNativeBuild
.idea/
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,47 @@

public class BaristaAssertions {

public static void assertTextIsDisplayed(int id) {
onView(withText(id)).check(matches(isDisplayed()));
public static void assertViewIsDisplayed(@IdRes int id) {
onView(withId(id)).check(matches(isDisplayed()));
}

public static void assertTextIsDisplayed(@StringRes int text) {
onView(withText(text)).check(matches(isDisplayed()));
}

public static void assertTextIsDisplayed(String text) {
onView(withText(text)).check(matches(isDisplayed()));
}

public static void assertTextDoesNotExist(@StringRes int id) {
onView(withText(id)).check(doesNotExist());
public static void assertViewDoesNotExist(@IdRes int viewId) {
onView(withId(viewId)).check(doesNotExist());
}

public static void assertViewIsDisplayed(@IdRes int id) {
onView(withId(id)).check(matches(isDisplayed()));
public static void assertTextDoesNotExist(@StringRes int text) {
onView(withText(text)).check(doesNotExist());
}

public static void assertTextDoesNotExist(String text) {
onView(withText(text)).check(doesNotExist());
}

public static void assertViewIsNotDisplayed(@IdRes int id) {
onView(withId(id)).check(matches(not(isDisplayed())));
}

public static void assertViewDoesNotExist(@IdRes int viewId) {
onView(withId(viewId)).check(doesNotExist());
public static void assertTextIsNotDisplayed(@StringRes int text) {
onView(withText(text)).check(matches(not(isDisplayed())));
}

public static void assertTextIsNotDisplayed(String text) {
onView(withText(text)).check(matches(not(isDisplayed())));
}

public static void assertViewWithIdIsEnabled(@IdRes int viewId) {
public static void assertViewIsEnabled(@IdRes int viewId) {
onView(withId(viewId)).check(matches(isEnabled()));
}

public static void assertViewWithIdIsDisabled(@IdRes int viewId) {
public static void assertViewIsDisabled(@IdRes int viewId) {
onView(withId(viewId)).check(matches(not(isEnabled())));
}

Expand All @@ -57,5 +69,4 @@ public static void assertThatBackButtonClosesTheApp() {
// We expect this Exception if the Activity is the first one of the stack
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;
Expand All @@ -20,25 +21,49 @@

public class BaristaClickActions {

public static void clickScrollingIfNeeded(@IdRes int id) {
try {
scrollToAndClick(id);
} catch (Exception e) {
click(id);
}
}

public static void clickScrollingIfNeeded(String text) {
try {
scrollToAndClick(text);
} catch (Exception e) {
click(text);
}
}

public static void click(@IdRes int id) {
onView(withId(id)).perform(scrollTo(), ViewActions.click());
onView(withId(id)).perform(ViewActions.click());
}

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

public static void clickWithoutScrolling(@IdRes int id) {
onView(withId(id)).perform(ViewActions.click());
public static void scrollToAndClick(@IdRes int id) {
onView(withId(id)).perform(scrollTo(), ViewActions.click());
}

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

public static void clickRadioButtonItem(@IdRes int radioButtonId, int itemToClick) {
onView(allOf(withParent(withId(radioButtonId)), withId(itemToClick))).perform(ViewActions.click());
}

public static void clickBack() {
pressBack();
}

public static void clickListItem(@IdRes int listViewId, int position, Class<?> modelClass) {
// This is ugly, I know. But depending on the layout only one of the following methods will work.
// The try/catch let's us forget about it and always use the same generic method to click on lists.
// The try/catch let's us forget about it and always use the same generic method to clickScrollingIfNeeded on lists.
try {
clickListItemForMultipleListsOnScreen(listViewId, position, modelClass);
} catch (NoMatchingViewException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.schibsted.spain.barista;

import android.support.annotation.IdRes;
import android.support.test.espresso.action.ViewActions;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

public class BaristaScrollActions {

public static void scrollTo(@IdRes int id) {
onView(withId(id)).perform(ViewActions.scrollTo());
}

public static void scrollTo(String text) {
onView(withText(text)).perform(ViewActions.scrollTo());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class BaristaSpinnerActions {

public static void chooseSpinnerPosition(@IdRes int id, Class<?> itemClass, int position) {
click(id);
BaristaClickActions.clickScrollingIfNeeded(id);

onData(is(instanceOf(itemClass)))
.inAdapterView(allOf(isAssignableFrom(AdapterView.class), isDisplayed()))
Expand All @@ -26,7 +26,7 @@ public static void chooseSpinnerPosition(@IdRes int id, Class<?> itemClass, int
}

public static void chooseSpinnerPositions(@IdRes int id, Class<?> itemClass, List<Integer> positions) {
click(id);
BaristaClickActions.clickScrollingIfNeeded(id);
for (int i : positions) {
onData(is(instanceOf(itemClass))).inAdapterView(allOf(isAssignableFrom(AdapterView.class), isDisplayed())).atPosition(i).perform(ViewActions.click());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.schibsted.spain.barista.sample;

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

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

import static com.schibsted.spain.barista.BaristaAssertions.assertTextDoesNotExist;
import static com.schibsted.spain.barista.BaristaAssertions.assertTextIsDisplayed;
import static com.schibsted.spain.barista.BaristaAssertions.assertTextIsNotDisplayed;
import static com.schibsted.spain.barista.BaristaAssertions.assertThatBackButtonClosesTheApp;
import static com.schibsted.spain.barista.BaristaAssertions.assertViewDoesNotExist;
import static com.schibsted.spain.barista.BaristaAssertions.assertViewIsDisabled;
import static com.schibsted.spain.barista.BaristaAssertions.assertViewIsDisplayed;
import static com.schibsted.spain.barista.BaristaAssertions.assertViewIsEnabled;
import static com.schibsted.spain.barista.BaristaAssertions.assertViewIsNotDisplayed;

@RunWith(AndroidJUnit4.class)
public class AssertionsTest {

@Rule
public ActivityTestRule<SomeViewsWithDifferentVisibilitesActivity> activityRule = new ActivityTestRule<>(SomeViewsWithDifferentVisibilitesActivity.class);

@Test
public void checkVisibleViews() {
assertViewIsDisplayed(R.id.visible_view);

assertTextIsDisplayed(R.string.hello_world);
assertTextIsDisplayed("Hello world!");
}

@Test
public void checkInvisibleViews() {
assertViewIsNotDisplayed(R.id.invisible_view);
assertViewIsNotDisplayed(R.id.gone_view);

assertTextIsNotDisplayed(R.string.im_invisible);
assertTextIsNotDisplayed("I'm invisible!");
}

@Test
public void checkUnexistingView() {
assertTextDoesNotExist(R.string.unknown);
assertTextDoesNotExist("Unknown");
}

@Test
public void cantFindAnIdThatDoesntExist() {
assertViewDoesNotExist(R.id.view_in_another_layout);
}

@Test
public void checkEnabledView() {
assertViewIsEnabled(R.id.enabled_button);
}

@Test
public void checkDisabledView() {
assertViewIsDisabled(R.id.disabled_button);
}

@Test
public void checkBackButton() {
assertThatBackButtonClosesTheApp();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.schibsted.spain.barista.sample;

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

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

import static com.schibsted.spain.barista.BaristaAssertions.assertTextIsDisplayed;
import static com.schibsted.spain.barista.BaristaClickActions.click;
import static com.schibsted.spain.barista.BaristaClickActions.clickBack;
import static com.schibsted.spain.barista.BaristaClickActions.clickScrollingIfNeeded;

@RunWith(AndroidJUnit4.class)
public class ButtonsTest {

@Rule
public ActivityTestRule<FlowFirstScreen> activityRule = new ActivityTestRule<>(FlowFirstScreen.class);

@Test
public void checkClickById() {
click(R.id.next);
assertTextIsDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkClickAndScrollIfNeededById() {
clickScrollingIfNeeded(R.id.next);
assertTextIsDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkClickByText() {
click("Next");
assertTextIsDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkClickAndScrollIfNeededByText() {
clickScrollingIfNeeded("Next");
assertTextIsDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkScrollingClickById() {
clickScrollingIfNeeded(R.id.really_far_away_button);
assertTextIsDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkScrollingClickByText() {
clickScrollingIfNeeded("Really far away button");
assertTextIsDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkBackButton() {
click("Next");
assertTextIsDisplayed("Hi! I'm the second screen!");
clickBack();
assertTextIsDisplayed("Hi! I'm the first screen!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.schibsted.spain.barista.sample;

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

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

import static com.schibsted.spain.barista.BaristaAssertions.assertTextIsDisplayed;
import static com.schibsted.spain.barista.BaristaAssertions.assertTextIsNotDisplayed;
import static com.schibsted.spain.barista.BaristaScrollActions.scrollTo;

@RunWith(AndroidJUnit4.class)
public class ScrollsTest {

@Rule
public ActivityTestRule<FlowFirstScreen> activityRule = new ActivityTestRule<>(FlowFirstScreen.class);

@Test
public void checkScrollByText() {
assertTextIsDisplayed("Hi! I'm the first screen!");
assertTextIsNotDisplayed("Really far away button");

scrollTo("Really far away button");

assertTextIsNotDisplayed("Hi! I'm the first screen!");
assertTextIsDisplayed("Really far away button");
}

@Test
public void checkScrollById() {
assertTextIsDisplayed("Hi! I'm the first screen!");
assertTextIsNotDisplayed("Really far away button");

scrollTo(R.id.really_far_away_button);

assertTextIsNotDisplayed("Hi! I'm the first screen!");
assertTextIsDisplayed("Really far away button");
}
}
4 changes: 3 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".SomeViewsWithDifferentVisibilitesActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FlowFirstScreen" />
<activity android:name=".FlowSecondScreen" />
</application>

</manifest>
Loading

0 comments on commit 36c3239

Please sign in to comment.