diff options
author | Mohamed Zenadi <mohamed@zenadi.com> | 2017-07-30 19:37:29 +0100 |
---|---|---|
committer | Mohamed Zenadi <zeapo@users.noreply.github.com> | 2017-08-10 11:10:29 +0200 |
commit | b145dfcf7f20c9d10cd5274e6582edb8a6e6b21c (patch) | |
tree | 5952f6ec043db3101b12d49a6b7444b905f2fda4 /app/src/androidTest/java | |
parent | d347e8349eae4520b4a27fe9239b0b7f25e54446 (diff) |
Add Espresso for testing
Diffstat (limited to 'app/src/androidTest/java')
-rw-r--r-- | app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt b/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt new file mode 100644 index 00000000..52e1e65d --- /dev/null +++ b/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt @@ -0,0 +1,97 @@ +package com.zeapo.pwdstore + +import android.content.Context +import android.content.Intent +import android.support.test.InstrumentationRegistry +import android.support.test.filters.LargeTest +import android.support.test.rule.ActivityTestRule +import android.support.test.runner.AndroidJUnit4 +import android.util.Log +import com.zeapo.pwdstore.crypto.PgpActivity +import kotlinx.android.synthetic.main.decrypt_layout.* +import org.apache.commons.io.IOUtils +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import java.io.File +import java.io.FileOutputStream +import java.io.IOException + + +@RunWith(AndroidJUnit4::class) +@LargeTest +class WelcomeActivityTest { + @Rule @JvmField + var mActivityRule: ActivityTestRule<PgpActivity> = ActivityTestRule(PgpActivity::class.java, true, false) + + @Test + fun pathShouldDecompose() { + val path = "/data/my.app.com/files/store/cat1/name.gpg" + val repoPath = "/data/my.app.com/files/store" + assertEquals("/cat1/name.gpg", PgpActivity.getRelativePath(path, repoPath)) + assertEquals("/cat1/", PgpActivity.getParentPath(path, repoPath)) + assertEquals("name", PgpActivity.getName(path, repoPath)) + assertEquals("name", PgpActivity.getName(path, "$repoPath/")) + } + + @Test + fun activityShouldShowName() { + val context = InstrumentationRegistry.getInstrumentation().targetContext + val name = "name" + val parentPath = "/cat1/" + val repoPath = "${context.filesDir}/store/" + val path = "$repoPath/cat1/name.gpg" + + + val intent = Intent(context, PgpActivity::class.java) + intent.putExtra("OPERATION", "DECRYPT") + intent.putExtra("FILE_PATH", path) + intent.putExtra("REPO_PATH", repoPath) + + copyAssets(context, "store", context.filesDir.absolutePath) + + val activity: PgpActivity = mActivityRule.launchActivity(intent) + + val categoryView = activity.crypto_password_category_decrypt + assertNotNull(categoryView) + assertEquals(parentPath, categoryView.text) + + val nameView = activity.crypto_password_file + assertNotNull(nameView) + assertEquals(name, nameView.text) + } + + companion object { + fun copyAssets(context: Context, source: String, destination: String) { + val assetManager = context.assets + val files: Array<String>? = assetManager.list(source) + + files?.map { filename -> + val destPath = "$destination/$filename" + val sourcePath = "$source/$filename" + if (assetManager.list(filename).isNotEmpty()) { + File(destPath).mkdir() + copyAssets(context, "$source/$filename", destPath) + } else { + try { + val input = assetManager.open(sourcePath) + val outFile = File(destination, filename) + val output = FileOutputStream(outFile) + IOUtils.copy(input, output) + input.close() + output.flush() + output.close() + } catch (e: IOException) { + Log.e("tag", "Failed to copy asset file: " + filename, e) + } + } + } + } + } + +} + + + |