From 64a8932e3cff7013c235b72c695dd1e06c237229 Mon Sep 17 00:00:00 2001 From: mcarare Date: Wed, 15 Dec 2021 15:29:01 +0200 Subject: [PATCH] For #22477: Ignore null and empty values for share data title. --- .../mozilla/fenix/share/ShareController.kt | 4 +- .../fenix/share/ShareControllerTest.kt | 67 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/mozilla/fenix/share/ShareController.kt b/app/src/main/java/org/mozilla/fenix/share/ShareController.kt index 832baa2148..15558c7f87 100644 --- a/app/src/main/java/org/mozilla/fenix/share/ShareController.kt +++ b/app/src/main/java/org/mozilla/fenix/share/ShareController.kt @@ -204,7 +204,9 @@ class DefaultShareController( } @VisibleForTesting - internal fun getShareSubject() = shareSubject ?: shareData.map { it.title }.joinToString(", ") + internal fun getShareSubject() = + shareSubject ?: shareData.filterNot { it.title.isNullOrEmpty() } + .joinToString(", ") { it.title.toString() } // Navigation between app fragments uses ShareTab as arguments. SendTabUseCases uses TabData. @VisibleForTesting diff --git a/app/src/test/java/org/mozilla/fenix/share/ShareControllerTest.kt b/app/src/test/java/org/mozilla/fenix/share/ShareControllerTest.kt index 05a76d21aa..1f4fe807a5 100644 --- a/app/src/test/java/org/mozilla/fenix/share/ShareControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/share/ShareControllerTest.kt @@ -187,6 +187,73 @@ class ShareControllerTest { } } + @Test + fun `getShareSubject should return the shareSubject when shareSubject is not null`() { + val activityContext: Context = mockk() + val testController = DefaultShareController( + activityContext, shareSubject, shareData, mockk(), + mockk(), mockk(), recentAppStorage, testCoroutineScope, testDispatcher, dismiss + ) + + assertEquals(shareSubject, testController.getShareSubject()) + } + + @Test + fun `getShareSubject should return a combination of non-null titles when shareSubject is null`() { + val activityContext: Context = mockk() + val testController = DefaultShareController( + activityContext, null, shareData, mockk(), + mockk(), mockk(), recentAppStorage, testCoroutineScope, testDispatcher, dismiss + ) + + assertEquals("title0, title1", testController.getShareSubject()) + } + + @Test + fun `getShareSubject should return just the not null titles string when shareSubject is null`() { + val activityContext: Context = mockk() + val partialTitlesShareData = listOf( + ShareData(url = "url0", title = null), + ShareData(url = "url1", title = "title1") + ) + val testController = DefaultShareController( + activityContext, null, partialTitlesShareData, mockk(), + mockk(), mockk(), recentAppStorage, testCoroutineScope, testDispatcher, dismiss + ) + + assertEquals("title1", testController.getShareSubject()) + } + + @Test + fun `getShareSubject should return empty string when shareSubject and all titles are null`() { + val activityContext: Context = mockk() + val noTitleShareData = listOf( + ShareData(url = "url0", title = null), + ShareData(url = "url1", title = null) + ) + val testController = DefaultShareController( + activityContext, null, noTitleShareData, mockk(), + mockk(), mockk(), recentAppStorage, testCoroutineScope, testDispatcher, dismiss + ) + + assertEquals("", testController.getShareSubject()) + } + + @Test + fun `getShareSubject should return empty string when shareSubject is null and and all titles are empty`() { + val activityContext: Context = mockk() + val noTitleShareData = listOf( + ShareData(url = "url0", title = ""), + ShareData(url = "url1", title = "") + ) + val testController = DefaultShareController( + activityContext, null, noTitleShareData, mockk(), + mockk(), mockk(), recentAppStorage, testCoroutineScope, testDispatcher, dismiss + ) + + assertEquals("", testController.getShareSubject()) + } + @Test @Suppress("DeferredResultUnused") fun `handleShareToDevice should share to account device, inform callbacks and dismiss`() {