Move ListHabitsBehavior to uhabits-core

This commit is contained in:
2017-05-26 22:04:59 -04:00
parent 95385fa8f4
commit 3e558be4d4
31 changed files with 665 additions and 341 deletions

View File

@@ -19,6 +19,7 @@
package org.isoron.uhabits;
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.models.memory.*;
import org.isoron.uhabits.tasks.*;
@@ -27,6 +28,8 @@ import org.junit.*;
import java.util.*;
import static org.mockito.Mockito.*;
public class BaseUnitTest
{
protected HabitList habitList;
@@ -37,6 +40,8 @@ public class BaseUnitTest
protected SingleThreadTaskRunner taskRunner;
protected CommandRunner commandRunner;
@Before
public void setUp()
{
@@ -45,9 +50,10 @@ public class BaseUnitTest
DateUtils.setFixedLocalTime(fixed_local_time);
modelFactory = new MemoryModelFactory();
habitList = modelFactory.buildHabitList();
habitList = spy(modelFactory.buildHabitList());
fixtures = new HabitFixtures(modelFactory);
taskRunner = new SingleThreadTaskRunner();
commandRunner = new CommandRunner(taskRunner);
}
@After

View File

@@ -62,6 +62,31 @@ public class HabitFixtures
return habit;
}
public Habit createNumericalHabit()
{
Habit habit = modelFactory.buildHabit();
habit.setType(Habit.NUMBER_HABIT);
habit.setName("Run");
habit.setDescription("How many miles did you run today?");
habit.setUnit("miles");
habit.setTargetType(Habit.AT_LEAST);
habit.setTargetValue(2.0);
habit.setColor(1);
long day = DateUtils.millisecondsInOneDay;
long today = DateUtils.getStartOfToday();
int times[] = { 0, 1, 3, 5, 7, 8, 9, 10 };
int values[] = { 100, 200, 300, 400, 500, 600, 700, 800 };
for(int i = 0; i < times.length; i++)
{
long timestamp = today - times[i] * day;
habit.getRepetitions().add(new Repetition(timestamp, values[i]));
}
return habit;
}
public Habit createShortHabit()
{
Habit habit = modelFactory.buildHabit();

View File

@@ -0,0 +1,132 @@
/*
* Copyright (C) 2017 Á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.ui.habits.list;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.preferences.*;
import org.isoron.uhabits.utils.*;
import org.junit.*;
import org.junit.runner.*;
import org.mockito.*;
import org.mockito.junit.*;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.CoreMatchers.*;
import static org.isoron.uhabits.ui.habits.list.ListHabitsBehavior.Message.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class ListHabitsBehaviorTest extends BaseUnitTest
{
@Mock
private ListHabitsBehavior.System system;
@Mock
private Preferences prefs;
private ListHabitsBehavior behavior;
@Mock
private ListHabitsBehavior.Screen screen;
private Habit habit1, habit2;
@Captor
ArgumentCaptor<ListHabitsBehavior.NumberPickerCallback> captor;
@Override
@Before
public void setUp()
{
super.setUp();
habit1 = fixtures.createShortHabit();
habit2 = fixtures.createNumericalHabit();
habitList.add(habit1);
habitList.add(habit2);
clearInvocations(habitList);
behavior = new ListHabitsBehavior(habitList, system, taskRunner, screen,
commandRunner, prefs);
}
@Test
public void testOnEdit()
{
behavior.onEdit(habit2, DateUtils.getStartOfToday());
verify(screen).showNumberPicker(eq(0.1), eq("miles"), captor.capture());
captor.getValue().onNumberPicked(100);
assertThat(habit2.getCheckmarks().getTodayValue(), equalTo(100000));
}
@Test
public void testOnHabitClick()
{
behavior.onClickHabit(habit1);
verify(screen).showHabitScreen(habit1);
}
@Test
public void testOnHabitReorder()
{
Habit from = habit1;
Habit to = habit2;
behavior.onReorderHabit(from, to);
verify(habitList).reorder(from, to);
}
@Test
public void testOnRepairDB()
{
behavior.onRepairDB();
verify(habitList).repair();
verify(screen).showMessage(DATABASE_REPAIRED);
}
@Test
public void testOnStartup_firstLaunch()
{
long today = DateUtils.getStartOfToday();
when(prefs.isFirstRun()).thenReturn(true);
behavior.onStartup();
verify(prefs).setFirstRun(false);
verify(prefs).updateLastHint(-1, today);
verify(screen).showIntroScreen();
}
@Test
public void testOnStartup_notFirstLaunch()
{
when(prefs.isFirstRun()).thenReturn(false);
behavior.onStartup();
verify(prefs).incrementLaunchCount();
}
@Test
public void testOnToggle()
{
assertTrue(habit1.isCompletedToday());
behavior.onToggle(habit1, DateUtils.getStartOfToday());
assertFalse(habit1.isCompletedToday());
}
}