David PHAM-VAN

Add Text Widget tests

@@ -103,7 +103,7 @@ class RichText extends Widget { @@ -103,7 +103,7 @@ class RichText extends Widget {
103 this.maxLines}) 103 this.maxLines})
104 : assert(text != null); 104 : assert(text != null);
105 105
106 - static const bool debug = false; 106 + static bool debug = false;
107 107
108 final TextSpan text; 108 final TextSpan text;
109 109
@@ -30,6 +30,7 @@ import 'widget_clip_test.dart' as widget_clip; @@ -30,6 +30,7 @@ import 'widget_clip_test.dart' as widget_clip;
30 import 'widget_container_test.dart' as widget_container; 30 import 'widget_container_test.dart' as widget_container;
31 import 'widget_table_test.dart' as widget_table; 31 import 'widget_table_test.dart' as widget_table;
32 import 'widget_test.dart' as widget; 32 import 'widget_test.dart' as widget;
  33 +import 'widget_text_test.dart' as widget_text;
33 34
34 void main() { 35 void main() {
35 annotations.main(); 36 annotations.main();
@@ -44,6 +45,7 @@ void main() { @@ -44,6 +45,7 @@ void main() {
44 widget_clip.main(); 45 widget_clip.main();
45 widget_container.main(); 46 widget_container.main();
46 widget_table.main(); 47 widget_table.main();
  48 + widget_text.main();
47 widget.main(); 49 widget.main();
48 example.main(); 50 example.main();
49 } 51 }
  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:io';
  18 +import 'dart:math' as math;
  19 +import 'dart:typed_data';
  20 +
  21 +import 'package:test/test.dart';
  22 +import 'package:pdf/pdf.dart';
  23 +import 'package:pdf/widgets.dart';
  24 +
  25 +Document pdf;
  26 +Font ttf;
  27 +Font ttfBold;
  28 +
  29 +void main() {
  30 + setUpAll(() {
  31 + Document.debug = true;
  32 + RichText.debug = true;
  33 + final Uint8List fontData = File('open-sans.ttf').readAsBytesSync();
  34 + ttf = Font.ttf(fontData.buffer.asByteData());
  35 + final Uint8List fontDataBold = File('open-sans-bold.ttf').readAsBytesSync();
  36 + ttfBold = Font.ttf(fontDataBold.buffer.asByteData());
  37 + pdf = Document();
  38 + });
  39 +
  40 + test('Text Widgets Quotes', () {
  41 + pdf.addPage(Page(
  42 + build: (Context context) => Text('Text containing \' or " works!')));
  43 + });
  44 +
  45 + test('Text Widgets Unicode Quotes', () {
  46 + pdf.addPage(Page(
  47 + build: (Context context) => Text('Text containing ’ and ” works!',
  48 + style: TextStyle(font: ttf))));
  49 + });
  50 +
  51 + test('Text Widgets softWrap', () {
  52 + pdf.addPage(MultiPage(
  53 + build: (Context context) => <Widget>[
  54 + Text(
  55 + 'Text with\nsoft wrap\nenabled',
  56 + softWrap: true,
  57 + ),
  58 + Text(
  59 + 'Text with\nsoft wrap\ndisabled',
  60 + softWrap: false,
  61 + ),
  62 + ]));
  63 + });
  64 +
  65 + test('Text Widgets Alignement', () {
  66 + final String para = LoremText().paragraph(40);
  67 +
  68 + final List<Widget> widgets = <Widget>[];
  69 + for (TextAlign align in TextAlign.values) {
  70 + widgets.add(
  71 + Text(
  72 + '$align:\n' + para,
  73 + textAlign: align,
  74 + softWrap: true,
  75 + ),
  76 + );
  77 + }
  78 +
  79 + pdf.addPage(MultiPage(build: (Context context) => widgets));
  80 + });
  81 +
  82 + test('Text Widgets lineSpacing', () {
  83 + final String para = LoremText().paragraph(40);
  84 +
  85 + final List<Widget> widgets = <Widget>[];
  86 + for (double spacing = 0.0; spacing < 10.0; spacing += 2.0) {
  87 + widgets.add(
  88 + Text(para, style: TextStyle(font: ttf, lineSpacing: spacing)),
  89 + );
  90 + widgets.add(
  91 + SizedBox(height: 30),
  92 + );
  93 + }
  94 +
  95 + pdf.addPage(MultiPage(build: (Context context) => widgets));
  96 + });
  97 +
  98 + test('Text Widgets wordSpacing', () {
  99 + final String para = LoremText().paragraph(40);
  100 +
  101 + final List<Widget> widgets = <Widget>[];
  102 + for (double spacing = 0.0; spacing < 10.0; spacing += 2.0) {
  103 + widgets.add(
  104 + Text(para, style: TextStyle(font: ttf, wordSpacing: spacing)),
  105 + );
  106 + widgets.add(
  107 + SizedBox(height: 30),
  108 + );
  109 + }
  110 +
  111 + pdf.addPage(MultiPage(build: (Context context) => widgets));
  112 + });
  113 +
  114 + test('Text Widgets letterSpacing', () {
  115 + final String para = LoremText().paragraph(40);
  116 +
  117 + final List<Widget> widgets = <Widget>[];
  118 + for (double spacing = 0.0; spacing < 10.0; spacing += 2.0) {
  119 + widgets.add(
  120 + Text(para, style: TextStyle(font: ttf, letterSpacing: spacing)),
  121 + );
  122 + widgets.add(
  123 + SizedBox(height: 30),
  124 + );
  125 + }
  126 +
  127 + pdf.addPage(MultiPage(build: (Context context) => widgets));
  128 + });
  129 +
  130 + test('Text Widgets background', () {
  131 + final String para = LoremText().paragraph(40);
  132 + pdf.addPage(MultiPage(
  133 + build: (Context context) => <Widget>[
  134 + Text(
  135 + para,
  136 + style: TextStyle(
  137 + font: ttf,
  138 + background: PdfColors.purple50,
  139 + ),
  140 + ),
  141 + ]));
  142 + });
  143 +
  144 + test('Text Widgets RichText', () {
  145 + final math.Random rnd = math.Random(42);
  146 + final String para = LoremText(random: rnd).paragraph(40);
  147 +
  148 + final List<TextSpan> spans = <TextSpan>[];
  149 + for (String word in para.split(' ')) {
  150 + spans.add(
  151 + TextSpan(
  152 + text: '$word',
  153 + style: TextStyle(
  154 + font: ttf,
  155 + fontSize: rnd.nextDouble() * 20 + 20,
  156 + color:
  157 + PdfColors.primaries[rnd.nextInt(PdfColors.primaries.length)]),
  158 + ),
  159 + );
  160 + spans.add(const TextSpan(text: ' '));
  161 + }
  162 +
  163 + pdf.addPage(MultiPage(
  164 + build: (Context context) => <Widget>[
  165 + RichText(
  166 + text: TextSpan(
  167 + text: 'Hello ',
  168 + style: TextStyle(
  169 + font: ttf,
  170 + fontSize: 20,
  171 + ),
  172 + children: <TextSpan>[
  173 + TextSpan(
  174 + text: 'bold',
  175 + style: TextStyle(
  176 + font: ttfBold,
  177 + fontSize: 40,
  178 + color: PdfColors.blue)),
  179 + TextSpan(
  180 + text: ' world!\n',
  181 + children: spans,
  182 + ),
  183 + ],
  184 + ),
  185 + ),
  186 + ]));
  187 + });
  188 +
  189 + tearDownAll(() {
  190 + final File file = File('widgets-text.pdf');
  191 + file.writeAsBytesSync(pdf.save());
  192 + });
  193 +}