[macOS] Reuse NSWindow instance across unit tests (flutter/engine#47350)

Adds a gtest test fixture mixin and convenience class that instantiates an NSAutoreleasePool at the beginning of each test and flushes it at the end; this allows Objective-C tests using ARC to free  allocations at the end of each test.

Adds a subclass for the macOS accessibility bridge tests that instantiates and re-uses an NSWindow* across any tests that use it. This is because instantiating, closing, and immediately collecting an NSWindow results in a crash.

Prior to this patch, tests started failing (on my machine) around the 855th iteration, and issued the following warning on the 101st iteration:

```
2023-10-26 13:02:45.390829-0700 flutter_desktop_darwin_unittests[40837:1509026] [Window] WARNING: NSWindow has detected an excessive live window count of 101. Window 0x1423 of class 'NSWindow' created after passing the threshold of 100. This window is not necessarily the cause, and this warning will only be shown once per window class. (
  0   AppKit                              0x0000000192820d28 -[NSWindow _setWindowNumber:] + 684
  1   AppKit                              0x00000001933050e4 _NXCreateWindow + 284
  2   AppKit                              0x0000000192901ae0 -[NSWindow _commonAwake] + 672
  3   AppKit                              0x000000019281ff00 -[NSWindow _commonInitFrame:styleMask:backing:defer:] + 972
  4   AppKit                              0x000000019281f798 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 796
  5   AppKit                              0x000000019281f470 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 48
  6   flutter_desktop_darwin_unittests    0x0000000100001e3c _ZN7flutter7testing89AccessibilityBridgeMacTest_SendsAccessibilityCreateNotificationToWindowOfFlutterView_Test8TestBodyEv + 328
```

See: http://www.openradar.me/FB13291861

Issue: https://github.com/flutter/flutter/issues/104789
Issue: https://github.com/flutter/flutter/issues/127441
Issue: https://github.com/flutter/flutter/issues/124840

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Chris Bracken
2023-10-26 14:17:01 -07:00
committed by GitHub
parent 269225124c
commit 987dac0ae1
4 changed files with 104 additions and 15 deletions

View File

@@ -211,6 +211,7 @@ executable("flutter_desktop_darwin_unittests") {
"//flutter/shell/platform/embedder:embedder_as_internal_library",
"//flutter/shell/platform/embedder:embedder_test_utils",
"//flutter/testing",
"//flutter/testing:autoreleasepool_test",
"//flutter/testing:dart",
"//flutter/testing:skia",
"//flutter/testing:testing_lib",

View File

@@ -1,12 +1,14 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/testing/testing.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMac.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
#include "flutter/testing/autoreleasepool_test.h"
#include "flutter/testing/testing.h"
namespace flutter::testing {
@@ -56,17 +58,48 @@ FlutterViewController* CreateTestViewController() {
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
return [[AccessibilityBridgeTestViewController alloc] initWithProject:project];
}
// Test fixture that instantiates and re-uses a single NSWindow across multiple tests.
//
// Works around: http://www.openradar.me/FB13291861
class AccessibilityBridgeMacWindowTest : public AutoreleasePoolTest {
public:
AccessibilityBridgeMacWindowTest() {
if (!gWindow_) {
gWindow_ = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
}
}
NSWindow* GetWindow() const { return gWindow_; }
private:
static NSWindow* gWindow_;
FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridgeMacWindowTest);
};
// AutoreleasePoolTest subclass that exists simply to provide more specific naming.
class AccessibilityBridgeMacTest : public AutoreleasePoolTest {
public:
AccessibilityBridgeMacTest() = default;
~AccessibilityBridgeMacTest() = default;
private:
FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridgeMacTest);
};
NSWindow* AccessibilityBridgeMacWindowTest::gWindow_ = nil;
} // namespace
TEST(AccessibilityBridgeMacTest, SendsAccessibilityCreateNotificationToWindowOfFlutterView) {
TEST_F(AccessibilityBridgeMacWindowTest, SendsAccessibilityCreateNotificationFlutterViewWindow) {
FlutterViewController* viewController = CreateTestViewController();
FlutterEngine* engine = viewController.engine;
NSWindow* expectedTarget = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
NSWindow* expectedTarget = GetWindow();
expectedTarget.contentView = viewController.view;
// Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
// can query semantics information from.
engine.semanticsEnabled = YES;
@@ -118,15 +151,12 @@ TEST(AccessibilityBridgeMacTest, SendsAccessibilityCreateNotificationToWindowOfF
// node1
// |
// node2
TEST(AccessibilityBridgeMacTest, NonZeroRootNodeId) {
TEST_F(AccessibilityBridgeMacWindowTest, NonZeroRootNodeId) {
FlutterViewController* viewController = CreateTestViewController();
FlutterEngine* engine = viewController.engine;
NSWindow* expectedTarget = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
NSWindow* expectedTarget = GetWindow();
expectedTarget.contentView = viewController.view;
// Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
// can query semantics information from.
engine.semanticsEnabled = YES;
@@ -187,7 +217,7 @@ TEST(AccessibilityBridgeMacTest, NonZeroRootNodeId) {
[engine shutDownEngine];
}
TEST(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenHeadless) {
TEST_F(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenHeadless) {
FlutterViewController* viewController = CreateTestViewController();
FlutterEngine* engine = viewController.engine;
@@ -233,7 +263,7 @@ TEST(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenH
[engine shutDownEngine];
}
TEST(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenNoWindow) {
TEST_F(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenNoWindow) {
FlutterViewController* viewController = CreateTestViewController();
FlutterEngine* engine = viewController.engine;

View File

@@ -124,6 +124,19 @@ source_set("fixture_test") {
}
}
if (is_mac || is_ios) {
source_set("autoreleasepool_test") {
testonly = true
sources = [ "autoreleasepool_test.h" ]
deps = [
"//flutter/fml",
"//third_party/googletest:gtest",
]
}
}
if (enable_unittests && shell_enable_vulkan) {
source_set("vulkan") {
testonly = true

View File

@@ -0,0 +1,45 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_TESTING_AUTORELEASEPOOL_TEST_H_
#define FLUTTER_TESTING_AUTORELEASEPOOL_TEST_H_
#include "flutter/fml/platform/darwin/scoped_nsautorelease_pool.h"
#include "gtest/gtest.h"
namespace flutter::testing {
// GoogleTest mixin that runs the test within the scope of an NSAutoReleasePool.
//
// This can be mixed into test fixture classes that also inherit from gtest's
// ::testing::Test base class.
class AutoreleasePoolTestMixin {
public:
AutoreleasePoolTestMixin() = default;
~AutoreleasePoolTestMixin() = default;
private:
fml::ScopedNSAutoreleasePool autorelease_pool_;
FML_DISALLOW_COPY_AND_ASSIGN(AutoreleasePoolTestMixin);
};
// GoogleTest fixture that runs the test within the scope of an
// NSAutoReleasePool.
class AutoreleasePoolTest : public ::testing::Test,
public AutoreleasePoolTestMixin {
public:
AutoreleasePoolTest() = default;
~AutoreleasePoolTest() = default;
private:
fml::ScopedNSAutoreleasePool autorelease_pool_;
FML_DISALLOW_COPY_AND_ASSIGN(AutoreleasePoolTest);
};
} // namespace flutter::testing
#endif // FLUTTER_TESTING_AUTORELEASEPOOL_TEST_H_