66 lines
2 KiB
Dart
66 lines
2 KiB
Dart
|
|
import 'dart:typed_data';
|
|||
|
|
import 'dart:ui' as ui;
|
|||
|
|
|
|||
|
|
import 'package:flutter_test/flutter_test.dart';
|
|||
|
|
import 'package:halkyon_learn_flutter/src/feature/profile/data/avatar_image.dart';
|
|||
|
|
|
|||
|
|
/// A PNG [width] × [height], red on its left half, blue on its right.
|
|||
|
|
Future<Uint8List> picture(int width, int height) async {
|
|||
|
|
final recorder = ui.PictureRecorder();
|
|||
|
|
ui.Canvas(recorder)
|
|||
|
|
..drawRect(
|
|||
|
|
ui.Rect.fromLTWH(0, 0, width / 2, height.toDouble()),
|
|||
|
|
ui.Paint()..color = const ui.Color(0xFFFF0000),
|
|||
|
|
)
|
|||
|
|
..drawRect(
|
|||
|
|
ui.Rect.fromLTWH(width / 2, 0, width / 2, height.toDouble()),
|
|||
|
|
ui.Paint()..color = const ui.Color(0xFF0000FF),
|
|||
|
|
);
|
|||
|
|
final image = await recorder.endRecording().toImage(width, height);
|
|||
|
|
final png = await image.toByteData(format: ui.ImageByteFormat.png);
|
|||
|
|
return png!.buffer.asUint8List();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Future<ui.Image> decoded(Uint8List png) async {
|
|||
|
|
final codec = await ui.instantiateImageCodec(png);
|
|||
|
|
return (await codec.getNextFrame()).image;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void main() {
|
|||
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|||
|
|
|
|||
|
|
test('a picture becomes its middle square, at most 400 pixels a side, as '
|
|||
|
|
'a PNG', () async {
|
|||
|
|
final wide = await avatarPng(await picture(1200, 600));
|
|||
|
|
final image = await decoded(wide);
|
|||
|
|
|
|||
|
|
expect((image.width, image.height), (400, 400));
|
|||
|
|
expect(wide.sublist(0, 8), [
|
|||
|
|
0x89,
|
|||
|
|
0x50,
|
|||
|
|
0x4e,
|
|||
|
|
0x47,
|
|||
|
|
0x0d,
|
|||
|
|
0x0a,
|
|||
|
|
0x1a,
|
|||
|
|
0x0a,
|
|||
|
|
]);
|
|||
|
|
// The middle square of a picture red and blue side by side holds both.
|
|||
|
|
final pixels = (await image.toByteData())!;
|
|||
|
|
expect(pixels.getUint8(0), greaterThan(200));
|
|||
|
|
expect(pixels.getUint8(399 * 4 + 2), greaterThan(200));
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
test('a small picture keeps its size', () async {
|
|||
|
|
final image = await decoded(await avatarPng(await picture(100, 160)));
|
|||
|
|
|
|||
|
|
expect((image.width, image.height), (100, 100));
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
test('what is no picture is refused', () async {
|
|||
|
|
await expectLater(
|
|||
|
|
avatarPng(Uint8List.fromList([1, 2, 3])),
|
|||
|
|
throwsA(anything),
|
|||
|
|
);
|
|||
|
|
});
|
|||
|
|
}
|