From f5e4a88415a1ed4669d336e69db35c50ddcd5ed6 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Thu, 17 Mar 2016 06:16:47 -0400 Subject: [PATCH] Implement missing tests for Habit; remove some dead code --- .../isoron/uhabits/unit/models/HabitTest.java | 237 +++++++++++++++++- .../uhabits/HabitBroadcastReceiver.java | 6 +- .../java/org/isoron/uhabits/models/Habit.java | 43 ++-- 3 files changed, 254 insertions(+), 32 deletions(-) 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 index f31db5f35..a4e3d5678 100644 --- a/app/src/androidTest/java/org/isoron/uhabits/unit/models/HabitTest.java +++ b/app/src/androidTest/java/org/isoron/uhabits/unit/models/HabitTest.java @@ -23,6 +23,7 @@ import android.graphics.Color; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; +import org.isoron.helpers.DateHelper; import org.isoron.uhabits.models.Habit; import org.junit.Before; import org.junit.Test; @@ -31,10 +32,12 @@ import org.junit.runner.RunWith; import java.util.LinkedList; import java.util.List; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; @RunWith(AndroidJUnit4.class) @SmallTest @@ -90,11 +93,116 @@ public class HabitTest } @Test - public void reorderTest() + public void get_withValidId() + { + Habit habit = new Habit(); + habit.save(); + + Habit habit2 = Habit.get(habit.getId()); + assertThat(habit, equalTo(habit2)); + } + + @Test + public void get_withInvalidId() + { + Habit habit = Habit.get(123456L); + assertThat(habit, is(nullValue())); + } + + @Test + public void getAll_withoutArchived() + { + List habits = new LinkedList<>(); + List habitsWithArchived = new LinkedList<>(); + + for(int i = 0; i < 10; i++) + { + Habit h = new Habit(); + + if(i % 2 == 0) + h.archived = 1; + else + habits.add(h); + + habitsWithArchived.add(h); + h.save(); + } + + assertThat(habits, equalTo(Habit.getAll(false))); + assertThat(habitsWithArchived, equalTo(Habit.getAll(true))); + } + + @Test + public void getByPosition() + { + List habits = new LinkedList<>(); + + for(int i = 0; i < 10; i++) + { + Habit h = new Habit(); + h.save(); + habits.add(h); + } + + for(int i = 0; i < 10; i++) + { + Habit h = Habit.getByPosition(i); + if(h == null) fail(); + assertThat(h, equalTo(habits.get(i))); + } + } + + @Test + public void count() + { + for(int i = 0; i < 10; i++) + { + Habit h = new Habit(); + if(i % 2 == 0) h.archived = 1; + h.save(); + } + + assertThat(Habit.count(), equalTo(5)); + } + + + @Test + public void countWithArchived() + { + for(int i = 0; i < 10; i++) + { + Habit h = new Habit(); + if(i % 2 == 0) h.archived = 1; + h.save(); + } + + assertThat(Habit.countWithArchived(), equalTo(10)); + } + + @Test + public void updateId() + { + Habit habit = new Habit(); + habit.name = "Hello World"; + habit.save(); + + Long oldId = habit.getId(); + Long newId = 123456L; + Habit.updateId(oldId, newId); + + Habit newHabit = Habit.get(newId); + if(newHabit == null) fail(); + assertThat(newHabit, is(not(nullValue()))); + assertThat(newHabit.name, equalTo(habit.name)); + } + + @Test + public void reorder() { List ids = new LinkedList<>(); - for (int i = 0; i < 10; i++) + int n = 10; + for (int i = 0; i < n; i++) { Habit h = new Habit(); h.save(); @@ -102,22 +210,44 @@ public class HabitTest assertThat(h.position, is(i)); } - int from = 5, to = 2; - int expectedPosition[] = {0, 1, 3, 4, 5, 2, 6, 7, 8, 9}; + int operations[][] = { + {5, 2}, + {3, 7}, + {4, 4}, + {3, 2} + }; - Habit fromHabit = Habit.get(ids.get(from)); - Habit toHabit = Habit.get(ids.get(to)); - Habit.reorder(fromHabit, toHabit); + int expectedPosition[][] = { + {0, 1, 3, 4, 5, 2, 6, 7, 8, 9}, + {0, 1, 7, 3, 4, 2, 5, 6, 8, 9}, + {0, 1, 7, 3, 4, 2, 5, 6, 8, 9}, + {0, 1, 7, 2, 4, 3, 5, 6, 8, 9}, + }; - for (int i = 0; i < 10; i++) + for(int i = 0; i < operations.length; i++) { - Habit h = Habit.get(ids.get(i)); - assertThat(h.position, is(expectedPosition[i])); + int from = operations[i][0]; + int to = operations[i][1]; + + Habit fromHabit = Habit.getByPosition(from); + Habit toHabit = Habit.getByPosition(to); + Habit.reorder(fromHabit, toHabit); + + int actualPositions[] = new int[n]; + + for (int j = 0; j < n; j++) + { + Habit h = Habit.get(ids.get(j)); + if (h == null) fail(); + actualPositions[j] = h.position; + } + + assertThat(actualPositions, equalTo(expectedPosition[i])); } } @Test - public void rebuildOrderTest() + public void rebuildOrder() { List ids = new LinkedList<>(); int originalPositions[] = { 0, 1, 1, 4, 6, 8, 10, 10, 13}; @@ -135,7 +265,92 @@ public class HabitTest for (int i = 0; i < originalPositions.length; i++) { Habit h = Habit.get(ids.get(i)); + if(h == null) fail(); assertThat(h.position, is(i)); } } + + @Test + public void getHabitsWithReminder() + { + List habitsWithReminder = new LinkedList<>(); + + for(int i = 0; i < 10; i++) + { + Habit habit = new Habit(); + if(i % 2 == 0) + { + habit.reminderDays = DateHelper.ALL_WEEK_DAYS; + habit.reminderHour = 8; + habit.reminderMin = 30; + habitsWithReminder.add(habit); + } + habit.save(); + } + + assertThat(habitsWithReminder, equalTo(Habit.getHabitsWithReminder())); + } + + @Test + public void archive_unarchive() + { + List allHabits = new LinkedList<>(); + List archivedHabits = new LinkedList<>(); + List unarchivedHabits = new LinkedList<>(); + + for(int i = 0; i < 10; i++) + { + Habit habit = new Habit(); + habit.save(); + allHabits.add(habit); + + if(i % 2 == 0) + archivedHabits.add(habit); + else + unarchivedHabits.add(habit); + } + + Habit.archive(archivedHabits); + assertThat(Habit.getAll(false), equalTo(unarchivedHabits)); + assertThat(Habit.getAll(true), equalTo(allHabits)); + + Habit.unarchive(archivedHabits); + assertThat(Habit.getAll(false), equalTo(allHabits)); + assertThat(Habit.getAll(true), equalTo(allHabits)); + } + + @Test + public void setColor() + { + List habits = new LinkedList<>(); + + for(int i = 0; i < 10; i++) + { + Habit habit = new Habit(); + habit.color = i; + habit.save(); + habits.add(habit); + } + + int newColor = 100; + Habit.setColor(habits, newColor); + + for(Habit h : habits) + assertThat(h.color, equalTo(newColor)); + } + + @Test + public void hasReminder_clearReminder() + { + Habit h = new Habit(); + assertThat(h.hasReminder(), is(false)); + + h.reminderDays = DateHelper.ALL_WEEK_DAYS; + h.reminderHour = 8; + h.reminderMin = 30; + assertThat(h.hasReminder(), is(true)); + + h.clearReminder(); + assertThat(h.hasReminder(), is(false)); + } } diff --git a/app/src/main/java/org/isoron/uhabits/HabitBroadcastReceiver.java b/app/src/main/java/org/isoron/uhabits/HabitBroadcastReceiver.java index 1141a6f75..db88d38e9 100644 --- a/app/src/main/java/org/isoron/uhabits/HabitBroadcastReceiver.java +++ b/app/src/main/java/org/isoron/uhabits/HabitBroadcastReceiver.java @@ -125,11 +125,7 @@ public class HabitBroadcastReceiver extends BroadcastReceiver private void dismissAllHabits() { - for (Habit h : Habit.getHighlightedHabits()) - { - h.highlight = 0; - h.save(); - } + } private void dismissNotification(Context context, Long habitId) diff --git a/app/src/main/java/org/isoron/uhabits/models/Habit.java b/app/src/main/java/org/isoron/uhabits/models/Habit.java index 851077189..362fea02a 100644 --- a/app/src/main/java/org/isoron/uhabits/models/Habit.java +++ b/app/src/main/java/org/isoron/uhabits/models/Habit.java @@ -37,6 +37,7 @@ import com.activeandroid.util.SQLiteUtils; import org.isoron.helpers.ColorHelper; import java.util.List; +import java.util.Locale; @Table(name = "Habits") public class Habit extends Model @@ -174,7 +175,7 @@ public class Habit extends Model * @return the habit, or null if none exist */ @Nullable - public static Habit get(@NonNull Long id) + public static Habit get(long id) { return Habit.load(Habit.class, id); } @@ -185,12 +186,25 @@ public class Habit extends Model * @param includeArchive whether archived habits should be included the list * @return list of all habits */ + @NonNull public static List getAll(boolean includeArchive) { if(includeArchive) return selectWithArchived().execute(); else return select().execute(); } + /** + * Returns the habit that occupies a certain position. + * + * @param position the position of the desired habit + * @return the habit at that position, or null if there is none + */ + @Nullable + public static Habit getByPosition(int position) + { + return selectWithArchived().where("position = ?", position).executeSingle(); + } + /** * Changes the id of a habit on the database. * @@ -203,11 +217,13 @@ public class Habit extends Model SQLiteUtils.execSql(String.format("update Habits set Id = %d where Id = %d", newId, oldId)); } + @NonNull protected static From select() { return new Select().from(Habit.class).where("archived = 0").orderBy("position"); } + @NonNull protected static From selectWithArchived() { return new Select().from(Habit.class).orderBy("position"); @@ -233,19 +249,12 @@ public class Habit extends Model return selectWithArchived().count(); } - - public static java.util.List getHighlightedHabits() - { - return select().where("highlight = 1") - .orderBy("reminder_hour desc, reminder_min desc") - .execute(); - } - /** * Returns a list the habits that have a reminder. Does not include archived habits. * * @return list of habits with reminder */ + @NonNull public static List getHabitsWithReminder() { return select().where("reminder_hour is not null").execute(); @@ -310,7 +319,7 @@ public class Habit extends Model * * @param model the model whose attributes should be copied from */ - public void copyAttributes(Habit model) + public void copyAttributes(@NonNull Habit model) { this.name = model.name; this.description = model.description; @@ -330,7 +339,7 @@ public class Habit extends Model * * @param id the id that the habit should receive */ - public void save(Long id) + public void save(long id) { save(); Habit.updateId(getId(), id); @@ -367,7 +376,8 @@ public class Habit extends Model */ public Uri getUri() { - return Uri.parse(String.format("content://org.isoron.uhabits/habit/%d", getId())); + String s = String.format(Locale.US, "content://org.isoron.uhabits/habit/%d", getId()); + return Uri.parse(s); } /** @@ -379,7 +389,8 @@ public class Habit extends Model return archived != 0; } - private static void updateAttributes(List habits, Integer color, Integer archived) + private static void updateAttributes(@NonNull List habits, @Nullable Integer color, + @Nullable Integer archived) { ActiveAndroid.beginTransaction(); @@ -405,7 +416,7 @@ public class Habit extends Model * * @param habits the habits to be archived */ - public static void archive(List habits) + public static void archive(@NonNull List habits) { updateAttributes(habits, null, 1); } @@ -415,7 +426,7 @@ public class Habit extends Model * * @param habits the habits to be unarchived */ - public static void unarchive(List habits) + public static void unarchive(@NonNull List habits) { updateAttributes(habits, null, 0); } @@ -426,7 +437,7 @@ public class Habit extends Model * @param habits the habits to be modified * @param color the new color to be set */ - public static void setColor(List habits, int color) + public static void setColor(@NonNull List habits, int color) { updateAttributes(habits, color, null); }