|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:getwidget/getwidget.dart'; |
| 4 | +import 'package:flutter/cupertino.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + testWidgets('GFAccordion can be created.', (tester) async { |
| 8 | + const GFAccordion accordion = GFAccordion( |
| 9 | + title: 'title bar', |
| 10 | + content: 'content body', |
| 11 | + ); |
| 12 | + |
| 13 | + const TestApp app = TestApp(accordion); |
| 14 | + await tester.pumpWidget(app); |
| 15 | + |
| 16 | + expect(find.text('title bar'), findsOneWidget); |
| 17 | + expect(find.text('content body'), findsNothing); |
| 18 | + await tester.tap(find.text('title bar')); |
| 19 | + await tester.pump(); |
| 20 | + expect(find.text('title bar'), findsOneWidget); |
| 21 | + expect(find.text('content body'), findsOneWidget); |
| 22 | + }); |
| 23 | + |
| 24 | + testWidgets('GFAccordion can be constructed with child.', (tester) async { |
| 25 | + const GFAccordion accordion = GFAccordion( |
| 26 | + titleChild: Text('title bar'), |
| 27 | + contentChild: Text('content body'), |
| 28 | + ); |
| 29 | + |
| 30 | + const TestApp app = TestApp(accordion); |
| 31 | + await tester.pumpWidget(app); |
| 32 | + |
| 33 | + expect(find.text('title bar'), findsOneWidget); |
| 34 | + expect(find.text('content body'), findsNothing); |
| 35 | + await tester.tap(find.text('title bar')); |
| 36 | + await tester.pump(); |
| 37 | + expect(find.text('title bar'), findsOneWidget); |
| 38 | + expect(find.text('content body'), findsOneWidget); |
| 39 | + }); |
| 40 | + |
| 41 | + testWidgets( |
| 42 | + 'GFAccordion can be constructed with expanded and collapsed icons', |
| 43 | + (tester) async { |
| 44 | + const GFAccordion accordion = GFAccordion( |
| 45 | + titleChild: Text('title bar'), |
| 46 | + contentChild: Text('content body'), |
| 47 | + collapsedIcon: Icon(Icons.add), |
| 48 | + expandedIcon: Icon(Icons.remove), |
| 49 | + ); |
| 50 | + |
| 51 | + const TestApp app = TestApp(accordion); |
| 52 | + await tester.pumpWidget(app); |
| 53 | + |
| 54 | + expect(find.text('title bar'), findsOneWidget); |
| 55 | + expect(find.text('content body'), findsNothing); |
| 56 | + expect(find.byIcon(Icons.add), findsOneWidget); |
| 57 | + await tester.tap(find.text('title bar')); |
| 58 | + await tester.pump(); |
| 59 | + expect(find.text('title bar'), findsOneWidget); |
| 60 | + expect(find.text('content body'), findsOneWidget); |
| 61 | + expect(find.byIcon(Icons.remove), findsOneWidget); |
| 62 | + }); |
| 63 | + |
| 64 | + testWidgets('GFAccordion can be constructed with show accordion', |
| 65 | + (tester) async { |
| 66 | + const GFAccordion accordion = GFAccordion( |
| 67 | + titleChild: Text('title bar'), |
| 68 | + contentChild: Text('content body'), |
| 69 | + showAccordion: true, |
| 70 | + ); |
| 71 | + |
| 72 | + const TestApp app = TestApp(accordion); |
| 73 | + await tester.pumpWidget(app); |
| 74 | + |
| 75 | + expect(find.text('title bar'), findsOneWidget); |
| 76 | + expect(find.text('content body'), findsOneWidget); |
| 77 | + }); |
| 78 | + |
| 79 | + testWidgets( |
| 80 | + 'GFAccordion can be constructed with show button on toggle collapsed', |
| 81 | + (tester) async { |
| 82 | + bool showButton = false; |
| 83 | + |
| 84 | + final accordion = MaterialApp( |
| 85 | + home: Directionality( |
| 86 | + textDirection: TextDirection.ltr, |
| 87 | + child: StatefulBuilder( |
| 88 | + builder: (BuildContext context, StateSetter setState) => Material( |
| 89 | + child: GFAccordion( |
| 90 | + titleChild: Row( |
| 91 | + children: [ |
| 92 | + const Text('title bar'), |
| 93 | + showButton |
| 94 | + ? const GFButton( |
| 95 | + onPressed: null, |
| 96 | + text: 'Select one', |
| 97 | + ) |
| 98 | + : Container(), |
| 99 | + ], |
| 100 | + ), |
| 101 | + contentChild: const Text('content body'), |
| 102 | + onToggleCollapsed: (value) { |
| 103 | + print('collapsed $value'); |
| 104 | + setState(() { |
| 105 | + showButton = value; |
| 106 | + }); |
| 107 | + }, |
| 108 | + ), |
| 109 | + ), |
| 110 | + ), |
| 111 | + ), |
| 112 | + ); |
| 113 | + |
| 114 | + await tester.pumpWidget(accordion); |
| 115 | + |
| 116 | + expect(find.text('title bar'), findsOneWidget); |
| 117 | + expect(find.text('content body'), findsNothing); |
| 118 | + expect(find.text('Select one'), findsNothing); |
| 119 | + await tester.tap(find.text('title bar')); |
| 120 | + await tester.pump(); |
| 121 | + expect(find.text('title bar'), findsOneWidget); |
| 122 | + expect(find.text('content body'), findsOneWidget); |
| 123 | + expect(find.text('Select one'), findsOneWidget); |
| 124 | + }); |
| 125 | + |
| 126 | + testWidgets('GFAccordion can be constructed with show accordion', |
| 127 | + (tester) async { |
| 128 | + const GFAccordion accordion = GFAccordion( |
| 129 | + titleChild: Text('title bar'), |
| 130 | + contentChild: Text('content body'), |
| 131 | + showAccordion: true, |
| 132 | + ); |
| 133 | + |
| 134 | + const TestApp app = TestApp(accordion); |
| 135 | + await tester.pumpWidget(app); |
| 136 | + |
| 137 | + expect(find.text('title bar'), findsOneWidget); |
| 138 | + expect(find.text('content body'), findsOneWidget); |
| 139 | + }); |
| 140 | + |
| 141 | + testWidgets('GFAccordion with all properties.', (WidgetTester tester) async { |
| 142 | + final GFAccordion accordion = GFAccordion( |
| 143 | + title: 'title', |
| 144 | + content: 'content', |
| 145 | + titleChild: const Text('title bar'), |
| 146 | + contentChild: const Text('content body'), |
| 147 | + textStyle: const TextStyle(color: Colors.deepOrange, fontSize: 16), |
| 148 | + collapsedIcon: const Icon(Icons.ac_unit), |
| 149 | + expandedIcon: const Icon(Icons.baby_changing_station), |
| 150 | + collapsedTitleBackgroundColor: Colors.amber, |
| 151 | + expandedTitleBackgroundColor: Colors.greenAccent, |
| 152 | + contentBackgroundColor: Colors.tealAccent, |
| 153 | + titlePadding: const EdgeInsets.all(13), |
| 154 | + contentPadding: const EdgeInsets.all(16), |
| 155 | + margin: const EdgeInsets.all(5), |
| 156 | + titleBorder: Border.all(color: Colors.blueAccent, width: 1), |
| 157 | + contentBorder: Border.all(color: Colors.deepPurpleAccent, width: 1), |
| 158 | + titleBorderRadius: const BorderRadius.all(Radius.circular(2)), |
| 159 | + contentBorderRadius: const BorderRadius.all(Radius.circular(6)), |
| 160 | + showAccordion: true, |
| 161 | + onToggleCollapsed: (value) { |
| 162 | + print('collapsed $value'); |
| 163 | + }, |
| 164 | + ); |
| 165 | + |
| 166 | + final TestApp app = TestApp(accordion); |
| 167 | + await tester.pumpWidget(app); |
| 168 | + |
| 169 | + expect(app.accordion.title, 'title'); |
| 170 | + expect(app.accordion.content, 'content'); |
| 171 | + expect(app.accordion.textStyle, |
| 172 | + const TextStyle(color: Colors.deepOrange, fontSize: 16)); |
| 173 | + expect(app.accordion.collapsedTitleBackgroundColor, Colors.amber); |
| 174 | + expect(app.accordion.expandedTitleBackgroundColor, Colors.greenAccent); |
| 175 | + expect(app.accordion.contentBackgroundColor, Colors.tealAccent); |
| 176 | + expect(app.accordion.titlePadding, const EdgeInsets.all(13)); |
| 177 | + expect(app.accordion.contentPadding, const EdgeInsets.all(16)); |
| 178 | + expect(app.accordion.margin, const EdgeInsets.all(5)); |
| 179 | + expect( |
| 180 | + app.accordion.titleBorder, |
| 181 | + Border.all(color: Colors.blueAccent, width: 1), |
| 182 | + ); |
| 183 | + expect(app.accordion.contentBorder, |
| 184 | + Border.all(color: Colors.deepPurpleAccent, width: 1)); |
| 185 | + expect(app.accordion.contentBorder, |
| 186 | + Border.all(color: Colors.deepPurpleAccent, width: 1)); |
| 187 | + expect(app.accordion.titleBorderRadius, |
| 188 | + const BorderRadius.all(Radius.circular(2))); |
| 189 | + expect(app.accordion.contentBorderRadius, |
| 190 | + const BorderRadius.all(Radius.circular(6))); |
| 191 | + expect(app.accordion.showAccordion, isTrue); |
| 192 | + }); |
| 193 | +} |
| 194 | + |
| 195 | +class TestApp extends StatefulWidget { |
| 196 | + const TestApp(this.accordion); |
| 197 | + final GFAccordion accordion; |
| 198 | + @override |
| 199 | + _TestAppState createState() => _TestAppState(); |
| 200 | +} |
| 201 | + |
| 202 | +class _TestAppState extends State<TestApp> { |
| 203 | + @override |
| 204 | + Widget build(BuildContext context) => MaterialApp( |
| 205 | + home: Scaffold( |
| 206 | + body: Column( |
| 207 | + children: [ |
| 208 | + widget.accordion, |
| 209 | + ], |
| 210 | + ), |
| 211 | + ), |
| 212 | + ); |
| 213 | +} |
0 commit comments