Clean up BaseUnitTest

pull/151/head
Alinson S. Xavier 9 years ago
parent 5f4ac21a41
commit 03dd24d17b

@ -19,76 +19,34 @@
package org.isoron.uhabits; package org.isoron.uhabits;
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.io.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.activities.common.dialogs.*; import org.isoron.uhabits.models.memory.*;
import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.utils.*; import org.isoron.uhabits.utils.*;
import org.junit.*; import org.junit.*;
import javax.inject.*;
public class BaseUnitTest public class BaseUnitTest
{ {
// 8:00am, January 25th, 2015 (UTC)
public static final long FIXED_LOCAL_TIME = 1422172800000L;
@Inject
protected ModelFactory modelFactory;
@Inject
protected DialogFactory dialogFactory;
@Inject
protected IntentFactory intentFactory;
@Inject
protected HabitList habitList; protected HabitList habitList;
@Inject
protected HabitLogger logger;
@Inject
protected PendingIntentFactory pendingIntentFactory;
@Inject
protected IntentScheduler intentScheduler;
@Inject
protected DirFinder dirFinder;
@Inject
protected CommandRunner commandRunner;
protected TestComponent testComponent;
protected HabitFixtures fixtures; protected HabitFixtures fixtures;
protected SingleThreadTaskRunner taskRunner; protected MemoryModelFactory modelFactory;
public void log(String format, Object... args)
{
System.out.println(String.format(format, args));
}
@Before @Before
public void setUp() public void setUp()
{ {
// 8:00am, January 25th, 2015 (UTC)
long FIXED_LOCAL_TIME = 1422172800000L;
DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME); DateUtils.setFixedLocalTime(FIXED_LOCAL_TIME);
testComponent = DaggerTestComponent.create();
HabitsApplication.setComponent(testComponent); modelFactory = new MemoryModelFactory();
testComponent.inject(this); habitList = modelFactory.buildHabitList();
fixtures = new HabitFixtures(modelFactory, habitList); fixtures = new HabitFixtures(modelFactory);
taskRunner = new SingleThreadTaskRunner();
} }
@After @After
public void tearDown() public void tearDown()
{ {
DateUtils.setFixedLocalTime(null); DateUtils.setFixedLocalTime(null);
fixtures.purgeHabits();
} }
} }

@ -1,121 +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;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.io.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.activities.common.dialogs.*;
import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.widgets.*;
import javax.inject.*;
import dagger.*;
import static org.mockito.Mockito.*;
@Module
public class MockModule
{
@Provides
@Singleton
DialogFactory provideDialogFactory()
{
return mock(DialogFactory.class);
}
@Provides
@Singleton
DirFinder provideDirFinder()
{
return mock(DirFinder.class);
}
@Provides
Habit provideHabit()
{
return mock(Habit.class);
}
@Provides
@Singleton
IntentFactory provideIntentFactory()
{
return mock(IntentFactory.class);
}
@Provides
@Singleton
IntentScheduler provideIntentScheduler()
{
return mock(IntentScheduler.class);
}
@Provides
@Singleton
HabitLogger provideLogger()
{
return mock(HabitLogger.class);
}
@Singleton
@Provides
PendingIntentFactory providePendingIntentFactory()
{
return mock(PendingIntentFactory.class);
}
@Singleton
@Provides
Preferences providePreferences()
{
return mock(Preferences.class);
}
@Provides
@Singleton
ReminderScheduler provideReminderScheduler()
{
return mock(ReminderScheduler.class);
}
@Provides
@Singleton
WidgetPreferences provideWidgetPreferences()
{
return mock(WidgetPreferences.class);
}
@Provides
@Singleton
TaskRunner provideTaskRunner()
{
return new SingleThreadTaskRunner();
}
@Provides
@Singleton
WidgetUpdater provideWidgetUpdate()
{
return mock(WidgetUpdater.class);
}
}

