Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class RetryRule(private val retryCount: Int = 0) : BaseRule() {
Thread.sleep(1000 * (times + 1).toLong())
caughtThrowable = it
System.err.println(description.displayName.toString() + ": run ${times + 1} failed")
//without it a failure fixed by a retry is not visible in logcat
it.printStackTrace()
}
}
System.err.println(description.displayName.toString() + ": giving up after " + attempts + " failures")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ import android.graphics.Bitmap
import android.os.Environment
import android.provider.MediaStore
import android.webkit.MimeTypeMap
import androidx.test.core.app.canTakeScreenshot
import androidx.test.core.app.takeScreenshot
import androidx.test.platform.app.InstrumentationRegistry
import com.flowcrypt.email.extensions.kotlin.getPossibleAndroidMimeType
import org.apache.commons.io.FilenameUtils
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import java.io.BufferedOutputStream
import java.io.IOException
import java.util.UUID


Expand All @@ -35,7 +32,12 @@ class ScreenshotTestRule : TestWatcher() {

private fun makeScreenshot(description: Description?) {
try {
if (canTakeScreenshot()) {
//UiAutomation takes a screenshot on the system side. androidx.test takeScreenshot()
//force-redraws the app windows on the main thread. If a window is being destroyed
//(e.g. after a failure), it crashes the app process with NPE in ViewCapture.forceRedraw,
//hides the original failure and stops RetryRule.
val screenshot = InstrumentationRegistry.getInstrumentation().uiAutomation.takeScreenshot()
if (screenshot != null) {
val filename = description?.testClass?.simpleName +
"-" +
description?.methodName +
Expand Down Expand Up @@ -68,11 +70,12 @@ class ScreenshotTestRule : TestWatcher() {
val outputStream = contentResolver.openOutputStream(imageUri)
?: throw IllegalStateException("ContentResolver issue")
BufferedOutputStream(outputStream).use { out ->
takeScreenshot().compress(Bitmap.CompressFormat.PNG, 100, out)
screenshot.compress(Bitmap.CompressFormat.PNG, 100, out)
out.flush()
}
}
} catch (e: IOException) {
} catch (e: Exception) {
//a screenshot is only a debug helper. It must not replace the original failure
e.printStackTrace()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,36 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() {
}

@Test
@Ignore("flaky. Temporary disabled")
fun testEmptyEmailSubject() {
activeActivityRule?.launch(intent)
waitForObjectWithText(
getResString(R.string.prompt_compose_security_email),
TimeUnit.SECONDS.toMillis(10)
)

//typeText() can lose characters on a slow emulator when the IME session restarts
onView(withId(R.id.editTextEmailAddress))
.perform(
typeText(TestConstants.RECIPIENT_WITH_PUBLIC_KEY_ON_ATTESTER),
replaceText(TestConstants.RECIPIENT_WITH_PUBLIC_KEY_ON_ATTESTER),
pressImeActionButton()
)
//sending is blocked with a "please wait" toast until the recipient lookup is done
waitUntilRecipientChipIsSettled(
email = TestConstants.RECIPIENT_WITH_PUBLIC_KEY_ON_ATTESTER,
colorResId = RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_HAS_USABLE_PUB_KEY
)
onView(withId(R.id.editTextEmailSubject))
.perform(scrollTo(), click(), typeText("subject"), clearText())
.perform(scrollTo(), click(), replaceText(""))
.check(matches(withText("")))
Espresso.closeSoftKeyboard()
onView(withId(R.id.menuActionSend))
.check(matches(isDisplayed()))
.perform(click())
onView(
withText(getResString(R.string.text_must_not_be_empty, getResString(R.string.prompt_subject)))
).check(matches(isDisplayed()))

val errorText =
getResString(R.string.text_must_not_be_empty, getResString(R.string.prompt_subject))
waitForObjectWithText(errorText, TimeUnit.SECONDS.toMillis(10))
onView(withText(errorText)).check(matches(isDisplayed()))
}

@Test
Expand Down Expand Up @@ -367,7 +375,6 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() {
}

@Test
@Ignore("Need to fix. Flaky")
fun testSelectImportPublicKeyFromPopUp() {
activeActivityRule?.launch(intent)
intending(hasComponent(ComponentName(getTargetContext(), MainActivity::class.java)))
Expand All @@ -376,70 +383,44 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() {
val email = primaryInternetAddress.address
fillInAllFields(to = setOf(primaryInternetAddress))

onView(withId(R.id.recyclerViewChipsTo))
.perform(
RecyclerViewActions.scrollTo<RecyclerView.ViewHolder>(
allOf(
withText(email),
withChipsBackgroundColor(
getTargetContext(),
RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_NO_PUB_KEY
)
)
)
)
//sending is blocked with a "please wait" toast until the recipient lookup is done
waitUntilRecipientChipIsSettled(
email = email,
colorResId = RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_NO_PUB_KEY
)

onView(withId(R.id.menuActionSend))
.check(matches(isDisplayed()))
.perform(click())

waitForObjectWithText(
getResString(R.string.import_their_public_key),
TimeUnit.SECONDS.toMillis(10)
)
onView(withText(R.string.import_their_public_key))
.check(matches(isDisplayed()))
.perform(click())
var clipboardOk = false

val clipboard =
getTargetContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
repeat(5) { _ ->

waitUntil(TimeUnit.SECONDS.toMillis(10)) {
addTextToClipboard("public key", pgpKeyRingDetails.publicKey)
Thread.sleep(1000)

val clip = clipboard.primaryClip
val actualText = clip
clipboard.primaryClip
?.takeIf { it.itemCount > 0 }
?.getItemAt(0)
?.coerceToText(getTargetContext())
?.toString()

if (actualText == pgpKeyRingDetails.publicKey) {
clipboardOk = true
return@repeat
}
}

if (!clipboardOk) {
throw AssertionError("Clipboard did not contain expected public key after 5 attempts")
?.toString() == pgpKeyRingDetails.publicKey
}

onView(withId(R.id.buttonLoadFromClipboard))
.check(matches(isDisplayed()))
.perform(click())

waitForObjectWithText(email, TimeUnit.SECONDS.toMillis(10))

onView(withId(R.id.recyclerViewChipsTo))
.perform(
RecyclerViewActions.scrollTo<RecyclerView.ViewHolder>(
allOf(
withText(email),
withChipsBackgroundColor(
getTargetContext(),
RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_HAS_USABLE_PUB_KEY
)
)
)
)
//returning to the compose screen starts a new lookup for the recipient
waitUntilRecipientChipIsSettled(
email = email,
colorResId = RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_HAS_USABLE_PUB_KEY
)
}

@Test
Expand All @@ -451,6 +432,7 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() {
requireNotNull(TestConstants.RECIPIENT_WITHOUT_PUBLIC_KEY_ON_ATTESTER.asInternetAddress())
)
)
waitUntilRecipientWithoutPubKeyIsSettled()
onView(withId(R.id.menuActionSend))
.check(matches(isDisplayed()))
.perform(click())
Expand All @@ -464,17 +446,21 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() {
fun testSelectedRemoveRecipientFromPopUp() {
activeActivityRule?.launch(intent)

//typeText() can lose characters on a slow emulator when the IME session restarts
//("Session id mismatch" in logcat), e.g. the recipient became "...@flowcrypt.te"
onView(withId(R.id.editTextEmailAddress))
.perform(
typeText(TestConstants.RECIPIENT_WITHOUT_PUBLIC_KEY_ON_ATTESTER),
replaceText(TestConstants.RECIPIENT_WITHOUT_PUBLIC_KEY_ON_ATTESTER),
pressImeActionButton()
)
//move the focus to the next view
onView(withId(R.id.editTextEmailMessage))
.perform(
scrollTo(), click(),
typeText(TestConstants.RECIPIENT_WITHOUT_PUBLIC_KEY_ON_ATTESTER), closeSoftKeyboard()
replaceText(TestConstants.RECIPIENT_WITHOUT_PUBLIC_KEY_ON_ATTESTER), closeSoftKeyboard()
)
//sending is blocked with a "please wait" toast until the recipient lookup is done
waitUntilRecipientWithoutPubKeyIsSettled()
onView(withId(R.id.menuActionSend))
.check(matches(isDisplayed()))
.perform(click())
Expand Down Expand Up @@ -517,7 +503,7 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() {
)
)

Thread.sleep(2000)
waitUntilRecipientWithoutPubKeyIsSettled()

onView(withId(R.id.menuActionSend))
.check(matches(isDisplayed()))
Expand Down Expand Up @@ -934,6 +920,17 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() {
}
}

/**
* Clicking 'Send' while the recipient lookup is in progress shows a "please wait" toast
* instead of the dialog for a recipient without a public key.
*/
private fun waitUntilRecipientWithoutPubKeyIsSettled() {
waitUntilRecipientChipIsSettled(
email = TestConstants.RECIPIENT_WITHOUT_PUBLIC_KEY_ON_ATTESTER,
colorResId = RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_NO_PUB_KEY
)
}

companion object {

private const val ATTACHMENTS_COUNT = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import java.util.concurrent.TimeUnit

/**
* @author Denys Bondarenko
Expand Down Expand Up @@ -117,8 +118,15 @@ class PrivateKeyDetailsFragmentPassInRamFlexibleTimeFlowTest : BaseTest() {
.check(matches(withText(getResString(R.string.stored_pass_phrase_matched))))
.check(matches(hasTextColor(R.color.colorPrimaryLight)))

//we need to wait around 1 minute to check that [PassPhrasesInRAMService] works well
Thread.sleep(PassPhrasesInRAMService.DELAY_TIMEOUT)
//[PassPhrasesInRAMService] clears expired pass phrases once per DELAY_TIMEOUT, counted from
//the service start, and the screen is updated after that asynchronously. On a slow emulator
//the service can start late, so wait for the result instead of a fixed sleep
waitUntil(PassPhrasesInRAMService.DELAY_TIMEOUT + TimeUnit.SECONDS.toMillis(30)) {
runCatching {
onView(withId(R.id.tVPassPhraseVerification))
.check(matches(withText(getResString(R.string.pass_phrase_not_provided))))
}.isSuccess
}
onView(withId(R.id.tVPassPhraseVerification))
.check(matches(withText(getResString(R.string.pass_phrase_not_provided))))
.check(matches(hasTextColor(R.color.red)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.test.espresso.action.ViewActions.closeSoftKeyboard
import androidx.test.espresso.action.ViewActions.pressImeActionButton
import androidx.test.espresso.action.ViewActions.replaceText
import androidx.test.espresso.action.ViewActions.scrollTo
import androidx.test.espresso.action.ViewActions.typeTextIntoFocusedView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents.intending
import androidx.test.espresso.intent.matcher.IntentMatchers.hasAction
Expand All @@ -34,6 +33,7 @@ import com.flowcrypt.email.rules.lazyActivityScenarioRule
import com.flowcrypt.email.ui.activity.CreateMessageActivity
import com.flowcrypt.email.ui.activity.fragment.CreateMessageFragmentArgs
import com.flowcrypt.email.util.TestGeneralUtil
import com.flowcrypt.email.viewaction.CustomViewActions.insertTextAtCursor
import jakarta.mail.internet.InternetAddress
import org.hamcrest.Matchers.allOf
import java.io.File
Expand Down Expand Up @@ -124,10 +124,12 @@ abstract class BaseComposeScreenTest : BaseTest() {
closeSoftKeyboard()
)

//typing with key events can lose the first characters on a slow emulator while the IME
//reconnects after the focus change (e.g. "message" -> "essage")
onView(withId(R.id.editTextEmailMessage))
.perform(
scrollTo(),
typeTextIntoFocusedView(message),
insertTextAtCursor(message),
closeSoftKeyboard()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@

package com.flowcrypt.email.ui.fragment.isolation.incontainer

import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.closeSoftKeyboard
import androidx.test.espresso.action.ViewActions.pressImeActionButton
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.contrib.RecyclerViewActions.scrollTo
import androidx.test.espresso.action.ViewActions.replaceText
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import com.flowcrypt.email.R
import com.flowcrypt.email.TestConstants
import com.flowcrypt.email.api.retrofit.response.model.ClientConfiguration
import com.flowcrypt.email.base.BaseTest
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.matchers.CustomMatchers.Companion.withChipsBackgroundColor
import com.flowcrypt.email.model.KeyImportDetails
import com.flowcrypt.email.rules.AddAccountToDatabaseRule
import com.flowcrypt.email.rules.AddPrivateKeyToDatabaseRule
Expand All @@ -33,7 +29,6 @@ import com.flowcrypt.email.ui.activity.fragment.CreateMessageFragment
import com.flowcrypt.email.ui.activity.fragment.CreateMessageFragmentArgs
import com.flowcrypt.email.ui.adapter.RecipientChipRecyclerViewAdapter
import com.flowcrypt.email.util.AccountDaoManager
import org.hamcrest.Matchers.allOf
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -90,7 +85,8 @@ class CreateMessageFragmentDisallowAttesterSearchForDomainInIsolationTest : Base
val recipient = "user@$DISALLOWED_DOMAIN"
onView(withId(R.id.editTextEmailAddress))
.perform(
typeText(recipient),
//typeText() can lose characters on a slow emulator when the IME session restarts
replaceText(recipient),
pressImeActionButton(),
closeSoftKeyboard()
)
Expand All @@ -99,27 +95,21 @@ class CreateMessageFragmentDisallowAttesterSearchForDomainInIsolationTest : Base
onView(withId(R.id.editTextEmailSubject))
.perform(
click(),
typeText("subject"),
replaceText("subject"),
closeSoftKeyboard()
)
onView(withId(R.id.editTextEmailMessage))
.perform(
typeText("message"),
replaceText("message"),
closeSoftKeyboard()
)

onView(withId(R.id.recyclerViewChipsTo))
.perform(
scrollTo<RecyclerView.ViewHolder>(
allOf(
withText(recipient),
withChipsBackgroundColor(
getTargetContext(),
RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_NO_PUB_KEY
)
)
)
)
//the chip has the 'updating' color while the lookup is in progress. A single check can run
//before the lookup finishes on a slow emulator
waitUntilRecipientChipIsSettled(
email = recipient,
colorResId = RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_NO_PUB_KEY
)
}

companion object {
Expand Down
Loading
Loading