Showing
2 changed files
with
160 additions
and
0 deletions
test/flutter_screenutil_test.dart
0 → 100644
1 | +import 'package:flutter/material.dart'; | ||
2 | +import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
3 | +import 'package:flutter_test/flutter_test.dart'; | ||
4 | + | ||
5 | +import 'home.test.dart'; | ||
6 | + | ||
7 | +void main() { | ||
8 | + const smallerDeviceSize = Size(300, 600); | ||
9 | + const smallerDeviceData = MediaQueryData(size: smallerDeviceSize); | ||
10 | + | ||
11 | + const biggerDeviceSize = Size(500, 900); | ||
12 | + const biggerDeviceData = MediaQueryData(size: biggerDeviceSize); | ||
13 | + | ||
14 | + const uiSize = Size(470, 740); | ||
15 | + | ||
16 | + group('[Test calculations]', () { | ||
17 | + test('Test smaller size', () { | ||
18 | + ScreenUtil.configure( | ||
19 | + data: smallerDeviceData, | ||
20 | + designSize: uiSize, | ||
21 | + minTextAdapt: true, | ||
22 | + splitScreenMode: false, | ||
23 | + ); | ||
24 | + | ||
25 | + expect(1.w, smallerDeviceSize.width / uiSize.width); | ||
26 | + expect(1.w < 1, true); | ||
27 | + expect(1.h, smallerDeviceSize.height / uiSize.height); | ||
28 | + expect(1.h < 1, true); | ||
29 | + }); | ||
30 | + | ||
31 | + test('Test bigger size', () { | ||
32 | + ScreenUtil.configure( | ||
33 | + data: biggerDeviceData, | ||
34 | + designSize: uiSize, | ||
35 | + minTextAdapt: true, | ||
36 | + splitScreenMode: false, | ||
37 | + ); | ||
38 | + | ||
39 | + expect(1.w, biggerDeviceSize.width / uiSize.width); | ||
40 | + expect(1.w > 1, true); | ||
41 | + expect(1.h, biggerDeviceSize.height / uiSize.height); | ||
42 | + expect(1.h > 1, true); | ||
43 | + }); | ||
44 | + }); | ||
45 | + | ||
46 | + group('[Test overflow]', () { | ||
47 | + testWidgets('Test overflow width', (tester) async { | ||
48 | + await tester.pumpWidget(ScreenUtilInit( | ||
49 | + designSize: uiSize, | ||
50 | + child: MaterialApp(home: WidgetTest(width: () => uiSize.width.w)), | ||
51 | + )); | ||
52 | + | ||
53 | + // Wait until all widget rendered | ||
54 | + await tester.pumpAndSettle(); | ||
55 | + | ||
56 | + // a Text widget must be present | ||
57 | + expect(find.text('Test'), findsOneWidget); | ||
58 | + }); | ||
59 | + | ||
60 | + testWidgets('Test overflow height', (tester) async { | ||
61 | + await tester.pumpWidget(ScreenUtilInit( | ||
62 | + designSize: uiSize, | ||
63 | + child: MaterialApp(home: WidgetTest(height: () => uiSize.height.h)), | ||
64 | + )); | ||
65 | + | ||
66 | + // Wait until all widget rendered | ||
67 | + await tester.pumpAndSettle(); | ||
68 | + | ||
69 | + // a Text widget must be present | ||
70 | + expect(find.text('Test'), findsOneWidget); | ||
71 | + }); | ||
72 | + }); | ||
73 | + | ||
74 | + testWidgets('[Rebuilding]', (tester) async { | ||
75 | + final textFieldKey = UniqueKey(); | ||
76 | + final buildCountNotifier = ValueNotifier(0); | ||
77 | + final focusNode = FocusNode(); | ||
78 | + | ||
79 | + Finder textField() => find.byKey(textFieldKey); | ||
80 | + | ||
81 | + await tester.pumpWidget(ScreenUtilInit( | ||
82 | + designSize: uiSize, | ||
83 | + rebuildFactor: RebuildFactors.always, | ||
84 | + child: MaterialApp( | ||
85 | + home: Scaffold( | ||
86 | + body: Builder( | ||
87 | + builder: (context) { | ||
88 | + buildCountNotifier.value += 1; | ||
89 | + | ||
90 | + assert(uiSize.width.w == MediaQuery.of(context).size.width); | ||
91 | + | ||
92 | + return SizedBox( | ||
93 | + width: 1.sw, | ||
94 | + child: Column( | ||
95 | + children: [ | ||
96 | + ValueListenableBuilder<int>( | ||
97 | + valueListenable: buildCountNotifier, | ||
98 | + builder: (_, count, __) => Text('Built count: $count'), | ||
99 | + ), | ||
100 | + TextField( | ||
101 | + key: textFieldKey, | ||
102 | + focusNode: focusNode, | ||
103 | + ), | ||
104 | + ], | ||
105 | + ), | ||
106 | + ); | ||
107 | + }, | ||
108 | + ), | ||
109 | + ), | ||
110 | + ), | ||
111 | + )); | ||
112 | + | ||
113 | + await tester.pumpAndSettle(); | ||
114 | + expect(buildCountNotifier.value, 1); | ||
115 | + | ||
116 | + expect(textField(), findsOneWidget); | ||
117 | + expect(focusNode.hasFocus, false); | ||
118 | + | ||
119 | + await tester.tap(textField()).then((_) => tester.pumpAndSettle()); | ||
120 | + expect(textField(), findsOneWidget); | ||
121 | + expect(focusNode.hasFocus, true); | ||
122 | + expect(buildCountNotifier.value, 1); | ||
123 | + | ||
124 | + // Simulate keyboard | ||
125 | + tester.view.viewInsets = FakeViewPadding(bottom: 20); | ||
126 | + | ||
127 | + await tester.pumpAndSettle(); | ||
128 | + expect(focusNode.hasFocus, true); | ||
129 | + expect(buildCountNotifier.value, 1); | ||
130 | + }); | ||
131 | +} |
test/home.test.dart
0 → 100644
1 | +import 'package:flutter/widgets.dart'; | ||
2 | + | ||
3 | +class WidgetTest extends StatelessWidget { | ||
4 | + const WidgetTest({ | ||
5 | + super.key, | ||
6 | + this.width = _zero, | ||
7 | + this.height = _zero, | ||
8 | + }); | ||
9 | + | ||
10 | + final double Function() width; | ||
11 | + final double Function() height; | ||
12 | + | ||
13 | + @override | ||
14 | + Widget build(BuildContext context) { | ||
15 | + return LayoutBuilder( | ||
16 | + builder: (_, c) { | ||
17 | + final w = width(), h = height(); | ||
18 | + | ||
19 | + if (c.biggest >= Size(w, h)) { | ||
20 | + return const Text('Test'); | ||
21 | + } | ||
22 | + | ||
23 | + throw Error(); | ||
24 | + }, | ||
25 | + ); | ||
26 | + } | ||
27 | + | ||
28 | + static double _zero() => 0; | ||
29 | +} |
-
Please register or login to post a comment