diff --git a/FlowCrypt/src/androidTest/java/com/flowcrypt/email/WkdClientTest.kt b/FlowCrypt/src/androidTest/java/com/flowcrypt/email/WkdClientTest.kt index 74f5f969ee..099bc7b79b 100644 --- a/FlowCrypt/src/androidTest/java/com/flowcrypt/email/WkdClientTest.kt +++ b/FlowCrypt/src/androidTest/java/com/flowcrypt/email/WkdClientTest.kt @@ -16,6 +16,7 @@ import com.flowcrypt.email.extensions.org.bouncycastle.openpgp.armor import com.flowcrypt.email.rules.ClearAppSettingsRule import com.flowcrypt.email.rules.FlowCryptMockWebServerRule import com.flowcrypt.email.rules.GrantPermissionRuleChooser +import com.flowcrypt.email.rules.RetryRule import kotlinx.coroutines.runBlocking import okhttp3.mockwebserver.Dispatcher import okhttp3.mockwebserver.MockResponse @@ -80,7 +81,8 @@ class WkdClientTest { @get:Rule var ruleChain: TestRule = RuleChain - .outerRule(ClearAppSettingsRule()) + .outerRule(RetryRule.DEFAULT) + .around(ClearAppSettingsRule()) .around(GrantPermissionRuleChooser.grant(android.Manifest.permission.POST_NOTIFICATIONS)) .around(mockWebServerRule) diff --git a/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/DraftsGmailAPITestCorrectCreatingAndUpdatingFlowTest.kt b/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/DraftsGmailAPITestCorrectCreatingAndUpdatingFlowTest.kt index 550b12930e..1c9666dffc 100644 --- a/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/DraftsGmailAPITestCorrectCreatingAndUpdatingFlowTest.kt +++ b/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/DraftsGmailAPITestCorrectCreatingAndUpdatingFlowTest.kt @@ -38,7 +38,6 @@ import com.google.api.services.gmail.model.Message import okhttp3.mockwebserver.Dispatcher import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.RecordedRequest -import org.hamcrest.Matchers import org.hamcrest.Matchers.allOf import org.junit.Assert.assertEquals import org.junit.Rule @@ -130,19 +129,7 @@ class DraftsGmailAPITestCorrectCreatingAndUpdatingFlowTest : BaseDraftsGmailAPIF onView(allOf(withId(R.id.recyclerViewMsgs), isDisplayed())).perform( actionOnItemAtPosition(0, click()) ) - waitForObjectWithText(MESSAGE_SUBJECT_FIRST, TimeUnit.SECONDS.toMillis(10)) - - onView(Matchers.allOf(withId(R.id.recyclerViewMessages), isDisplayed())) - .perform( - RecyclerViewActions.scrollTo( - hasDescendant( - allOf( - withId(R.id.textViewSubject), - withText(MESSAGE_SUBJECT_FIRST) - ) - ) - ) - ) + scrollToMessageWithSubject(MESSAGE_SUBJECT_FIRST) //click to edit a draft onView(allOf(withId(R.id.recyclerViewMessages), isDisplayed())) @@ -167,19 +154,9 @@ class DraftsGmailAPITestCorrectCreatingAndUpdatingFlowTest : BaseDraftsGmailAPIF ) //back to the message details screen and check if content was updated + //the thread is reloaded after the edited draft is uploaded pressBack() - waitForObjectWithText(MESSAGE_SUBJECT_FIRST_EDITED, TimeUnit.SECONDS.toMillis(10)) - onView(Matchers.allOf(withId(R.id.recyclerViewMessages), isDisplayed())) - .perform( - RecyclerViewActions.scrollTo( - hasDescendant( - allOf( - withId(R.id.textViewSubject), - withText(MESSAGE_SUBJECT_FIRST_EDITED) - ) - ) - ) - ) + scrollToMessageWithSubject(MESSAGE_SUBJECT_FIRST_EDITED) //back to the messages list screen and check if content was updated pressBack() @@ -192,6 +169,24 @@ class DraftsGmailAPITestCorrectCreatingAndUpdatingFlowTest : BaseDraftsGmailAPIF .check(matches(isDisplayed())) } + /** + * Wait until the thread shows a message with [subject] and scroll to it. Messages are loaded + * asynchronously, and the scroll action fails immediately if the item isn't there yet. + */ + private fun scrollToMessageWithSubject(subject: String) { + val scrollToMessage = RecyclerViewActions.scrollTo( + hasDescendant(allOf(withId(R.id.textViewSubject), withText(subject))) + ) + val messages = { onView(allOf(withId(R.id.recyclerViewMessages), isDisplayed())) } + waitUntil(TimeUnit.SECONDS.toMillis(30)) { + runCatching { + //the default failure handler takes a screenshot on every failed check + messages().withFailureHandler { error, _ -> throw error }.perform(scrollToMessage) + }.isSuccess + } + messages().perform(scrollToMessage) + } + companion object { const val MESSAGE_SUBJECT_FIRST_EDITED = "first edited" } diff --git a/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/base/BaseMessageDetailsFlowTest.kt b/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/base/BaseMessageDetailsFlowTest.kt index aa81064245..07889c2105 100644 --- a/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/base/BaseMessageDetailsFlowTest.kt +++ b/FlowCrypt/src/androidTest/java/com/flowcrypt/email/ui/base/BaseMessageDetailsFlowTest.kt @@ -111,9 +111,12 @@ abstract class BaseMessageDetailsFlowTest : BaseTest() { } protected fun matchReplyButtons(msgEntity: MessageEntity) { - Thread.sleep(1000)//temporary added to complete test. Idling issue - onView(withId(R.id.imageButtonReplyAll)) - .check(matches(isDisplayed())) + //the reply buttons are shown only after the message is parsed + waitUntilViewMatches( + R.id.imageButtonReplyAll, + isDisplayed(), + timeoutMs = TimeUnit.SECONDS.toMillis(30) + ) onView(withId(R.id.replyButton)) .perform(scrollTo()) .check(matches(isDisplayed())) @@ -162,10 +165,9 @@ abstract class BaseMessageDetailsFlowTest : BaseTest() { actionBeforeProceed.invoke() matchHeader(incomingMsgInfo) - Thread.sleep(1000)//temporary added to complete test. Idling issue //checkWebViewText(incomingMsgInfo.text) temporary disabled - onView(withId(R.id.layoutAtt)) - .check(matches(isDisplayed())) + //attachments are shown only after the message is parsed + waitUntilViewMatches(R.id.layoutAtt, isDisplayed(), timeoutMs = TimeUnit.SECONDS.toMillis(30)) matchAtt(att) matchReplyButtons(msgEntity) } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt index 8790fad7c4..8e27ca11ec 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt @@ -113,7 +113,6 @@ class UploadDraftsWorker(context: Context, params: WorkerParameters) : val draftsDir = CacheManager.getDraftDirectory(applicationContext) val directories = draftsDir.listFiles(FileFilter { it.isDirectory }) ?: emptyArray() var attemptsCount = 0 - val setOfThreadToBeAffected = mutableSetOf() while (attemptsCount < MAX_ATTEMPTS_COUNT && FileUtils.listFiles( draftsDir, TrueFileFilter.INSTANCE, @@ -188,31 +187,20 @@ class UploadDraftsWorker(context: Context, params: WorkerParameters) : EXTRA_KEY_STATE to STATE_UPLOAD_COMPLETED ) ) - - existingDraftEntity.threadId?.let { setOfThreadToBeAffected.add(it) } } } attemptsCount++ } - - setProgress( - workDataOf( - EXTRA_KEY_STATE to STATE_COMMON_UPLOAD_COMPLETED, - EXTRA_KEY_THREAD_ID_LIST to setOfThreadToBeAffected.toTypedArray(), - ) - ) } companion object { const val EXTRA_KEY_MESSAGE_UID = "MESSAGE_UID" const val EXTRA_KEY_THREAD_ID = "THREAD_ID" - const val EXTRA_KEY_THREAD_ID_LIST = "THREAD_ID_LIST" const val EXTRA_KEY_STATE = "STATE" const val STATE_UPLOADING = 0 const val STATE_UPLOAD_COMPLETED = 1 - const val STATE_COMMON_UPLOAD_COMPLETED = 2 const val GROUP_UNIQUE_TAG = BuildConfig.APPLICATION_ID + ".UPLOAD_DRAFTS" const val MAX_ATTEMPTS_COUNT = 10 diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/ThreadDetailsFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/ThreadDetailsFragment.kt index 86498e7f9a..bdefd76ae5 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/ThreadDetailsFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/ThreadDetailsFragment.kt @@ -27,6 +27,7 @@ import androidx.navigation.NavDirections import androidx.navigation.fragment.navArgs import androidx.preference.PreferenceManager import androidx.recyclerview.widget.LinearLayoutManager +import androidx.work.WorkInfo import androidx.work.WorkManager import com.flowcrypt.email.Constants import com.flowcrypt.email.R @@ -100,6 +101,7 @@ import com.flowcrypt.email.util.exception.ThreadNotFoundException import com.google.api.client.googleapis.json.GoogleJsonResponseException import org.apache.commons.io.FilenameUtils import java.net.HttpURLConnection +import java.util.UUID import java.util.concurrent.TimeUnit /** @@ -123,6 +125,14 @@ class ThreadDetailsFragment : BaseFragment(), Prog private val args by navArgs() private var isActive: Boolean = false + + /** + * IDs of [UploadDraftsWorker] runs seen in the RUNNING state. WorkManager doesn't deliver every + * progress update: when uploading is fast, the "upload completed" updates can be replaced by the + * next WorkInfo before we see them, and a finished WorkInfo has no progress. So we reload the + * thread when a run we saw running finishes. + */ + private val runningUploadDraftsWorkIds = mutableSetOf() private val recipientsViewModel: RecipientsViewModel by viewModels() private val threadDetailsViewModel: ThreadDetailsViewModel by viewModels { object : CustomAndroidViewModelFactory(requireActivity().application) { @@ -891,6 +901,13 @@ class ThreadDetailsFragment : BaseFragment(), Prog .getWorkInfosForUniqueWorkFlow(UploadDraftsWorker.GROUP_UNIQUE_TAG) .collect { workInfoList -> val workInfo = workInfoList.firstOrNull() ?: return@collect + if (workInfo.state == WorkInfo.State.RUNNING) { + runningUploadDraftsWorkIds.add(workInfo.id) + } else if (workInfo.state.isFinished && runningUploadDraftsWorkIds.remove(workInfo.id)) { + threadDetailsViewModel.loadMessages(silentUpdate = true) + return@collect + } + val messageUID = workInfo.progress.getLong(UploadDraftsWorker.EXTRA_KEY_MESSAGE_UID, -1) val threadId = workInfo.progress.getLong(UploadDraftsWorker.EXTRA_KEY_THREAD_ID, -1) val state = workInfo.progress.getInt(UploadDraftsWorker.EXTRA_KEY_STATE, -1) @@ -925,19 +942,6 @@ class ThreadDetailsFragment : BaseFragment(), Prog delayInMilliseconds = TimeUnit.SECONDS.toMillis(1) ) } - - state == UploadDraftsWorker.STATE_COMMON_UPLOAD_COMPLETED -> { - if (currentThreadId != null) { - val threadIdList = - workInfo.progress.getLongArray(UploadDraftsWorker.EXTRA_KEY_THREAD_ID_LIST) - ?: return@collect - - - if (currentThreadId in threadIdList) { - threadDetailsViewModel.loadMessages(silentUpdate = true) - } - } - } } } } diff --git a/docs/testing-like-ci.md b/docs/testing-like-ci.md index 314e5c93ec..6e6b34daa9 100644 --- a/docs/testing-like-ci.md +++ b/docs/testing-like-ci.md @@ -142,6 +142,7 @@ Look at logcat (`adb logcat -d`) and at the view hierarchy dumps in | A toast instead of the expected dialog after clicking Send | The compose screen is still looking up a recipient's key (`please_wait_while_information_about_recipients_will_be_updated`). | | A click opens another screen | The layout moved between computing the click coordinates and the click (a button appeared or the keyboard opened). | | `Failed to take screenshot`, `Timed out waiting for 5000 ms` | Espresso takes a screenshot on every failed check. In a polling loop each failed check then costs ~5 s. | +| A screen doesn't show the result of a background worker even after a long wait, and the worker logged `Worker result SUCCESS` | WorkManager doesn't deliver every `setProgress()` update: a fast worker's last updates are replaced by the finished `WorkInfo`, which has no progress. Code that reacts only to progress values misses them. React to the finished state too (see `ThreadDetailsFragment.subscribeToDraftsUpdates()`). | | A request returns nothing (`null`, "nothing was thrown") only on CI, and the test takes much longer there | The mock web server does heavy work (e.g. generates a PGP key) while handling the request and exceeds the client's read timeout. Some clients (e.g. `WkdClient`) return `null` on a timeout. | CI keeps `logcat.txt.gz` in the job artifacts (Semaphore → job → Artifacts). It shows the