From 0f828cbd3a92edad91caf6ef366f16550f07993e Mon Sep 17 00:00:00 2001 From: Quentin Hibon Date: Thu, 14 Jan 2021 22:07:19 +0100 Subject: [PATCH] Convert uhabits-android (test) --- .../isoron/uhabits/BaseAndroidJVMTest.java | 77 ------------------ .../org/isoron/uhabits/BaseAndroidJVMTest.kt | 64 +++++++++++++++ .../receivers/ReminderControllerTest.java | 79 ------------------- .../receivers/ReminderControllerTest.kt | 70 ++++++++++++++++ 4 files changed, 134 insertions(+), 156 deletions(-) delete mode 100644 uhabits-android/src/test/java/org/isoron/uhabits/BaseAndroidJVMTest.java create mode 100644 uhabits-android/src/test/java/org/isoron/uhabits/BaseAndroidJVMTest.kt delete mode 100644 uhabits-android/src/test/java/org/isoron/uhabits/receivers/ReminderControllerTest.java create mode 100644 uhabits-android/src/test/java/org/isoron/uhabits/receivers/ReminderControllerTest.kt diff --git a/uhabits-android/src/test/java/org/isoron/uhabits/BaseAndroidJVMTest.java b/uhabits-android/src/test/java/org/isoron/uhabits/BaseAndroidJVMTest.java deleted file mode 100644 index ac41f26a3..000000000 --- a/uhabits-android/src/test/java/org/isoron/uhabits/BaseAndroidJVMTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2016-2021 Álinson Santos Xavier - * - * 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 . - */ - -package org.isoron.uhabits; - -import org.isoron.uhabits.core.commands.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.core.models.memory.*; -import org.isoron.uhabits.core.tasks.*; -import org.isoron.uhabits.core.test.*; -import org.isoron.uhabits.core.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 BaseAndroidJVMTest -{ - protected HabitList habitList; - protected HabitFixtures fixtures; - protected MemoryModelFactory modelFactory; - protected SingleThreadTaskRunner taskRunner; - protected CommandRunner commandRunner; - - @Before - public void setUp() - { - long fixed_local_time = 1422172800000L; - DateUtils.setFixedLocalTime(fixed_local_time); - DateUtils.setStartDayOffset(0, 0); - modelFactory = new MemoryModelFactory(); - habitList = spy(modelFactory.buildHabitList()); - fixtures = new HabitFixtures(modelFactory, habitList); - taskRunner = new SingleThreadTaskRunner(); - commandRunner = new CommandRunner(taskRunner); - } - - @After - public void tearDown() - { - DateUtils.setFixedLocalTime(null); - DateUtils.setStartDayOffset(0, 0); - } - - public long timestamp(int year, int month, int day) - { - GregorianCalendar cal = DateUtils.getStartOfTodayCalendar(); - cal.set(year, month, day); - return cal.getTimeInMillis(); - } - - @Test - public void nothing() - { - - } -} diff --git a/uhabits-android/src/test/java/org/isoron/uhabits/BaseAndroidJVMTest.kt b/uhabits-android/src/test/java/org/isoron/uhabits/BaseAndroidJVMTest.kt new file mode 100644 index 000000000..41e7cf570 --- /dev/null +++ b/uhabits-android/src/test/java/org/isoron/uhabits/BaseAndroidJVMTest.kt @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * 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 . + */ +package org.isoron.uhabits + +import org.isoron.uhabits.core.commands.CommandRunner +import org.isoron.uhabits.core.models.HabitList +import org.isoron.uhabits.core.models.memory.MemoryModelFactory +import org.isoron.uhabits.core.tasks.SingleThreadTaskRunner +import org.isoron.uhabits.core.test.HabitFixtures +import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedLocalTime +import org.isoron.uhabits.core.utils.DateUtils.Companion.setStartDayOffset +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito +import org.mockito.junit.MockitoJUnitRunner + +@RunWith(MockitoJUnitRunner::class) +open class BaseAndroidJVMTest { + private lateinit var habitList: HabitList + protected lateinit var fixtures: HabitFixtures + private lateinit var modelFactory: MemoryModelFactory + private lateinit var taskRunner: SingleThreadTaskRunner + private lateinit var commandRunner: CommandRunner + + @Before + open fun setUp() { + val fixedLocalTime = 1422172800000L + setFixedLocalTime(fixedLocalTime) + setStartDayOffset(0, 0) + modelFactory = MemoryModelFactory() + habitList = Mockito.spy(modelFactory.buildHabitList()) + fixtures = HabitFixtures(modelFactory, habitList) + taskRunner = SingleThreadTaskRunner() + commandRunner = CommandRunner(taskRunner) + } + + @After + fun tearDown() { + setFixedLocalTime(null) + setStartDayOffset(0, 0) + } + + @Test + fun nothing() { + } +} diff --git a/uhabits-android/src/test/java/org/isoron/uhabits/receivers/ReminderControllerTest.java b/uhabits-android/src/test/java/org/isoron/uhabits/receivers/ReminderControllerTest.java deleted file mode 100644 index f7a384392..000000000 --- a/uhabits-android/src/test/java/org/isoron/uhabits/receivers/ReminderControllerTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2016-2021 Álinson Santos Xavier - * - * 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 . - */ - -package org.isoron.uhabits.receivers; - -import org.isoron.uhabits.*; -import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.core.preferences.*; -import org.isoron.uhabits.core.reminders.*; -import org.isoron.uhabits.core.ui.*; -import org.isoron.uhabits.core.utils.*; -import org.junit.*; - -import static org.mockito.Mockito.*; - -public class ReminderControllerTest extends BaseAndroidJVMTest -{ - - private ReminderController controller; - - private ReminderScheduler reminderScheduler; - - private NotificationTray notificationTray; - - private Preferences preferences; - - @Override - public void setUp() - { - super.setUp(); - - reminderScheduler = mock(ReminderScheduler.class); - notificationTray = mock(NotificationTray.class); - preferences = mock(Preferences.class); - - controller = new ReminderController(reminderScheduler, - notificationTray, preferences); - } - - @Test - public void testOnDismiss() throws Exception - { - verifyNoMoreInteractions(reminderScheduler); - verifyNoMoreInteractions(notificationTray); - verifyNoMoreInteractions(preferences); - } - - @Test - public void testOnShowReminder() throws Exception - { - Habit habit = mock(Habit.class); - controller.onShowReminder(habit, Timestamp.ZERO.plus(100), 456); - verify(notificationTray).show(habit, Timestamp.ZERO.plus(100), 456); - verify(reminderScheduler).scheduleAll(); - } - - @Test - public void testOnBootCompleted() throws Exception - { - controller.onBootCompleted(); - verify(reminderScheduler).scheduleAll(); - } -} \ No newline at end of file diff --git a/uhabits-android/src/test/java/org/isoron/uhabits/receivers/ReminderControllerTest.kt b/uhabits-android/src/test/java/org/isoron/uhabits/receivers/ReminderControllerTest.kt new file mode 100644 index 000000000..df37441b9 --- /dev/null +++ b/uhabits-android/src/test/java/org/isoron/uhabits/receivers/ReminderControllerTest.kt @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * 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 . + */ +package org.isoron.uhabits.receivers + +import org.isoron.uhabits.BaseAndroidJVMTest +import org.isoron.uhabits.core.models.Habit +import org.isoron.uhabits.core.models.Timestamp +import org.isoron.uhabits.core.preferences.Preferences +import org.isoron.uhabits.core.reminders.ReminderScheduler +import org.isoron.uhabits.core.ui.NotificationTray +import org.junit.Test +import org.mockito.Mockito + +class ReminderControllerTest : BaseAndroidJVMTest() { + private lateinit var controller: ReminderController + private lateinit var reminderScheduler: ReminderScheduler + private lateinit var notificationTray: NotificationTray + private lateinit var preferences: Preferences + override fun setUp() { + super.setUp() + reminderScheduler = Mockito.mock(ReminderScheduler::class.java) + notificationTray = Mockito.mock(NotificationTray::class.java) + preferences = Mockito.mock(Preferences::class.java) + controller = ReminderController( + reminderScheduler, + notificationTray, + preferences + ) + } + + @Test + @Throws(Exception::class) + fun testOnDismiss() { + Mockito.verifyNoMoreInteractions(reminderScheduler) + Mockito.verifyNoMoreInteractions(notificationTray) + Mockito.verifyNoMoreInteractions(preferences) + } + + @Test + @Throws(Exception::class) + fun testOnShowReminder() { + val habit = Mockito.mock(Habit::class.java) + controller.onShowReminder(habit, Timestamp.ZERO.plus(100), 456) + Mockito.verify(notificationTray).show(habit, Timestamp.ZERO.plus(100), 456) + Mockito.verify(reminderScheduler).scheduleAll() + } + + @Test + @Throws(Exception::class) + fun testOnBootCompleted() { + controller.onBootCompleted() + Mockito.verify(reminderScheduler).scheduleAll() + } +}