Skip to content

Commit 9bfcd77

Browse files
test: expand DefaultMainRepository unit coverage
1 parent 31e7acb commit 9bfcd77

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

app/src/test/java/com/d4rk/androidtutorials/java/data/repository/DefaultMainRepositoryTest.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNotNull;
56
import static org.junit.Assert.assertSame;
67
import static org.junit.Assert.assertTrue;
78
import static org.mockito.ArgumentMatchers.anyInt;
9+
import static org.mockito.ArgumentMatchers.any;
810
import static org.mockito.ArgumentMatchers.anyString;
911
import static org.mockito.Mockito.mock;
1012
import static org.mockito.Mockito.mockStatic;
@@ -81,19 +83,32 @@ public void tearDown() {
8183
@Test
8284
public void applyThemeSettings_noChangeWhenPreferenceSame() {
8385
defaultPrefs.edit().putString("theme", "light").apply();
84-
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
85-
boolean changed = repository.applyThemeSettings(new String[]{"system", "light", "dark", "auto"});
86-
assertFalse(changed);
87-
assertEquals(AppCompatDelegate.MODE_NIGHT_NO, AppCompatDelegate.getDefaultNightMode());
86+
87+
try (MockedStatic<AppCompatDelegate> appCompatDelegate = mockStatic(AppCompatDelegate.class)) {
88+
appCompatDelegate.when(AppCompatDelegate::getDefaultNightMode).thenReturn(AppCompatDelegate.MODE_NIGHT_NO);
89+
90+
boolean changed = repository.applyThemeSettings(new String[]{"system", "light", "dark", "auto"});
91+
92+
assertFalse(changed);
93+
appCompatDelegate.verify(AppCompatDelegate::getDefaultNightMode);
94+
appCompatDelegate.verifyNoMoreInteractions();
95+
}
8896
}
8997

9098
@Test
9199
public void applyThemeSettings_changesWhenPreferenceDiffers() {
92100
defaultPrefs.edit().putString("theme", "dark").apply();
93-
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
94-
boolean changed = repository.applyThemeSettings(new String[]{"system", "light", "dark", "auto"});
95-
assertTrue(changed);
96-
assertEquals(AppCompatDelegate.MODE_NIGHT_YES, AppCompatDelegate.getDefaultNightMode());
101+
102+
try (MockedStatic<AppCompatDelegate> appCompatDelegate = mockStatic(AppCompatDelegate.class)) {
103+
appCompatDelegate.when(AppCompatDelegate::getDefaultNightMode).thenReturn(AppCompatDelegate.MODE_NIGHT_NO);
104+
105+
boolean changed = repository.applyThemeSettings(new String[]{"system", "light", "dark", "auto"});
106+
107+
assertTrue(changed);
108+
appCompatDelegate.verify(AppCompatDelegate::getDefaultNightMode);
109+
appCompatDelegate.verify(() -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES));
110+
appCompatDelegate.verifyNoMoreInteractions();
111+
}
97112
}
98113

99114
@Test
@@ -104,6 +119,12 @@ public void getBottomNavLabelVisibilityAndDefaultTabPreferenceReturnStoredValues
104119
assertEquals("search", repository.getDefaultTabPreference());
105120
}
106121

122+
@Test
123+
public void getBottomNavLabelVisibilityAndDefaultTabPreferenceReturnDefaultValues() {
124+
assertEquals("never", repository.getBottomNavLabelVisibility());
125+
assertEquals("home", repository.getDefaultTabPreference());
126+
}
127+
107128
@Test
108129
public void startupScreenDefaultsTrueAndFlipsAfterMark() {
109130
assertTrue(repository.shouldShowStartupScreen());
@@ -114,8 +135,24 @@ public void startupScreenDefaultsTrueAndFlipsAfterMark() {
114135
@Test
115136
public void applyLanguageSettings_setsLocales() {
116137
defaultPrefs.edit().putString("language", "fr").apply();
117-
repository.applyLanguageSettings();
118-
assertEquals("fr", AppCompatDelegate.getApplicationLocales().toLanguageTags());
138+
139+
final LocaleListCompat[] capturedLocale = new LocaleListCompat[1];
140+
141+
try (MockedStatic<AppCompatDelegate> appCompatDelegate = mockStatic(AppCompatDelegate.class)) {
142+
appCompatDelegate
143+
.when(() -> AppCompatDelegate.setApplicationLocales(any(LocaleListCompat.class)))
144+
.thenAnswer(invocation -> {
145+
capturedLocale[0] = invocation.getArgument(0);
146+
return null;
147+
});
148+
149+
repository.applyLanguageSettings();
150+
151+
assertNotNull(capturedLocale[0]);
152+
assertEquals("fr", capturedLocale[0].toLanguageTags());
153+
appCompatDelegate.verify(() -> AppCompatDelegate.setApplicationLocales(capturedLocale[0]));
154+
appCompatDelegate.verifyNoMoreInteractions();
155+
}
119156
}
120157

121158
@Test

0 commit comments

Comments
 (0)