Skip to content

Commit afbd642

Browse files
Merge pull request #171 from MihaiCristianCondrea/codex/add-defaulthomelocaldatasourcetest-with-mocks
test: add DefaultHomeLocalDataSource tests
2 parents d5dbddb + c6f2989 commit afbd642

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.when;
6+
7+
import android.content.Context;
8+
import android.content.res.Resources;
9+
10+
import com.d4rk.androidtutorials.java.R;
11+
12+
import org.junit.Test;
13+
14+
public class DefaultHomeLocalDataSourceTest {
15+
16+
@Test
17+
public void playStoreUrlsFormattedCorrectly() {
18+
DefaultHomeLocalDataSource dataSource =
19+
new DefaultHomeLocalDataSource(mockContextWithTips(new String[]{"tip"}));
20+
assertEquals("https://play.google.com/store/apps/details?id=com.d4rk.androidtutorials", dataSource.getPlayStoreUrl());
21+
assertEquals("https://play.google.com/store/apps/details?id=pkg", dataSource.getAppPlayStoreUrl("pkg"));
22+
}
23+
24+
@Test
25+
public void dailyTipUsesEpochDayIndex() {
26+
String[] tips = {"tip1", "tip2", "tip3"};
27+
Context context = mockContextWithTips(tips);
28+
DefaultHomeLocalDataSource dataSource = new DefaultHomeLocalDataSource(context);
29+
30+
long daysSinceEpoch = System.currentTimeMillis() / (24L * 60L * 60L * 1000L);
31+
int expectedIndex = (int) (daysSinceEpoch % tips.length);
32+
assertEquals(tips[expectedIndex], dataSource.getDailyTip());
33+
}
34+
35+
private static Context mockContextWithTips(String[] tips) {
36+
Context context = mock(Context.class);
37+
Resources resources = mock(Resources.class);
38+
when(context.getApplicationContext()).thenReturn(context);
39+
when(context.getResources()).thenReturn(resources);
40+
when(resources.getStringArray(R.array.daily_tips)).thenReturn(tips);
41+
return context;
42+
}
43+
}

0 commit comments

Comments
 (0)