diff --git a/app/build.gradle b/app/build.gradle
index a1a1bb4bf..d0a194735 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -8,7 +8,6 @@ android {
applicationId "org.isoron.uhabits"
minSdkVersion 15
targetSdkVersion 23
-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
@@ -36,6 +35,7 @@ dependencies {
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner: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-intents:2.2.1'
}
diff --git a/app/src/androidTest/java/org/isoron/uhabits/HabitMatchers.java b/app/src/androidTest/java/org/isoron/uhabits/ui/HabitMatchers.java
similarity index 98%
rename from app/src/androidTest/java/org/isoron/uhabits/HabitMatchers.java
rename to app/src/androidTest/java/org/isoron/uhabits/ui/HabitMatchers.java
index c96b24c16..0fbb13f34 100644
--- a/app/src/androidTest/java/org/isoron/uhabits/HabitMatchers.java
+++ b/app/src/androidTest/java/org/isoron/uhabits/ui/HabitMatchers.java
@@ -17,7 +17,7 @@
* with this program. If not, see .
*/
-package org.isoron.uhabits;
+package org.isoron.uhabits.ui;
import android.view.View;
import android.widget.Adapter;
diff --git a/app/src/androidTest/java/org/isoron/uhabits/HabitViewActions.java b/app/src/androidTest/java/org/isoron/uhabits/ui/HabitViewActions.java
similarity index 98%
rename from app/src/androidTest/java/org/isoron/uhabits/HabitViewActions.java
rename to app/src/androidTest/java/org/isoron/uhabits/ui/HabitViewActions.java
index 7c9a7e0a2..afa630ea0 100644
--- a/app/src/androidTest/java/org/isoron/uhabits/HabitViewActions.java
+++ b/app/src/androidTest/java/org/isoron/uhabits/ui/HabitViewActions.java
@@ -17,7 +17,7 @@
* with this program. If not, see .
*/
-package org.isoron.uhabits;
+package org.isoron.uhabits.ui;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
@@ -32,6 +32,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import org.hamcrest.Matcher;
+import org.isoron.uhabits.R;
import java.security.InvalidParameterException;
import java.util.Random;
diff --git a/app/src/androidTest/java/org/isoron/uhabits/MainActivityActions.java b/app/src/androidTest/java/org/isoron/uhabits/ui/MainActivityActions.java
similarity index 95%
rename from app/src/androidTest/java/org/isoron/uhabits/MainActivityActions.java
rename to app/src/androidTest/java/org/isoron/uhabits/ui/MainActivityActions.java
index 3ce26da37..412bee5f9 100644
--- a/app/src/androidTest/java/org/isoron/uhabits/MainActivityActions.java
+++ b/app/src/androidTest/java/org/isoron/uhabits/ui/MainActivityActions.java
@@ -17,11 +17,11 @@
* with this program. If not, see .
*/
-package org.isoron.uhabits;
+package org.isoron.uhabits.ui;
-import android.content.Context;
-import android.support.test.InstrumentationRegistry;
+import android.support.test.espresso.matcher.ViewMatchers;
+import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
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.is;
import static org.hamcrest.Matchers.not;
-import static org.isoron.uhabits.HabitMatchers.containsHabit;
-import static org.isoron.uhabits.HabitMatchers.withName;
+import static org.isoron.uhabits.ui.HabitMatchers.containsHabit;
+import static org.isoron.uhabits.ui.HabitMatchers.withName;
public class MainActivityActions
{
diff --git a/app/src/androidTest/java/org/isoron/uhabits/MainTest.java b/app/src/androidTest/java/org/isoron/uhabits/ui/MainTest.java
similarity index 84%
rename from app/src/androidTest/java/org/isoron/uhabits/MainTest.java
rename to app/src/androidTest/java/org/isoron/uhabits/ui/MainTest.java
index b6047c69a..ab7119f3d 100644
--- a/app/src/androidTest/java/org/isoron/uhabits/MainTest.java
+++ b/app/src/androidTest/java/org/isoron/uhabits/ui/MainTest.java
@@ -1,4 +1,4 @@
-package org.isoron.uhabits;
+package org.isoron.uhabits.ui;
import android.content.Context;
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.test.suitebuilder.annotation.LargeTest;
+import org.isoron.uhabits.MainActivity;
+import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
import org.junit.Before;
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.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;
-import static org.isoron.uhabits.MainActivityActions.assertHabitsDontExist;
-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.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;
+import static org.isoron.uhabits.ui.HabitMatchers.withName;
+import static org.isoron.uhabits.ui.HabitViewActions.clickAtRandomLocations;
+import static org.isoron.uhabits.ui.HabitViewActions.toggleAllCheckmarks;
+import static org.isoron.uhabits.ui.MainActivityActions.addHabit;
+import static org.isoron.uhabits.ui.MainActivityActions.assertHabitExists;
+import static org.isoron.uhabits.ui.MainActivityActions.assertHabitsDontExist;
+import static org.isoron.uhabits.ui.MainActivityActions.assertHabitsExist;
+import static org.isoron.uhabits.ui.MainActivityActions.clickActionModeMenuItem;
+import static org.isoron.uhabits.ui.MainActivityActions.deleteHabit;
+import static org.isoron.uhabits.ui.MainActivityActions.deleteHabits;
+import static org.isoron.uhabits.ui.MainActivityActions.selectHabit;
+import static org.isoron.uhabits.ui.MainActivityActions.selectHabits;
+import static org.isoron.uhabits.ui.MainActivityActions.typeHabitData;
+import static org.isoron.uhabits.ui.ShowHabitActivityActions.openHistoryEditor;
@RunWith(AndroidJUnit4.class)
@LargeTest
diff --git a/app/src/androidTest/java/org/isoron/uhabits/ShowHabitActivityActions.java b/app/src/androidTest/java/org/isoron/uhabits/ui/ShowHabitActivityActions.java
similarity index 86%
rename from app/src/androidTest/java/org/isoron/uhabits/ShowHabitActivityActions.java
rename to app/src/androidTest/java/org/isoron/uhabits/ui/ShowHabitActivityActions.java
index cacc9f21f..31a89c397 100644
--- a/app/src/androidTest/java/org/isoron/uhabits/ShowHabitActivityActions.java
+++ b/app/src/androidTest/java/org/isoron/uhabits/ui/ShowHabitActivityActions.java
@@ -17,18 +17,21 @@
* with this program. If not, see .
*/
-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.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))
+ onView(ViewMatchers.withId(R.id.btEditHistory))
.perform(scrollTo(), click());
}
}
diff --git a/app/src/androidTest/java/org/isoron/uhabits/unit/models/HabitTest.java b/app/src/androidTest/java/org/isoron/uhabits/unit/models/HabitTest.java
new file mode 100644
index 000000000..4715396b8
--- /dev/null
+++ b/app/src/androidTest/java/org/isoron/uhabits/unit/models/HabitTest.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2016 Álinson Santos Xavier
+ *
+ * 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 .
+ */
+
+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 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 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));
+ }
+ }
+}