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 @@ -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
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -130,19 +129,7 @@ class DraftsGmailAPITestCorrectCreatingAndUpdatingFlowTest : BaseDraftsGmailAPIF
onView(allOf(withId(R.id.recyclerViewMsgs), isDisplayed())).perform(
actionOnItemAtPosition<ViewHolder>(0, click())
)
waitForObjectWithText(MESSAGE_SUBJECT_FIRST, TimeUnit.SECONDS.toMillis(10))

onView(Matchers.allOf(withId(R.id.recyclerViewMessages), isDisplayed()))
.perform(
RecyclerViewActions.scrollTo<ViewHolder>(
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()))
Expand All @@ -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<ViewHolder>(
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()
Expand All @@ -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<ViewHolder>(
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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long>()
while (attemptsCount < MAX_ATTEMPTS_COUNT && FileUtils.listFiles(
draftsDir,
TrueFileFilter.INSTANCE,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

/**
Expand All @@ -123,6 +125,14 @@ class ThreadDetailsFragment : BaseFragment<FragmentThreadDetailsBinding>(), Prog

private val args by navArgs<ThreadDetailsFragmentArgs>()
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<UUID>()
private val recipientsViewModel: RecipientsViewModel by viewModels()
private val threadDetailsViewModel: ThreadDetailsViewModel by viewModels {
object : CustomAndroidViewModelFactory(requireActivity().application) {
Expand Down Expand Up @@ -891,6 +901,13 @@ class ThreadDetailsFragment : BaseFragment<FragmentThreadDetailsBinding>(), 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)
Expand Down Expand Up @@ -925,19 +942,6 @@ class ThreadDetailsFragment : BaseFragment<FragmentThreadDetailsBinding>(), 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)
}
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/testing-like-ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading