Skip to content

Commit

Permalink
Migrate //testing to nullsafety (flutter#26825)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield authored Jun 18, 2021
1 parent 6be8b07 commit dea585f
Show file tree
Hide file tree
Showing 31 changed files with 123 additions and 217 deletions.
21 changes: 6 additions & 15 deletions testing/dart/canvas_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.6

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
Expand Down Expand Up @@ -54,8 +52,6 @@ void testNoCrashes() {
final Picture picture = recorder.endRecording();
final Image image = await picture.toImage(1, 1);

try { Canvas(null, null); } catch (error) { } // ignore: empty_catches
try { Canvas(null, rect); } catch (error) { } // ignore: empty_catches
try { Canvas(PictureRecorder(), null); } catch (error) { } // ignore: empty_catches
try { Canvas(PictureRecorder(), rect); } catch (error) { } // ignore: empty_catches

Expand Down Expand Up @@ -88,19 +84,18 @@ void testNoCrashes() {
testCanvas((Canvas canvas) => canvas.drawRawPoints(PointMode.points, Float32List(0), paint));
testCanvas((Canvas canvas) => canvas.drawRect(rect, paint));
testCanvas((Canvas canvas) => canvas.drawRRect(rrect, paint));
testCanvas((Canvas canvas) => canvas.drawShadow(path, color, double.nan, null));
testCanvas((Canvas canvas) => canvas.drawShadow(path, color, double.nan, false));
testCanvas((Canvas canvas) => canvas.drawShadow(path, color, double.nan, true));
testCanvas((Canvas canvas) => canvas.drawVertices(Vertices(VertexMode.triangles, <Offset>[]), null, paint));
testCanvas((Canvas canvas) => canvas.drawVertices(Vertices(VertexMode.triangles, <Offset>[]), BlendMode.screen, paint));
testCanvas((Canvas canvas) => canvas.getSaveCount());
testCanvas((Canvas canvas) => canvas.restore());
testCanvas((Canvas canvas) => canvas.rotate(double.nan));
testCanvas((Canvas canvas) => canvas.save());
testCanvas((Canvas canvas) => canvas.saveLayer(rect, paint));
testCanvas((Canvas canvas) => canvas.saveLayer(null, null));
testCanvas((Canvas canvas) => canvas.saveLayer(null, paint));
testCanvas((Canvas canvas) => canvas.scale(double.nan, double.nan));
testCanvas((Canvas canvas) => canvas.skew(double.nan, double.nan));
testCanvas((Canvas canvas) => canvas.transform(null));
testCanvas((Canvas canvas) => canvas.transform(Float64List(16)));
testCanvas((Canvas canvas) => canvas.translate(double.nan, double.nan));
});
}
Expand All @@ -112,8 +107,8 @@ Future<bool> fuzzyCompareImages(Image golden, Image img) async {
return false;
}
int getPixel(ByteData data, int x, int y) => data.getUint32((x + y * golden.width) * 4);
final ByteData goldenData = await golden.toByteData();
final ByteData imgData = await img.toByteData();
final ByteData goldenData = (await golden.toByteData())!;
final ByteData imgData = (await img.toByteData())!;
for (int y = 0; y < golden.height; y++) {
for (int x = 0; x < golden.width; x++) {
if (getPixel(goldenData, x, y) != getPixel(imgData, x, y)) {
Expand Down Expand Up @@ -144,7 +139,7 @@ Future<bool> fuzzyGoldenImageCompare(
}

if (!areEqual) {
final ByteData pngData = await image.toByteData(format: ImageByteFormat.png);
final ByteData pngData = (await image.toByteData(format: ImageByteFormat.png))!;
final String outPath = path.join(imagesPath, 'found_' + goldenImageName);
File(outPath).writeAsBytesSync(pngData.buffer.asUint8List());
print('wrote: ' + outPath);
Expand Down Expand Up @@ -231,11 +226,7 @@ void main() {
canvas.drawRawAtlas(image, Float32List(0), Float32List(0), Int32List(0), BlendMode.src, null, paint);
canvas.drawRawAtlas(image, Float32List(0), Float32List(0), null, null, rect, paint);

expectAssertion(() => canvas.drawAtlas(image, <RSTransform>[transform], <Rect>[rect], <Color>[color], BlendMode.src, rect, null));
expectAssertion(() => canvas.drawAtlas(image, <RSTransform>[transform], <Rect>[rect], <Color>[color], null, rect, paint));
expectAssertion(() => canvas.drawAtlas(image, <RSTransform>[transform], null, <Color>[color], BlendMode.src, rect, paint));
expectAssertion(() => canvas.drawAtlas(image, null, <Rect>[rect], <Color>[color], BlendMode.src, rect, paint));
expectAssertion(() => canvas.drawAtlas(null, <RSTransform>[transform], <Rect>[rect], <Color>[color], BlendMode.src, rect, paint));
});

test('Data lengths must match for drawAtlas methods', () async {
Expand Down
88 changes: 43 additions & 45 deletions testing/dart/channel_buffers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.6

// KEEP THIS SYNCHRONIZED WITH ../../lib/web_ui/test/channel_buffers_test.dart

import 'dart:async';
Expand All @@ -29,30 +27,29 @@ void main() {
final ByteData data = _makeByteData('bar');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
bool called = false;
void callback(ByteData responseData) {
void callback(ByteData? responseData) {
called = true;
}
buffers.push(channel, data, callback);
await buffers.drain(channel, (ByteData drainedData, ui.PlatformMessageResponseCallback drainedCallback) {
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
expect(drainedData, equals(data));
assert(!called);
drainedCallback(drainedData);
assert(called);
return;
});
});

test('drain is sync', () async {
const String channel = 'foo';
final ByteData data = _makeByteData('message');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
void callback(ByteData responseData) {}
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');
buffers.drain(channel, (ByteData drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
log.add('callback');
completer.complete();
});
Expand All @@ -73,13 +70,12 @@ void main() {
final ByteData data = _makeByteData('bar');
final
ui.ChannelBuffers buffers = ui.ChannelBuffers();
void callback(ByteData responseData) {}
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) {
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
didCall = true;
return;
});
expect(didCall, equals(false));
});
Expand All @@ -88,9 +84,8 @@ void main() {
const String channel = 'foo';
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
bool didCall = false;
await buffers.drain(channel, (ByteData drainedData, ui.PlatformMessageResponseCallback drainedCallback) {
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
didCall = true;
return;
});
expect(didCall, equals(false));
});
Expand All @@ -102,14 +97,14 @@ void main() {
final ByteData three = _makeByteData('three');
final ByteData four = _makeByteData('four');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
void callback(ByteData responseData) {}
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) {
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
switch (counter) {
case 0:
expect(drainedData, equals(two));
Expand All @@ -122,7 +117,6 @@ void main() {
break;
}
counter += 1;
return;
});
expect(counter, equals(3));
});
Expand All @@ -133,18 +127,17 @@ void main() {
final ByteData two = _makeByteData('two');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
_resize(buffers, channel, 100);
void callback(ByteData responseData) {}
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) {
await buffers.drain(channel, (ByteData? drainedData, ui.PlatformMessageResponseCallback drainedCallback) async {
switch (counter) {
case 0:
expect(drainedData, equals(two));
}
counter += 1;
return;
});
expect(counter, equals(1));
});
Expand All @@ -155,11 +148,11 @@ void main() {
final ByteData two = _makeByteData('two');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
bool didCallCallback = false;
void oneCallback(ByteData responseData) {
void oneCallback(ByteData? responseData) {
expect(responseData, isNull);
didCallCallback = true;
}
void twoCallback(ByteData responseData) {
void twoCallback(ByteData? responseData) {
fail('wrong callback called');
}
_resize(buffers, channel, 100);
Expand All @@ -176,11 +169,11 @@ void main() {
final ByteData two = _makeByteData('two');
final ui.ChannelBuffers buffers = ui.ChannelBuffers();
bool didCallCallback = false;
void oneCallback(ByteData responseData) {
void oneCallback(ByteData? responseData) {
expect(responseData, isNull);
didCallCallback = true;
}
void twoCallback(ByteData responseData) {
void twoCallback(ByteData? responseData) {
fail('wrong callback called');
}
_resize(buffers, channel, 1);
Expand Down Expand Up @@ -211,37 +204,40 @@ void main() {
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) { });
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) {
log.add('a1: ${utf8.decode(data.buffer.asUint8List())}');
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) {
log.add('a2: ${utf8.decode(data.buffer.asUint8List())}');
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) {
log.add('b: ${utf8.decode(data.buffer.asUint8List())}');
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) { });
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) { });
buffers.push('a', six, (ByteData? data) { });
buffers.push('b', seven, (ByteData? data) { });
await null;
log.add('-9');
expect(log, <String>[
Expand Down Expand Up @@ -271,24 +267,26 @@ void main() {
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) { });
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) {
log.add('a1: ${utf8.decode(data.buffer.asUint8List())}');
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) {
log.add('a2: ${utf8.decode(data.buffer.asUint8List())}');
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) { });
buffers.push('a', four, (ByteData? data) { });
log.add('-5');
await null;
log.add('-6');
Expand Down Expand Up @@ -337,14 +335,14 @@ void main() {
final Zone zone2 = Zone.current.fork();
zone1.run(() {
log.add('first zone run: ${Zone.current == zone1}');
buffers.setListener('a', (ByteData data, ui.PlatformMessageResponseCallback callback) {
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) {
buffers.push('a', ByteData.sublistView(Uint8List.fromList(<int>[]), 0, 0), (ByteData? data) {
log.add('callback2: ${Zone.current == zone2}');
});
});
Expand Down
1 change: 0 additions & 1 deletion testing/dart/codec_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.6
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
Expand Down
11 changes: 1 addition & 10 deletions testing/dart/color_filter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.6
import 'dart:typed_data';
import 'dart:ui';

Expand Down Expand Up @@ -36,20 +35,12 @@ void main() {
recorderCanvas.drawPaint(paint);
final Picture picture = recorder.endRecording();
final Image image = await picture.toImage(width, height);
final ByteData bytes = await image.toByteData();
final ByteData bytes = (await image.toByteData())!;

expect(bytes.lengthInBytes, width * height * 4);
return bytes.buffer.asUint32List();
}

test('ColorFilter - nulls', () async {
final Paint paint = Paint()..colorFilter = const ColorFilter.mode(null, null);
expect(paint.colorFilter, null);

paint.colorFilter = const ColorFilter.matrix(null);
expect(paint.colorFilter, null);
});

test('ColorFilter - mode', () async {
final Paint paint = Paint()
..color = green
Expand Down
1 change: 0 additions & 1 deletion testing/dart/color_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.6
import 'dart:ui';

import 'package:litetest/litetest.dart';
Expand Down
Loading

0 comments on commit dea585f

Please sign in to comment.