|
| 1 | +package com.d4rk.androidtutorials.java.utils; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import android.content.Context; |
| 11 | +import android.content.SharedPreferences; |
| 12 | + |
| 13 | +import androidx.preference.PreferenceManager; |
| 14 | + |
| 15 | +import com.d4rk.androidtutorials.java.R; |
| 16 | +import com.google.firebase.analytics.FirebaseAnalytics; |
| 17 | + |
| 18 | +import org.junit.Test; |
| 19 | +import org.mockito.ArgumentCaptor; |
| 20 | +import org.mockito.MockedStatic; |
| 21 | +import org.mockito.Mockito; |
| 22 | + |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +public class ConsentUtilsTest { |
| 26 | + |
| 27 | + @Test |
| 28 | + public void updateFirebaseConsent_setsExpectedStatuses() { |
| 29 | + Context context = mock(Context.class); |
| 30 | + FirebaseAnalytics analytics = mock(FirebaseAnalytics.class); |
| 31 | + |
| 32 | + try (MockedStatic<FirebaseAnalytics> firebaseStatic = Mockito.mockStatic(FirebaseAnalytics.class)) { |
| 33 | + firebaseStatic.when(() -> FirebaseAnalytics.getInstance(context)).thenReturn(analytics); |
| 34 | + |
| 35 | + boolean analyticsConsent = true; |
| 36 | + boolean adStorageConsent = false; |
| 37 | + boolean adUserDataConsent = true; |
| 38 | + boolean adPersonalizationConsent = false; |
| 39 | + |
| 40 | + ConsentUtils.updateFirebaseConsent(context, |
| 41 | + analyticsConsent, |
| 42 | + adStorageConsent, |
| 43 | + adUserDataConsent, |
| 44 | + adPersonalizationConsent); |
| 45 | + |
| 46 | + ArgumentCaptor<Map<FirebaseAnalytics.ConsentType, FirebaseAnalytics.ConsentStatus>> captor = |
| 47 | + ArgumentCaptor.forClass(Map.class); |
| 48 | + verify(analytics).setConsent(captor.capture()); |
| 49 | + Map<FirebaseAnalytics.ConsentType, FirebaseAnalytics.ConsentStatus> result = captor.getValue(); |
| 50 | + |
| 51 | + assertEquals(FirebaseAnalytics.ConsentStatus.GRANTED, |
| 52 | + result.get(FirebaseAnalytics.ConsentType.ANALYTICS_STORAGE)); |
| 53 | + assertEquals(FirebaseAnalytics.ConsentStatus.DENIED, |
| 54 | + result.get(FirebaseAnalytics.ConsentType.AD_STORAGE)); |
| 55 | + assertEquals(FirebaseAnalytics.ConsentStatus.GRANTED, |
| 56 | + result.get(FirebaseAnalytics.ConsentType.AD_USER_DATA)); |
| 57 | + assertEquals(FirebaseAnalytics.ConsentStatus.DENIED, |
| 58 | + result.get(FirebaseAnalytics.ConsentType.AD_PERSONALIZATION)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void canShowAds_readsStoredPreference() { |
| 64 | + Context context = mock(Context.class); |
| 65 | + SharedPreferences prefs = mock(SharedPreferences.class); |
| 66 | + when(context.getString(R.string.key_consent_ad_storage)).thenReturn("consent_ad_storage"); |
| 67 | + |
| 68 | + try (MockedStatic<PreferenceManager> prefsStatic = Mockito.mockStatic(PreferenceManager.class)) { |
| 69 | + prefsStatic.when(() -> PreferenceManager.getDefaultSharedPreferences(context)).thenReturn(prefs); |
| 70 | + |
| 71 | + when(prefs.getBoolean("consent_ad_storage", true)).thenReturn(false); |
| 72 | + assertFalse(ConsentUtils.canShowAds(context)); |
| 73 | + |
| 74 | + when(prefs.getBoolean("consent_ad_storage", true)).thenReturn(true); |
| 75 | + assertTrue(ConsentUtils.canShowAds(context)); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments