|
| 1 | +package com.d4rk.androidtutorials.java.ads.managers; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.*; |
| 4 | + |
| 5 | +import android.content.Context; |
| 6 | +import android.graphics.drawable.Drawable; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.Button; |
| 10 | +import android.widget.ImageView; |
| 11 | +import android.widget.TextView; |
| 12 | + |
| 13 | +import com.d4rk.androidtutorials.java.R; |
| 14 | +import com.google.android.gms.ads.AdListener; |
| 15 | +import com.google.android.gms.ads.LoadAdError; |
| 16 | +import com.google.android.gms.ads.nativead.MediaView; |
| 17 | +import com.google.android.gms.ads.nativead.NativeAd; |
| 18 | +import com.google.android.gms.ads.nativead.NativeAdView; |
| 19 | + |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import java.lang.reflect.Constructor; |
| 23 | +import java.lang.reflect.Method; |
| 24 | + |
| 25 | +/** |
| 26 | + * Tests for {@link NativeAdLoader}. |
| 27 | + */ |
| 28 | +public class NativeAdLoaderTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void populateNativeAdViewPopulatesViews() throws Exception { |
| 32 | + NativeAd nativeAd = mock(NativeAd.class); |
| 33 | + when(nativeAd.getHeadline()).thenReturn("headline"); |
| 34 | + when(nativeAd.getBody()).thenReturn("body"); |
| 35 | + when(nativeAd.getCallToAction()).thenReturn("cta"); |
| 36 | + NativeAd.Image icon = mock(NativeAd.Image.class); |
| 37 | + Drawable drawable = mock(Drawable.class); |
| 38 | + when(icon.getDrawable()).thenReturn(drawable); |
| 39 | + when(nativeAd.getIcon()).thenReturn(icon); |
| 40 | + when(nativeAd.getAdvertiser()).thenReturn("advertiser"); |
| 41 | + NativeAd.MediaContent mediaContent = mock(NativeAd.MediaContent.class); |
| 42 | + when(nativeAd.getMediaContent()).thenReturn(mediaContent); |
| 43 | + |
| 44 | + NativeAdView adView = mock(NativeAdView.class); |
| 45 | + MediaView mediaView = mock(MediaView.class); |
| 46 | + TextView headlineView = mock(TextView.class); |
| 47 | + TextView bodyView = mock(TextView.class); |
| 48 | + Button callToActionView = mock(Button.class); |
| 49 | + ImageView iconView = mock(ImageView.class); |
| 50 | + TextView attributionView = mock(TextView.class); |
| 51 | + |
| 52 | + when(adView.findViewById(R.id.ad_media)).thenReturn(mediaView); |
| 53 | + when(adView.findViewById(R.id.ad_headline)).thenReturn(headlineView); |
| 54 | + when(adView.findViewById(R.id.ad_body)).thenReturn(bodyView); |
| 55 | + when(adView.findViewById(R.id.ad_call_to_action)).thenReturn(callToActionView); |
| 56 | + when(adView.findViewById(R.id.ad_app_icon)).thenReturn(iconView); |
| 57 | + when(adView.findViewById(R.id.ad_attribution)).thenReturn(attributionView); |
| 58 | + Context context = mock(Context.class); |
| 59 | + when(adView.getContext()).thenReturn(context); |
| 60 | + when(context.getString(R.string.ad)).thenReturn("Ad"); |
| 61 | + |
| 62 | + Method method = NativeAdLoader.class.getDeclaredMethod("populateNativeAdView", NativeAd.class, NativeAdView.class); |
| 63 | + method.setAccessible(true); |
| 64 | + method.invoke(null, nativeAd, adView); |
| 65 | + |
| 66 | + verify(headlineView).setText("headline"); |
| 67 | + verify(bodyView).setVisibility(View.VISIBLE); |
| 68 | + verify(bodyView).setText("body"); |
| 69 | + verify(callToActionView).setVisibility(View.VISIBLE); |
| 70 | + verify(callToActionView).setText("cta"); |
| 71 | + verify(attributionView).setText("Ad advertiser"); |
| 72 | + verify(iconView).setImageDrawable(drawable); |
| 73 | + verify(iconView).setVisibility(View.VISIBLE); |
| 74 | + verify(mediaView).setMediaContent(mediaContent); |
| 75 | + verify(mediaView).setVisibility(View.VISIBLE); |
| 76 | + verify(adView).setNativeAd(nativeAd); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void populateNativeAdViewHidesMissingViews() throws Exception { |
| 81 | + NativeAd nativeAd = mock(NativeAd.class); |
| 82 | + when(nativeAd.getHeadline()).thenReturn("headline"); |
| 83 | + when(nativeAd.getBody()).thenReturn(null); |
| 84 | + when(nativeAd.getCallToAction()).thenReturn(null); |
| 85 | + when(nativeAd.getIcon()).thenReturn(null); |
| 86 | + when(nativeAd.getAdvertiser()).thenReturn(null); |
| 87 | + when(nativeAd.getMediaContent()).thenReturn(null); |
| 88 | + |
| 89 | + NativeAdView adView = mock(NativeAdView.class); |
| 90 | + MediaView mediaView = mock(MediaView.class); |
| 91 | + TextView headlineView = mock(TextView.class); |
| 92 | + TextView bodyView = mock(TextView.class); |
| 93 | + Button callToActionView = mock(Button.class); |
| 94 | + ImageView iconView = mock(ImageView.class); |
| 95 | + TextView attributionView = mock(TextView.class); |
| 96 | + |
| 97 | + when(adView.findViewById(R.id.ad_media)).thenReturn(mediaView); |
| 98 | + when(adView.findViewById(R.id.ad_headline)).thenReturn(headlineView); |
| 99 | + when(adView.findViewById(R.id.ad_body)).thenReturn(bodyView); |
| 100 | + when(adView.findViewById(R.id.ad_call_to_action)).thenReturn(callToActionView); |
| 101 | + when(adView.findViewById(R.id.ad_app_icon)).thenReturn(iconView); |
| 102 | + when(adView.findViewById(R.id.ad_attribution)).thenReturn(attributionView); |
| 103 | + Context context = mock(Context.class); |
| 104 | + when(adView.getContext()).thenReturn(context); |
| 105 | + when(context.getString(R.string.ad)).thenReturn("Ad"); |
| 106 | + |
| 107 | + Method method = NativeAdLoader.class.getDeclaredMethod("populateNativeAdView", NativeAd.class, NativeAdView.class); |
| 108 | + method.setAccessible(true); |
| 109 | + method.invoke(null, nativeAd, adView); |
| 110 | + |
| 111 | + verify(bodyView).setVisibility(View.GONE); |
| 112 | + verify(callToActionView).setVisibility(View.GONE); |
| 113 | + verify(attributionView).setText("Ad"); |
| 114 | + verify(iconView).setVisibility(View.GONE); |
| 115 | + verify(mediaView).setVisibility(View.GONE); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void onAdFailedToLoadClearsContainer() throws Exception { |
| 120 | + ViewGroup container = mock(ViewGroup.class); |
| 121 | + |
| 122 | + Class<?> clazz = Class.forName("com.d4rk.androidtutorials.java.ads.managers.NativeAdLoader$1"); |
| 123 | + Constructor<?> ctor = clazz.getDeclaredConstructor(ViewGroup.class); |
| 124 | + ctor.setAccessible(true); |
| 125 | + AdListener listener = (AdListener) ctor.newInstance(container); |
| 126 | + |
| 127 | + LoadAdError loadAdError = mock(LoadAdError.class); |
| 128 | + listener.onAdFailedToLoad(loadAdError); |
| 129 | + |
| 130 | + verify(container).removeAllViews(); |
| 131 | + verify(container).setVisibility(View.GONE); |
| 132 | + } |
| 133 | +} |
| 134 | + |
0 commit comments