Write tests for ListHabits behaviors

This commit is contained in:
2017-05-27 20:33:30 -04:00
parent 70423ddb0a
commit d8d4c4f55e
7 changed files with 371 additions and 30 deletions

View File

@@ -121,20 +121,6 @@ public class ListHabitsMenuBehavior
screen.applyTheme();
}
private void toggleShowArchived()
{
showArchived = !showArchived;
preferences.setShowArchived(showArchived);
updateAdapterFilter();
}
private void toggleShowCompleted()
{
showCompleted = !showCompleted;
preferences.setShowCompleted(showCompleted);
updateAdapterFilter();
}
private void updateAdapterFilter()
{
adapter.setFilter(new HabitMatcherBuilder()

View File

@@ -25,11 +25,14 @@ import org.isoron.uhabits.models.memory.*;
import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.utils.*;
import org.junit.*;
import org.junit.runner.*;
import org.mockito.junit.*;
import java.util.*;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class BaseUnitTest
{
protected HabitList habitList;
@@ -68,4 +71,10 @@ public class BaseUnitTest
cal.set(year, month, day);
return cal.getTimeInMillis();
}
@Test
public void nothing()
{
}
}

View File

@@ -24,9 +24,7 @@ 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.*;
@@ -35,7 +33,6 @@ 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

View File

@@ -0,0 +1,190 @@
/*
* 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.models.*;
import org.isoron.uhabits.preferences.*;
import org.isoron.uhabits.ui.*;
import org.junit.*;
import org.mockito.*;
import static junit.framework.TestCase.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.isoron.uhabits.models.HabitList.Order.*;
import static org.mockito.Mockito.*;
public class ListHabitsMenuBehaviorTest extends BaseUnitTest
{
private ListHabitsMenuBehavior behavior;
@Mock
private ListHabitsMenuBehavior.Screen screen;
@Mock
private ListHabitsMenuBehavior.Adapter adapter;
@Mock
private Preferences prefs;
@Mock
private ThemeSwitcher themeSwitcher;
@Captor
private ArgumentCaptor<HabitMatcher> matcherCaptor;
@Captor
private ArgumentCaptor<HabitList.Order> orderCaptor;
@Override
public void setUp()
{
super.setUp();
behavior =
new ListHabitsMenuBehavior(screen, adapter, prefs, themeSwitcher);
clearInvocations(adapter);
}
@Test
public void testInitialFilter()
{
when(prefs.getShowArchived()).thenReturn(true);
when(prefs.getShowCompleted()).thenReturn(true);
behavior =
new ListHabitsMenuBehavior(screen, adapter, prefs, themeSwitcher);
verify(adapter).setFilter(matcherCaptor.capture());
verify(adapter).refresh();
verifyNoMoreInteractions(adapter);
clearInvocations(adapter);
assertTrue(matcherCaptor.getValue().isArchivedAllowed());
assertTrue(matcherCaptor.getValue().isCompletedAllowed());
when(prefs.getShowArchived()).thenReturn(false);
when(prefs.getShowCompleted()).thenReturn(false);
behavior =
new ListHabitsMenuBehavior(screen, adapter, prefs, themeSwitcher);
verify(adapter).setFilter(matcherCaptor.capture());
verify(adapter).refresh();
verifyNoMoreInteractions(adapter);
assertFalse(matcherCaptor.getValue().isArchivedAllowed());
assertFalse(matcherCaptor.getValue().isCompletedAllowed());
}
@Test
public void testOnCreateHabit()
{
behavior.onCreateHabit();
verify(screen).showCreateHabitScreen();
}
@Test
public void testOnSortByColor()
{
behavior.onSortByColor();
verify(adapter).setOrder(orderCaptor.capture());
assertThat(orderCaptor.getValue(), equalTo(BY_COLOR));
}
@Test
public void testOnSortManually()
{
behavior.onSortByManually();
verify(adapter).setOrder(orderCaptor.capture());
assertThat(orderCaptor.getValue(), equalTo(BY_POSITION));
}
@Test
public void testOnSortScore()
{
behavior.onSortByScore();
verify(adapter).setOrder(orderCaptor.capture());
assertThat(orderCaptor.getValue(), equalTo(BY_SCORE));
}
@Test
public void testOnSortName()
{
behavior.onSortByName();
verify(adapter).setOrder(orderCaptor.capture());
assertThat(orderCaptor.getValue(), equalTo(BY_NAME));
}
@Test
public void testOnToggleShowArchived()
{
behavior.onToggleShowArchived();
verify(adapter).setFilter(matcherCaptor.capture());
assertTrue(matcherCaptor.getValue().isArchivedAllowed());
clearInvocations(adapter);
behavior.onToggleShowArchived();
verify(adapter).setFilter(matcherCaptor.capture());
assertFalse(matcherCaptor.getValue().isArchivedAllowed());
}
@Test
public void testOnToggleShowCompleted()
{
behavior.onToggleShowCompleted();
verify(adapter).setFilter(matcherCaptor.capture());
assertTrue(matcherCaptor.getValue().isCompletedAllowed());
clearInvocations(adapter);
behavior.onToggleShowCompleted();
verify(adapter).setFilter(matcherCaptor.capture());
assertFalse(matcherCaptor.getValue().isCompletedAllowed());
}
@Test
public void testOnViewAbout()
{
behavior.onViewAbout();
verify(screen).showAboutScreen();
}
@Test
public void testOnViewFAQ()
{
behavior.onViewFAQ();
verify(screen).showFAQScreen();
}
@Test
public void testOnViewSettings()
{
behavior.onViewSettings();
verify(screen).showSettingsScreen();
}
@Test
public void testOnToggleNightMode()
{
behavior.onToggleNightMode();
verify(themeSwitcher).toggleNightMode();
verify(screen).applyTheme();
}
}

View File

@@ -0,0 +1,159 @@
/*
* 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.models.*;
import org.isoron.uhabits.ui.callbacks.*;
import org.junit.*;
import org.mockito.*;
import java.util.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static junit.framework.TestCase.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
public class ListHabitsSelectionMenuBehaviorTest extends BaseUnitTest
{
@Mock
private ListHabitsSelectionMenuBehavior.Screen screen;
@Mock
private ListHabitsSelectionMenuBehavior.Adapter adapter;
private ListHabitsSelectionMenuBehavior behavior;
private Habit habit1, habit2, habit3;
@Captor
private ArgumentCaptor<OnColorPickedCallback> colorPickerCallback;
@Captor
private ArgumentCaptor<OnConfirmedCallback> deleteCallback;
@Test
public void canArchive() throws Exception
{
when(adapter.getSelected()).thenReturn(asList(habit1, habit2));
assertFalse(behavior.canArchive());
when(adapter.getSelected()).thenReturn(asList(habit2, habit3));
assertTrue(behavior.canArchive());
}
@Test
public void canEdit() throws Exception
{
when(adapter.getSelected()).thenReturn(singletonList(habit1));
assertTrue(behavior.canEdit());
when(adapter.getSelected()).thenReturn(asList(habit1, habit2));
assertFalse(behavior.canEdit());
}
@Test
public void canUnarchive() throws Exception
{
when(adapter.getSelected()).thenReturn(asList(habit1, habit2));
assertFalse(behavior.canUnarchive());
when(adapter.getSelected()).thenReturn(singletonList(habit1));
assertTrue(behavior.canUnarchive());
}
@Test
public void onArchiveHabits() throws Exception
{
assertFalse(habit2.isArchived());
when(adapter.getSelected()).thenReturn(singletonList(habit2));
behavior.onArchiveHabits();
assertTrue(habit2.isArchived());
}
@Test
public void onChangeColor() throws Exception
{
assertThat(habit1.getColor(), equalTo(5));
assertThat(habit2.getColor(), equalTo(5));
when(adapter.getSelected()).thenReturn(asList(habit1, habit2));
behavior.onChangeColor();
verify(screen).showColorPicker(eq(5), colorPickerCallback.capture());
colorPickerCallback.getValue().onColorPicked(30);
assertThat(habit1.getColor(), equalTo(30));
}
@Test
public void onDeleteHabits() throws Exception
{
Long id = habit1.getId();
assertNotNull(id);
assertNotNull(habitList.getById(id));
when(adapter.getSelected()).thenReturn(singletonList(habit1));
behavior.onDeleteHabits();
verify(screen).showDeleteConfirmationScreen(deleteCallback.capture());
deleteCallback.getValue().onConfirmed();
assertNull(habitList.getById(id));
}
@Test
public void onEditHabits() throws Exception
{
List<Habit> selected = asList(habit1, habit2);
when(adapter.getSelected()).thenReturn(selected);
behavior.onEditHabits();
verify(screen).showEditHabitsScreen(selected);
}
@Test
public void onUnarchiveHabits() throws Exception
{
assertTrue(habit1.isArchived());
when(adapter.getSelected()).thenReturn(singletonList(habit1));
behavior.onUnarchiveHabits();
assertFalse(habit1.isArchived());
}
@Override
public void setUp()
{
super.setUp();
habit1 = fixtures.createShortHabit();
habit1.setArchived(true);
habit2 = fixtures.createShortHabit();
habit3 = fixtures.createShortHabit();
habitList.add(habit1);
habitList.add(habit2);
habitList.add(habit3);
behavior =
new ListHabitsSelectionMenuBehavior(habitList, screen, adapter,
commandRunner);
}
}