|
| 1 | +/* |
| 2 | + * Nextcloud Android Common Library |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: MIT |
| 6 | + */ |
| 7 | + |
| 8 | +package com.nextcloud.android.common.core.utils |
| 9 | + |
| 10 | +import android.content.Context |
| 11 | +import android.os.Build |
| 12 | +import androidx.test.core.app.ApplicationProvider |
| 13 | +import com.nextcloud.android.common.core.R |
| 14 | +import org.junit.Assert.assertEquals |
| 15 | +import org.junit.Before |
| 16 | +import org.junit.Test |
| 17 | +import org.junit.runner.RunWith |
| 18 | +import org.robolectric.RobolectricTestRunner |
| 19 | +import org.robolectric.annotation.Config |
| 20 | +import java.text.SimpleDateFormat |
| 21 | +import java.util.Calendar |
| 22 | +import java.util.Locale |
| 23 | + |
| 24 | +/** |
| 25 | + * DateFormatter tests checking the different formats relative/absolute date formatting. |
| 26 | + */ |
| 27 | +@RunWith(RobolectricTestRunner::class) |
| 28 | +@Config(sdk = [Build.VERSION_CODES.Q]) |
| 29 | +class DateFormatterTest { |
| 30 | + private lateinit var dateFormatter: DateFormatter |
| 31 | + private lateinit var context: Context |
| 32 | + |
| 33 | + @Before |
| 34 | + fun setUp() { |
| 35 | + context = ApplicationProvider.getApplicationContext() |
| 36 | + // Force a specific locale to make tests independent of the test environment's locale |
| 37 | + val config = context.resources.configuration |
| 38 | + config.setLocale(Locale.US) |
| 39 | + val localizedContext = context.createConfigurationContext(config) |
| 40 | + dateFormatter = DateFormatter(localizedContext) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `Test 'now' formatting`() { |
| 45 | + val calendar = Calendar.getInstance() |
| 46 | + val formattedDate = dateFormatter.getConditionallyRelativeFormattedTimeSpan(calendar) |
| 47 | + val expected = context.getString(R.string.date_formatting_now) |
| 48 | + assertEquals(expected, formattedDate) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `Test relative minutes formatting`() { |
| 53 | + val calendar = Calendar.getInstance() |
| 54 | + calendar.add(Calendar.MINUTE, -5) |
| 55 | + val formattedDate = dateFormatter.getConditionallyRelativeFormattedTimeSpan(calendar) |
| 56 | + val expected = context.getString(R.string.date_formatting_relative_minutes, 5) |
| 57 | + assertEquals(expected, formattedDate) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `Test relative hours formatting`() { |
| 62 | + val calendar = Calendar.getInstance() |
| 63 | + calendar.add(Calendar.HOUR, -3) |
| 64 | + val formattedDate = dateFormatter.getConditionallyRelativeFormattedTimeSpan(calendar) |
| 65 | + val expected = context.resources.getQuantityString(R.plurals.date_formatting_relative_hours, 3, 3) |
| 66 | + assertEquals(expected, formattedDate) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `Test day of the week formatting`() { |
| 71 | + val calendar = Calendar.getInstance() |
| 72 | + calendar.add(Calendar.DAY_OF_YEAR, -4) |
| 73 | + val sdf = SimpleDateFormat("EEE", Locale.US) |
| 74 | + val expected = sdf.format(calendar.time) |
| 75 | + val formattedDate = dateFormatter.getConditionallyRelativeFormattedTimeSpan(calendar) |
| 76 | + assertEquals(expected, formattedDate) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `Test month and day formatting`() { |
| 81 | + val calendar = Calendar.getInstance() |
| 82 | + calendar.add(Calendar.DAY_OF_YEAR, -100) |
| 83 | + val sdf = SimpleDateFormat("MMM d", Locale.US) |
| 84 | + val expected = sdf.format(calendar.time) |
| 85 | + val formattedDate = dateFormatter.getConditionallyRelativeFormattedTimeSpan(calendar) |
| 86 | + assertEquals(expected, formattedDate) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun `Test full date formatting`() { |
| 91 | + val calendar = Calendar.getInstance() |
| 92 | + calendar.add(Calendar.YEAR, -2) |
| 93 | + val sdf = SimpleDateFormat("MMM d, yyyy", Locale.US) |
| 94 | + val expected = sdf.format(calendar.time) |
| 95 | + val formattedDate = dateFormatter.getConditionallyRelativeFormattedTimeSpan(calendar) |
| 96 | + assertEquals(expected, formattedDate) |
| 97 | + } |
| 98 | +} |
0 commit comments