// Copyright 2018 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'package:flutter_web/widgets.dart'; import 'package:flutter_web_test/flutter_web_test.dart'; import '../rendering/mock_canvas.dart'; // TODO(flutter_web): Semantics. class TestState extends StatefulWidget { const TestState({Key key, this.child, this.log}) : super(key: key); final Widget child; final List log; @override State createState() => _TestStateState(); } class _TestStateState extends State { @override void initState() { super.initState(); widget.log.add('created new state'); } @override Widget build(BuildContext context) { return widget.child; } } void main() { testWidgets('Visibility', (WidgetTester tester) async { final List log = []; final Widget testChild = GestureDetector( onTap: () { log.add('tap'); }, child: Builder( builder: (BuildContext context) { final bool animating = TickerMode.of(context); return TestState( log: log, child: Text('a $animating', textDirection: TextDirection.rtl), ); }, ), ); // We now run a sequence of pumpWidget calls one after the other. In // addition to verifying that the right behaviour is seen in each case, this // also verifies that the widget can dynamically change from state to state. await tester.pumpWidget(Visibility(child: testChild)); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Visibility), paints..paragraph()); expect(tester.getSize(find.byType(Visibility)), const Size(800.0, 600.0)); expect(log, ['created new state']); await tester.tap(find.byType(Visibility)); expect(log, ['created new state', 'tap']); log.clear(); await tester.pumpWidget(Visibility(child: testChild, visible: false)); expect(find.byType(Text, skipOffstage: false), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), const Size(800.0, 600.0)); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, []); log.clear(); await tester.pumpWidget( Center(child: Visibility(child: testChild, visible: false))); expect(find.byType(Text, skipOffstage: false), findsNothing); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), Size.zero); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, []); log.clear(); await tester.pumpWidget(Center( child: Visibility( child: testChild, replacement: const Placeholder(), visible: false))); expect(find.byType(Text, skipOffstage: false), findsNothing); expect(find.byType(Placeholder), findsOneWidget); expect(find.byType(Visibility), paints..path()); expect(tester.getSize(find.byType(Visibility)), const Size(800.0, 600.0)); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, []); log.clear(); await tester.pumpWidget(Center( child: Visibility( child: testChild, replacement: const Placeholder(), visible: true))); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paints..paragraph()); expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); expect(log, ['created new state']); await tester.tap(find.byType(Visibility)); expect(log, ['created new state', 'tap']); log.clear(); await tester.pumpWidget(Center( child: Visibility( child: testChild, visible: true, maintainState: true, maintainAnimation: true, maintainSize: true, maintainInteractivity: true, maintainSemantics: true))); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paints..paragraph()); expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); expect(log, ['created new state']); await tester.tap(find.byType(Visibility)); expect(log, ['created new state', 'tap']); log.clear(); await tester.pumpWidget(Center( child: Visibility( child: testChild, visible: false, maintainState: true, maintainAnimation: true, maintainSize: true, maintainInteractivity: true, maintainSemantics: true))); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, ['tap']); log.clear(); await tester.pumpWidget(Center( child: Visibility( child: testChild, visible: false, maintainState: true, maintainAnimation: true, maintainSize: true, maintainInteractivity: true))); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, ['tap']); log.clear(); await tester.pumpWidget(Center( child: Visibility( child: testChild, visible: false, maintainState: true, maintainAnimation: true, maintainSize: true, maintainSemantics: true))); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); expect(log, ['created new state']); await tester.tap(find.byType(Visibility)); expect(log, ['created new state']); log.clear(); await tester.pumpWidget(Center( child: Visibility( child: testChild, visible: false, maintainState: true, maintainAnimation: true, maintainSize: true))); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, []); log.clear(); await tester.pumpWidget(Center( child: Visibility( child: testChild, visible: false, maintainState: true, maintainAnimation: true))); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.byType(Text, skipOffstage: true), findsNothing); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), Size.zero); expect(log, ['created new state']); await tester.tap(find.byType(Visibility)); expect(log, ['created new state']); log.clear(); await tester.pumpWidget(Center( child: Visibility(child: testChild, visible: false, maintainState: true))); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.byType(Text, skipOffstage: true), findsNothing); expect(find.text('a false', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), Size.zero); expect(log, ['created new state']); await tester.tap(find.byType(Visibility)); expect(log, ['created new state']); log.clear(); // Now we toggle the visibility off and on a few times to make sure that works. await tester.pumpWidget(Center( child: Visibility(child: testChild, visible: true, maintainState: true))); expect(find.byType(Text), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paints..paragraph()); expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, ['tap']); log.clear(); await tester.pumpWidget(Center( child: Visibility(child: testChild, visible: false, maintainState: true))); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.byType(Text, skipOffstage: true), findsNothing); expect(find.text('a false', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), Size.zero); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, []); log.clear(); await tester.pumpWidget(Center( child: Visibility(child: testChild, visible: true, maintainState: true))); expect(find.byType(Text), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paints..paragraph()); expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, ['tap']); log.clear(); await tester.pumpWidget(Center( child: Visibility(child: testChild, visible: false, maintainState: true))); expect(find.byType(Text, skipOffstage: false), findsOneWidget); expect(find.byType(Text, skipOffstage: true), findsNothing); expect(find.text('a false', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), Size.zero); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, []); log.clear(); // Same but without maintainState. await tester.pumpWidget( Center(child: Visibility(child: testChild, visible: false))); expect(find.byType(Text, skipOffstage: false), findsNothing); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), Size.zero); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, []); log.clear(); await tester .pumpWidget(Center(child: Visibility(child: testChild, visible: true))); expect(find.byType(Text), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paints..paragraph()); expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); expect(log, ['created new state']); await tester.tap(find.byType(Visibility)); expect(log, ['created new state', 'tap']); log.clear(); await tester.pumpWidget( Center(child: Visibility(child: testChild, visible: false))); expect(find.byType(Text, skipOffstage: false), findsNothing); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paintsNothing); expect(tester.getSize(find.byType(Visibility)), Size.zero); expect(log, []); await tester.tap(find.byType(Visibility)); expect(log, []); log.clear(); await tester .pumpWidget(Center(child: Visibility(child: testChild, visible: true))); expect(find.byType(Text), findsOneWidget); expect(find.text('a true', skipOffstage: false), findsOneWidget); expect(find.byType(Placeholder), findsNothing); expect(find.byType(Visibility), paints..paragraph()); expect(tester.getSize(find.byType(Visibility)), const Size(84.0, 14.0)); expect(log, ['created new state']); await tester.tap(find.byType(Visibility)); expect(log, ['created new state', 'tap']); log.clear(); }); }