Skip to content

Commit

Permalink
Adjust and refactor all RaisedButton tests into its respective file (f…
Browse files Browse the repository at this point in the history
  • Loading branch information
Inconnu08 authored and Shi-Hao Hong committed Nov 5, 2019
1 parent e5af60c commit 4bf2e55
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 217 deletions.
221 changes: 4 additions & 217 deletions packages/flutter/test/material/buttons_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,90 +9,12 @@ import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

import '../rendering/mock_canvas.dart';
import '../widgets/semantics_tester.dart';

void main() {
setUp(() {
debugResetSemanticsIdCounter();
});

testWidgets('RaisedButton defaults', (WidgetTester tester) async {
final Finder rawButtonMaterial = find.descendant(
of: find.byType(RaisedButton),
matching: find.byType(Material),
);

// Enabled RaisedButton
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: RaisedButton(
onPressed: () { },
child: const Text('button'),
),
),
);
Material material = tester.widget<Material>(rawButtonMaterial);
expect(material.animationDuration, const Duration(milliseconds: 200));
expect(material.borderOnForeground, true);
expect(material.borderRadius, null);
expect(material.clipBehavior, Clip.none);
expect(material.color, const Color(0xffe0e0e0));
expect(material.elevation, 2.0);
expect(material.shadowColor, const Color(0xff000000));
expect(material.shape, RoundedRectangleBorder(borderRadius: BorderRadius.circular(2.0)));
expect(material.textStyle.color, const Color(0xdd000000));
expect(material.textStyle.fontFamily, 'Roboto');
expect(material.textStyle.fontSize, 14);
expect(material.textStyle.fontWeight, FontWeight.w500);
expect(material.type, MaterialType.button);

final Offset center = tester.getCenter(find.byType(RaisedButton));
await tester.startGesture(center);
await tester.pumpAndSettle();

// Only elevation changes when enabled and pressed.
material = tester.widget<Material>(rawButtonMaterial);
expect(material.animationDuration, const Duration(milliseconds: 200));
expect(material.borderOnForeground, true);
expect(material.borderRadius, null);
expect(material.clipBehavior, Clip.none);
expect(material.color, const Color(0xffe0e0e0));
expect(material.elevation, 8.0);
expect(material.shadowColor, const Color(0xff000000));
expect(material.shape, RoundedRectangleBorder(borderRadius: BorderRadius.circular(2.0)));
expect(material.textStyle.color, const Color(0xdd000000));
expect(material.textStyle.fontFamily, 'Roboto');
expect(material.textStyle.fontSize, 14);
expect(material.textStyle.fontWeight, FontWeight.w500);
expect(material.type, MaterialType.button);

// Disabled RaisedButton
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: RaisedButton(
onPressed: null,
child: Text('button'),
),
),
);
material = tester.widget<Material>(rawButtonMaterial);
expect(material.animationDuration, const Duration(milliseconds: 200));
expect(material.borderOnForeground, true);
expect(material.borderRadius, null);
expect(material.clipBehavior, Clip.none);
expect(material.color, const Color(0x61000000));
expect(material.elevation, 0.0);
expect(material.shadowColor, const Color(0xff000000));
expect(material.shape, RoundedRectangleBorder(borderRadius: BorderRadius.circular(2.0)));
expect(material.textStyle.color, const Color(0x61000000));
expect(material.textStyle.fontFamily, 'Roboto');
expect(material.textStyle.fontSize, 14);
expect(material.textStyle.fontWeight, FontWeight.w500);
expect(material.type, MaterialType.button);
});