@ -1,34 +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;
import org.isoron.uhabits.models.memory.*;
import javax.inject.*;
import dagger.*;
@Singleton
@Component(modules = { MockModule.class, MemoryModelFactory.class })
public interface TestComponent extends AppComponent
{
void inject(BaseUnitTest baseUnitTest);
}

@ -64,6 +64,14 @@ public class ListHabitsScreenTest extends BaseUnitTest
private FilePickerDialogFactory filePickerDialogFactory; private FilePickerDialogFactory filePickerDialogFactory;
private IntentFactory intentFactory;
private DialogFactory dialogFactory;
private DirFinder dirFinder;
private CommandRunner commandRunner;
@Before @Before
@Override @Override
public void setUp() public void setUp()

@ -22,6 +22,7 @@ package org.isoron.uhabits.activities.habits.list.model;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.commands.*; import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.utils.*; import org.isoron.uhabits.utils.*;
import org.junit.*; import org.junit.*;
@ -38,19 +39,22 @@ public class HabitCardListCacheTest extends BaseUnitTest
private HabitCardListCache.Listener listener; private HabitCardListCache.Listener listener;
private CommandRunner commandRunner;
@Override @Override
public void setUp() public void setUp()
{ {
super.setUp(); super.setUp();
fixtures.purgeHabits();
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
if (i == 3) fixtures.createLongHabit(); if (i == 3) habitList.add(fixtures.createLongHabit());
else fixtures.createShortHabit(); else habitList.add(fixtures.createShortHabit());
} }
SingleThreadTaskRunner taskRunner = new SingleThreadTaskRunner();
commandRunner = new CommandRunner(taskRunner);
cache = new HabitCardListCache(habitList, commandRunner, taskRunner); cache = new HabitCardListCache(habitList, commandRunner, taskRunner);
cache.setCheckmarkCount(10); cache.setCheckmarkCount(10);
cache.refreshAllHabits(); cache.refreshAllHabits();

@ -29,7 +29,6 @@ import static org.hamcrest.MatcherAssert.*;
public class CreateHabitCommandTest extends BaseUnitTest public class CreateHabitCommandTest extends BaseUnitTest
{ {
private CreateHabitCommand command; private CreateHabitCommand command;
private Habit model; private Habit model;
@ -44,7 +43,6 @@ public class CreateHabitCommandTest extends BaseUnitTest
model.setName("New habit"); model.setName("New habit");
command = new CreateHabitCommand(modelFactory, habitList, model); command = new CreateHabitCommand(modelFactory, habitList, model);
fixtures.purgeHabits();
} }
@Test @Test

@ -33,7 +33,7 @@ public class DeleteHabitsCommandTest extends BaseUnitTest
{ {
private DeleteHabitsCommand command; private DeleteHabitsCommand command;
private LinkedList<Habit> habits; private LinkedList<Habit> selected;
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@ -43,22 +43,22 @@ public class DeleteHabitsCommandTest extends BaseUnitTest
public void setUp() public void setUp()
{ {
super.setUp(); super.setUp();
selected = new LinkedList<>();
fixtures.purgeHabits();
habits = new LinkedList<>();
// Habits that should be deleted // Habits that should be deleted
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
Habit habit = fixtures.createShortHabit(); Habit habit = fixtures.createShortHabit();
habits.add(habit); habitList.add(habit);
selected.add(habit);
} }
// Extra habit that should not be deleted // Extra habit that should not be deleted
Habit extraHabit = fixtures.createShortHabit(); Habit extraHabit = fixtures.createShortHabit();
extraHabit.setName("extra"); extraHabit.setName("extra");
habitList.add(extraHabit);
command = new DeleteHabitsCommand(habitList, habits); command = new DeleteHabitsCommand(habitList, selected);
} }
@Test @Test

@ -44,10 +44,12 @@ public class EditHabitCommandTest extends BaseUnitTest
habit = fixtures.createShortHabit(); habit = fixtures.createShortHabit();
habit.setName("original"); habit.setName("original");
habit.setFrequency(Frequency.DAILY); habit.setFrequency(Frequency.DAILY);
habitList.add(habit);
modified = fixtures.createEmptyHabit(); modified = fixtures.createEmptyHabit();
modified.copyFrom(habit); modified.copyFrom(habit);
modified.setName("modified"); modified.setName("modified");
habitList.add(modified);
} }
@Test @Test

