|
| 1 | +package com.d4rk.androidtutorials.java.data.source; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.mockStatic; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | + |
| 8 | +import android.content.Context; |
| 9 | +import android.content.res.Resources; |
| 10 | + |
| 11 | +import com.d4rk.androidtutorials.java.R; |
| 12 | + |
| 13 | +import org.junit.Test; |
| 14 | +import org.mockito.MockedStatic; |
| 15 | + |
| 16 | +public class DefaultHomeLocalDataSourceTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void playStoreUrlsFormattedCorrectly() { |
| 20 | + DefaultHomeLocalDataSource dataSource = |
| 21 | + new DefaultHomeLocalDataSource(mockContextWithTips(new String[]{"tip"})); |
| 22 | + assertEquals("https://play.google.com/store/apps/details?id=com.d4rk.androidtutorials", dataSource.getPlayStoreUrl()); |
| 23 | + assertEquals("https://play.google.com/store/apps/details?id=pkg", dataSource.getAppPlayStoreUrl("pkg")); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void dailyTipUsesEpochDayIndex() { |
| 28 | + Context context = mockContextWithTips(new String[]{"tip1", "tip2", "tip3"}); |
| 29 | + DefaultHomeLocalDataSource dataSource = new DefaultHomeLocalDataSource(context); |
| 30 | + |
| 31 | + long days = 5L; // 5 days since epoch -> index = 2 |
| 32 | + long millis = days * 24L * 60L * 60L * 1000L; |
| 33 | + |
| 34 | + try (MockedStatic<System> mocked = mockStatic(System.class)) { |
| 35 | + mocked.when(System::currentTimeMillis).thenReturn(millis); |
| 36 | + assertEquals("tip3", dataSource.getDailyTip()); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private static Context mockContextWithTips(String[] tips) { |
| 41 | + Context context = mock(Context.class); |
| 42 | + Resources resources = mock(Resources.class); |
| 43 | + when(context.getApplicationContext()).thenReturn(context); |
| 44 | + when(context.getResources()).thenReturn(resources); |
| 45 | + when(resources.getStringArray(R.array.daily_tips)).thenReturn(tips); |
| 46 | + return context; |
| 47 | + } |
| 48 | +} |
0 commit comments