-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add instrumentation tests for verifying filestore behaviour
- Loading branch information
1 parent
de0b66f
commit 7505015
Showing
3 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
bugsnag-android-core/src/androidTest/java/com/bugsnag/android/FileStoreTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.bugsnag.android | ||
|
||
import android.content.Context | ||
import androidx.test.core.app.ApplicationProvider | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import java.io.File | ||
import java.io.FileNotFoundException | ||
import java.lang.Exception | ||
import java.lang.RuntimeException | ||
|
||
class FileStoreTest { | ||
val appContext = ApplicationProvider.getApplicationContext<Context>() | ||
val config = Configuration("api-key") | ||
|
||
@Test | ||
fun sendsInternalErrorReport() { | ||
val delegate = CustomDelegate() | ||
val dir = File(appContext.dataDir, "custom-store") | ||
dir.mkdir() | ||
|
||
val store = CustomFileStore(config, appContext, dir.absolutePath, 1, null, delegate) | ||
val exc = RuntimeException("Whoops") | ||
store.write(CustomStreamable(exc)) | ||
|
||
assertEquals("Crash report serialization", delegate.context) | ||
assertEquals(File(dir, "foo.json"), delegate.errorFile) | ||
assertEquals(exc, delegate.exception) | ||
assertEquals(0, dir.listFiles().size) | ||
} | ||
|
||
@Test | ||
fun sendsInternalErrorReportNdk() { | ||
val delegate = CustomDelegate() | ||
val dir = File(appContext.dataDir, "custom-store") | ||
dir.mkdir() | ||
|
||
val store = CustomFileStore(config, appContext, "", 1, null, delegate) | ||
store.enqueueContentForDelivery("foo") | ||
|
||
assertEquals("NDK Crash report copy", delegate.context) | ||
assertEquals(File("/foo.json"), delegate.errorFile) | ||
assertTrue(delegate.exception is FileNotFoundException) | ||
} | ||
} | ||
|
||
class CustomDelegate: FileStore.Delegate { | ||
var exception: Exception? = null | ||
var errorFile: File? = null | ||
var context: String? = null | ||
|
||
override fun onErrorIOFailure(exception: Exception?, errorFile: File?, context: String?) { | ||
this.exception = exception | ||
this.errorFile = errorFile | ||
this.context = context | ||
} | ||
} | ||
|
||
class CustomStreamable(val exc: Throwable) : JsonStream.Streamable { | ||
override fun toStream(stream: JsonStream) = throw exc | ||
} | ||
|
||
internal class CustomFileStore( | ||
config: Configuration, | ||
appContext: Context, | ||
val folder: String?, | ||
maxStoreCount: Int, | ||
comparator: java.util.Comparator<File>?, | ||
delegate: Delegate? | ||
) : FileStore<CustomStreamable>(config, appContext, folder, maxStoreCount, comparator, delegate) { | ||
override fun getFilename(`object`: Any?) = "$folder/foo.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters