Skip to content

Commit 7d31fdf

Browse files
Merge pull request #200 from MihaiCristianCondrea/codex/add-fontmanager-test-cases
Add unit tests for FontManager
2 parents 7914bd4 + a66e784 commit 7d31fdf

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.d4rk.androidtutorials.java.utils;
2+
3+
import static org.junit.Assert.assertSame;
4+
import static org.mockito.Mockito.inOrder;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.when;
8+
9+
import android.content.Context;
10+
import android.content.SharedPreferences;
11+
import android.graphics.Typeface;
12+
13+
import androidx.core.content.res.ResourcesCompat;
14+
15+
import com.d4rk.androidtutorials.java.R;
16+
17+
import org.junit.Test;
18+
import org.mockito.InOrder;
19+
import org.mockito.MockedStatic;
20+
import org.mockito.Mockito;
21+
22+
import java.util.LinkedHashMap;
23+
import java.util.Map;
24+
25+
public class FontManagerTest {
26+
27+
@Test
28+
public void getMonospaceFont_returnsExpectedTypefaceForStoredValues() {
29+
Context context = mock(Context.class);
30+
when(context.getString(R.string.key_monospace_font)).thenReturn("monospace_font");
31+
32+
Map<String, Integer> values = new LinkedHashMap<>();
33+
values.put("0", R.font.font_audiowide);
34+
values.put("1", R.font.font_fira_code);
35+
values.put("2", R.font.font_jetbrains_mono);
36+
values.put("3", R.font.font_noto_sans_mono);
37+
values.put("4", R.font.font_poppins);
38+
values.put("5", R.font.font_roboto_mono);
39+
values.put("6", R.font.font_google_sans_code);
40+
41+
for (Map.Entry<String, Integer> entry : values.entrySet()) {
42+
SharedPreferences prefs = mock(SharedPreferences.class);
43+
when(prefs.getString("monospace_font", "6")).thenReturn(entry.getKey());
44+
Typeface expectedTypeface = mock(Typeface.class);
45+
46+
try (MockedStatic<ResourcesCompat> resourcesStatic = Mockito.mockStatic(ResourcesCompat.class)) {
47+
resourcesStatic.when(() -> ResourcesCompat.getFont(context, entry.getValue()))
48+
.thenReturn(expectedTypeface);
49+
50+
Typeface result = FontManager.getMonospaceFont(context, prefs);
51+
52+
assertSame("Unexpected typeface for value " + entry.getKey(), expectedTypeface, result);
53+
verify(prefs).getString("monospace_font", "6");
54+
resourcesStatic.verify(() -> ResourcesCompat.getFont(context, entry.getValue()));
55+
}
56+
}
57+
}
58+
59+
@Test
60+
public void getMonospaceFont_removesInvalidPreferenceValueAndReturnsDefaultFont() {
61+
Context context = mock(Context.class);
62+
SharedPreferences prefs = mock(SharedPreferences.class);
63+
SharedPreferences.Editor editor = mock(SharedPreferences.Editor.class);
64+
65+
when(context.getString(R.string.key_monospace_font)).thenReturn("monospace_font");
66+
when(prefs.edit()).thenReturn(editor);
67+
when(editor.remove("monospace_font")).thenReturn(editor);
68+
when(prefs.getString("monospace_font", "6")).thenThrow(new ClassCastException("Expected string"));
69+
70+
Typeface defaultTypeface = mock(Typeface.class);
71+
72+
try (MockedStatic<ResourcesCompat> resourcesStatic = Mockito.mockStatic(ResourcesCompat.class)) {
73+
resourcesStatic.when(() -> ResourcesCompat.getFont(context, R.font.font_google_sans_code))
74+
.thenReturn(defaultTypeface);
75+
76+
Typeface result = FontManager.getMonospaceFont(context, prefs);
77+
78+
assertSame(defaultTypeface, result);
79+
verify(prefs).getString("monospace_font", "6");
80+
verify(prefs).edit();
81+
InOrder inOrder = inOrder(editor);
82+
inOrder.verify(editor).remove("monospace_font");
83+
inOrder.verify(editor).apply();
84+
resourcesStatic.verify(() -> ResourcesCompat.getFont(context, R.font.font_google_sans_code));
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)