For #17655: Do not pass threshold if showTopFrecentSites is false. (#17657) (#17813)

* For #17655: Do not pass threshold if showTopFrecentSites is false.

* For #17655: Add unit tests for getTopSitesConfig.

(cherry picked from commit 8993a0acb2)

Co-authored-by: Mihai Adrian Carare <48995920+mcarare@users.noreply.github.com>
pull/293/head
Stefan Arentz 3 years ago committed by GitHub
parent d89d89140c
commit 6518a60480
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,6 +21,7 @@ import android.view.accessibility.AccessibilityEvent
import android.widget.Button
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.content.res.AppCompatResources
import androidx.constraintlayout.widget.ConstraintLayout
@ -273,11 +274,12 @@ class HomeFragment : Fragment() {
* Returns a [TopSitesConfig] which specifies how many top sites to display and whether or
* not frequently visited sites should be displayed.
*/
private fun getTopSitesConfig(): TopSitesConfig {
@VisibleForTesting
internal fun getTopSitesConfig(): TopSitesConfig {
val settings = requireContext().settings()
return TopSitesConfig(
settings.topSitesMaxLimit,
FrecencyThresholdOption.SKIP_ONE_TIME_PAGES
if (settings.showTopFrecentSites) FrecencyThresholdOption.SKIP_ONE_TIME_PAGES else null
)
}

@ -0,0 +1,68 @@
/* 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.content.Context
import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.utils.Settings
@ExperimentalCoroutinesApi
@RunWith(FenixRobolectricTestRunner::class)
class HomeFragmentTest {
private lateinit var settings: Settings
private lateinit var context: Context
private lateinit var homeFragment: HomeFragment
@Before
fun setup() {
context = mockk(relaxed = true)
settings = mockk(relaxed = true)
homeFragment = spyk(HomeFragment())
every { homeFragment.context } returns context
every { context.settings() } returns settings
}
@Test
fun `GIVEN showTopFrecentSites is false WHEN getTopSitesConfig is called THEN it returns TopSitesConfig with null frecencyConfig`() {
every { settings.showTopFrecentSites } returns false
every { settings.topSitesMaxLimit } returns 10
val topSitesConfig = homeFragment.getTopSitesConfig()
Assert.assertNull(topSitesConfig.frecencyConfig)
}
@Test
fun `GIVEN showTopFrecentSites is true WHEN getTopSitesConfig is called THEN it returns TopSitesConfig with non-null frecencyConfig`() {
every { context.settings().showTopFrecentSites } returns true
every { settings.topSitesMaxLimit } returns 10
val topSitesConfig = homeFragment.getTopSitesConfig()
Assert.assertNotNull(topSitesConfig.frecencyConfig)
}
@Test
fun `GIVEN a topSitesMaxLimit WHEN getTopSitesConfig is called THEN it returns TopSitesConfig with totalSites = topSitesMaxLimit`() {
val topSitesMaxLimit = 10
every { settings.topSitesMaxLimit } returns topSitesMaxLimit
val topSitesConfig = homeFragment.getTopSitesConfig()
Assert.assertEquals(topSitesMaxLimit, topSitesConfig.totalSites)
}
}
Loading…
Cancel
Save