buttons_test.dart 10.9 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
import 'dart:ui' show SemanticsFlag;
6

7 8 9 10 11 12 13 14 15
import 'package:flutter/rendering.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

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

void main() {
16 17 18 19
  setUp(() {
    debugResetSemanticsIdCounter();
  });

20 21 22
  testWidgets('Does FlatButton contribute semantics', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
23 24 25 26 27 28 29 30 31 32 33
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Material(
          child: new Center(
            child: new FlatButton(
              onPressed: () { },
              child: const Text('ABC')
            ),
          ),
        ),
      ),
34 35 36
    );

    expect(semantics, hasSemantics(
37
      new TestSemantics.root(
38
        children: <TestSemantics>[
39
          new TestSemantics.rootChild(
40 41 42
            actions: <SemanticsAction>[
              SemanticsAction.tap,
            ],
43 44
            label: 'ABC',
            rect: new Rect.fromLTRB(0.0, 0.0, 88.0, 36.0),
45
            transform: new Matrix4.translationValues(356.0, 282.0, 0.0),
46 47 48 49 50
            flags: <SemanticsFlag>[
              SemanticsFlag.isButton,
              SemanticsFlag.hasEnabledState,
              SemanticsFlag.isEnabled,
            ],
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
          )
        ],
      ),
      ignoreId: true,
    ));

    semantics.dispose();
  });

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

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
80 81 82
            actions: <SemanticsAction>[
              SemanticsAction.tap,
            ],
83 84 85
            label: 'ABC',
            rect: new Rect.fromLTRB(0.0, 0.0, 88.0, 36.0),
            transform: new Matrix4.translationValues(356.0, 282.0, 0.0),
86 87 88 89 90
            flags: <SemanticsFlag>[
              SemanticsFlag.isButton,
              SemanticsFlag.hasEnabledState,
              SemanticsFlag.isEnabled,
            ],
91 92
          )
        ]
93 94
      ),
      ignoreId: true,
95 96 97 98 99
    ));

    semantics.dispose();
  });

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  testWidgets('Does FlatButton scale with font scale changes', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Material(
          child: new MediaQuery(
            data: const MediaQueryData(textScaleFactor: 1.0),
            child: new Center(
              child: new FlatButton(
                onPressed: () { },
                child: const Text('ABC'),
              ),
            ),
          ),
        ),
      ),
    );

    expect(tester.getSize(find.byType(FlatButton)), equals(const Size(88.0, 36.0)));
119
    expect(tester.getSize(find.byType(Text)), equals(const Size(42.0, 14.0)));
120

121
    // textScaleFactor expands text, but not button.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Material(
          child: new MediaQuery(
            data: const MediaQueryData(textScaleFactor: 1.3),
            child: new Center(
              child: new FlatButton(
                onPressed: () { },
                child: const Text('ABC'),
              ),
            ),
          ),
        ),
      ),
    );

    expect(tester.getSize(find.byType(FlatButton)), equals(const Size(88.0, 36.0)));
140 141 142
    // Scaled text rendering is different on Linux and Mac by one pixel.
    // TODO(#12357): Update this test when text rendering is fixed.
    expect(tester.getSize(find.byType(Text)).width, isIn(<double>[54.0, 55.0]));
143
    expect(tester.getSize(find.byType(Text)).height, isIn(<double>[18.0, 19.0]));
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169


    // Set text scale large enough to expand text and button.
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Material(
          child: new MediaQuery(
            data: const MediaQueryData(textScaleFactor: 3.0),
            child: new Center(
              child: new FlatButton(
                onPressed: () { },
                child: const Text('ABC'),
              ),
            ),
          ),
        ),
      ),
    );

    // Scaled text rendering is different on Linux and Mac by one pixel.
    // TODO(#12357): Update this test when text rendering is fixed.
    expect(tester.getSize(find.byType(FlatButton)).width, isIn(<double>[158.0, 159.0]));
    expect(tester.getSize(find.byType(FlatButton)).height, equals(42.0));
    expect(tester.getSize(find.byType(Text)).width, isIn(<double>[126.0, 127.0]));
    expect(tester.getSize(find.byType(Text)).height, equals(42.0));
170 171
  });

172 173 174
  // This test is very similar to the '...explicit splashColor and highlightColor' test
  // in icon_button_test.dart. If you change this one, you may want to also change that one.
  testWidgets('MaterialButton with explicit splashColor and highlightColor', (WidgetTester tester) async {
175 176
    const Color directSplashColor = const Color(0xFF000011);
    const Color directHighlightColor = const Color(0xFF000011);
177 178 179 180 181 182 183 184 185 186 187 188

    Widget buttonWidget = new Material(
      child: new Center(
        child: new MaterialButton(
          splashColor: directSplashColor,
          highlightColor: directHighlightColor,
          onPressed: () { /* to make sure the button is enabled */ },
        ),
      ),
    );

    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
189 190 191 192 193 194
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Theme(
          data: new ThemeData(),
          child: buttonWidget,
        ),
195 196 197
      ),
    );

