|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter/widgets.dart'; |
| 7 | +import 'package:flutter_adaptive_scaffold/src/breakpoints.dart'; |
| 8 | +import 'package:flutter_adaptive_scaffold/src/slot_layout.dart'; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + testWidgets( |
| 13 | + 'SlotLayout displays correct widget based on screen width', |
| 14 | + (WidgetTester tester) async { |
| 15 | + MediaQuery slot(double width) { |
| 16 | + return MediaQuery( |
| 17 | + data: MediaQueryData(size: Size(width, 2000)), |
| 18 | + child: Directionality( |
| 19 | + textDirection: TextDirection.ltr, |
| 20 | + child: SlotLayout( |
| 21 | + config: <Breakpoint, SlotLayoutConfig>{ |
| 22 | + Breakpoints.smallAndUp: SlotLayout.from( |
| 23 | + key: const Key('0'), builder: (_) => const Text('Small')), |
| 24 | + Breakpoints.mediumAndUp: SlotLayout.from( |
| 25 | + key: const Key('400'), |
| 26 | + builder: (_) => const Text('Medium')), |
| 27 | + Breakpoints.largeAndUp: SlotLayout.from( |
| 28 | + key: const Key('800'), builder: (_) => const Text('Large')), |
| 29 | + }, |
| 30 | + ), |
| 31 | + ), |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + await tester.pumpWidget(slot(300)); |
| 36 | + expect(find.text('Small'), findsOneWidget); |
| 37 | + expect(find.text('Medium'), findsNothing); |
| 38 | + expect(find.text('Large'), findsNothing); |
| 39 | + |
| 40 | + await tester.pumpWidget(slot(600)); |
| 41 | + expect(find.text('Small'), findsNothing); |
| 42 | + expect(find.text('Medium'), findsOneWidget); |
| 43 | + expect(find.text('Large'), findsNothing); |
| 44 | + |
| 45 | + await tester.pumpWidget(slot(1200)); |
| 46 | + expect(find.text('Small'), findsNothing); |
| 47 | + expect(find.text('Medium'), findsNothing); |
| 48 | + expect(find.text('Large'), findsOneWidget); |
| 49 | + }, |
| 50 | + ); |
| 51 | + |
| 52 | + testWidgets( |
| 53 | + 'SlotLayout handles null configurations gracefully', |
| 54 | + (WidgetTester tester) async { |
| 55 | + await tester.pumpWidget( |
| 56 | + MediaQuery( |
| 57 | + data: MediaQueryData.fromView(tester.view) |
| 58 | + .copyWith(size: const Size(500, 2000)), |
| 59 | + child: Directionality( |
| 60 | + textDirection: TextDirection.ltr, |
| 61 | + child: SlotLayout( |
| 62 | + config: <Breakpoint, SlotLayoutConfig?>{ |
| 63 | + Breakpoints.smallAndUp: SlotLayout.from( |
| 64 | + key: const Key('0'), |
| 65 | + builder: (BuildContext context) => Container(), |
| 66 | + ), |
| 67 | + Breakpoints.mediumAndUp: null, |
| 68 | + Breakpoints.largeAndUp: SlotLayout.from( |
| 69 | + key: const Key('800'), |
| 70 | + builder: (BuildContext context) => Container(), |
| 71 | + ), |
| 72 | + }, |
| 73 | + ), |
| 74 | + ), |
| 75 | + ), |
| 76 | + ); |
| 77 | + |
| 78 | + expect(find.byKey(const Key('0')), findsOneWidget); |
| 79 | + expect(find.byKey(const Key('400')), findsNothing); |
| 80 | + expect(find.byKey(const Key('800')), findsNothing); |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + testWidgets( |
| 85 | + 'SlotLayout builder generates widgets correctly', |
| 86 | + (WidgetTester tester) async { |
| 87 | + await tester.pumpWidget( |
| 88 | + MediaQuery( |
| 89 | + data: const MediaQueryData(size: Size(600, 2000)), |
| 90 | + child: Directionality( |
| 91 | + textDirection: TextDirection.ltr, |
| 92 | + child: SlotLayout( |
| 93 | + config: <Breakpoint, SlotLayoutConfig>{ |
| 94 | + Breakpoints.mediumAndUp: SlotLayout.from( |
| 95 | + key: const Key('0'), |
| 96 | + builder: (_) => const Text('Builder Test')), |
| 97 | + }, |
| 98 | + ), |
| 99 | + ), |
| 100 | + ), |
| 101 | + ); |
| 102 | + |
| 103 | + expect(find.text('Builder Test'), findsOneWidget); |
| 104 | + }, |
| 105 | + ); |
| 106 | + |
| 107 | + testWidgets( |
| 108 | + 'SlotLayout applies inAnimation and outAnimation correctly when changing breakpoints', |
| 109 | + (WidgetTester tester) async { |
| 110 | + // Define a SlotLayout with custom animations. |
| 111 | + Widget buildSlotLayout(double width) { |
| 112 | + return MediaQuery( |
| 113 | + data: MediaQueryData(size: Size(width, 2000)), |
| 114 | + child: Directionality( |
| 115 | + textDirection: TextDirection.ltr, |
| 116 | + child: SlotLayout( |
| 117 | + config: <Breakpoint, SlotLayoutConfig>{ |
| 118 | + Breakpoints.smallAndUp: SlotLayout.from( |
| 119 | + key: const Key('small'), |
| 120 | + builder: (_) => const SizedBox( |
| 121 | + key: Key('smallBox'), width: 100, height: 100), |
| 122 | + inAnimation: (Widget widget, Animation<double> animation) => |
| 123 | + ScaleTransition( |
| 124 | + scale: animation, |
| 125 | + child: widget, |
| 126 | + ), |
| 127 | + outAnimation: (Widget widget, Animation<double> animation) => |
| 128 | + FadeTransition( |
| 129 | + opacity: animation, |
| 130 | + child: widget, |
| 131 | + ), |
| 132 | + inDuration: const Duration(seconds: 1), |
| 133 | + outDuration: const Duration(seconds: 2), |
| 134 | + inCurve: Curves.easeIn, |
| 135 | + outCurve: Curves.easeOut, |
| 136 | + ), |
| 137 | + Breakpoints.mediumAndUp: SlotLayout.from( |
| 138 | + key: const Key('medium'), |
| 139 | + builder: (_) => const SizedBox( |
| 140 | + key: Key('mediumBox'), width: 200, height: 200), |
| 141 | + inAnimation: (Widget widget, Animation<double> animation) => |
| 142 | + ScaleTransition( |
| 143 | + scale: animation, |
| 144 | + child: widget, |
| 145 | + ), |
| 146 | + outAnimation: (Widget widget, Animation<double> animation) => |
| 147 | + FadeTransition( |
| 148 | + opacity: animation, |
| 149 | + child: widget, |
| 150 | + ), |
| 151 | + inDuration: const Duration(seconds: 1), |
| 152 | + outDuration: const Duration(seconds: 2), |
| 153 | + inCurve: Curves.easeIn, |
| 154 | + outCurve: Curves.easeOut, |
| 155 | + ), |
| 156 | + }, |
| 157 | + ), |
| 158 | + ), |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + // Pump the widget with the SlotLayout at small breakpoint. |
| 163 | + await tester.pumpWidget(buildSlotLayout(300)); |
| 164 | + expect(find.byKey(const Key('smallBox')), findsOneWidget); |
| 165 | + expect(find.byKey(const Key('mediumBox')), findsNothing); |
| 166 | + |
| 167 | + // Change to medium breakpoint to trigger outAnimation for small and inAnimation for medium. |
| 168 | + await tester.pumpWidget(buildSlotLayout(600)); |
| 169 | + await tester.pump(); // Start the animation. |
| 170 | + await tester.pump(const Duration( |
| 171 | + milliseconds: 1000)); // Halfway through the outDuration. |
| 172 | + |
| 173 | + // Verify that the outAnimation is in progress for smallBox. |
| 174 | + final FadeTransition fadeTransitionMid = |
| 175 | + tester.widget(find.byType(FadeTransition)); |
| 176 | + expect(fadeTransitionMid.opacity.value, lessThan(1.0)); |
| 177 | + expect(fadeTransitionMid.opacity.value, greaterThan(0.0)); |
| 178 | + |
| 179 | + // Complete the animation. |
| 180 | + await tester.pumpAndSettle(); |
| 181 | + expect(find.byKey(const Key('smallBox')), findsNothing); |
| 182 | + expect(find.byKey(const Key('mediumBox')), findsOneWidget); |
| 183 | + }, |
| 184 | + ); |
| 185 | +} |
0 commit comments