arena_test.dart 4.84 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4
// 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.

5
import 'package:flutter/gestures.dart';
Adam Barth's avatar
Adam Barth committed
6 7 8 9
import 'package:test/test.dart';

typedef void GestureArenaCallback(Object key);

Hixie's avatar
Hixie committed
10 11
const int primaryKey = 4;

Adam Barth's avatar
Adam Barth committed
12
class TestGestureArenaMember extends GestureArenaMember {
Hixie's avatar
Hixie committed
13 14 15 16 17 18 19 20 21 22 23
  bool acceptRan = false;
  void acceptGesture(Object key) {
    expect(key, equals(primaryKey));
    acceptRan = true;
  }
  bool rejectRan = false;
  void rejectGesture(Object key) {
    expect(key, equals(primaryKey));
    rejectRan = true;
  }
}
Adam Barth's avatar
Adam Barth committed
24

Hixie's avatar
Hixie committed
25 26 27 28
class GestureTester {
  GestureArena arena = new GestureArena();
  TestGestureArenaMember first = new TestGestureArenaMember();
  TestGestureArenaMember second = new TestGestureArenaMember();
Adam Barth's avatar
Adam Barth committed
29

Hixie's avatar
Hixie committed
30 31 32
  GestureArenaEntry firstEntry;
  void addFirst() {
    firstEntry = arena.add(primaryKey, first);
Adam Barth's avatar
Adam Barth committed
33 34
  }

Hixie's avatar
Hixie committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  GestureArenaEntry secondEntry;
  void addSecond() {
    secondEntry = arena.add(primaryKey, second);
  }

  void expectNothing() {
    expect(first.acceptRan, isFalse);
    expect(first.rejectRan, isFalse);
    expect(second.acceptRan, isFalse);
    expect(second.rejectRan, isFalse);
  }

  void expectFirstWin() {
    expect(first.acceptRan, isTrue);
    expect(first.rejectRan, isFalse);
    expect(second.acceptRan, isFalse);
    expect(second.rejectRan, isTrue);
  }

  void expectSecondWin() {
    expect(first.acceptRan, isFalse);
    expect(first.rejectRan, isTrue);
    expect(second.acceptRan, isTrue);
    expect(second.rejectRan, isFalse);
Adam Barth's avatar
Adam Barth committed
59 60 61 62 63
  }
}

void main() {
  test('Should win by accepting', () {
Hixie's avatar
Hixie committed
64 65 66 67 68 69 70
    GestureTester tester = new GestureTester();
    tester.addFirst();
    tester.addSecond();
    tester.arena.close(primaryKey);
    tester.expectNothing();
    tester.firstEntry.resolve(GestureDisposition.accepted);
    tester.expectFirstWin();
Adam Barth's avatar
Adam Barth committed
71
  });
72 73

  test('Should win by sweep', () {
Hixie's avatar
Hixie committed
74 75 76 77 78 79 80
    GestureTester tester = new GestureTester();
    tester.addFirst();
    tester.addSecond();
    tester.arena.close(primaryKey);
    tester.expectNothing();
    tester.arena.sweep(primaryKey);
    tester.expectFirstWin();
81 82 83
  });

  test('Should win on release after hold sweep release', () {
Hixie's avatar
Hixie committed
84 85 86 87 88 89 90 91 92 93 94
    GestureTester tester = new GestureTester();
    tester.addFirst();
    tester.addSecond();
    tester.arena.close(primaryKey);
    tester.expectNothing();
    tester.arena.hold(primaryKey);
    tester.expectNothing();
    tester.arena.sweep(primaryKey);
    tester.expectNothing();
    tester.arena.release(primaryKey);
    tester.expectFirstWin();
95 96 97
  });

  test('Should win on sweep after hold release sweep', () {
Hixie's avatar
Hixie committed
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    GestureTester tester = new GestureTester();
    tester.addFirst();
    tester.addSecond();
    tester.arena.close(primaryKey);
    tester.expectNothing();
    tester.arena.hold(primaryKey);
    tester.expectNothing();
    tester.arena.release(primaryKey);
    tester.expectNothing();
    tester.arena.sweep(primaryKey);
    tester.expectFirstWin();
  });

  test('Only first winner should win', () {
    GestureTester tester = new GestureTester();
    tester.addFirst();
    tester.addSecond();
    tester.arena.close(primaryKey);
    tester.expectNothing();
    tester.firstEntry.resolve(GestureDisposition.accepted);
    tester.secondEntry.resolve(GestureDisposition.accepted);
    tester.expectFirstWin();
  });

  test('Only first winner should win, regardless of order', () {
    GestureTester tester = new GestureTester();
    tester.addFirst();
    tester.addSecond();
    tester.arena.close(primaryKey);
    tester.expectNothing();
    tester.secondEntry.resolve(GestureDisposition.accepted);
    tester.firstEntry.resolve(GestureDisposition.accepted);
    tester.expectSecondWin();
  });

  test('Win before close is delayed to close', () {
    GestureTester tester = new GestureTester();
    tester.addFirst();
    tester.addSecond();
    tester.expectNothing();
    tester.firstEntry.resolve(GestureDisposition.accepted);
    tester.expectNothing();
    tester.arena.close(primaryKey);
    tester.expectFirstWin();
  });

  test('Win before close is delayed to close, and only first winner should win', () {
    GestureTester tester = new GestureTester();
    tester.addFirst();
    tester.addSecond();
    tester.expectNothing();
    tester.firstEntry.resolve(GestureDisposition.accepted);
    tester.secondEntry.resolve(GestureDisposition.accepted);
    tester.expectNothing();
    tester.arena.close(primaryKey);
    tester.expectFirstWin();
  });

  test('Win before close is delayed to close, and only first winner should win, regardless of order', () {
    GestureTester tester = new GestureTester();
    tester.addFirst();
    tester.addSecond();
    tester.expectNothing();
    tester.secondEntry.resolve(GestureDisposition.accepted);
    tester.firstEntry.resolve(GestureDisposition.accepted);
    tester.expectNothing();
    tester.arena.close(primaryKey);
    tester.expectSecondWin();
166
  });
Adam Barth's avatar
Adam Barth committed
167
}