[fenix] Extract TP onboarding popup (https://github.com/mozilla-mobile/fenix/pull/6700)
parent
02b66be3d7
commit
f0969797aa
@ -0,0 +1,89 @@
|
|||||||
|
/* 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.trackingprotection
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.Color
|
||||||
|
import android.graphics.drawable.ColorDrawable
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.View.MeasureSpec
|
||||||
|
import android.view.WindowManager
|
||||||
|
import android.widget.ImageView
|
||||||
|
import android.widget.PopupWindow
|
||||||
|
import kotlinx.android.synthetic.main.tracking_protection_onboarding_popup.view.*
|
||||||
|
import mozilla.components.browser.session.Session
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
import org.mozilla.fenix.ext.getDimenInDip
|
||||||
|
import org.mozilla.fenix.ext.increaseTapArea
|
||||||
|
import org.mozilla.fenix.utils.Settings
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays an overlay above the tracking protection button in the browser toolbar
|
||||||
|
* to onboard the user about tracking protection.
|
||||||
|
*/
|
||||||
|
class TrackingProtectionOverlay(
|
||||||
|
private val context: Context,
|
||||||
|
private val settings: Settings,
|
||||||
|
private val toolbar: View,
|
||||||
|
private val trackingProtectionIcon: View
|
||||||
|
) : Session.Observer {
|
||||||
|
|
||||||
|
override fun onLoadingStateChanged(session: Session, loading: Boolean) {
|
||||||
|
if (!loading && shouldShowTrackingProtectionOnboarding(session)) {
|
||||||
|
showTrackingProtectionOnboarding()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun shouldShowTrackingProtectionOnboarding(session: Session) =
|
||||||
|
settings.shouldShowTrackingProtectionOnboarding &&
|
||||||
|
session.trackerBlockingEnabled &&
|
||||||
|
session.trackersBlocked.isNotEmpty()
|
||||||
|
|
||||||
|
@Suppress("MagicNumber", "InflateParams")
|
||||||
|
private fun showTrackingProtectionOnboarding() {
|
||||||
|
val layout = LayoutInflater.from(context)
|
||||||
|
.inflate(R.layout.tracking_protection_onboarding_popup, null)
|
||||||
|
layout.onboarding_message.text =
|
||||||
|
context.getString(R.string.etp_onboarding_message_2, context.getString(R.string.app_name))
|
||||||
|
|
||||||
|
val trackingOnboarding = PopupWindow(
|
||||||
|
layout,
|
||||||
|
context.resources.getDimensionPixelSize(R.dimen.tp_onboarding_width),
|
||||||
|
WindowManager.LayoutParams.WRAP_CONTENT
|
||||||
|
).apply {
|
||||||
|
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
|
||||||
|
isOutsideTouchable = true
|
||||||
|
isFocusable = true
|
||||||
|
elevation = context.resources.getDimension(R.dimen.mozac_browser_menu_elevation)
|
||||||
|
animationStyle = R.style.Mozac_Browser_Menu_Animation_OverflowMenuBottom
|
||||||
|
}
|
||||||
|
|
||||||
|
val closeButton = layout.findViewById<ImageView>(R.id.close_onboarding)
|
||||||
|
closeButton.increaseTapArea(BUTTON_INCREASE_DPS)
|
||||||
|
closeButton.setOnClickListener {
|
||||||
|
trackingOnboarding.dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Measure layout view
|
||||||
|
val spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
|
||||||
|
layout.measure(spec, spec)
|
||||||
|
|
||||||
|
val containerHeight = layout.measuredHeight
|
||||||
|
val triangleHeight = context.getDimenInDip(R.dimen.tp_onboarding_triangle_height).toInt()
|
||||||
|
|
||||||
|
val xOffset = context.resources.getDimensionPixelSize(R.dimen.tp_onboarding_x_offset)
|
||||||
|
|
||||||
|
// Positioning the popup above the tp anchor.
|
||||||
|
val yOffset = -containerHeight - (toolbar.height / 3 * 2) + triangleHeight
|
||||||
|
|
||||||
|
trackingOnboarding.showAsDropDown(trackingProtectionIcon, xOffset, yOffset)
|
||||||
|
settings.incrementTrackingProtectionOnboardingCount()
|
||||||
|
}
|
||||||
|
|
||||||
|
private companion object {
|
||||||
|
private const val BUTTON_INCREASE_DPS = 12
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
/* 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.trackingprotection
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.View
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.spyk
|
||||||
|
import io.mockk.verify
|
||||||
|
import mozilla.components.browser.session.Session
|
||||||
|
import mozilla.components.support.test.robolectric.testContext
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mozilla.fenix.TestApplication
|
||||||
|
import org.mozilla.fenix.utils.Settings
|
||||||
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
@Config(application = TestApplication::class)
|
||||||
|
class TrackingProtectionOverlayTest {
|
||||||
|
|
||||||
|
private lateinit var context: Context
|
||||||
|
private lateinit var settings: Settings
|
||||||
|
private lateinit var toolbar: View
|
||||||
|
private lateinit var icon: View
|
||||||
|
private lateinit var session: Session
|
||||||
|
private lateinit var overlay: TrackingProtectionOverlay
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
context = spyk(testContext)
|
||||||
|
settings = mockk(relaxed = true)
|
||||||
|
toolbar = mockk(relaxed = true)
|
||||||
|
icon = mockk(relaxed = true)
|
||||||
|
session = mockk(relaxed = true)
|
||||||
|
|
||||||
|
overlay = TrackingProtectionOverlay(context, settings, toolbar, icon)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `no-op when loading`() {
|
||||||
|
every { settings.shouldShowTrackingProtectionOnboarding } returns true
|
||||||
|
every { session.trackerBlockingEnabled } returns true
|
||||||
|
every { session.trackersBlocked } returns listOf(mockk())
|
||||||
|
|
||||||
|
overlay.onLoadingStateChanged(session, loading = true)
|
||||||
|
verify(exactly = 0) { settings.incrementTrackingProtectionOnboardingCount() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `no-op when should not show onboarding`() {
|
||||||
|
every { settings.shouldShowTrackingProtectionOnboarding } returns false
|
||||||
|
|
||||||
|
overlay.onLoadingStateChanged(session, loading = false)
|
||||||
|
verify(exactly = 0) { settings.incrementTrackingProtectionOnboardingCount() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `no-op when tracking protection disabled`() {
|
||||||
|
every { settings.shouldShowTrackingProtectionOnboarding } returns true
|
||||||
|
every { session.trackerBlockingEnabled } returns false
|
||||||
|
|
||||||
|
overlay.onLoadingStateChanged(session, loading = false)
|
||||||
|
verify(exactly = 0) { settings.incrementTrackingProtectionOnboardingCount() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `no-op when no trackers blocked`() {
|
||||||
|
every { settings.shouldShowTrackingProtectionOnboarding } returns true
|
||||||
|
every { session.trackerBlockingEnabled } returns true
|
||||||
|
every { session.trackersBlocked } returns emptyList()
|
||||||
|
|
||||||
|
overlay.onLoadingStateChanged(session, loading = false)
|
||||||
|
verify(exactly = 0) { settings.incrementTrackingProtectionOnboardingCount() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `show onboarding when trackers are blocked`() {
|
||||||
|
every { settings.shouldShowTrackingProtectionOnboarding } returns true
|
||||||
|
every { session.trackerBlockingEnabled } returns true
|
||||||
|
every { session.trackersBlocked } returns listOf(mockk())
|
||||||
|
|
||||||
|
overlay.onLoadingStateChanged(session, loading = false)
|
||||||
|
verify { settings.incrementTrackingProtectionOnboardingCount() }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue