Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0906a1d

Browse files
committed
[Darwin] Move FlutterBinaryMessengerRelay to common
Migrates FlutterBinaryMessengerRelay to //flutter/shell/platform/darwin/common so that it can be used in a followup patch on macOS, to fix a memory leak due to a retain cycle. Migrates the unit tests to be compatible with gtest, which is used by the darwin/common unit tests. Issue: flutter/flutter#116445
1 parent 4f4734c commit 0906a1d

File tree

10 files changed

+100
-32
lines changed

10 files changed

+100
-32
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,9 @@ ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterC
25672567
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterDartProject.h + ../../../flutter/LICENSE
25682568
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h + ../../../flutter/LICENSE
25692569
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h + ../../../flutter/LICENSE
2570+
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.h + ../../../flutter/LICENSE
2571+
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.mm + ../../../flutter/LICENSE
2572+
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelayTest.mm + ../../../flutter/LICENSE
25702573
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannels.mm + ../../../flutter/LICENSE
25712574
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannelsTest.m + ../../../flutter/LICENSE
25722575
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm + ../../../flutter/LICENSE
@@ -2576,6 +2579,8 @@ ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterSt
25762579
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.cc + ../../../flutter/LICENSE
25772580
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.h + ../../../flutter/LICENSE
25782581
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodec_Internal.h + ../../../flutter/LICENSE
2582+
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterTestUtils.h + ../../../flutter/LICENSE
2583+
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterTestUtils.mm + ../../../flutter/LICENSE
25792584
ORIGIN: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.h + ../../../flutter/LICENSE
25802585
ORIGIN: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.mm + ../../../flutter/LICENSE
25812586
ORIGIN: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.h + ../../../flutter/LICENSE
@@ -2595,9 +2600,6 @@ ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterView
25952600
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm + ../../../flutter/LICENSE
25962601
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm + ../../../flutter/LICENSE
25972602
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h + ../../../flutter/LICENSE
2598-
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h + ../../../flutter/LICENSE
2599-
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.mm + ../../../flutter/LICENSE
2600-
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelayTest.mm + ../../../flutter/LICENSE
26012603
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterCallbackCache.mm + ../../../flutter/LICENSE
26022604
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterCallbackCache_Internal.h + ../../../flutter/LICENSE
26032605
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterChannelKeyResponder.h + ../../../flutter/LICENSE
@@ -5296,6 +5298,9 @@ FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterCod
52965298
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterDartProject.h
52975299
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h
52985300
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h
5301+
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.h
5302+
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.mm
5303+
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelayTest.mm
52995304
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannels.mm
53005305
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannelsTest.m
53015306
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm
@@ -5305,6 +5310,8 @@ FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStan
53055310
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.cc
53065311
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.h
53075312
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodec_Internal.h
5313+
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterTestUtils.h
5314+
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterTestUtils.mm
53085315
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.h
53095316
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.mm
53105317
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.h
@@ -5325,9 +5332,6 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Info.plist
53255332
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm
53265333
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm
53275334
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h
5328-
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h
5329-
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.mm
5330-
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelayTest.mm
53315335
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterCallbackCache.mm
53325336
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterCallbackCache_Internal.h
53335337
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterChannelKeyResponder.h

