Skip to content

Commit 1ec45dd

Browse files
Mobile Ads Developer Relationscopybara-github
authored andcommitted
Add unit tests for UnityBannerAd lifecycle for additional necessary coverage.
PiperOrigin-RevId: 963568108
1 parent 0a7f36a commit 1ec45dd

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

  • source/plugin/Assets/Plugins/Android/GoogleMobileAdsPlugin.androidlib/src/test/java/com/google/unity/ads/nextgen

source/plugin/Assets/Plugins/Android/GoogleMobileAdsPlugin.androidlib/src/test/java/com/google/unity/ads/nextgen/UnityBannerAdTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,4 +544,41 @@ public void testDestroy_nullifiesAdViewAndBannerLayout() throws Exception {
544544
assertThat(adView).isNull();
545545
assertThat(bannerLayout).isNull();
546546
}
547+
548+
@Test
549+
public void testLoad_afterDestroy_resetsIsDestroyedAndLoadsAd() {
550+
unityBannerAd.destroy();
551+
View fakeView = simulateAdLoadSuccess();
552+
verify(mockCallback, timeout(1000)).onAdLoaded();
553+
assertThat(fakeView).isNotNull();
554+
}
555+
556+
@Test
557+
public void testDestroy_calledMultipleTimes_doesNotCrash() throws Exception {
558+
unityBannerAd.destroy();
559+
unityBannerAd.destroy();
560+
ShadowLooper.idleMainLooper();
561+
562+
Field isDestroyedField = UnityBannerAd.class.getDeclaredField("isDestroyed");
563+
isDestroyedField.setAccessible(true);
564+
boolean isDestroyed = (boolean) isDestroyedField.get(unityBannerAd);
565+
566+
Field adViewField = UnityBannerAd.class.getDeclaredField("adView");
567+
adViewField.setAccessible(true);
568+
View adView = (View) adViewField.get(unityBannerAd);
569+
570+
Field bannerLayoutField = UnityBannerAd.class.getDeclaredField("bannerLayout");
571+
bannerLayoutField.setAccessible(true);
572+
FrameLayout bannerLayout = (FrameLayout) bannerLayoutField.get(unityBannerAd);
573+
574+
Field layoutChangeListenerField = UnityBannerAd.class.getDeclaredField("layoutChangeListener");
575+
layoutChangeListenerField.setAccessible(true);
576+
View.OnLayoutChangeListener layoutChangeListener =
577+
(View.OnLayoutChangeListener) layoutChangeListenerField.get(unityBannerAd);
578+
579+
assertThat(isDestroyed).isTrue();
580+
assertThat(adView).isNull();
581+
assertThat(bannerLayout).isNull();
582+
assertThat(layoutChangeListener).isNull();
583+
}
547584
}

0 commit comments

Comments
 (0)