2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-17 15:26:23 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/20440 - Forces TopSitePager bind if no. of pages changed

This commit is contained in:
codrut.topliceanu 2021-07-26 13:33:30 +03:00 committed by mergify[bot]
parent cb0f5facd9
commit 8593bfd0fd
2 changed files with 87 additions and 2 deletions

View File

@ -86,8 +86,15 @@ sealed class AdapterItem(@LayoutRes val viewType: Int) {
* See https://github.com/mozilla-mobile/fenix/pull/20189#issuecomment-877124730 * See https://github.com/mozilla-mobile/fenix/pull/20189#issuecomment-877124730
*/ */
override fun getChangePayload(newItem: AdapterItem): Any? { override fun getChangePayload(newItem: AdapterItem): Any? {
val newTopSites = (newItem as? TopSitePager) ?: return null val newTopSites = (newItem as? TopSitePager)
val oldTopSites = (this as? TopSitePager) ?: return null val oldTopSites = (this as? TopSitePager)
if (newTopSites == null || oldTopSites == null ||
(newTopSites.topSites.size > TopSitePagerViewHolder.TOP_SITES_PER_PAGE)
!= (oldTopSites.topSites.size > TopSitePagerViewHolder.TOP_SITES_PER_PAGE)
) {
return null
}
val changed = mutableSetOf<Pair<Int, TopSite>>() val changed = mutableSetOf<Pair<Int, TopSite>>()

View File

@ -0,0 +1,78 @@
/* 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.sessioncontrol
import io.mockk.every
import io.mockk.mockk
import mozilla.components.feature.top.sites.TopSite
import mozilla.components.feature.top.sites.TopSite.Type.FRECENT
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.mozilla.fenix.home.sessioncontrol.AdapterItem.CollectionItem
import org.mozilla.fenix.home.sessioncontrol.AdapterItem.TopSitePager
import org.mozilla.fenix.home.sessioncontrol.AdapterItem.TopSitePagerPayload
class SessionControlAdapterTest {
@Test
fun `WHEN getChangePayload called with wrong type THEN return null`() {
val newItem: AdapterItem = CollectionItem(mockk(), mockk(relaxed = true))
val result = TopSitePager(mockk()).getChangePayload(newItem)
assertNull(result)
}
@Test
fun `GIVEN topSitePager with 5 topSites WHEN getChangePayload with 10 items THEN return null`() {
val newItem = TopSitePager(mockk(relaxed = true))
val topSitePager = TopSitePager(mockk(relaxed = true))
every { topSitePager.topSites.size } returns 5
every { newItem.topSites.size } returns 10
val result = topSitePager.getChangePayload(newItem)
assertNull(result)
}
@Test
fun `GIVEN topSitePager with 10 topSites WHEN getChangePayload with 5 items THEN return null`() {
val newItem = TopSitePager(mockk(relaxed = true))
val topSitePager = TopSitePager(mockk(relaxed = true))
every { topSitePager.topSites.size } returns 10
every { newItem.topSites.size } returns 5
val result = topSitePager.getChangePayload(newItem)
assertNull(result)
}
@Test
fun `GIVEN two topSites WHEN getChangePayload called with one changed item THEN return TopSitePagerPayload with changes`() {
val topSite0 = TopSite(-1, "topSite0", "", 0, FRECENT)
val topSite1 = TopSite(-1, "topSite1", "", 0, FRECENT)
val topSiteChanged = TopSite(-1, "changed", "", 0, FRECENT)
val topSitePager = TopSitePager(listOf(topSite0, topSite1))
val newItem = TopSitePager(listOf(topSite0, topSiteChanged))
val result = topSitePager.getChangePayload(newItem)
assertEquals(TopSitePagerPayload(setOf(Pair(1, topSiteChanged))), result)
}
@Test
fun `GIVEN two topSites WHEN getChangePayload called with one removed THEN return TopSitePagerPayload with removed item`() {
val topSite0 = TopSite(-1, "topSite0", "", 0, FRECENT)
val topSite1 = TopSite(-1, "topSite1", "", 0, FRECENT)
val topSiteRemoved = TopSite(-1, "REMOVED", "", 0, FRECENT)
val topSitePager = TopSitePager(listOf(topSite0, topSite1))
val newItem = TopSitePager(listOf(topSite0))
val result = topSitePager.getChangePayload(newItem)
assertEquals(TopSitePagerPayload(setOf(Pair(1, topSiteRemoved))), result)
}
}