[fenix] Closes https://github.com/mozilla-mobile/fenix/issues/22671: Show correct time groups header in history

pull/600/head
Roger Yang 3 years ago committed by mergify[bot]
parent ecbd4e36b0
commit 11417996c8

@ -5,9 +5,9 @@
package org.mozilla.fenix.library.history package org.mozilla.fenix.library.history
import android.content.Context import android.content.Context
import android.text.format.DateUtils
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.annotation.VisibleForTesting
import androidx.paging.PagedListAdapter import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import org.mozilla.fenix.R import org.mozilla.fenix.R
@ -87,24 +87,29 @@ class HistoryAdapter(
private const val oneDay = 1 private const val oneDay = 1
private const val sevenDays = 7 private const val sevenDays = 7
private const val thirtyDays = 30 private const val thirtyDays = 30
private val zeroDaysAgo = getDaysAgo(zeroDays).time private val today = getDaysAgo(zeroDays).time
private val oneDayAgo = getDaysAgo(oneDay).time private val yesterday = getDaysAgo(oneDay).time
private val sevenDaysAgo = getDaysAgo(sevenDays).time private val sevenDaysAgo = getDaysAgo(sevenDays).time
private val thirtyDaysAgo = getDaysAgo(thirtyDays).time private val thirtyDaysAgo = getDaysAgo(thirtyDays).time
private val yesterdayRange = LongRange(oneDayAgo, zeroDaysAgo) private val todayRange = LongRange(today, Long.MAX_VALUE) // all future time is considered today
private val lastWeekRange = LongRange(sevenDaysAgo, oneDayAgo) private val yesterdayRange = LongRange(yesterday, today)
private val lastWeekRange = LongRange(sevenDaysAgo, yesterday)
private val lastMonthRange = LongRange(thirtyDaysAgo, sevenDaysAgo) private val lastMonthRange = LongRange(thirtyDaysAgo, sevenDaysAgo)
private fun getDaysAgo(daysAgo: Int): Date { private fun getDaysAgo(daysAgo: Int): Date {
val calendar = Calendar.getInstance() return Calendar.getInstance().apply {
calendar.add(Calendar.DAY_OF_YEAR, -daysAgo) set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
return calendar.time 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 { return when {
DateUtils.isToday(item.visitedAt) -> HistoryItemTimeGroup.Today todayRange.contains(item.visitedAt) -> HistoryItemTimeGroup.Today
yesterdayRange.contains(item.visitedAt) -> HistoryItemTimeGroup.Yesterday yesterdayRange.contains(item.visitedAt) -> HistoryItemTimeGroup.Yesterday
lastWeekRange.contains(item.visitedAt) -> HistoryItemTimeGroup.ThisWeek lastWeekRange.contains(item.visitedAt) -> HistoryItemTimeGroup.ThisWeek
lastMonthRange.contains(item.visitedAt) -> HistoryItemTimeGroup.ThisMonth lastMonthRange.contains(item.visitedAt) -> HistoryItemTimeGroup.ThisMonth

@ -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)
}
}
Loading…
Cancel
Save