NativeAdViewHelper { @Deprecated @NonNull static View getAdView(@Nullable View convertView, @Nullable final ViewGroup parent, @NonNull final Context context, @Nullable final NativeResponse nativeResponse, @Nullable final ViewBinder viewBinder) { Preconditions.NoThrow.checkNotNull(viewBinder, "ViewBinder is null."); if (convertView != null) { clearNativeResponse(context, convertView); } if (nativeResponse == null || nativeResponse.isDestroyed() || viewBinder == null) { MoPubLog.d("nativeResponse or viewBinder null or invalid. Returning empty view"); if (convertView == null || !ViewType.EMPTY.equals(convertView.getTag())) { convertView = new View(context); convertView.setTag(ViewType.EMPTY); convertView.setVisibility(View.GONE); } } else { final MoPubNativeAdRenderer moPubNativeAdRenderer = new MoPubNativeAdRenderer(viewBinder); if (convertView == null || !ViewType.AD.equals(convertView.getTag())) { convertView = moPubNativeAdRenderer.createAdView(context, parent); convertView.setTag(ViewType.AD); } prepareNativeResponse(context, convertView, nativeResponse); moPubNativeAdRenderer.renderAdView(convertView, nativeResponse); } return convertView; } private  NativeAdViewHelper();   }
@Test public void getAdView_shouldReturnPopulatedView() throws Exception { when(mockNativeResponse1.getTitle()).thenReturn("test title"); when(mockNativeResponse1.getText()).thenReturn("test text"); when(mockNativeResponse1.getCallToAction()).thenReturn("test call to action"); View view = NativeAdViewHelper.getAdView(relativeLayout, viewGroup, context, mockNativeResponse1, viewBinder); assertThat(((TextView)view.findViewById(titleView.getId())).getText()).isEqualTo("test title"); assertThat(((TextView)view.findViewById(textView.getId())).getText()).isEqualTo("test text"); assertThat(((TextView)view.findViewById(callToActionView.getId())).getText()).isEqualTo("test call to action"); assertThat(view.getTag()).isEqualTo(NativeAdViewHelper.ViewType.AD); }
@Test public void getAdView_withDestroyedNativeResponse_shouldReturnEmptyAndGoneConvertView() throws Exception { when(mockNativeResponse1.isDestroyed()).thenReturn(true); View view = NativeAdViewHelper.getAdView(relativeLayout, viewGroup, context, mockNativeResponse1, viewBinder); assertThat(view).isNotEqualTo(relativeLayout); assertThat(view.getTag()).isEqualTo(NativeAdViewHelper.ViewType.EMPTY); assertThat(view.getVisibility()).isEqualTo(View.GONE); }
@Test public void getAdView_shouldRemoveViewFromImpressionTracker_shouldClearPreviousNativeResponse() throws Exception { NativeAdViewHelper.sImpressionTrackerMap.put(context, mockImpressionTracker); NativeAdViewHelper.getAdView(relativeLayout, viewGroup, context, mockNativeResponse1, viewBinder); verify(mockImpressionTracker).removeView(relativeLayout); NativeAdViewHelper.getAdView(relativeLayout, viewGroup, context, mockNativeResponse2, viewBinder); verify(mockImpressionTracker, times(2)).removeView(relativeLayout); verify(mockNativeResponse1).clear(relativeLayout); NativeAdViewHelper.getAdView(relativeLayout, viewGroup, context, mockNativeResponse1, viewBinder); verify(mockImpressionTracker, times(3)).removeView(relativeLayout); verify(mockNativeResponse2).clear(relativeLayout); }
@Test public void getAdView_withNetworkImpressionTracker_shouldNotAddViewToImpressionTracker_shouldPrepareNativeResponse() throws Exception { NativeAdViewHelper.sImpressionTrackerMap.put(context, mockImpressionTracker); when(mockNativeResponse1.isOverridingImpressionTracker()).thenReturn(true); NativeAdViewHelper.getAdView(relativeLayout, viewGroup, context, mockNativeResponse1, viewBinder); verify(mockImpressionTracker, never()).addView(any(View.class), any(NativeResponse.class)); verify(mockNativeResponse1).prepare(relativeLayout); }
@Test public void getAdView_withoutNetworkImpressionTracker_shouldAddViewToImpressionTracker_shouldPrepareNativeResponse() throws Exception { NativeAdViewHelper.sImpressionTrackerMap.put(context, mockImpressionTracker); when(mockNativeResponse1.isOverridingImpressionTracker()).thenReturn(false); NativeAdViewHelper.getAdView(relativeLayout, viewGroup, context, mockNativeResponse1, viewBinder); verify(mockImpressionTracker).addView(relativeLayout, mockNativeResponse1); verify(mockNativeResponse1).prepare(relativeLayout); }