Write tests for ListHabits controller, menu and screen

pull/165/head
Alinson S. Xavier 9 years ago
parent 7b8ab6a625
commit fc2087fe68

@ -51,7 +51,7 @@ public class MainActivityActions
String num = "4";
String den = "8";
onView(withId(R.id.action_add)).perform(click());
onView(withId(R.id.actionAdd)).perform(click());
typeHabitData(name, description, num, den);

@ -145,7 +145,7 @@ public class MainTest
@Test
public void testAddInvalidHabit()
{
onView(withId(R.id.action_add)).perform(click());
onView(withId(R.id.actionAdd)).perform(click());
typeHabitData("", "", "15", "7");

@ -26,6 +26,8 @@ import android.support.annotation.*;
import com.activeandroid.*;
import org.isoron.uhabits.notifications.*;
import org.isoron.uhabits.preferences.*;
import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.widgets.*;
@ -107,6 +109,16 @@ public class HabitsApplication extends Application
notificationTray = component.getNotificationTray();
notificationTray.startListening();
Preferences prefs = component.getPreferences();
prefs.initialize();
prefs.updateLastAppVersion();
TaskRunner taskRunner = component.getTaskRunner();
taskRunner.execute(() -> {
reminderScheduler.scheduleAll();
widgetUpdater.updateWidgets();
});
DatabaseUtils.initializeActiveAndroid();
}

