From 1dfb2bd40939fb8585a07c185175545c90b7cfc7 Mon Sep 17 00:00:00 2001 From: Alexandru2909 Date: Mon, 20 Feb 2023 11:14:07 +0200 Subject: [PATCH] Add FLAG_SECURE to SearchDialogFragment --- .../fenix/search/SearchDialogFragment.kt | 5 ++ .../fenix/search/SearchDialogFragmentTest.kt | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/app/src/main/java/org/mozilla/fenix/search/SearchDialogFragment.kt b/app/src/main/java/org/mozilla/fenix/search/SearchDialogFragment.kt index f319460c9b..d245a817e3 100644 --- a/app/src/main/java/org/mozilla/fenix/search/SearchDialogFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/search/SearchDialogFragment.kt @@ -86,6 +86,7 @@ import org.mozilla.fenix.ext.getRectWithScreenLocation import org.mozilla.fenix.ext.increaseTapArea import org.mozilla.fenix.ext.registerForActivityResult import org.mozilla.fenix.ext.requireComponents +import org.mozilla.fenix.ext.secure import org.mozilla.fenix.ext.settings import org.mozilla.fenix.search.awesomebar.AwesomeBarView import org.mozilla.fenix.search.awesomebar.toSearchProviderState @@ -169,6 +170,10 @@ class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler { override fun onBackPressed() { this@SearchDialogFragment.onBackPressed() } + }.apply { + if ((requireActivity() as HomeActivity).browsingModeManager.mode.isPrivate) { + this.secure(requireActivity()) + } } } diff --git a/app/src/test/java/org/mozilla/fenix/search/SearchDialogFragmentTest.kt b/app/src/test/java/org/mozilla/fenix/search/SearchDialogFragmentTest.kt index e2cff7b995..3d04859139 100644 --- a/app/src/test/java/org/mozilla/fenix/search/SearchDialogFragmentTest.kt +++ b/app/src/test/java/org/mozilla/fenix/search/SearchDialogFragmentTest.kt @@ -4,6 +4,7 @@ package org.mozilla.fenix.search +import android.view.WindowManager.LayoutParams import androidx.fragment.app.Fragment import androidx.navigation.NavBackStackEntry import androidx.navigation.NavController @@ -14,20 +15,27 @@ import io.mockk.every import io.mockk.just import io.mockk.mockk import io.mockk.mockkStatic +import io.mockk.spyk import io.mockk.unmockkStatic import io.mockk.verify import mozilla.components.browser.state.search.SearchEngine import mozilla.components.browser.state.state.SearchState import mozilla.components.browser.state.state.searchEngines import mozilla.components.browser.state.state.selectedOrDefaultSearchEngine +import mozilla.components.support.test.robolectric.testContext import org.junit.After +import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Assert.assertSame import org.junit.Before import org.junit.Test +import org.junit.runner.RunWith +import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.components.Components import org.mozilla.fenix.ext.requireComponents +import org.mozilla.fenix.helpers.FenixRobolectricTestRunner +@RunWith(FenixRobolectricTestRunner::class) internal class SearchDialogFragmentTest { private val navController: NavController = mockk() private val fragment = SearchDialogFragment() @@ -153,6 +161,44 @@ internal class SearchDialogFragmentTest { verify { fragment.interactor wasNot Called } } + + @Test + fun `GIVEN app is in private mode WHEN search dialog is created THEN the dialog is secure`() { + val activity: HomeActivity = mockk(relaxed = true) + val fragment = spyk(SearchDialogFragment()) + val layoutParams = LayoutParams() + layoutParams.flags = LayoutParams.FLAG_SECURE + + every { activity.browsingModeManager.mode.isPrivate } returns true + every { activity.window } returns mockk(relaxed = true) { + every { attributes } returns LayoutParams().apply { flags = LayoutParams.FLAG_SECURE } + } + every { fragment.requireActivity() } returns activity + every { fragment.requireContext() } returns testContext + + val dialog = fragment.onCreateDialog(null) + + assertEquals(LayoutParams.FLAG_SECURE, dialog.window?.attributes?.flags!! and LayoutParams.FLAG_SECURE) + } + + @Test + fun `GIVEN app is in normal mode WHEN search dialog is created THEN the dialog is not secure`() { + val activity: HomeActivity = mockk(relaxed = true) + val fragment = spyk(SearchDialogFragment()) + val layoutParams = LayoutParams() + layoutParams.flags = LayoutParams.FLAG_SECURE + + every { activity.browsingModeManager.mode.isPrivate } returns false + every { activity.window } returns mockk(relaxed = true) { + every { attributes } returns LayoutParams().apply { flags = LayoutParams.FLAG_SECURE } + } + every { fragment.requireActivity() } returns activity + every { fragment.requireContext() } returns testContext + + val dialog = fragment.onCreateDialog(null) + + assertEquals(0, dialog.window?.attributes?.flags!! and LayoutParams.FLAG_SECURE) + } } private val fragmentName = SearchDialogFragment::class.java.canonicalName?.substringAfterLast('.')!!