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