From 11417996c82211f0837dac6d19281008e384eba3 Mon Sep 17 00:00:00 2001 From: Roger Yang Date: Thu, 16 Dec 2021 14:33:23 -0500 Subject: [PATCH] [fenix] Closes https://github.com/mozilla-mobile/fenix/issues/22671: Show correct time groups header in history --- .../fenix/library/history/HistoryAdapter.kt | 27 ++- .../library/history/HistoryAdapterTest.kt | 205 ++++++++++++++++++ 2 files changed, 221 insertions(+), 11 deletions(-) create mode 100644 app/src/test/java/org/mozilla/fenix/library/history/HistoryAdapterTest.kt diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt index 5e535de4b0..2e9c1e5d69 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt @@ -5,9 +5,9 @@ package org.mozilla.fenix.library.history import android.content.Context -import android.text.format.DateUtils import android.view.LayoutInflater import android.view.ViewGroup +import androidx.annotation.VisibleForTesting import androidx.paging.PagedListAdapter import androidx.recyclerview.widget.DiffUtil import org.mozilla.fenix.R @@ -87,24 +87,29 @@ class HistoryAdapter( private const val oneDay = 1 private const val sevenDays = 7 private const val thirtyDays = 30 - private val zeroDaysAgo = getDaysAgo(zeroDays).time - private val oneDayAgo = getDaysAgo(oneDay).time + private val today = getDaysAgo(zeroDays).time + private val yesterday = getDaysAgo(oneDay).time private val sevenDaysAgo = getDaysAgo(sevenDays).time private val thirtyDaysAgo = getDaysAgo(thirtyDays).time - private val yesterdayRange = LongRange(oneDayAgo, zeroDaysAgo) - private val lastWeekRange = LongRange(sevenDaysAgo, oneDayAgo) + private val todayRange = LongRange(today, Long.MAX_VALUE) // all future time is considered today + private val yesterdayRange = LongRange(yesterday, today) + private val lastWeekRange = LongRange(sevenDaysAgo, yesterday) private val lastMonthRange = LongRange(thirtyDaysAgo, sevenDaysAgo) private fun getDaysAgo(daysAgo: Int): Date { - val calendar = Calendar.getInstance() - calendar.add(Calendar.DAY_OF_YEAR, -daysAgo) - - return calendar.time + return Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, 0) + set(Calendar.MINUTE, 0) + set(Calendar.SECOND, 0) + set(Calendar.MILLISECOND, 0) + add(Calendar.DAY_OF_YEAR, -daysAgo) + }.time } - private fun timeGroupForHistoryItem(item: History): HistoryItemTimeGroup { + @VisibleForTesting + internal fun timeGroupForHistoryItem(item: History): HistoryItemTimeGroup { return when { - DateUtils.isToday(item.visitedAt) -> HistoryItemTimeGroup.Today + todayRange.contains(item.visitedAt) -> HistoryItemTimeGroup.Today yesterdayRange.contains(item.visitedAt) -> HistoryItemTimeGroup.Yesterday lastWeekRange.contains(item.visitedAt) -> HistoryItemTimeGroup.ThisWeek lastMonthRange.contains(item.visitedAt) -> HistoryItemTimeGroup.ThisMonth diff --git a/app/src/test/java/org/mozilla/fenix/library/history/HistoryAdapterTest.kt b/app/src/test/java/org/mozilla/fenix/library/history/HistoryAdapterTest.kt new file mode 100644 index 0000000000..550f8b6119 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/library/history/HistoryAdapterTest.kt @@ -0,0 +1,205 @@ +/* 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.library.history + +import android.text.format.DateUtils +import org.junit.Assert.assertEquals +import org.junit.Test +import java.util.Calendar + +class HistoryAdapterTest { + + @Test + fun `WHEN grouping history item with future date THEN item is grouped to today`() { + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = System.currentTimeMillis() + DateUtils.WEEK_IN_MILLIS + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.Today, timeGroup) + } + + @Test + fun `WHEN grouping history item with today's date THEN item is grouped to today`() { + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = System.currentTimeMillis() - DateUtils.MINUTE_IN_MILLIS + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.Today, timeGroup) + } + + @Test + fun `WHEN grouping history item with today's midnight date THEN item is grouped to today`() { + val calendar = Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, 0) + set(Calendar.MINUTE, 0) + set(Calendar.SECOND, 0) + } + + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = calendar.timeInMillis + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.Today, timeGroup) + } + + @Test + fun `WHEN grouping history item with yesterday's night date THEN item is grouped to yesterday`() { + val calendar = Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, 0) + set(Calendar.MINUTE, 0) + set(Calendar.SECOND, 0) + } + + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = calendar.timeInMillis - DateUtils.HOUR_IN_MILLIS + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.Yesterday, timeGroup) + } + + @Test + fun `WHEN grouping history item with 23 hours before midnight date THEN item is grouped to yesterday`() { + val calendar = Calendar.getInstance() + calendar.set(Calendar.HOUR_OF_DAY, 0) + calendar.set(Calendar.MINUTE, 0) + calendar.set(Calendar.SECOND, 0) + + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = calendar.timeInMillis - (DateUtils.HOUR_IN_MILLIS * 23) + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.Yesterday, timeGroup) + } + + @Test + fun `WHEN grouping history item with 25 hours before midnight date THEN item is grouped to this week`() { + val calendar = Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, 0) + set(Calendar.MINUTE, 0) + set(Calendar.SECOND, 0) + } + + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = calendar.timeInMillis - (DateUtils.HOUR_IN_MILLIS * 25) + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.ThisWeek, timeGroup) + } + + @Test + fun `WHEN grouping history item with 3 days ago date THEN item is grouped to this week`() { + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = System.currentTimeMillis() - (DateUtils.DAY_IN_MILLIS * 3) + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.ThisWeek, timeGroup) + } + + @Test + fun `WHEN grouping history item with 6 days ago date THEN item is grouped to this week`() { + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = System.currentTimeMillis() - (DateUtils.DAY_IN_MILLIS * 6) + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.ThisWeek, timeGroup) + } + + @Test + fun `WHEN grouping history item with 8 days ago date THEN item is grouped to this month`() { + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = System.currentTimeMillis() - (DateUtils.DAY_IN_MILLIS * 8) + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.ThisMonth, timeGroup) + } + + @Test + fun `WHEN grouping history item with 29 days ago date THEN item is grouped to this month`() { + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = System.currentTimeMillis() - (DateUtils.DAY_IN_MILLIS * 29) + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.ThisMonth, timeGroup) + } + + @Test + fun `WHEN grouping history item with 31 days ago date THEN item is grouped to older`() { + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = System.currentTimeMillis() - (DateUtils.DAY_IN_MILLIS * 31) + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.Older, timeGroup) + } + + @Test + fun `WHEN grouping history item with zero date THEN item is grouped to older`() { + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = 0 + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.Older, timeGroup) + } + + @Test + fun `WHEN grouping history item with negative date THEN item is grouped to older`() { + val history = History.Regular( + id = 1, + title = "test item", + url = "url", + visitedAt = -100 + ) + + val timeGroup = HistoryAdapter.timeGroupForHistoryItem(history as History) + assertEquals(HistoryItemTimeGroup.Older, timeGroup) + } +}