David PHAM-VAN

Improve tests

@@ -48,3 +48,5 @@ test/diff @@ -48,3 +48,5 @@ test/diff
48 ref 48 ref
49 !test/golden/*.pdf 49 !test/golden/*.pdf
50 .flutter-plugins-dependencies 50 .flutter-plugins-dependencies
  51 +
  52 +cache_*
@@ -25,6 +25,8 @@ import 'package:pdf/pdf.dart'; @@ -25,6 +25,8 @@ import 'package:pdf/pdf.dart';
25 import 'package:pdf/widgets.dart'; 25 import 'package:pdf/widgets.dart';
26 import 'package:test/test.dart'; 26 import 'package:test/test.dart';
27 27
  28 +import 'utils.dart';
  29 +
28 class Message { 30 class Message {
29 Message(this.image, this.sendPort); 31 Message(this.image, this.sendPort);
30 32
@@ -32,16 +34,6 @@ class Message { @@ -32,16 +34,6 @@ class Message {
32 final SendPort sendPort; 34 final SendPort sendPort;
33 } 35 }
34 36
35 -Future<Uint8List> download(String url) async {  
36 - final HttpClient client = HttpClient();  
37 - final HttpClientRequest request = await client.getUrl(Uri.parse(url));  
38 - final HttpClientResponse response = await request.close();  
39 - final BytesBuilder builder = await response.fold(  
40 - BytesBuilder(), (BytesBuilder b, List<int> d) => b..add(d));  
41 - final List<int> data = builder.takeBytes();  
42 - return Uint8List.fromList(data);  
43 -}  
44 -  
45 void compute(Message message) { 37 void compute(Message message) {
46 final Document pdf = Document(); 38 final Document pdf = Document();
47 39
@@ -16,15 +16,15 @@ @@ -16,15 +16,15 @@
16 16
17 // ignore_for_file: omit_local_variable_types 17 // ignore_for_file: omit_local_variable_types
18 18
19 -import 'dart:async';  
20 import 'dart:convert'; 19 import 'dart:convert';
21 import 'dart:io'; 20 import 'dart:io';
22 -import 'dart:typed_data';  
23 21
24 import 'package:pdf/pdf.dart'; 22 import 'package:pdf/pdf.dart';
25 import 'package:pdf/widgets.dart'; 23 import 'package:pdf/widgets.dart';
26 import 'package:test/test.dart'; 24 import 'package:test/test.dart';
27 25
  26 +import 'utils.dart';
  27 +
28 Document pdf; 28 Document pdf;
29 PdfImage image; 29 PdfImage image;
30 30
@@ -118,16 +118,6 @@ void main() { @@ -118,16 +118,6 @@ void main() {
118 }); 118 });
119 } 119 }
120 120
121 -Future<Uint8List> download(String url) async {  
122 - final HttpClient client = HttpClient();  
123 - final HttpClientRequest request = await client.getUrl(Uri.parse(url));  
124 - final HttpClientResponse response = await request.close();  
125 - final BytesBuilder builder = await response.fold(  
126 - BytesBuilder(), (BytesBuilder b, List<int> d) => b..add(d));  
127 - final List<int> data = builder.takeBytes();  
128 - return Uint8List.fromList(data);  
129 -}  
130 -  
131 const List<String> images = <String>[ 121 const List<String> images = <String>[
132 '/9j/4AAQSkZJRgABAQEA3ADcAAD/4QAiRXhpZgAASUkqAAgAAAABABIBAwABAAAAAQAAAAAAAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCABQADADASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAkI/8QAFBABAAAAAAAAAAAAAAAAAAAAAP/EABQBAQAAAAAAAAAAAAAAAAAAAAD/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwDKgAAAAAAAKqAAAAlWAAACqgAJVgAqoAAACVYAKqAAAA//2Q==', 122 '/9j/4AAQSkZJRgABAQEA3ADcAAD/4QAiRXhpZgAASUkqAAgAAAABABIBAwABAAAAAQAAAAAAAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCABQADADASIAAhEBAxEB/8QAF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MBAAIRAxEAPwDKgAAAAAKqAAAAlWACqgAJVgAAAqoAAACVYAKqAAAAlWAD/9k=',
  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 +// ignore_for_file: omit_local_variable_types
  18 +
  19 +import 'dart:async';
  20 +import 'dart:io';
  21 +import 'dart:math' as math;
  22 +import 'dart:typed_data';
  23 +
  24 +import 'package:pdf/pdf.dart';
  25 +import 'package:pdf/widgets.dart';
  26 +
  27 +Future<Uint8List> download(
  28 + String url, {
  29 + bool cache = true,
  30 + String prefix = 'cache_',
  31 + String suffix,
  32 +}) async {
  33 + File file;
  34 + if (cache) {
  35 + if (suffix == null) {
  36 + final int l = url.lastIndexOf('.');
  37 + if (l >= 0) {
  38 + suffix = url.substring(l);
  39 + }
  40 + }
  41 + file = File('$prefix${url.hashCode}$suffix');
  42 + if (file.existsSync()) {
  43 + return await file.readAsBytes();
  44 + }
  45 + }
  46 +
  47 + print('Downloading $url');
  48 + final HttpClient client = HttpClient();
  49 + final HttpClientRequest request = await client.getUrl(Uri.parse(url));
  50 + final HttpClientResponse response = await request.close();
  51 + final BytesBuilder builder = await response.fold(
  52 + BytesBuilder(), (BytesBuilder b, List<int> d) => b..add(d));
  53 + final List<int> data = builder.takeBytes();
  54 +
  55 + if (cache) {
  56 + await file.writeAsBytes(data);
  57 + }
  58 + return Uint8List.fromList(data);
  59 +}
  60 +
  61 +PdfImage generateBitmap(PdfDocument pdf, int w, int h) {
  62 + final Uint32List bm = Uint32List(w * h);
  63 + final double dw = w.toDouble();
  64 + final double dh = h.toDouble();
  65 + for (int y = 0; y < h; y++) {
  66 + for (int x = 0; x < w; x++) {
  67 + bm[y * w + x] = (math.sin(x / dw) * 256).toInt() |
  68 + (math.sin(y / dh) * 256).toInt() << 8 |
  69 + (math.sin(x / dw * y / dh) * 256).toInt() << 16 |
  70 + 0xff000000;
  71 + }
  72 + }
  73 +
  74 + return PdfImage(
  75 + pdf,
  76 + image: bm.buffer.asUint8List(),
  77 + width: w,
  78 + height: h,
  79 + );
  80 +}
  81 +
  82 +Font loadFont(String filename) {
  83 + final Uint8List data = File(filename).readAsBytesSync();
  84 + return Font.ttf(data.buffer.asByteData());
  85 +}
@@ -17,35 +17,14 @@ @@ -17,35 +17,14 @@
17 // ignore_for_file: omit_local_variable_types 17 // ignore_for_file: omit_local_variable_types
18 18
19 import 'dart:io'; 19 import 'dart:io';
20 -import 'dart:math' as math;  
21 -import 'dart:typed_data';  
22 20
23 -import 'package:test/test.dart';  
24 import 'package:pdf/pdf.dart'; 21 import 'package:pdf/pdf.dart';
25 import 'package:pdf/widgets.dart'; 22 import 'package:pdf/widgets.dart';
  23 +import 'package:test/test.dart';
26 24
27 -Document pdf; 25 +import 'utils.dart';
28 26
29 -PdfImage generateBitmap(PdfDocument pdf, int w, int h) {  
30 - final Uint32List bm = Uint32List(w * h);  
31 - final double dw = w.toDouble();  
32 - final double dh = h.toDouble();  
33 - for (int y = 0; y < h; y++) {  
34 - for (int x = 0; x < w; x++) {  
35 - bm[y * w + x] = (math.sin(x / dw) * 256).toInt() |  
36 - (math.sin(y / dh) * 256).toInt() << 8 |  
37 - (math.sin(x / dw * y / dh) * 256).toInt() << 16 |  
38 - 0xff000000;  
39 - }  
40 - }  
41 -  
42 - return PdfImage(  
43 - pdf,  
44 - image: bm.buffer.asUint8List(),  
45 - width: w,  
46 - height: h,  
47 - );  
48 -} 27 +Document pdf;
49 28
50 void main() { 29 void main() {
51 setUpAll(() { 30 setUpAll(() {
@@ -17,11 +17,12 @@ @@ -17,11 +17,12 @@
17 // ignore_for_file: omit_local_variable_types 17 // ignore_for_file: omit_local_variable_types
18 18
19 import 'dart:io'; 19 import 'dart:io';
20 -import 'dart:typed_data';  
21 20
22 import 'package:pdf/pdf.dart'; 21 import 'package:pdf/pdf.dart';
23 -import 'package:test/test.dart';  
24 import 'package:pdf/widgets.dart'; 22 import 'package:pdf/widgets.dart';
  23 +import 'package:test/test.dart';
  24 +
  25 +import 'utils.dart';
25 26
26 Document pdf; 27 Document pdf;
27 Font openSans; 28 Font openSans;
@@ -30,11 +31,6 @@ Font roboto; @@ -30,11 +31,6 @@ Font roboto;
30 Font notoSans; 31 Font notoSans;
31 Font genyomintw; 32 Font genyomintw;
32 33
33 -Font loadFont(String filename) {  
34 - final Uint8List data = File(filename).readAsBytesSync();  
35 - return Font.ttf(data.buffer.asByteData());  
36 -}  
37 -  
38 void main() { 34 void main() {
39 setUpAll(() { 35 setUpAll(() {
40 Document.debug = true; 36 Document.debug = true;