From e2af42a49768fc91423e5e872ca18c222463718b Mon Sep 17 00:00:00 2001 From: "codrut.topliceanu" Date: Mon, 26 Jul 2021 13:33:30 +0300 Subject: [PATCH] For #20440 - Forces TopSitePager bind if no. of pages changed --- .../sessioncontrol/SessionControlAdapter.kt | 11 ++- .../SessionControlAdapterTest.kt | 78 +++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 app/src/test/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapterTest.kt diff --git a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapter.kt b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapter.kt index 091ac078c..eb4d38ee7 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapter.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapter.kt @@ -86,8 +86,15 @@ sealed class AdapterItem(@LayoutRes val viewType: Int) { * See https://github.com/mozilla-mobile/fenix/pull/20189#issuecomment-877124730 */ override fun getChangePayload(newItem: AdapterItem): Any? { - val newTopSites = (newItem as? TopSitePager) ?: return null - val oldTopSites = (this as? TopSitePager) ?: return null + val newTopSites = (newItem as? TopSitePager) + 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>() diff --git a/app/src/test/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapterTest.kt b/app/src/test/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapterTest.kt new file mode 100644 index 000000000..73d67e60d --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapterTest.kt @@ -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) + } +}