// Copyright 2017 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_ui/ui.dart'; import 'package:flutter_web/rendering.dart'; import 'package:flutter_web/services.dart'; import 'package:flutter_web_test/flutter_web_test.dart'; import 'package:flutter_web/material.dart'; import '../widgets/semantics_tester.dart'; void main() { setUp(() { debugResetSemanticsIdCounter(); }); testWidgets( 'Checkbox size is configurable by ThemeData.materialTapTargetSize', (WidgetTester tester) async { await tester.pumpWidget( Theme( data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.padded), child: Directionality( textDirection: TextDirection.ltr, child: Material( child: Center( child: Checkbox( value: true, onChanged: (bool newValue) {}, ), ), ), ), ), ); expect(tester.getSize(find.byType(Checkbox)), const Size(48.0, 48.0)); await tester.pumpWidget( Theme( data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.shrinkWrap), child: Directionality( textDirection: TextDirection.ltr, child: Material( child: Center( child: Checkbox( value: true, onChanged: (bool newValue) {}, ), ), ), ), ), ); expect(tester.getSize(find.byType(Checkbox)), const Size(40.0, 40.0)); }); testWidgets('CheckBox semantics', (WidgetTester tester) async { final SemanticsHandle handle = tester.ensureSemantics(); await tester.pumpWidget(Material( child: Checkbox( value: false, onChanged: (bool b) {}, ), )); expect( tester.getSemantics(find.byType(Checkbox)), matchesSemantics( hasCheckedState: true, hasEnabledState: true, isEnabled: true, hasTapAction: true, )); await tester.pumpWidget(Material( child: Checkbox( value: true, onChanged: (bool b) {}, ), )); expect( tester.getSemantics(find.byType(Checkbox)), matchesSemantics( hasCheckedState: true, hasEnabledState: true, isChecked: true, isEnabled: true, hasTapAction: true, )); await tester.pumpWidget(const Material( child: Checkbox( value: false, onChanged: null, ), )); expect( tester.getSemantics(find.byType(Checkbox)), matchesSemantics( hasCheckedState: true, hasEnabledState: true, )); await tester.pumpWidget(const Material( child: Checkbox( value: true, onChanged: null, ), )); expect( tester.getSemantics(find.byType(Checkbox)), matchesSemantics( hasCheckedState: true, hasEnabledState: true, isChecked: true, )); handle.dispose(); }); testWidgets('Can wrap CheckBox with Semantics', (WidgetTester tester) async { final SemanticsHandle handle = tester.ensureSemantics(); await tester.pumpWidget(Material( child: Semantics( label: 'foo', textDirection: TextDirection.ltr, child: Checkbox( value: false, onChanged: (bool b) {}, ), ), )); expect( tester.getSemantics(find.byType(Checkbox)), matchesSemantics( label: 'foo', textDirection: TextDirection.ltr, hasCheckedState: true, hasEnabledState: true, isEnabled: true, hasTapAction: true, )); handle.dispose(); }); testWidgets('CheckBox tristate: true', (WidgetTester tester) async { bool checkBoxValue; await tester.pumpWidget( Material( child: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return Checkbox( tristate: true, value: checkBoxValue, onChanged: (bool value) { setState(() { checkBoxValue = value; }); }, ); }, ), ), ); expect(tester.widget(find.byType(Checkbox)).value, null); await tester.tap(find.byType(Checkbox)); await tester.pumpAndSettle(); expect(checkBoxValue, false); await tester.tap(find.byType(Checkbox)); await tester.pumpAndSettle(); expect(checkBoxValue, true); await tester.tap(find.byType(Checkbox)); await tester.pumpAndSettle(); expect(checkBoxValue, null); checkBoxValue = true; await tester.pumpAndSettle(); expect(checkBoxValue, true); checkBoxValue = null; await tester.pumpAndSettle(); expect(checkBoxValue, null); }); testWidgets('has semantics for tristate', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester); await tester.pumpWidget( Material( child: Checkbox( tristate: true, value: null, onChanged: (bool newValue) {}, ), ), ); expect( semantics.nodesWith( flags: [ SemanticsFlag.hasCheckedState, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled ], actions: [SemanticsAction.tap], ), hasLength(1)); await tester.pumpWidget( Material( child: Checkbox( tristate: true, value: true, onChanged: (bool newValue) {}, ), ), ); expect( semantics.nodesWith( flags: [ SemanticsFlag.hasCheckedState, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, SemanticsFlag.isChecked, ], actions: [SemanticsAction.tap], ), hasLength(1)); await tester.pumpWidget( Material( child: Checkbox( tristate: true, value: false, onChanged: (bool newValue) {}, ), ), ); expect( semantics.nodesWith( flags: [ SemanticsFlag.hasCheckedState, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, ], actions: [SemanticsAction.tap], ), hasLength(1)); semantics.dispose(); }); testWidgets('has semantic events', (WidgetTester tester) async { dynamic semanticEvent; bool checkboxValue = false; SystemChannels.accessibility.setMockMessageHandler((dynamic message) async { semanticEvent = message; }); final SemanticsTester semanticsTester = SemanticsTester(tester); await tester.pumpWidget( Material( child: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return Checkbox( value: checkboxValue, onChanged: (bool value) { setState(() { checkboxValue = value; }); }, ); }, ), ), ); await tester.tap(find.byType(Checkbox)); final RenderObject object = tester.firstRenderObject(find.byType(Checkbox)); expect(checkboxValue, true); expect(semanticEvent, { 'type': 'tap', 'nodeId': object.debugSemantics.id, 'data': {}, }); expect( object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.tap), true); SystemChannels.accessibility.setMockMessageHandler(null); semanticsTester.dispose(); }); }