Implement additional UI tests

pull/87/merge
Alinson S. Xavier 8 years ago
parent ead87519b1
commit 28f095e56a

@ -93,7 +93,7 @@ public class BaseUIAutomatorTest
h1.setColor(5); h1.setColor(5);
habitList.update(h1); habitList.update(h1);
Habit h2 = fixtures.createEmptyHabit(); Habit h2 = fixtures.createShortHabit();
h2.setName("Track time"); h2.setName("Track time");
h2.setDescription("Did you track your time?"); h2.setDescription("Did you track your time?");
h2.setColor(5); h2.setColor(5);

@ -26,10 +26,12 @@ import org.isoron.uhabits.*;
import org.junit.*; import org.junit.*;
import org.junit.runner.*; import org.junit.runner.*;
import static org.isoron.uhabits.acceptance.steps.CommonSteps.Screen.*;
import static org.isoron.uhabits.acceptance.steps.CommonSteps.*; import static org.isoron.uhabits.acceptance.steps.CommonSteps.*;
import static org.isoron.uhabits.acceptance.steps.EditHabitSteps.*; import static org.isoron.uhabits.acceptance.steps.EditHabitSteps.*;
import static org.isoron.uhabits.acceptance.steps.ListHabitsSteps.MenuItem.*; import static org.isoron.uhabits.acceptance.steps.ListHabitsSteps.MenuItem.*;
import static org.isoron.uhabits.acceptance.steps.ListHabitsSteps.*; import static org.isoron.uhabits.acceptance.steps.ListHabitsSteps.*;
import static org.isoron.uhabits.acceptance.steps.ListHabitsSteps.MenuItem.EDIT;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@LargeTest @LargeTest
@ -39,12 +41,18 @@ public class HabitsTest extends BaseUIAutomatorTest
public void shouldCreateHabit() throws Exception public void shouldCreateHabit() throws Exception
{ {
launchApp(); launchApp();
clickMenu(CREATE_HABIT);
verifyShowsScreen(LIST_HABITS);
clickMenu(ADD);
verifyShowsScreen(EDIT_HABIT);
typeName("Hello world"); typeName("Hello world");
typeQuestion("Did you say hello to the world today?"); typeQuestion("Did you say hello to the world today?");
pickFrequency("Every week"); pickFrequency("Every week");
pickColor(5); pickColor(5);
clickSave(); clickSave();
verifyShowsScreen(LIST_HABITS);
verifyDisplaysText("Hello world"); verifyDisplaysText("Hello world");
} }
@ -52,7 +60,10 @@ public class HabitsTest extends BaseUIAutomatorTest
public void shouldShowHabitStatistics() throws Exception public void shouldShowHabitStatistics() throws Exception
{ {
launchApp(); launchApp();
verifyShowsScreen(LIST_HABITS);
clickText("Track time"); clickText("Track time");
verifyShowsScreen(SHOW_HABIT);
verifyDisplayGraphs(); verifyDisplayGraphs();
} }
@ -60,6 +71,8 @@ public class HabitsTest extends BaseUIAutomatorTest
public void shouldDeleteHabit() throws Exception public void shouldDeleteHabit() throws Exception
{ {
launchApp(); launchApp();
verifyShowsScreen(LIST_HABITS);
longClickText("Track time"); longClickText("Track time");
clickMenu(DELETE); clickMenu(DELETE);
clickOK(); clickOK();
@ -70,11 +83,17 @@ public class HabitsTest extends BaseUIAutomatorTest
public void shouldEditHabit() throws Exception public void shouldEditHabit() throws Exception
{ {
launchApp(); launchApp();
verifyShowsScreen(LIST_HABITS);
longClickText("Track time"); longClickText("Track time");
clickMenu(EDIT_HABIT); clickMenu(EDIT);
verifyShowsScreen(EDIT_HABIT);
typeName("Take a walk"); typeName("Take a walk");
typeQuestion("Did you take a walk today?"); typeQuestion("Did you take a walk today?");
clickSave(); clickSave();
verifyShowsScreen(LIST_HABITS);
verifyDisplaysText("Take a walk"); verifyDisplaysText("Take a walk");
verifyDoesNotDisplayText("Track time"); verifyDoesNotDisplayText("Track time");
} }
@ -83,14 +102,24 @@ public class HabitsTest extends BaseUIAutomatorTest
public void shouldEditHabit_fromStatisticsScreen() throws Exception public void shouldEditHabit_fromStatisticsScreen() throws Exception
{ {
launchApp(); launchApp();
verifyShowsScreen(LIST_HABITS);
clickText("Track time"); clickText("Track time");
clickMenu(EDIT_HABIT);
verifyShowsScreen(SHOW_HABIT);
clickMenu(EDIT);
verifyShowsScreen(EDIT_HABIT);
typeName("Take a walk"); typeName("Take a walk");
typeQuestion("Did you take a walk today?"); typeQuestion("Did you take a walk today?");
pickColor(10); pickColor(10);
clickSave(); clickSave();
verifyShowsScreen(SHOW_HABIT);
verifyDisplaysText("Take a walk"); verifyDisplaysText("Take a walk");
pressBack(); pressBack();
verifyShowsScreen(LIST_HABITS);
verifyDisplaysText("Take a walk"); verifyDisplaysText("Take a walk");
verifyDoesNotDisplayText("Track time"); verifyDoesNotDisplayText("Track time");
} }
@ -99,15 +128,49 @@ public class HabitsTest extends BaseUIAutomatorTest
public void shouldArchiveAndUnarchiveHabits() throws Exception public void shouldArchiveAndUnarchiveHabits() throws Exception
{ {
launchApp(); launchApp();
verifyShowsScreen(LIST_HABITS);
longClickText("Track time"); longClickText("Track time");
clickMenu(ARCHIVE); clickMenu(ARCHIVE);
verifyDoesNotDisplayText("Track time"); verifyDoesNotDisplayText("Track time");
clickMenu(HIDE_ARCHIVED); clickMenu(TOGGLE_ARCHIVED);
verifyDisplaysText("Track time"); verifyDisplaysText("Track time");
longClickText("Track time"); longClickText("Track time");
clickMenu(UNARCHIVE); clickMenu(UNARCHIVE);
clickMenu(HIDE_ARCHIVED); clickMenu(TOGGLE_ARCHIVED);
verifyDisplaysText("Track time");
}
@Test
public void shouldToggleCheckmarksAndUpdateScore() throws Exception
{
launchApp();
verifyShowsScreen(LIST_HABITS);
longPressCheckmarks("Wake up early", 2);
clickText("Wake up early");
verifyShowsScreen(SHOW_HABIT);
verifyDisplaysText("10%");
}
@Test
public void shouldHideCompleted() throws Exception
{
launchApp();
verifyShowsScreen(LIST_HABITS);
verifyDisplaysText("Track time");
verifyDisplaysText("Wake up early");
clickMenu(TOGGLE_COMPLETED);
verifyDoesNotDisplayText("Track time");
verifyDisplaysText("Wake up early");
longPressCheckmarks("Wake up early", 1);
verifyDoesNotDisplayText("Wake up early");
clickMenu(TOGGLE_COMPLETED);
verifyDisplaysText("Track time"); verifyDisplaysText("Track time");
verifyDisplaysText("Wake up early");
} }
} }

