-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Use a more deterministic way of waiting for ad widgets to be ready. #8920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
auto-submit
merged 4 commits into
flutter:main
from
eyebrowsoffire:adsense_unit_test_fix
Mar 27, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,17 @@ import 'js_interop_mocks/adsense_test_js_interop.dart'; | |
const String testClient = 'test_client'; | ||
const String testSlot = 'test_slot'; | ||
|
||
class CallbackTracker { | ||
void Function() createCallback() { | ||
final int callbackIndex = _callbackStates.length; | ||
_callbackStates.add(false); | ||
return () => _callbackStates[callbackIndex] = true; | ||
} | ||
|
||
bool get allCalled => _callbackStates.every((bool state) => state); | ||
final List<bool> _callbackStates = <bool>[]; | ||
ditman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
void main() async { | ||
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
|
@@ -47,15 +58,17 @@ void main() async { | |
|
||
await adSense.initialize(testClient); | ||
|
||
final CallbackTracker tracker = CallbackTracker(); | ||
final Widget adUnitWidget = AdUnitWidget( | ||
configuration: AdUnitConfiguration.displayAdUnit( | ||
adSlot: testSlot, | ||
adFormat: AdFormat.AUTO, // Important! | ||
), | ||
adClient: adSense.adClient, | ||
onInjected: tracker.createCallback(), | ||
); | ||
|
||
await pumpAdWidget(adUnitWidget, tester); | ||
await pumpAdWidget(adUnitWidget, tester, tracker); | ||
|
||
// Then | ||
// Widget level | ||
|
@@ -81,19 +94,21 @@ void main() async { | |
|
||
await adSense.initialize(testClient); | ||
|
||
final CallbackTracker tracker = CallbackTracker(); | ||
final Widget adUnitWidget = AdUnitWidget( | ||
configuration: AdUnitConfiguration.displayAdUnit( | ||
adSlot: testSlot, | ||
), | ||
adClient: adSense.adClient, | ||
onInjected: tracker.createCallback(), | ||
); | ||
|
||
final Widget constrainedAd = Container( | ||
constraints: constraints, | ||
child: adUnitWidget, | ||
); | ||
|
||
await pumpAdWidget(constrainedAd, tester); | ||
await pumpAdWidget(constrainedAd, tester, tracker); | ||
|
||
// Then | ||
// Widget level | ||
|
@@ -110,14 +125,17 @@ void main() async { | |
mockAdsByGoogle(mockAd(adStatus: AdStatus.UNFILLED)); | ||
|
||
await adSense.initialize(testClient); | ||
|
||
final CallbackTracker tracker = CallbackTracker(); | ||
final Widget adUnitWidget = AdUnitWidget( | ||
configuration: AdUnitConfiguration.displayAdUnit( | ||
adSlot: testSlot, | ||
), | ||
adClient: adSense.adClient, | ||
onInjected: tracker.createCallback(), | ||
); | ||
|
||
await pumpAdWidget(adUnitWidget, tester); | ||
await pumpAdWidget(adUnitWidget, tester, tracker); | ||
|
||
// Then | ||
expect(find.byType(HtmlElementView), findsNothing, | ||
|
@@ -142,6 +160,7 @@ void main() async { | |
|
||
await adSense.initialize(testClient); | ||
|
||
final CallbackTracker tracker = CallbackTracker(); | ||
final Widget bunchOfAds = Column( | ||
children: <Widget>[ | ||
AdUnitWidget( | ||
|
@@ -150,13 +169,15 @@ void main() async { | |
adFormat: AdFormat.AUTO, | ||
), | ||
adClient: adSense.adClient, | ||
onInjected: tracker.createCallback(), | ||
), | ||
AdUnitWidget( | ||
configuration: AdUnitConfiguration.displayAdUnit( | ||
adSlot: testSlot, | ||
adFormat: AdFormat.AUTO, | ||
), | ||
adClient: adSense.adClient, | ||
onInjected: tracker.createCallback(), | ||
), | ||
Container( | ||
constraints: const BoxConstraints(maxHeight: 100), | ||
|
@@ -165,12 +186,13 @@ void main() async { | |
adSlot: testSlot, | ||
), | ||
adClient: adSense.adClient, | ||
onInjected: tracker.createCallback(), | ||
), | ||
), | ||
], | ||
); | ||
|
||
await pumpAdWidget(bunchOfAds, tester); | ||
await pumpAdWidget(bunchOfAds, tester, tracker); | ||
|
||
// Then | ||
// Widget level | ||
|
@@ -192,7 +214,8 @@ void main() async { | |
} | ||
|
||
// Pumps an AdUnit Widget into a given tester, with some parameters | ||
Future<void> pumpAdWidget(Widget adUnit, WidgetTester tester) async { | ||
Future<void> pumpAdWidget( | ||
Widget adUnit, WidgetTester tester, CallbackTracker tracker) async { | ||
await tester.pumpWidget( | ||
MaterialApp( | ||
home: Scaffold( | ||
|
@@ -203,10 +226,14 @@ Future<void> pumpAdWidget(Widget adUnit, WidgetTester tester) async { | |
), | ||
); | ||
|
||
// This extra pump is needed for the platform view to actually render in the DOM. | ||
await tester.pump(); | ||
// One more for skwasm. | ||
await tester.pump(); | ||
final Stopwatch timer = Stopwatch()..start(); | ||
while (!tracker.allCalled) { | ||
ditman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (timer.elapsedMilliseconds > 1000) { | ||
fail('timeout while waiting for ad widget to be injected'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sweet! |
||
} | ||
// Pump until all the widgets have had their platform views injected into the dom. | ||
await tester.pump(); | ||
} | ||
// This extra pump is needed to simulate the async behavior of the adsense JS mock. | ||
await tester.pumpAndSettle(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.