|
4 | 4 |
|
5 | 5 | import 'dart:convert' show jsonDecode;
|
6 | 6 |
|
| 7 | +import 'package:flutter/foundation.dart'; |
7 | 8 | import 'package:flutter/services.dart';
|
8 | 9 | import 'package:flutter_test/flutter_test.dart';
|
9 | 10 |
|
@@ -65,6 +66,162 @@ void main() {
|
65 | 66 | expect(client.latestMethodCall, 'updateEditingValueWithDeltas');
|
66 | 67 | },
|
67 | 68 | );
|
| 69 | + |
| 70 | + test('Invalid TextRange fails loudly when being converted to JSON - NonTextUpdate', () async { |
| 71 | + final List<FlutterErrorDetails> record = <FlutterErrorDetails>[]; |
| 72 | + FlutterError.onError = (FlutterErrorDetails details) { |
| 73 | + record.add(details); |
| 74 | + }; |
| 75 | + |
| 76 | + final FakeDeltaTextInputClient client = FakeDeltaTextInputClient(const TextEditingValue(text: '1')); |
| 77 | + const TextInputConfiguration configuration = TextInputConfiguration(enableDeltaModel: true); |
| 78 | + TextInput.attach(client, configuration); |
| 79 | + |
| 80 | + const String jsonDelta = '{' |
| 81 | + '"oldText": "1",' |
| 82 | + ' "deltaText": "",' |
| 83 | + ' "deltaStart": -1,' |
| 84 | + ' "deltaEnd": -1,' |
| 85 | + ' "selectionBase": 3,' |
| 86 | + ' "selectionExtent": 3,' |
| 87 | + ' "selectionAffinity" : "TextAffinity.downstream" ,' |
| 88 | + ' "selectionIsDirectional": false,' |
| 89 | + ' "composingBase": -1,' |
| 90 | + ' "composingExtent": -1}'; |
| 91 | + |
| 92 | + final ByteData? messageBytes = const JSONMessageCodec().encodeMessage(<String, dynamic>{ |
| 93 | + 'method': 'TextInputClient.updateEditingStateWithDeltas', |
| 94 | + 'args': <dynamic>[-1, jsonDecode('{"deltas": [$jsonDelta]}')], |
| 95 | + }); |
| 96 | + |
| 97 | + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( |
| 98 | + 'flutter/textinput', |
| 99 | + messageBytes, |
| 100 | + (ByteData? _) {}, |
| 101 | + ); |
| 102 | + expect(record.length, 1); |
| 103 | + // Verify the error message in parts because Web formats the message |
| 104 | + // differently from others. |
| 105 | + expect(record[0].exception.toString(), matches(RegExp(r'\bThe selection range: TextSelection.collapsed\(offset: 3, affinity: TextAffinity.downstream, isDirectional: false\)(?!\w)'))); |
| 106 | + expect(record[0].exception.toString(), matches(RegExp(r'\bis not within the bounds of text: 1 of length: 1\b'))); |
| 107 | + }); |
| 108 | + |
| 109 | + test('Invalid TextRange fails loudly when being converted to JSON - Faulty deltaStart and deltaEnd', () async { |
| 110 | + final List<FlutterErrorDetails> record = <FlutterErrorDetails>[]; |
| 111 | + FlutterError.onError = (FlutterErrorDetails details) { |
| 112 | + record.add(details); |
| 113 | + }; |
| 114 | + |
| 115 | + final FakeDeltaTextInputClient client = FakeDeltaTextInputClient(TextEditingValue.empty); |
| 116 | + const TextInputConfiguration configuration = TextInputConfiguration(enableDeltaModel: true); |
| 117 | + TextInput.attach(client, configuration); |
| 118 | + |
| 119 | + const String jsonDelta = '{' |
| 120 | + '"oldText": "",' |
| 121 | + ' "deltaText": "hello",' |
| 122 | + ' "deltaStart": 0,' |
| 123 | + ' "deltaEnd": 1,' |
| 124 | + ' "selectionBase": 5,' |
| 125 | + ' "selectionExtent": 5,' |
| 126 | + ' "selectionAffinity" : "TextAffinity.downstream" ,' |
| 127 | + ' "selectionIsDirectional": false,' |
| 128 | + ' "composingBase": -1,' |
| 129 | + ' "composingExtent": -1}'; |
| 130 | + |
| 131 | + final ByteData? messageBytes = const JSONMessageCodec().encodeMessage(<String, dynamic>{ |
| 132 | + 'method': 'TextInputClient.updateEditingStateWithDeltas', |
| 133 | + 'args': <dynamic>[-1, jsonDecode('{"deltas": [$jsonDelta]}')], |
| 134 | + }); |
| 135 | + |
| 136 | + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( |
| 137 | + 'flutter/textinput', |
| 138 | + messageBytes, |
| 139 | + (ByteData? _) {}, |
| 140 | + ); |
| 141 | + expect(record.length, 1); |
| 142 | + // Verify the error message in parts because Web formats the message |
| 143 | + // differently from others. |
| 144 | + expect(record[0].exception.toString(), matches(RegExp(r'\bThe delta range: TextRange\(start: 0, end: 5\)(?!\w)'))); |
| 145 | + expect(record[0].exception.toString(), matches(RegExp(r'\bis not within the bounds of text: of length: 0\b'))); |
| 146 | + }); |
| 147 | + |
| 148 | + test('Invalid TextRange fails loudly when being converted to JSON - Faulty Selection', () async { |
| 149 | + final List<FlutterErrorDetails> record = <FlutterErrorDetails>[]; |
| 150 | + FlutterError.onError = (FlutterErrorDetails details) { |
| 151 | + record.add(details); |
| 152 | + }; |
| 153 | + |
| 154 | + final FakeDeltaTextInputClient client = FakeDeltaTextInputClient(TextEditingValue.empty); |
| 155 | + const TextInputConfiguration configuration = TextInputConfiguration(enableDeltaModel: true); |
| 156 | + TextInput.attach(client, configuration); |
| 157 | + |
| 158 | + const String jsonDelta = '{' |
| 159 | + '"oldText": "",' |
| 160 | + ' "deltaText": "hello",' |
| 161 | + ' "deltaStart": 0,' |
| 162 | + ' "deltaEnd": 0,' |
| 163 | + ' "selectionBase": 6,' |
| 164 | + ' "selectionExtent": 6,' |
| 165 | + ' "selectionAffinity" : "TextAffinity.downstream" ,' |
| 166 | + ' "selectionIsDirectional": false,' |
| 167 | + ' "composingBase": -1,' |
| 168 | + ' "composingExtent": -1}'; |
| 169 | + |
| 170 | + final ByteData? messageBytes = const JSONMessageCodec().encodeMessage(<String, dynamic>{ |
| 171 | + 'method': 'TextInputClient.updateEditingStateWithDeltas', |
| 172 | + 'args': <dynamic>[-1, jsonDecode('{"deltas": [$jsonDelta]}')], |
| 173 | + }); |
| 174 | + |
| 175 | + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( |
| 176 | + 'flutter/textinput', |
| 177 | + messageBytes, |
| 178 | + (ByteData? _) {}, |
| 179 | + ); |
| 180 | + expect(record.length, 1); |
| 181 | + // Verify the error message in parts because Web formats the message |
| 182 | + // differently from others. |
| 183 | + expect(record[0].exception.toString(), matches(RegExp(r'\bThe selection range: TextSelection.collapsed\(offset: 6, affinity: TextAffinity.downstream, isDirectional: false\)(?!\w)'))); |
| 184 | + expect(record[0].exception.toString(), matches(RegExp(r'\bis not within the bounds of text: hello of length: 5\b'))); |
| 185 | + }); |
| 186 | + |
| 187 | + test('Invalid TextRange fails loudly when being converted to JSON - Faulty Composing Region', () async { |
| 188 | + final List<FlutterErrorDetails> record = <FlutterErrorDetails>[]; |
| 189 | + FlutterError.onError = (FlutterErrorDetails details) { |
| 190 | + record.add(details); |
| 191 | + }; |
| 192 | + |
| 193 | + final FakeDeltaTextInputClient client = FakeDeltaTextInputClient(const TextEditingValue(text: 'worl')); |
| 194 | + const TextInputConfiguration configuration = TextInputConfiguration(enableDeltaModel: true); |
| 195 | + TextInput.attach(client, configuration); |
| 196 | + |
| 197 | + const String jsonDelta = '{' |
| 198 | + '"oldText": "worl",' |
| 199 | + ' "deltaText": "world",' |
| 200 | + ' "deltaStart": 0,' |
| 201 | + ' "deltaEnd": 4,' |
| 202 | + ' "selectionBase": 5,' |
| 203 | + ' "selectionExtent": 5,' |
| 204 | + ' "selectionAffinity" : "TextAffinity.downstream" ,' |
| 205 | + ' "selectionIsDirectional": false,' |
| 206 | + ' "composingBase": 0,' |
| 207 | + ' "composingExtent": 6}'; |
| 208 | + |
| 209 | + final ByteData? messageBytes = const JSONMessageCodec().encodeMessage(<String, dynamic>{ |
| 210 | + 'method': 'TextInputClient.updateEditingStateWithDeltas', |
| 211 | + 'args': <dynamic>[-1, jsonDecode('{"deltas": [$jsonDelta]}')], |
| 212 | + }); |
| 213 | + |
| 214 | + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( |
| 215 | + 'flutter/textinput', |
| 216 | + messageBytes, |
| 217 | + (ByteData? _) {}, |
| 218 | + ); |
| 219 | + expect(record.length, 1); |
| 220 | + // Verify the error message in parts because Web formats the message |
| 221 | + // differently from others. |
| 222 | + expect(record[0].exception.toString(), matches(RegExp(r'\bThe composing range: TextRange\(start: 0, end: 6\)(?!\w)'))); |
| 223 | + expect(record[0].exception.toString(), matches(RegExp(r'\bis not within the bounds of text: world of length: 5\b'))); |
| 224 | + }); |
68 | 225 | });
|
69 | 226 | }
|
70 | 227 |
|
|
0 commit comments