Showing
1 changed file
with
126 additions
and
0 deletions
test/scan_window_test.dart
0 → 100644
| 1 | +import 'package:flutter/painting.dart'; | ||
| 2 | +import 'package:flutter_test/flutter_test.dart'; | ||
| 3 | +import 'package:mobile_scanner/src/scan_window_calculation.dart'; | ||
| 4 | + | ||
| 5 | +void main() { | ||
| 6 | + group('Scan window relative to texture', () { | ||
| 7 | + group('Widget (landscape) smaller than texture (portrait)', () { | ||
| 8 | + const textureSize = Size(480.0, 640.0); | ||
| 9 | + const widgetSize = Size(432.0, 256.0); | ||
| 10 | + final ctx = ScanWindowTestContext( | ||
| 11 | + textureSize: textureSize, | ||
| 12 | + widgetSize: widgetSize, | ||
| 13 | + scanWindow: Rect.fromLTWH( | ||
| 14 | + widgetSize.width / 4, | ||
| 15 | + widgetSize.height / 4, | ||
| 16 | + widgetSize.width / 2, | ||
| 17 | + widgetSize.height / 2, | ||
| 18 | + ), | ||
| 19 | + ); | ||
| 20 | + | ||
| 21 | + test('wl tp: BoxFit.none', () { | ||
| 22 | + ctx.testScanWindow(BoxFit.none, const Rect.fromLTRB(0.275, 0.4, 0.725, 0.6)); | ||
| 23 | + }); | ||
| 24 | + | ||
| 25 | + test('wl tp: BoxFit.fill', () { | ||
| 26 | + ctx.testScanWindow(BoxFit.fill, const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75)); | ||
| 27 | + }); | ||
| 28 | + | ||
| 29 | + test('wl tp: BoxFit.fitHeight', () { | ||
| 30 | + ctx.testScanWindow(BoxFit.fitHeight, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | ||
| 31 | + }); | ||
| 32 | + | ||
| 33 | + test('wl tp: BoxFit.fitWidth', () { | ||
| 34 | + ctx.testScanWindow(BoxFit.fitWidth, const Rect.fromLTRB(0.25, 0.38888888888888895, 0.75, 0.6111111111111112)); | ||
| 35 | + }); | ||
| 36 | + | ||
| 37 | + test('wl tp: BoxFit.cover', () { | ||
| 38 | + // equal to fitWidth | ||
| 39 | + ctx.testScanWindow(BoxFit.cover, const Rect.fromLTRB(0.25, 0.38888888888888895, 0.75, 0.6111111111111112)); | ||
| 40 | + }); | ||
| 41 | + | ||
| 42 | + test('wl tp: BoxFit.contain', () { | ||
| 43 | + // equal to fitHeigth | ||
| 44 | + ctx.testScanWindow(BoxFit.contain, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | ||
| 45 | + }); | ||
| 46 | + | ||
| 47 | + test('wl tp: BoxFit.scaleDown', () { | ||
| 48 | + // equal to fitHeigth, contain | ||
| 49 | + ctx.testScanWindow(BoxFit.scaleDown, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | ||
| 50 | + }); | ||
| 51 | + }); | ||
| 52 | + | ||
| 53 | + group('Widget (landscape) smaller than texture and texture (landscape)', () { | ||
| 54 | + const textureSize = Size(640.0, 480.0); | ||
| 55 | + const widgetSize = Size(320.0, 120.0); | ||
| 56 | + final ctx = ScanWindowTestContext( | ||
| 57 | + textureSize: textureSize, | ||
| 58 | + widgetSize: widgetSize, | ||
| 59 | + scanWindow: Rect.fromLTWH( | ||
| 60 | + widgetSize.width / 4, | ||
| 61 | + widgetSize.height / 4, | ||
| 62 | + widgetSize.width / 2, | ||
| 63 | + widgetSize.height / 2, | ||
| 64 | + ), | ||
| 65 | + ); | ||
| 66 | + | ||
| 67 | + test('wl tl: BoxFit.none', () { | ||
| 68 | + ctx.testScanWindow(BoxFit.none, const Rect.fromLTRB(0.375, 0.4375, 0.625, 0.5625)); | ||
| 69 | + }); | ||
| 70 | + | ||
| 71 | + test('wl tl: BoxFit.fill', () { | ||
| 72 | + ctx.testScanWindow(BoxFit.fill, const Rect.fromLTRB(0.25, 0.25, 0.75, 0.75)); | ||
| 73 | + }); | ||
| 74 | + | ||
| 75 | + test('wl tl: BoxFit.fitHeight', () { | ||
| 76 | + ctx.testScanWindow(BoxFit.fitHeight, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | ||
| 77 | + }); | ||
| 78 | + | ||
| 79 | + test('wl tl: BoxFit.fitWidth', () { | ||
| 80 | + ctx.testScanWindow(BoxFit.fitWidth, const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625)); | ||
| 81 | + }); | ||
| 82 | + | ||
| 83 | + test('wl tl: BoxFit.cover', () { | ||
| 84 | + // equal to fitWidth | ||
| 85 | + ctx.testScanWindow(BoxFit.cover, const Rect.fromLTRB(0.25, 0.375, 0.75, 0.625)); | ||
| 86 | + }); | ||
| 87 | + | ||
| 88 | + test('wl tl: BoxFit.contain', () { | ||
| 89 | + // equal to fitHeigth | ||
| 90 | + ctx.testScanWindow(BoxFit.contain, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | ||
| 91 | + }); | ||
| 92 | + | ||
| 93 | + test('wl tl: BoxFit.scaleDown', () { | ||
| 94 | + // equal to fitHeigth, contain | ||
| 95 | + ctx.testScanWindow(BoxFit.scaleDown, const Rect.fromLTRB(0.0, 0.25, 1.0, 0.75)); | ||
| 96 | + }); | ||
| 97 | + }); | ||
| 98 | + }); | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +class ScanWindowTestContext { | ||
| 102 | + final Size textureSize; | ||
| 103 | + final Size widgetSize; | ||
| 104 | + final Rect scanWindow; | ||
| 105 | + | ||
| 106 | + ScanWindowTestContext({ | ||
| 107 | + required this.textureSize, | ||
| 108 | + required this.widgetSize, | ||
| 109 | + required this.scanWindow, | ||
| 110 | + }); | ||
| 111 | + | ||
| 112 | + void testScanWindow(BoxFit fit, Rect expected) { | ||
| 113 | + final actual = calculateScanWindowRelativeToTextureInPercentage( | ||
| 114 | + fit, | ||
| 115 | + scanWindow, | ||
| 116 | + textureSize, | ||
| 117 | + widgetSize, | ||
| 118 | + ); | ||
| 119 | + | ||
| 120 | + // don't use expect(actual, expected) because Rect.toString() only shows one digit after the comma which can be confusing | ||
| 121 | + expect(actual.left, expected.left); | ||
| 122 | + expect(actual.top, expected.top); | ||
| 123 | + expect(actual.right, expected.right); | ||
| 124 | + expect(actual.bottom, expected.bottom); | ||
| 125 | + } | ||
| 126 | +} |
-
Please register or login to post a comment