progress_indicator_test.dart 6.35 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/material.dart';

9 10
import '../rendering/mock_canvas.dart';

11 12 13 14 15
void main() {

  // The "can be constructed" tests that follow are primarily to ensure that any
  // animations started by the progress indicators are stopped at dispose() time.

16 17
  testWidgets('LinearProgressIndicator(value: 0.0) can be constructed', (WidgetTester tester) async {
    await tester.pumpWidget(
18 19 20 21 22 23 24 25 26
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const Center(
          child: const SizedBox(
            width: 200.0,
            child: const LinearProgressIndicator(value: 0.0),
          ),
        ),
      ),
27
    );
28 29
  });

30 31
  testWidgets('LinearProgressIndicator(value: null) can be constructed', (WidgetTester tester) async {
    await tester.pumpWidget(
32 33 34 35 36 37 38 39 40
      const Directionality(
        textDirection: TextDirection.rtl,
        child: const Center(
          child: const SizedBox(
            width: 200.0,
            child: const LinearProgressIndicator(value: null),
          ),
        ),
      ),
41
    );
42 43
  });

44 45 46 47 48 49 50 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 145
  testWidgets('LinearProgressIndicator paint (LTR)', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const Center(
          child: const SizedBox(
            width: 200.0,
            child: const LinearProgressIndicator(value: 0.25),
          ),
        ),
      ),
    );

    expect(
      find.byType(LinearProgressIndicator),
      paints
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0))
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 50.0, 6.0))
    );

    expect(tester.binding.transientCallbackCount, 0);
  });

  testWidgets('LinearProgressIndicator paint (RTL)', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.rtl,
        child: const Center(
          child: const SizedBox(
            width: 200.0,
            child: const LinearProgressIndicator(value: 0.25),
          ),
        ),
      ),
    );

    expect(
      find.byType(LinearProgressIndicator),
      paints
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0))
        ..rect(rect: new Rect.fromLTRB(150.0, 0.0, 200.0, 6.0))
    );

    expect(tester.binding.transientCallbackCount, 0);
  });

  testWidgets('LinearProgressIndicator indeterminate (LTR)', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const Center(
          child: const SizedBox(
            width: 200.0,
            child: const LinearProgressIndicator(),
          ),
        ),
      ),
    );

    await tester.pump(const Duration(milliseconds: 750));

    final double animationValue = Curves.fastOutSlowIn.transform(0.5);
    final double startX = 200.0 * (1.5 * animationValue - 0.5);

    expect(
      find.byType(LinearProgressIndicator),
      paints
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0))
        ..rect(rect: new Rect.fromLTRB(startX, 0.0, 200.0, 6.0))
    );

    expect(tester.binding.transientCallbackCount, 1);
  });

  testWidgets('LinearProgressIndicator paint (RTL)', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.rtl,
        child: const Center(
          child: const SizedBox(
            width: 200.0,
            child: const LinearProgressIndicator(),
          ),
        ),
      ),
    );

    await tester.pump(const Duration(milliseconds: 750));

    final double animationValue = Curves.fastOutSlowIn.transform(0.5);
    final double startX = 200.0 * (1.5 * animationValue - 0.5);

    expect(
      find.byType(LinearProgressIndicator),
      paints
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0))
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0 - startX, 6.0))
    );

    expect(tester.binding.transientCallbackCount, 1);
  });

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  testWidgets('LinearProgressIndicator with colors', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const Center(
          child: const SizedBox(
            width: 200.0,
            child: const LinearProgressIndicator(
              value: 0.25,
              valueColor: const AlwaysStoppedAnimation<Color>(Colors.white),
              backgroundColor: Colors.black,
            ),
          ),
        ),
      ),
    );

    expect(
      find.byType(LinearProgressIndicator),
      paints
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0))
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 50.0, 6.0), color: Colors.white)
    );
  });

171 172
  testWidgets('CircularProgressIndicator(value: 0.0) can be constructed', (WidgetTester tester) async {
    await tester.pumpWidget(
173 174
      const Center(
        child: const CircularProgressIndicator(value: 0.0)
175 176
      )
    );
177 178
  });

179 180
  testWidgets('CircularProgressIndicator(value: null) can be constructed', (WidgetTester tester) async {
    await tester.pumpWidget(
181 182
      const Center(
        child: const CircularProgressIndicator(value: null)
183 184
      )
    );
185 186
  });

187
  testWidgets('LinearProgressIndicator causes a repaint when it changes', (WidgetTester tester) async {
188 189 190 191
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new ListView(children: <Widget>[const LinearProgressIndicator(value: 0.0)]),
    ));
192
    final List<Layer> layers1 = tester.layers;
193 194 195 196
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new ListView(children: <Widget>[const LinearProgressIndicator(value: 0.5)])),
    );
197
    final List<Layer> layers2 = tester.layers;
198
    expect(layers1, isNot(equals(layers2)));
199
  });
200 201 202 203 204 205 206 207 208 209 210

  testWidgets('CircularProgressIndicator stoke width', (WidgetTester tester) async {
    await tester.pumpWidget(const CircularProgressIndicator());

    expect(find.byType(CircularProgressIndicator), paints..arc(strokeWidth: 4.0));

    await tester.pumpWidget(const CircularProgressIndicator(strokeWidth: 16.0));

    expect(find.byType(CircularProgressIndicator), paints..arc(strokeWidth: 16.0));
  });

211
}