Fix tests on pre-Lollipop devices

pull/30/head
Alinson S. Xavier 10 years ago
parent 2b40633110
commit 8b10138cd6

@ -24,7 +24,6 @@ import android.support.test.InstrumentationRegistry;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -32,13 +31,14 @@ import java.util.Random;
import static android.support.test.espresso.Espresso.onData; import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; import static android.support.test.espresso.Espresso.openContextualActionModeOverflowMenu;
import static android.support.test.espresso.Espresso.pressBack; import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.longClick; import static android.support.test.espresso.action.ViewActions.longClick;
import static android.support.test.espresso.action.ViewActions.replaceText; import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.assertion.ViewAssertions.matches; 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.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText; import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.allOf;
@ -149,17 +149,30 @@ public class MainActivityActions
public static void deleteHabits(List<String> names) public static void deleteHabits(List<String> names)
{ {
Context context = InstrumentationRegistry.getTargetContext();
selectHabits(names); selectHabits(names);
clickActionModeMenuItem(R.string.delete);
openActionBarOverflowOrOptionsMenu(context);
onView(withText(R.string.delete))
.perform(click());
onView(withText("OK")) onView(withText("OK"))
.perform(click()); .perform(click());
assertHabitsDontExist(names); assertHabitsDontExist(names);
} }
public static void clickActionModeMenuItem(int stringId)
{
try
{
onView(withText(stringId)).perform(click());
}
catch (Exception e1)
{
try
{
onView(withContentDescription(stringId)).perform(click());
}
catch(Exception e2)
{
openContextualActionModeOverflowMenu();
onView(withText(stringId)).perform(click());
}
}
}
} }

@ -3,7 +3,6 @@ package org.isoron.uhabits;
import android.content.Context; import android.content.Context;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.NoMatchingViewException; import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.intent.rule.IntentsTestRule; import android.support.test.espresso.intent.rule.IntentsTestRule;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest; import android.test.suitebuilder.annotation.LargeTest;
@ -32,7 +31,6 @@ 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.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isRoot; import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withClassName; import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText; import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.allOf;
@ -46,6 +44,7 @@ import static org.isoron.uhabits.MainActivityActions.addHabit;
import static org.isoron.uhabits.MainActivityActions.assertHabitExists; import static org.isoron.uhabits.MainActivityActions.assertHabitExists;
import static org.isoron.uhabits.MainActivityActions.assertHabitsDontExist; import static org.isoron.uhabits.MainActivityActions.assertHabitsDontExist;
import static org.isoron.uhabits.MainActivityActions.assertHabitsExist; import static org.isoron.uhabits.MainActivityActions.assertHabitsExist;
import static org.isoron.uhabits.MainActivityActions.clickActionModeMenuItem;
import static org.isoron.uhabits.MainActivityActions.deleteHabit; import static org.isoron.uhabits.MainActivityActions.deleteHabit;
import static org.isoron.uhabits.MainActivityActions.deleteHabits; import static org.isoron.uhabits.MainActivityActions.deleteHabits;
import static org.isoron.uhabits.MainActivityActions.selectHabit; import static org.isoron.uhabits.MainActivityActions.selectHabit;
@ -86,8 +85,8 @@ public class MainTest
names.add(addHabit()); names.add(addHabit());
selectHabits(names); selectHabits(names);
onView(withContentDescription(R.string.archive))
.perform(click()); clickActionModeMenuItem(R.string.archive);
assertHabitsDontExist(names); assertHabitsDontExist(names);
openActionBarOverflowOrOptionsMenu(context); openActionBarOverflowOrOptionsMenu(context);
@ -96,8 +95,7 @@ public class MainTest
assertHabitsExist(names); assertHabitsExist(names);
selectHabits(names); selectHabits(names);
onView(withContentDescription(R.string.unarchive)) clickActionModeMenuItem(R.string.unarchive);
.perform(click());
openActionBarOverflowOrOptionsMenu(context); openActionBarOverflowOrOptionsMenu(context);
onView(withText(R.string.show_archived)) onView(withText(R.string.show_archived))
@ -148,8 +146,7 @@ public class MainTest
.onChildView(withId(R.id.label)) .onChildView(withId(R.id.label))
.perform(longClick()); .perform(longClick());
onView(withContentDescription(R.string.edit)) clickActionModeMenuItem(R.string.edit);
.perform(click());
String modifiedName = "Modified " + new Random().nextInt(10000); String modifiedName = "Modified " + new Random().nextInt(10000);
typeHabitData(modifiedName, "", "1", "1"); typeHabitData(modifiedName, "", "1", "1");
@ -160,8 +157,7 @@ public class MainTest
assertHabitExists(modifiedName); assertHabitExists(modifiedName);
selectHabit(modifiedName); selectHabit(modifiedName);
onView(withContentDescription(R.string.color_picker_default_title)) clickActionModeMenuItem(R.string.color_picker_default_title);
.perform(click());
pressBack(); pressBack();
deleteHabit(modifiedName); deleteHabit(modifiedName);

Loading…
Cancel
Save