Add first unit tests for habit

pull/69/head
Alinson S. Xavier 10 years ago
parent 9061182301
commit eee2605f74

@ -8,7 +8,6 @@ android {
applicationId "org.isoron.uhabits" applicationId "org.isoron.uhabits"
minSdkVersion 15 minSdkVersion 15
targetSdkVersion 23 targetSdkVersion 23
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }
@ -36,6 +35,7 @@ dependencies {
androidTestCompile 'com.android.support:support-annotations:23.1.1' androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1' androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1' androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1' androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
} }

@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits; package org.isoron.uhabits.ui;
import android.view.View; import android.view.View;
import android.widget.Adapter; import android.widget.Adapter;

@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits; package org.isoron.uhabits.ui;
import android.support.test.espresso.UiController; import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction; import android.support.test.espresso.ViewAction;
@ -32,6 +32,7 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.isoron.uhabits.R;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.util.Random; import java.util.Random;

@ -17,11 +17,11 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits; package org.isoron.uhabits.ui;
import android.content.Context; import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.InstrumentationRegistry;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
import java.util.Collections; import java.util.Collections;
@ -45,8 +45,8 @@ import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.isoron.uhabits.HabitMatchers.containsHabit; import static org.isoron.uhabits.ui.HabitMatchers.containsHabit;
import static org.isoron.uhabits.HabitMatchers.withName; import static org.isoron.uhabits.ui.HabitMatchers.withName;
public class MainActivityActions public class MainActivityActions
{ {

@ -1,4 +1,4 @@
package org.isoron.uhabits; package org.isoron.uhabits.ui;
import android.content.Context; import android.content.Context;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
@ -7,6 +7,8 @@ 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;
import org.isoron.uhabits.MainActivity;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
@ -37,20 +39,20 @@ import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.isoron.uhabits.HabitMatchers.withName; import static org.isoron.uhabits.ui.HabitMatchers.withName;
import static org.isoron.uhabits.HabitViewActions.clickAtRandomLocations; import static org.isoron.uhabits.ui.HabitViewActions.clickAtRandomLocations;
import static org.isoron.uhabits.HabitViewActions.toggleAllCheckmarks; import static org.isoron.uhabits.ui.HabitViewActions.toggleAllCheckmarks;
import static org.isoron.uhabits.MainActivityActions.addHabit; import static org.isoron.uhabits.ui.MainActivityActions.addHabit;
import static org.isoron.uhabits.MainActivityActions.assertHabitExists; import static org.isoron.uhabits.ui.MainActivityActions.assertHabitExists;
import static org.isoron.uhabits.MainActivityActions.assertHabitsDontExist; import static org.isoron.uhabits.ui.MainActivityActions.assertHabitsDontExist;
import static org.isoron.uhabits.MainActivityActions.assertHabitsExist; import static org.isoron.uhabits.ui.MainActivityActions.assertHabitsExist;
import static org.isoron.uhabits.MainActivityActions.clickActionModeMenuItem; import static org.isoron.uhabits.ui.MainActivityActions.clickActionModeMenuItem;
import static org.isoron.uhabits.MainActivityActions.deleteHabit; import static org.isoron.uhabits.ui.MainActivityActions.deleteHabit;
import static org.isoron.uhabits.MainActivityActions.deleteHabits; import static org.isoron.uhabits.ui.MainActivityActions.deleteHabits;
import static org.isoron.uhabits.MainActivityActions.selectHabit; import static org.isoron.uhabits.ui.MainActivityActions.selectHabit;
import static org.isoron.uhabits.MainActivityActions.selectHabits; import static org.isoron.uhabits.ui.MainActivityActions.selectHabits;
import static org.isoron.uhabits.MainActivityActions.typeHabitData; import static org.isoron.uhabits.ui.MainActivityActions.typeHabitData;
import static org.isoron.uhabits.ShowHabitActivityActions.openHistoryEditor; import static org.isoron.uhabits.ui.ShowHabitActivityActions.openHistoryEditor;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@LargeTest @LargeTest

@ -17,18 +17,21 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits; package org.isoron.uhabits.ui;
import android.support.test.espresso.matcher.ViewMatchers;
import org.isoron.uhabits.R;
import static android.support.test.espresso.Espresso.onView; 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.click;
import static android.support.test.espresso.action.ViewActions.scrollTo; import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
public class ShowHabitActivityActions public class ShowHabitActivityActions
{ {
public static void openHistoryEditor() public static void openHistoryEditor()
{ {
onView(withId(R.id.btEditHistory)) onView(ViewMatchers.withId(R.id.btEditHistory))
.perform(scrollTo(), click()); .perform(scrollTo(), click());
} }
} }

@ -0,0 +1,98 @@
/*
* 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.unit.models;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import org.isoron.uhabits.models.Habit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.LinkedList;
import java.util.List;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class HabitTest
{
@Before
public void prepare()
{
for(Habit h : Habit.getAll(true))
h.cascadeDelete();
}
@Test
public void reorderTest()
{
List<Long> ids = new LinkedList<>();
for (int i = 0; i < 10; i++)
{
Habit h = new Habit();
h.save();
ids.add(h.getId());
assertThat(h.position, is(i));
}
int from = 5, to = 2;
int expectedPosition[] = {0, 1, 3, 4, 5, 2, 6, 7, 8, 9};
Habit fromHabit = Habit.get(ids.get(from));
Habit toHabit = Habit.get(ids.get(to));
Habit.reorder(fromHabit, toHabit);
for (int i = 0; i < 10; i++)
{
Habit h = Habit.get(ids.get(i));
assertThat(h.position, is(expectedPosition[i]));
}
}
@Test
public void rebuildOrderTest()
{
List<Long> ids = new LinkedList<>();
int originalPositions[] = { 0, 1, 1, 4, 6, 8, 10, 10, 13};
int length = originalPositions.length;
for (int i = 0; i < length; i++)
{
Habit h = new Habit();
h.position = originalPositions[i];
h.save();
ids.add(h.getId());
}
Habit.rebuildOrder();
for (int i = 0; i < length; i++)
{
Habit h = Habit.get(ids.get(i));
assertThat(h.position, is(i));
}
}
}
Loading…
Cancel
Save