From daced89387e5b85024aa26cd303eb13b5e7a51b1 Mon Sep 17 00:00:00 2001 From: mcarare Date: Mon, 28 Sep 2020 12:40:29 +0300 Subject: [PATCH] For #15310: Add test for ActivityNotFoundException when sharing to app. --- .../fenix/share/ShareControllerTest.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) 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 ace841db6..90540b24c 100644 --- a/app/src/test/java/org/mozilla/fenix/share/ShareControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/share/ShareControllerTest.kt @@ -5,6 +5,7 @@ package org.mozilla.fenix.share import android.app.Activity +import android.content.ActivityNotFoundException import android.content.Context import android.content.Intent import androidx.navigation.NavController @@ -141,6 +142,31 @@ class ShareControllerTest { } } + @Test + fun `handleShareToApp should dismiss with an error start when a ActivityNotFoundException occurs`() { + val appPackageName = "package" + val appClassName = "activity" + val appShareOption = AppShareOption("app", mockk(), appPackageName, appClassName) + val shareIntent = slot() + // Our share Intent uses `FLAG_ACTIVITY_NEW_TASK` but when resolving the startActivity call + // needed for capturing the actual Intent used the `slot` one doesn't have this flag so we + // need to use an Activity Context. + val activityContext: Context = mockk() + val testController = DefaultShareController(activityContext, shareSubject, shareData, mockk(), + snackbar, mockk(), mockk(), testCoroutineScope, dismiss) + every { activityContext.startActivity(capture(shareIntent)) } throws ActivityNotFoundException() + every { activityContext.getString(R.string.share_error_snackbar) } returns "Cannot share to this app" + + testController.handleShareToApp(appShareOption) + + verifyOrder { + activityContext.startActivity(shareIntent.captured) + snackbar.setText("Cannot share to this app") + snackbar.show() + dismiss(ShareController.Result.SHARE_ERROR) + } + } + @Test @Suppress("DeferredResultUnused") fun `handleShareToDevice should share to account device, inform callbacks and dismiss`() {