33import static org .junit .Assert .assertSame ;
44import static org .mockito .Mockito .inOrder ;
55import static org .mockito .Mockito .mock ;
6+ import static org .mockito .Mockito .never ;
67import static org .mockito .Mockito .verify ;
78import static org .mockito .Mockito .when ;
89
2526public 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