diff --git a/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn b/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn index d36e9b1486..56400d2a5b 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/macos/BUILD.gn @@ -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", diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacTest.mm index 1b3c10a002..cdecfc825e 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacTest.mm @@ -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; diff --git a/engine/src/flutter/testing/BUILD.gn b/engine/src/flutter/testing/BUILD.gn index 390ed9ed73..cd7dd82529 100644 --- a/engine/src/flutter/testing/BUILD.gn +++ b/engine/src/flutter/testing/BUILD.gn @@ -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 diff --git a/engine/src/flutter/testing/autoreleasepool_test.h b/engine/src/flutter/testing/autoreleasepool_test.h new file mode 100644 index 0000000000..2e11da5553 --- /dev/null +++ b/engine/src/flutter/testing/autoreleasepool_test.h @@ -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_