From 0d41c60a383cb6c77e0582ad2cdb6e45ddff76e2 Mon Sep 17 00:00:00 2001 From: len Date: Sat, 19 Mar 2016 21:09:51 +0100 Subject: [PATCH] Fix tests --- app/build.gradle | 6 ++-- .../tachiyomi/data/database/DatabaseHelper.kt | 6 ++-- .../eu/kanade/tachiyomi/CategoryTest.java | 5 ++-- .../test/java/eu/kanade/tachiyomi/TestApp.kt | 2 +- .../data/library/LibraryUpdateAlarmTest.java | 7 ++--- .../library/LibraryUpdateServiceTest.java | 9 +++--- .../injection/module/TestDataModule.java | 28 ------------------- .../injection/module/TestDataModule.kt | 23 +++++++++++++++ 8 files changed, 40 insertions(+), 46 deletions(-) delete mode 100644 app/src/test/java/eu/kanade/tachiyomi/injection/module/TestDataModule.java create mode 100644 app/src/test/java/eu/kanade/tachiyomi/injection/module/TestDataModule.kt diff --git a/app/build.gradle b/app/build.gradle index aac5991ba..4bf8add1b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -113,7 +113,7 @@ dependencies { compile 'com.jakewharton:disklrucache:2.0.2' compile 'org.jsoup:jsoup:1.8.3' compile 'io.reactivex:rxandroid:1.1.0' - compile 'io.reactivex:rxjava:1.1.1' + compile 'io.reactivex:rxjava:1.1.2' compile "com.squareup.retrofit2:retrofit:$RETROFIT_VERSION" compile "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION" compile "com.squareup.retrofit2:adapter-rxjava:$RETROFIT_VERSION" @@ -123,7 +123,7 @@ dependencies { compile 'info.android15.nucleus:nucleus:2.0.5' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.jakewharton.timber:timber:4.1.1' - compile 'ch.acra:acra:4.8.3' + compile 'ch.acra:acra:4.8.5' compile 'com.github.dmytrodanylyk.android-process-button:library:1.0.4' compile 'eu.davidea:flexible-adapter:4.2.0' compile 'com.nononsenseapps:filepicker:2.5.2' @@ -142,7 +142,7 @@ dependencies { testCompile 'junit:junit:4.12' - testCompile 'org.assertj:assertj-core:2.3.0' + testCompile 'org.assertj:assertj-core:1.7.1' testCompile "org.mockito:mockito-core:$MOCKITO_VERSION" testCompile('org.robolectric:robolectric:3.0') { exclude group: 'commons-logging', module: 'commons-logging' diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.kt index d73b93ce3..67e988303 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.kt @@ -19,7 +19,7 @@ import eu.kanade.tachiyomi.util.ChapterRecognition import rx.Observable import java.util.* -class DatabaseHelper(context: Context) { +open class DatabaseHelper(context: Context) { val db = DefaultStorIOSQLite.builder() .sqliteOpenHelper(DbOpenHelper(context)) @@ -58,7 +58,7 @@ class DatabaseHelper(context: Context) { .withGetResolver(LibraryMangaGetResolver.INSTANCE) .prepare() - fun getFavoriteMangas() = db.get() + open fun getFavoriteMangas() = db.get() .listOfObjects(Manga::class.java) .withQuery(Query.builder() .table(MangaTable.TABLE) @@ -178,7 +178,7 @@ class DatabaseHelper(context: Context) { fun insertChapters(chapters: List) = db.put().objects(chapters).prepare() // Add new chapters or delete if the source deletes them - fun insertOrRemoveChapters(manga: Manga, sourceChapters: List, source: Source): Observable> { + open fun insertOrRemoveChapters(manga: Manga, sourceChapters: List, source: Source): Observable> { val dbChapters = getChapters(manga).executeAsBlocking() val newChapters = Observable.from(sourceChapters) diff --git a/app/src/test/java/eu/kanade/tachiyomi/CategoryTest.java b/app/src/test/java/eu/kanade/tachiyomi/CategoryTest.java index f201f274c..4f7325d3a 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/CategoryTest.java +++ b/app/src/test/java/eu/kanade/tachiyomi/CategoryTest.java @@ -6,7 +6,6 @@ import android.os.Build; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @@ -17,10 +16,10 @@ import eu.kanade.tachiyomi.data.database.models.Category; import eu.kanade.tachiyomi.data.database.models.Manga; import eu.kanade.tachiyomi.data.database.models.MangaCategory; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) -@RunWith(RobolectricGradleTestRunner.class) +@RunWith(CustomRobolectricGradleTestRunner.class) public class CategoryTest { DatabaseHelper db; diff --git a/app/src/test/java/eu/kanade/tachiyomi/TestApp.kt b/app/src/test/java/eu/kanade/tachiyomi/TestApp.kt index 554865e5d..d85bc10dc 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/TestApp.kt +++ b/app/src/test/java/eu/kanade/tachiyomi/TestApp.kt @@ -4,7 +4,7 @@ import eu.kanade.tachiyomi.injection.component.DaggerAppComponent import eu.kanade.tachiyomi.injection.module.AppModule import eu.kanade.tachiyomi.injection.module.TestDataModule -class TestApp : App() { +open class TestApp : App() { override fun prepareAppComponent(): DaggerAppComponent.Builder { return DaggerAppComponent.builder() 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 index f4557f0bd..ea5d04019 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.java +++ b/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.java @@ -6,7 +6,6 @@ import android.content.Intent; import android.os.Build; import android.os.SystemClock; -import org.assertj.core.data.Offset; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -132,9 +131,9 @@ public class LibraryUpdateAlarmTest { long shouldRunAt = SystemClock.elapsedRealtime() + (hours * 60 * 60 * 1000); // Margin error of 3 seconds - Offset offset = Offset.offset(3 * 1000L); - - assertThat(alarmManager.getNextScheduledAlarm().triggerAtTime).isCloseTo(shouldRunAt, offset); + assertThat(alarmManager.getNextScheduledAlarm().triggerAtTime) + .isGreaterThan(shouldRunAt - 3000) + .isLessThan(shouldRunAt + 3000); } } diff --git a/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateServiceTest.java b/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateServiceTest.java index 8c3225c39..887f28dbd 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateServiceTest.java +++ b/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateServiceTest.java @@ -23,6 +23,7 @@ import rx.Observable; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyListOf; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -88,7 +89,7 @@ public class LibraryUpdateServiceTest { // One of the updates will fail when(source.pullChaptersFromNetwork("manga1")).thenReturn(Observable.just(chapters)); - when(source.pullChaptersFromNetwork("manga2")).thenReturn(Observable.error(new Exception())); + when(source.pullChaptersFromNetwork("manga2")).thenReturn(Observable.>error(new Exception())); when(source.pullChaptersFromNetwork("manga3")).thenReturn(Observable.just(chapters3)); when(service.db.insertOrRemoveChapters(manga1, chapters, source)).thenReturn(Observable.just(Pair.create(2, 0))); @@ -97,9 +98,9 @@ public class LibraryUpdateServiceTest { service.updateLibrary().subscribe(); // There are 3 network attempts and 2 insertions (1 request failed) - verify(source, times(3)).pullChaptersFromNetwork(any()); - verify(service.db, times(2)).insertOrRemoveChapters(any(), any(), any()); - verify(service.db, never()).insertOrRemoveChapters(eq(manga2), any(), any()); + verify(source, times(3)).pullChaptersFromNetwork((String)any()); + verify(service.db, times(2)).insertOrRemoveChapters((Manga)any(), anyListOf(Chapter.class), (Source)any()); + verify(service.db, never()).insertOrRemoveChapters(eq(manga2), anyListOf(Chapter.class), (Source)any()); } private List createChapters(String... urls) { diff --git a/app/src/test/java/eu/kanade/tachiyomi/injection/module/TestDataModule.java b/app/src/test/java/eu/kanade/tachiyomi/injection/module/TestDataModule.java deleted file mode 100644 index 0188d4e52..000000000 --- a/app/src/test/java/eu/kanade/tachiyomi/injection/module/TestDataModule.java +++ /dev/null @@ -1,28 +0,0 @@ -package eu.kanade.tachiyomi.injection.module; - -import android.app.Application; - -import org.mockito.Mockito; - -import eu.kanade.tachiyomi.data.database.DatabaseHelper; -import eu.kanade.tachiyomi.data.network.NetworkHelper; -import eu.kanade.tachiyomi.data.source.SourceManager; - -public class TestDataModule extends DataModule { - - @Override - DatabaseHelper provideDatabaseHelper(Application app) { - return Mockito.mock(DatabaseHelper.class, Mockito.RETURNS_DEEP_STUBS); - } - - @Override - NetworkHelper provideNetworkHelper(Application app) { - return Mockito.mock(NetworkHelper.class); - } - - @Override - SourceManager provideSourceManager(Application app) { - return Mockito.mock(SourceManager.class, Mockito.RETURNS_DEEP_STUBS); - } - -} diff --git a/app/src/test/java/eu/kanade/tachiyomi/injection/module/TestDataModule.kt b/app/src/test/java/eu/kanade/tachiyomi/injection/module/TestDataModule.kt new file mode 100644 index 000000000..347cd67c2 --- /dev/null +++ b/app/src/test/java/eu/kanade/tachiyomi/injection/module/TestDataModule.kt @@ -0,0 +1,23 @@ +package eu.kanade.tachiyomi.injection.module + +import android.app.Application +import eu.kanade.tachiyomi.data.database.DatabaseHelper +import eu.kanade.tachiyomi.data.network.NetworkHelper +import eu.kanade.tachiyomi.data.source.SourceManager +import org.mockito.Mockito + +class TestDataModule : DataModule() { + + override fun provideDatabaseHelper(app: Application): DatabaseHelper { + return Mockito.mock(DatabaseHelper::class.java, Mockito.RETURNS_DEEP_STUBS) + } + + override fun provideNetworkHelper(app: Application): NetworkHelper { + return Mockito.mock(NetworkHelper::class.java) + } + + override fun provideSourceManager(app: Application): SourceManager { + return Mockito.mock(SourceManager::class.java, Mockito.RETURNS_DEEP_STUBS) + } + +}