Skip to content

Commit ee51131

Browse files
Mobile Ads Developer Relationscopybara-github
authored andcommitted
Add unit tests for InterstitialAdPreloader.PeekAdResponseInfo.
PiperOrigin-RevId: 952413031
1 parent 1c59495 commit ee51131

10 files changed

Lines changed: 101 additions & 2 deletions

source/plugin/Assets/GoogleMobileAds/Api/InterstitialAdPreloader.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static Dictionary<string, PreloadConfiguration> GetConfigurations()
127127
/// Returns a preloaded ad and removes it from the cache.
128128
/// </summary>
129129
/// <param name="preloadId">
130-
/// The ad's preload ID.
130+
/// A string identifier used to preload the ads for that configuration.
131131
/// </param>
132132
/// <returns>
133133
/// The preloaded ad for the given preload ID, or null if no ad is available.
@@ -146,6 +146,26 @@ public static InterstitialAd DequeueAd(string preloadId)
146146
return new InterstitialAd(client);
147147
}
148148

149+
/// <summary>
150+
/// Returns the ResponseInfo of a preloaded ad for the given preload ID without
151+
/// dequeuing it, or null if no ad is available.
152+
/// </summary>
153+
/// <param name="preloadId">
154+
/// A string identifier used to preload the ads for that configuration.
155+
/// </param>
156+
/// <returns>
157+
/// The ResponseInfo of the preloaded ad for the given preload ID, or null if no ad is available.
158+
/// </returns>
159+
public static ResponseInfo PeekAdResponseInfo(string preloadId)
160+
{
161+
var responseInfoClient = _client.PeekAdResponseInfo(preloadId);
162+
if (responseInfoClient == null)
163+
{
164+
return null;
165+
}
166+
return new ResponseInfo(responseInfoClient);
167+
}
168+
149169
/// <summary>
150170
/// Get the number of ads available for the given preload ID.
151171
/// </summary>

