Skip to content

Commit 9d63517

Browse files
gaaclarkeditman
andauthored
upgraded usage of BinaryMessenger (flutter#4451)
Co-authored-by: David Iglesias Teixeira <[email protected]>
1 parent 94b836e commit 9d63517

File tree

8 files changed

+443
-269
lines changed

8 files changed

+443
-269
lines changed

.cirrus.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ task:
197197
- export CIRRUS_CHANGE_MESSAGE=""
198198
- export CIRRUS_COMMIT_MESSAGE=""
199199
- ./script/tool_runner.sh lint-android # must come after build-examples
200+
stable_channel_conditional_script:
201+
- if [[ "$CHANNEL" == "stable" ]]; then
202+
- dart ./ci/stable_conditional.dart
203+
- fi
200204
native_unit_test_script:
201205
# Unsetting CIRRUS_CHANGE_MESSAGE and CIRRUS_COMMIT_MESSAGE as they
202206
# might include non-ASCII characters which makes Gradle crash.

ci/stable_conditional.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
// stable_conditional.dart
6+
//
7+
// Performs simple find and replace operations for conditional compilation
8+
// before executing stable channel tests.
9+
//
10+
// Example input:
11+
// int main() {
12+
// // FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
13+
// printf("hello world\n");
14+
// // FLUTTER_STABLE_CONDITIONAL_ELSE
15+
// // printf("goodbye world\n");
16+
// // FLUTTER_STABLE_CONDITIONAL_ENDIF
17+
// }
18+
//
19+
// Example output:
20+
// int main() {
21+
// printf("goodbye world\n");
22+
// }
23+
24+
import 'dart:convert' show LineSplitter;
25+
import 'dart:io' show FileSystemEntity, File;
26+
27+
final List<String> _filesToProcess = <String>[
28+
'packages/android_intent/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java',
29+
'packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/DartMessengerTest.java',
30+
'packages/quick_actions/quick_actions/android/src/test/java/io/flutter/plugins/quickactions/QuickActionsTest.java',
31+
'packages/url_launcher/url_launcher/android/src/test/java/io/flutter/plugins/urllauncher/MethodCallHandlerImplTest.java',
32+
];
33+
34+
final RegExp _replacer = RegExp(
35+
r'^\s*// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE(.*?)^\s*// FLUTTER_STABLE_CONDITIONAL_ELSE(.*?)^\s*// FLUTTER_STABLE_CONDITIONAL_ENDIF',
36+
multiLine: true,
37+
dotAll: true);
38+
final RegExp _commentRemover = RegExp(r'^(\s*)\/\/\s*(.*)');
39+
const String _newline = '\n';
40+
41+
void _process(FileSystemEntity entity) {
42+
const LineSplitter splitter = LineSplitter();
43+
final String text = File(entity.path).readAsStringSync();
44+
String replaced = '';
45+
int index = 0;
46+
for (final RegExpMatch match in _replacer.allMatches(text)) {
47+
replaced += text.substring(index, match.start);
48+
for (final String line in splitter.convert(match.group(2)!)) {
49+
final RegExpMatch? commentRemoverMatch = _commentRemover.firstMatch(line);
50+
if (commentRemoverMatch != null) {
51+
replaced += commentRemoverMatch.group(1)! +
52+
commentRemoverMatch.group(2)! +
53+
_newline;
54+
}
55+
}
56+
index = match.end;
57+
}
58+
if (replaced.isNotEmpty) {
59+
replaced += text.substring(index, text.length);
60+
File(entity.path).writeAsStringSync(replaced);
61+
print('modified: ${entity.path}');
62+
}
63+
}
64+
65+
void main(List<String> args) {
66+
_filesToProcess.map((String path) => File(path)).forEach(_process);
67+
}

packages/android_intent/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public void startListening_registersChannel() {
5555
methodCallHandler.startListening(messenger);
5656

5757
verify(messenger, times(1))
58-
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
58+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
59+
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class), eq(null));
60+
// FLUTTER_STABLE_CONDITIONAL_ELSE
61+
// .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
62+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
5963
}
6064

6165
@Test
@@ -67,9 +71,15 @@ public void startListening_unregistersExistingChannel() {
6771
methodCallHandler.startListening(secondMessenger);
6872

6973
// Unregisters the first and then registers the second.
70-
verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
74+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
75+
verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null, null);
7176
verify(secondMessenger, times(1))
72-
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
77+
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class), eq(null));
78+
// FLUTTER_STABLE_CONDITIONAL_ELSE
79+
// verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
80+
// verify(secondMessenger, times(1))
81+
// .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
82+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
7383
}
7484

7585
@Test
@@ -79,7 +89,11 @@ public void stopListening_unregistersExistingChannel() {
7989

8090
methodCallHandler.stopListening();
8191

82-
verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
92+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
93+
verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null, null);
94+
// FLUTTER_STABLE_CONDITIONAL_ELSE
95+
// verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
96+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
8397
}
8498

8599
@Test
@@ -88,7 +102,11 @@ public void stopListening_doesNothingWhenUnset() {
88102

89103
methodCallHandler.stopListening();
90104

91-
verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null);
105+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
106+
verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null, null);
107+
// FLUTTER_STABLE_CONDITIONAL_ELSE
108+
// verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null);
109+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
92110
}
93111

94112
@Test

packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/DartMessengerTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import android.os.Handler;
1414
import androidx.annotation.NonNull;
15+
import androidx.annotation.Nullable;
1516
import io.flutter.embedding.engine.systemchannels.PlatformChannel;
1617
import io.flutter.plugin.common.BinaryMessenger;
1718
import io.flutter.plugin.common.MethodCall;
@@ -31,6 +32,15 @@ public class DartMessengerTest {
3132
private static class FakeBinaryMessenger implements BinaryMessenger {
3233
private final List<ByteBuffer> sentMessages = new ArrayList<>();
3334

35+
// TODO(aaclarke): Remove when https://github.com/flutter/engine/pull/29147 is on stable.
36+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
37+
@Override
38+
public BinaryMessenger.TaskQueue makeBackgroundTaskQueue() {
39+
return null;
40+
}
41+
// FLUTTER_STABLE_CONDITIONAL_ELSE
42+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
43+
3444
@Override
3545
public void send(@NonNull String channel, ByteBuffer message) {
3646
sentMessages.add(message);
@@ -41,8 +51,17 @@ public void send(@NonNull String channel, ByteBuffer message, BinaryReply callba
4151
send(channel, message);
4252
}
4353

54+
// TODO(aaclarke): Remove when https://github.com/flutter/engine/pull/29147 is on stable.
55+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
4456
@Override
45-
public void setMessageHandler(@NonNull String channel, BinaryMessageHandler handler) {}
57+
public void setMessageHandler(
58+
@NonNull String channel,
59+
BinaryMessageHandler handler,
60+
@Nullable BinaryMessenger.TaskQueue taskQueue) {}
61+
// FLUTTER_STABLE_CONDITIONAL_ELSE
62+
// @Override
63+
// public void setMessageHandler(@NonNull String channel, BinaryMessageHandler handler) {}
64+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
4665

4766
List<ByteBuffer> getMessages() {
4867
return new ArrayList<>(sentMessages);

packages/quick_actions/quick_actions/android/src/test/java/io/flutter/plugins/quickactions/QuickActionsTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ public class QuickActionsTest {
3333
private static class TestBinaryMessenger implements BinaryMessenger {
3434
public MethodCall lastMethodCall;
3535

36+
// TODO(aaclarke): Remove when https://github.com/flutter/engine/pull/29147 is on stable.
37+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
38+
@Override
39+
public BinaryMessenger.TaskQueue makeBackgroundTaskQueue() {
40+
return null;
41+
}
42+
// FLUTTER_STABLE_CONDITIONAL_ELSE
43+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
44+
3645
@Override
3746
public void send(@NonNull String channel, @Nullable ByteBuffer message) {
3847
send(channel, message, null);
@@ -49,10 +58,21 @@ public void send(
4958
}
5059
}
5160

61+
// TODO(aaclarke): Remove when https://github.com/flutter/engine/pull/29147 is on stable.
62+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
5263
@Override
53-
public void setMessageHandler(@NonNull String channel, @Nullable BinaryMessageHandler handler) {
64+
public void setMessageHandler(
65+
@NonNull String channel,
66+
@Nullable BinaryMessageHandler handler,
67+
@Nullable BinaryMessenger.TaskQueue taskQueue) {
5468
// Do nothing.
5569
}
70+
// FLUTTER_STABLE_CONDITIONAL_ELSE
71+
// @Override
72+
// public void setMessageHandler(
73+
// @NonNull String channel,
74+
// @Nullable BinaryMessageHandler handler) {}
75+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
5676
}
5777

5878
static final int SUPPORTED_BUILD = 25;

packages/url_launcher/url_launcher/android/src/test/java/io/flutter/plugins/urllauncher/MethodCallHandlerImplTest.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ public void startListening_registersChannel() {
4444
methodCallHandler.startListening(messenger);
4545

4646
verify(messenger, times(1))
47-
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
47+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
48+
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class), eq(null));
49+
// FLUTTER_STABLE_CONDITIONAL_ELSE
50+
// .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
51+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
4852
}
4953

5054
@Test
@@ -56,9 +60,15 @@ public void startListening_unregistersExistingChannel() {
5660
methodCallHandler.startListening(secondMessenger);
5761

5862
// Unregisters the first and then registers the second.
59-
verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
63+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
64+
verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null, null);
6065
verify(secondMessenger, times(1))
61-
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
66+
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class), eq(null));
67+
// FLUTTER_STABLE_CONDITIONAL_ELSE
68+
// verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
69+
// verify(secondMessenger, times(1))
70+
// .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
71+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
6272
}
6373

6474
@Test
@@ -68,7 +78,11 @@ public void stopListening_unregistersExistingChannel() {
6878

6979
methodCallHandler.stopListening();
7080

71-
verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
81+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
82+
verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null, null);
83+
// FLUTTER_STABLE_CONDITIONAL_ELSE
84+
// verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
85+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
7286
}
7387

7488
@Test
@@ -77,7 +91,11 @@ public void stopListening_doesNothingWhenUnset() {
7791

7892
methodCallHandler.stopListening();
7993

80-
verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null);
94+
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
95+
verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null, null);
96+
// FLUTTER_STABLE_CONDITIONAL_ELSE
97+
// verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null);
98+
// FLUTTER_STABLE_CONDITIONAL_ENDIF
8199
}
82100

83101
@Test

0 commit comments

Comments
 (0)