box_painter_test.dart 13 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])

9
import 'package:flutter/material.dart';
10
import 'package:flutter_test/flutter_test.dart';
11 12

void main() {
13 14 15 16
  tearDown(() {
    debugDisableShadows = true;
  });

xster's avatar
xster committed
17
  test('BorderSide control test', () {
18
    const BorderSide side1 = BorderSide();
19
    final BorderSide side2 = side1.copyWith(
20 21 22 23 24 25 26 27 28 29 30 31 32 33
      color: const Color(0xFF00FFFF),
      width: 2.0,
      style: BorderStyle.solid,
    );

    expect(side1, hasOneLineDescription);
    expect(side1.hashCode, isNot(equals(side2.hashCode)));

    expect(side2.color, equals(const Color(0xFF00FFFF)));
    expect(side2.width, equals(2.0));
    expect(side2.style, equals(BorderStyle.solid));

    expect(BorderSide.lerp(side1, side2, 0.0), equals(side1));
    expect(BorderSide.lerp(side1, side2, 1.0), equals(side2));
34
    expect(BorderSide.lerp(side1, side2, 0.5), equals(BorderSide(
35
      color: Color.lerp(const Color(0xFF000000), const Color(0xFF00FFFF), 0.5)!,
36 37 38
      width: 1.5,
    )));

39
    final BorderSide side3 = side2.copyWith(style: BorderStyle.none);
40 41 42 43 44 45 46 47 48
    BorderSide interpolated = BorderSide.lerp(side2, side3, 0.2);
    expect(interpolated.style, equals(BorderStyle.solid));
    expect(interpolated.color, equals(side2.color.withOpacity(0.8)));

    interpolated = BorderSide.lerp(side3, side2, 0.2);
    expect(interpolated.style, equals(BorderStyle.solid));
    expect(interpolated.color, equals(side2.color.withOpacity(0.2)));
  });

49
  test('BorderSide toString test', () {
50
    const BorderSide side1 = BorderSide();
51 52 53 54 55 56
    final BorderSide side2 = side1.copyWith(
      color: const Color(0xFF00FFFF),
      width: 2.0,
      style: BorderStyle.solid,
    );

57 58
    expect(side1.toString(), equals('BorderSide'));
    expect(side2.toString(), equals('BorderSide(color: Color(0xff00ffff), width: 2.0)'));
59 60
  });

xster's avatar
xster committed
61
  test('Border control test', () {
62
    final Border border1 = Border.all(width: 4.0);
63 64
    final Border border2 = Border.lerp(null, border1, 0.25)!;
    final Border border3 = Border.lerp(border1, null, 0.25)!;
65 66 67 68 69 70 71

    expect(border1, hasOneLineDescription);
    expect(border1.hashCode, isNot(equals(border2.hashCode)));

    expect(border2.top.width, equals(1.0));
    expect(border3.bottom.width, equals(3.0));

72
    final Border border4 = Border.lerp(border2, border3, 0.5)!;
73 74 75
    expect(border4.left.width, equals(2.0));
  });

76 77
  test('Border toString test', () {
    expect(
78
      Border.all(width: 4.0).toString(),
79
      equals('Border.all(BorderSide(width: 4.0))'),
80 81 82
    );
    expect(
      const Border(
83 84 85 86
        top: BorderSide(width: 3.0),
        right: BorderSide(width: 3.0),
        bottom: BorderSide(width: 3.0),
        left: BorderSide(width: 3.0),
87
      ).toString(),
88
      equals('Border.all(BorderSide(width: 3.0))'),
89 90 91
    );
  });

xster's avatar
xster committed
92
  test('BoxShadow control test', () {
93
    const BoxShadow shadow1 = BoxShadow(blurRadius: 4.0);
94 95
    final BoxShadow shadow2 = BoxShadow.lerp(null, shadow1, 0.25)!;
    final BoxShadow shadow3 = BoxShadow.lerp(shadow1, null, 0.25)!;
96 97 98

    expect(shadow1, hasOneLineDescription);
    expect(shadow1.hashCode, isNot(equals(shadow2.hashCode)));
99
    expect(shadow1, equals(const BoxShadow(blurRadius: 4.0)));
100 101 102 103

    expect(shadow2.blurRadius, equals(1.0));
    expect(shadow3.blurRadius, equals(3.0));

104
    final BoxShadow shadow4 = BoxShadow.lerp(shadow2, shadow3, 0.5)!;
105 106
    expect(shadow4.blurRadius, equals(2.0));

107
    List<BoxShadow> shadowList = BoxShadow.lerpList(<BoxShadow>[shadow2, shadow1], <BoxShadow>[shadow3], 0.5)!;
108
    expect(shadowList, equals(<BoxShadow>[shadow4, shadow1.scale(0.5)]));
109
    shadowList = BoxShadow.lerpList(<BoxShadow>[shadow2], <BoxShadow>[shadow3, shadow1], 0.5)!;
110 111
    expect(shadowList, equals(<BoxShadow>[shadow4, shadow1.scale(0.5)]));
  });
xster's avatar
xster committed
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  test('BoxShadow BlurStyle test', () {
    const BoxShadow shadow1 = BoxShadow(blurRadius: 4.0);
    const BoxShadow shadow2 = BoxShadow(blurRadius: 4.0, blurStyle: BlurStyle.outer);
    final BoxShadow shadow3 = BoxShadow.lerp(shadow1, null, 0.25)!;
    final BoxShadow shadow4 = BoxShadow.lerp(null, shadow1, 0.25)!;
    final BoxShadow shadow5 = BoxShadow.lerp(shadow1, shadow2, 0.25)!;
    final BoxShadow shadow6 = BoxShadow.lerp(const BoxShadow(blurStyle: BlurStyle.solid), shadow2, 0.25)!;

    expect(shadow1.blurStyle, equals(BlurStyle.normal));
    expect(shadow2.blurStyle, equals(BlurStyle.outer));
    expect(shadow3.blurStyle, equals(BlurStyle.normal));
    expect(shadow4.blurStyle, equals(BlurStyle.normal));
    expect(shadow5.blurStyle, equals(BlurStyle.outer));
    expect(shadow6.blurStyle, equals(BlurStyle.solid));

    List<BoxShadow> shadowList = BoxShadow.lerpList(<BoxShadow>[shadow2, shadow1], <BoxShadow>[shadow3], 0.5)!;
    expect(shadowList[0].blurStyle, equals(BlurStyle.outer));
    expect(shadowList[1].blurStyle, equals(BlurStyle.normal));

    shadowList = BoxShadow.lerpList(<BoxShadow>[shadow6], <BoxShadow>[shadow3, shadow1], 0.5)!;
    expect(shadowList[0].blurStyle, equals(BlurStyle.solid));
    expect(shadowList[1].blurStyle, equals(BlurStyle.normal));

    shadowList = BoxShadow.lerpList(<BoxShadow>[shadow3], <BoxShadow>[shadow6, shadow1], 0.5)!;
    expect(shadowList[0].blurStyle, equals(BlurStyle.solid));
    expect(shadowList[1].blurStyle, equals(BlurStyle.normal));

    shadowList = BoxShadow.lerpList(<BoxShadow>[shadow3], <BoxShadow>[shadow2, shadow1], 0.5)!;
    expect(shadowList[0].blurStyle, equals(BlurStyle.outer));
    expect(shadowList[1].blurStyle, equals(BlurStyle.normal));
  });

145
  test('BoxShadow toString test', () {
146 147
    expect(const BoxShadow(blurRadius: 4.0).toString(), equals('BoxShadow(Color(0xff000000), Offset(0.0, 0.0), 4.0, 0.0, BlurStyle.normal)'));
    expect(const BoxShadow(blurRadius: 4.0, blurStyle: BlurStyle.solid).toString(), equals('BoxShadow(Color(0xff000000), Offset(0.0, 0.0), 4.0, 0.0, BlurStyle.solid)'));
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  });

  testWidgets('BoxShadow BoxStyle.solid', (WidgetTester tester) async {
    final Key key = UniqueKey();
    debugDisableShadows = false;
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          key: key,
          child: Container(
            color: Colors.white,
            width: 50,
            height: 50,
            child: Center(
              child: Container(
                decoration: const BoxDecoration(
                  boxShadow: <BoxShadow>[BoxShadow(blurRadius: 3.0, blurStyle: BlurStyle.solid)],
                ),
                width: 10,
                height: 10,
              ),
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byKey(key),
      matchesGoldenFile('boxShadow.boxStyle.solid.0.0.png'),
    );
    debugDisableShadows = true;
  });

  testWidgets('BoxShadow BoxStyle.outer', (WidgetTester tester) async {
    final Key key = UniqueKey();
    debugDisableShadows = false;
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          key: key,
          child: Container(
            color: Colors.white,
            width: 50,
            height: 50,
            child: Center(
              child: Container(
                decoration: const BoxDecoration(
                  boxShadow: <BoxShadow>[BoxShadow(blurRadius: 8.0, blurStyle: BlurStyle.outer)],
                ),
                width: 20,
                height: 20,
              ),
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byKey(key),
      matchesGoldenFile('boxShadow.boxStyle.outer.0.0.png'),
    );
    debugDisableShadows = true;
  });

  testWidgets('BoxShadow BoxStyle.inner', (WidgetTester tester) async {
    final Key key = UniqueKey();
    debugDisableShadows = false;
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          key: key,
          child: Container(
            color: Colors.white,
            width: 50,
            height: 50,
            child: Center(
              child: Container(
                decoration: const BoxDecoration(
                  boxShadow: <BoxShadow>[BoxShadow(blurRadius: 4.0, blurStyle: BlurStyle.inner)],
                ),
                width: 20,
                height: 20,
              ),
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byKey(key),
      matchesGoldenFile('boxShadow.boxStyle.inner.0.0.png'),
    );
    debugDisableShadows = true;
  });

  testWidgets('BoxShadow BoxStyle.normal', (WidgetTester tester) async {
    final Key key = UniqueKey();
    debugDisableShadows = false;
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          key: key,
          child: Container(
            color: Colors.white,
            width: 50,
            height: 50,
            child: Center(
              child: Container(
                decoration: const BoxDecoration(
260
                  boxShadow: <BoxShadow>[BoxShadow(blurRadius: 4.0)],
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                ),
                width: 20,
                height: 20,
              ),
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byKey(key),
      matchesGoldenFile('boxShadow.boxStyle.normal.0.0.png'),
    );
    debugDisableShadows = true;
  });

  testWidgets('BoxShadow BoxStyle.normal.wide_radius', (WidgetTester tester) async {
    final Key key = UniqueKey();
    debugDisableShadows = false;
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          key: key,
          child: Container(
            color: Colors.amber,
            width: 128,
            height: 128,
            child: Center(
              child: Container(
                decoration: const BoxDecoration(
                  color: Colors.black,
293
                  boxShadow: <BoxShadow>[BoxShadow(blurRadius: 16.0, offset: Offset(4, 4), color: Colors.green, spreadRadius: 2)],
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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
                ),
                width: 64,
                height: 64,
              ),
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byKey(key),
      matchesGoldenFile('boxShadow.boxStyle.normal.wide_radius.0.0.png'),
    );
    debugDisableShadows = true;
  });

  testWidgets('BoxShadow BoxStyle.outer.wide_radius', (WidgetTester tester) async {
    final Key key = UniqueKey();
    debugDisableShadows = false;
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          key: key,
          child: Container(
            color: Colors.amber,
            width: 128,
            height: 128,
            child: Center(
              child: Container(
                decoration: const BoxDecoration(
                  color: Colors.black,
                  boxShadow: <BoxShadow>[BoxShadow(blurRadius: 16.0, offset: Offset(4, 4), blurStyle: BlurStyle.outer, color: Colors.red, spreadRadius: 2)],
                ),
                width: 64,
                height: 64,
              ),
            ),
          ),
        ),
      ),
    );

    await expectLater(
      find.byKey(key),
      matchesGoldenFile('boxShadow.boxStyle.outer.wide_radius.0.0.png'),
    );
    debugDisableShadows = true;
  });

  testWidgets('BoxShadow BoxStyle.solid.wide_radius', (WidgetTester tester) async {
    final Key key = UniqueKey();
    debugDisableShadows = false;
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          key: key,
          child: Container(
            color: Colors.grey,
            width: 128,
            height: 128,
            child: Center(
              child: Container(
                decoration: const BoxDecoration(
                  color: Colors.black,
                  boxShadow: <BoxShadow>[BoxShadow(blurRadius: 16.0, offset: Offset(4, 4), blurStyle: BlurStyle.solid, color: Colors.purple, spreadRadius: 2)],
                ),
                width: 64,
                height: 64,
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byKey(key),
      matchesGoldenFile('boxShadow.boxStyle.solid.wide_radius.0.0.png'),
    );
    debugDisableShadows = true;
  });

  testWidgets('BoxShadow BoxStyle.inner.wide_radius', (WidgetTester tester) async {
    final Key key = UniqueKey();
    debugDisableShadows = false;
    await tester.pumpWidget(
      Center(
        child: RepaintBoundary(
          key: key,
          child: Container(
            color: Colors.green,
            width: 128,
            height: 128,
            child: Center(
              child: Container(
                decoration: const BoxDecoration(
                  color: Colors.black,
                  boxShadow: <BoxShadow>[BoxShadow(blurRadius: 16.0, offset: Offset(4, 4), blurStyle: BlurStyle.inner, color: Colors.amber, spreadRadius: 2)],
                ),
                width: 64,
                height: 64,
              ),
            ),
          ),
        ),
      ),
    );
    await expectLater(
      find.byKey(key),
      matchesGoldenFile('boxShadow.boxStyle.inner.wide_radius.0.0.png'),
    );
    debugDisableShadows = true;
406
  });
407
}