1
0
Fork 0

For #4744 - Extract private browsing button (#4813)

master
Tiger Oakes 2019-08-20 13:41:18 -04:00 committed by Sawyer Blatz
parent 090e8006b0
commit ad98b2f3ea
3 changed files with 126 additions and 11 deletions

View File

@ -50,7 +50,6 @@ import org.jetbrains.anko.constraint.layout.ConstraintSetBuilder.Side.START
import org.jetbrains.anko.constraint.layout.ConstraintSetBuilder.Side.TOP
import org.jetbrains.anko.constraint.layout.applyConstraintSet
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.BrowsingMode
import org.mozilla.fenix.FenixViewModelProvider
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
@ -234,22 +233,17 @@ class HomeFragment : Fragment(), AccountObserver {
requireComponents.analytics.metrics.track(Event.SearchBarTapped(Event.SearchBarTapped.Source.HOME))
}
val isPrivate = browsingModeManager.mode.isPrivate
privateBrowsingButton.contentDescription =
contentDescriptionForPrivateBrowsingButton(isPrivate)
privateBrowsingButton.setOnClickListener {
PrivateBrowsingButtonView(
privateBrowsingButton,
browsingModeManager
) { newMode ->
invokePendingDeleteJobs()
val invertedMode = BrowsingMode.fromBoolean(!browsingModeManager.mode.isPrivate)
if (onboarding.userHasBeenOnboarded()) {
getManagedEmitter<SessionControlChange>().onNext(
SessionControlChange.ModeChange(Mode.fromBrowsingMode(invertedMode))
SessionControlChange.ModeChange(Mode.fromBrowsingMode(newMode))
)
}
browsingModeManager.mode = invertedMode
}
// We need the shadow to be above the components.

View File

@ -0,0 +1,48 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.home
import android.view.View
import androidx.annotation.StringRes
import org.mozilla.fenix.BrowsingMode
import org.mozilla.fenix.BrowsingModeManager
import org.mozilla.fenix.R
/**
* Sets up the private browsing toggle button on the [HomeFragment].
*/
class PrivateBrowsingButtonView(
button: View,
private val browsingModeManager: BrowsingModeManager,
private val onClick: (BrowsingMode) -> Unit
) : View.OnClickListener {
init {
button.contentDescription = button.context.getString(getContentDescription(browsingModeManager.mode))
button.setOnClickListener(this)
}
/**
* Calls [onClick] with the new [BrowsingMode] and updates the [browsingModeManager].
*/
override fun onClick(v: View) {
val invertedMode = BrowsingMode.fromBoolean(!browsingModeManager.mode.isPrivate)
onClick(invertedMode)
browsingModeManager.mode = invertedMode
}
companion object {
/**
* Returns the appropriate content description depending on the browsing mode.
*/
@StringRes
private fun getContentDescription(mode: BrowsingMode) = when (mode) {
BrowsingMode.Normal -> R.string.content_description_private_browsing_button
BrowsingMode.Private -> R.string.content_description_disable_private_browsing_button
}
}
}

View File

@ -0,0 +1,73 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.home
import android.view.View
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.mozilla.fenix.BrowsingMode
import org.mozilla.fenix.BrowsingModeManager
import org.mozilla.fenix.R
class PrivateBrowsingButtonViewTest {
private val enable = "Enable private browsing"
private val disable = "Disable private browsing"
private lateinit var button: View
private lateinit var browsingModeManager: BrowsingModeManager
@Before
fun setup() {
button = mockk(relaxed = true)
browsingModeManager = mockk(relaxed = true)
every { button.context.getString(R.string.content_description_private_browsing_button) } returns enable
every { button.context.getString(R.string.content_description_disable_private_browsing_button) } returns disable
every { browsingModeManager.mode } returns BrowsingMode.Normal
}
@Test
fun `constructor sets contentDescription and click listener`() {
val view = PrivateBrowsingButtonView(button, browsingModeManager) {}
verify { button.context.getString(R.string.content_description_private_browsing_button) }
verify { button.contentDescription = enable }
verify { button.setOnClickListener(view) }
every { browsingModeManager.mode } returns BrowsingMode.Private
val privateView = PrivateBrowsingButtonView(button, browsingModeManager) {}
verify { button.context.getString(R.string.content_description_disable_private_browsing_button) }
verify { button.contentDescription = disable }
verify { button.setOnClickListener(privateView) }
}
@Test
fun `click listener calls onClick with inverted mode from normal mode`() {
every { browsingModeManager.mode } returns BrowsingMode.Normal
var mode: BrowsingMode? = null
val view = PrivateBrowsingButtonView(button, browsingModeManager) { mode = it }
view.onClick(button)
assertEquals(BrowsingMode.Private, mode)
verify { browsingModeManager.mode = BrowsingMode.Private }
}
@Test
fun `click listener calls onClick with inverted mode from private mode`() {
every { browsingModeManager.mode } returns BrowsingMode.Private
var mode: BrowsingMode? = null
val view = PrivateBrowsingButtonView(button, browsingModeManager) { mode = it }
view.onClick(button)
assertEquals(BrowsingMode.Normal, mode)
verify { browsingModeManager.mode = BrowsingMode.Normal }
}
}