@ -27,6 +27,8 @@ import android.view.*;
import org.isoron.uhabits.*;
import static android.R.anim.*;
/**
* Base class for all activities in the application.
* <p>
@ -75,6 +77,17 @@ abstract public class BaseActivity extends AppCompatActivity
return baseMenu.onItemSelected(item);
}
public void restartWithFade()
{
new Handler().postDelayed(() -> {
Intent intent = new Intent(this, MainActivity.class);
finish();
overridePendingTransition(fade_in, fade_out);
startActivity(intent);
}, 500); // HACK: Let the menu disappear first
}
public void setBaseMenu(@Nullable BaseMenu baseMenu)
{
this.baseMenu = baseMenu;

@ -66,6 +66,17 @@ public class ThemeSwitcher
return getTheme() == THEME_DARK;
}
public void refreshTheme()
{
}
public void toggleNightMode()
{
if (isNightMode()) setTheme(THEME_LIGHT);
else setTheme(THEME_DARK);
}
private void applyDarkTheme()
{
if (preferences.isPureBlackEnabled())

@ -19,7 +19,6 @@
package org.isoron.uhabits.activities.habits.list;
import android.os.*;
import android.support.annotation.*;
import org.isoron.uhabits.*;
@ -69,6 +68,8 @@ public class ListHabitsController
private ImportDataTaskFactory importTaskFactory;
private ExportCSVTaskFactory exportCSVFactory;
@Inject
public ListHabitsController(@NonNull BaseSystem system,
@NonNull CommandRunner commandRunner,
@ -79,7 +80,8 @@ public class ListHabitsController
@NonNull ReminderScheduler reminderScheduler,
@NonNull TaskRunner taskRunner,
@NonNull WidgetUpdater widgetUpdater,
@NonNull ImportDataTaskFactory importTaskFactory)
@NonNull ImportDataTaskFactory importTaskFactory,
@NonNull ExportCSVTaskFactory exportCSVFactory)
{
this.adapter = adapter;
this.commandRunner = commandRunner;
@ -91,6 +93,7 @@ public class ListHabitsController
this.reminderScheduler = reminderScheduler;
this.widgetUpdater = widgetUpdater;
this.importTaskFactory = importTaskFactory;
this.exportCSVFactory = exportCSVFactory;
}
public void onExportCSV()
@ -98,7 +101,7 @@ public class ListHabitsController
List<Habit> selected = new LinkedList<>();
for (Habit h : habitList) selected.add(h);
taskRunner.execute(new ExportCSVTask(habitList, selected, filename -> {
taskRunner.execute(exportCSVFactory.create(selected, filename -> {
if (filename != null) screen.showSendFileScreen(filename);
else screen.showMessage(R.string.could_not_export);
}));
@ -179,15 +182,8 @@ public class ListHabitsController
public void onStartup()
{
prefs.initialize();
prefs.incrementLaunchCount();
prefs.updateLastAppVersion();
if (prefs.isFirstRun()) onFirstRun();
new Handler().postDelayed(() -> taskRunner.execute(() -> {
reminderScheduler.scheduleAll();
widgetUpdater.updateWidgets();
}), 1000);
}
@Override

@ -67,10 +67,10 @@ public class ListHabitsMenu extends BaseMenu
@Override
public void onCreate(@NonNull Menu menu)
{
MenuItem nightModeItem = menu.findItem(R.id.action_night_mode);
MenuItem nightModeItem = menu.findItem(R.id.actionToggleNightMode);
nightModeItem.setChecked(themeSwitcher.isNightMode());
MenuItem showArchivedItem = menu.findItem(R.id.action_show_archived);
MenuItem showArchivedItem = menu.findItem(R.id.actionShowArchived);
showArchivedItem.setChecked(showArchived);
MenuItem showCompletedItem = menu.findItem(R.id.actionShowCompleted);
@ -82,27 +82,27 @@ public class ListHabitsMenu extends BaseMenu
{
switch (item.getItemId())
{
case R.id.action_night_mode:
case R.id.actionToggleNightMode:
screen.toggleNightMode();
return true;
case R.id.action_add:
case R.id.actionAdd:
screen.showCreateHabitScreen();
return true;
case R.id.action_faq:
case R.id.actionFAQ:
screen.showFAQScreen();
return true;
case R.id.action_about:
case R.id.actionAbout:
screen.showAboutScreen();
return true;
case R.id.action_settings:
case R.id.actionSettings:
screen.showSettingsScreen();
return true;
case R.id.action_show_archived:
case R.id.actionShowArchived:
toggleShowArchived();
invalidate();
return true;

@ -20,7 +20,6 @@
package org.isoron.uhabits.activities.habits.list;
import android.content.*;
import android.os.*;
import android.support.annotation.*;
import org.isoron.uhabits.*;
@ -233,23 +232,7 @@ public class ListHabitsScreen extends BaseScreen
public void toggleNightMode()
{
if (themeSwitcher.isNightMode())
themeSwitcher.setTheme(ThemeSwitcher.THEME_LIGHT);
else themeSwitcher.setTheme(ThemeSwitcher.THEME_DARK);
refreshTheme();
}
private void refreshTheme()
{
new Handler().postDelayed(() -> {
Intent intent = new Intent(activity, MainActivity.class);
activity.finish();
activity.overridePendingTransition(android.R.anim.fade_in,
android.R.anim.fade_out);
activity.startActivity(intent);
}, 500); // HACK: Let the menu disappear first
themeSwitcher.toggleNightMode();
activity.restartWithFade();
}
}

@ -23,6 +23,7 @@ import android.support.annotation.*;
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.notifications.*;
import javax.inject.*;
@ -32,10 +33,14 @@ public class WidgetController
@NonNull
private final CommandRunner commandRunner;
private NotificationTray notificationTray;
@Inject
public WidgetController(@NonNull CommandRunner commandRunner)
public WidgetController(@NonNull CommandRunner commandRunner,
@NonNull NotificationTray notificationTray)
{
this.commandRunner = commandRunner;
this.notificationTray = notificationTray;
}
public void onAddRepetition(@NonNull Habit habit, long timestamp)
@ -43,6 +48,7 @@ public class WidgetController
Repetition rep = habit.getRepetitions().getByTimestamp(timestamp);
if (rep != null) return;
performToggle(habit, timestamp);
notificationTray.cancel(habit);
}
public void onRemoveRepetition(@NonNull Habit habit, long timestamp)

@ -21,6 +21,8 @@ package org.isoron.uhabits.tasks;
import android.support.annotation.*;
import com.google.auto.factory.*;
import org.isoron.uhabits.io.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.utils.*;
@ -28,6 +30,7 @@ import org.isoron.uhabits.utils.*;
import java.io.*;
import java.util.*;
@AutoFactory(allowSubclasses = true)
public class ExportCSVTask implements Task
{
private String archiveFilename;
@ -41,7 +44,7 @@ public class ExportCSVTask implements Task
@NonNull
private final HabitList habitList;
public ExportCSVTask(@NonNull HabitList habitList,
public ExportCSVTask(@Provided @NonNull HabitList habitList,
@NonNull List<Habit> selectedHabits,
@NonNull Listener listener)
{

@ -27,7 +27,7 @@ import org.isoron.uhabits.io.*;
import java.io.*;
@AutoFactory
@AutoFactory(allowSubclasses = true)
public class ImportDataTask implements Task
{
public static final int FAILED = 3;

@ -23,7 +23,7 @@
tools:context=".MainActivity">
<item
android:id="@+id/action_add"
android:id="@+id/actionAdd"
android:icon="?iconAdd"
android:title="@string/add_habit"
app:showAsAction="ifRoom"/>
@ -35,7 +35,7 @@
app:showAsAction="ifRoom">
<menu>
<item
android:id="@+id/action_show_archived"
android:id="@+id/actionShowArchived"
android:checkable="true"
android:enabled="true"
android:title="@string/show_archived"/>
@ -49,7 +49,7 @@
</item>
<item
android:id="@+id/action_night_mode"
android:id="@+id/actionToggleNightMode"
android:checkable="true"
android:enabled="true"
android:orderInCategory="50"
@ -57,19 +57,19 @@
app:showAsAction="never"/>
<item
android:id="@+id/action_settings"
android:id="@+id/actionSettings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_faq"
android:id="@+id/actionFAQ"
android:orderInCategory="100"
android:title="@string/help"
app:showAsAction="never"/>
<item
android:id="@+id/action_about"
android:id="@+id/actionAbout"
android:orderInCategory="100"
android:title="@string/about"
app:showAsAction="never"/>

@ -0,0 +1,126 @@
/*
* 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.activities.habits.list;
import org.isoron.uhabits.*;
import org.isoron.uhabits.activities.*;
import org.isoron.uhabits.activities.habits.list.model.*;
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.preferences.*;
import org.isoron.uhabits.tasks.*;
import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.widgets.*;
import org.junit.*;
import static org.mockito.Mockito.*;
public class ListHabitsControllerTest extends BaseUnitTest
{
private ListHabitsController controller;
private ImportDataTaskFactory importTaskFactory;
private BaseSystem system;
private CommandRunner commandRunner;
private HabitCardListAdapter adapter;
private ListHabitsScreen screen;
private Preferences prefs;
private ReminderScheduler reminderScheduler;
private SingleThreadTaskRunner taskRunner;
private WidgetUpdater widgetUpdater;
private ExportCSVTaskFactory exportCSVFactory;
@Override
public void setUp()
{
super.setUp();
habitList = mock(HabitList.class);
system = mock(BaseSystem.class);
commandRunner = mock(CommandRunner.class);
adapter = mock(HabitCardListAdapter.class);
screen = mock(ListHabitsScreen.class);
prefs = mock(Preferences.class);
reminderScheduler = mock(ReminderScheduler.class);
taskRunner = new SingleThreadTaskRunner();
widgetUpdater = mock(WidgetUpdater.class);
importTaskFactory = mock(ImportDataTaskFactory.class);
exportCSVFactory = mock(ExportCSVTaskFactory.class);
controller =
spy(new ListHabitsController(system, commandRunner, habitList,
adapter, screen, prefs, reminderScheduler, taskRunner,
widgetUpdater, importTaskFactory, exportCSVFactory));
}
@Test
public void testOnHabitClick()
{
Habit h = mock(Habit.class);
controller.onHabitClick(h);
verify(screen).showHabitScreen(h);
}
@Test
public void testOnHabitReorder()
{
Habit from = mock(Habit.class);
Habit to = mock(Habit.class);
controller.onHabitReorder(from, to);
verify(habitList).reorder(from, to);
}
@Test
public void onInvalidToggle()
{
controller.onInvalidToggle();
verify(screen).showMessage(R.string.long_press_to_toggle);
}
@Test
public void onStartup_notFirstLaunch()
{
when(prefs.isFirstRun()).thenReturn(false);
controller.onStartup();
verify(prefs).incrementLaunchCount();
}
@Test
public void onStartup_firstLaunch()
{
long today = DateUtils.getStartOfToday();
when(prefs.isFirstRun()).thenReturn(true);
controller.onStartup();
verify(prefs).setFirstRun(false);
verify(prefs).updateLastHint(-1, today);
verify(screen).showIntroScreen();
}
}

@ -0,0 +1,174 @@
/*
* 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.activities.habits.list;
import android.view.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.activities.*;
import org.isoron.uhabits.activities.habits.list.model.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.preferences.*;
import org.junit.*;
import org.mockito.*;
import static junit.framework.Assert.*;
import static org.mockito.Mockito.*;
public class ListHabitsMenuTest extends BaseUnitTest
{
private BaseActivity activity;
private ListHabitsScreen screen;
private HabitCardListAdapter adapter;
private Preferences preferences;
private ThemeSwitcher themeSwitcher;
private ListHabitsMenu menu;
private ArgumentCaptor<HabitMatcher> matcherCaptor;
@Override
public void setUp()
{
super.setUp();
activity = mock(BaseActivity.class);
screen = mock(ListHabitsScreen.class);
adapter = mock(HabitCardListAdapter.class);
preferences = mock(Preferences.class);
themeSwitcher = mock(ThemeSwitcher.class);
when(preferences.getShowArchived()).thenReturn(false);
when(preferences.getShowCompleted()).thenReturn(false);
when(themeSwitcher.isNightMode()).thenReturn(false);
menu = new ListHabitsMenu(activity, screen, adapter, preferences,
themeSwitcher);
matcherCaptor = ArgumentCaptor.forClass(HabitMatcher.class);
reset(adapter);
}
@Test
public void testOnCreate()
{
MenuItem nightModeItem = mock(MenuItem.class);
MenuItem showArchivedItem = mock(MenuItem.class);
MenuItem showCompletedItem = mock(MenuItem.class);
Menu androidMenu = mock(Menu.class);
when(androidMenu.findItem(R.id.actionToggleNightMode)).thenReturn(
nightModeItem);
when(androidMenu.findItem(R.id.actionShowArchived)).thenReturn
(showArchivedItem);
when(androidMenu.findItem(R.id.actionShowCompleted)).thenReturn
(showCompletedItem);
menu.onCreate(androidMenu);
verify(nightModeItem).setChecked(false);
verify(showArchivedItem).setChecked(false);
verify(showCompletedItem).setChecked(false);
reset(nightModeItem, showArchivedItem, showCompletedItem);
when(themeSwitcher.isNightMode()).thenReturn(true);
menu.onCreate(androidMenu);
verify(nightModeItem).setChecked(true);
}
@Test
public void testOnSelected_about()
{
onItemSelected(R.id.actionAbout);
verify(screen).showAboutScreen();
}
@Test
public void testOnSelected_add()
{
onItemSelected(R.id.actionAdd);
verify(screen).showCreateHabitScreen();
}
@Test
public void testOnSelected_faq()
{
onItemSelected(R.id.actionFAQ);
verify(screen).showFAQScreen();
}
@Test
public void testOnSelected_nightMode()
{
onItemSelected(R.id.actionToggleNightMode);
verify(screen).toggleNightMode();
}
@Test
public void testOnSelected_settings()
{
onItemSelected(R.id.actionSettings);
verify(screen).showSettingsScreen();
}
@Test
public void testOnSelected_showArchived()
{
onItemSelected(R.id.actionShowArchived);
verify(preferences).setShowArchived(true);
verify(adapter).setFilter(matcherCaptor.capture());
verify(adapter).refresh();
assertTrue(matcherCaptor.getValue().isArchivedAllowed());
reset(adapter);
onItemSelected(R.id.actionShowArchived);
verify(preferences).setShowArchived(false);
verify(adapter).setFilter(matcherCaptor.capture());
verify(adapter).refresh();
assertFalse(matcherCaptor.getValue().isArchivedAllowed());
}
@Test
public void testOnSelected_showCompleted()
{
onItemSelected(R.id.actionShowCompleted);
verify(preferences).setShowCompleted(true);
verify(adapter).setFilter(matcherCaptor.capture());
verify(adapter).refresh();
assertTrue(matcherCaptor.getValue().isCompletedAllowed());
reset(adapter);
onItemSelected(R.id.actionShowCompleted);
verify(preferences).setShowCompleted(false);
verify(adapter).setFilter(matcherCaptor.capture());
verify(adapter).refresh();
assertFalse(matcherCaptor.getValue().isCompletedAllowed());
}
protected void onItemSelected(int actionId)
{
MenuItem item = mock(MenuItem.class);
when(item.getItemId()).thenReturn(actionId);
menu.onItemSelected(item);
}
}

@ -76,6 +76,8 @@ public class ListHabitsScreenTest extends BaseUnitTest
private ThemeSwitcher themeSwitcher;
private ListHabitsScreen baseScreen;
@Before
@Override
public void setUp()
@ -94,11 +96,12 @@ public class ListHabitsScreenTest extends BaseUnitTest
colorPickerDialogFactory = mock(ColorPickerDialogFactory.class);
editHabitDialogFactory = mock(EditHabitDialogFactory.class);
screen =
new ListHabitsScreen(activity, commandRunner, dirFinder, rootView,
intentFactory, themeSwitcher, confirmDeleteDialogFactory,
createHabitDialogFactory, filePickerDialogFactory,
colorPickerDialogFactory, editHabitDialogFactory);
screen = spy(new ListHabitsScreen(activity, commandRunner, dirFinder,
rootView, intentFactory, themeSwitcher, confirmDeleteDialogFactory,
createHabitDialogFactory, filePickerDialogFactory,
colorPickerDialogFactory, editHabitDialogFactory));
doNothing().when(screen).showMessage(anyInt());
controller = mock(ListHabitsController.class);
screen.setController(controller);
@ -139,12 +142,12 @@ public class ListHabitsScreenTest extends BaseUnitTest
verify(controller).onExportDB();
}
// @Test
// public void testOnResult_importData()
// {
// screen.onResult(0, ListHabitsScreen.RESULT_IMPORT_DATA, null);
// testShowImportScreen();
// }
@Test
public void testOnResult_importData()
{
screen.onResult(0, ListHabitsScreen.RESULT_IMPORT_DATA, null);
testShowImportScreen();
}
@Test
public void testShowAboutScreen() throws Exception
@ -225,12 +228,13 @@ public class ListHabitsScreenTest extends BaseUnitTest
verify(activity).showDialog(dialog);
}
// @Test
// public void testShowImportScreen_withInvalidPath()
// {
// when(dirFinder.findStorageDir(any())).thenReturn(null);
// screen.showImportScreen();
// }
@Test
public void testShowImportScreen_withInvalidPath()
{
when(dirFinder.findStorageDir(any())).thenReturn(null);
screen.showImportScreen();
verify(screen).showMessage(R.string.could_not_import);
}
@Test
public void testShowIntroScreen()
@ -247,4 +251,35 @@ public class ListHabitsScreenTest extends BaseUnitTest
screen.showSettingsScreen();
verify(activity).startActivityForResult(eq(intent), anyInt());
}
@Test
public void testToggleNightMode()
{
screen.toggleNightMode();
verify(themeSwitcher).toggleNightMode();
verify(activity).restartWithFade();
}
@Test
public void testOnAttached()
{
screen.onAttached();
verify(commandRunner).addListener(screen);
}
@Test
public void testOnDetach()
{
screen.onDettached();
verify(commandRunner).removeListener(screen);
}
@Test
public void testOnCommand()
{
Command c = mock(Command.class);
when(c.getExecuteStringId()).thenReturn(R.string.toast_habit_deleted);
screen.onCommandExecuted(c, null);
verify(screen).showMessage(R.string.toast_habit_deleted);
}
}

@ -22,6 +22,7 @@ package org.isoron.uhabits.receivers;
import org.isoron.uhabits.*;
import org.isoron.uhabits.commands.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.notifications.*;
import org.isoron.uhabits.utils.*;
import org.junit.*;
@ -40,6 +41,8 @@ public class WidgetControllerTest extends BaseUnitTest
private long today;
private NotificationTray notificationTray;
@Override
public void setUp()
{
@ -48,7 +51,8 @@ public class WidgetControllerTest extends BaseUnitTest
today = DateUtils.getStartOfToday();
habit = fixtures.createEmptyHabit();
commandRunner = mock(CommandRunner.class);
controller = new WidgetController(commandRunner);
notificationTray = mock(NotificationTray.class);
controller = new WidgetController(commandRunner, notificationTray);
}
@Test
@ -68,6 +72,7 @@ public class WidgetControllerTest extends BaseUnitTest
assertThat(todayValue, equalTo(UNCHECKED));
controller.onAddRepetition(habit, today);
verify(commandRunner).execute(any(), anyLong());
verify(notificationTray).cancel(habit);
}
@Test

Loading…
Cancel
Save