forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchannel_buffers_test.dart
373 lines (348 loc) · 12.6 KB
/
channel_buffers_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// 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.
// KEEP THIS SYNCHRONIZED WITH ../../lib/web_ui/test/channel_buffers_test.dart
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:litetest/litetest.dart';
ByteData _makeByteData(String str) {
final Uint8List list = utf8.encode(str) as Uint8List;
final ByteBuffer buffer = list.buffer;
return ByteData.view(buffer);
}
void _resize(ui.ChannelBuffers buffers, String name, int newSize) {
buffers.handleMessage(_makeByteData('resize\r$name\r$newSize'));
}
void main() {
test('push drain', () async {
const String channel = 'foo';
final ByteData data = _makeByteData('bar');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
bool called = false;
void callback(ByteData? responseData) {
called = true;
}
buffers.push(channel, data, callback);
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
expect(drainedData, equals(data));
assert(!called);
drainedCallback(drainedData);
assert(called);
});
});
test('drain is sync', () async {
const String channel = 'foo';
final ByteData data = _makeByteData('message');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
void callback(ByteData? responseData) {}
buffers.push(channel, data, callback);
final List<String> log = <String>[];
final Completer<void> completer = Completer<void>();
scheduleMicrotask(() { log.add('before drain, microtask'); });
log.add('before drain');
// Ignoring the returned future because the completion of the drain is
// communicated using the `completer`.
buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
log.add('callback');
completer.complete();
});
log.add('after drain, before await');
await completer.future;
log.add('after await');
expect(log, <String>[
'before drain',
'callback',
'after drain, before await',
'before drain, microtask',
'after await'
]);
});
test('push drain zero', () async {
const String channel = 'foo';
final ByteData data = _makeByteData('bar');
final
ui.ChannelBuffers buffers = ui.ChannelBuffers();
void callback(ByteData? responseData) {}
_resize(buffers, channel, 0);
buffers.push(channel, data, callback);
bool didCall = false;
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
didCall = true;
});
expect(didCall, equals(false));
});
test('drain when empty', () async {
const String channel = 'foo';
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
bool didCall = false;
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
didCall = true;
});
expect(didCall, equals(false));
});
test('overflow', () async {
const String channel = 'foo';
final ByteData one = _makeByteData('one');
final ByteData two = _makeByteData('two');
final ByteData three = _makeByteData('three');
final ByteData four = _makeByteData('four');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
void callback(ByteData? responseData) {}
_resize(buffers, channel, 3);
buffers.push(channel, one, callback);
buffers.push(channel, two, callback);
buffers.push(channel, three, callback);
buffers.push(channel, four, callback);
int counter = 0;
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
switch (counter) {
case 0:
expect(drainedData, equals(two));
case 1:
expect(drainedData, equals(three));
case 2:
expect(drainedData, equals(four));
}
counter += 1;
});
expect(counter, equals(3));
});
test('resize drop', () async {
const String channel = 'foo';
final ByteData one = _makeByteData('one');
final ByteData two = _makeByteData('two');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
_resize(buffers, channel, 100);
void callback(ByteData? responseData) {}
buffers.push(channel, one, callback);
buffers.push(channel, two, callback);
_resize(buffers, channel, 1);
int counter = 0;
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
switch (counter) {
case 0:
expect(drainedData, equals(two));
}
counter += 1;
});
expect(counter, equals(1));
});
test('resize dropping calls callback', () async {
const String channel = 'foo';
final ByteData one = _makeByteData('one');
final ByteData two = _makeByteData('two');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
bool didCallCallback = false;
void oneCallback(ByteData? responseData) {
expect(responseData, isNull);
didCallCallback = true;
}
void twoCallback(ByteData? responseData) {
fail('wrong callback called');
}
_resize(buffers, channel, 100);
buffers.push(channel, one, oneCallback);
buffers.push(channel, two, twoCallback);
expect(didCallCallback, equals(false));
_resize(buffers, channel, 1);
expect(didCallCallback, equals(true));
});
test('overflow calls callback', () async {
const String channel = 'foo';
final ByteData one = _makeByteData('one');
final ByteData two = _makeByteData('two');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
bool didCallCallback = false;
void oneCallback(ByteData? responseData) {
expect(responseData, isNull);
didCallCallback = true;
}
void twoCallback(ByteData? responseData) {
fail('wrong callback called');
}
_resize(buffers, channel, 1);
buffers.push(channel, one, oneCallback);
buffers.push(channel, two, twoCallback);
expect(didCallCallback, equals(true));
});
test('handle garbage', () async {
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
expect(() => buffers.handleMessage(_makeByteData('asdfasdf')),
throwsException);
});
test('handle resize garbage', () async {
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
expect(() => buffers.handleMessage(_makeByteData('resize\rfoo\rbar')),
throwsException);
});
test('ChannelBuffers.setListener', () async {
final List<String> log = <String>[];
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
final ByteData one = _makeByteData('one');
final ByteData two = _makeByteData('two');
final ByteData three = _makeByteData('three');
final ByteData four = _makeByteData('four');
final ByteData five = _makeByteData('five');
final ByteData six = _makeByteData('six');
final ByteData seven = _makeByteData('seven');
buffers.push('a', one, (ByteData? data) { });
buffers.push('b', two, (ByteData? data) { });
buffers.push('a', three, (ByteData? data) { });
log.add('top');
buffers.setListener('a', (ByteData? data, ui.PlatformMessageResponseCallback callback) {
assert(data != null);
log.add('a1: ${utf8.decode(data!.buffer.asUint8List())}');
});
log.add('-1');
await null;
log.add('-2');
buffers.setListener('a', (ByteData? data, ui.PlatformMessageResponseCallback callback) {
assert(data != null);
log.add('a2: ${utf8.decode(data!.buffer.asUint8List())}');
});
log.add('-3');
await null;
log.add('-4');
buffers.setListener('b', (ByteData? data, ui.PlatformMessageResponseCallback callback) {
assert(data != null);
log.add('b: ${utf8.decode(data!.buffer.asUint8List())}');
});
log.add('-5');
await null; // first microtask after setting listener drains the first message
await null; // second microtask ends the draining.
log.add('-6');
buffers.push('b', four, (ByteData? data) { });
buffers.push('a', five, (ByteData? data) { });
log.add('-7');
await null;
log.add('-8');
buffers.clearListener('a');
buffers.push('a', six, (ByteData? data) { });
buffers.push('b', seven, (ByteData? data) { });
await null;
log.add('-9');
expect(log, <String>[
'top',
'-1',
'a1: three',
'-2',
'-3',
'-4',
'-5',
'b: two',
'-6',
'b: four',
'a2: five',
'-7',
'-8',
'b: seven',
'-9',
]);
});
test('ChannelBuffers.clearListener', () async {
final List<String> log = <String>[];
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
final ByteData one = _makeByteData('one');
final ByteData two = _makeByteData('two');
final ByteData three = _makeByteData('three');
final ByteData four = _makeByteData('four');
buffers.handleMessage(_makeByteData('resize\ra\r10'));
buffers.push('a', one, (ByteData? data) { });
buffers.push('a', two, (ByteData? data) { });
buffers.push('a', three, (ByteData? data) { });
log.add('-1');
buffers.setListener('a', (ByteData? data, ui.PlatformMessageResponseCallback callback) {
assert(data != null);
log.add('a1: ${utf8.decode(data!.buffer.asUint8List())}');
});
await null; // handles one
log.add('-2');
buffers.clearListener('a');
await null;
log.add('-3');
buffers.setListener('a', (ByteData? data, ui.PlatformMessageResponseCallback callback) {
assert(data != null);
log.add('a2: ${utf8.decode(data!.buffer.asUint8List())}');
});
log.add('-4');
await null;
buffers.push('a', four, (ByteData? data) { });
log.add('-5');
await null;
log.add('-6');
await null;
log.add('-7');
await null;
expect(log, <String>[
'-1',
'a1: one',
'-2',
'-3',
'-4',
'a2: two',
'-5',
'a2: three',
'-6',
'a2: four',
'-7',
]);
});
test('ChannelBuffers.handleMessage for resize', () async {
final List<String> log = <String>[];
final ui.ChannelBuffers buffers = _TestChannelBuffers(log);
// Created as follows:
// print(StandardMethodCodec().encodeMethodCall(MethodCall('resize', ['abcdef', 12345])).buffer.asUint8List());
// ...with three 0xFF bytes on either side to ensure the method works with an offset on the underlying buffer.
buffers.handleMessage(ByteData.sublistView(Uint8List.fromList(<int>[255, 255, 255, 7, 6, 114, 101, 115, 105, 122, 101, 12, 2, 7, 6, 97, 98, 99, 100, 101, 102, 3, 57, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255]), 3, 27));
expect(log, const <String>['resize abcdef 12345']);
});
test('ChannelBuffers.handleMessage for overflow', () async {
final List<String> log = <String>[];
final ui.ChannelBuffers buffers = _TestChannelBuffers(log);
// Created as follows:
// print(StandardMethodCodec().encodeMethodCall(MethodCall('overflow', ['abcdef', false])).buffer.asUint8List());
// ...with three 0xFF bytes on either side to ensure the method works with an offset on the underlying buffer.
buffers.handleMessage(ByteData.sublistView(Uint8List.fromList(<int>[255, 255, 255, 7, 8, 111, 118, 101, 114, 102, 108, 111, 119, 12, 2, 7, 6, 97, 98, 99, 100, 101, 102, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255]), 3, 24));
expect(log, const <String>['allowOverflow abcdef false']);
});
test('ChannelBuffers uses the right zones', () async {
final List<String> log = <String>[];
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
final Zone zone1 = Zone.current.fork();
final Zone zone2 = Zone.current.fork();
zone1.run(() {
log.add('first zone run: ${Zone.current == zone1}');
buffers.setListener('a', (ByteData? data, ui.PlatformMessageResponseCallback callback) {
log.add('callback1: ${Zone.current == zone1}');
callback(data);
});
});
zone2.run(() {
log.add('second zone run: ${Zone.current == zone2}');
buffers.push('a', ByteData.sublistView(Uint8List.fromList(<int>[]), 0, 0), (ByteData? data) {
log.add('callback2: ${Zone.current == zone2}');
});
});
await null;
expect(log, <String>[
'first zone run: true',
'second zone run: true',
'callback1: true',
'callback2: true',
]);
});
}
class _TestChannelBuffers extends ui.ChannelBuffers {
_TestChannelBuffers(this.log);
final List<String> log;
@override
void resize(String name, int newSize) {
log.add('resize $name $newSize');
}
@override
void allowOverflow(String name, bool allowed) {
log.add('allowOverflow $name $allowed');
}
}