David PHAM-VAN

Add Isolates test

@@ -20,6 +20,7 @@ import '../example/main.dart' as example; @@ -20,6 +20,7 @@ import '../example/main.dart' as example;
20 import 'annotations_test.dart' as annotations; 20 import 'annotations_test.dart' as annotations;
21 import 'colors_test.dart' as colors; 21 import 'colors_test.dart' as colors;
22 import 'complex_test.dart' as complex; 22 import 'complex_test.dart' as complex;
  23 +import 'isolate_test.dart' as isolate;
23 import 'jpeg_test.dart' as jpeg; 24 import 'jpeg_test.dart' as jpeg;
24 import 'metrics_test.dart' as metrics; 25 import 'metrics_test.dart' as metrics;
25 import 'minimal_test.dart' as minimal; 26 import 'minimal_test.dart' as minimal;
@@ -40,6 +41,7 @@ void main() { @@ -40,6 +41,7 @@ void main() {
40 colors.main(); 41 colors.main();
41 complex.main(); 42 complex.main();
42 example.main(); 43 example.main();
  44 + isolate.main();
43 jpeg.main(); 45 jpeg.main();
44 metrics.main(); 46 metrics.main();
45 minimal.main(); 47 minimal.main();
  1 +/*
  2 + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +import 'dart:async';
  18 +import 'dart:io';
  19 +import 'dart:isolate';
  20 +import 'dart:typed_data';
  21 +
  22 +import 'package:pdf/pdf.dart';
  23 +import 'package:pdf/widgets.dart';
  24 +import 'package:test/test.dart';
  25 +
  26 +class Message {
  27 + Message(this.image, this.sendPort);
  28 +
  29 + final Uint8List image;
  30 + final SendPort sendPort;
  31 +}
  32 +
  33 +Future<Uint8List> download(String url) async {
  34 + final HttpClient client = HttpClient();
  35 + final HttpClientRequest request = await client.getUrl(Uri.parse(url));
  36 + final HttpClientResponse response = await request.close();
  37 + final BytesBuilder builder = await response.fold(
  38 + BytesBuilder(), (BytesBuilder b, List<int> d) => b..add(d));
  39 + final List<int> data = builder.takeBytes();
  40 + return Uint8List.fromList(data);
  41 +}
  42 +
  43 +void compute(Message message) {
  44 + final Document pdf = Document();
  45 +
  46 + final PdfImage image = PdfImage.jpeg(
  47 + pdf.document,
  48 + image: message.image,
  49 + );
  50 +
  51 + pdf.addPage(Page(build: (Context context) => Center(child: Image(image))));
  52 +
  53 + message.sendPort.send(pdf.save());
  54 +}
  55 +
  56 +void main() {
  57 + test('Pdf Isolate', () async {
  58 + final Completer<void> completer = Completer<void>();
  59 + final ReceivePort receivePort = ReceivePort();
  60 +
  61 + receivePort.listen((dynamic data) async {
  62 + if (data is List<int>) {
  63 + print('Received a ${data.length} bytes PDF');
  64 + final File file = File('isolate.pdf');
  65 + await file.writeAsBytes(data);
  66 + print('File saved');
  67 + }
  68 + completer.complete();
  69 + });
  70 +
  71 + print('Download image');
  72 + final Uint8List imageBytes =
  73 + await download('https://www.nfet.net/nfet.jpg');
  74 +
  75 + print('Generate PDF');
  76 + await Isolate.spawn<Message>(
  77 + compute,
  78 + Message(imageBytes, receivePort.sendPort),
  79 + );
  80 +
  81 + print('Wait PDF to be generated');
  82 + await completer.future;
  83 + print('Done');
  84 + });
  85 +}