source/plugin/Assets/GoogleMobileAds/Common/IInterstitialAdPreloaderClient.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ bool Preload(string preloadId, PreloadConfiguration preloadConfiguration,
6565
/// Returns a preloaded ad and removes it from the cache.
6666
/// </summary>
6767
/// <param name="preloadId">
68-
/// The ad's preload ID.
68+
/// A string identifier used to preload the ads for that configuration.
6969
/// </param>
7070
/// <returns>
7171
/// The preloaded ad for the given preload ID, or null if no ad is available.
@@ -76,6 +76,18 @@ bool Preload(string preloadId, PreloadConfiguration preloadConfiguration,
7676
/// </remarks>
7777
IInterstitialClient DequeueAd(string preloadId);
7878

79+
/// <summary>
80+
/// Returns the ResponseInfo of an ad available for the given preload ID without
81+
/// dequeuing it, or null if no ad is available.
82+
/// </summary>
83+
/// <param name="preloadId">
84+
/// A string identifier used to preload the ads for that configuration.
85+
/// </param>
86+
/// <returns>
87+
/// The ResponseInfo of the preloaded ad for the given preload ID, or null if no ad is available.
88+
/// </returns>
89+
IResponseInfoClient PeekAdResponseInfo(string preloadId);
90+
7991
/// <summary>
8092
/// Get the number of ads available for the given preload ID.
8193
/// </summary>

source/plugin/Assets/GoogleMobileAds/Platforms/Android/InterstitialAdPreloaderClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ public IInterstitialClient DequeueAd(string preloadId)
7070
return interstitialClient;
7171
}
7272

73+
public IResponseInfoClient PeekAdResponseInfo(string preloadId)
74+
{
75+
UnityEngine.Debug.LogError(
76+
"PeekAdResponseInfo API is not available on the Legacy version of Google Mobile Ads Android SDK.");
77+
return null;
78+
}
79+
7380
public int GetNumAdsAvailable(string preloadId)
7481
{
7582
return _unityInterstitialPreloader.Call<int>("getNumAdsAvailable", preloadId);

source/plugin/Assets/GoogleMobileAds/Platforms/Android/NextGenInterstitialAdPreloaderClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public IInterstitialClient DequeueAd(string preloadId) {
6363
return interstitialAdClient;
6464
}
6565

66+
public IResponseInfoClient PeekAdResponseInfo(string preloadId) {
67+
// TODO: Implement Android Peek API
68+
return null;
69+
}
70+
6671
public int GetNumAdsAvailable(string preloadId) {
6772
return _unityInterstitialAdPreloader.Call<int>("getNumAdsAvailable", preloadId);
6873
}

source/plugin/Assets/GoogleMobileAds/Platforms/Unity/InterstitialAdPreloaderClient.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ public IInterstitialClient DequeueAd(string preloadId)
117117
return null;
118118
}
119119

120+
public IResponseInfoClient PeekAdResponseInfo(string preloadId)
121+
{
122+
Queue<InterstitialClient> queue;
123+
if (_bufferedAds.TryGetValue(preloadId, out queue) && queue.Count > 0)
124+
{
125+
InterstitialClient adClient = queue.Peek();
126+
return adClient.GetResponseInfoClient();
127+
}
128+
return null;
129+
}
130+
120131
public int GetNumAdsAvailable(string preloadId)
121132
{
122133
Queue<InterstitialClient> queue;

source/plugin/Assets/GoogleMobileAds/Platforms/iOS/Externs.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ internal static extern bool GADUInterstitialAdPreloaderIsAdAvailable(
414414
internal static extern IntPtr GADUInterstitialAdPreloaderDequeueAd(
415415
IntPtr interstitialAdPreloader, string preloadId, IntPtr appOpenAdClientPtr);
416416

417+
[DllImport("__Internal")]
418+
internal static extern IntPtr GADUInterstitialAdPreloaderPeekAdResponseInfo(
419+
IntPtr interstitialAdPreloader, string preloadId);
420+
417421
[DllImport("__Internal")]
418422
internal static extern int GADUInterstitialAdPreloaderGetNumAdsAvailable(
419423
IntPtr interstitialAdPreloader, string preloadId);

source/plugin/Assets/GoogleMobileAds/Platforms/iOS/InterstitialAdPreloaderClient.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ public IInterstitialClient DequeueAd(string preloadId)
124124
return interstitialAdClient;
125125
}
126126

127+
public IResponseInfoClient PeekAdResponseInfo(string preloadId)
128+
{
129+
if (InterstitialAdPreloaderPtr == IntPtr.Zero)
130+
{
131+
return null;
132+
}
133+
var responseInfoPtr = Externs.GADUInterstitialAdPreloaderPeekAdResponseInfo(
134+
InterstitialAdPreloaderPtr, preloadId);
135+
if (responseInfoPtr == IntPtr.Zero)
136+
{
137+
return null;
138+
}
139+
return new ResponseInfoClient(responseInfoPtr);
140+
}
141+
127142
public int GetNumAdsAvailable(string preloadId)
128143
{
129144
if (InterstitialAdPreloaderPtr == IntPtr.Zero)

source/plugin/Assets/Plugins/iOS/GADUInterface.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,23 @@ GADUTypeInterstitialRef GADUInterstitialAdPreloaderDequeueAd(
11551155
return nil;
11561156
}
11571157

1158+
GADUTypeResponseInfoRef GADUInterstitialAdPreloaderPeekAdResponseInfo(
1159+
GADUTypeInterstitialAdPreloaderClientRef interstitialAdPreloader, const char *preloadId) {
1160+
GADUInterstitialAdPreloader *internalInterstitialAdPreloader =
1161+
(__bridge GADUInterstitialAdPreloader *)interstitialAdPreloader;
1162+
if (!internalInterstitialAdPreloader) {
1163+
return nil;
1164+
}
1165+
GADResponseInfo *responseInfo = [internalInterstitialAdPreloader
1166+
adResponseInfoWithPreloadID:GADUStringFromUTF8String(preloadId)];
1167+
if (responseInfo) {
1168+
GADUObjectCache *cache = GADUObjectCache.sharedInstance;
1169+
cache[responseInfo.gadu_referenceKey] = responseInfo;
1170+
return (__bridge GADUTypeResponseInfoRef)responseInfo;
1171+
}
1172+
return nil;
1173+
}
1174+
11581175
unsigned long GADUInterstitialAdPreloaderGetNumAdsAvailable(
11591176
GADUTypeInterstitialAdPreloaderClientRef interstitialAdPreloader, const char *preloadId) {
11601177
GADUInterstitialAdPreloader *internalInterstitialAdPreloader =

source/plugin/Assets/Plugins/iOS/GADUInterstitialAdPreloader.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
interstitialAdClient:(_Nonnull GADUTypeInterstitialClientRef *_Nonnull)
6666
interstitialAdClient;
6767

68+
/// Returns the response info of a preloaded interstitial ad for the given preload ID without
69+
/// removing the ad from the queue. Returns nil if an ad is not available.
70+
- (nullable GADResponseInfo *)adResponseInfoWithPreloadID:(nonnull NSString *)preloadID;
71+
6872
/// Returns the corresponding configuration for the given preload ID.
6973
- (nullable GADUPreloadConfigurationV2 *)configurationWithPreloadID:(nonnull NSString *)preloadID;
7074

source/plugin/Assets/Plugins/iOS/GADUInterstitialAdPreloader.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ - (nullable GADUInterstitial *)adWithPreloadID:(nonnull NSString *)preloadID
5858
return nil;
5959
}
6060

61+
- (nullable GADResponseInfo *)adResponseInfoWithPreloadID:(nonnull NSString *)preloadID {
62+
return [GADInterstitialAdPreloader.sharedInstance adResponseInfoWithPreloadID:preloadID];
63+
}
64+
6165
- (nullable GADUPreloadConfigurationV2 *)configurationWithPreloadID:(nonnull NSString *)preloadID {
6266
GADPreloadConfigurationV2 *config =
6367
[GADInterstitialAdPreloader.sharedInstance configurationWithPreloadID:preloadID];

0 commit comments

Comments
 (0)