From e955e021fd8dd7353748b596eda9d02a3f191e88 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 6 Aug 2019 08:16:37 +0200 Subject: [PATCH] Add a simple instrumentation test for testing on real devices --- .travis.yml | 4 +- app/build.gradle | 4 ++ .../java/com/stevesoltys/backup/Test.kt | 70 +++++++++++++++++++ .../plugins/DocumentsProviderBackupPlugin.kt | 2 +- 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 app/src/androidTest/java/com/stevesoltys/backup/Test.kt diff --git a/.travis.yml b/.travis.yml index dd23f68d..64b5f022 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,8 @@ before_cache: - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ +script: ./gradlew check + cache: directories: - $HOME/.gradle/caches/ @@ -37,4 +39,4 @@ deploy: on: repo: stevesoltys/backup tags: true - skip_cleanup: true \ No newline at end of file + skip_cleanup: true diff --git a/app/build.gradle b/app/build.gradle index cd662b64..88a2f425 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,6 +12,7 @@ android { defaultConfig { minSdkVersion 26 targetSdkVersion 28 + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { @@ -108,4 +109,7 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.0' testImplementation 'io.mockk:mockk:1.9.3' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.0' + + androidTestImplementation 'androidx.test:runner:1.2.0' + androidTestImplementation 'androidx.test:rules:1.2.0' } diff --git a/app/src/androidTest/java/com/stevesoltys/backup/Test.kt b/app/src/androidTest/java/com/stevesoltys/backup/Test.kt new file mode 100644 index 00000000..1dafbfea --- /dev/null +++ b/app/src/androidTest/java/com/stevesoltys/backup/Test.kt @@ -0,0 +1,70 @@ +package com.stevesoltys.backup + +import androidx.documentfile.provider.DocumentFile +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.runner.AndroidJUnit4 +import com.stevesoltys.backup.settings.getBackupFolderUri +import com.stevesoltys.backup.transport.backup.plugins.DocumentsStorage +import com.stevesoltys.backup.transport.backup.plugins.createOrGetFile +import org.junit.After +import org.junit.Assert.assertArrayEquals +import org.junit.Assert.assertNotNull +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import kotlin.random.Random + +private const val filename = "test-file" + +@RunWith(AndroidJUnit4::class) +class AndroidUnitTest { + + private val context = InstrumentationRegistry.getInstrumentation().targetContext + private val folderUri = getBackupFolderUri(context) + private val deviceName = "device name" + private val storage = DocumentsStorage(context, folderUri, deviceName) + + private lateinit var file: DocumentFile + + @Before + fun setup() { + assertNotNull("Select a storage location in the app first!", storage.rootBackupDir) + file = storage.rootBackupDir?.createOrGetFile(filename) + ?: throw RuntimeException("Could not create test file") + } + + @After + fun tearDown() { + file.delete() + } + + @Test + fun testWritingAndReadingFile() { + // write to output stream + val outputStream = storage.getOutputStream(file) + val content = ByteArray(1337).apply { Random.nextBytes(this) } + outputStream.write(content) + outputStream.flush() + outputStream.close() + + // read written data from input stream + val inputStream = storage.getInputStream(file) + val readContent = inputStream.readBytes() + inputStream.close() + assertArrayEquals(content, readContent) + + // write smaller content to same file + val outputStream2 = storage.getOutputStream(file) + val content2 = ByteArray(42).apply { Random.nextBytes(this) } + outputStream2.write(content2) + outputStream2.flush() + outputStream2.close() + + // read written data from input stream + val inputStream2 = storage.getInputStream(file) + val readContent2 = inputStream2.readBytes() + inputStream2.close() + assertArrayEquals(content2, readContent2) + } + +} diff --git a/app/src/main/java/com/stevesoltys/backup/transport/backup/plugins/DocumentsProviderBackupPlugin.kt b/app/src/main/java/com/stevesoltys/backup/transport/backup/plugins/DocumentsProviderBackupPlugin.kt index fe6ee589..e15466c1 100644 --- a/app/src/main/java/com/stevesoltys/backup/transport/backup/plugins/DocumentsProviderBackupPlugin.kt +++ b/app/src/main/java/com/stevesoltys/backup/transport/backup/plugins/DocumentsProviderBackupPlugin.kt @@ -21,7 +21,7 @@ class DocumentsProviderBackupPlugin( @Throws(IOException::class) override fun initializeDevice() { // get or create root backup dir - val rootDir = storage.rootBackupDir ?: throw IOException() + storage.rootBackupDir ?: throw IOException() // create backup folders val kvDir = storage.defaultKvBackupDir