mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Create android-base module
This commit is contained in:
@@ -34,32 +34,43 @@ import javax.inject.*;
|
||||
|
||||
public class ListHabitsBehavior
|
||||
{
|
||||
private HabitList habitList;
|
||||
@NonNull
|
||||
private final HabitList habitList;
|
||||
|
||||
private System system;
|
||||
@NonNull
|
||||
private final DirFinder dirFinder;
|
||||
|
||||
private TaskRunner taskRunner;
|
||||
@NonNull
|
||||
private final TaskRunner taskRunner;
|
||||
|
||||
private Screen screen;
|
||||
@NonNull
|
||||
private final Screen screen;
|
||||
|
||||
private CommandRunner commandRunner;
|
||||
@NonNull
|
||||
private final CommandRunner commandRunner;
|
||||
|
||||
private Preferences prefs;
|
||||
@NonNull
|
||||
private final Preferences prefs;
|
||||
|
||||
@NonNull
|
||||
private final BugReporter bugReporter;
|
||||
|
||||
@Inject
|
||||
public ListHabitsBehavior(@NonNull HabitList habitList,
|
||||
@NonNull System system,
|
||||
@NonNull DirFinder dirFinder,
|
||||
@NonNull TaskRunner taskRunner,
|
||||
@NonNull Screen screen,
|
||||
@NonNull CommandRunner commandRunner,
|
||||
@NonNull Preferences prefs)
|
||||
@NonNull Preferences prefs,
|
||||
@NonNull BugReporter bugReporter)
|
||||
{
|
||||
this.habitList = habitList;
|
||||
this.system = system;
|
||||
this.dirFinder = dirFinder;
|
||||
this.taskRunner = taskRunner;
|
||||
this.screen = screen;
|
||||
this.commandRunner = commandRunner;
|
||||
this.prefs = prefs;
|
||||
this.bugReporter = bugReporter;
|
||||
}
|
||||
|
||||
public void onClickHabit(@NonNull Habit h)
|
||||
@@ -85,7 +96,7 @@ public class ListHabitsBehavior
|
||||
{
|
||||
List<Habit> selected = new LinkedList<>();
|
||||
for (Habit h : habitList) selected.add(h);
|
||||
File outputDir = system.getCSVOutputDir();
|
||||
File outputDir = dirFinder.getCSVOutputDir();
|
||||
|
||||
taskRunner.execute(
|
||||
new ExportCSVTask(habitList, selected, outputDir, filename ->
|
||||
@@ -95,6 +106,13 @@ public class ListHabitsBehavior
|
||||
}));
|
||||
}
|
||||
|
||||
public void onFirstRun()
|
||||
{
|
||||
prefs.setFirstRun(false);
|
||||
prefs.updateLastHint(-1, DateUtils.getStartOfToday());
|
||||
screen.showIntroScreen();
|
||||
}
|
||||
|
||||
public void onReorderHabit(@NonNull Habit from, @NonNull Habit to)
|
||||
{
|
||||
taskRunner.execute(() -> habitList.reorder(from, to));
|
||||
@@ -111,11 +129,11 @@ public class ListHabitsBehavior
|
||||
|
||||
public void onSendBugReport()
|
||||
{
|
||||
system.dumpBugReportToFile();
|
||||
bugReporter.dumpBugReportToFile();
|
||||
|
||||
try
|
||||
{
|
||||
String log = system.getBugReport();
|
||||
String log = bugReporter.getBugReport();
|
||||
screen.showSendBugReportToDeveloperScreen(log);
|
||||
}
|
||||
catch (IOException e)
|
||||
@@ -137,19 +155,19 @@ public class ListHabitsBehavior
|
||||
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 BugReporter
|
||||
{
|
||||
void dumpBugReportToFile();
|
||||
|
||||
String getBugReport() throws IOException;
|
||||
}
|
||||
|
||||
public interface NumberPickerCallback
|
||||
{
|
||||
void onNumberPicked(double newValue);
|
||||
@@ -172,12 +190,8 @@ public class ListHabitsBehavior
|
||||
void showSendFileScreen(@NonNull String filename);
|
||||
}
|
||||
|
||||
public interface System
|
||||
public interface DirFinder
|
||||
{
|
||||
void dumpBugReportToFile();
|
||||
|
||||
String getBugReport() throws IOException;
|
||||
|
||||
File getCSVOutputDir();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,20 +28,26 @@ import org.mockito.*;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static java.nio.file.Files.*;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.apache.commons.io.FileUtils.*;
|
||||
import static java.nio.file.Files.createTempDirectory;
|
||||
import static org.apache.commons.io.FileUtils.deleteDirectory;
|
||||
import static org.apache.commons.io.FileUtils.listFiles;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.COULD_NOT_EXPORT;
|
||||
import static org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.COULD_NOT_GENERATE_BUG_REPORT;
|
||||
import static org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.DATABASE_REPAIRED;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ListHabitsBehaviorTest extends BaseUnitTest
|
||||
{
|
||||
@Mock
|
||||
private ListHabitsBehavior.System system;
|
||||
private ListHabitsBehavior.DirFinder dirFinder;
|
||||
|
||||
@Mock
|
||||
private Preferences prefs;
|
||||
@@ -56,6 +62,9 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
|
||||
@Captor
|
||||
ArgumentCaptor<ListHabitsBehavior.NumberPickerCallback> picker;
|
||||
|
||||
@Mock
|
||||
private ListHabitsBehavior.BugReporter bugReporter;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp()
|
||||
@@ -67,8 +76,8 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
|
||||
habitList.add(habit2);
|
||||
clearInvocations(habitList);
|
||||
|
||||
behavior = new ListHabitsBehavior(habitList, system, taskRunner, screen,
|
||||
commandRunner, prefs);
|
||||
behavior = new ListHabitsBehavior(habitList, dirFinder, taskRunner, screen,
|
||||
commandRunner, prefs, bugReporter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -84,7 +93,7 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
|
||||
public void testOnExportCSV() throws Exception
|
||||
{
|
||||
File outputDir = createTempDirectory("CSV").toFile();
|
||||
when(system.getCSVOutputDir()).thenReturn(outputDir);
|
||||
when(dirFinder.getCSVOutputDir()).thenReturn(outputDir);
|
||||
behavior.onExportCSV();
|
||||
verify(screen).showSendFileScreen(any());
|
||||
assertThat(listFiles(outputDir, null, false).size(), equalTo(1));
|
||||
@@ -96,7 +105,7 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
|
||||
{
|
||||
File outputDir = createTempDirectory("CSV").toFile();
|
||||
outputDir.setWritable(false);
|
||||
when(system.getCSVOutputDir()).thenReturn(outputDir);
|
||||
when(dirFinder.getCSVOutputDir()).thenReturn(outputDir);
|
||||
behavior.onExportCSV();
|
||||
verify(screen).showMessage(COULD_NOT_EXPORT);
|
||||
assertTrue(outputDir.delete());
|
||||
@@ -126,6 +135,20 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
|
||||
verify(screen).showMessage(DATABASE_REPAIRED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnSendBugReport() throws IOException
|
||||
{
|
||||
when(bugReporter.getBugReport()).thenReturn("hello");
|
||||
behavior.onSendBugReport();
|
||||
verify(bugReporter).dumpBugReportToFile();
|
||||
verify(screen).showSendBugReportToDeveloperScreen("hello");
|
||||
|
||||
when(bugReporter.getBugReport()).thenThrow(new IOException());
|
||||
behavior.onSendBugReport();
|
||||
verify(screen).showMessage(COULD_NOT_GENERATE_BUG_REPORT);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnStartup_firstLaunch()
|
||||
{
|
||||
@@ -154,17 +177,4 @@ public class ListHabitsBehaviorTest extends BaseUnitTest
|
||||
assertFalse(habit1.isCompletedToday());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnSendBugReport() throws IOException
|
||||
{
|
||||
when(system.getBugReport()).thenReturn("hello");
|
||||
behavior.onSendBugReport();
|
||||
verify(screen).showSendBugReportToDeveloperScreen("hello");
|
||||
|
||||
when(system.getBugReport()).thenThrow(new IOException());
|
||||
behavior.onSendBugReport();
|
||||
verify(screen).showMessage(COULD_NOT_GENERATE_BUG_REPORT);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user