@ -19,19 +19,15 @@
package org.isoron.uhabits.models; package org.isoron.uhabits.models;
import org.isoron.uhabits.BaseUnitTest; import org.isoron.uhabits.*;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.utils.DateUtils; import org.junit.*;
import org.junit.Test;
import java.io.IOException; import java.io.*;
import java.io.StringWriter;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.*;
import static org.isoron.uhabits.models.Checkmark.CHECKED_EXPLICITLY; import static org.isoron.uhabits.models.Checkmark.*;
import static org.isoron.uhabits.models.Checkmark.CHECKED_IMPLICITLY;
import static org.isoron.uhabits.models.Checkmark.UNCHECKED;
public class CheckmarkListTest extends BaseUnitTest public class CheckmarkListTest extends BaseUnitTest
{ {
@ -46,7 +42,10 @@ public class CheckmarkListTest extends BaseUnitTest
fixtures.createShortHabit(); fixtures.createShortHabit();
nonDailyHabit = fixtures.createShortHabit(); nonDailyHabit = fixtures.createShortHabit();
habitList.add(nonDailyHabit);
emptyHabit = fixtures.createEmptyHabit(); emptyHabit = fixtures.createEmptyHabit();
habitList.add(emptyHabit);
} }
@Test @Test

@ -29,12 +29,9 @@ public class HabitFixtures
private final ModelFactory modelFactory; private final ModelFactory modelFactory;
private final HabitList habitList; public HabitFixtures(ModelFactory modelFactory)
public HabitFixtures(ModelFactory modelFactory, HabitList habitList)
{ {
this.modelFactory = modelFactory; this.modelFactory = modelFactory;
this.habitList = habitList;
} }
public Habit createEmptyHabit() public Habit createEmptyHabit()
@ -44,7 +41,6 @@ public class HabitFixtures
habit.setDescription("Did you meditate this morning?"); habit.setDescription("Did you meditate this morning?");
habit.setColor(3); habit.setColor(3);
habit.setFrequency(Frequency.DAILY); habit.setFrequency(Frequency.DAILY);
habitList.add(habit);
return habit; return habit;
} }
@ -72,7 +68,6 @@ public class HabitFixtures
habit.setName("Wake up early"); habit.setName("Wake up early");
habit.setDescription("Did you wake up before 6am?"); habit.setDescription("Did you wake up before 6am?");
habit.setFrequency(new Frequency(2, 3)); habit.setFrequency(new Frequency(2, 3));
habitList.add(habit);
long timestamp = DateUtils.getStartOfToday(); long timestamp = DateUtils.getStartOfToday();
for (boolean c : NON_DAILY_HABIT_CHECKS) for (boolean c : NON_DAILY_HABIT_CHECKS)
@ -83,9 +78,4 @@ public class HabitFixtures
return habit; return habit;
} }
public void purgeHabits()
{
habitList.removeAll();
}
} }

