document_test.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import 'dart:io';
  17. import 'package:fis_lib_print/printing.dart';
  18. import 'package:flutter/services.dart';
  19. import 'package:flutter/widgets.dart';
  20. import 'package:flutter_test/flutter_test.dart';
  21. import 'package:pdf/widgets.dart' as pw;
  22. late pw.Document doc;
  23. pw.Font? ttf;
  24. void main() {
  25. final path = Directory.current.path.split('/').last == 'test' ? '..' : '.';
  26. const channel = MethodChannel('net.nfet.printing');
  27. TestWidgetsFlutterBinding.ensureInitialized();
  28. setUp(() {
  29. channel.setMockMethodCallHandler((MethodCall methodCall) async {
  30. // ignore: avoid_print
  31. print(methodCall);
  32. return '1';
  33. });
  34. });
  35. tearDown(() {
  36. channel.setMockMethodCallHandler(null);
  37. });
  38. test('convertHtml', () async {
  39. // expect(await Printing.platformVersion, '42');
  40. });
  41. test('flutterImageProvider(FileImage)', () async {
  42. final image =
  43. await flutterImageProvider(FileImage(File('$path/example.png')));
  44. doc.addPage(
  45. pw.Page(
  46. build: (pw.Context context) => pw.Center(
  47. child: pw.Container(
  48. child: pw.Image(image),
  49. ),
  50. ),
  51. ),
  52. );
  53. });
  54. setUpAll(() {
  55. pw.Document.debug = true;
  56. pw.RichText.debug = true;
  57. final fontData = File('$path/../pdf/open-sans.ttf').readAsBytesSync();
  58. ttf = pw.Font.ttf(fontData.buffer.asByteData());
  59. doc = pw.Document();
  60. });
  61. tearDownAll(() async {
  62. final file = File('printing.pdf');
  63. await file.writeAsBytes(await doc.save());
  64. });
  65. }