Skip to content

Commit

Permalink
Added tests. wip.| #1251
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Jun 7, 2021
1 parent 48d7b1f commit b042875
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"encryptionType": "STANDARD",
"inlineSubject": "Simple encrypted message",
"localFolder": {
"account": "[email protected]",
"attributes": [
"\\HasNoChildren"
],
"folderAlias": "INBOX",
"fullName": "INBOX",
"isCustom": false,
"msgCount": 14
},
"msgBlocks": [
{
"complete": true,
"content": "\n \u003c!DOCTYPE html\u003e\u003chtml\u003e\n \u003chead\u003e\n \u003cmeta name\u003d\"viewport\" content\u003d\"width\u003ddevice-width\" /\u003e\n \u003cstyle\u003e\n body { word-wrap: break-word; word-break: break-word; hyphens: auto; margin-left: 0px; padding-left: 0px; }\n body img { display: inline !important; height: auto !important; max-width: 95% !important; }\n body pre { white-space: pre-wrap !important; }\n body \u003e div.MsgBlock \u003e table { zoom: 75% } /* table layouts tend to overflow - eg emails from fb */\n \u003c/style\u003e\n \u003c/head\u003e\n \u003cbody\u003e\u003c/body\u003e\n \u003c/html\u003e",
"type": "plainHtml"
},
{
"complete": true,
"content": "-----BEGIN PGP MESSAGE-----\nVersion: PGPainless\n\nhF4D16Pe22XLHvsSAQdAfIIpNkCZvyOeQ+WY3ZuqJVaCaGuTj+gafXZsjZEGUBkw\nFEoutq0LAaIoZgTpVdcjFhOuUmfiyy0NLYoenh+45SQcUDFuD6DwO+sb0BeiurqW\nhF4DTxRYvSK3u1MSAQdA6ZqfNCDVMRS4E/jKqY2GZZO1dfaAG8QtAvgRp5yjClsw\nq0nR6a/jIv6JrAjI49xCAqTpLYyKSxyQAMlGgWnLn0Ltpq+4VA30nvQquFuFllK1\n0rUBNuGZH/Nvb44aCpthISzFUxbMlRSTW4ITqfx7Vu3Y7ECn/B4kvSXJx2HN/L47\nqO+fe3DPRAB7u+r8yRgJlSExOu7yodkJuIpm2RKHRk2WLaJilwK84/M6fjmvoO9n\nC9Y9EoJiCLfbXCTx/G0Y5iKvq2338oYd0LA/DHFUZpExkb21hsG37P0DubF/ZeKp\nRL/EpOo+vBKRrPp1nBpOXuiVwIecCnsRHS5GMnndVv1BpigYpDr1\n\u003dfWI0\n-----END PGP MESSAGE-----",
"decryptErr": {
"error": {
"message": "Missing pass phrase",
"type": "need_passphrase"
},
"isEncrypted": true,
"isSuccess": false,
"longids": {
"chosen": [],
"matching": [
"3DEBE9F677D5B9BB38E5A244225F8023C20D0957"
],
"message": [
"D7A3DEDB65CB1EFB",
"4F1458BD22B7BB53"
],
"needPassphrase": [
"3DEBE9F677D5B9BB38E5A244225F8023C20D0957"
]
}
},
"type": "decryptErr"
}
],
"msgEntity": {
"cc": [],
"email": "[email protected]",
"flags": "\\SEEN",
"folder": "INBOX",
"from": [
{
"address": "[email protected]"
}
],
"fromAddress": "[email protected]",
"hasAttachments": false,
"id": 292,
"isSeen": true,
"msgState": "NONE",
"receivedDate": 1619763416000,
"replyTo": "[email protected]",
"replyToAddress": [
{
"address": "[email protected]"
}
],
"sentDate": 1619763414000,
"state": -1,
"subject": "Simple encrypted message",
"to": [
{
"address": "[email protected]"
}
],
"toAddress": "[email protected]",
"uid": 14,
"uidAsHEX": "e"
},
"text": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.flowcrypt.email.api.email.model.AttachmentInfo
import com.flowcrypt.email.api.email.model.IncomingMessageInfo
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.entity.AttachmentEntity
import com.flowcrypt.email.matchers.CustomMatchers.Companion.isToast
import com.flowcrypt.email.ui.activity.base.BaseActivity
import com.flowcrypt.email.util.TestGeneralUtil
import com.google.android.material.snackbar.Snackbar
Expand Down Expand Up @@ -115,15 +116,18 @@ abstract class BaseTest : BaseActivityTestImplementation {
}

