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 091ac078c9..eb4d38ee7f 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 0000000000..73d67e60df --- /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) + } +}