progress_indicator_test.dart 7.25 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
  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(),
          ),
        ),
      ),
    );

103 104 105
    await tester.pump(const Duration(milliseconds: 300));
    final double animationValue = const Interval(0.0, 750.0 / 1800.0, curve: const Cubic(0.2, 0.0, 0.8, 1.0))
      .transform(300.0 / 1800.0);
106 107 108 109 110

    expect(
      find.byType(LinearProgressIndicator),
      paints
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0))
111
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, animationValue * 200.0, 6.0))
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    );

    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(),
          ),
        ),
      ),
    );

130 131 132
    await tester.pump(const Duration(milliseconds: 300));
    final double animationValue = const Interval(0.0, 750.0 / 1800.0, curve: const Cubic(0.2, 0.0, 0.8, 1.0))
      .transform(300.0 / 1800.0);
133 134 135 136 137

    expect(
      find.byType(LinearProgressIndicator),
      paints
        ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 200.0, 6.0))
138
        ..rect(rect: new Rect.fromLTRB(200.0 - animationValue * 200.0, 0.0, 200.0, 6.0))
139 140 141 142 143
    );

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

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
  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)
    );
  });

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

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

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

  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));
  });

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
  testWidgets('Indeterminate RefreshProgressIndicator keeps spinning until end of time (approximate)', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/13782

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const Center(
          child: const SizedBox(
            width: 200.0,
            child: const RefreshProgressIndicator(),
          ),
        ),
      ),
    );
    expect(tester.hasRunningAnimations, isTrue);

    await tester.pump(const Duration(milliseconds: 6666));
    expect(tester.hasRunningAnimations, isTrue);

    await tester.pump(const Duration(milliseconds: 1));
    expect(tester.hasRunningAnimations, isTrue);

    await tester.pump(const Duration(days: 9999));
    expect(tester.hasRunningAnimations, isTrue);
  });

235
}