From a540d32a4e07d2d9ac32e7466aabd68002268b86 Mon Sep 17 00:00:00 2001 From: Jeff Boek Date: Wed, 6 Feb 2019 10:09:30 -0800 Subject: [PATCH] Fixes #1970 - Guards the close listener --- .../customtabs/CustomTabsToolbarFeature.kt | 3 ++- .../CustomTabsToolbarFeatureTest.kt | 19 ++++++++++++++++++- docs/changelog.md | 3 +++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/components/feature/customtabs/src/main/java/mozilla/components/feature/customtabs/CustomTabsToolbarFeature.kt b/components/feature/customtabs/src/main/java/mozilla/components/feature/customtabs/CustomTabsToolbarFeature.kt index c00e65925c8..b3733575c01 100644 --- a/components/feature/customtabs/src/main/java/mozilla/components/feature/customtabs/CustomTabsToolbarFeature.kt +++ b/components/feature/customtabs/src/main/java/mozilla/components/feature/customtabs/CustomTabsToolbarFeature.kt @@ -135,10 +135,11 @@ class CustomTabsToolbarFeature( */ fun onBackPressed(): Boolean { val result = sessionManager.runWithSession(sessionId) { + closeListener.invoke() remove(it) true } - closeListener.invoke() + return result } diff --git a/components/feature/customtabs/src/test/java/mozilla/components/feature/customtabs/CustomTabsToolbarFeatureTest.kt b/components/feature/customtabs/src/test/java/mozilla/components/feature/customtabs/CustomTabsToolbarFeatureTest.kt index 600a569a80a..6b821a87b2e 100644 --- a/components/feature/customtabs/src/test/java/mozilla/components/feature/customtabs/CustomTabsToolbarFeatureTest.kt +++ b/components/feature/customtabs/src/test/java/mozilla/components/feature/customtabs/CustomTabsToolbarFeatureTest.kt @@ -317,10 +317,27 @@ class CustomTabsToolbarFeatureTest { `when`(sessionManager.findSessionById(anyString())).thenReturn(session) val feature = spy(CustomTabsToolbarFeature(sessionManager, toolbar, sessionId) { closeExecuted = true }) - val result = feature.onBackPressed() assertTrue(result) assertTrue(closeExecuted) } + + @Test + fun `onBackPressed without a session does nothing`() { + val sessionId = null + val session: Session = mock() + val toolbar = BrowserToolbar(RuntimeEnvironment.application) + val sessionManager: SessionManager = mock() + var closeExecuted = false + `when`(session.customTabConfig).thenReturn(mock()) + `when`(sessionManager.findSessionById(anyString())).thenReturn(session) + + val feature = spy(CustomTabsToolbarFeature(sessionManager, toolbar, sessionId) { closeExecuted = true }) + + val result = feature.onBackPressed() + + assertFalse(result) + assertFalse(closeExecuted) + } } diff --git a/docs/changelog.md b/docs/changelog.md index c2ebe9f2823..0bf36a99a97 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -19,6 +19,9 @@ permalink: /changelog/ * **browser-engine-system** * Added support for [JavaScript prompt alerts](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt) on WebView. +* **feature-customtabs** + * Fixed an issue causing the `closeListener` to be invoked even when the current session isn't a Custom Tab. + # 0.41.0 * [Commits](https://github.com/mozilla-mobile/android-components/compare/v0.40.0...v0.41.0)