Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -40,7 +40,6 @@ import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiT
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.Until
import com.flowcrypt.email.BuildConfig
import com.flowcrypt.email.Constants
Expand All @@ -53,6 +52,7 @@ import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.AttachmentEntity
import com.flowcrypt.email.extensions.kotlin.toInputStream
import com.flowcrypt.email.extensions.shutdown
import com.flowcrypt.email.matchers.CustomMatchers.Companion.withChipsBackgroundColor
import com.flowcrypt.email.rules.FlowCryptTestSettingsRule
import com.flowcrypt.email.util.FlavorSettings
import com.flowcrypt.email.util.TestGeneralUtil
Expand Down Expand Up @@ -104,9 +104,13 @@ abstract class BaseTest : BaseActivityTestImplementation {
@Before
fun dismissANRSystemDialogIfExists() {
val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
val waitButton = device.findObject(UiSelector().textContains("wait"))
if (waitButton.exists()) {
//a slow emulator can show "System UI isn't responding" for a few minutes after boot.
//The dialog can appear again right after we dismiss it, so we repeat it a few times
val deadline = SystemClock.uptimeMillis() + TimeUnit.SECONDS.toMillis(60)
while (SystemClock.uptimeMillis() < deadline) {
val waitButton = device.findObject(By.res("android", "aerr_wait")) ?: break
waitButton.click()
device.wait(Until.gone(By.res("android", "aerr_wait")), TimeUnit.SECONDS.toMillis(5))
}

//close the notification bar if needed
Expand Down Expand Up @@ -403,6 +407,47 @@ abstract class BaseTest : BaseActivityTestImplementation {
}
}

/**
* Wait until the recipient chip keeps its final color during [stableForMs].
* The chip is white while the recipient lookup is in progress, and the compose screen
* blocks sending during that time. The lookup can start asynchronously,
* so a single check can pass before it begins.
*/
protected fun waitUntilRecipientChipIsSettled(
email: String,
colorResId: Int,
stableForMs: Long = TimeUnit.SECONDS.toMillis(1),
timeoutMs: Long = TimeUnit.SECONDS.toMillis(30)
) {
val chipMatcher = allOf(
withText(email),
withChipsBackgroundColor(getTargetContext(), colorResId)
)
val startTime = SystemClock.uptimeMillis()
var settledSince: Long? = null
while (true) {
val now = SystemClock.uptimeMillis()
val isSettled = runCatching {
onView(chipMatcher).check(matches(isDisplayed()))
}.isSuccess

if (isSettled) {
val since = settledSince ?: now.also { settledSince = it }
if (now - since >= stableForMs) return
} else {
settledSince = null
}

if (now - startTime >= timeoutMs) {
//fail with a clear Espresso error
onView(chipMatcher).check(matches(isDisplayed()))
return
}

SystemClock.sleep(100)
}
}

protected fun getIdentifierByName(name: String): Int {
return getTargetContext()
.resources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() {
replaceText(TestConstants.RECIPIENT_WITH_PUBLIC_KEY_ON_ATTESTER),
pressImeActionButton()
)
//while the lookup is in progress the screen shows the web portal password button,
//and the layout shifts when it is hidden. Also sending is blocked until the 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))
.check(matches(isDisplayed()))
.perform(scrollTo(), click(), replaceText(EMAIL_SUBJECT))
Expand Down Expand Up @@ -591,12 +597,19 @@ class ComposeScreenFlowTest : BaseComposeScreenTest() {
EmailUtil.genAttInfoFromPubKey(secondKeyDetails, addAccountToDatabaseRule.account.email)

activeActivityRule?.launch(intent)
waitForObjectWithText(
getResString(R.string.prompt_compose_security_email),
TimeUnit.SECONDS.toMillis(10)
)

openActionBarOverflowOrOptionsMenu(getTargetContext())
onView(withText(R.string.include_public_key))
.check(matches(isDisplayed()))
.perform(click())

//keys are loaded asynchronously, the list is hidden until they are ready
waitForObjectWithResourceName("listViewKeys", TimeUnit.SECONDS.toMillis(10))

onData(withPubKeyName(att?.name))
.inAdapterView(withId(R.id.listViewKeys))
.perform(click())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.flowcrypt.email.rules.FlowCryptMockWebServerRule
import com.flowcrypt.email.rules.GrantPermissionRuleChooser
import com.flowcrypt.email.rules.RetryRule
import com.flowcrypt.email.rules.ScreenshotTestRule
import com.flowcrypt.email.ui.adapter.RecipientChipRecyclerViewAdapter
import com.flowcrypt.email.ui.base.BaseDraftsGmailAPIFlowTest
import com.flowcrypt.email.util.TestGeneralUtil
import com.flowcrypt.email.viewaction.ClickOnViewInRecyclerViewItem
Expand All @@ -50,6 +51,7 @@ import org.junit.rules.TestRule
import org.junit.runner.RunWith
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.TimeUnit
import java.util.zip.GZIPInputStream

Expand All @@ -61,7 +63,8 @@ import java.util.zip.GZIPInputStream
@RunWith(AndroidJUnit4::class)
@FlowCryptTestSettings(useCommonIdling = false)
class DraftsGmailAPITestCorrectSendingFlowTest : BaseDraftsGmailAPIFlowTest() {
private val sentCache = mutableListOf<com.google.api.services.gmail.model.Message>()
//it's filled by the mock web server thread and read by the test thread
private val sentCache = CopyOnWriteArrayList<com.google.api.services.gmail.model.Message>()

override val mockWebServerRule: FlowCryptMockWebServerRule = FlowCryptMockWebServerRule(
TestConstants.MOCK_WEB_SERVER_PORT, object : Dispatcher() {
Expand Down Expand Up @@ -186,16 +189,23 @@ class DraftsGmailAPITestCorrectSendingFlowTest : BaseDraftsGmailAPIFlowTest() {
pressImeActionButton()
)

//sending is blocked while the recipient lookup is in progress
waitUntilRecipientChipIsSettled(
email = TestConstants.RECIPIENT_WITH_PUBLIC_KEY_ON_ATTESTER,
colorResId = RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_HAS_USABLE_PUB_KEY
)

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

//need to wait while a message will be sent
waitUntil {
//need to wait while a message will be sent. Right after clicking 'Send' the message may not
//be added to the outbox yet, so an empty outbox alone doesn't mean the message was sent
waitUntil(TimeUnit.SECONDS.toMillis(30)) {
val countOfOutgoingMessages = runBlocking {
roomDatabase.msgDao().getOutboxMsgsSuspend(addAccountToDatabaseRule.account.email).size
}
countOfOutgoingMessages == 0
countOfOutgoingMessages == 0 && sentCache.isNotEmpty()
}

assertEquals(0, runBlocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.flowcrypt.email.ui.base

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.scrollTo
import androidx.test.espresso.assertion.ViewAssertions.matches
Expand Down Expand Up @@ -60,14 +61,16 @@ import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.net.HttpURLConnection
import java.util.Properties
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.TimeUnit

/**
* @author Denys Bondarenko
*/
abstract class BaseComposeGmailFlow(accountEntity: AccountEntity = BASE_ACCOUNT_ENTITY) :
BaseGmailApiTest(accountEntity) {
protected val sentCache = mutableListOf<com.google.api.services.gmail.model.Message>()
//it's filled by the mock web server thread and read by the test thread
protected val sentCache = CopyOnWriteArrayList<com.google.api.services.gmail.model.Message>()

@get:Rule
val outgoingMessageConfigurationRule = OutgoingMessageConfigurationRule()
Expand Down Expand Up @@ -186,6 +189,25 @@ abstract class BaseComposeGmailFlow(accountEntity: AccountEntity = BASE_ACCOUNT_
)
}

/**
* Attach the sender's own public key. [com.flowcrypt.email.ui.activity.fragment.dialog.ChoosePublicKeyDialogFragment]
* loads keys asynchronously and closes itself when there is a single key.
* Until then the dialog has the window focus, so we wait until the compose screen
* is in focus again (its Send action can be found).
*/
protected fun includeOwnPublicKey() {
openActionBarOverflowOrOptionsMenu(getTargetContext())
onView(ViewMatchers.withText(R.string.include_public_key))
.check(matches(isDisplayed()))
.perform(click())

waitUntil(TimeUnit.SECONDS.toMillis(10)) {
runCatching {
onView(withId(R.id.menuActionSend)).check(matches(isDisplayed()))
}.isSuccess
}
}

protected fun extractKeyId(pgpKeyRingDetails: PgpKeyRingDetails): Long {
return PgpKey.parseKeys(pgpKeyRingDetails.publicKey)
.pgpKeyRingCollection
Expand Down Expand Up @@ -281,7 +303,9 @@ abstract class BaseComposeGmailFlow(accountEntity: AccountEntity = BASE_ACCOUNT_
val countOfOutgoingMessages = runBlocking {
roomDatabase.msgDao().getOutboxMsgsSuspend(addAccountToDatabaseRule.account.email).size
}
if (countOfOutgoingMessages == 0) {
//right after clicking 'Send' the message may not be added to the outbox yet,
//so an empty outbox alone doesn't mean the message was sent
if (countOfOutgoingMessages == 0 && sentCache.isNotEmpty()) {
Thread.sleep(TimeUnit.SECONDS.toMillis(1))
break
} else {
Expand All @@ -299,21 +323,23 @@ abstract class BaseComposeGmailFlow(accountEntity: AccountEntity = BASE_ACCOUNT_

//check that we have one message in the server cache and outbox label is not displayed
assertEquals(1, sentCache.size)
onView(withId(R.id.toolbar))
.check(
matches(
not(
hasDescendant(
withText(
getQuantityString(
R.plurals.outbox_msgs_count,
1
)
)
val toolbarWithoutOutboxLabel = matches(
not(
hasDescendant(
withText(
getQuantityString(
R.plurals.outbox_msgs_count,
1
)
)
)
)
)
//the toolbar is updated a bit later than the outbox in the database
waitUntil(TimeUnit.SECONDS.toMillis(10)) {
runCatching { onView(withId(R.id.toolbar)).check(toolbarWithoutOutboxLabel) }.isSuccess
}
onView(withId(R.id.toolbar)).check(toolbarWithoutOutboxLabel)

//check sent MIME message
val rawMime = requireNotNull(sentCache.first().raw)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ package com.flowcrypt.email.ui.gmailapi
* @author Denys Bondarenko
*/
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
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.FlakyTest
import androidx.test.filters.MediumTest
Expand Down Expand Up @@ -82,10 +80,7 @@ class EncryptedWithAttachmentsAndOwnPublicKeyComposeGmailApiFlow : BaseComposeGm
}

//attach a public key
openActionBarOverflowOrOptionsMenu(getTargetContext())
onView(withText(R.string.include_public_key))
.check(matches(isDisplayed()))
.perform(click())
includeOwnPublicKey()

//enqueue outgoing message
onView(withId(R.id.menuActionSend))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
package com.flowcrypt.email.ui.gmailapi

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.replaceText
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
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.FlakyTest
import androidx.test.filters.LargeTest
Expand Down Expand Up @@ -168,10 +166,7 @@ class PasswordProtectedEncryptedComposeGmailApiFlow : BaseComposeGmailFlow() {
}

//attach a public key
openActionBarOverflowOrOptionsMenu(getTargetContext())
onView(withText(R.string.include_public_key))
.check(matches(isDisplayed()))
.perform(click())
includeOwnPublicKey()

//add password-protected logic
onView(withId(R.id.btnSetWebPortalPassword))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ class StandardWithPublicKeyComposeGmailApiFlow : BaseComposeGmailFlow() {
.perform(click())

//attach a public key
openActionBarOverflowOrOptionsMenu(getTargetContext())
onView(withText(R.string.include_public_key))
.check(matches(isDisplayed()))
.perform(click())
includeOwnPublicKey()

//enqueue outgoing message
onView(withId(R.id.menuActionSend))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.matchers.CustomMatchers.Companion.withTextViewDrawable
import com.flowcrypt.email.matchers.TextViewDrawableMatcher
import com.flowcrypt.email.rules.FlowCryptMockWebServerRule
import com.flowcrypt.email.ui.adapter.RecipientChipRecyclerViewAdapter
import com.flowcrypt.email.ui.base.BaseComposeGmailFlow
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
Expand Down Expand Up @@ -70,6 +71,13 @@ open class BaseComposeScreenPasswordProtectedDisallowedTermsTest(

onView(withId(R.id.btSetPassword))
.perform(click())

//returning to the compose screen starts a new lookup for the recipient.
//Sending is blocked while it is in progress
waitUntilRecipientChipIsSettled(
email = TestConstants.RECIPIENT_WITHOUT_PUBLIC_KEY_ON_ATTESTER,
colorResId = RecipientChipRecyclerViewAdapter.CHIP_COLOR_RES_ID_NO_PUB_KEY
)
}

companion object {
Expand Down
Loading
Loading