@ -26,6 +26,7 @@ import android.support.test.uiautomator.*;
import android.support.v7.widget.*; import android.support.v7.widget.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.R;
import org.isoron.uhabits.activities.habits.list.*; import org.isoron.uhabits.activities.habits.list.*;
import static android.support.test.espresso.Espresso.*; import static android.support.test.espresso.Espresso.*;
@ -124,4 +125,27 @@ public class CommonSteps extends BaseUIAutomatorTest
device.waitForIdle(); device.waitForIdle();
assertTrue(device.findObject(new UiSelector().text(url)).exists()); assertTrue(device.findObject(new UiSelector().text(url)).exists());
} }
public enum Screen
{
LIST_HABITS, SHOW_HABIT, EDIT_HABIT
}
public static void verifyShowsScreen(Screen screen)
{
switch(screen)
{
case LIST_HABITS:
onView(withId(R.id.header)).check(matches(isDisplayed()));
break;
case SHOW_HABIT:
onView(withId(R.id.subtitleCard)).check(matches(isDisplayed()));
break;
case EDIT_HABIT:
onView(withId(R.id.tvDescription)).check(matches(isDisplayed()));
break;
}
}
} }

@ -35,7 +35,7 @@ import static android.support.test.espresso.action.ViewActions.*;
import static android.support.test.espresso.matcher.ViewMatchers.*; import static android.support.test.espresso.matcher.ViewMatchers.*;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.isoron.uhabits.BaseUIAutomatorTest.device; import static org.isoron.uhabits.BaseUIAutomatorTest.device;
import static org.isoron.uhabits.acceptance.steps.CommonSteps.clickText; import static org.isoron.uhabits.acceptance.steps.CommonSteps.*;
public abstract class ListHabitsSteps public abstract class ListHabitsSteps
{ {
@ -55,11 +55,11 @@ public abstract class ListHabitsSteps
clickTextInsideOverflowMenu(R.string.settings); clickTextInsideOverflowMenu(R.string.settings);
break; break;
case CREATE_HABIT: case ADD:
clickViewWithId(R.id.actionAdd); clickViewWithId(R.id.actionAdd);
break; break;
case EDIT_HABIT: case EDIT:
clickViewWithId(R.id.action_edit_habit); clickViewWithId(R.id.action_edit_habit);
break; break;
@ -75,10 +75,15 @@ public abstract class ListHabitsSteps
clickTextInsideOverflowMenu(R.string.unarchive); clickTextInsideOverflowMenu(R.string.unarchive);
break; break;
case HIDE_ARCHIVED: case TOGGLE_ARCHIVED:
clickViewWithId(R.id.action_filter); clickViewWithId(R.id.action_filter);
clickText(R.string.hide_archived); clickText(R.string.hide_archived);
break; break;
case TOGGLE_COMPLETED:
clickViewWithId(R.id.action_filter);
clickText(R.string.hide_completed);
break;
} }
} }
@ -86,10 +91,10 @@ public abstract class ListHabitsSteps
{ {
UiObject toolbar = device.findObject( UiObject toolbar = device.findObject(
new UiSelector().resourceId("org.isoron.uhabits:id/toolbar")); new UiSelector().resourceId("org.isoron.uhabits:id/toolbar"));
if(toolbar.exists()) if (toolbar.exists())
{ {
onView(allOf(withContentDescription("More options"), withParent onView(allOf(withContentDescription("More options"),
(withParent(withId(R.id.toolbar))))).perform(click()); withParent(withParent(withId(R.id.toolbar))))).perform(click());
} }
else else
{ {
@ -104,7 +109,7 @@ public abstract class ListHabitsSteps
onView(withId(id)).perform(click()); onView(withId(id)).perform(click());
} }
private static ViewAction longClickEveryDescendantWithClass(Class cls) private static ViewAction longClickDescendantWithClass(Class cls, int count)
{ {
return new ViewAction() return new ViewAction()
{ {
@ -126,6 +131,7 @@ public abstract class ListHabitsSteps
{ {
LinkedList<ViewGroup> stack = new LinkedList<>(); LinkedList<ViewGroup> stack = new LinkedList<>();
if (view instanceof ViewGroup) stack.push((ViewGroup) view); if (view instanceof ViewGroup) stack.push((ViewGroup) view);
int countRemaining = count;
while (!stack.isEmpty()) while (!stack.isEmpty())
{ {
@ -134,10 +140,11 @@ public abstract class ListHabitsSteps
{ {
View v = vg.getChildAt(i); View v = vg.getChildAt(i);
if (v instanceof ViewGroup) stack.push((ViewGroup) v); if (v instanceof ViewGroup) stack.push((ViewGroup) v);
if (cls.isInstance(v)) if (cls.isInstance(v) && countRemaining > 0)
{ {
v.performLongClick(); v.performLongClick();
uiController.loopMainThreadUntilIdle(); uiController.loopMainThreadUntilIdle();
countRemaining--;
} }
} }
} }
@ -145,17 +152,18 @@ public abstract class ListHabitsSteps
}; };
} }
public static void longPressCheckmarks(String habit) public static void longPressCheckmarks(String habit, int count)
{ {
CommonSteps.scrollToText(habit); CommonSteps.scrollToText(habit);
onView(allOf(hasDescendant(withText(habit)), onView(allOf(hasDescendant(withText(habit)),
withClassName(endsWith("HabitCardView")))).perform( withClassName(endsWith("HabitCardView")))).perform(
longClickEveryDescendantWithClass(CheckmarkButtonView.class)); longClickDescendantWithClass(CheckmarkButtonView.class, count));
device.waitForIdle();
} }
public enum MenuItem public enum MenuItem
{ {
ABOUT, HELP, SETTINGS, EDIT_HABIT, DELETE, ARCHIVE, HIDE_ARCHIVED, ABOUT, HELP, SETTINGS, EDIT, DELETE, ARCHIVE, TOGGLE_ARCHIVED,
UNARCHIVE, CREATE_HABIT UNARCHIVE, TOGGLE_COMPLETED, ADD
} }
} }

@ -33,8 +33,6 @@ public abstract class ScoreList implements Iterable<Score>
protected ModelObservable observable; protected ModelObservable observable;
protected Double todayValue = null;
/** /**
* Creates a new ScoreList for the given habit. * Creates a new ScoreList for the given habit.
* <p> * <p>
@ -71,8 +69,7 @@ public abstract class ScoreList implements Iterable<Score>
*/ */
public double getTodayValue() public double getTodayValue()
{ {
if(todayValue == null) todayValue = getValue(DateUtils.getStartOfToday()); return getValue(DateUtils.getStartOfToday());
return todayValue;
} }
/** /**

@ -78,7 +78,6 @@ public class MemoryScoreList extends ScoreList
list.removeAll(discard); list.removeAll(discard);
todayValue = null;
getObservable().notifyListeners(); getObservable().notifyListeners();
} }

Loading…
Cancel
Save