Implement tests for Archive and ChangeColor commands

pull/77/merge
Alinson S. Xavier 10 years ago
parent 21157eaa19
commit f24e300b40

@ -28,7 +28,7 @@ public class HabitFixtures
public static boolean NON_DAILY_HABIT_CHECKS[] = { true, false, false, true, true, true, false,
false, true, true };
public static Habit createNonDailyHabit()
public static Habit createShortHabit()
{
Habit habit = new Habit();
habit.name = "Wake up early";

@ -0,0 +1,69 @@
/*
* 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.commands;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import org.isoron.uhabits.BaseTest;
import org.isoron.uhabits.commands.ArchiveHabitsCommand;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.unit.HabitFixtures;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Collections;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class ArchiveHabitsCommandTest extends BaseTest
{
private ArchiveHabitsCommand command;
private Habit habit;
@Before
public void setup()
{
super.setup();
habit = HabitFixtures.createShortHabit();
command = new ArchiveHabitsCommand(Collections.singletonList(habit));
}
@Test
public void executeUndoRedo()
{
assertFalse(habit.isArchived());
command.execute();
assertTrue(habit.isArchived());
command.undo();
assertFalse(habit.isArchived());
command.execute();
assertTrue(habit.isArchived());
}
}

@ -0,0 +1,91 @@
/*
* 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.commands;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import org.isoron.uhabits.BaseTest;
import org.isoron.uhabits.commands.ChangeHabitColorCommand;
import org.isoron.uhabits.helpers.ColorHelper;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.unit.HabitFixtures;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.LinkedList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class ChangeHabitColorCommandTest extends BaseTest
{
private ChangeHabitColorCommand command;
private LinkedList<Habit> habits;
@Before
public void setup()
{
super.setup();
habits = new LinkedList<>();
for(int i = 0; i < 3; i ++)
{
Habit habit = HabitFixtures.createShortHabit();
habit.color = ColorHelper.palette[i+1];
habit.save();
habits.add(habit);
}
command = new ChangeHabitColorCommand(habits, ColorHelper.palette[0]);
}
@Test
public void executeUndoRedo()
{
checkOriginalColors();
command.execute();
checkNewColors();
command.undo();
checkOriginalColors();
command.execute();
checkNewColors();
}
private void checkOriginalColors()
{
int k = 0;
for(Habit h : habits)
assertThat(h.color, equalTo(ColorHelper.palette[++k]));
}
private void checkNewColors()
{
for(Habit h : habits)
assertThat(h.color, equalTo(ColorHelper.palette[0]));
}
}

@ -55,7 +55,7 @@ public class HabitsCSVExporterTest extends BaseTest
super.setup();
HabitFixtures.purgeHabits();
HabitFixtures.createNonDailyHabit();
HabitFixtures.createShortHabit();
HabitFixtures.createEmptyHabit();
Context targetContext = InstrumentationRegistry.getTargetContext();

@ -53,7 +53,7 @@ public class CheckmarkListTest extends BaseTest
super.setup();
HabitFixtures.purgeHabits();
nonDailyHabit = HabitFixtures.createNonDailyHabit();
nonDailyHabit = HabitFixtures.createShortHabit();
emptyHabit = HabitFixtures.createEmptyHabit();
}

@ -364,7 +364,7 @@ public class HabitTest extends BaseTest
public void writeCSV() throws IOException
{
HabitFixtures.createEmptyHabit();
HabitFixtures.createNonDailyHabit();
HabitFixtures.createShortHabit();
String expectedCSV =
"Name,Description,NumRepetitions,Interval,Color\n" +

@ -53,7 +53,7 @@ public class RepetitionListTest extends BaseTest
super.setup();
HabitFixtures.purgeHabits();
habit = HabitFixtures.createNonDailyHabit();
habit = HabitFixtures.createShortHabit();
emptyHabit = HabitFixtures.createEmptyHabit();
}

@ -140,7 +140,7 @@ public class ScoreListTest extends BaseTest
public void writeCSV() throws IOException
{
HabitFixtures.purgeHabits();
Habit habit = HabitFixtures.createNonDailyHabit();
Habit habit = HabitFixtures.createShortHabit();
String expectedCSV =
"2015-01-16,0.0519\n" +

@ -53,7 +53,7 @@ public class ExportCSVTaskTest extends BaseTest
@Test
public void exportCSV() throws Throwable
{
HabitFixtures.createNonDailyHabit();
HabitFixtures.createShortHabit();
List<Habit> habits = Habit.getAll(true);
ProgressBar bar = new ProgressBar(targetContext);

@ -44,7 +44,7 @@ public class CheckmarkViewTest extends ViewTest
{
super.setup();
habit = HabitFixtures.createNonDailyHabit();
habit = HabitFixtures.createShortHabit();
view = new CheckmarkView(targetContext);
view.setHabit(habit);
measureView(dpToPixels(100), dpToPixels(200), view);

@ -22,7 +22,6 @@ package org.isoron.uhabits.commands;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
import java.util.LinkedList;
import java.util.List;
public class ArchiveHabitsCommand extends Command
@ -30,12 +29,6 @@ public class ArchiveHabitsCommand extends Command
private List<Habit> habits;
public ArchiveHabitsCommand(Habit habit)
{
habits = new LinkedList<>();
habits.add(habit);
}
public ArchiveHabitsCommand(List<Habit> habits)
{
this.habits = habits;

@ -22,6 +22,7 @@ package org.isoron.uhabits.commands;
import com.activeandroid.ActiveAndroid;
import org.isoron.uhabits.R;
import org.isoron.uhabits.helpers.DatabaseHelper;
import org.isoron.uhabits.models.Habit;
import java.util.ArrayList;
@ -52,23 +53,19 @@ public class ChangeHabitColorCommand extends Command
@Override
public void undo()
{
ActiveAndroid.beginTransaction();
try
DatabaseHelper.executeAsTransaction(new DatabaseHelper.Command()
{
int k = 0;
for(Habit h : habits)
@Override
public void execute()
{
h.color = originalColors.get(k++);
h.save();
int k = 0;
for(Habit h : habits)
{
h.color = originalColors.get(k++);
h.save();
}
}
ActiveAndroid.setTransactionSuccessful();
}
finally
{
ActiveAndroid.endTransaction();
}
});
}
public Integer getExecuteStringId()

@ -1,33 +0,0 @@
/*
* 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.commands;
public class CommandFailedException extends RuntimeException
{
public CommandFailedException()
{
super();
}
public CommandFailedException(String message)
{
super(message);
}
}

@ -51,7 +51,7 @@ public class CreateHabitCommand extends Command
public void undo()
{
Habit habit = Habit.get(savedId);
if(habit == null) throw new CommandFailedException("Habit not found");
if(habit == null) throw new RuntimeException("Habit not found");
habit.delete();
}

@ -42,7 +42,7 @@ public class EditHabitCommand extends Command
public void execute()
{
Habit habit = Habit.get(savedId);
if(habit == null) throw new CommandFailedException("Habit not found");
if(habit == null) throw new RuntimeException("Habit not found");
habit.copyAttributes(modified);
habit.save();
@ -57,7 +57,7 @@ public class EditHabitCommand extends Command
public void undo()
{
Habit habit = Habit.get(savedId);
if(habit == null) throw new CommandFailedException("Habit not found");
if(habit == null) throw new RuntimeException("Habit not found");
habit.copyAttributes(original);
habit.save();

Loading…
Cancel
Save