|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Element Creations Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial. |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +@file:OptIn(ExperimentalCoroutinesApi::class) |
| 9 | + |
| 10 | +package io.element.android.features.linknewdevice.impl.screens.qrcode |
| 11 | + |
| 12 | +import com.google.common.truth.Truth.assertThat |
| 13 | +import io.element.android.features.linknewdevice.impl.LinkNewMobileHandler |
| 14 | +import io.element.android.libraries.matrix.api.linknewdevice.LinkMobileHandler |
| 15 | +import io.element.android.libraries.matrix.api.linknewdevice.LinkMobileStep |
| 16 | +import io.element.android.libraries.matrix.test.FakeMatrixClient |
| 17 | +import io.element.android.libraries.matrix.test.linknewdevice.FakeLinkMobileHandler |
| 18 | +import io.element.android.tests.testutils.WarmUpRule |
| 19 | +import io.element.android.tests.testutils.lambda.lambdaRecorder |
| 20 | +import io.element.android.tests.testutils.test |
| 21 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 22 | +import kotlinx.coroutines.test.runCurrent |
| 23 | +import kotlinx.coroutines.test.runTest |
| 24 | +import org.junit.Rule |
| 25 | +import org.junit.Test |
| 26 | + |
| 27 | +class ShowQrCodePresenterTest { |
| 28 | + @get:Rule |
| 29 | + val warmUpRule = WarmUpRule() |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `present - initial state`() = runTest { |
| 33 | + createPresenter().test { |
| 34 | + val initialState = awaitItem() |
| 35 | + assertThat(initialState.data1.dataOrNull()).isEqualTo("DATA") |
| 36 | + assertThat(initialState.data2.isUninitialized()).isTrue() |
| 37 | + assertThat(initialState.dataToRender).isEqualTo(1) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `present - when handler emits QrRotating, the presenter requests to rotate the QrCode`() = runTest { |
| 43 | + val linkMobileHandler = FakeLinkMobileHandler( |
| 44 | + startResult = {}, |
| 45 | + ) |
| 46 | + val createLinkMobileHandlerResult = lambdaRecorder<Result<LinkMobileHandler>> { |
| 47 | + Result.success(linkMobileHandler) |
| 48 | + } |
| 49 | + val matrixClient = FakeMatrixClient( |
| 50 | + sessionCoroutineScope = backgroundScope, |
| 51 | + createLinkMobileHandlerResult = createLinkMobileHandlerResult, |
| 52 | + ) |
| 53 | + val linkNewMobileHandler = LinkNewMobileHandler(matrixClient) |
| 54 | + linkNewMobileHandler.createAndStartNewHandler() |
| 55 | + createPresenter( |
| 56 | + linkNewMobileHandler = linkNewMobileHandler, |
| 57 | + ).test { |
| 58 | + awaitItem() |
| 59 | + linkMobileHandler.emitStep( |
| 60 | + LinkMobileStep.QrRotating |
| 61 | + ) |
| 62 | + runCurrent() |
| 63 | + val finalState = awaitItem() |
| 64 | + assertThat(finalState.data2.isLoading()).isTrue() |
| 65 | + assertThat(finalState.dataToRender).isEqualTo(2) |
| 66 | + createLinkMobileHandlerResult.assertions().isCalledExactly(2) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `present - when handler emits QrRotating, the presenter requests to rotate the QrCode and the code is rotated`() = runTest { |
| 72 | + val linkMobileHandler = FakeLinkMobileHandler( |
| 73 | + startResult = {}, |
| 74 | + ) |
| 75 | + val matrixClient = FakeMatrixClient( |
| 76 | + sessionCoroutineScope = backgroundScope, |
| 77 | + createLinkMobileHandlerResult = { Result.success(linkMobileHandler) }, |
| 78 | + ) |
| 79 | + val linkNewMobileHandler = LinkNewMobileHandler(matrixClient) |
| 80 | + linkNewMobileHandler.createAndStartNewHandler() |
| 81 | + createPresenter( |
| 82 | + linkNewMobileHandler = linkNewMobileHandler, |
| 83 | + ).test { |
| 84 | + awaitItem() |
| 85 | + linkMobileHandler.emitStep( |
| 86 | + LinkMobileStep.QrRotating |
| 87 | + ) |
| 88 | + runCurrent() |
| 89 | + linkMobileHandler.emitStep( |
| 90 | + LinkMobileStep.QrReady("DATA2") |
| 91 | + ) |
| 92 | + val finalState = awaitItem() |
| 93 | + assertThat(finalState.data1.dataOrNull()).isEqualTo("DATA") |
| 94 | + assertThat(finalState.data2.dataOrNull()).isEqualTo("DATA2") |
| 95 | + assertThat(finalState.dataToRender).isEqualTo(2) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private fun createPresenter( |
| 100 | + linkNewMobileHandler: LinkNewMobileHandler = LinkNewMobileHandler(FakeMatrixClient()), |
| 101 | + ) = ShowQrCodePresenter( |
| 102 | + initialData = "DATA", |
| 103 | + linkNewMobileHandler = linkNewMobileHandler, |
| 104 | + ) |
| 105 | +} |
0 commit comments