Skip to content

Commit d0ac2c9

Browse files
Handle invalid monospace font preferences
1 parent 31e7acb commit d0ac2c9

File tree

2 files changed

+105
-15
lines changed

2 files changed

+105
-15
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/utils/FontManager.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,56 @@
1010

1111
public class FontManager {
1212

13+
private static final String DEFAULT_FONT_CODE = "6";
14+
1315
public static Typeface getMonospaceFont(Context context, SharedPreferences prefs) {
1416
String key = context.getString(R.string.key_monospace_font);
1517
String font;
18+
boolean resetPreference = false;
1619
try {
17-
font = prefs.getString(key, "6");
20+
font = prefs.getString(key, DEFAULT_FONT_CODE);
1821
} catch (ClassCastException e) {
19-
prefs.edit().remove(key).apply();
20-
font = "6";
22+
font = DEFAULT_FONT_CODE;
23+
resetPreference = true;
2124
}
2225
if (font == null) {
23-
font = "6";
26+
font = DEFAULT_FONT_CODE;
27+
resetPreference = true;
28+
}
29+
30+
Typeface typeface;
31+
switch (font) {
32+
case "0":
33+
typeface = ResourcesCompat.getFont(context, R.font.font_audiowide);
34+
break;
35+
case "1":
36+
typeface = ResourcesCompat.getFont(context, R.font.font_fira_code);
37+
break;
38+
case "2":
39+
typeface = ResourcesCompat.getFont(context, R.font.font_jetbrains_mono);
40+
break;
41+
case "3":
42+
typeface = ResourcesCompat.getFont(context, R.font.font_noto_sans_mono);
43+
break;
44+
case "4":
45+
typeface = ResourcesCompat.getFont(context, R.font.font_poppins);
46+
break;
47+
case "5":
48+
typeface = ResourcesCompat.getFont(context, R.font.font_roboto_mono);
49+
break;
50+
case DEFAULT_FONT_CODE:
51+
typeface = ResourcesCompat.getFont(context, R.font.font_google_sans_code);
52+
break;
53+
default:
54+
typeface = ResourcesCompat.getFont(context, R.font.font_google_sans_code);
55+
resetPreference = true;
56+
break;
2457
}
25-
return switch (font) {
26-
case "0" -> ResourcesCompat.getFont(context, R.font.font_audiowide);
27-
case "1" -> ResourcesCompat.getFont(context, R.font.font_fira_code);
28-
case "2" -> ResourcesCompat.getFont(context, R.font.font_jetbrains_mono);
29-
case "3" -> ResourcesCompat.getFont(context, R.font.font_noto_sans_mono);
30-
case "4" -> ResourcesCompat.getFont(context, R.font.font_poppins);
31-
case "5" -> ResourcesCompat.getFont(context, R.font.font_roboto_mono);
32-
default -> ResourcesCompat.getFont(context, R.font.font_google_sans_code);
33-
};
58+
59+
if (resetPreference) {
60+
prefs.edit().remove(key).apply();
61+
}
62+
63+
return typeface;
3464
}
3565
}

app/src/test/java/com/d4rk/androidtutorials/java/utils/FontManagerTest.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertSame;
44
import static org.mockito.Mockito.inOrder;
55
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.never;
67
import static org.mockito.Mockito.verify;
78
import static org.mockito.Mockito.when;
89

