diff --git a/android/uhabits-android/src/androidTest/java/org/isoron/uhabits/regression/ListHabitsRegressionTest.kt b/android/uhabits-android/src/androidTest/java/org/isoron/uhabits/regression/ListHabitsRegressionTest.kt new file mode 100644 index 000000000..68b8ba192 --- /dev/null +++ b/android/uhabits-android/src/androidTest/java/org/isoron/uhabits/regression/ListHabitsRegressionTest.kt @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2020 Á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.regression + +import android.support.test.filters.* +import org.isoron.uhabits.* +import org.isoron.uhabits.acceptance.steps.CommonSteps.* +import org.isoron.uhabits.acceptance.steps.CommonSteps.Screen.* +import org.isoron.uhabits.acceptance.steps.EditHabitSteps.* +import org.isoron.uhabits.acceptance.steps.ListHabitsSteps.* +import org.isoron.uhabits.acceptance.steps.ListHabitsSteps.MenuItem.* +import org.junit.* + + +@LargeTest +class ListHabitsRegressionTest : BaseUserInterfaceTest() { + /** + * https://github.com/iSoron/uhabits/issues/539 + */ + @Test + @Throws(Exception::class) + fun should_not_crash_after_deleting_then_adding_a_habit() { + launchApp() + verifyShowsScreen(LIST_HABITS) + longClickText("Track time") + clickMenu(DELETE) + clickOK() + + clickMenu(ADD) + verifyShowsScreen(EDIT_HABIT) + typeName("Hello world") + clickSave() + + verifyDisplaysText("Hello world") + longPressCheckmarks("Hello world", 3) + } +} \ No newline at end of file diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.java b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.java index ab086cff5..dd78b4508 100644 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.java +++ b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitList.java @@ -79,13 +79,14 @@ public class SQLiteHabitList extends HabitList { loadRecords(); habit.setPosition(size()); - list.add(habit); HabitRecord record = new HabitRecord(); record.copyFrom(habit); repository.save(record); + habit.id = record.id; rebuildOrder(); + list.add(habit); getObservable().notifyListeners(); } diff --git a/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.java b/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.java index 0c7ba02f2..a78313417 100644 --- a/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.java +++ b/android/uhabits-core/src/test/java/org/isoron/uhabits/core/models/sqlite/SQLiteHabitListTest.java @@ -65,7 +65,7 @@ public class SQLiteHabitListTest extends BaseUnitTest for (int i = 0; i < 10; i++) { Habit habit = fixtures.createEmptyHabit(); - habit.setName("habit " + i); + habit.setName("habit " + (i+1)); habitList.update(habit); habitsArray.add(habit); @@ -147,13 +147,13 @@ public class SQLiteHabitListTest extends BaseUnitTest @Test public void testGetById() { - Habit h1 = habitList.getById(0); + Habit h1 = habitList.getById(1); assertNotNull(h1); - assertThat(h1.getName(), equalTo("habit 0")); + assertThat(h1.getName(), equalTo("habit 1")); - Habit h2 = habitList.getById(0); + Habit h2 = habitList.getById(2); assertNotNull(h2); - assertThat(h1, equalTo(h2)); + assertThat(h2, equalTo(h2)); } @Test @@ -167,7 +167,7 @@ public class SQLiteHabitListTest extends BaseUnitTest @Test public void testGetByPosition() { - Habit h = habitList.getByPosition(5); + Habit h = habitList.getByPosition(4); assertNotNull(h); assertThat(h.getName(), equalTo("habit 5")); } @@ -189,7 +189,7 @@ public class SQLiteHabitListTest extends BaseUnitTest @Test public void testRemove() throws Exception { - Habit h = habitList.getByPosition(2); + Habit h = habitList.getById(2); habitList.remove(h); assertThat(habitList.indexOf(h), equalTo(-1)); @@ -198,7 +198,7 @@ public class SQLiteHabitListTest extends BaseUnitTest rec = repository.find(3L); assertNotNull(rec); - assertThat(rec.position, equalTo(2)); + assertThat(rec.position, equalTo(1)); } @Test @@ -212,11 +212,11 @@ public class SQLiteHabitListTest extends BaseUnitTest HabitRecord record3 = repository.find(3L); assertNotNull(record3); - assertThat(record3.position, equalTo(4)); + assertThat(record3.position, equalTo(3)); HabitRecord record4 = repository.find(4L); assertNotNull(record4); - assertThat(record4.position, equalTo(3)); + assertThat(record4.position, equalTo(2)); }