mirror of https://github.com/iSoron/uhabits.git
parent
95385fa8f4
commit
3e558be4d4
@ -1,129 +0,0 @@
|
||||
/*
|
||||
* 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.activities.habits.list;
|
||||
|
||||
import org.isoron.androidbase.*;
|
||||
import org.isoron.uhabits.*;
|
||||
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.tasks.android.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
import org.isoron.uhabits.widgets.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class ListHabitsControllerTest extends BaseAndroidTest
|
||||
{
|
||||
private ListHabitsController controller;
|
||||
|
||||
private ImportDataTaskFactory importTaskFactory;
|
||||
|
||||
private BaseSystem system;
|
||||
|
||||
private CommandRunner commandRunner;
|
||||
|
||||
private HabitCardListAdapter adapter;
|
||||
|
||||
private ListHabitsScreen screen;
|
||||
|
||||
private AndroidPreferences prefs;
|
||||
|
||||
private ReminderScheduler reminderScheduler;
|
||||
|
||||
private SingleThreadTaskRunner taskRunner;
|
||||
|
||||
private WidgetUpdater widgetUpdater;
|
||||
|
||||
private ExportCSVTaskFactory exportCSVFactory;
|
||||
|
||||
private ExportDBTaskFactory exportDBFactory;
|
||||
|
||||
@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(AndroidPreferences.class);
|
||||
reminderScheduler = mock(ReminderScheduler.class);
|
||||
taskRunner = new SingleThreadTaskRunner();
|
||||
widgetUpdater = mock(WidgetUpdater.class);
|
||||
importTaskFactory = mock(ImportDataTaskFactory.class);
|
||||
exportCSVFactory = mock(ExportCSVTaskFactory.class);
|
||||
exportDBFactory = mock(ExportDBTaskFactory.class);
|
||||
|
||||
controller =
|
||||
spy(new ListHabitsController(system, commandRunner, habitList,
|
||||
adapter, screen, prefs, reminderScheduler, taskRunner,
|
||||
widgetUpdater, importTaskFactory, exportCSVFactory, exportDBFactory));
|
||||
}
|
||||
|
||||
@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,35 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.isoron.uhabits.preferences.*;
|
||||
|
||||
import dagger.*;
|
||||
|
||||
@Module
|
||||
public class HabitsModule
|
||||
{
|
||||
@Provides
|
||||
@AppScope
|
||||
public static Preferences getPreferences(AndroidPreferences preferences)
|
||||
{
|
||||
return preferences;
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.activities.habits.list;
|
||||
|
||||
|
||||
import org.isoron.androidbase.*;
|
||||
import org.isoron.androidbase.activities.*;
|
||||
import org.isoron.uhabits.ui.habits.list.*;
|
||||
|
||||
import dagger.*;
|
||||
|
||||
@Module
|
||||
public class ListHabitsModule extends ActivityModule
|
||||
{
|
||||
public ListHabitsModule(BaseActivity activity)
|
||||
{
|
||||
super(activity);
|
||||
}
|
||||
|
||||
@Provides
|
||||
ListHabitsBehavior.Screen getScreen(ListHabitsScreen screen)
|
||||
{
|
||||
return screen;
|
||||
}
|
||||
|
||||
@Provides
|
||||
ListHabitsBehavior.System getSystem(BaseSystem system)
|
||||
{
|
||||
return system;
|
||||
}
|
||||
}
|
@ -0,0 +1,220 @@
|
||||
/*
|
||||
* 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.habits.list;
|
||||
|
||||
import android.support.annotation.*;
|
||||
|
||||
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 java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
public class ListHabitsBehavior
|
||||
{
|
||||
private HabitList habitList;
|
||||
|
||||
private System system;
|
||||
|
||||
private TaskRunner taskRunner;
|
||||
|
||||
private Screen screen;
|
||||
|
||||
private CommandRunner commandRunner;
|
||||
|
||||
private Preferences prefs;
|
||||
|
||||
@Inject
|
||||
public ListHabitsBehavior(@NonNull HabitList habitList,
|
||||
@NonNull System system,
|
||||
@NonNull TaskRunner taskRunner,
|
||||
@NonNull Screen screen,
|
||||
@NonNull CommandRunner commandRunner,
|
||||
@NonNull Preferences prefs)
|
||||
{
|
||||
this.habitList = habitList;
|
||||
this.system = system;
|
||||
this.taskRunner = taskRunner;
|
||||
this.screen = screen;
|
||||
this.commandRunner = commandRunner;
|
||||
this.prefs = prefs;
|
||||
}
|
||||
|
||||
public void onClickHabit(@NonNull Habit h)
|
||||
{
|
||||
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();
|
||||
double oldValue = checkmarks.getValues(timestamp, timestamp)[0];
|
||||
|
||||
screen.showNumberPicker(oldValue / 1000, habit.getUnit(), newValue ->
|
||||
{
|
||||
newValue = Math.round(newValue * 1000);
|
||||
commandRunner.execute(
|
||||
new CreateRepetitionCommand(habit, timestamp, (int) newValue),
|
||||
habit.getId());
|
||||
});
|
||||
}
|
||||
|
||||
public void onExportCSV()
|
||||
{
|
||||
List<Habit> selected = new LinkedList<>();
|
||||
for (Habit h : habitList) selected.add(h);
|
||||
File outputDir = system.getCSVOutputDir();
|
||||
|
||||
taskRunner.execute(
|
||||
new ExportCSVTask(habitList, selected, outputDir, filename ->
|
||||
{
|
||||
if (filename != null) screen.showSendFileScreen(filename);
|
||||
else screen.showMessage(Message.COULD_NOT_EXPORT);
|
||||
}));
|
||||
}
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
public void onRepairDB()
|
||||
{
|
||||
taskRunner.execute(() ->
|
||||
{
|
||||
habitList.repair();
|
||||
screen.showMessage(Message.DATABASE_REPAIRED);
|
||||
});
|
||||
}
|
||||
|
||||
public void onSendBugReport()
|
||||
{
|
||||
system.dumpBugReportToFile();
|
||||
|
||||
try
|
||||
{
|
||||
String log = system.getBugReport();
|
||||
screen.showSendBugReportToDeveloperScreen(log);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
screen.showMessage(Message.COULD_NOT_GENERATE_BUG_REPORT);
|
||||
}
|
||||
}
|
||||
|
||||
public void onStartup()
|
||||
{
|
||||
prefs.incrementLaunchCount();
|
||||
if (prefs.isFirstRun()) onFirstRun();
|
||||
}
|
||||
|
||||
public void onToggle(@NonNull Habit habit, long timestamp)
|
||||
{
|
||||
commandRunner.execute(new ToggleRepetitionCommand(habit, timestamp),
|
||||
habit.getId());
|
||||
}
|
||||
|
||||
public void onFirstRun()
|
||||
{
|
||||
prefs.setFirstRun(false);
|
||||
prefs.updateLastHint(-1, DateUtils.getStartOfToday());
|
||||
screen.showIntroScreen();
|
||||
}
|
||||
|
||||
public enum Message
|
||||
{
|
||||
COULD_NOT_EXPORT, IMPORT_SUCCESSFUL, IMPORT_FAILED, DATABASE_REPAIRED,
|
||||
COULD_NOT_GENERATE_BUG_REPORT, FILE_NOT_RECOGNIZED
|
||||
}
|
||||
|
||||
public interface NumberPickerCallback
|
||||
{
|
||||
void onNumberPicked(double newValue);
|
||||
}
|
||||
|
||||
public interface OnFinishedListener
|
||||
{
|
||||
void onFinish();
|
||||
}
|
||||
|
||||
public interface Screen
|
||||
{
|
||||
void showHabitScreen(@NonNull Habit h);
|
||||
|
||||
void showIntroScreen();
|
||||
|
||||
void showMessage(@NonNull Message m);
|
||||
|
||||
void showNumberPicker(double value,
|
||||
@NonNull String unit,
|
||||
@NonNull NumberPickerCallback callback);
|
||||
|
||||
void showSendBugReportToDeveloperScreen(String log);
|
||||
|
||||
void showSendFileScreen(@NonNull String filename);
|
||||
}
|
||||
|
||||
public interface System
|
||||
{
|
||||
void dumpBugReportToFile();
|
||||
|
||||
String getBugReport() throws IOException;
|
||||
|
||||
File getCSVOutputDir();
|
||||
}
|
||||
}
|
@ -0,0 +1,132 @@
|
||||
/*
|
||||
* 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.habits.list;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
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.*;
|
||||
import static org.isoron.uhabits.ui.habits.list.ListHabitsBehavior.Message.*;
|
||||
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
|
||||
private ListHabitsBehavior.System system;
|
||||
|
||||
@Mock
|
||||
private Preferences prefs;
|
||||
|
||||
private ListHabitsBehavior behavior;
|
||||
|
||||
@Mock
|
||||
private ListHabitsBehavior.Screen screen;
|
||||
|
||||
private Habit habit1, habit2;
|
||||
|
||||
@Captor
|
||||
ArgumentCaptor<ListHabitsBehavior.NumberPickerCallback> captor;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
super.setUp();
|
||||
habit1 = fixtures.createShortHabit();
|
||||
habit2 = fixtures.createNumericalHabit();
|
||||
habitList.add(habit1);
|
||||
habitList.add(habit2);
|
||||
clearInvocations(habitList);
|
||||
|
||||
behavior = new ListHabitsBehavior(habitList, system, taskRunner, screen,
|
||||
commandRunner, prefs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnEdit()
|
||||
{
|
||||
behavior.onEdit(habit2, DateUtils.getStartOfToday());
|
||||
verify(screen).showNumberPicker(eq(0.1), eq("miles"), captor.capture());
|
||||
captor.getValue().onNumberPicked(100);
|
||||
assertThat(habit2.getCheckmarks().getTodayValue(), equalTo(100000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnHabitClick()
|
||||
{
|
||||
behavior.onClickHabit(habit1);
|
||||
verify(screen).showHabitScreen(habit1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnHabitReorder()
|
||||
{
|
||||
Habit from = habit1;
|
||||
Habit to = habit2;
|
||||
behavior.onReorderHabit(from, to);
|
||||
verify(habitList).reorder(from, to);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnRepairDB()
|
||||
{
|
||||
behavior.onRepairDB();
|
||||
verify(habitList).repair();
|
||||
verify(screen).showMessage(DATABASE_REPAIRED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnStartup_firstLaunch()
|
||||
{
|
||||
long today = DateUtils.getStartOfToday();
|
||||
|
||||
when(prefs.isFirstRun()).thenReturn(true);
|
||||
behavior.onStartup();
|
||||
verify(prefs).setFirstRun(false);
|
||||
verify(prefs).updateLastHint(-1, today);
|
||||
verify(screen).showIntroScreen();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnStartup_notFirstLaunch()
|
||||
{
|
||||
when(prefs.isFirstRun()).thenReturn(false);
|
||||
behavior.onStartup();
|
||||
verify(prefs).incrementLaunchCount();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnToggle()
|
||||
{
|
||||
assertTrue(habit1.isCompletedToday());
|
||||
behavior.onToggle(habit1, DateUtils.getStartOfToday());
|
||||
assertFalse(habit1.isCompletedToday());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue