|
| 1 | +// Copyright 2013 The Flutter Authors |
| 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_test/flutter_test.dart'; |
| 7 | + |
| 8 | +class _MockOnEndFunction { |
| 9 | + int called = 0; |
| 10 | + |
| 11 | + void handler() { |
| 12 | + called++; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +const Duration _animationDuration = Duration(milliseconds: 1000); |
| 17 | +const Duration _additionalDelay = Duration(milliseconds: 1); |
| 18 | + |
| 19 | +void main() { |
| 20 | + late _MockOnEndFunction mockOnEndFunction; |
| 21 | + const switchKey = Key('switchKey'); |
| 22 | + |
| 23 | + setUp(() { |
| 24 | + mockOnEndFunction = _MockOnEndFunction(); |
| 25 | + }); |
| 26 | + |
| 27 | + testWidgets('AnimatedTheme onEnd callback test', (WidgetTester tester) async { |
| 28 | + await tester.pumpWidget( |
| 29 | + Directionality( |
| 30 | + textDirection: TextDirection.ltr, |
| 31 | + child: Center( |
| 32 | + child: _TestAnimatedThemeWidget( |
| 33 | + callback: mockOnEndFunction.handler, |
| 34 | + switchKey: switchKey, |
| 35 | + ), |
| 36 | + ), |
| 37 | + ), |
| 38 | + ); |
| 39 | + |
| 40 | + final Finder widgetFinder = find.byKey(switchKey); |
| 41 | + |
| 42 | + await tester.tap(widgetFinder); |
| 43 | + |
| 44 | + await tester.pump(); |
| 45 | + expect(mockOnEndFunction.called, 0); |
| 46 | + await tester.pump(_animationDuration); |
| 47 | + expect(mockOnEndFunction.called, 0); |
| 48 | + await tester.pump(_additionalDelay); |
| 49 | + expect(mockOnEndFunction.called, 1); |
| 50 | + |
| 51 | + await tester.tap(widgetFinder); |
| 52 | + |
| 53 | + await tester.pump(); |
| 54 | + await tester.pump(_animationDuration + _additionalDelay); |
| 55 | + expect(mockOnEndFunction.called, 2); |
| 56 | + |
| 57 | + await tester.tap(widgetFinder); |
| 58 | + |
| 59 | + await tester.pump(); |
| 60 | + await tester.pump(_animationDuration + _additionalDelay); |
| 61 | + expect(mockOnEndFunction.called, 3); |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +class _TestAnimatedThemeWidget extends StatefulWidget { |
| 66 | + const _TestAnimatedThemeWidget({this.callback, required this.switchKey}); |
| 67 | + |
| 68 | + final VoidCallback? callback; |
| 69 | + final Key switchKey; |
| 70 | + |
| 71 | + @override |
| 72 | + State<_TestAnimatedThemeWidget> createState() => _TestAnimatedThemeWidgetState(); |
| 73 | +} |
| 74 | + |
| 75 | +class _TestAnimatedThemeWidgetState extends State<_TestAnimatedThemeWidget> { |
| 76 | + bool toggle = false; |
| 77 | + |
| 78 | + @override |
| 79 | + Widget build(BuildContext context) { |
| 80 | + return Stack( |
| 81 | + children: <Widget>[ |
| 82 | + AnimatedTheme( |
| 83 | + data: toggle ? ThemeData.dark() : ThemeData(), |
| 84 | + duration: _animationDuration, |
| 85 | + onEnd: widget.callback, |
| 86 | + child: const Placeholder(), |
| 87 | + ), |
| 88 | + GestureDetector( |
| 89 | + key: widget.switchKey, |
| 90 | + behavior: HitTestBehavior.opaque, |
| 91 | + onTap: () { |
| 92 | + setState(() { |
| 93 | + toggle = !toggle; |
| 94 | + }); |
| 95 | + }, |
| 96 | + child: const SizedBox(width: 48.0, height: 48.0), |
| 97 | + ), |
| 98 | + ], |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
0 commit comments