testWidgets('OutlineButton defaults', (WidgetTester tester) async {
final Finder rawButtonMaterial = find.descendant(
of: find.byType(OutlineButton),
Expand Down Expand Up @@ -185,22 +107,7 @@ void main() {
await gesture.addPointer();
await gesture.moveTo(tester.getCenter(find.byType(OutlineButton)));
await tester.pumpAndSettle();
RenderObject inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
expect(inkFeatures, paints..rect(color: hoverColor));

await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: RaisedButton(
hoverColor: hoverColor,
onPressed: () { },
child: const Text('button'),
),
),
);

await tester.pumpAndSettle();
inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
final RenderObject inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
expect(inkFeatures, paints..rect(color: hoverColor));

gesture.removePointer();
Expand All @@ -209,11 +116,11 @@ void main() {
testWidgets('Do buttons work with focus', (WidgetTester tester) async {
const Color focusColor = Color(0xff001122);

FocusNode focusNode = FocusNode(debugLabel: 'RaisedButton Node');
final FocusNode focusNode = FocusNode(debugLabel: 'OutlineButton Node');
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: RaisedButton(
child: OutlineButton(
focusColor: focusColor,
focusNode: focusNode,
onPressed: () { },
Expand All @@ -226,127 +133,7 @@ void main() {
focusNode.requestFocus();
await tester.pumpAndSettle();

RenderObject inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
expect(inkFeatures, paints..rect(color: focusColor));

focusNode = FocusNode(debugLabel: 'OutlineButton Node');
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: OutlineButton(
focusColor: focusColor,
focusNode: focusNode,
onPressed: () { },
child: const Text('button'),
),
),
);
focusNode.requestFocus();
await tester.pumpAndSettle();
inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
final RenderObject inkFeatures = tester.allRenderObjects.firstWhere((RenderObject object) => object.runtimeType.toString() == '_RenderInkFeatures');
expect(inkFeatures, paints..rect(color: focusColor));
});

testWidgets('Does RaisedButton contribute semantics', (WidgetTester tester) async {
final SemanticsTester semantics = SemanticsTester(tester);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Material(
child: Center(
child: RaisedButton(
onPressed: () { },
child: const Text('ABC'),
),
),
),
),
);

expect(semantics, hasSemantics(
TestSemantics.root(
children: <TestSemantics>[
TestSemantics.rootChild(
actions: <SemanticsAction>[
SemanticsAction.tap,
],
label: 'ABC',
rect: const Rect.fromLTRB(0.0, 0.0, 88.0, 48.0),
transform: Matrix4.translationValues(356.0, 276.0, 0.0),
flags: <SemanticsFlag>[
SemanticsFlag.hasEnabledState,
SemanticsFlag.isButton,
SemanticsFlag.isEnabled,
SemanticsFlag.isFocusable,
],
),
],
),
ignoreId: true,
));

semantics.dispose();
});

testWidgets('RaisedButton size is configurable by ThemeData.materialTapTargetSize', (WidgetTester tester) async {
final Key key1 = UniqueKey();
await tester.pumpWidget(
Theme(
data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.padded),
child: Directionality(
textDirection: TextDirection.ltr,
child: Material(
child: Center(
child: RaisedButton(
key: key1,
child: const SizedBox(width: 50.0, height: 8.0),
onPressed: () { },
),
),
),
),
),
);

expect(tester.getSize(find.byKey(key1)), const Size(88.0, 48.0));

final Key key2 = UniqueKey();
await tester.pumpWidget(
Theme(
data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.shrinkWrap),
child: Directionality(
textDirection: TextDirection.ltr,
child: Material(
child: Center(
child: RaisedButton(
key: key2,
child: const SizedBox(width: 50.0, height: 8.0),
onPressed: () { },
),
),
),
),
),
);

expect(tester.getSize(find.byKey(key2)), const Size(88.0, 36.0));
});

testWidgets('RaisedButton has no clip by default', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Material(
child: RaisedButton(
onPressed: () { /* to make sure the button is enabled */ },
),
),
),
);

expect(
tester.renderObject(find.byType(RaisedButton)),
paintsExactlyCountTimes(#clipPath, 0),
);
});
}
Loading

0 comments on commit 4bf2e55

Please sign in to comment.