|
| 1 | +package com.d4rk.androidtutorials.java.ads; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.*; |
| 4 | + |
| 5 | +import android.content.Context; |
| 6 | +import android.view.View; |
| 7 | + |
| 8 | +import com.d4rk.androidtutorials.java.ads.views.NativeAdBannerView; |
| 9 | +import com.google.android.gms.ads.AdRequest; |
| 10 | +import com.google.android.gms.ads.AdView; |
| 11 | +import com.google.android.gms.ads.MobileAds; |
| 12 | + |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.mockito.MockedStatic; |
| 16 | + |
| 17 | +import java.lang.reflect.Field; |
| 18 | + |
| 19 | +/** |
| 20 | + * Unit tests for {@link AdUtils}. |
| 21 | + */ |
| 22 | +public class AdUtilsTest { |
| 23 | + |
| 24 | + @Before |
| 25 | + public void setUp() throws Exception { |
| 26 | + // Reset the initialized flag before each test |
| 27 | + Field field = AdUtils.class.getDeclaredField("initialized"); |
| 28 | + field.setAccessible(true); |
| 29 | + field.set(null, false); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void initialize_callsMobileAdsInitializeOnlyOnce() { |
| 34 | + Context context = mock(Context.class); |
| 35 | + when(context.getApplicationContext()).thenReturn(context); |
| 36 | + |
| 37 | + try (MockedStatic<MobileAds> mobileAds = mockStatic(MobileAds.class)) { |
| 38 | + AdUtils.initialize(context); |
| 39 | + AdUtils.initialize(context); |
| 40 | + |
| 41 | + mobileAds.verify(() -> MobileAds.initialize(context), times(1)); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void loadBanner_withAdView_loadsAd() { |
| 47 | + Context context = mock(Context.class); |
| 48 | + when(context.getApplicationContext()).thenReturn(context); |
| 49 | + AdView adView = mock(AdView.class); |
| 50 | + when(adView.getContext()).thenReturn(context); |
| 51 | + |
| 52 | + try (MockedStatic<MobileAds> mobileAds = mockStatic(MobileAds.class)) { |
| 53 | + AdUtils.loadBanner(adView); |
| 54 | + mobileAds.verify(() -> MobileAds.initialize(context)); |
| 55 | + } |
| 56 | + |
| 57 | + verify(adView, times(1)).loadAd(any(AdRequest.class)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void loadBanner_withNativeAdBannerView_loadsAd() { |
| 62 | + Context context = mock(Context.class); |
| 63 | + when(context.getApplicationContext()).thenReturn(context); |
| 64 | + NativeAdBannerView nativeView = mock(NativeAdBannerView.class); |
| 65 | + when(nativeView.getContext()).thenReturn(context); |
| 66 | + |
| 67 | + try (MockedStatic<MobileAds> mobileAds = mockStatic(MobileAds.class)) { |
| 68 | + AdUtils.loadBanner(nativeView); |
| 69 | + mobileAds.verify(() -> MobileAds.initialize(context)); |
| 70 | + } |
| 71 | + |
| 72 | + verify(nativeView, times(1)).loadAd(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void loadBanner_withOtherView_doesNothing() { |
| 77 | + Context context = mock(Context.class); |
| 78 | + when(context.getApplicationContext()).thenReturn(context); |
| 79 | + View view = mock(View.class); |
| 80 | + when(view.getContext()).thenReturn(context); |
| 81 | + |
| 82 | + try (MockedStatic<MobileAds> mobileAds = mockStatic(MobileAds.class)) { |
| 83 | + AdUtils.loadBanner(view); |
| 84 | + mobileAds.verifyNoInteractions(); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments