Skip to content

Commit

Permalink
[web] Combine duplicate platform message spy implementations (flutter…
Browse files Browse the repository at this point in the history
  • Loading branch information
mdebbar authored Apr 10, 2020
1 parent 68fd833 commit c69f21a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lib/web_ui/test/spy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PlatformMessage {
String get methodName => methodCall.method;

/// Shorthand for getting the arguments of the method call.
String get methodArguments => methodCall.arguments;
dynamic get methodArguments => methodCall.arguments;
}

/// Intercepts platform messages sent from the engine to the framework.
Expand Down
85 changes: 38 additions & 47 deletions lib/web_ui/test/text_editing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import 'dart:html';
import 'dart:js_util' as js_util;
import 'dart:typed_data';

import 'package:ui/ui.dart' as ui;
import 'package:ui/src/engine.dart' hide window;

import 'package:test/test.dart';

import 'matchers.dart';
import 'spy.dart';

/// The `keyCode` of the "Enter" key.
const int _kReturnKeyCode = 13;
Expand Down Expand Up @@ -597,11 +597,11 @@ void main() {

setUp(() {
textEditing = HybridTextEditing();
spy.activate();
spy.setUp();
});

tearDown(() {
spy.deactivate();
spy.tearDown();
if (textEditing.isEditing) {
textEditing.stopEditing();
}
Expand Down Expand Up @@ -699,15 +699,15 @@ void main() {
textEditing.editingElement.domElement.blur();

expect(spy.messages, hasLength(1));
MethodCall call = spy.messages[0];
spy.messages.clear();
expect(call.method, 'TextInputClient.onConnectionClosed');
expect(spy.messages[0].channel, 'flutter/textinput');
expect(spy.messages[0].methodName, 'TextInputClient.onConnectionClosed');
expect(
call.arguments,
spy.messages[0].methodArguments,
<dynamic>[
123, // Client ID
],
);
spy.messages.clear();
// Input element is removed from DOM.
expect(document.getElementsByTagName('input'), hasLength(0));
},
Expand Down Expand Up @@ -998,11 +998,10 @@ void main() {
input.dispatchEvent(Event.eventType('Event', 'input'));

expect(spy.messages, hasLength(1));
MethodCall call = spy.messages[0];
spy.messages.clear();
expect(call.method, 'TextInputClient.updateEditingState');
expect(spy.messages[0].channel, 'flutter/textinput');
expect(spy.messages[0].methodName, 'TextInputClient.updateEditingState');
expect(
call.arguments,
spy.messages[0].methodArguments,
<dynamic>[
123, // Client ID
<String, dynamic>{
Expand All @@ -1012,6 +1011,7 @@ void main() {
}
],
);
spy.messages.clear();

input.setSelectionRange(2, 5);
if (browserEngine == BrowserEngine.firefox) {
Expand All @@ -1022,11 +1022,10 @@ void main() {
}

expect(spy.messages, hasLength(1));
call = spy.messages[0];
spy.messages.clear();
expect(call.method, 'TextInputClient.updateEditingState');
expect(spy.messages[0].channel, 'flutter/textinput');
expect(spy.messages[0].methodName, 'TextInputClient.updateEditingState');
expect(
call.arguments,
spy.messages[0].methodArguments,
<dynamic>[
123, // Client ID
<String, dynamic>{
Expand All @@ -1036,6 +1035,7 @@ void main() {
}
],
);
spy.messages.clear();

hideKeyboard();
});
Expand Down Expand Up @@ -1078,20 +1078,35 @@ void main() {
// Two messages should've been sent. One for the 'input' event and one for
// the 'selectionchange' event.
expect(spy.messages, hasLength(2));
final MethodCall call = spy.messages.last;
spy.messages.clear();
expect(call.method, 'TextInputClient.updateEditingState');

expect(spy.messages[0].channel, 'flutter/textinput');
expect(spy.messages[0].methodName, 'TextInputClient.updateEditingState');
expect(
spy.messages[0].methodArguments,
<dynamic>[
123, // Client ID
<String, dynamic>{
'text': 'something\nelse',
'selectionBase': 14,
'selectionExtent': 14,
}
],
);

expect(spy.messages[1].channel, 'flutter/textinput');
expect(spy.messages[1].methodName, 'TextInputClient.updateEditingState');
expect(
call.arguments,
spy.messages[1].methodArguments,
<dynamic>[
123, // Client ID
<String, dynamic>{
'text': 'something\nelse',
'selectionBase': 2,
'selectionExtent': 5
'selectionExtent': 5,
}
],
);
spy.messages.clear();

const MethodCall hide = MethodCall('TextInput.hide');
sendFrameworkMessage(codec.encodeMethodCall(hide));
Expand Down Expand Up @@ -1171,10 +1186,10 @@ void main() {
);

expect(spy.messages, hasLength(1));
final MethodCall call = spy.messages.first;
expect(call.method, 'TextInputClient.performAction');
expect(spy.messages[0].channel, 'flutter/textinput');
expect(spy.messages[0].methodName, 'TextInputClient.performAction');
expect(
call.arguments,
spy.messages[0].methodArguments,
<dynamic>[clientId, 'TextInputAction.next'],
);
},
Expand Down Expand Up @@ -1384,30 +1399,6 @@ void checkTextAreaEditingState(
expect(textarea.selectionEnd, end);
}

class PlatformMessagesSpy {
bool _isActive = false;
ui.PlatformMessageCallback _backup;

final List<MethodCall> messages = <MethodCall>[];

void activate() {
assert(!_isActive);
_isActive = true;
_backup = ui.window.onPlatformMessage;
ui.window.onPlatformMessage = (String channel, ByteData data,
ui.PlatformMessageResponseCallback callback) {
messages.add(codec.decodeMethodCall(data));
};
}

void deactivate() {
assert(_isActive);
_isActive = false;
messages.clear();
ui.window.onPlatformMessage = _backup;
}
}

Map<String, dynamic> createFlutterConfig(
String inputType, {
bool obscureText = false,
Expand Down

0 comments on commit c69f21a

Please sign in to comment.