|
| 1 | +package io.invertase.googlemobileads |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2016-present Invertase Limited & Contributors |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this library except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +import com.google.android.gms.ads.AdError |
| 21 | +import com.google.android.gms.ads.AdRequest |
| 22 | +import com.google.android.gms.ads.AdSize |
| 23 | +import org.junit.Assert.assertEquals |
| 24 | +import org.junit.Assert.assertFalse |
| 25 | +import org.junit.Assert.assertTrue |
| 26 | +import org.junit.Test |
| 27 | +import org.junit.runner.RunWith |
| 28 | +import org.robolectric.RobolectricTestRunner |
| 29 | +import org.robolectric.annotation.Config |
| 30 | + |
| 31 | +/** |
| 32 | + * Owned mapper/helper coverage for [ReactNativeGoogleMobileAdsCommon]. |
| 33 | + * |
| 34 | + * Does not assert Google Mobile Ads auction, fill, or adapter behavior. |
| 35 | + */ |
| 36 | +@RunWith(RobolectricTestRunner::class) |
| 37 | +@Config(sdk = [34]) |
| 38 | +class ReactNativeGoogleMobileAdsCommonTest { |
| 39 | + @Test |
| 40 | + fun isAdManagerUnit_detectsSlashPrefix() { |
| 41 | + assertTrue(ReactNativeGoogleMobileAdsCommon.isAdManagerUnit("/1234/unit")) |
| 42 | + assertFalse(ReactNativeGoogleMobileAdsCommon.isAdManagerUnit("ca-app-pub-xxx")) |
| 43 | + assertFalse(ReactNativeGoogleMobileAdsCommon.isAdManagerUnit(null)) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun stringToAdSize_parsesCustomAndNamedSizes() { |
| 48 | + val custom = ReactNativeGoogleMobileAdsCommon.stringToAdSize("320x50") |
| 49 | + assertEquals(320, custom.width) |
| 50 | + assertEquals(50, custom.height) |
| 51 | + |
| 52 | + assertEquals(AdSize.BANNER, ReactNativeGoogleMobileAdsCommon.stringToAdSize("BANNER")) |
| 53 | + assertEquals(AdSize.MEDIUM_RECTANGLE, ReactNativeGoogleMobileAdsCommon.stringToAdSize("medium_rectangle")) |
| 54 | + assertEquals(AdSize.LEADERBOARD, ReactNativeGoogleMobileAdsCommon.stringToAdSize("LEADERBOARD")) |
| 55 | + assertEquals(AdSize.FLUID, ReactNativeGoogleMobileAdsCommon.stringToAdSize("FLUID")) |
| 56 | + assertEquals(AdSize.BANNER, ReactNativeGoogleMobileAdsCommon.stringToAdSize("unknown-size")) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun getCodeAndMessageFromAdError_mapsOwnedCodes() { |
| 61 | + assertMapped(AdRequest.ERROR_CODE_INTERNAL_ERROR, "internal-error") |
| 62 | + assertMapped(AdRequest.ERROR_CODE_INVALID_REQUEST, "invalid-request") |
| 63 | + assertMapped(AdRequest.ERROR_CODE_NETWORK_ERROR, "network-error") |
| 64 | + assertMapped(AdRequest.ERROR_CODE_NO_FILL, "no-fill") |
| 65 | + assertMapped(AdRequest.ERROR_CODE_APP_ID_MISSING, "app-id-missing") |
| 66 | + assertMapped(AdRequest.ERROR_CODE_MEDIATION_NO_FILL, "mediation-no-fill") |
| 67 | + assertMapped(AdRequest.ERROR_CODE_INVALID_AD_STRING, "invalid-ad-string") |
| 68 | + assertMapped(AdRequest.ERROR_CODE_REQUEST_ID_MISMATCH, "request-id-mismatch") |
| 69 | + assertMapped(999, "unknown") |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun eventConstants_matchOwnedWireNames() { |
| 74 | + assertEquals("loaded", ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_LOADED) |
| 75 | + assertEquals("error", ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_ERROR) |
| 76 | + assertEquals("google_mobile_ads_interstitial_event", ReactNativeGoogleMobileAdsEvent.GOOGLE_MOBILE_ADS_EVENT_INTERSTITIAL) |
| 77 | + } |
| 78 | + |
| 79 | + private fun assertMapped( |
| 80 | + errorCode: Int, |
| 81 | + expectedCode: String, |
| 82 | + ) { |
| 83 | + val error = AdError(errorCode, "detail-$errorCode", "test-domain") |
| 84 | + val mapped = ReactNativeGoogleMobileAdsCommon.getCodeAndMessageFromAdError(error) |
| 85 | + assertEquals(expectedCode, mapped[0]) |
| 86 | + assertEquals("detail-$errorCode", mapped[1]) |
| 87 | + } |
| 88 | +} |
0 commit comments