Fix tests

This commit is contained in:
len 2016-03-19 21:09:51 +01:00
parent 5132f4850f
commit 0d41c60a38
8 changed files with 40 additions and 46 deletions

View File

@ -113,7 +113,7 @@ dependencies {
compile 'com.jakewharton:disklrucache:2.0.2' compile 'com.jakewharton:disklrucache:2.0.2'
compile 'org.jsoup:jsoup:1.8.3' compile 'org.jsoup:jsoup:1.8.3'
compile 'io.reactivex:rxandroid:1.1.0' 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:retrofit:$RETROFIT_VERSION"
compile "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION" compile "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION"
compile "com.squareup.retrofit2:adapter-rxjava:$RETROFIT_VERSION" compile "com.squareup.retrofit2:adapter-rxjava:$RETROFIT_VERSION"
@ -123,7 +123,7 @@ dependencies {
compile 'info.android15.nucleus:nucleus:2.0.5' compile 'info.android15.nucleus:nucleus:2.0.5'
compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.jakewharton.timber:timber:4.1.1' 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 'com.github.dmytrodanylyk.android-process-button:library:1.0.4'
compile 'eu.davidea:flexible-adapter:4.2.0' compile 'eu.davidea:flexible-adapter:4.2.0'
compile 'com.nononsenseapps:filepicker:2.5.2' compile 'com.nononsenseapps:filepicker:2.5.2'
@ -142,7 +142,7 @@ dependencies {
testCompile 'junit:junit:4.12' 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.mockito:mockito-core:$MOCKITO_VERSION"
testCompile('org.robolectric:robolectric:3.0') { testCompile('org.robolectric:robolectric:3.0') {
exclude group: 'commons-logging', module: 'commons-logging' exclude group: 'commons-logging', module: 'commons-logging'

View File

@ -19,7 +19,7 @@ import eu.kanade.tachiyomi.util.ChapterRecognition
import rx.Observable import rx.Observable
import java.util.* import java.util.*
class DatabaseHelper(context: Context) { open class DatabaseHelper(context: Context) {
val db = DefaultStorIOSQLite.builder() val db = DefaultStorIOSQLite.builder()
.sqliteOpenHelper(DbOpenHelper(context)) .sqliteOpenHelper(DbOpenHelper(context))
@ -58,7 +58,7 @@ class DatabaseHelper(context: Context) {
.withGetResolver(LibraryMangaGetResolver.INSTANCE) .withGetResolver(LibraryMangaGetResolver.INSTANCE)
.prepare() .prepare()
fun getFavoriteMangas() = db.get() open fun getFavoriteMangas() = db.get()
.listOfObjects(Manga::class.java) .listOfObjects(Manga::class.java)
.withQuery(Query.builder() .withQuery(Query.builder()
.table(MangaTable.TABLE) .table(MangaTable.TABLE)
@ -178,7 +178,7 @@ class DatabaseHelper(context: Context) {
fun insertChapters(chapters: List<Chapter>) = db.put().objects(chapters).prepare() fun insertChapters(chapters: List<Chapter>) = db.put().objects(chapters).prepare()
// Add new chapters or delete if the source deletes them // Add new chapters or delete if the source deletes them
fun insertOrRemoveChapters(manga: Manga, sourceChapters: List<Chapter>, source: Source): Observable<Pair<Int, Int>> { open fun insertOrRemoveChapters(manga: Manga, sourceChapters: List<Chapter>, source: Source): Observable<Pair<Int, Int>> {
val dbChapters = getChapters(manga).executeAsBlocking() val dbChapters = getChapters(manga).executeAsBlocking()
val newChapters = Observable.from(sourceChapters) val newChapters = Observable.from(sourceChapters)

View File

@ -6,7 +6,6 @@ import android.os.Build;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; 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.Manga;
import eu.kanade.tachiyomi.data.database.models.MangaCategory; 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) @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(RobolectricGradleTestRunner.class) @RunWith(CustomRobolectricGradleTestRunner.class)
public class CategoryTest { public class CategoryTest {
DatabaseHelper db; DatabaseHelper db;

View File

@ -4,7 +4,7 @@ import eu.kanade.tachiyomi.injection.component.DaggerAppComponent
import eu.kanade.tachiyomi.injection.module.AppModule import eu.kanade.tachiyomi.injection.module.AppModule
import eu.kanade.tachiyomi.injection.module.TestDataModule import eu.kanade.tachiyomi.injection.module.TestDataModule
class TestApp : App() { open class TestApp : App() {
override fun prepareAppComponent(): DaggerAppComponent.Builder { override fun prepareAppComponent(): DaggerAppComponent.Builder {
return DaggerAppComponent.builder() return DaggerAppComponent.builder()

View File

@ -6,7 +6,6 @@ import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.SystemClock; import android.os.SystemClock;
import org.assertj.core.data.Offset;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -132,9 +131,9 @@ public class LibraryUpdateAlarmTest {
long shouldRunAt = SystemClock.elapsedRealtime() + (hours * 60 * 60 * 1000); long shouldRunAt = SystemClock.elapsedRealtime() + (hours * 60 * 60 * 1000);
// Margin error of 3 seconds // Margin error of 3 seconds
Offset<Long> offset = Offset.offset(3 * 1000L); assertThat(alarmManager.getNextScheduledAlarm().triggerAtTime)
.isGreaterThan(shouldRunAt - 3000)
assertThat(alarmManager.getNextScheduledAlarm().triggerAtTime).isCloseTo(shouldRunAt, offset); .isLessThan(shouldRunAt + 3000);
} }
} }

View File

@ -23,6 +23,7 @@ import rx.Observable;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
@ -88,7 +89,7 @@ public class LibraryUpdateServiceTest {
// One of the updates will fail // One of the updates will fail
when(source.pullChaptersFromNetwork("manga1")).thenReturn(Observable.just(chapters)); when(source.pullChaptersFromNetwork("manga1")).thenReturn(Observable.just(chapters));
when(source.pullChaptersFromNetwork("manga2")).thenReturn(Observable.error(new Exception())); when(source.pullChaptersFromNetwork("manga2")).thenReturn(Observable.<List<Chapter>>error(new Exception()));
when(source.pullChaptersFromNetwork("manga3")).thenReturn(Observable.just(chapters3)); when(source.pullChaptersFromNetwork("manga3")).thenReturn(Observable.just(chapters3));
when(service.db.insertOrRemoveChapters(manga1, chapters, source)).thenReturn(Observable.just(Pair.create(2, 0))); when(service.db.insertOrRemoveChapters(manga1, chapters, source)).thenReturn(Observable.just(Pair.create(2, 0)));
@ -97,9 +98,9 @@ public class LibraryUpdateServiceTest {
service.updateLibrary().subscribe(); service.updateLibrary().subscribe();
// There are 3 network attempts and 2 insertions (1 request failed) // There are 3 network attempts and 2 insertions (1 request failed)
verify(source, times(3)).pullChaptersFromNetwork(any()); verify(source, times(3)).pullChaptersFromNetwork((String)any());
verify(service.db, times(2)).insertOrRemoveChapters(any(), any(), any()); verify(service.db, times(2)).insertOrRemoveChapters((Manga)any(), anyListOf(Chapter.class), (Source)any());
verify(service.db, never()).insertOrRemoveChapters(eq(manga2), any(), any()); verify(service.db, never()).insertOrRemoveChapters(eq(manga2), anyListOf(Chapter.class), (Source)any());
} }
private List<Chapter> createChapters(String... urls) { private List<Chapter> createChapters(String... urls) {

View File

@ -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);
}
}

View File

@ -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)
}
}