198
    final Offset center = tester.getCenter(find.byType(MaterialButton));
199 200
    final TestGesture gesture = await tester.startGesture(center);
    await tester.pump(); // start gesture
201
    await tester.pump(const Duration(milliseconds: 200)); // wait for splash to be well under way
202

203 204 205 206 207 208
    final Rect expectedClipRect = new Rect.fromLTRB(356.0, 282.0, 444.0, 318.0);
    final Path expectedClipPath = new Path()
     ..addRRect(new RRect.fromRectAndRadius(
         expectedClipRect,
         const Radius.circular(2.0),
     ));
209 210 211
    expect(
      Material.of(tester.element(find.byType(MaterialButton))),
      paints
212 213 214 215
        ..clipPath(pathMatcher: coversSameAreaAs(
            expectedClipPath,
            areaToCompare: expectedClipRect.inflate(10.0),
        ))
216
        ..circle(color: directSplashColor)
217
        ..rect(color: directHighlightColor)
218 219
    );

220 221
    const Color themeSplashColor1 = const Color(0xFF001100);
    const Color themeHighlightColor1 = const Color(0xFF001100);
222 223 224 225 226 227 228 229 230 231

    buttonWidget = new Material(
      child: new Center(
        child: new MaterialButton(
          onPressed: () { /* to make sure the button is enabled */ },
        ),
      ),
    );

    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
232 233 234 235 236 237 238 239
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Theme(
          data: new ThemeData(
            highlightColor: themeHighlightColor1,
            splashColor: themeSplashColor1,
          ),
          child: buttonWidget,
240 241 242 243 244 245 246
        ),
      ),
    );

    expect(
      Material.of(tester.element(find.byType(MaterialButton))),
      paints
247 248 249 250
        ..clipPath(pathMatcher: coversSameAreaAs(
            expectedClipPath,
            areaToCompare: expectedClipRect.inflate(10.0),
        ))
251
        ..circle(color: themeSplashColor1)
252
        ..rect(color: themeHighlightColor1)
253 254
    );

255 256
    const Color themeSplashColor2 = const Color(0xFF002200);
    const Color themeHighlightColor2 = const Color(0xFF002200);
257 258

    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
259 260 261 262 263 264 265 266
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Theme(
          data: new ThemeData(
            highlightColor: themeHighlightColor2,
            splashColor: themeSplashColor2,
          ),
          child: buttonWidget, // same widget, so does not get updated because of us
267 268 269 270 271 272 273 274
        ),
      ),
    );

    expect(
      Material.of(tester.element(find.byType(MaterialButton))),
      paints
        ..circle(color: themeSplashColor2)
275
        ..rect(color: themeHighlightColor2)
276 277 278 279 280
    );

    await gesture.up();
  });

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
  testWidgets('Disabled MaterialButton has same semantic size as enabled and exposes disabled semantics', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final Rect expectedButtonSize = new Rect.fromLTRB(0.0, 0.0, 116.0, 36.0);
    // Button is in center of screen
    final Matrix4 expectedButtonTransform = new Matrix4.identity()
      ..translate(
        TestSemantics.fullScreen.width / 2 - expectedButtonSize.width /2,
        TestSemantics.fullScreen.height / 2 - expectedButtonSize.height /2,
      );

    // enabled button
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new Material(
        child: new Center(
          child: new MaterialButton(
            child: const Text('Button'),
            onPressed: () { /* to make sure the button is enabled */ },
          ),
        ),
      ),
    ));

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 1,
            rect: expectedButtonSize,
            transform: expectedButtonTransform,
            label: 'Button',
            actions: <SemanticsAction>[
              SemanticsAction.tap,
            ],
            flags: <SemanticsFlag>[
              SemanticsFlag.isButton,
              SemanticsFlag.hasEnabledState,
              SemanticsFlag.isEnabled,
            ],
          ),
        ],
      ),
    ));

    // disabled button
    await tester.pumpWidget(const Directionality(
      textDirection: TextDirection.ltr,
      child: const Material(
        child: const Center(
          child: const MaterialButton(
            child: const Text('Button'),
            onPressed: null, // button is disabled
          ),
        ),
      ),
    ));

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 1,
            rect: expectedButtonSize,
            transform: expectedButtonTransform,
            label: 'Button',
            flags: <SemanticsFlag>[
              SemanticsFlag.isButton,
              SemanticsFlag.hasEnabledState,
            ],
          ),
        ],
      ),
    ));


    semantics.dispose();
  });
359
}