From 73e2f02c4897b6995e7d350f7604f6f878f6cf50 Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Wed, 21 Dec 2022 14:30:32 +0300 Subject: [PATCH 1/4] Increase coverage of devices view model. --- .../devices/v2/DevicesViewModelTest.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt index 4bfd5c4496..524858da77 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/DevicesViewModelTest.kt @@ -53,6 +53,8 @@ import org.matrix.android.sdk.api.session.crypto.crosssigning.DeviceTrustLevel import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel +import org.matrix.android.sdk.api.session.crypto.verification.VerificationTransaction +import org.matrix.android.sdk.api.session.crypto.verification.VerificationTxState import org.matrix.android.sdk.api.session.uia.DefaultBaseAuth private const val A_CURRENT_DEVICE_ID = "current-device-id" @@ -76,6 +78,10 @@ class DevicesViewModelTest { private val fakeVectorPreferences = FakeVectorPreferences() private val toggleIpAddressVisibilityUseCase = mockk() + private val verifiedTransaction = mockk().apply { + every { state } returns VerificationTxState.Verified + } + private fun createViewModel(): DevicesViewModel { return DevicesViewModel( initialState = DevicesViewState(), @@ -375,6 +381,18 @@ class DevicesViewModelTest { viewModelTest.finish() } + @Test + fun `given the view model when a verified transaction is updated then device list is refreshed`() { + // Given + val viewModel = createViewModel() + + // When + viewModel.transactionUpdated(verifiedTransaction) + + // Then + verify { viewModel.refreshDeviceList() } + } + private fun givenCurrentSessionCrossSigningInfo(): CurrentSessionCrossSigningInfo { val currentSessionCrossSigningInfo = mockk() every { currentSessionCrossSigningInfo.deviceId } returns A_CURRENT_DEVICE_ID From fcac1849c39f8ea4fb391932728832c49d538dc6 Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Wed, 21 Dec 2022 17:07:16 +0300 Subject: [PATCH 2/4] Write unit test for toggling ip address visibility. --- .../ToggleIpAddressVisibilityUseCaseTest.kt | 53 +++++++++++++++++++ .../app/test/fakes/FakeVectorPreferences.kt | 8 +++ 2 files changed, 61 insertions(+) create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/ToggleIpAddressVisibilityUseCaseTest.kt diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/ToggleIpAddressVisibilityUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/ToggleIpAddressVisibilityUseCaseTest.kt new file mode 100644 index 0000000000..437e46c175 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/ToggleIpAddressVisibilityUseCaseTest.kt @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.devices.v2 + +import im.vector.app.test.fakes.FakeVectorPreferences +import org.junit.Test + +class ToggleIpAddressVisibilityUseCaseTest { + + private val fakeVectorPreferences = FakeVectorPreferences() + + private val toggleIpAddressVisibilityUseCase = ToggleIpAddressVisibilityUseCase( + vectorPreferences = fakeVectorPreferences.instance, + ) + + @Test + fun `given ip addresses are currently visible then then visibility is set as false`() { + // Given + fakeVectorPreferences.givenShowIpAddressInSessionManagerScreens(true) + + // When + toggleIpAddressVisibilityUseCase.execute() + + // Then + fakeVectorPreferences.verifySetIpAddressVisibilityInDeviceManagerScreens(false) + } + + @Test + fun `given ip addresses are currently not visible then then visibility is set as true`() { +// Given + fakeVectorPreferences.givenShowIpAddressInSessionManagerScreens(false) + + // When + toggleIpAddressVisibilityUseCase.execute() + + // Then + fakeVectorPreferences.verifySetIpAddressVisibilityInDeviceManagerScreens(true) + } +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt index 58bc1a18b8..3d3f415778 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeVectorPreferences.kt @@ -77,4 +77,12 @@ class FakeVectorPreferences { fun givenIsBackgroundSyncEnabled(isEnabled: Boolean) { every { instance.isBackgroundSyncEnabled() } returns isEnabled } + + fun givenShowIpAddressInSessionManagerScreens(show: Boolean) { + every { instance.showIpAddressInSessionManagerScreens() } returns show + } + + fun verifySetIpAddressVisibilityInDeviceManagerScreens(isVisible: Boolean) { + verify { instance.setIpAddressVisibilityInDeviceManagerScreens(isVisible) } + } } From a96f2d5771de60d5d4e78be77094d4b16addba33 Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Thu, 22 Dec 2022 15:39:10 +0300 Subject: [PATCH 3/4] Add changelog. --- changelog.d/7836.misc | 1 + .../clientinfo/DeleteMatrixClientInfoUseCaseTest.kt | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 changelog.d/7836.misc diff --git a/changelog.d/7836.misc b/changelog.d/7836.misc new file mode 100644 index 0000000000..7086e57d5a --- /dev/null +++ b/changelog.d/7836.misc @@ -0,0 +1 @@ +Increase session manager test coverage diff --git a/vector/src/test/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCaseTest.kt b/vector/src/test/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCaseTest.kt index c89bd73979..44ee4d8e9e 100644 --- a/vector/src/test/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/core/session/clientinfo/DeleteMatrixClientInfoUseCaseTest.kt @@ -63,6 +63,11 @@ class DeleteMatrixClientInfoUseCaseTest { // Given val error = Exception() givenSetMatrixClientInfoFails(error) + val expectedClientInfoToBeSet = MatrixClientInfoContent( + name = "", + version = "", + url = "", + ) // When val result = deleteMatrixClientInfoUseCase.execute() @@ -70,6 +75,12 @@ class DeleteMatrixClientInfoUseCaseTest { // Then result.isFailure shouldBe true result.exceptionOrNull() shouldBeEqualTo error + coVerify { + fakeSetMatrixClientInfoUseCase.execute( + fakeActiveSessionHolder.fakeSession, + expectedClientInfoToBeSet + ) + } } private fun givenSetMatrixClientInfoSucceeds() { From 2ddc3b6d05f5e8bc2822c67f0d8f5b5d0a6fc790 Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Fri, 23 Dec 2022 13:40:08 +0300 Subject: [PATCH 4/4] Fix indentation. --- .../settings/devices/v2/ToggleIpAddressVisibilityUseCaseTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/ToggleIpAddressVisibilityUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/ToggleIpAddressVisibilityUseCaseTest.kt index 437e46c175..53dfc707b1 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/ToggleIpAddressVisibilityUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/ToggleIpAddressVisibilityUseCaseTest.kt @@ -41,7 +41,7 @@ class ToggleIpAddressVisibilityUseCaseTest { @Test fun `given ip addresses are currently not visible then then visibility is set as true`() { -// Given + // Given fakeVectorPreferences.givenShowIpAddressInSessionManagerScreens(false) // When