mirror of https://github.com/iSoron/uhabits.git
parent
7b8ab6a625
commit
fc2087fe68
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue