From 5737a5533cfe9c7b2476e666a57ec70a04e32104 Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Fri, 19 Nov 2021 18:35:16 -0500 Subject: [PATCH] [fenix] For issue https://github.com/mozilla-mobile/fenix/issues/5298 the same page appears in the History section multiple times --- .../history/PagedHistoryProvider.kt | 19 ++++- .../history/PagedHistoryProviderTest.kt | 70 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/history/PagedHistoryProvider.kt b/app/src/main/java/org/mozilla/fenix/components/history/PagedHistoryProvider.kt index 4fe9ff26e0..efd7540b2f 100644 --- a/app/src/main/java/org/mozilla/fenix/components/history/PagedHistoryProvider.kt +++ b/app/src/main/java/org/mozilla/fenix/components/history/PagedHistoryProvider.kt @@ -4,6 +4,7 @@ package org.mozilla.fenix.components.history +import androidx.annotation.VisibleForTesting import mozilla.components.browser.storage.sync.PlacesHistoryStorage import mozilla.components.concept.storage.VisitInfo import mozilla.components.concept.storage.VisitType @@ -203,7 +204,8 @@ class DefaultPagedHistoryProvider( } ) - return result.sortedByDescending { it.visitedAt } + return result.removeConsecutiveDuplicates() + .sortedByDescending { it.visitedAt } } private fun transformVisitInfoToHistoryItem(offset: Int): (id: Int, visit: VisitInfo) -> History.Regular { @@ -221,3 +223,18 @@ class DefaultPagedHistoryProvider( } } } + +@VisibleForTesting +internal fun List.removeConsecutiveDuplicates(): List { + var previousURL = "" + return filter { + var isNotDuplicate = true + previousURL = if (it is History.Regular) { + isNotDuplicate = it.url != previousURL + it.url + } else { + "" + } + isNotDuplicate + } +} diff --git a/app/src/test/java/org/mozilla/fenix/components/history/PagedHistoryProviderTest.kt b/app/src/test/java/org/mozilla/fenix/components/history/PagedHistoryProviderTest.kt index 51e60fbc28..81a5742a75 100644 --- a/app/src/test/java/org/mozilla/fenix/components/history/PagedHistoryProviderTest.kt +++ b/app/src/test/java/org/mozilla/fenix/components/history/PagedHistoryProviderTest.kt @@ -452,4 +452,74 @@ class PagedHistoryProviderTest { ) assertEquals(results, actualResults) } + + @Test + fun `WHEN removeConsecutiveDuplicates is called THEN all consecutive duplicates must be removed`() { + val results = listOf( + History.Group( + id = 1, + title = "Group 1", + visitedAt = 0, + items = emptyList() + ), + History.Regular( + id = 2, + title = "No duplicate item", + url = "url", + visitedAt = 0 + ), + History.Regular( + id = 3, + title = "Duplicate item 1", + url = "url", + visitedAt = 0 + ), + History.Regular( + id = 4, + title = "Duplicate item 2", + url = "url", + visitedAt = 0 + ), + History.Group( + id = 5, + title = "Group 5", + visitedAt = 0, + items = emptyList() + ), + History.Regular( + id = 6, + title = "No duplicate item", + url = "url", + visitedAt = 0 + ), + ).removeConsecutiveDuplicates() + + val expectedList = listOf( + History.Group( + id = 1, + title = "Group 1", + visitedAt = 0, + items = emptyList() + ), + History.Regular( + id = 2, + title = "No duplicate item", + url = "url", + visitedAt = 0 + ), + History.Group( + id = 5, + title = "Group 5", + visitedAt = 0, + items = emptyList() + ), + History.Regular( + id = 6, + title = "No duplicate item", + url = "url", + visitedAt = 0 + ), + ) + assertEquals(expectedList, results) + } }