Skip to content

Commit da33f2f

Browse files
authored
Made FlutterWindowControllerSizeTest.SizedToContentNotResizable more robust (flutter#195845)
Made `FlutterWindowControllerSizeTest.SizedToContentNotResizable` more robust to timing failures where `onBeginFrame` is called before the window is created. This test used to flake because the `onBeginFrame` callback was being called before the window was created so the frame was getting sent to the wrong view. The testing logic is also made more robust by hooking into the notification center events instead of assuming that only one update will happen per run of the event loop. The actual fix is in the dart testing code `flutter_desktop_test.dart`. I've tested this locally where I could reproduce the flake 1/300 times, now the tests runs 1000/1000. fixes flutter#194780 gemini output: <details> > I've found the real cause, fixed it, and staged the fix. It's different from my first diagnosis: the test wasn't missing a size change, the first frame was going to the wrong view. > > **Cause:** the Dart fixture [`testRenderSizedToContent`](file:///Users/aaclarke/dev/flutter/engine/src/flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart#L124-L153) draws into `PlatformDispatcher.instance.views.last` and assumes its first frame lands in the test's window. > - Sometimes a frame starts before the test's `CreateRegularWindow` call has run. At that moment `views.last` is the implicit view (id 0), which has no window in this test. > - The 300×300 frame goes to view 0. Creating its render surface fails (`Could not create the embedder backing store`), so it's dropped. > - The frame counter has already moved past 0, so the new window only ever gets the 200×200 frame. The test then waits for 300×300 until its 30 s timeout, which matches the CI log. > > The temporary logging confirmed this directly: the failing 300×300 frame was for view id 0, and the test created view 1 a few ms later. > > **Staged changes (2 files):** > 1. **Fix**, in `flutter_desktop_test.dart`: `onBeginFrame` now skips any frame where `views.last` is the implicit view, then renders into the test's window. A later frame is triggered when the window's view is added, so nothing hangs. > 2. **Test hardening**, in [FlutterWindowControllerTest.mm](file:///Users/aaclarke/dev/flutter/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterWindowControllerTest.mm#L781-L826): the resize-notification version from before. On failure it now also lists every size the window went through; that list is what pointed to the real cause here. > > The temporary logging in `FlutterCompositor.mm`, `FlutterViewEngineProvider.mm` and `FlutterWindowController.mm` is reverted. The `git checkout` I used for that printed a hook notice suggesting `gclient sync -D`; I didn't run it. > > **Verification:** > > | Run | Result | > |---|---| > | Test hardening only, before the fixture fix | 2 failures in 600 runs, both from the frame going to view 0 | > | Both changes, `FlutterWindowControllerSizeTest.*` × 1000 | **1000/1000 passed** | > | All `FlutterWindowController*` tests | 16/16 passed | > > `clang-format`, `dart format` and `dart analyze --fatal-infos` are all clean. > > `SizedToContentResizable` still sends its first frame to view 0 sometimes (14 of 1000 runs). It passes anyway because that fixture draws 300×300 on every frame, so a later frame fixes it. I can add the same implicit-view skip there for consistency if you'd like. > </details> ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 1261478 commit da33f2f

2 files changed

Lines changed: 48 additions & 20 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterWindowControllerTest.mm‎

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include <algorithm>
6+
#include <vector>
7+
58
#import "flutter/shell/platform/common/isolate_scope.h"
69
#import "flutter/shell/platform/common/windowing.h"
710

@@ -776,31 +779,48 @@ static void RunLoopUntilPredicate(NSTimeInterval timeout, bool (^predicate)(void
776779
}
777780

778781
auto controller = [engine viewControllerForIdentifier:viewId];
779-
auto window = controller.view.window;
782+
NSWindow* window = controller.view.window;
783+
784+
// The fixture renders a 300x300 frame followed immediately by a 200x200 frame.
785+
// Both frame commits can be processed within a single run loop turn, so
786+
// polling window.frame may never observe the intermediate size. Record every
787+
// content size the window is resized to instead.
788+
// See https://github.com/flutter/flutter/issues/194780.
789+
__block std::vector<NSSize> contentSizes;
790+
id observer = [[NSNotificationCenter defaultCenter]
791+
addObserverForName:NSWindowDidResizeNotification
792+
object:window
793+
queue:nil
794+
usingBlock:^(NSNotification* note) {
795+
NSWindow* resizedWindow = note.object;
796+
contentSizes.push_back(
797+
[resizedWindow contentRectForFrameRect:resizedWindow.frame].size);
798+
}];
780799

781-
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
782800
double pixelRatio = [window backingScaleFactor];
783-
while (window.frame.size.width != 300 / pixelRatio &&
784-
CFAbsoluteTimeGetCurrent() - startTime < kTestTimeout) {
785-
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true);
786-
}
787-
788-
NSRect contentRect = [window contentRectForFrameRect:window.frame];
789-
EXPECT_EQ(contentRect.size.width, 300 / pixelRatio);
790-
EXPECT_EQ(contentRect.size.height, 300 / pixelRatio);
791-
EXPECT_TRUE((window.styleMask & NSWindowStyleMaskResizable) == 0);
801+
NSSize firstSize = NSMakeSize(300 / pixelRatio, 300 / pixelRatio);
802+
NSSize secondSize = NSMakeSize(200 / pixelRatio, 200 / pixelRatio);
792803

793804
// Wait until the second frame is rendered, which should resize the window based
794805
// on new content.
795-
startTime = CFAbsoluteTimeGetCurrent();
796-
while (window.frame.size.width == 300 / pixelRatio &&
797-
CFAbsoluteTimeGetCurrent() - startTime < kTestTimeout) {
798-
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true);
806+
RunLoopUntilPredicate(kTestTimeout, ^bool {
807+
return !contentSizes.empty() && NSEqualSizes(contentSizes.back(), secondSize);
808+
});
809+
[[NSNotificationCenter defaultCenter] removeObserver:observer];
810+
811+
// The window must have been sized to the first frame before the second.
812+
auto firstSizeIt = std::find_if(contentSizes.begin(), contentSizes.end(),
813+
[&](NSSize size) { return NSEqualSizes(size, firstSize); });
814+
std::string observedSizes;
815+
for (NSSize size : contentSizes) {
816+
observedSizes += NSStringFromSize(size).UTF8String;
817+
observedSizes += " ";
799818
}
800-
801-
contentRect = [window contentRectForFrameRect:window.frame];
802-
EXPECT_EQ(contentRect.size.width, 200 / pixelRatio);
803-
EXPECT_EQ(contentRect.size.height, 200 / pixelRatio);
819+
EXPECT_NE(firstSizeIt, contentSizes.end())
820+
<< "Window was never sized to the first frame. Observed sizes: " << observedSizes;
821+
ASSERT_FALSE(contentSizes.empty());
822+
EXPECT_TRUE(NSEqualSizes(contentSizes.back(), secondSize));
823+
EXPECT_TRUE((window.styleMask & NSWindowStyleMaskResizable) == 0);
804824

805825
[engine.windowController closeAllWindows];
806826
[engine shutDownEngine];

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ void testRenderSizedToContentResizable() {
126126
void testRenderSizedToContent() {
127127
var frameCount = 0;
128128
PlatformDispatcher.instance.onBeginFrame = (Duration duration) {
129+
final FlutterView view = PlatformDispatcher.instance.views.last;
130+
// A frame may begin before the test has created its window, in which case
131+
// the only view is the implicit view, which has no window in this test.
132+
// Skip such frames so that the first frame rendered to the test's window is
133+
// the 300x300 one. See https://github.com/flutter/flutter/issues/194780.
134+
if (view == PlatformDispatcher.instance.implicitView) {
135+
return;
136+
}
129137
final size = frameCount == 0 ? const Size(300, 300) : const Size(200, 200);
130138
final baseRecorder = PictureRecorder();
131139
final canvas = Canvas(baseRecorder);
@@ -134,7 +142,7 @@ void testRenderSizedToContent() {
134142
final Picture picture = baseRecorder.endRecording();
135143
final builder = SceneBuilder();
136144
builder.addPicture(Offset.zero, picture);
137-
PlatformDispatcher.instance.views.last.render(builder.build(), size: size);
145+
view.render(builder.build(), size: size);
138146
++frameCount;
139147
if (frameCount == 1) {
140148
PlatformDispatcher.instance.scheduleFrame();

0 commit comments

Comments
 (0)