image_test.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * QR.Flutter
  3. * Copyright (c) 2019 the QR.Flutter authors.
  4. * See LICENSE for distribution and usage details.
  5. */
  6. import 'dart:io';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_test/flutter_test.dart';
  9. // ignore: directives_ordering
  10. import 'package:fis_lib_qrcode/qr_flutter.dart';
  11. void main() {
  12. testWidgets('QrImageView generates correct image', (tester) async {
  13. final qrImage = MaterialApp(
  14. home: Center(
  15. child: RepaintBoundary(
  16. child: QrImageView(
  17. data: 'This is a test image',
  18. version: QrVersions.auto,
  19. gapless: true,
  20. errorCorrectionLevel: QrErrorCorrectLevel.L,
  21. ),
  22. ),
  23. ),
  24. );
  25. await tester.pumpWidget(qrImage);
  26. await expectLater(
  27. find.byType(QrImageView),
  28. matchesGoldenFile('./.golden/qr_image_golden.png'),
  29. );
  30. });
  31. testWidgets('QrImageView generates correct image with eye style',
  32. (tester) async {
  33. final qrImage = MaterialApp(
  34. home: Center(
  35. child: RepaintBoundary(
  36. child: QrImageView(
  37. data: 'This is a test image',
  38. version: QrVersions.auto,
  39. gapless: true,
  40. errorCorrectionLevel: QrErrorCorrectLevel.L,
  41. eyeStyle: const QrEyeStyle(
  42. eyeShape: QrEyeShape.circle,
  43. color: Colors.green,
  44. ),
  45. ),
  46. ),
  47. ),
  48. );
  49. await tester.pumpWidget(qrImage);
  50. await expectLater(
  51. find.byType(QrImageView),
  52. matchesGoldenFile('./.golden/qr_image_eye_styled_golden.png'),
  53. );
  54. });
  55. testWidgets('QrImageView generates correct image with data module style',
  56. (tester) async {
  57. final qrImage = MaterialApp(
  58. home: Center(
  59. child: RepaintBoundary(
  60. child: QrImageView(
  61. data: 'This is a test image',
  62. version: QrVersions.auto,
  63. gapless: true,
  64. errorCorrectionLevel: QrErrorCorrectLevel.L,
  65. dataModuleStyle: const QrDataModuleStyle(
  66. dataModuleShape: QrDataModuleShape.circle,
  67. color: Colors.blue,
  68. ),
  69. ),
  70. ),
  71. ),
  72. );
  73. await tester.pumpWidget(qrImage);
  74. await expectLater(
  75. find.byType(QrImageView),
  76. matchesGoldenFile('./.golden/qr_image_data_module_styled_golden.png'),
  77. );
  78. });
  79. testWidgets(
  80. 'QrImageView generates correct image with eye and data module sytle',
  81. (tester) async {
  82. final qrImage = MaterialApp(
  83. home: Center(
  84. child: RepaintBoundary(
  85. child: QrImageView(
  86. data: 'This is a test image',
  87. version: QrVersions.auto,
  88. gapless: true,
  89. errorCorrectionLevel: QrErrorCorrectLevel.L,
  90. eyeStyle: const QrEyeStyle(
  91. eyeShape: QrEyeShape.circle,
  92. color: Colors.green,
  93. ),
  94. dataModuleStyle: const QrDataModuleStyle(
  95. dataModuleShape: QrDataModuleShape.circle,
  96. color: Colors.blue,
  97. ),
  98. ),
  99. ),
  100. ),
  101. );
  102. await tester.pumpWidget(qrImage);
  103. await expectLater(
  104. find.byType(QrImageView),
  105. matchesGoldenFile('./.golden/qr_image_eye_data_module_styled_golden.png'),
  106. );
  107. });
  108. testWidgets(
  109. 'QrImageView does not apply eye and data module color when foreground '
  110. 'color is also specified', (tester) async {
  111. final qrImage = MaterialApp(
  112. home: Center(
  113. child: RepaintBoundary(
  114. child: QrImageView(
  115. data: 'This is a test image',
  116. version: QrVersions.auto,
  117. gapless: true,
  118. foregroundColor: Colors.red,
  119. errorCorrectionLevel: QrErrorCorrectLevel.L,
  120. eyeStyle: const QrEyeStyle(
  121. eyeShape: QrEyeShape.circle,
  122. color: Colors.green,
  123. ),
  124. dataModuleStyle: const QrDataModuleStyle(
  125. dataModuleShape: QrDataModuleShape.circle,
  126. color: Colors.blue,
  127. ),
  128. ),
  129. ),
  130. ),
  131. );
  132. await tester.pumpWidget(qrImage);
  133. await expectLater(
  134. find.byType(QrImageView),
  135. matchesGoldenFile('./.golden/qr_image_foreground_colored_golden.png'),
  136. );
  137. });
  138. testWidgets('QrImageView generates correct image with logo', (tester) async {
  139. await pumpWidgetWithImages(
  140. tester,
  141. MaterialApp(
  142. home: Center(
  143. child: RepaintBoundary(
  144. child: QrImageView(
  145. data: 'This is a a qr code with a logo',
  146. version: QrVersions.auto,
  147. gapless: true,
  148. errorCorrectionLevel: QrErrorCorrectLevel.L,
  149. embeddedImage: FileImage(File('test/.images/logo_yakka.png')),
  150. ),
  151. ),
  152. ),
  153. ),
  154. ['test/.images/logo_yakka.png'],
  155. );
  156. await tester.pumpAndSettle();
  157. await expectLater(
  158. find.byType(QrImageView),
  159. matchesGoldenFile('./.golden/qr_image_logo_golden.png'),
  160. );
  161. });
  162. }
  163. /// Pre-cache images to make sure they show up in golden tests.
  164. ///
  165. /// See https://github.com/flutter/flutter/issues/36552 for more info.
  166. Future<void> pumpWidgetWithImages(
  167. WidgetTester tester,
  168. Widget widget,
  169. List<String> assetNames,
  170. ) async {
  171. Future<void>? precacheFuture;
  172. await tester.pumpWidget(
  173. Builder(builder: (buildContext) {
  174. precacheFuture = tester.runAsync(() async {
  175. await Future.wait([
  176. for (final assetName in assetNames)
  177. precacheImage(FileImage(File(assetName)), buildContext),
  178. ]);
  179. });
  180. return widget;
  181. }),
  182. );
  183. await precacheFuture;
  184. }
  185. Widget buildTestableWidget(Widget widget) {
  186. return MediaQuery(data: MediaQueryData(), child: MaterialApp(home: widget));
  187. }