|
1 | | -// import 'package:flutter/material.dart'; |
2 | | -// import 'package:flutter_test/flutter_test.dart'; |
3 | | -// import 'package:getwidget/getwidget.dart'; |
4 | | - |
5 | | -// void main() { |
6 | | -// final Widget childWidget = Container( |
7 | | -// width: 11, |
8 | | -// height: 22, |
9 | | -// ); |
10 | | -// const duration = Duration(milliseconds: 1000); |
11 | | -// |
12 | | -// const List<String> imageList = [ |
13 | | -// 'https://cdn.pixabay.com/photo/2017/12/03/18/04/christmas-balls-2995437_960_720.jpg', |
14 | | -// 'https://cdn.pixabay.com/photo/2017/12/13/00/23/christmas-3015776_960_720.jpg', |
15 | | -// 'https://cdn.pixabay.com/photo/2019/12/19/10/55/christmas-market-4705877_960_720.jpg', |
16 | | -// 'https://cdn.pixabay.com/photo/2019/12/20/00/03/road-4707345_960_720.jpg', |
17 | | -// 'https://cdn.pixabay.com/photo/2019/12/22/04/18/x-mas-4711785__340.jpg', |
18 | | -// 'https://cdn.pixabay.com/photo/2016/11/22/07/09/spruce-1848543__340.jpg' |
19 | | -// ]; |
20 | | -// |
21 | | -// testWidgets('GF Carousel basic', (tester) async { |
22 | | -// const GFCarousel carousel = GFCarousel( |
23 | | -// autoPlayInterval: duration, |
24 | | -// ); |
25 | | -// await tester.pumpWidget(Container(child: childWidget)); |
26 | | -// expect(find.byWidget(childWidget), findsOneWidget); |
27 | | -// await tester.pump(duration); |
28 | | -// }); |
29 | | - |
30 | | -// testWidgets('GFCarousel Test', (tester) async { |
31 | | -// await provideMockedNetworkImages(() async { |
32 | | -// await tester.pumpWidget( |
33 | | -// MaterialApp( |
34 | | -// home: Material( |
35 | | -// child: GFItemsCarousel( |
36 | | -// rowCount: 3, |
37 | | -// children: imageList.map( |
38 | | -// (url) { |
39 | | -// return Container( |
40 | | -// // margin: EdgeInsets.all(5.0), |
41 | | -// child: ClipRRect( |
42 | | -// // borderRadius: BorderRadius.all(Radius.circular(5.0)), |
43 | | -// child: Image.network(url), |
44 | | -// ), |
45 | | -// ); |
46 | | -// }, |
47 | | -// ).toList(), |
48 | | -// ), |
49 | | -// ), |
50 | | -// ), |
51 | | -// ); |
52 | | -// // debugDefaultTargetPlatformOverride = null; |
53 | | -// }); |
54 | | -// }); |
55 | | - |
56 | | -// testWidgets('GF Carousel image test', (WidgetTester tester) async { |
57 | | -// await provideMockedNetworkImages(() async { |
58 | | -// /// Now we can pump NetworkImages without crashing our tests. Yay! |
59 | | -// await tester.pumpWidget( |
60 | | -// MaterialApp( |
61 | | -// home: Image.network( |
62 | | -// 'https://cdn.pixabay.com/photo/2017/12/03/18/04/christmas-balls-2995437_960_720.jpg'), |
63 | | -// ), |
64 | | -// ); |
65 | | -// |
66 | | -// /// No crashes. |
67 | | -// }); |
68 | | -// }); |
69 | | -// } |
70 | | -// |
71 | | -// class TestApp extends StatefulWidget { |
72 | | -// const TestApp(this.carousel); |
73 | | -// |
74 | | -// final GFCarousel carousel; |
75 | | -// |
76 | | -// @override |
77 | | -// _TestAppState createState() => _TestAppState(); |
78 | | -// } |
79 | | -// |
80 | | -// class _TestAppState extends State<TestApp> { |
81 | | -// @override |
82 | | -// Widget build(BuildContext context) => MaterialApp( |
83 | | -// home: Scaffold( |
84 | | -// body: Column( |
85 | | -// children: [ |
86 | | -// Expanded( |
87 | | -// child: widget.carousel, |
88 | | -// ) |
89 | | -// ], |
90 | | -// ), |
91 | | -// ), |
92 | | -// ); |
93 | | -// } |
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:getwidget/getwidget.dart'; |
| 4 | + |
| 5 | +class StateMarker extends StatefulWidget { |
| 6 | + const StateMarker({Key key, this.child}) : super(key: key); |
| 7 | + final Widget child; |
| 8 | + @override |
| 9 | + StateMarkerState createState() => StateMarkerState(); |
| 10 | +} |
| 11 | + |
| 12 | +class StateMarkerState extends State<StateMarker> { |
| 13 | + String marker; |
| 14 | + @override |
| 15 | + Widget build(BuildContext context) { |
| 16 | + if (widget.child != null) { |
| 17 | + return widget.child; |
| 18 | + } |
| 19 | + return Container(); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +void main() { |
| 24 | + final Key carouselKey = UniqueKey(); |
| 25 | + final List<String> textList = <String>[ |
| 26 | + 'AAAAAA', |
| 27 | + 'BBBBBB', |
| 28 | + 'CCCCCC', |
| 29 | + 'DDDDDD', |
| 30 | + 'EEEEEE' |
| 31 | + ]; |
| 32 | + final List<Widget> itemList = [ |
| 33 | + Text(textList[0]), |
| 34 | + Text(textList[1]), |
| 35 | + Text(textList[2]), |
| 36 | + Text(textList[3]), |
| 37 | + Text(textList[4]) |
| 38 | + ]; |
| 39 | + |
| 40 | + testWidgets('GFCarousel without items list', (tester) async { |
| 41 | + // `GFCarousel.items` null |
| 42 | + expect( |
| 43 | + () => GFCarousel( |
| 44 | + items: null, |
| 45 | + ), |
| 46 | + throwsAssertionError, |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + testWidgets('GFCarousel can be constructed', (tester) async { |
| 51 | + String value = textList[0]; |
| 52 | + int changedIndex; |
| 53 | + |
| 54 | + final GFCarousel carousel = GFCarousel( |
| 55 | + key: carouselKey, |
| 56 | + items: itemList.map((text) => StateMarker(child: text)).toList(), |
| 57 | + onPageChanged: (index) { |
| 58 | + changedIndex = index; |
| 59 | + print('inr $index $changedIndex'); |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + StateMarkerState findStateMarkerState(String name) => |
| 64 | + tester.state(find.widgetWithText(StateMarker, name)); |
| 65 | + |
| 66 | + final TestApp app = TestApp(carousel); |
| 67 | + await tester.pumpWidget(app); |
| 68 | + |
| 69 | + // find carousel by key |
| 70 | + expect(find.byKey(carouselKey), findsOneWidget); |
| 71 | + // find the 'AAAAAA' text in carousel |
| 72 | + expect(find.text('AAAAAA'), findsOneWidget); |
| 73 | + |
| 74 | + TestGesture gesture = |
| 75 | + await tester.startGesture(tester.getCenter(find.text('AAAAAA'))); |
| 76 | + await gesture.moveBy(const Offset(-600, 0)); |
| 77 | + await tester.pump(); |
| 78 | + expect(value, equals(textList[0])); |
| 79 | + findStateMarkerState(textList[1]).marker = 'marked'; |
| 80 | + await gesture.up(); |
| 81 | + await tester.pump(); |
| 82 | + await tester.pump(const Duration(seconds: 1)); |
| 83 | + value = textList[changedIndex]; |
| 84 | + expect(value, equals(textList[1])); |
| 85 | + await tester.pumpWidget(app); |
| 86 | + expect(findStateMarkerState(textList[1]).marker, equals('marked')); |
| 87 | + |
| 88 | + // slide on to the third slide. |
| 89 | + gesture = |
| 90 | + await tester.startGesture(tester.getCenter(find.text(textList[1]))); |
| 91 | + await gesture.moveBy(const Offset(-600, 0)); |
| 92 | + await gesture.up(); |
| 93 | + await tester.pump(); |
| 94 | + expect(findStateMarkerState(textList[1]).marker, equals('marked')); |
| 95 | + await tester.pump(const Duration(seconds: 1)); |
| 96 | + value = textList[changedIndex]; |
| 97 | + expect(value, equals(textList[2])); |
| 98 | + await tester.pumpWidget(app); |
| 99 | + expect(find.text(textList[2]), findsOneWidget); |
| 100 | + // slide back to the second slide. |
| 101 | + gesture = |
| 102 | + await tester.startGesture(tester.getCenter(find.text(textList[2]))); |
| 103 | + await gesture.moveBy(const Offset(600, 0)); |
| 104 | + await tester.pump(); |
| 105 | + final StateMarkerState markerState = findStateMarkerState(textList[1]); |
| 106 | + markerState.marker = 'marked'; |
| 107 | + await gesture.up(); |
| 108 | + await tester.pump(); |
| 109 | + await tester.pump(const Duration(seconds: 1)); |
| 110 | + value = textList[changedIndex]; |
| 111 | + expect(value, equals(textList[1])); |
| 112 | + await tester.pumpWidget(app); |
| 113 | + expect(findStateMarkerState(textList[1]).marker, equals('marked')); |
| 114 | + }); |
| 115 | + |
| 116 | + testWidgets('GFCarousel can be constructed with properties', (tester) async { |
| 117 | + final GFCarousel carousel = GFCarousel( |
| 118 | + key: carouselKey, |
| 119 | + initialPage: 1, |
| 120 | + items: itemList.map((text) => text).toList(), |
| 121 | + height: 333, |
| 122 | + autoPlay: true, |
| 123 | + enlargeMainPage: true, |
| 124 | + pagination: true, |
| 125 | + pagerSize: 16, |
| 126 | + activeIndicator: Colors.tealAccent, |
| 127 | + passiveIndicator: Colors.teal, |
| 128 | + viewportFraction: 1.0, |
| 129 | + aspectRatio: 2, |
| 130 | + autoPlayInterval: const Duration(seconds: 6), |
| 131 | + autoPlayAnimationDuration: const Duration(milliseconds: 1600), |
| 132 | + autoPlayCurve: Curves.fastLinearToSlowEaseIn, |
| 133 | + ); |
| 134 | + |
| 135 | + final TestApp app = TestApp(carousel); |
| 136 | + await tester.pumpWidget(app); |
| 137 | + |
| 138 | + expect(app.carousel.items, itemList); |
| 139 | + expect(app.carousel.initialPage, 1); |
| 140 | + expect(app.carousel.height, 333); |
| 141 | + expect(app.carousel.autoPlay, isTrue); |
| 142 | + expect(app.carousel.enlargeMainPage, isTrue); |
| 143 | + expect(app.carousel.reverse, isFalse); |
| 144 | + expect(app.carousel.enableInfiniteScroll, isTrue); |
| 145 | + expect(app.carousel.pagination, isTrue); |
| 146 | + expect(app.carousel.pagerSize, 16); |
| 147 | + expect(app.carousel.activeIndicator, Colors.tealAccent); |
| 148 | + expect(app.carousel.passiveIndicator, Colors.teal); |
| 149 | + expect(app.carousel.viewportFraction, 1); |
| 150 | + expect(app.carousel.aspectRatio, 2); |
| 151 | + expect(app.carousel.autoPlayInterval, const Duration(seconds: 6)); |
| 152 | + expect(app.carousel.autoPlayAnimationDuration, |
| 153 | + const Duration(milliseconds: 1600)); |
| 154 | + expect(app.carousel.autoPlayCurve, Curves.fastLinearToSlowEaseIn); |
| 155 | + }); |
| 156 | +} |
| 157 | + |
| 158 | +class TestApp extends StatefulWidget { |
| 159 | + const TestApp(this.carousel); |
| 160 | + |
| 161 | + final GFCarousel carousel; |
| 162 | + |
| 163 | + @override |
| 164 | + _TestAppState createState() => _TestAppState(); |
| 165 | +} |
| 166 | + |
| 167 | +class _TestAppState extends State<TestApp> { |
| 168 | + @override |
| 169 | + Widget build(BuildContext context) => MaterialApp( |
| 170 | + home: Scaffold( |
| 171 | + body: Column( |
| 172 | + children: [ |
| 173 | + widget.carousel, |
| 174 | + ], |
| 175 | + ), |
| 176 | + ), |
| 177 | + ); |
| 178 | +} |
0 commit comments