Ticker.stop should work when muted (#7487)

Previously, the ticker would not stop when it was muted because it thought it
was already not ticking.
This commit is contained in:
Adam Barth 2017-01-13 12:46:13 -08:00 committed by GitHub
parent fea7496546
commit 3150e3fbd4
2 changed files with 73 additions and 4 deletions

View File

@ -128,7 +128,7 @@ class Ticker {
/// (as opposed to the [TickerProvider] which created the ticker). /// (as opposed to the [TickerProvider] which created the ticker).
Future<Null> start() { Future<Null> start() {
assert(() { assert(() {
if (isTicking) { if (isActive) {
throw new FlutterError( throw new FlutterError(
'A ticker was started twice.\n' 'A ticker was started twice.\n'
'A ticker that is already active cannot be started again without first stopping it.\n' 'A ticker that is already active cannot be started again without first stopping it.\n'
@ -158,7 +158,7 @@ class Ticker {
/// By convention, this method is used by the object that receives the ticks /// By convention, this method is used by the object that receives the ticks
/// (as opposed to the [TickerProvider] which created the ticker). /// (as opposed to the [TickerProvider] which created the ticker).
void stop() { void stop() {
if (!isTicking) if (!isActive)
return; return;
// We take the _completer into a local variable so that isTicking is false // We take the _completer into a local variable so that isTicking is false
@ -167,7 +167,7 @@ class Ticker {
Completer<Null> localCompleter = _completer; Completer<Null> localCompleter = _completer;
_completer = null; _completer = null;
_startTime = null; _startTime = null;
assert(!isTicking); assert(!isActive);
unscheduleTick(); unscheduleTick();
localCompleter.complete(); localCompleter.complete();
@ -246,7 +246,7 @@ class Ticker {
/// ///
/// This ticker must not be active when this method is called. /// This ticker must not be active when this method is called.
void absorbTicker(Ticker originalTicker) { void absorbTicker(Ticker originalTicker) {
assert(!isTicking); assert(!isActive);
assert(_completer == null); assert(_completer == null);
assert(_startTime == null); assert(_startTime == null);
assert(_animationId == null); assert(_animationId == null);

View File

@ -0,0 +1,69 @@
// Copyright 2017 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/scheduler.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Ticker mute control test', (WidgetTester tester) async {
int tickCount = 0;
void handleTick(Duration duration) {
++tickCount;
}
Ticker ticker = new Ticker(handleTick);
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
ticker.start();
expect(ticker.isTicking, isTrue);
expect(ticker.isActive, isTrue);
expect(tickCount, equals(0));
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);
});
}