/**
* Check is [Toast] displayed. This method can be used only with activity. It doesn't work if a toast is displayed
* when some toast is displayed.
* Check is [Toast] displayed.
*
* @param message A message which was displayed.
* @param delay If we have to check a few toasts one by one
* we need to have some timeout between checking.
*/
protected fun isToastDisplayed(decorView: View?, message: String) {
protected fun isToastDisplayed(message: String, delay: Long? = null) {
onView(withText(message))
.inRoot(withDecorView(not(`is`(decorView))))
.inRoot(isToast())
.check(matches(isDisplayed()))

delay?.let { Thread.sleep(it) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package com.flowcrypt.email.matchers

import android.view.View
import android.widget.ListView
import androidx.test.espresso.Root
import androidx.test.espresso.matcher.BoundedMatcher
import com.flowcrypt.email.api.email.model.SecurityType
import com.flowcrypt.email.ui.widget.PGPContactChipSpan
Expand Down Expand Up @@ -104,5 +105,9 @@ class CustomMatchers {
BoundedMatcher<View, NachoTextView> {
return NachoTextViewChipBackgroundColorMatcher(chipText, backgroundColor)
}

fun isToast(): BaseMatcher<Root?> {
return ToastMatcher()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* © 2016-present FlowCrypt a.s. Limitations apply. Contact [email protected]
* Contributors: DenBond7
*/

package com.flowcrypt.email.matchers

import android.view.WindowManager
import androidx.test.espresso.Root
import androidx.test.espresso.remote.annotation.RemoteMsgConstructor
import org.hamcrest.Description
import org.hamcrest.TypeSafeMatcher

/**
* @author Denis Bondarenko
* Date: 6/7/21
* Time: 11:37 AM
* E-mail: [email protected]
*
* See details here
* https://stackoverflow.com/questions/28390574/checking-toast-message-in-android-espresso
*/
class ToastMatcher @RemoteMsgConstructor constructor() : TypeSafeMatcher<Root?>() {
override fun describeTo(description: Description) {
description.appendText("is toast")
}

@SuppressWarnings("deprecation")
public override fun matchesSafely(root: Root?): Boolean {
val type = root?.windowLayoutParams?.get()?.type
if (type == WindowManager.LayoutParams.TYPE_TOAST) {
val windowToken = root.decorView.windowToken
val appToken = root.decorView.applicationWindowToken
if (windowToken === appToken) { // means this window isn't contained by any other windows.
return true
}
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package com.flowcrypt.email.rules

import com.flowcrypt.email.TestConstants
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.model.KeyImportDetails
import com.flowcrypt.email.security.model.PgpKeyDetails
import com.flowcrypt.email.security.pgp.PgpKey
Expand All @@ -25,22 +26,32 @@ class AddPrivateKeyToDatabaseRule(
val accountEntity: AccountEntity,
val keyPath: String,
val passphrase: String,
val sourceType: KeyImportDetails.SourceType
val sourceType: KeyImportDetails.SourceType,
val passphraseType: KeyEntity.PassphraseType = KeyEntity.PassphraseType.DATABASE
) : BaseRule() {

lateinit var pgpKeyDetails: PgpKeyDetails
private set

constructor() : this(
AccountDaoManager.getDefaultAccountDao(), "pgp/[email protected]_fisrtKey_prv_strong.asc",
TestConstants.DEFAULT_STRONG_PASSWORD, KeyImportDetails.SourceType.EMAIL
constructor(passphraseType: KeyEntity.PassphraseType = KeyEntity.PassphraseType.DATABASE) : this(
accountEntity = AccountDaoManager.getDefaultAccountDao(),
keyPath = "pgp/[email protected]_fisrtKey_prv_strong.asc",
passphrase = TestConstants.DEFAULT_STRONG_PASSWORD,
sourceType = KeyImportDetails.SourceType.EMAIL,
passphraseType = passphraseType
)

override fun apply(base: Statement, description: Description): Statement {
return object : Statement() {
override fun evaluate() {
pgpKeyDetails = PgpKey.parseKeys(context.assets.open(keyPath)).toPgpKeyDetailsList().first()
PrivateKeysManager.saveKeyToDatabase(accountEntity, pgpKeyDetails, passphrase, sourceType)
PrivateKeysManager.saveKeyToDatabase(
accountEntity = accountEntity,
pgpKeyDetails = pgpKeyDetails,
passphrase = passphrase,
sourceType = sourceType,
passphraseType = passphraseType
)
base.evaluate()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class CreateMessageActivityTest : BaseTest() {
onView(withText(R.string.copy_from_other_contact))
.check(matches(isDisplayed()))
.perform(click())
isToastDisplayed(decorView, getResString(R.string.key_successfully_copied))
isToastDisplayed(getResString(R.string.key_successfully_copied))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class ImportPgpContactActivityTest : BaseTest() {
.check(matches(isDisplayed()))
.perform(click())
//due to realization of MockWebServer I can't produce the same response.
isToastDisplayed(decorView, "API error: code = 404, message = ")
isToastDisplayed("API error: code = 404, message = ")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class KeysSettingsActivityTest : BaseTest() {
onView(withId(R.id.btnCopyToClipboard))
.check(matches(isDisplayed()))
.perform(click())
isToastDisplayed(decorView, getResString(R.string.copied))
isToastDisplayed(getResString(R.string.copied))
UiThreadStatement.runOnUiThread { checkClipboardText(details.publicKey) }
}

Expand All @@ -157,7 +157,7 @@ class KeysSettingsActivityTest : BaseTest() {
onView(withId(R.id.btnShowPrKey))
.perform(scrollTo())
.perform(click())
isToastDisplayed(decorView, getResString(R.string.see_backups_to_save_your_private_keys))
isToastDisplayed(getResString(R.string.see_backups_to_save_your_private_keys))
}

@Test
Expand Down Expand Up @@ -257,7 +257,7 @@ class KeysSettingsActivityTest : BaseTest() {
onView(withId(R.id.btnSaveToFile))
.check(matches(isDisplayed()))
.perform(click())
isToastDisplayed(decorView, getResString(R.string.saved))
isToastDisplayed(getResString(R.string.saved))
}

private fun selectFirstKey() {
Expand Down
Loading

0 comments on commit b042875

Please sign in to comment.