19
19
#include < cstdlib>
20
20
#include < cstring>
21
21
#include < ctime>
22
+ #include < map>
22
23
#include < vector>
23
24
24
25
#include " app_framework.h" // NOLINT
25
26
#include " firebase/admob.h"
27
+ #include " firebase/admob/types.h"
26
28
#include " firebase/app.h"
27
29
#include " firebase/util.h"
28
30
#include " firebase_test_framework.h" // NOLINT
@@ -72,6 +74,28 @@ const char* kInterstitialAdUnit = "ca-app-pub-3940256099942544/4411468910";
72
74
const std::vector<std::string> kTestDeviceIDs = {
73
75
" 2077ef9a63d2b398840261c8221a0c9b" , " 098fe087d987c9a878965454a65654d7" };
74
76
77
+ // Sample keywords to use in making the request.
78
+ static const std::vector<std::string> kKeywords ({" AdMob" , " C++" , " Fun" });
79
+
80
+ // "Extra" key value pairs can be added to the request as well. Typically
81
+ // these are used when testing new features.
82
+ static const std::map<std::string, std::string> kAdMobAdapterExtras = {
83
+ {" the_name_of_an_extra" , " the_value_for_that_extra" },
84
+ {" heres" , " a second example" }};
85
+
86
+ #if defined(__ANDROID__)
87
+ static const char * kAdNetworkExtrasClassName =
88
+ " com/google/ads/mediation/admob/AdMobAdapter" ;
89
+ #else
90
+ static const char * kAdNetworkExtrasClassName = " GADExtras" ;
91
+ #endif
92
+
93
+ // Used to detect kAdMobErrorAdNetworkClassLoadErrors when loading
94
+ // ads.
95
+ static const char * kAdNetworkExtrasInvalidClassName = " abc123321cba" ;
96
+
97
+ static const char * kContentUrl = " http://www.firebase.com" ;
98
+
75
99
using app_framework::LogDebug;
76
100
using app_framework::ProcessEvents;
77
101
@@ -96,6 +120,8 @@ class FirebaseAdMobTest : public FirebaseTest {
96
120
97
121
firebase::App* FirebaseAdMobTest::shared_app_ = nullptr ;
98
122
123
+ void BrieflyPauseForVisualInspection () { app_framework::ProcessEvents (100 ); }
124
+
99
125
void FirebaseAdMobTest::SetUpTestSuite () {
100
126
LogDebug (" Initialize Firebase App." );
101
127
@@ -161,28 +187,72 @@ void FirebaseAdMobTest::SetUp() {
161
187
void FirebaseAdMobTest::TearDown () { FirebaseTest::TearDown (); }
162
188
163
189
firebase::admob::AdRequest FirebaseAdMobTest::GetAdRequest () {
164
- // Sample keywords to use in making the request.
165
- static const char * kKeywords [] = {" AdMob" , " C++" , " Fun" };
166
-
167
190
firebase::admob::AdRequest request;
168
191
169
192
// Additional keywords to be used in targeting.
170
- request.keyword_count = sizeof (kKeywords ) / sizeof (kKeywords [0 ]);
171
- request.keywords = kKeywords ;
193
+ for (auto keyword_iter = kKeywords .begin (); keyword_iter != kKeywords .end ();
194
+ ++keyword_iter) {
195
+ request.add_keyword ((*keyword_iter).c_str ());
196
+ }
172
197
173
- // "Extra" key value pairs can be added to the request as well. Typically
174
- // these are used when testing new features.
175
- static const firebase::admob::KeyValuePair kRequestExtras [] = {
176
- {" the_name_of_an_extra" , " the_value_for_that_extra" }};
177
- request.extras_count = sizeof (kRequestExtras ) / sizeof (kRequestExtras [0 ]);
178
- request.extras = kRequestExtras ;
198
+ for (auto extras_iter = kAdMobAdapterExtras .begin ();
199
+ extras_iter != kAdMobAdapterExtras .end (); ++extras_iter) {
200
+ request.add_extra (kAdNetworkExtrasClassName , extras_iter->first .c_str (),
201
+ extras_iter->second .c_str ());
202
+ }
203
+
204
+ request.set_content_url (kContentUrl );
179
205
180
206
return request;
181
207
}
182
208
183
209
// Test cases below.
184
210
TEST_F (FirebaseAdMobTest, TestGetAdRequest) { GetAdRequest (); }
185
211
212
+ TEST_F (FirebaseAdMobTest, TestGetAdRequestValues) {
213
+ SKIP_TEST_ON_DESKTOP;
214
+
215
+ const firebase::admob::AdRequest request = GetAdRequest ();
216
+
217
+ // Content URL.
218
+ EXPECT_TRUE (request.content_url () == std::string (kContentUrl ));
219
+
220
+ // Extras.
221
+ std::map<std::string, std::map<std::string, std::string> > configured_extras =
222
+ request.extras ();
223
+
224
+ EXPECT_EQ (configured_extras.size (), 1 );
225
+ for (auto extras_name_iter = configured_extras.begin ();
226
+ extras_name_iter != configured_extras.end (); ++extras_name_iter) {
227
+ // Confirm class name.
228
+ const std::string class_name = extras_name_iter->first ;
229
+ EXPECT_EQ (class_name, kAdNetworkExtrasClassName );
230
+
231
+ // Grab the extras.
232
+ const std::map<std::string, std::string>& configured_extras =
233
+ extras_name_iter->second ;
234
+ EXPECT_EQ (configured_extras.size (), kAdMobAdapterExtras .size ());
235
+
236
+ // Check the extra key value pairs.
237
+ for (auto constant_extras_iter = kAdMobAdapterExtras .begin ();
238
+ constant_extras_iter != kAdMobAdapterExtras .end ();
239
+ ++constant_extras_iter) {
240
+ // Ensure the configured value matches the constant for the same key.
241
+ EXPECT_EQ (configured_extras.at (constant_extras_iter->first ),
242
+ constant_extras_iter->second );
243
+ }
244
+ }
245
+
246
+ const std::unordered_set<std::string> configured_keywords =
247
+ request.keywords ();
248
+ EXPECT_EQ (configured_keywords.size (), kKeywords .size ());
249
+ for (auto keyword_iter = kKeywords .begin (); keyword_iter != kKeywords .end ();
250
+ ++keyword_iter) {
251
+ EXPECT_TRUE (configured_keywords.find (*keyword_iter) !=
252
+ configured_keywords.end ());
253
+ }
254
+ }
255
+
186
256
// A simple listener to help test changes to a BannerView.
187
257
class TestBannerViewListener : public firebase ::admob::BannerView::Listener {
188
258
public:
@@ -200,6 +270,71 @@ class TestBannerViewListener : public firebase::admob::BannerView::Listener {
200
270
std::vector<firebase::admob::BoundingBox> bounding_box_changes_;
201
271
};
202
272
273
+ TEST_F (FirebaseAdMobTest, TestAdSize) {
274
+ uint32_t kWidth = 50 ;
275
+ uint32_t kHeight = 10 ;
276
+
277
+ using firebase::admob::AdSize;
278
+
279
+ const AdSize adaptive_landscape =
280
+ AdSize::GetLandscapeAnchoredAdaptiveBannerAdSize (kWidth );
281
+ EXPECT_EQ (adaptive_landscape.width (), kWidth );
282
+ EXPECT_EQ (adaptive_landscape.height (), 0 );
283
+ EXPECT_EQ (adaptive_landscape.type (), AdSize::kTypeAnchoredAdaptive );
284
+ EXPECT_EQ (adaptive_landscape.orientation (), AdSize::kOrientationLandscape );
285
+
286
+ const AdSize adaptive_portrait =
287
+ AdSize::GetPortraitAnchoredAdaptiveBannerAdSize (kWidth );
288
+ EXPECT_EQ (adaptive_portrait.width (), kWidth );
289
+ EXPECT_EQ (adaptive_portrait.height (), 0 );
290
+ EXPECT_EQ (adaptive_portrait.type (), AdSize::kTypeAnchoredAdaptive );
291
+ EXPECT_EQ (adaptive_portrait.orientation (), AdSize::kOrientationPortrait );
292
+
293
+ EXPECT_FALSE (adaptive_portrait == adaptive_landscape);
294
+ EXPECT_TRUE (adaptive_portrait != adaptive_landscape);
295
+
296
+ const firebase::admob::AdSize adaptive_current =
297
+ AdSize::GetCurrentOrientationAnchoredAdaptiveBannerAdSize (kWidth );
298
+ EXPECT_EQ (adaptive_current.width (), kWidth );
299
+ EXPECT_EQ (adaptive_current.height (), 0 );
300
+ EXPECT_EQ (adaptive_current.type (), AdSize::kTypeAnchoredAdaptive );
301
+ EXPECT_EQ (adaptive_current.orientation (), AdSize::kOrientationCurrent );
302
+
303
+ const firebase::admob::AdSize custom_ad_size (kWidth , kHeight );
304
+ EXPECT_EQ (custom_ad_size.width (), kWidth );
305
+ EXPECT_EQ (custom_ad_size.height (), kHeight );
306
+ EXPECT_EQ (custom_ad_size.type (), AdSize::kTypeStandard );
307
+ EXPECT_EQ (custom_ad_size.orientation (), AdSize::kOrientationCurrent );
308
+
309
+ const AdSize custom_ad_size_2 (kWidth , kHeight );
310
+ EXPECT_TRUE (custom_ad_size == custom_ad_size_2);
311
+ EXPECT_FALSE (custom_ad_size != custom_ad_size_2);
312
+
313
+ const AdSize banner = AdSize::kBanner ;
314
+ EXPECT_EQ (banner.width (), 320 );
315
+ EXPECT_EQ (banner.height (), 50 );
316
+ EXPECT_EQ (banner.type (), AdSize::kTypeStandard );
317
+ EXPECT_EQ (banner.orientation (), AdSize::kOrientationCurrent );
318
+
319
+ const AdSize fullbanner = AdSize::kFullBanner ;
320
+ EXPECT_EQ (fullbanner.width (), 468 );
321
+ EXPECT_EQ (fullbanner.height (), 60 );
322
+ EXPECT_EQ (fullbanner.type (), AdSize::kTypeStandard );
323
+ EXPECT_EQ (fullbanner.orientation (), AdSize::kOrientationCurrent );
324
+
325
+ const AdSize leaderboard = AdSize::kLeaderBoard ;
326
+ EXPECT_EQ (leaderboard.width (), 728 );
327
+ EXPECT_EQ (leaderboard.height (), 90 );
328
+ EXPECT_EQ (leaderboard.type (), AdSize::kTypeStandard );
329
+ EXPECT_EQ (leaderboard.orientation (), AdSize::kOrientationCurrent );
330
+
331
+ const AdSize medium_rectangle = AdSize::kMediumRectangle ;
332
+ EXPECT_EQ (medium_rectangle.width (), 300 );
333
+ EXPECT_EQ (medium_rectangle.height (), 250 );
334
+ EXPECT_EQ (medium_rectangle.type (), AdSize::kTypeStandard );
335
+ EXPECT_EQ (medium_rectangle.orientation (), AdSize::kOrientationCurrent );
336
+ }
337
+
203
338
TEST_F (FirebaseAdMobTest, TestRequestConfigurationSetGetEmptyConfig) {
204
339
SKIP_TEST_ON_DESKTOP;
205
340
@@ -273,11 +408,7 @@ TEST_F(FirebaseAdMobTest, TestBannerView) {
273
408
static const int kBannerWidth = 320 ;
274
409
static const int kBannerHeight = 50 ;
275
410
276
- firebase::admob::AdSize banner_ad_size;
277
- banner_ad_size.ad_size_type = firebase::admob::kAdSizeStandard ;
278
- banner_ad_size.width = kBannerWidth ;
279
- banner_ad_size.height = kBannerHeight ;
280
-
411
+ const firebase::admob::AdSize banner_ad_size (kBannerWidth , kBannerHeight );
281
412
firebase::admob::BannerView* banner = new firebase::admob::BannerView ();
282
413
WaitForCompletion (banner->Initialize (app_framework::GetWindowContext (),
283
414
kBannerAdUnit , banner_ad_size),
@@ -301,81 +432,106 @@ TEST_F(FirebaseAdMobTest, TestBannerView) {
301
432
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
302
433
expected_num_bounding_box_changes++;
303
434
304
- // Move to each of the six pre-defined positions.
435
+ BrieflyPauseForVisualInspection ();
305
436
437
+ // Move to each of the six pre-defined positions.
306
438
WaitForCompletion (banner->MoveTo (firebase::admob::BannerView::kPositionTop ),
307
439
" MoveTo(Top)" );
308
440
expected_presentation_states.push_back (
309
441
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
310
442
expected_num_bounding_box_changes++;
311
443
444
+ BrieflyPauseForVisualInspection ();
445
+
312
446
WaitForCompletion (
313
447
banner->MoveTo (firebase::admob::BannerView::kPositionTopLeft ),
314
448
" MoveTo(TopLeft)" );
315
449
expected_presentation_states.push_back (
316
450
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
317
451
expected_num_bounding_box_changes++;
318
452
453
+ BrieflyPauseForVisualInspection ();
454
+
319
455
WaitForCompletion (
320
456
banner->MoveTo (firebase::admob::BannerView::kPositionTopRight ),
321
457
" MoveTo(TopRight)" );
322
458
expected_presentation_states.push_back (
323
459
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
324
460
expected_num_bounding_box_changes++;
325
461
462
+ BrieflyPauseForVisualInspection ();
463
+
326
464
WaitForCompletion (
327
465
banner->MoveTo (firebase::admob::BannerView::kPositionBottom ),
328
466
" Moveto(Bottom)" );
329
467
expected_presentation_states.push_back (
330
468
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
331
469
expected_num_bounding_box_changes++;
332
470
471
+ BrieflyPauseForVisualInspection ();
472
+
333
473
WaitForCompletion (
334
474
banner->MoveTo (firebase::admob::BannerView::kPositionBottomLeft ),
335
475
" MoveTo(BottomLeft)" );
336
476
expected_presentation_states.push_back (
337
477
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
338
478
expected_num_bounding_box_changes++;
339
479
480
+ BrieflyPauseForVisualInspection ();
481
+
340
482
WaitForCompletion (
341
483
banner->MoveTo (firebase::admob::BannerView::kPositionBottomRight ),
342
484
" MoveTo(BottomRight)" );
343
485
expected_presentation_states.push_back (
344
486
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
345
487
expected_num_bounding_box_changes++;
346
488
489
+ BrieflyPauseForVisualInspection ();
490
+
347
491
// Move to some coordinates.
348
492
WaitForCompletion (banner->MoveTo (100 , 300 ), " MoveTo(x0, y0)" );
349
493
expected_presentation_states.push_back (
350
494
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
351
495
expected_num_bounding_box_changes++;
352
496
497
+ BrieflyPauseForVisualInspection ();
498
+
353
499
WaitForCompletion (banner->MoveTo (100 , 400 ), " MoveTo(x1, y1)" );
354
500
expected_presentation_states.push_back (
355
501
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
356
502
expected_num_bounding_box_changes++;
357
503
504
+ BrieflyPauseForVisualInspection ();
505
+
358
506
// Try hiding and showing the BannerView.
359
507
WaitForCompletion (banner->Hide (), " Hide 1" );
360
508
expected_presentation_states.push_back (
361
509
firebase::admob::BannerView::kPresentationStateHidden );
362
510
511
+ BrieflyPauseForVisualInspection ();
512
+
363
513
WaitForCompletion (banner->Show (), " Show 1" );
364
514
expected_presentation_states.push_back (
365
515
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
366
516
expected_num_bounding_box_changes++;
367
517
518
+ BrieflyPauseForVisualInspection ();
519
+
368
520
// Move again after hiding/showing.
369
521
WaitForCompletion (banner->MoveTo (100 , 300 ), " MoveTo(x2, y2)" );
370
522
expected_presentation_states.push_back (
371
523
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
372
524
expected_num_bounding_box_changes++;
373
525
526
+ BrieflyPauseForVisualInspection ();
527
+
374
528
WaitForCompletion (banner->MoveTo (100 , 400 ), " Moveto(x3, y3)" );
375
529
expected_presentation_states.push_back (
376
530
firebase::admob::BannerView::kPresentationStateVisibleWithAd );
377
531
expected_num_bounding_box_changes++;
378
532
533
+ BrieflyPauseForVisualInspection ();
534
+
379
535
WaitForCompletion (banner->Hide (), " Hide 2" );
380
536
expected_presentation_states.push_back (
381
537
firebase::admob::BannerView::kPresentationStateHidden );
@@ -429,11 +585,7 @@ TEST_F(FirebaseAdMobTest, TestBannerViewAlreadyInitialized) {
429
585
static const int kBannerWidth = 320 ;
430
586
static const int kBannerHeight = 50 ;
431
587
432
- firebase::admob::AdSize banner_ad_size;
433
- banner_ad_size.ad_size_type = firebase::admob::kAdSizeStandard ;
434
- banner_ad_size.width = kBannerWidth ;
435
- banner_ad_size.height = kBannerHeight ;
436
-
588
+ const firebase::admob::AdSize banner_ad_size (kBannerWidth , kBannerHeight );
437
589
firebase::admob::BannerView* banner = new firebase::admob::BannerView ();
438
590
439
591
{
@@ -464,6 +616,26 @@ TEST_F(FirebaseAdMobTest, TestBannerViewAlreadyInitialized) {
464
616
}
465
617
}
466
618
619
+ TEST_F (FirebaseAdMobTest, TestBannerViewWithBadExtrasClassName) {
620
+ SKIP_TEST_ON_DESKTOP;
621
+
622
+ static const int kBannerWidth = 320 ;
623
+ static const int kBannerHeight = 50 ;
624
+
625
+ const firebase::admob::AdSize banner_ad_size (kBannerWidth , kBannerHeight );
626
+ firebase::admob::BannerView* banner = new firebase::admob::BannerView ();
627
+ WaitForCompletion (banner->Initialize (app_framework::GetWindowContext (),
628
+ kBannerAdUnit , banner_ad_size),
629
+ " Initialize" );
630
+
631
+ // Load the banner ad.
632
+ firebase::admob::AdRequest request = GetAdRequest ();
633
+ request.add_extra (kAdNetworkExtrasInvalidClassName , " shouldnot" , " work" );
634
+ WaitForCompletion (banner->LoadAd (request), " LoadAd" ,
635
+ firebase::admob::kAdMobErrorAdNetworkClassLoadError );
636
+ delete banner;
637
+ }
638
+
467
639
// A simple listener to help test changes to a InterstitialAd.
468
640
class TestInterstitialAdListener
469
641
: public firebase::admob::InterstitialAd::Listener {
@@ -543,10 +715,7 @@ TEST_F(FirebaseAdMobTest, TestBannerViewMultithreadDeletion) {
543
715
static const int kBannerWidth = 320 ;
544
716
static const int kBannerHeight = 50 ;
545
717
546
- firebase::admob::AdSize banner_ad_size;
547
- banner_ad_size.ad_size_type = firebase::admob::kAdSizeStandard ;
548
- banner_ad_size.width = kBannerWidth ;
549
- banner_ad_size.height = kBannerHeight ;
718
+ const firebase::admob::AdSize banner_ad_size (kBannerWidth , kBannerHeight );
550
719
551
720
for (int i = 0 ; i < 5 ; ++i) {
552
721
firebase::admob::BannerView* banner = new firebase::admob::BannerView ();
0 commit comments