ticker_test.dart 5.46 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
import 'package:flutter/foundation.dart';
6
import 'package:flutter/scheduler.dart';
7
import 'package:flutter/services.dart';
8 9 10 11 12 13
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('Ticker mute control test', (WidgetTester tester) async {
    int tickCount = 0;
    void handleTick(Duration duration) {
14
      tickCount += 1;
15 16
    }

17
    final Ticker ticker = Ticker(handleTick);
18 19 20 21 22 23 24 25 26 27

    expect(ticker.isTicking, isFalse);
    expect(ticker.isActive, isFalse);

    ticker.start();

    expect(ticker.isTicking, isTrue);
    expect(ticker.isActive, isTrue);
    expect(tickCount, equals(0));

28
    FlutterError? error;
29 30 31 32 33 34
    try {
      ticker.start();
    } on FlutterError catch (e) {
      error = e;
    }
    expect(error, isNotNull);
35
    expect(error!.diagnostics.length, 3);
Dan Field's avatar
Dan Field committed
36
    expect(error.diagnostics.last, isA<DiagnosticsProperty<Ticker>>());
37 38 39 40 41 42 43 44 45 46 47
    expect(
      error.toStringDeep(),
      startsWith(
        'FlutterError\n'
        '   A ticker was started twice.\n'
        '   A ticker that is already active cannot be started again without\n'
        '   first stopping it.\n'
        '   The affected ticker was:\n'
        '     Ticker()\n',
      ),
    );
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
    await tester.pump(const Duration(milliseconds: 10));

    expect(tickCount, equals(1));

    ticker.muted = true;
    await tester.pump(const Duration(milliseconds: 10));

    expect(tickCount, equals(1));
    expect(ticker.isTicking, isFalse);
    expect(ticker.isActive, isTrue);

    ticker.muted = false;
    await tester.pump(const Duration(milliseconds: 10));

    expect(tickCount, equals(2));
    expect(ticker.isTicking, isTrue);
    expect(ticker.isActive, isTrue);

    ticker.muted = true;
    await tester.pump(const Duration(milliseconds: 10));

    expect(tickCount, equals(2));
    expect(ticker.isTicking, isFalse);
    expect(ticker.isActive, isTrue);

    ticker.stop();

    expect(tickCount, equals(2));
    expect(ticker.isTicking, isFalse);
    expect(ticker.isActive, isFalse);

    ticker.muted = false;

    expect(tickCount, equals(2));
    expect(ticker.isTicking, isFalse);
    expect(ticker.isActive, isFalse);

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

    expect(tickCount, equals(2));
    expect(ticker.isTicking, isFalse);
    expect(ticker.isActive, isFalse);
  });
92 93

  testWidgets('Ticker control test', (WidgetTester tester) async {
94
    late Ticker ticker;
95 96

    void testFunction() {
97
      ticker = Ticker((Duration _) { });
98 99 100 101 102 103 104
    }

    testFunction();

    expect(ticker, hasOneLineDescription);
    expect(ticker.toString(debugIncludeStack: true), contains('testFunction'));
  });
105

106 107
  testWidgets('Ticker can be sped up with time dilation', (WidgetTester tester) async {
    timeDilation = 0.5; // Move twice as fast.
108
    late Duration lastDuration;
109 110 111 112
    void handleTick(Duration duration) {
      lastDuration = duration;
    }

113
    final Ticker ticker = Ticker(handleTick);
114 115 116 117 118 119 120 121 122 123
    ticker.start();
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    expect(lastDuration, const Duration(milliseconds: 20));

    ticker.dispose();
  });

  testWidgets('Ticker can be slowed down with time dilation', (WidgetTester tester) async {
    timeDilation = 2.0; // Move half as fast.
124
    late Duration lastDuration;
125 126 127 128
    void handleTick(Duration duration) {
      lastDuration = duration;
    }

129
    final Ticker ticker = Ticker(handleTick);
130 131 132 133 134 135 136 137
    ticker.start();
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    expect(lastDuration, const Duration(milliseconds: 5));

    ticker.dispose();
  });

138 139 140 141 142 143
  testWidgets('Ticker stops ticking when application is paused', (WidgetTester tester) async {
    int tickCount = 0;
    void handleTick(Duration duration) {
      tickCount += 1;
    }

144
    final Ticker ticker = Ticker(handleTick);
145 146 147 148 149 150
    ticker.start();

    expect(ticker.isTicking, isTrue);
    expect(ticker.isActive, isTrue);
    expect(tickCount, equals(0));

151 152
    final ByteData? message = const StringCodec().encodeMessage('AppLifecycleState.paused');
    await ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
153 154 155 156 157
    expect(ticker.isTicking, isFalse);
    expect(ticker.isActive, isTrue);

    ticker.stop();
  });
158 159

  testWidgets('Ticker can be created before application unpauses', (WidgetTester tester) async {
160 161
    final ByteData? pausedMessage = const StringCodec().encodeMessage('AppLifecycleState.paused');
    await ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', pausedMessage, (_) { });
162

163 164 165 166 167
    int tickCount = 0;
    void handleTick(Duration duration) {
      tickCount += 1;
    }

168
    final Ticker ticker = Ticker(handleTick);
169 170 171 172 173 174 175 176 177 178
    ticker.start();

    expect(tickCount, equals(0));
    expect(ticker.isTicking, isFalse);

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

    expect(tickCount, equals(0));
    expect(ticker.isTicking, isFalse);

179 180
    final ByteData? resumedMessage = const StringCodec().encodeMessage('AppLifecycleState.resumed');
    await ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', resumedMessage, (_) { });
181

182 183 184 185 186 187 188
    await tester.pump(const Duration(milliseconds: 10));

    expect(tickCount, equals(1));
    expect(ticker.isTicking, isTrue);

    ticker.stop();
  });
189
}