|
| 1 | +package com.d4rk.androidtutorials.java.data.repository; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertSame; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 8 | +import static org.mockito.ArgumentMatchers.anyString; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.mockStatic; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | + |
| 13 | +import android.content.Context; |
| 14 | +import android.content.SharedPreferences; |
| 15 | + |
| 16 | +import androidx.appcompat.app.AppCompatDelegate; |
| 17 | +import androidx.core.os.LocaleListCompat; |
| 18 | +import androidx.preference.PreferenceManager; |
| 19 | + |
| 20 | +import com.d4rk.androidtutorials.java.R; |
| 21 | +import com.google.android.play.core.appupdate.AppUpdateManager; |
| 22 | +import com.google.android.play.core.appupdate.AppUpdateManagerFactory; |
| 23 | + |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.mockito.MockedStatic; |
| 28 | + |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +public class DefaultMainRepositoryTest { |
| 34 | + |
| 35 | + private Context context; |
| 36 | + private FakeSharedPreferences defaultPrefs; |
| 37 | + private FakeSharedPreferences startupPrefs; |
| 38 | + private AppUpdateManager updateManager; |
| 39 | + private MockedStatic<PreferenceManager> prefManager; |
| 40 | + private MockedStatic<AppUpdateManagerFactory> updateManagerFactory; |
| 41 | + private DefaultMainRepository repository; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setUp() { |
| 45 | + context = mock(Context.class); |
| 46 | + defaultPrefs = new FakeSharedPreferences(); |
| 47 | + startupPrefs = new FakeSharedPreferences(); |
| 48 | + updateManager = mock(AppUpdateManager.class); |
| 49 | + |
| 50 | + prefManager = mockStatic(PreferenceManager.class); |
| 51 | + prefManager.when(() -> PreferenceManager.getDefaultSharedPreferences(context)).thenReturn(defaultPrefs); |
| 52 | + |
| 53 | + updateManagerFactory = mockStatic(AppUpdateManagerFactory.class); |
| 54 | + updateManagerFactory.when(() -> AppUpdateManagerFactory.create(context)).thenReturn(updateManager); |
| 55 | + |
| 56 | + when(context.getApplicationContext()).thenReturn(context); |
| 57 | + when(context.getString(R.string.key_theme)).thenReturn("theme"); |
| 58 | + when(context.getString(R.string.default_value_theme)).thenReturn("system"); |
| 59 | + when(context.getString(R.string.key_bottom_navigation_bar_labels)).thenReturn("labels"); |
| 60 | + when(context.getString(R.string.default_value_bottom_navigation_bar_labels)).thenReturn("never"); |
| 61 | + when(context.getString(R.string.key_default_tab)).thenReturn("tab"); |
| 62 | + when(context.getString(R.string.default_value_tab)).thenReturn("home"); |
| 63 | + when(context.getString(R.string.key_language)).thenReturn("language"); |
| 64 | + when(context.getString(R.string.default_value_language)).thenReturn("en"); |
| 65 | + when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(startupPrefs); |
| 66 | + |
| 67 | + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); |
| 68 | + AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList()); |
| 69 | + |
| 70 | + repository = new DefaultMainRepository(context); |
| 71 | + } |
| 72 | + |
| 73 | + @After |
| 74 | + public void tearDown() { |
| 75 | + prefManager.close(); |
| 76 | + updateManagerFactory.close(); |
| 77 | + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); |
| 78 | + AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void applyThemeSettings_noChangeWhenPreferenceSame() { |
| 83 | + 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()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void applyThemeSettings_changesWhenPreferenceDiffers() { |
| 92 | + 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()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void getBottomNavLabelVisibilityAndDefaultTabPreferenceReturnStoredValues() { |
| 101 | + defaultPrefs.edit().putString("labels", "always").apply(); |
| 102 | + defaultPrefs.edit().putString("tab", "search").apply(); |
| 103 | + assertEquals("always", repository.getBottomNavLabelVisibility()); |
| 104 | + assertEquals("search", repository.getDefaultTabPreference()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void startupScreenDefaultsTrueAndFlipsAfterMark() { |
| 109 | + assertTrue(repository.shouldShowStartupScreen()); |
| 110 | + repository.markStartupScreenShown(); |
| 111 | + assertFalse(repository.shouldShowStartupScreen()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void applyLanguageSettings_setsLocales() { |
| 116 | + defaultPrefs.edit().putString("language", "fr").apply(); |
| 117 | + repository.applyLanguageSettings(); |
| 118 | + assertEquals("fr", AppCompatDelegate.getApplicationLocales().toLanguageTags()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void getAppUpdateManager_returnsInjectedManager() { |
| 123 | + assertSame(updateManager, repository.getAppUpdateManager()); |
| 124 | + } |
| 125 | + |
| 126 | + private static class FakeSharedPreferences implements SharedPreferences { |
| 127 | + private final Map<String, Object> values = new HashMap<>(); |
| 128 | + |
| 129 | + @Override |
| 130 | + public Map<String, ?> getAll() { |
| 131 | + return values; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public String getString(String key, String defValue) { |
| 136 | + Object v = values.get(key); |
| 137 | + return v instanceof String ? (String) v : defValue; |
| 138 | + } |
| 139 | + |
| 140 | + @SuppressWarnings("unchecked") |
| 141 | + @Override |
| 142 | + public Set<String> getStringSet(String key, Set<String> defValues) { |
| 143 | + Object v = values.get(key); |
| 144 | + return v instanceof Set ? (Set<String>) v : defValues; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public int getInt(String key, int defValue) { |
| 149 | + Object v = values.get(key); |
| 150 | + return v instanceof Integer ? (Integer) v : defValue; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public long getLong(String key, long defValue) { |
| 155 | + Object v = values.get(key); |
| 156 | + return v instanceof Long ? (Long) v : defValue; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public float getFloat(String key, float defValue) { |
| 161 | + Object v = values.get(key); |
| 162 | + return v instanceof Float ? (Float) v : defValue; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public boolean getBoolean(String key, boolean defValue) { |
| 167 | + Object v = values.get(key); |
| 168 | + return v instanceof Boolean ? (Boolean) v : defValue; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public boolean contains(String key) { |
| 173 | + return values.containsKey(key); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public Editor edit() { |
| 178 | + return new Editor() { |
| 179 | + @Override |
| 180 | + public Editor putString(String key, String value) { |
| 181 | + values.put(key, value); |
| 182 | + return this; |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public Editor putStringSet(String key, Set<String> value) { |
| 187 | + values.put(key, value); |
| 188 | + return this; |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public Editor putInt(String key, int value) { |
| 193 | + values.put(key, value); |
| 194 | + return this; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public Editor putLong(String key, long value) { |
| 199 | + values.put(key, value); |
| 200 | + return this; |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public Editor putFloat(String key, float value) { |
| 205 | + values.put(key, value); |
| 206 | + return this; |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public Editor putBoolean(String key, boolean value) { |
| 211 | + values.put(key, value); |
| 212 | + return this; |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public Editor remove(String key) { |
| 217 | + values.remove(key); |
| 218 | + return this; |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public Editor clear() { |
| 223 | + values.clear(); |
| 224 | + return this; |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public boolean commit() { |
| 229 | + return true; |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public void apply() { |
| 234 | + } |
| 235 | + }; |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments