animation_test.dart 1.66 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
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
import 'package:flutter_test/flutter_test.dart';
9

10 11
import 'scheduler_tester.dart';

12
class TestSchedulerBinding extends BindingBase with SchedulerBinding, ServicesBinding { }
Ian Hickson's avatar
Ian Hickson committed
13

14
void main() {
15
  final SchedulerBinding scheduler = TestSchedulerBinding();
Ian Hickson's avatar
Ian Hickson committed
16

17
  test('Check for a time dilation being in effect', () {
18 19 20
    expect(timeDilation, equals(1.0));
  });

21
  test('Can cancel queued callback', () {
22
    late int secondId;
23 24 25 26

    bool firstCallbackRan = false;
    bool secondCallbackRan = false;

27
    void firstCallback(Duration timeStamp) {
28 29
      expect(firstCallbackRan, isFalse);
      expect(secondCallbackRan, isFalse);
30
      expect(timeStamp.inMilliseconds, equals(0));
31
      firstCallbackRan = true;
32
      scheduler.cancelFrameCallbackWithId(secondId);
33 34
    }

35
    void secondCallback(Duration timeStamp) {
36 37
      expect(firstCallbackRan, isTrue);
      expect(secondCallbackRan, isFalse);
38
      expect(timeStamp.inMilliseconds, equals(0));
39 40 41
      secondCallbackRan = true;
    }

42 43
    scheduler.scheduleFrameCallback(firstCallback);
    secondId = scheduler.scheduleFrameCallback(secondCallback);
44

45
    tick(const Duration(milliseconds: 16));
46 47 48 49 50 51 52

    expect(firstCallbackRan, isTrue);
    expect(secondCallbackRan, isFalse);

    firstCallbackRan = false;
    secondCallbackRan = false;

53
    tick(const Duration(milliseconds: 32));
54 55 56 57 58

    expect(firstCallbackRan, isFalse);
    expect(secondCallbackRan, isFalse);
  });
}