Add more tests for settings and about

pull/30/head
Alinson S. Xavier 10 years ago
parent 1102d05a61
commit 2b40633110

@ -17,6 +17,9 @@ android {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
testCoverageEnabled = true
}
}
lintOptions {

@ -21,6 +21,7 @@ package org.isoron.uhabits;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.action.CoordinatesProvider;
import android.support.test.espresso.action.GeneralClickAction;
import android.support.test.espresso.action.GeneralLocation;
import android.support.test.espresso.action.Press;
@ -33,6 +34,7 @@ import android.widget.TextView;
import org.hamcrest.Matcher;
import java.security.InvalidParameterException;
import java.util.Random;
public class HabitViewActions
{
@ -72,4 +74,57 @@ public class HabitViewActions
}
};
}
public static ViewAction clickAt(final int x, final int y)
{
return new GeneralClickAction(Tap.SINGLE, new CoordinatesProvider()
{
@Override
public float[] calculateCoordinates(View view)
{
int[] locations = new int[2];
view.getLocationOnScreen(locations);
final float locationX = locations[0] + x;
final float locationY = locations[1] + y;
return new float[]{locationX, locationY};
}
}, Press.FINGER);
}
public static ViewAction clickAtRandomLocations(final int count)
{
return new ViewAction()
{
@Override
public Matcher<View> getConstraints()
{
return ViewMatchers.isDisplayed();
}
@Override
public String getDescription()
{
return "clickAtRandomLocations";
}
@Override
public void perform(UiController uiController, View view)
{
int width = view.getWidth();
int height = view.getHeight();
Random random = new Random();
for(int i = 0; i < count; i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
ViewAction action = clickAt(x, y);
action.perform(uiController, view);
}
}
};
}
}

@ -24,6 +24,8 @@ import android.support.test.InstrumentationRegistry;
import org.isoron.uhabits.models.Habit;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
@ -101,6 +103,11 @@ public class MainActivityActions
.perform(replaceText(den));
}
public static void selectHabit(String name)
{
selectHabits(Collections.singletonList(name));
}
public static void selectHabits(List<String> names)
{
boolean first = true;
@ -137,9 +144,7 @@ public class MainActivityActions
public static void deleteHabit(String name)
{
LinkedList<String> names = new LinkedList<>();
names.add(name);
deleteHabits(names);
deleteHabits(Collections.singletonList(name));
}
public static void deleteHabits(List<String> names)

@ -3,6 +3,7 @@ package org.isoron.uhabits;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
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.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
@ -20,11 +21,16 @@ import java.util.Random;
import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
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.longClick;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.action.ViewActions.swipeLeft;
import static android.support.test.espresso.action.ViewActions.swipeRight;
import static android.support.test.espresso.action.ViewActions.swipeUp;
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.isRoot;
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;
@ -34,6 +40,7 @@ import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.isoron.uhabits.HabitMatchers.withName;
import static org.isoron.uhabits.HabitViewActions.clickAtRandomLocations;
import static org.isoron.uhabits.HabitViewActions.toggleAllCheckmarks;
import static org.isoron.uhabits.MainActivityActions.addHabit;
import static org.isoron.uhabits.MainActivityActions.assertHabitExists;
@ -41,12 +48,14 @@ import static org.isoron.uhabits.MainActivityActions.assertHabitsDontExist;
import static org.isoron.uhabits.MainActivityActions.assertHabitsExist;
import static org.isoron.uhabits.MainActivityActions.deleteHabit;
import static org.isoron.uhabits.MainActivityActions.deleteHabits;
import static org.isoron.uhabits.MainActivityActions.selectHabit;
import static org.isoron.uhabits.MainActivityActions.selectHabits;
import static org.isoron.uhabits.MainActivityActions.typeHabitData;
import static org.isoron.uhabits.ShowHabitActivityActions.openHistoryEditor;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest
public class MainTest
{
@Rule
public IntentsTestRule<MainActivity> activityRule = new IntentsTestRule<>(
@ -111,7 +120,7 @@ public class MainActivityTest
}
@Test
public void testAddHabitAndToggleCheckmarks()
public void testAddHabitAndViewStats()
{
String name = addHabit(true);
@ -123,6 +132,9 @@ public class MainActivityTest
.onChildView(withId(R.id.label))
.perform(click());
onView(withId(R.id.scoreView))
.perform(swipeRight());
onView(withId(R.id.punchcardView))
.perform(scrollTo());
}
@ -146,6 +158,48 @@ public class MainActivityTest
.perform(click());
assertHabitExists(modifiedName);
selectHabit(modifiedName);
onView(withContentDescription(R.string.color_picker_default_title))
.perform(click());
pressBack();
deleteHabit(modifiedName);
}
@Test
public void testEditHistory()
{
String name = addHabit();
onData(allOf(is(instanceOf(Habit.class)), withName(name)))
.onChildView(withId(R.id.label))
.perform(click());
openHistoryEditor();
onView(withClassName(endsWith("HabitHistoryView")))
.perform(clickAtRandomLocations(20));
pressBack();
onView(withId(R.id.historyView))
.perform(scrollTo(), swipeRight(), swipeLeft());
}
@Test
public void testSettingsAndAbout()
{
Context context = InstrumentationRegistry.getContext();
openActionBarOverflowOrOptionsMenu(context);
onView(withText(R.string.settings))
.perform(click());
pressBack();
openActionBarOverflowOrOptionsMenu(context);
onView(withText(R.string.about))
.perform(click());
onView(isRoot())
.perform(swipeUp());
pressBack();
}
}

@ -0,0 +1,34 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
public class ShowHabitActivityActions
{
public static void openHistoryEditor()
{
onView(withId(R.id.btEditHistory))
.perform(scrollTo(), click());
}
}
Loading…
Cancel
Save