Skip to content

Commit

Permalink
upgraded usage of BinaryMessenger (flutter#4451)
Browse files Browse the repository at this point in the history
Co-authored-by: David Iglesias Teixeira <[email protected]>
  • Loading branch information
gaaclarke and ditman authored Oct 26, 2021
1 parent 94b836e commit 9d63517
Show file tree
Hide file tree
Showing 8 changed files with 443 additions and 269 deletions.
4 changes: 4 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ task:
- export CIRRUS_CHANGE_MESSAGE=""
- export CIRRUS_COMMIT_MESSAGE=""
- ./script/tool_runner.sh lint-android # must come after build-examples
stable_channel_conditional_script:
- if [[ "$CHANNEL" == "stable" ]]; then
- dart ./ci/stable_conditional.dart
- fi
native_unit_test_script:
# Unsetting CIRRUS_CHANGE_MESSAGE and CIRRUS_COMMIT_MESSAGE as they
# might include non-ASCII characters which makes Gradle crash.
Expand Down
67 changes: 67 additions & 0 deletions ci/stable_conditional.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.
//
// stable_conditional.dart
//
// Performs simple find and replace operations for conditional compilation
// before executing stable channel tests.
//
// Example input:
// int main() {
// // FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
// printf("hello world\n");
// // FLUTTER_STABLE_CONDITIONAL_ELSE
// // printf("goodbye world\n");
// // FLUTTER_STABLE_CONDITIONAL_ENDIF
// }
//
// Example output:
// int main() {
// printf("goodbye world\n");
// }

import 'dart:convert' show LineSplitter;
import 'dart:io' show FileSystemEntity, File;

final List<String> _filesToProcess = <String>[
'packages/android_intent/android/src/test/java/io/flutter/plugins/androidintent/MethodCallHandlerImplTest.java',
'packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/DartMessengerTest.java',
'packages/quick_actions/quick_actions/android/src/test/java/io/flutter/plugins/quickactions/QuickActionsTest.java',
'packages/url_launcher/url_launcher/android/src/test/java/io/flutter/plugins/urllauncher/MethodCallHandlerImplTest.java',
];

final RegExp _replacer = RegExp(
r'^\s*// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE(.*?)^\s*// FLUTTER_STABLE_CONDITIONAL_ELSE(.*?)^\s*// FLUTTER_STABLE_CONDITIONAL_ENDIF',
multiLine: true,
dotAll: true);
final RegExp _commentRemover = RegExp(r'^(\s*)\/\/\s*(.*)');
const String _newline = '\n';

void _process(FileSystemEntity entity) {
const LineSplitter splitter = LineSplitter();
final String text = File(entity.path).readAsStringSync();
String replaced = '';
int index = 0;
for (final RegExpMatch match in _replacer.allMatches(text)) {
replaced += text.substring(index, match.start);
for (final String line in splitter.convert(match.group(2)!)) {
final RegExpMatch? commentRemoverMatch = _commentRemover.firstMatch(line);
if (commentRemoverMatch != null) {
replaced += commentRemoverMatch.group(1)! +
commentRemoverMatch.group(2)! +
_newline;
}
}
index = match.end;
}
if (replaced.isNotEmpty) {
replaced += text.substring(index, text.length);
File(entity.path).writeAsStringSync(replaced);
print('modified: ${entity.path}');
}
}

void main(List<String> args) {
_filesToProcess.map((String path) => File(path)).forEach(_process);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public void startListening_registersChannel() {
methodCallHandler.startListening(messenger);

verify(messenger, times(1))
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class), eq(null));
// FLUTTER_STABLE_CONDITIONAL_ELSE
// .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
// FLUTTER_STABLE_CONDITIONAL_ENDIF
}

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

// Unregisters the first and then registers the second.
verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null, null);
verify(secondMessenger, times(1))
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class), eq(null));
// FLUTTER_STABLE_CONDITIONAL_ELSE
// verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
// verify(secondMessenger, times(1))
// .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
// FLUTTER_STABLE_CONDITIONAL_ENDIF
}

@Test
Expand All @@ -79,7 +89,11 @@ public void stopListening_unregistersExistingChannel() {

methodCallHandler.stopListening();

verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null, null);
// FLUTTER_STABLE_CONDITIONAL_ELSE
// verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
// FLUTTER_STABLE_CONDITIONAL_ENDIF
}

@Test
Expand All @@ -88,7 +102,11 @@ public void stopListening_doesNothingWhenUnset() {

methodCallHandler.stopListening();

verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null);
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null, null);
// FLUTTER_STABLE_CONDITIONAL_ELSE
// verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null);
// FLUTTER_STABLE_CONDITIONAL_ENDIF
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

// TODO(aaclarke): Remove when https://github.com/flutter/engine/pull/29147 is on stable.
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
@Override
public BinaryMessenger.TaskQueue makeBackgroundTaskQueue() {
return null;
}
// FLUTTER_STABLE_CONDITIONAL_ELSE
// FLUTTER_STABLE_CONDITIONAL_ENDIF

@Override
public void send(@NonNull String channel, ByteBuffer message) {
sentMessages.add(message);
Expand All @@ -41,8 +51,17 @@ public void send(@NonNull String channel, ByteBuffer message, BinaryReply callba
send(channel, message);
}

// TODO(aaclarke): Remove when https://github.com/flutter/engine/pull/29147 is on stable.
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
@Override
public void setMessageHandler(@NonNull String channel, BinaryMessageHandler handler) {}
public void setMessageHandler(
@NonNull String channel,
BinaryMessageHandler handler,
@Nullable BinaryMessenger.TaskQueue taskQueue) {}
// FLUTTER_STABLE_CONDITIONAL_ELSE
// @Override
// public void setMessageHandler(@NonNull String channel, BinaryMessageHandler handler) {}
// FLUTTER_STABLE_CONDITIONAL_ENDIF

List<ByteBuffer> getMessages() {
return new ArrayList<>(sentMessages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public class QuickActionsTest {
private static class TestBinaryMessenger implements BinaryMessenger {
public MethodCall lastMethodCall;

// TODO(aaclarke): Remove when https://github.com/flutter/engine/pull/29147 is on stable.
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
@Override
public BinaryMessenger.TaskQueue makeBackgroundTaskQueue() {
return null;
}
// FLUTTER_STABLE_CONDITIONAL_ELSE
// FLUTTER_STABLE_CONDITIONAL_ENDIF

@Override
public void send(@NonNull String channel, @Nullable ByteBuffer message) {
send(channel, message, null);
Expand All @@ -49,10 +58,21 @@ public void send(
}
}

// TODO(aaclarke): Remove when https://github.com/flutter/engine/pull/29147 is on stable.
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
@Override
public void setMessageHandler(@NonNull String channel, @Nullable BinaryMessageHandler handler) {
public void setMessageHandler(
@NonNull String channel,
@Nullable BinaryMessageHandler handler,
@Nullable BinaryMessenger.TaskQueue taskQueue) {
// Do nothing.
}
// FLUTTER_STABLE_CONDITIONAL_ELSE
// @Override
// public void setMessageHandler(
// @NonNull String channel,
// @Nullable BinaryMessageHandler handler) {}
// FLUTTER_STABLE_CONDITIONAL_ENDIF
}

static final int SUPPORTED_BUILD = 25;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public void startListening_registersChannel() {
methodCallHandler.startListening(messenger);

verify(messenger, times(1))
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class), eq(null));
// FLUTTER_STABLE_CONDITIONAL_ELSE
// .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
// FLUTTER_STABLE_CONDITIONAL_ENDIF
}

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

// Unregisters the first and then registers the second.
verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null, null);
verify(secondMessenger, times(1))
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class), eq(null));
// FLUTTER_STABLE_CONDITIONAL_ELSE
// verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
// verify(secondMessenger, times(1))
// .setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
// FLUTTER_STABLE_CONDITIONAL_ENDIF
}

@Test
Expand All @@ -68,7 +78,11 @@ public void stopListening_unregistersExistingChannel() {

methodCallHandler.stopListening();

verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null, null);
// FLUTTER_STABLE_CONDITIONAL_ELSE
// verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
// FLUTTER_STABLE_CONDITIONAL_ENDIF
}

@Test
Expand All @@ -77,7 +91,11 @@ public void stopListening_doesNothingWhenUnset() {

methodCallHandler.stopListening();

verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null);
// FLUTTER_STABLE_CONDITIONAL_IF_NOT_STABLE
verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null, null);
// FLUTTER_STABLE_CONDITIONAL_ELSE
// verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null);
// FLUTTER_STABLE_CONDITIONAL_ENDIF
}

@Test
Expand Down
Loading

0 comments on commit 9d63517

Please sign in to comment.