shell/platform/darwin/common/BUILD.gn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ source_set("framework_common") {
3737
cflags_objcc = flutter_cflags_objcc_arc
3838

3939
sources = [
40+
"framework/Source/FlutterBinaryMessengerRelay.mm",
4041
"framework/Source/FlutterChannels.mm",
4142
"framework/Source/FlutterCodecs.mm",
4243
"framework/Source/FlutterNSBundleUtils.h",
@@ -69,6 +70,8 @@ executable("framework_common_unittests") {
6970
testonly = true
7071

7172
sources = [
73+
"framework/Source/FlutterBinaryMessengerRelayTest.mm",
74+
"framework/Source/FlutterTestUtils.mm",
7275
"framework/Source/flutter_codecs_unittest.mm",
7376
"framework/Source/flutter_standard_codec_unittest.mm",
7477
]
@@ -82,6 +85,7 @@ executable("framework_common_unittests") {
8285
":framework_common_fixtures",
8386
"//flutter/testing",
8487
"//third_party/dart/runtime:libdart_jit",
88+
"//third_party/ocmock:ocmock",
8589
]
8690

8791
public_configs = [ "//flutter:config" ]

shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h renamed to shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.h

Lines changed: 5 additions & 0 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+
#ifndef SHELL_PLATFORM_DARWIN_COMMON_FRAMEWORK_SOURCE_FLUTTERBINARYMESSENGERRELAY_H_
6+
#define SHELL_PLATFORM_DARWIN_COMMON_FRAMEWORK_SOURCE_FLUTTERBINARYMESSENGERRELAY_H_
7+
58
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h"
69
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
710

@@ -12,3 +15,5 @@ FLUTTER_DARWIN_EXPORT
1215
@property(nonatomic, assign) NSObject<FlutterBinaryMessenger>* parent;
1316
- (instancetype)initWithParent:(NSObject<FlutterBinaryMessenger>*)parent;
1417
@end
18+
19+
#endif // SHELL_PLATFORM_DARWIN_COMMON_FRAMEWORK_SOURCE_FLUTTERBINARYMESSENGERRELAY_H_

shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.mm renamed to shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h"
5+
#import "flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.h"
66

77
#include "flutter/fml/logging.h"
88

9+
FLUTTER_ASSERT_ARC
10+
911
@implementation FlutterBinaryMessengerRelay
1012
#pragma mark - FlutterBinaryMessenger
1113

shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelayTest.mm renamed to shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelayTest.mm

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

5-
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h"
5+
#import "flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.h"
66

77
#import <OCMock/OCMock.h>
8-
#import <XCTest/XCTest.h>
98

109
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
10+
#import "flutter/shell/platform/darwin/common/framework/Source/FlutterTestUtils.h"
11+
#import "flutter/testing/testing.h"
12+
#include "gtest/gtest.h"
1113

1214
FLUTTER_ASSERT_ARC
1315

1416
@protocol FlutterTaskQueue <NSObject>
1517
@end
1618

17-
@interface FlutterBinaryMessengerRelayTest : XCTestCase
19+
@interface FlutterBinaryMessengerRelayTest : NSObject
1820
@end
1921

2022
@implementation FlutterBinaryMessengerRelayTest
2123

22-
- (void)setUp {
23-
}
24-
25-
- (void)tearDown {
26-
}
27-
28-
- (void)testCreate {
24+
- (BOOL)testCreate {
2925
id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
3026
FlutterBinaryMessengerRelay* relay =
3127
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
32-
XCTAssertNotNil(relay);
33-
XCTAssertEqual(messenger, relay.parent);
28+
EXPECT_NE(relay, nil);
29+
EXPECT_EQ(messenger, relay.parent);
30+
return YES;
3431
}
3532

36-
- (void)testPassesCallOn {
33+
- (BOOL)testPassesCallOn {
3734
id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
3835
FlutterBinaryMessengerRelay* relay =
3936
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
4037
char messageData[] = {'a', 'a', 'r', 'o', 'n'};
4138
NSData* message = [NSData dataWithBytes:messageData length:sizeof(messageData)];
4239
NSString* channel = @"foobar";
4340
[relay sendOnChannel:channel message:message binaryReply:nil];
44-
OCMVerify([messenger sendOnChannel:channel message:message binaryReply:nil]);
41+
return !FLTThrowsObjcException(^{
42+
OCMVerify([messenger sendOnChannel:channel message:message binaryReply:nil]);
43+
});
4544
}
4645

47-
- (void)testDoesntPassCallOn {
46+
- (BOOL)testDoesntPassCallOn {
4847
id messenger = OCMStrictProtocolMock(@protocol(FlutterBinaryMessenger));
4948
FlutterBinaryMessengerRelay* relay =
5049
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
@@ -53,9 +52,10 @@ - (void)testDoesntPassCallOn {
5352
NSString* channel = @"foobar";
5453
relay.parent = nil;
5554
[relay sendOnChannel:channel message:message binaryReply:nil];
55+
return YES;
5656
}
5757

58-
- (void)testSetMessageHandlerWithTaskQueue {
58+
- (BOOL)testSetMessageHandlerWithTaskQueue {
5959
id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
6060
FlutterBinaryMessengerRelay* relay =
6161
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
@@ -64,17 +64,41 @@ - (void)testSetMessageHandlerWithTaskQueue {
6464
FlutterBinaryMessageHandler handler = ^(NSData* _Nullable, FlutterBinaryReply _Nonnull) {
6565
};
6666
[relay setMessageHandlerOnChannel:channel binaryMessageHandler:handler taskQueue:taskQueue];
67-
OCMVerify([messenger setMessageHandlerOnChannel:channel
68-
binaryMessageHandler:handler
69-
taskQueue:taskQueue]);
67+
return !FLTThrowsObjcException(^{
68+
OCMVerify([messenger setMessageHandlerOnChannel:channel
69+
binaryMessageHandler:handler
70+
taskQueue:taskQueue]);
71+
});
7072
}
7173

72-
- (void)testMakeBackgroundTaskQueue {
74+
- (BOOL)testMakeBackgroundTaskQueue {
7375
id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
7476
FlutterBinaryMessengerRelay* relay =
7577
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
7678
[relay makeBackgroundTaskQueue];
77-
OCMVerify([messenger makeBackgroundTaskQueue]);
79+
return !FLTThrowsObjcException(^{
80+
OCMVerify([messenger makeBackgroundTaskQueue]);
81+
});
7882
}
7983

8084
@end
85+
86+
TEST(FlutterBinaryMessengerRelayTest, Create) {
87+
ASSERT_TRUE([[FlutterBinaryMessengerRelayTest alloc] testCreate]);
88+
}
89+
90+
TEST(FlutterBinaryMessengerRelayTest, PassesCallOn) {
91+
ASSERT_TRUE([[FlutterBinaryMessengerRelayTest alloc] testPassesCallOn]);
92+
}
93+
94+
TEST(FlutterBinaryMessengerRelayTest, DoesntPassCallOn) {
95+
ASSERT_TRUE([[FlutterBinaryMessengerRelayTest alloc] testDoesntPassCallOn]);
96+
}
97+
98+
TEST(FlutterBinaryMessengerRelayTest, SetMessageHandlerWithTaskQueue) {
99+
ASSERT_TRUE([[FlutterBinaryMessengerRelayTest alloc] testSetMessageHandlerWithTaskQueue]);
100+
}
101+
102+
TEST(FlutterBinaryMessengerRelayTest, SetMakeBackgroundTaskQueue) {
103+
ASSERT_TRUE([[FlutterBinaryMessengerRelayTest alloc] testMakeBackgroundTaskQueue]);
104+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef SHELL_PLATFORM_DARWIN_COMMON_FRAMEWORK_SOURCE_FLUTTERTESTUTILS_H_
6+
#define SHELL_PLATFORM_DARWIN_COMMON_FRAMEWORK_SOURCE_FLUTTERTESTUTILS_H_
7+
8+
#import <Foundation/Foundation.h>
9+
10+
/// Returns YES if the block throws an exception.
11+
BOOL FLTThrowsObjcException(dispatch_block_t block);
12+
13+
#endif // SHELL_PLATFORM_DARWIN_COMMON_FRAMEWORK_SOURCE_FLUTTERTESTUTILS_H_
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import "flutter/shell/platform/darwin/common/framework/Source/FlutterTestUtils.h"
6+
7+
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
8+
9+
FLUTTER_ASSERT_ARC
10+
11+
BOOL FLTThrowsObjcException(dispatch_block_t block) {
12+
@try {
13+
block();
14+
} @catch (...) {
15+
return YES;
16+
}
17+
return NO;
18+
}

shell/platform/darwin/ios/BUILD.gn

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ source_set("flutter_framework_source") {
7575

7676
sources = [
7777
"framework/Source/FlutterAppDelegate.mm",
78-
"framework/Source/FlutterBinaryMessengerRelay.mm",
7978
"framework/Source/FlutterCallbackCache.mm",
8079
"framework/Source/FlutterCallbackCache_Internal.h",
8180
"framework/Source/FlutterChannelKeyResponder.h",
@@ -283,7 +282,6 @@ shared_library("ios_test_flutter") {
283282
]
284283
sources = [
285284
"framework/Source/FlutterAppDelegateTest.mm",
286-
"framework/Source/FlutterBinaryMessengerRelayTest.mm",
287285
"framework/Source/FlutterChannelKeyResponderTest.mm",
288286
"framework/Source/FlutterDartProjectTest.mm",
289287
"framework/Source/FlutterEmbedderKeyResponderTest.mm",

shell/platform/darwin/ios/framework/Source/FlutterEngine.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "flutter/shell/common/thread_host.h"
2020
#include "flutter/shell/common/variable_refresh_rate_display.h"
2121
#import "flutter/shell/platform/darwin/common/command_line.h"
22-
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h"
22+
#import "flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.h"
2323
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
2424
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.h"
2525
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterIndirectScribbleDelegate.h"

shell/platform/darwin/ios/framework/Source/FlutterViewController.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
1616
#include "flutter/runtime/ptrace_check.h"
1717
#include "flutter/shell/common/thread_host.h"
18-
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h"
18+
#import "flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.h"
1919
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterChannelKeyResponder.h"
2020
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEmbedderKeyResponder.h"
2121
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h"

0 commit comments

Comments
 (0)