@@ -25,7 +26,7 @@
2526
public class FontManagerTest {
2627

2728
@Test
28-
public void getMonospaceFont_returnsExpectedTypefaceForStoredValues() {
29+
public void getMonospaceFont_returnsExpectedTypefaceForValidCodes() {
2930
Context context = mock(Context.class);
3031
when(context.getString(R.string.key_monospace_font)).thenReturn("monospace_font");
3132

@@ -51,13 +52,72 @@ public void getMonospaceFont_returnsExpectedTypefaceForStoredValues() {
5152

5253
assertSame("Unexpected typeface for value " + entry.getKey(), expectedTypeface, result);
5354
verify(prefs).getString("monospace_font", "6");
55+
verify(prefs, never()).edit();
5456
resourcesStatic.verify(() -> ResourcesCompat.getFont(context, entry.getValue()));
5557
}
5658
}
5759
}
5860

5961
@Test
60-
public void getMonospaceFont_removesInvalidPreferenceValueAndReturnsDefaultFont() {
62+
public void getMonospaceFont_resetsPreferenceWhenCodeIsInvalid() {
63+
Context context = mock(Context.class);
64+
SharedPreferences prefs = mock(SharedPreferences.class);
65+
SharedPreferences.Editor editor = mock(SharedPreferences.Editor.class);
66+
67+
when(context.getString(R.string.key_monospace_font)).thenReturn("monospace_font");
68+
when(prefs.edit()).thenReturn(editor);
69+
when(editor.remove("monospace_font")).thenReturn(editor);
70+
when(prefs.getString("monospace_font", "6")).thenReturn("invalid");
71+
72+
Typeface defaultTypeface = mock(Typeface.class);
73+
74+
try (MockedStatic<ResourcesCompat> resourcesStatic = Mockito.mockStatic(ResourcesCompat.class)) {
75+
resourcesStatic.when(() -> ResourcesCompat.getFont(context, R.font.font_google_sans_code))
76+
.thenReturn(defaultTypeface);
77+
78+
Typeface result = FontManager.getMonospaceFont(context, prefs);
79+
80+
assertSame(defaultTypeface, result);
81+
verify(prefs).getString("monospace_font", "6");
82+
verify(prefs).edit();
83+
InOrder inOrder = inOrder(editor);
84+
inOrder.verify(editor).remove("monospace_font");
85+
inOrder.verify(editor).apply();
86+
resourcesStatic.verify(() -> ResourcesCompat.getFont(context, R.font.font_google_sans_code));
87+
}
88+
}
89+
90+
@Test
91+
public void getMonospaceFont_resetsPreferenceWhenCodeIsMissing() {
92+
Context context = mock(Context.class);
93+
SharedPreferences prefs = mock(SharedPreferences.class);
94+
SharedPreferences.Editor editor = mock(SharedPreferences.Editor.class);
95+
96+
when(context.getString(R.string.key_monospace_font)).thenReturn("monospace_font");
97+
when(prefs.edit()).thenReturn(editor);
98+
when(editor.remove("monospace_font")).thenReturn(editor);
99+
when(prefs.getString("monospace_font", "6")).thenReturn(null);
100+
101+
Typeface defaultTypeface = mock(Typeface.class);
102+
103+
try (MockedStatic<ResourcesCompat> resourcesStatic = Mockito.mockStatic(ResourcesCompat.class)) {
104+
resourcesStatic.when(() -> ResourcesCompat.getFont(context, R.font.font_google_sans_code))
105+
.thenReturn(defaultTypeface);
106+
107+
Typeface result = FontManager.getMonospaceFont(context, prefs);
108+
109+
assertSame(defaultTypeface, result);
110+
verify(prefs).getString("monospace_font", "6");
111+
verify(prefs).edit();
112+
InOrder inOrder = inOrder(editor);
113+
inOrder.verify(editor).remove("monospace_font");
114+
inOrder.verify(editor).apply();
115+
resourcesStatic.verify(() -> ResourcesCompat.getFont(context, R.font.font_google_sans_code));
116+
}
117+
}
118+
119+
@Test
120+
public void getMonospaceFont_resetsPreferenceWhenValueHasWrongType() {
61121
Context context = mock(Context.class);
62122
SharedPreferences prefs = mock(SharedPreferences.class);
63123
SharedPreferences.Editor editor = mock(SharedPreferences.Editor.class);

0 commit comments

Comments
 (0)