Write tests for ListHabits behaviors

pull/87/merge
Alinson S. Xavier 8 years ago
parent 70423ddb0a
commit d8d4c4f55e

@ -27,6 +27,7 @@ import org.isoron.uhabits.activities.habits.list.model.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.preferences.*;
import org.isoron.uhabits.ui.*;
import org.isoron.uhabits.ui.screens.habits.list.*;
import org.junit.*;
import org.mockito.*;
@ -48,6 +49,8 @@ public class ListHabitsMenuTest extends BaseAndroidTest
private ArgumentCaptor<HabitMatcher> matcherCaptor;
private ListHabitsMenuBehavior behavior;
@Override
public void setUp()
{
@ -58,13 +61,14 @@ public class ListHabitsMenuTest extends BaseAndroidTest
adapter = mock(HabitCardListAdapter.class);
preferences = mock(AndroidPreferences.class);
themeSwitcher = mock(ThemeSwitcher.class);
behavior = mock(ListHabitsMenuBehavior.class);
when(preferences.getShowArchived()).thenReturn(false);
when(preferences.getShowCompleted()).thenReturn(false);
when(themeSwitcher.isNightMode()).thenReturn(false);
menu = new ListHabitsMenu(activity, preferences,
themeSwitcher);
themeSwitcher, behavior);
matcherCaptor = ArgumentCaptor.forClass(HabitMatcher.class);
@ -121,7 +125,7 @@ public class ListHabitsMenuTest extends BaseAndroidTest
public void testOnSelected_nightMode()
{
onItemSelected(R.id.actionToggleNightMode);
verify(screen).toggleNightMode();
verify(screen).applyTheme();
}
@Test

@ -25,17 +25,19 @@ import android.content.*;
import org.isoron.androidbase.activities.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.activities.common.dialogs.*;
import org.isoron.uhabits.activities.common.dialogs.ColorPickerDialog.*;
import org.isoron.uhabits.activities.habits.edit.*;
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.preferences.*;
import org.isoron.uhabits.ui.*;
import org.isoron.uhabits.ui.callbacks.*;
import org.junit.*;
import org.junit.runner.*;
import org.junit.runners.*;
import java.util.*;
import static org.isoron.uhabits.activities.habits.list.ListHabitsScreen.*;
import static org.mockito.Mockito.*;
@ -122,8 +124,6 @@ public class ListHabitsScreenTest extends BaseAndroidTest
public void testOnCommand()
{
Command c = mock(Command.class);
when(getExecuteString(c)).thenReturn(
R.string.toast_habit_deleted);
screen.onCommandExecuted(c, null);
verify(screen).showMessage(R.string.toast_habit_deleted);
}
@ -174,12 +174,11 @@ public class ListHabitsScreenTest extends BaseAndroidTest
@Test
public void testShowColorPicker()
{
habit.setColor(999);
ColorPickerDialog picker = mock(ColorPickerDialog.class);
when(colorPickerDialogFactory.create(999)).thenReturn(picker);
OnColorSelectedListener callback = mock(OnColorSelectedListener.class);
OnColorPickedCallback callback = mock(OnColorPickedCallback.class);
screen.showColorPicker(habit, callback);
screen.showColorPicker(999, callback);
verify(activity).showDialog(eq(picker), any());
verify(picker).setListener(callback);
@ -188,9 +187,7 @@ public class ListHabitsScreenTest extends BaseAndroidTest
@Test
public void testShowDeleteConfirmationScreen()
{
ConfirmDeleteDialog.Callback callback;
callback = mock(ConfirmDeleteDialog.Callback.class);
OnConfirmedCallback callback = mock(OnConfirmedCallback.class);
ConfirmDeleteDialog dialog = mock(ConfirmDeleteDialog.class);
when(confirmDeleteDialogFactory.create(callback)).thenReturn(dialog);
@ -204,8 +201,7 @@ public class ListHabitsScreenTest extends BaseAndroidTest
{
EditHabitDialog dialog = mock(EditHabitDialog.class);
when(dialogFactory.edit(habit)).thenReturn(dialog);
screen.showEditHabitScreen(habit);
screen.showEditHabitsScreen(Collections.singletonList(habit));
verify(activity).showDialog(eq(dialog), any());
}

@ -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()

@ -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()
{
}
}

@ -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

@ -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();
}
}

@ -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);
}
}
Loading…
Cancel
Save