Skip to content

Commit

Permalink
feat(ads-module): open debug menu programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan-24com authored Apr 4, 2023
1 parent c443ec2 commit 3480f1b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 0 deletions.
40 changes: 40 additions & 0 deletions RNGoogleMobileAdsExample/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,45 @@ class GAMInterstitialTest implements Test {
}
}

class DebugMenuTest implements Test {
constructor() {
// Android requires SDK initialization before opening the Debug Menu
Platform.OS === 'android' && MobileAds().initialize().catch(console.error);
}

getPath(): string {
return 'DebugMenuTest';
}

getTestType(): TestType {
return TestType.Interactive;
}

render(onMount: (component: any) => void): React.ReactNode {
return (
<View style={styles.testSpacing} ref={onMount}>
<Button
title="Show Ad Debug Menu"
onPress={() => {
MobileAds().openDebugMenu(TestIds.BANNER);
}}
/>
</View>
);
}

execute(component: any, complete: (result: TestResult) => void): void {
let results = new TestResult();
try {
// You can do anything here, it will execute on-device + in-app. Results are aggregated + visible in-app.
} catch (error) {
results.errors.push('Received unexpected error...');
} finally {
complete(results);
}
}
}

// All tests must be registered - a future feature will allow auto-bundling of tests via configured path or regex
Object.keys(BannerAdSize).forEach(bannerAdSize => {
TestRegistry.registerTest(new BannerTest(bannerAdSize));
Expand All @@ -865,6 +904,7 @@ TestRegistry.registerTest(new RewardedInterstitialHookTest());
TestRegistry.registerTest(new AdInspectorTest());
TestRegistry.registerTest(new GAMBannerTest());
TestRegistry.registerTest(new GAMInterstitialTest());
TestRegistry.registerTest(new DebugMenuTest());

const App = () => {
return (
Expand Down
8 changes: 8 additions & 0 deletions __tests__/googleMobileAds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,13 @@ describe('Admob', function () {
);
});
});

describe('testDebugMenu', function () {
it('throws if adUnit is empty', function () {
expect(() => {
admob().openDebugMenu('');
}).toThrowError('openDebugMenu expected a non-empty string value');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,12 @@ public void onAdInspectorClosed(@Nullable AdInspectorError adInspectorError) {
});
});
}

@ReactMethod
public void openDebugMenu(final String adUnit) {
if (getCurrentActivity() != null) {
getCurrentActivity()
.runOnUiThread(() -> MobileAds.openDebugMenu(getCurrentActivity(), adUnit));
}
}
}
9 changes: 9 additions & 0 deletions ios/RNGoogleMobileAds/RNGoogleMobileAdsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,13 @@ - (void)setRequestConfiguration:(NSDictionary *)requestConfiguration {
}];
}

RCT_EXPORT_METHOD(openDebugMenu : (NSString *)adUnit) {
GADDebugOptionsViewController *debugOptionsViewController =
[GADDebugOptionsViewController debugOptionsViewControllerWithAdUnitID:adUnit];
[RCTSharedApplication().delegate.window.rootViewController
presentViewController:debugOptionsViewController
animated:YES
completion:nil];
}

@end
5 changes: 5 additions & 0 deletions src/MobileAds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class MobileAdsModule extends Module implements MobileAdsModuleInterface {
openAdInspector() {
return this.native.openAdInspector();
}

openDebugMenu(adUnit: string) {
if (!adUnit) throw new Error('googleMobileAds.openDebugMenu expected a non-empty string value');
this.native.openDebugMenu(adUnit);
}
}

const MobileAdsInstance = new MobileAdsModule(
Expand Down
1 change: 1 addition & 0 deletions src/types/GoogleMobileAdsNativeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export interface GoogleMobileAdsNativeModule {
rewardedShow: AdShowFunction;
rewardedInterstitialLoad: AdLoadFunction;
rewardedInterstitialShow: AdShowFunction;
openDebugMenu(adUnit: string): void;
}
12 changes: 12 additions & 0 deletions src/types/MobileAdsModule.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,16 @@ export interface MobileAdsModuleInterface {
* Returns the shared event emitter instance used for all JS event routing.
*/
emitter: EventEmitter;

/**
* Opens the Ad Debug Menu.
*
* Android: `initialize` needs to be called before calling this function.
*
* @see https://developers.google.com/ad-manager/mobile-ads-sdk/android/debug
* @see https://developers.google.com/ad-manager/mobile-ads-sdk/ios/debug
*
* @param adUnit Any valid ad unit from your Ad Manager account is sufficient to open the debug options menu.
*/
openDebugMenu(adUnit: string): void;
}

0 comments on commit 3480f1b

Please sign in to comment.