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

Commit cb2b300

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 6076c59 commit cb2b300

File tree

7 files changed

+62
-26
lines changed

7 files changed

+62
-26
lines changed

shell/platform/darwin/common/BUILD.gn

Lines changed: 3 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,7 @@ executable("framework_common_unittests") {
6970
testonly = true
7071

7172
sources = [
73+
"framework/Source/FlutterBinaryMessengerRelayTest.mm",
7274
"framework/Source/flutter_codecs_unittest.mm",
7375
"framework/Source/flutter_standard_codec_unittest.mm",
7476
]
@@ -82,6 +84,7 @@ executable("framework_common_unittests") {
8284
":framework_common_fixtures",
8385
"//flutter/testing",
8486
"//third_party/dart/runtime:libdart_jit",
87+
"//third_party/ocmock:ocmock",
8588
]
8689

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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

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

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,51 @@
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/testing/testing.h"
11+
#include "gtest/gtest.h"
1112

1213
FLUTTER_ASSERT_ARC
1314

1415
@protocol FlutterTaskQueue <NSObject>
1516
@end
1617

17-
@interface FlutterBinaryMessengerRelayTest : XCTestCase
18+
@interface FlutterBinaryMessengerRelayTest : NSObject
1819
@end
1920

2021
@implementation FlutterBinaryMessengerRelayTest
2122

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

36-
- (void)testPassesCallOn {
32+
- (BOOL)testPassesCallOn {
3733
id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
3834
FlutterBinaryMessengerRelay* relay =
3935
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
4036
char messageData[] = {'a', 'a', 'r', 'o', 'n'};
4137
NSData* message = [NSData dataWithBytes:messageData length:sizeof(messageData)];
4238
NSString* channel = @"foobar";
4339
[relay sendOnChannel:channel message:message binaryReply:nil];
44-
OCMVerify([messenger sendOnChannel:channel message:message binaryReply:nil]);
40+
@try {
41+
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
42+
[messenger sendOnChannel:channel message:message binaryReply:nil]);
43+
} @catch (...) {
44+
return NO;
45+
}
46+
return YES;
4547
}
4648

47-
- (void)testDoesntPassCallOn {
49+
- (BOOL)testDoesntPassCallOn {
4850
id messenger = OCMStrictProtocolMock(@protocol(FlutterBinaryMessenger));
4951
FlutterBinaryMessengerRelay* relay =
5052
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
@@ -53,9 +55,10 @@ - (void)testDoesntPassCallOn {
5355
NSString* channel = @"foobar";
5456
relay.parent = nil;
5557
[relay sendOnChannel:channel message:message binaryReply:nil];
58+
return YES;
5659
}
5760

58-
- (void)testSetMessageHandlerWithTaskQueue {
61+
- (BOOL)testSetMessageHandlerWithTaskQueue {
5962
id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
6063
FlutterBinaryMessengerRelay* relay =
6164
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
@@ -64,17 +67,49 @@ - (void)testSetMessageHandlerWithTaskQueue {
6467
FlutterBinaryMessageHandler handler = ^(NSData* _Nullable, FlutterBinaryReply _Nonnull) {
6568
};
6669
[relay setMessageHandlerOnChannel:channel binaryMessageHandler:handler taskQueue:taskQueue];
67-
OCMVerify([messenger setMessageHandlerOnChannel:channel
68-
binaryMessageHandler:handler
69-
taskQueue:taskQueue]);
70+
@try {
71+
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
72+
[messenger setMessageHandlerOnChannel:channel
73+
binaryMessageHandler:handler
74+
taskQueue:taskQueue]);
75+
} @catch (...) {
76+
return NO;
77+
}
78+
return YES;
7079
}
7180

72-
- (void)testMakeBackgroundTaskQueue {
81+
- (BOOL)testMakeBackgroundTaskQueue {
7382
id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
7483
FlutterBinaryMessengerRelay* relay =
7584
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
7685
[relay makeBackgroundTaskQueue];
77-
OCMVerify([messenger makeBackgroundTaskQueue]);
86+
@try {
87+
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
88+
[messenger makeBackgroundTaskQueue]);
89+
} @catch (...) {
90+
return NO;
91+
}
92+
return YES;
7893
}
7994

8095
@end
96+
97+
TEST(FlutterBinaryMessengerRelayTest, Create) {
98+
ASSERT_TRUE([[FlutterBinaryMessengerRelayTest alloc] testCreate]);
99+
}
100+
101+
TEST(FlutterBinaryMessengerRelayTest, PassesCallOn) {
102+
ASSERT_TRUE([[FlutterBinaryMessengerRelayTest alloc] testPassesCallOn]);
103+
}
104+
105+
TEST(FlutterBinaryMessengerRelayTest, DoesntPassCallOn) {
106+
ASSERT_TRUE([[FlutterBinaryMessengerRelayTest alloc] testDoesntPassCallOn]);
107+
}
108+
109+
TEST(FlutterBinaryMessengerRelayTest, SetMessageHandlerWithTaskQueue) {
110+
ASSERT_TRUE([[FlutterBinaryMessengerRelayTest alloc] testSetMessageHandlerWithTaskQueue]);
111+
}
112+
113+
TEST(FlutterBinaryMessengerRelayTest, SetMakeBackgroundTaskQueue) {
114+
ASSERT_TRUE([[FlutterBinaryMessengerRelayTest alloc] testMakeBackgroundTaskQueue]);
115+
}

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)