mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-02 23:28:56 +01:00
Test package in Kotlin
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package eu.kanade.tachiyomi.data.database
|
||||
|
||||
import android.os.Build
|
||||
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
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@Config(constants = BuildConfig::class, sdk = intArrayOf(Build.VERSION_CODES.LOLLIPOP))
|
||||
@RunWith(CustomRobolectricGradleTestRunner::class)
|
||||
class CategoryTest {
|
||||
|
||||
lateinit var db: DatabaseHelper
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
val app = RuntimeEnvironment.application
|
||||
db = DatabaseHelper(app)
|
||||
|
||||
// Create 5 manga
|
||||
createManga("a")
|
||||
createManga("b")
|
||||
createManga("c")
|
||||
createManga("d")
|
||||
createManga("e")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHasCategories() {
|
||||
// Create 2 categories
|
||||
createCategory("Reading")
|
||||
createCategory("Hold")
|
||||
|
||||
val categories = db.getCategories().executeAsBlocking()
|
||||
assertThat(categories).hasSize(2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHasLibraryMangas() {
|
||||
val mangas = db.getLibraryMangas().executeAsBlocking()
|
||||
assertThat(mangas).hasSize(5)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHasCorrectFavorites() {
|
||||
val m = Manga.create(0)
|
||||
m.title = "title"
|
||||
m.author = ""
|
||||
m.artist = ""
|
||||
m.thumbnail_url = ""
|
||||
m.genre = "a list of genres"
|
||||
m.description = "long description"
|
||||
m.url = "url to manga"
|
||||
m.favorite = false
|
||||
db.insertManga(m).executeAsBlocking()
|
||||
val mangas = db.getLibraryMangas().executeAsBlocking()
|
||||
assertThat(mangas).hasSize(5)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMangaInCategory() {
|
||||
// Create 2 categories
|
||||
createCategory("Reading")
|
||||
createCategory("Hold")
|
||||
|
||||
// It should not have 0 as id
|
||||
val c = db.getCategories().executeAsBlocking()[0]
|
||||
assertThat(c.id).isNotZero()
|
||||
|
||||
// Add a manga to a category
|
||||
val m = db.getMangas().executeAsBlocking()[0]
|
||||
val mc = MangaCategory.create(m, c)
|
||||
db.insertMangaCategory(mc).executeAsBlocking()
|
||||
|
||||
// Get mangas from library and assert manga category is the same
|
||||
val mangas = db.getLibraryMangas().executeAsBlocking()
|
||||
for (manga in mangas) {
|
||||
if (manga.id == m.id) {
|
||||
assertThat(manga.category).isEqualTo(c.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun createManga(title: String) {
|
||||
val m = Manga.create(0)
|
||||
m.title = title
|
||||
m.author = ""
|
||||
m.artist = ""
|
||||
m.thumbnail_url = ""
|
||||
m.genre = "a list of genres"
|
||||
m.description = "long description"
|
||||
m.url = "url to manga"
|
||||
m.favorite = true
|
||||
db.insertManga(m).executeAsBlocking()
|
||||
}
|
||||
|
||||
private fun createCategory(name: String) {
|
||||
val c = CategoryImpl()
|
||||
c.name = name
|
||||
db.insertCategory(c).executeAsBlocking()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,432 @@
|
||||
package eu.kanade.tachiyomi.data.database
|
||||
|
||||
import eu.kanade.tachiyomi.data.database.models.Chapter
|
||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||
import eu.kanade.tachiyomi.util.ChapterRecognition
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
class ChapterRecognitionTest {
|
||||
/**
|
||||
* The manga containing manga title
|
||||
*/
|
||||
lateinit var manga: Manga
|
||||
|
||||
/**
|
||||
* The chapter containing chapter name
|
||||
*/
|
||||
lateinit var chapter: Chapter
|
||||
|
||||
/**
|
||||
* Set chapter title
|
||||
* @param name name of chapter
|
||||
* @return chapter object
|
||||
*/
|
||||
private fun createChapter(name: String): Chapter {
|
||||
chapter = Chapter.create()
|
||||
chapter.name = name
|
||||
return chapter
|
||||
}
|
||||
|
||||
/**
|
||||
* Set manga title
|
||||
* @param title title of manga
|
||||
* @return manga object
|
||||
*/
|
||||
private fun createManga(title: String): Manga {
|
||||
manga.title = title
|
||||
return manga
|
||||
}
|
||||
|
||||
/**
|
||||
* Called before test
|
||||
*/
|
||||
@Before fun setup() {
|
||||
manga = Manga.create(0).apply { title = "random" }
|
||||
chapter = Chapter.create()
|
||||
}
|
||||
|
||||
/**
|
||||
* Ch.xx base case
|
||||
*/
|
||||
@Test fun ChCaseBase() {
|
||||
createManga("Mokushiroku Alice")
|
||||
|
||||
createChapter("Mokushiroku Alice Vol.1 Ch.4: Misrepresentation")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(4f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Ch.xx.x base case
|
||||
*/
|
||||
@Test fun ChCaseDecimal() {
|
||||
createManga("Mokushiroku Alice")
|
||||
|
||||
createChapter("Mokushiroku Alice Vol.1 Ch.4.1: Misrepresentation")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(4.1f)
|
||||
|
||||
createChapter("Mokushiroku Alice Vol.1 Ch.4.4: Misrepresentation")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(4.4f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Ch.xx.a base case
|
||||
*/
|
||||
@Test fun ChCaseAlpha() {
|
||||
createManga("Mokushiroku Alice")
|
||||
|
||||
createChapter("Mokushiroku Alice Vol.1 Ch.4.a: Misrepresentation")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(4.1f)
|
||||
|
||||
createChapter("Mokushiroku Alice Vol.1 Ch.4.b: Misrepresentation")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(4.2f)
|
||||
|
||||
createChapter("Mokushiroku Alice Vol.1 Ch.4.extra: Misrepresentation")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(4.99f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Name containing one number base case
|
||||
*/
|
||||
@Test fun OneNumberCaseBase() {
|
||||
createManga("Bleach")
|
||||
|
||||
createChapter("Bleach 567 Down With Snowwhite")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(567f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Name containing one number and decimal case
|
||||
*/
|
||||
@Test fun OneNumberCaseDecimal() {
|
||||
createManga("Bleach")
|
||||
|
||||
createChapter("Bleach 567.1 Down With Snowwhite")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(567.1f)
|
||||
|
||||
createChapter("Bleach 567.4 Down With Snowwhite")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(567.4f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Name containing one number and alpha case
|
||||
*/
|
||||
@Test fun OneNumberCaseAlpha() {
|
||||
createManga("Bleach")
|
||||
|
||||
createChapter("Bleach 567.a Down With Snowwhite")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(567.1f)
|
||||
|
||||
createChapter("Bleach 567.b Down With Snowwhite")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(567.2f)
|
||||
|
||||
createChapter("Bleach 567.extra Down With Snowwhite")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(567.99f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter containing manga title and number base case
|
||||
*/
|
||||
@Test fun MangaTitleCaseBase() {
|
||||
createManga("Solanin")
|
||||
|
||||
createChapter("Solanin 028 Vol. 2")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter containing manga title and number decimal case
|
||||
*/
|
||||
@Test fun MangaTitleCaseDecimal() {
|
||||
createManga("Solanin")
|
||||
|
||||
createChapter("Solanin 028.1 Vol. 2")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28.1f)
|
||||
|
||||
createChapter("Solanin 028.4 Vol. 2")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28.4f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter containing manga title and number alpha case
|
||||
*/
|
||||
@Test fun MangaTitleCaseAlpha() {
|
||||
createManga("Solanin")
|
||||
|
||||
createChapter("Solanin 028.a Vol. 2")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28.1f)
|
||||
|
||||
createChapter("Solanin 028.b Vol. 2")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28.2f)
|
||||
|
||||
createChapter("Solanin 028.extra Vol. 2")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28.99f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extreme base case
|
||||
*/
|
||||
@Test fun ExtremeCaseBase() {
|
||||
createManga("Onepunch-Man")
|
||||
|
||||
createChapter("Onepunch-Man Punch Ver002 028")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extreme base case decimal
|
||||
*/
|
||||
@Test fun ExtremeCaseDecimal() {
|
||||
createManga("Onepunch-Man")
|
||||
|
||||
createChapter("Onepunch-Man Punch Ver002 028.1")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28.1f)
|
||||
|
||||
createChapter("Onepunch-Man Punch Ver002 028.4")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28.4f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extreme base case alpha
|
||||
*/
|
||||
@Test fun ExtremeCaseAlpha() {
|
||||
createManga("Onepunch-Man")
|
||||
|
||||
createChapter("Onepunch-Man Punch Ver002 028.a")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28.1f)
|
||||
|
||||
createChapter("Onepunch-Man Punch Ver002 028.b")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28.2f)
|
||||
|
||||
createChapter("Onepunch-Man Punch Ver002 028.extra")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(28.99f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter containing .v2
|
||||
*/
|
||||
@Test fun dotV2Case() {
|
||||
createChapter("Vol.1 Ch.5v.2: Alones")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(5f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for case with number in manga title
|
||||
*/
|
||||
@Test fun numberInMangaTitleCase() {
|
||||
createManga("Ayame 14")
|
||||
createChapter("Ayame 14 1 - The summer of 14")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(1f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Case with space between ch. x
|
||||
*/
|
||||
@Test fun spaceAfterChapterCase() {
|
||||
createManga("Mokushiroku Alice")
|
||||
createChapter("Mokushiroku Alice Vol.1 Ch. 4: Misrepresentation")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(4f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter containing mar(ch)
|
||||
*/
|
||||
@Test fun marchInChapterCase() {
|
||||
createManga("Ayame 14")
|
||||
createChapter("Vol.1 Ch.1: March 25 (First Day Cohabiting)")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(1f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter containing range
|
||||
*/
|
||||
@Test fun rangeInChapterCase() {
|
||||
createChapter("Ch.191-200 Read Online")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(191f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter containing multiple zeros
|
||||
*/
|
||||
@Test fun multipleZerosCase() {
|
||||
createChapter("Vol.001 Ch.003: Kaguya Doesn't Know Much")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(3f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter with version before number
|
||||
*/
|
||||
@Test fun chapterBeforeNumberCase() {
|
||||
createManga("Onepunch-Man")
|
||||
createChapter("Onepunch-Man Punch Ver002 086 : Creeping Darkness [3]")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(86f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Case with version attached to chapter number
|
||||
*/
|
||||
@Test fun vAttachedToChapterCase() {
|
||||
createManga("Ansatsu Kyoushitsu")
|
||||
createChapter("Ansatsu Kyoushitsu 011v002: Assembly Time")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(11f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Case where the chapter title contains the chapter
|
||||
* But wait it's not actual the chapter number.
|
||||
*/
|
||||
@Test fun NumberAfterMangaTitleWithChapterInChapterTitleCase() {
|
||||
createChapter("Tokyo ESP 027: Part 002: Chapter 001")
|
||||
createManga("Tokyo ESP")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(027f)
|
||||
}
|
||||
|
||||
/**
|
||||
* unParsable chapter
|
||||
*/
|
||||
@Test fun unParsableCase() {
|
||||
createChapter("Foo")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(-1f)
|
||||
}
|
||||
|
||||
/**
|
||||
* chapter with time in title
|
||||
*/
|
||||
@Test fun timeChapterCase() {
|
||||
createChapter("Fairy Tail 404: 00:00")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(404f)
|
||||
}
|
||||
|
||||
/**
|
||||
* chapter with alpha without dot
|
||||
*/
|
||||
@Test fun alphaWithoutDotCase() {
|
||||
createChapter("Asu No Yoichi 19a")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(19.1f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter title containing extra and vol
|
||||
*/
|
||||
@Test fun chapterContainingExtraCase() {
|
||||
createManga("Fairy Tail")
|
||||
|
||||
createChapter("Fairy Tail 404.extravol002")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(404.99f)
|
||||
|
||||
createChapter("Fairy Tail 404 extravol002")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(404.99f)
|
||||
|
||||
createChapter("Fairy Tail 404.evol002")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(404.5f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter title containing omake (japanese extra) and vol
|
||||
*/
|
||||
@Test fun chapterContainingOmakeCase() {
|
||||
createManga("Fairy Tail")
|
||||
|
||||
createChapter("Fairy Tail 404.omakevol002")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(404.98f)
|
||||
|
||||
createChapter("Fairy Tail 404 omakevol002")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(404.98f)
|
||||
|
||||
createChapter("Fairy Tail 404.ovol002")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(404.15f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter title containing special and vol
|
||||
*/
|
||||
@Test fun chapterContainingSpecialCase() {
|
||||
createManga("Fairy Tail")
|
||||
|
||||
createChapter("Fairy Tail 404.specialvol002")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(404.97f)
|
||||
|
||||
createChapter("Fairy Tail 404 specialvol002")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(404.97f)
|
||||
|
||||
createChapter("Fairy Tail 404.svol002")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(404.19f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Chapter title containing comma's
|
||||
*/
|
||||
@Test fun chapterContainingCommasCase() {
|
||||
createManga("One Piece")
|
||||
|
||||
createChapter("One Piece 300,a")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(300.1f)
|
||||
|
||||
createChapter("One Piece Ch,123,extra")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(123.99f)
|
||||
|
||||
createChapter("One Piece the sunny, goes swimming 024,005")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(24.005f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for chapters containing season
|
||||
*/
|
||||
@Test fun chapterContainingSeasonCase() {
|
||||
createManga("D.I.C.E")
|
||||
|
||||
createChapter("D.I.C.E[Season 001] Ep. 007")
|
||||
ChapterRecognition.parseChapterNumber(chapter, manga)
|
||||
assertThat(chapter.chapter_number).isEqualTo(7f)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user