@ -44,14 +44,12 @@ public class HabitListTest extends BaseUnitTest
public void setUp() public void setUp()
{ {
super.setUp(); super.setUp();
fixtures.purgeHabits();
habitsArray = new ArrayList<>(); habitsArray = new ArrayList<>();
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
Habit habit = fixtures.createEmptyHabit(); Habit habit = fixtures.createEmptyHabit();
habit.setId((long) i); habitList.add(habit);
habitsArray.add(habit); habitsArray.add(habit);
if (i % 3 == 0) if (i % 3 == 0)

@ -19,16 +19,12 @@
package org.isoron.uhabits.models; package org.isoron.uhabits.models;
import org.isoron.uhabits.BaseUnitTest; import org.isoron.uhabits.*;
import org.isoron.uhabits.utils.*; import org.isoron.uhabits.utils.*;
import org.junit.Before; import org.junit.*;
import org.junit.Test;
import java.io.IOException; import java.io.*;
import java.io.StringWriter; import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.MatcherAssert.*;
@ -175,13 +171,6 @@ public class ScoreListTest extends BaseUnitTest
assertThat(writer.toString(), equalTo(expectedCSV)); assertThat(writer.toString(), equalTo(expectedCSV));
} }
private void log(List<Score> list)
{
SimpleDateFormat df = DateFormats.getCSVDateFormat();
for (Score s : list)
log("%s %d", df.format(new Date(s.getTimestamp())), s.getValue());
}
private void toggleRepetitions(final int from, final int to) private void toggleRepetitions(final int from, final int to)
{ {
RepetitionList reps = habit.getRepetitions(); RepetitionList reps = habit.getRepetitions();

@ -137,11 +137,4 @@ public class StreakListTest extends BaseUnitTest
s = streaks.getNewestComputed(); s = streaks.getNewestComputed();
assertThat(s.getEnd(), equalTo(today - 12 * day)); assertThat(s.getEnd(), equalTo(today - 12 * day));
} }
private void log(List<Streak> sl)
{
for (Streak s : sl)
log("%5d -- %5d (%d)", (today - s.getEnd()) / day,
(today - s.getStart()) / day, s.getLength());
}
} }

@ -22,7 +22,6 @@ package org.isoron.uhabits.utils;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.junit.*; import org.junit.*;
import java.text.*;
import java.util.*; import java.util.*;
import static java.util.Calendar.*; import static java.util.Calendar.*;
@ -142,11 +141,4 @@ public class DateUtilsTest extends BaseUnitTest
cal.set(year, month, day); cal.set(year, month, day);
return cal.getTimeInMillis(); return cal.getTimeInMillis();
} }
private void log(long timestamp)
{
DateFormat df = SimpleDateFormat.getDateTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("GMT"));
log("%s", df.format(new Date(timestamp)));
}
} }

@ -22,6 +22,7 @@ package org.isoron.uhabits.utils;
import android.app.*; import android.app.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.junit.*; import org.junit.*;
@ -36,12 +37,22 @@ public class ReminderSchedulerTest extends BaseUnitTest
private ReminderScheduler reminderScheduler; private ReminderScheduler reminderScheduler;
private HabitLogger logger;
private PendingIntentFactory pendingIntentFactory;
private IntentScheduler intentScheduler;
@Before @Before
@Override @Override
public void setUp() public void setUp()
{ {
super.setUp(); super.setUp();
intent = mock(PendingIntent.class); intent = mock(PendingIntent.class);
logger = mock(HabitLogger.class);
pendingIntentFactory = mock(PendingIntentFactory.class);
intentScheduler = mock(IntentScheduler.class);
reminderScheduler = reminderScheduler =
new ReminderScheduler(pendingIntentFactory, intentScheduler, new ReminderScheduler(pendingIntentFactory, intentScheduler,
logger); logger);
@ -78,15 +89,16 @@ public class ReminderSchedulerTest extends BaseUnitTest
long now = 1422277200000L; // 13:00 jan 26, 2015 (UTC) long now = 1422277200000L; // 13:00 jan 26, 2015 (UTC)
DateUtils.setFixedLocalTime(now); DateUtils.setFixedLocalTime(now);
fixtures.purgeHabits();
Habit h1 = fixtures.createEmptyHabit(); Habit h1 = fixtures.createEmptyHabit();
h1.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY)); h1.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
habitList.add(h1);
Habit h2 = fixtures.createEmptyHabit(); Habit h2 = fixtures.createEmptyHabit();
h2.setReminder(new Reminder(18, 30, WeekdayList.EVERY_DAY)); h2.setReminder(new Reminder(18, 30, WeekdayList.EVERY_DAY));
habitList.add(h2);
fixtures.createEmptyHabit(); Habit h3 = fixtures.createEmptyHabit();
habitList.add(h3);
reminderScheduler.schedule(habitList); reminderScheduler.schedule(habitList);

Loading…
Cancel
Save