diff --git a/libraries/matrixui/build.gradle.kts b/libraries/matrixui/build.gradle.kts index ed7f3d068d..0a676e3bca 100644 --- a/libraries/matrixui/build.gradle.kts +++ b/libraries/matrixui/build.gradle.kts @@ -46,8 +46,11 @@ dependencies { ksp(libs.showkase.processor) + testImplementation(libs.coroutines.test) testImplementation(libs.test.junit) - testImplementation(libs.test.truth) testImplementation(libs.test.robolectric) + testImplementation(libs.test.truth) testImplementation(projects.libraries.matrix.test) + testImplementation(projects.libraries.sessionStorage.test) + testImplementation(projects.tests.testutils) } diff --git a/libraries/matrixui/src/test/kotlin/io/element/android/libraries/matrix/ui/media/DefaultImageLoaderHolderTest.kt b/libraries/matrixui/src/test/kotlin/io/element/android/libraries/matrix/ui/media/DefaultImageLoaderHolderTest.kt new file mode 100644 index 0000000000..46a29840b3 --- /dev/null +++ b/libraries/matrixui/src/test/kotlin/io/element/android/libraries/matrix/ui/media/DefaultImageLoaderHolderTest.kt @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2024 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 io.element.android.libraries.matrix.ui.media + +import androidx.test.platform.app.InstrumentationRegistry +import coil.ImageLoader +import com.google.common.truth.Truth.assertThat +import io.element.android.libraries.matrix.api.MatrixClient +import io.element.android.libraries.matrix.test.A_SESSION_ID +import io.element.android.libraries.matrix.test.FakeMatrixClient +import io.element.android.libraries.sessionstorage.test.observer.FakeSessionObserver +import io.element.android.libraries.sessionstorage.test.observer.NoOpSessionObserver +import io.element.android.tests.testutils.lambda.lambdaRecorder +import io.element.android.tests.testutils.lambda.value +import kotlinx.coroutines.test.runTest +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class DefaultImageLoaderHolderTest { + @Test + fun `get - returns the same ImageLoader for the same client`() { + val context = InstrumentationRegistry.getInstrumentation().context + val lambda = lambdaRecorder { ImageLoader.Builder(context).build() } + + val holder = DefaultImageLoaderHolder( + loggedInImageLoaderFactory = FakeLoggedInImageLoaderFactory(lambda), + sessionObserver = NoOpSessionObserver() + ) + val client = FakeMatrixClient() + val imageLoader1 = holder.get(client) + val imageLoader2 = holder.get(client) + assert(imageLoader1 === imageLoader2) + lambda.assertions() + .isCalledOnce() + .with(value(client)) + } + + @Test + fun `when session is deleted, the image loader is deleted`() = runTest { + val context = InstrumentationRegistry.getInstrumentation().context + val lambda = + lambdaRecorder { ImageLoader.Builder(context).build() } + val sessionObserver = FakeSessionObserver() + val holder = DefaultImageLoaderHolder( + loggedInImageLoaderFactory = FakeLoggedInImageLoaderFactory(lambda), + sessionObserver = sessionObserver + ) + assertThat(sessionObserver.listeners.size).isEqualTo(1) + val client = FakeMatrixClient() + holder.get(client) + sessionObserver.onSessionDeleted(client.sessionId.value) + holder.get(client) + lambda.assertions() + .isCalledExactly(2) + } + + @Test + fun `when session is created, nothing happen`() = runTest { + val context = InstrumentationRegistry.getInstrumentation().context + val lambda = + lambdaRecorder { ImageLoader.Builder(context).build() } + val sessionObserver = FakeSessionObserver() + DefaultImageLoaderHolder( + loggedInImageLoaderFactory = FakeLoggedInImageLoaderFactory(lambda), + sessionObserver = sessionObserver + ) + assertThat(sessionObserver.listeners.size).isEqualTo(1) + sessionObserver.onSessionCreated(A_SESSION_ID.value) + } +} diff --git a/libraries/matrixui/src/test/kotlin/io/element/android/libraries/matrix/ui/media/FakeLoggedInImageLoaderFactory.kt b/libraries/matrixui/src/test/kotlin/io/element/android/libraries/matrix/ui/media/FakeLoggedInImageLoaderFactory.kt new file mode 100644 index 0000000000..76ba75c5b9 --- /dev/null +++ b/libraries/matrixui/src/test/kotlin/io/element/android/libraries/matrix/ui/media/FakeLoggedInImageLoaderFactory.kt @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 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 io.element.android.libraries.matrix.ui.media + +import coil.ImageLoader +import io.element.android.libraries.matrix.api.MatrixClient + +class FakeLoggedInImageLoaderFactory( + private val newImageLoaderLambda: (MatrixClient) -> ImageLoader +) : LoggedInImageLoaderFactory { + override fun newImageLoader(matrixClient: MatrixClient): ImageLoader { + return newImageLoaderLambda(matrixClient) + } +} diff --git a/libraries/session-storage/test/src/main/kotlin/io/element/android/libraries/sessionstorage/test/observer/FakeSessionObserver.kt b/libraries/session-storage/test/src/main/kotlin/io/element/android/libraries/sessionstorage/test/observer/FakeSessionObserver.kt new file mode 100644 index 0000000000..71a5b85494 --- /dev/null +++ b/libraries/session-storage/test/src/main/kotlin/io/element/android/libraries/sessionstorage/test/observer/FakeSessionObserver.kt @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 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 io.element.android.libraries.sessionstorage.test.observer + +import io.element.android.libraries.sessionstorage.api.observer.SessionListener +import io.element.android.libraries.sessionstorage.api.observer.SessionObserver + +class FakeSessionObserver : SessionObserver { + private val _listeners = mutableListOf() + + val listeners: List + get() = _listeners + + override fun addListener(listener: SessionListener) { + _listeners.add(listener) + } + + override fun removeListener(listener: SessionListener) { + _listeners.remove(listener) + } + + suspend fun onSessionCreated(userId: String) { + listeners.forEach { it.onSessionCreated(userId) } + } + + suspend fun onSessionDeleted(userId: String) { + listeners.forEach { it.onSessionDeleted(userId) } + } +}