Write missing tests

pull/87/merge
Alinson S. Xavier 8 years ago
parent d8d4c4f55e
commit 56c5fb6c9d

@ -28,6 +28,12 @@ jacocoTestReport {
xml.enabled = true
html.enabled = true
}
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: '**/*Factory.*')
})
}
}
check.dependsOn jacocoTestReport

@ -67,14 +67,6 @@ public class ListHabitsBehavior
screen.showHabitScreen(h);
}
// public void onExportDB()
// {
// taskRunner.execute(exportDBFactory.create(filename -> {
// if (filename != null) screen.showSendFileScreen(filename);
// else screen.showMessage(R.string.could_not_export);
// }));
// }
public void onEdit(@NonNull Habit habit, long timestamp)
{
CheckmarkList checkmarks = habit.getCheckmarks();
@ -103,30 +95,6 @@ public class ListHabitsBehavior
}));
}
// public void onImportData(@NonNull File file,
// @NonNull OnFinishedListener finishedListener)
// {
// taskRunner.execute(importTaskFactory.create(file, result -> {
// switch (result)
// {
// case ImportDataTask.SUCCESS:
// adapter.refresh();
// screen.showMessage(R.string.habits_imported);
// break;
//
// case ImportDataTask.NOT_RECOGNIZED:
// screen.showMessage(R.string.file_not_recognized);
// break;
//
// default:
// screen.showMessage(R.string.could_not_import);
// break;
// }
//
// finishedListener.onFinish();
// }));
// }
public void onReorderHabit(@NonNull Habit from, @NonNull Habit to)
{
taskRunner.execute(() -> habitList.reorder(from, to));

@ -17,15 +17,15 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.activities.habits.list.model;
package org.isoron.uhabits.ui.screens.habits.list;
import junit.framework.Assert;
import org.isoron.uhabits.*;
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.ui.screens.habits.list.*;
import org.isoron.uhabits.utils.*;
import org.junit.*;
import org.junit.Test;
import java.util.*;
@ -33,14 +33,12 @@ import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.core.IsEqual.*;
import static org.mockito.Mockito.*;
public class HabitCardListCacheTest extends BaseAndroidTest
public class HabitCardListCacheTest extends BaseUnitTest
{
private HabitCardListCache cache;
private HabitCardListCache.Listener listener;
private CommandRunner commandRunner;
@Override
public void setUp()
{
@ -49,13 +47,10 @@ public class HabitCardListCacheTest extends BaseAndroidTest
for (int i = 0; i < 10; i++)
{
if (i == 3) fixtures.createLongHabit();
else fixtures.createShortHabit();
if (i == 3) habitList.add(fixtures.createLongHabit());
else habitList.add(fixtures.createShortHabit());
}
SingleThreadTaskRunner taskRunner = new SingleThreadTaskRunner();
commandRunner = new CommandRunner(taskRunner);
cache = new HabitCardListCache(habitList, commandRunner, taskRunner);
cache.setCheckmarkCount(10);
cache.refreshAllHabits();
@ -105,7 +100,7 @@ public class HabitCardListCacheTest extends BaseAndroidTest
assertThat(cache.getHabitCount(), equalTo(10));
Habit h = habitList.getByPosition(3);
assertNotNull(h.getId());
junit.framework.Assert.assertNotNull(h.getId());
double score = h.getScores().getTodayValue();
assertThat(cache.getHabitByPosition(3), equalTo(h));
@ -188,7 +183,7 @@ public class HabitCardListCacheTest extends BaseAndroidTest
protected void removeHabitAt(int position)
{
Habit h = habitList.getByPosition(position);
assertNotNull(h);
Assert.assertNotNull(h);
habitList.remove(h);
}

@ -0,0 +1,79 @@
/*
* 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.screens.habits.list;
import org.isoron.uhabits.*;
import org.isoron.uhabits.preferences.*;
import org.isoron.uhabits.utils.*;
import org.junit.*;
import org.mockito.*;
import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
public class HintListTest extends BaseUnitTest
{
private HintList hintList;
private String[] hints;
@Mock
private Preferences prefs;
private long today;
private long yesterday;
@Override
public void setUp()
{
super.setUp();
today = DateUtils.getStartOfToday();
yesterday = today - DateUtils.millisecondsInOneDay;
hints = new String[]{ "hint1", "hint2", "hint3" };
hintList = new HintList(prefs, hints);
}
@Test
public void pop() throws Exception
{
when(prefs.getLastHintNumber()).thenReturn(-1);
assertThat(hintList.pop(), equalTo("hint1"));
verify(prefs).updateLastHint(0, today);
when(prefs.getLastHintNumber()).thenReturn(2);
assertNull(hintList.pop());
}
@Test
public void shouldShow() throws Exception
{
when(prefs.getLastHintTimestamp()).thenReturn(today);
assertFalse(hintList.shouldShow());
when(prefs.getLastHintTimestamp()).thenReturn(yesterday);
assertTrue(hintList.shouldShow());
}
}

@ -26,11 +26,16 @@ import org.isoron.uhabits.utils.*;
import org.junit.*;
import org.mockito.*;
import java.io.*;
import static java.nio.file.Files.*;
import static junit.framework.TestCase.assertTrue;
import static org.apache.commons.io.FileUtils.*;
import static org.hamcrest.CoreMatchers.*;
import static org.isoron.uhabits.ui.screens.habits.list.ListHabitsBehavior.Message.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.*;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.*;
public class ListHabitsBehaviorTest extends BaseUnitTest
@ -49,7 +54,7 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
private Habit habit1, habit2;
@Captor
ArgumentCaptor<ListHabitsBehavior.NumberPickerCallback> captor;
ArgumentCaptor<ListHabitsBehavior.NumberPickerCallback> picker;
@Override
@Before
@ -70,11 +75,32 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
public void testOnEdit()
{
behavior.onEdit(habit2, DateUtils.getStartOfToday());
verify(screen).showNumberPicker(eq(0.1), eq("miles"), captor.capture());
captor.getValue().onNumberPicked(100);
verify(screen).showNumberPicker(eq(0.1), eq("miles"), picker.capture());
picker.getValue().onNumberPicked(100);
assertThat(habit2.getCheckmarks().getTodayValue(), equalTo(100000));
}
@Test
public void testOnExportCSV() throws Exception
{
File outputDir = createTempDirectory("CSV").toFile();
when(system.getCSVOutputDir()).thenReturn(outputDir);
behavior.onExportCSV();
verify(screen).showSendFileScreen(any());
assertThat(listFiles(outputDir, null, false).size(), equalTo(1));
deleteDirectory(outputDir);
}
@Test
public void testOnExportCSV_fail() throws Exception
{
File outputDir = createTempDirectory("CSV").toFile();
outputDir.setWritable(false);
when(system.getCSVOutputDir()).thenReturn(outputDir);
behavior.onExportCSV();
verify(screen).showMessage(COULD_NOT_EXPORT);
}
@Test
public void testOnHabitClick()
{
@ -126,4 +152,18 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
behavior.onToggle(habit1, DateUtils.getStartOfToday());
assertFalse(habit1.isCompletedToday());
}
@Test
public void testOnSendBugReport() throws IOException
{
when(system.getBugReport()).thenReturn("hello");
behavior.onSendBugReport();
verify(screen).showSendBugReportToDeveloperScreen("hello");
when(system.getBugReport()).thenThrow(new IOException());
behavior.onSendBugReport();
verify(screen).showMessage(COULD_NOT_GENERATE_BUG_REPORT);
}
}
Loading…
Cancel
Save