printing_test.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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:typed_data';
  17. import 'package:fis_lib_print/printing.dart';
  18. import 'package:fis_lib_print/src/interface.dart';
  19. import 'package:flutter/widgets.dart';
  20. import 'package:flutter_test/flutter_test.dart';
  21. import 'package:mockito/mockito.dart';
  22. import 'package:pdf/pdf.dart';
  23. import 'package:plugin_platform_interface/plugin_platform_interface.dart';
  24. void main() {
  25. setUp(() {
  26. TestWidgetsFlutterBinding.ensureInitialized();
  27. final mock = MockPrinting();
  28. PrintingPlatform.instance = mock;
  29. });
  30. test('info', () async {
  31. final info = await Printing.info();
  32. expect(info, isInstanceOf<PrintingInfo>());
  33. });
  34. test('layoutPdf', () async {
  35. expect(
  36. await Printing.layoutPdf(
  37. onLayout: (_) => Uint8List(0),
  38. name: 'doc',
  39. format: PdfPageFormat.letter,
  40. ),
  41. true);
  42. });
  43. test('sharePdf', () async {
  44. expect(
  45. await Printing.sharePdf(
  46. bytes: Uint8List(0),
  47. ),
  48. true,
  49. );
  50. });
  51. test('pickPrinter', () async {
  52. expect(
  53. await Printing.pickPrinter(context: MockContext()),
  54. null,
  55. );
  56. });
  57. test('directPrintPdf', () async {
  58. expect(
  59. await Printing.directPrintPdf(
  60. onLayout: (_) => Uint8List(0),
  61. printer: const Printer(url: 'test'),
  62. ),
  63. true,
  64. );
  65. });
  66. test('convertHtml', () async {
  67. expect(
  68. await Printing.convertHtml(html: '<html></html>'),
  69. isInstanceOf<Uint8List>(),
  70. );
  71. });
  72. test('raster', () async {
  73. expect(
  74. Printing.raster(Uint8List(0)),
  75. isInstanceOf<Stream>(),
  76. );
  77. });
  78. test('test image', () async {
  79. final bytes = Uint8List.fromList([
  80. 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 1, //
  81. 0, 0, 0, 1, 0, 1, 3, 0, 0, 0, 102, 188, 58, 37, 0, 0, 0, 3, 80, 76, 84,
  82. 69, 181, 208, 208, 99, 4, 22, 234, 0, 0, 0, 31, 73, 68, 65, 84, 104,
  83. 129, 237, 193, 1, 13, 0, 0, 0, 194, 160, 247, 79, 109, 14, 55, 160, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 190, 13, 33, 0, 0, 1, 154, 96, 225, 213, 0, 0, 0, 0, 73,
  85. 69, 78, 68, 174, 66, 96, 130
  86. ]);
  87. final imageProvider = Image.memory(bytes).image;
  88. expect(await flutterImageProvider(imageProvider), isNotNull);
  89. });
  90. }
  91. class MockPrinting extends Mock
  92. with MockPlatformInterfaceMixin
  93. implements PrintingPlatform {
  94. @override
  95. Future<PrintingInfo> info() async => const PrintingInfo();
  96. @override
  97. Future<bool> layoutPdf(
  98. Printer? printer,
  99. LayoutCallback onLayout,
  100. String name,
  101. PdfPageFormat format,
  102. bool dynamicLayout,
  103. bool usePrinterSettings,
  104. ) async =>
  105. true;
  106. @override
  107. Future<bool> sharePdf(Uint8List bytes, String filename, Rect bounds,
  108. String? subject, String? body, List<String>? emails) async =>
  109. true;
  110. @override
  111. Future<Printer?> pickPrinter(Rect bounds) async => null;
  112. @override
  113. Stream<PdfRaster> raster(
  114. Uint8List document, List<int>? pages, double dpi) async* {}
  115. @override
  116. Future<Uint8List> convertHtml(
  117. String html, String? baseUrl, PdfPageFormat format) async =>
  118. Uint8List(0);
  119. }
  120. class MockContext extends Mock implements BuildContext {}