diff --git a/app/src/test/java/eu/kanade/tachiyomi/CustomRobolectricGradleTestRunner.java b/app/src/test/java/eu/kanade/tachiyomi/CustomRobolectricGradleTestRunner.java deleted file mode 100644 index 9aa11e682..000000000 --- a/app/src/test/java/eu/kanade/tachiyomi/CustomRobolectricGradleTestRunner.java +++ /dev/null @@ -1,21 +0,0 @@ -package eu.kanade.tachiyomi; - -import org.junit.runners.model.InitializationError; -import org.robolectric.RobolectricGradleTestRunner; -import org.robolectric.annotation.Config; -import org.robolectric.manifest.AndroidManifest; - -public class CustomRobolectricGradleTestRunner - extends RobolectricGradleTestRunner { - - public CustomRobolectricGradleTestRunner(Class klass) throws InitializationError { - super(klass); - } - - @Override - protected AndroidManifest getAppManifest(Config config) { - AndroidManifest androidManifest = super.getAppManifest(config); - androidManifest.setPackageName("eu.kanade.tachiyomi"); - return androidManifest; - } -} \ No newline at end of file diff --git a/app/src/test/java/eu/kanade/tachiyomi/CustomRobolectricGradleTestRunner.kt b/app/src/test/java/eu/kanade/tachiyomi/CustomRobolectricGradleTestRunner.kt new file mode 100644 index 000000000..69cf23d80 --- /dev/null +++ b/app/src/test/java/eu/kanade/tachiyomi/CustomRobolectricGradleTestRunner.kt @@ -0,0 +1,12 @@ +package eu.kanade.tachiyomi + +import org.robolectric.RobolectricGradleTestRunner +import org.robolectric.annotation.Config +import org.robolectric.manifest.AndroidManifest + +class CustomRobolectricGradleTestRunner(klass: Class<*>) : RobolectricGradleTestRunner(klass) { + + override fun getAppManifest(config: Config): AndroidManifest { + return super.getAppManifest(config).apply { packageName = "eu.kanade.tachiyomi" } + } +} \ No newline at end of file diff --git a/app/src/test/java/eu/kanade/tachiyomi/CategoryTest.kt b/app/src/test/java/eu/kanade/tachiyomi/data/database/CategoryTest.kt similarity index 95% rename from app/src/test/java/eu/kanade/tachiyomi/CategoryTest.kt rename to app/src/test/java/eu/kanade/tachiyomi/data/database/CategoryTest.kt index f0b251699..61057b440 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/CategoryTest.kt +++ b/app/src/test/java/eu/kanade/tachiyomi/data/database/CategoryTest.kt @@ -1,7 +1,8 @@ -package eu.kanade.tachiyomi +package eu.kanade.tachiyomi.data.database import android.os.Build -import eu.kanade.tachiyomi.data.database.DatabaseHelper +import eu.kanade.tachiyomi.BuildConfig +import eu.kanade.tachiyomi.CustomRobolectricGradleTestRunner import eu.kanade.tachiyomi.data.database.models.CategoryImpl import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaCategory diff --git a/app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.kt b/app/src/test/java/eu/kanade/tachiyomi/data/database/ChapterRecognitionTest.kt similarity index 99% rename from app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.kt rename to app/src/test/java/eu/kanade/tachiyomi/data/database/ChapterRecognitionTest.kt index e13cd15cf..afc02e35e 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/ChapterRecognitionTest.kt +++ b/app/src/test/java/eu/kanade/tachiyomi/data/database/ChapterRecognitionTest.kt @@ -1,4 +1,4 @@ -package eu.kanade.tachiyomi +package eu.kanade.tachiyomi.data.database import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga diff --git a/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.java b/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.java deleted file mode 100644 index bee54844a..000000000 --- a/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.java +++ /dev/null @@ -1,140 +0,0 @@ -package eu.kanade.tachiyomi.data.library; - -import android.app.AlarmManager; -import android.content.Context; -import android.content.Intent; -import android.os.Build; -import android.os.SystemClock; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; -import org.robolectric.shadows.ShadowAlarmManager; -import org.robolectric.shadows.ShadowApplication; -import org.robolectric.shadows.ShadowPendingIntent; - -import eu.kanade.tachiyomi.BuildConfig; -import eu.kanade.tachiyomi.CustomRobolectricGradleTestRunner; -import eu.kanade.tachiyomi.data.preference.PreferencesHelper; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.spy; -import static org.robolectric.Shadows.shadowOf; - -@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) -@RunWith(CustomRobolectricGradleTestRunner.class) -public class LibraryUpdateAlarmTest { - - ShadowApplication app; - Context context; - ShadowAlarmManager alarmManager; - - @Before - public void setup() { - app = ShadowApplication.getInstance(); - context = spy(app.getApplicationContext()); - - alarmManager = shadowOf((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)); - } - - @Test - public void testLibraryIntentHandling() { - Intent intent = new Intent(LibraryUpdateAlarm.LIBRARY_UPDATE_ACTION); - assertThat(app.hasReceiverForIntent(intent)).isTrue(); - } - - @Test - public void testAlarmIsNotStarted() { - assertThat(alarmManager.getNextScheduledAlarm()).isNull(); - } - - @Test - public void testAlarmIsNotStartedWhenBootReceivedAndSettingZero() { - LibraryUpdateAlarm alarm = new LibraryUpdateAlarm(); - alarm.onReceive(context, new Intent(Intent.ACTION_BOOT_COMPLETED)); - - assertThat(alarmManager.getNextScheduledAlarm()).isNull(); - } - - @Test - public void testAlarmIsStartedWhenBootReceivedAndSettingNotZero() { - PreferencesHelper prefs = new PreferencesHelper(context); - prefs.libraryUpdateInterval().set(1); - - LibraryUpdateAlarm alarm = new LibraryUpdateAlarm(); - alarm.onReceive(context, new Intent(Intent.ACTION_BOOT_COMPLETED)); - - assertThat(alarmManager.getNextScheduledAlarm()).isNotNull(); - } - - @Test - public void testOnlyOneAlarmExists() { - PreferencesHelper prefs = new PreferencesHelper(context); - prefs.libraryUpdateInterval().set(1); - - LibraryUpdateAlarm.startAlarm(context); - LibraryUpdateAlarm.startAlarm(context); - LibraryUpdateAlarm.startAlarm(context); - - assertThat(alarmManager.getScheduledAlarms()).hasSize(1); - } - - @Test - public void testLibraryWillBeUpdatedWhenAlarmFired() { - PreferencesHelper prefs = new PreferencesHelper(context); - prefs.libraryUpdateInterval().set(1); - - Intent expectedIntent = new Intent(context, LibraryUpdateAlarm.class); - expectedIntent.setAction(LibraryUpdateAlarm.LIBRARY_UPDATE_ACTION); - - LibraryUpdateAlarm.startAlarm(context); - - ShadowAlarmManager.ScheduledAlarm scheduledAlarm = alarmManager.getNextScheduledAlarm(); - ShadowPendingIntent pendingIntent = shadowOf(scheduledAlarm.operation); - assertThat(pendingIntent.isBroadcastIntent()).isTrue(); - assertThat(pendingIntent.getSavedIntents()).hasSize(1); - assertThat(expectedIntent.getComponent()).isEqualTo(pendingIntent.getSavedIntents()[0].getComponent()); - assertThat(expectedIntent.getAction()).isEqualTo(pendingIntent.getSavedIntents()[0].getAction()); - } - - @Test - public void testLibraryUpdateServiceIsStartedWhenUpdateIntentIsReceived() { - Intent intent = new Intent(context, LibraryUpdateService.class); - intent.putExtra("is_manual", false); - assertThat(app.getNextStartedService()).isNotEqualTo(intent); - - LibraryUpdateAlarm alarm = new LibraryUpdateAlarm(); - alarm.onReceive(context, new Intent(LibraryUpdateAlarm.LIBRARY_UPDATE_ACTION)); - - assertThat(app.getNextStartedService()).isEqualTo(intent); - } - - @Test - public void testReceiverDoesntReactToNullActions() { - PreferencesHelper prefs = new PreferencesHelper(context); - prefs.libraryUpdateInterval().set(1); - - Intent intent = new Intent(context, LibraryUpdateService.class); - - LibraryUpdateAlarm alarm = new LibraryUpdateAlarm(); - alarm.onReceive(context, new Intent()); - - assertThat(app.getNextStartedService()).isNotEqualTo(intent); - assertThat(alarmManager.getScheduledAlarms()).hasSize(0); - } - - @Test - public void testAlarmFiresCloseToDesiredTime() { - int hours = 2; - LibraryUpdateAlarm.startAlarm(context, hours); - - long shouldRunAt = SystemClock.elapsedRealtime() + (hours * 60 * 60 * 1000); - - // Margin error of 3 seconds - assertThat(alarmManager.getNextScheduledAlarm().triggerAtTime) - .isGreaterThan(shouldRunAt - 3000) - .isLessThan(shouldRunAt + 3000); - } - -} diff --git a/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.kt b/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.kt new file mode 100644 index 000000000..d1f918ebf --- /dev/null +++ b/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.kt @@ -0,0 +1,134 @@ +package eu.kanade.tachiyomi.data.library + +import android.app.AlarmManager +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.SystemClock +import eu.kanade.tachiyomi.BuildConfig +import eu.kanade.tachiyomi.CustomRobolectricGradleTestRunner +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import org.assertj.core.api.Assertions.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito.spy +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.shadows.ShadowAlarmManager +import org.robolectric.shadows.ShadowApplication + +@Config(constants = BuildConfig::class, sdk = intArrayOf(Build.VERSION_CODES.LOLLIPOP)) +@RunWith(CustomRobolectricGradleTestRunner::class) +class LibraryUpdateAlarmTest { + + lateinit var app: ShadowApplication + lateinit var context: Context + lateinit var alarmManager: ShadowAlarmManager + + @Before + fun setup() { + app = ShadowApplication.getInstance() + context = spy(app.applicationContext) + + alarmManager = shadowOf(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) + } + + @Test + fun testLibraryIntentHandling() { + val intent = Intent(LibraryUpdateAlarm.LIBRARY_UPDATE_ACTION) + assertThat(app.hasReceiverForIntent(intent)).isTrue() + } + + @Test + fun testAlarmIsNotStarted() { + assertThat(alarmManager.nextScheduledAlarm).isNull() + } + + @Test + fun testAlarmIsNotStartedWhenBootReceivedAndSettingZero() { + val alarm = LibraryUpdateAlarm() + alarm.onReceive(context, Intent(Intent.ACTION_BOOT_COMPLETED)) + + assertThat(alarmManager.nextScheduledAlarm).isNull() + } + + @Test + fun testAlarmIsStartedWhenBootReceivedAndSettingNotZero() { + val prefs = PreferencesHelper(context) + prefs.libraryUpdateInterval().set(1) + + val alarm = LibraryUpdateAlarm() + alarm.onReceive(context, Intent(Intent.ACTION_BOOT_COMPLETED)) + + assertThat(alarmManager.nextScheduledAlarm).isNotNull() + } + + @Test + fun testOnlyOneAlarmExists() { + val prefs = PreferencesHelper(context) + prefs.libraryUpdateInterval().set(1) + + LibraryUpdateAlarm.startAlarm(context) + LibraryUpdateAlarm.startAlarm(context) + LibraryUpdateAlarm.startAlarm(context) + + assertThat(alarmManager.scheduledAlarms).hasSize(1) + } + + @Test + fun testLibraryWillBeUpdatedWhenAlarmFired() { + val prefs = PreferencesHelper(context) + prefs.libraryUpdateInterval().set(1) + + val expectedIntent = Intent(context, LibraryUpdateAlarm::class.java) + expectedIntent.action = LibraryUpdateAlarm.LIBRARY_UPDATE_ACTION + + LibraryUpdateAlarm.startAlarm(context) + + val scheduledAlarm = alarmManager.nextScheduledAlarm + val pendingIntent = shadowOf(scheduledAlarm.operation) + assertThat(pendingIntent.isBroadcastIntent).isTrue() + assertThat(pendingIntent.savedIntents).hasSize(1) + assertThat(expectedIntent.component).isEqualTo(pendingIntent.savedIntents[0].component) + assertThat(expectedIntent.action).isEqualTo(pendingIntent.savedIntents[0].action) + } + + @Test + fun testLibraryUpdateServiceIsStartedWhenUpdateIntentIsReceived() { + val intent = Intent(context, LibraryUpdateService::class.java) + intent.putExtra("is_manual", false) + assertThat(app.nextStartedService).isNotEqualTo(intent) + + val alarm = LibraryUpdateAlarm() + alarm.onReceive(context, Intent(LibraryUpdateAlarm.LIBRARY_UPDATE_ACTION)) + + assertThat(app.nextStartedService).isEqualTo(intent) + } + + @Test + fun testReceiverDoesntReactToNullActions() { + val prefs = PreferencesHelper(context) + prefs.libraryUpdateInterval().set(1) + + val intent = Intent(context, LibraryUpdateService::class.java) + + val alarm = LibraryUpdateAlarm() + alarm.onReceive(context, Intent()) + + assertThat(app.nextStartedService).isNotEqualTo(intent) + assertThat(alarmManager.scheduledAlarms).hasSize(0) + } + + @Test + fun testAlarmFiresCloseToDesiredTime() { + val hours = 2 + LibraryUpdateAlarm.startAlarm(context, hours) + + val shouldRunAt = SystemClock.elapsedRealtime() + hours * 60 * 60 * 1000 + + // Margin error of 3 seconds + assertThat(alarmManager.nextScheduledAlarm.triggerAtTime).isGreaterThan(shouldRunAt - 3000).isLessThan(shouldRunAt + 3000) + } + +} diff --git a/app/src/test/java/eu/kanade/tachiyomi/util/DefaultConfig.java b/app/src/test/java/eu/kanade/tachiyomi/util/DefaultConfig.java deleted file mode 100644 index 57af05b94..000000000 --- a/app/src/test/java/eu/kanade/tachiyomi/util/DefaultConfig.java +++ /dev/null @@ -1,7 +0,0 @@ -package eu.kanade.tachiyomi.util; - -public class DefaultConfig { - //The api level that Roboelectric will use to run the unit tests - public static final int EMULATE_SDK = 21; - public static final String MANIFEST = "./src/main/AndroidManifest.xml"; -} \ No newline at end of file