Add more Gesture Arena tests

...and clean them up so it's easier to understand them.
This commit is contained in:
Hixie 2015-11-05 15:58:53 -08:00
parent 255ed0b951
commit aaee3b93da

View File

@ -3,239 +3,161 @@ import 'package:test/test.dart';
typedef void GestureArenaCallback(Object key); typedef void GestureArenaCallback(Object key);
const int primaryKey = 4;
class TestGestureArenaMember extends GestureArenaMember { class TestGestureArenaMember extends GestureArenaMember {
TestGestureArenaMember({ this.onAcceptGesture, this.onRejectGesture }); bool acceptRan = false;
final GestureArenaCallback onAcceptGesture;
final GestureArenaCallback onRejectGesture;
void acceptGesture(Object key) { void acceptGesture(Object key) {
onAcceptGesture(key); expect(key, equals(primaryKey));
acceptRan = true;
}
bool rejectRan = false;
void rejectGesture(Object key) {
expect(key, equals(primaryKey));
rejectRan = true;
}
} }
void rejectGesture(Object key) { class GestureTester {
onRejectGesture(key); GestureArena arena = new GestureArena();
TestGestureArenaMember first = new TestGestureArenaMember();
TestGestureArenaMember second = new TestGestureArenaMember();
GestureArenaEntry firstEntry;
void addFirst() {
firstEntry = arena.add(primaryKey, first);
}
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);
} }
} }
void main() { void main() {
test('Should win by accepting', () { test('Should win by accepting', () {
GestureArena arena = new GestureArena(); GestureTester tester = new GestureTester();
tester.addFirst();
int primaryKey = 4; tester.addSecond();
bool firstAcceptRan = false; tester.arena.close(primaryKey);
bool firstRejectRan = false; tester.expectNothing();
bool secondAcceptRan = false; tester.firstEntry.resolve(GestureDisposition.accepted);
bool secondRejectRan = false; tester.expectFirstWin();
TestGestureArenaMember first = new TestGestureArenaMember(
onAcceptGesture: (int key) {
expect(key, equals(primaryKey));
firstAcceptRan = true;
},
onRejectGesture: (int key) {
expect(key, equals(primaryKey));
firstRejectRan = true;
}
);
TestGestureArenaMember second = new TestGestureArenaMember(
onAcceptGesture: (int key) {
expect(key, equals(primaryKey));
secondAcceptRan = true;
},
onRejectGesture: (int key) {
expect(key, equals(primaryKey));
secondRejectRan = true;
}
);
GestureArenaEntry firstEntry = arena.add(primaryKey, first);
arena.add(primaryKey, second);
arena.close(primaryKey);
expect(firstAcceptRan, isFalse);
expect(firstRejectRan, isFalse);
expect(secondAcceptRan, isFalse);
expect(secondRejectRan, isFalse);
firstEntry.resolve(GestureDisposition.accepted);
expect(firstAcceptRan, isTrue);
expect(firstRejectRan, isFalse);
expect(secondAcceptRan, isFalse);
expect(secondRejectRan, isTrue);
}); });
test('Should win by sweep', () { test('Should win by sweep', () {
GestureArena arena = new GestureArena(); GestureTester tester = new GestureTester();
tester.addFirst();
int primaryKey = 4; tester.addSecond();
bool firstAcceptRan = false; tester.arena.close(primaryKey);
bool firstRejectRan = false; tester.expectNothing();
bool secondAcceptRan = false; tester.arena.sweep(primaryKey);
bool secondRejectRan = false; tester.expectFirstWin();
TestGestureArenaMember first = new TestGestureArenaMember(
onAcceptGesture: (int key) {
expect(key, equals(primaryKey));
firstAcceptRan = true;
},
onRejectGesture: (int key) {
expect(key, equals(primaryKey));
firstRejectRan = true;
}
);
TestGestureArenaMember second = new TestGestureArenaMember(
onAcceptGesture: (int key) {
expect(key, equals(primaryKey));
secondAcceptRan = true;
},
onRejectGesture: (int key) {
expect(key, equals(primaryKey));
secondRejectRan = true;
}
);
arena.add(primaryKey, first);
arena.add(primaryKey, second);
arena.close(primaryKey);
expect(firstAcceptRan, isFalse);
expect(firstRejectRan, isFalse);
expect(secondAcceptRan, isFalse);
expect(secondRejectRan, isFalse);
arena.sweep(primaryKey);
expect(firstAcceptRan, isTrue);
expect(firstRejectRan, isFalse);
expect(secondAcceptRan, isFalse);
expect(secondRejectRan, isTrue);
}); });
test('Should win on release after hold sweep release', () { test('Should win on release after hold sweep release', () {
GestureArena arena = new GestureArena(); GestureTester tester = new GestureTester();
tester.addFirst();
int primaryKey = 4; tester.addSecond();
bool firstAcceptRan = false; tester.arena.close(primaryKey);
bool firstRejectRan = false; tester.expectNothing();
bool secondAcceptRan = false; tester.arena.hold(primaryKey);
bool secondRejectRan = false; tester.expectNothing();
tester.arena.sweep(primaryKey);
TestGestureArenaMember first = new TestGestureArenaMember( tester.expectNothing();
onAcceptGesture: (int key) { tester.arena.release(primaryKey);
expect(key, equals(primaryKey)); tester.expectFirstWin();
firstAcceptRan = true;
},
onRejectGesture: (int key) {
expect(key, equals(primaryKey));
firstRejectRan = true;
}
);
TestGestureArenaMember second = new TestGestureArenaMember(
onAcceptGesture: (int key) {
expect(key, equals(primaryKey));
secondAcceptRan = true;
},
onRejectGesture: (int key) {
expect(key, equals(primaryKey));
secondRejectRan = true;
}
);
arena.add(primaryKey, first);
arena.add(primaryKey, second);
arena.close(primaryKey);
expect(firstAcceptRan, isFalse);
expect(firstRejectRan, isFalse);
expect(secondAcceptRan, isFalse);
expect(secondRejectRan, isFalse);
arena.hold(primaryKey);
expect(firstAcceptRan, isFalse);
expect(firstRejectRan, isFalse);
expect(secondAcceptRan, isFalse);
expect(secondRejectRan, isFalse);
arena.sweep(primaryKey);
expect(firstAcceptRan, isFalse);
expect(firstRejectRan, isFalse);
expect(secondAcceptRan, isFalse);
expect(secondRejectRan, isFalse);
arena.release(primaryKey);
expect(firstAcceptRan, isTrue);
expect(firstRejectRan, isFalse);
expect(secondAcceptRan, isFalse);
expect(secondRejectRan, isTrue);
}); });
test('Should win on sweep after hold release sweep', () { test('Should win on sweep after hold release sweep', () {
GestureArena arena = new GestureArena(); 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();
});
int primaryKey = 4; test('Only first winner should win', () {
bool firstAcceptRan = false; GestureTester tester = new GestureTester();
bool firstRejectRan = false; tester.addFirst();
bool secondAcceptRan = false; tester.addSecond();
bool secondRejectRan = false; tester.arena.close(primaryKey);
tester.expectNothing();
tester.firstEntry.resolve(GestureDisposition.accepted);
tester.secondEntry.resolve(GestureDisposition.accepted);
tester.expectFirstWin();
});
TestGestureArenaMember first = new TestGestureArenaMember( test('Only first winner should win, regardless of order', () {
onAcceptGesture: (int key) { GestureTester tester = new GestureTester();
expect(key, equals(primaryKey)); tester.addFirst();
firstAcceptRan = true; tester.addSecond();
}, tester.arena.close(primaryKey);
onRejectGesture: (int key) { tester.expectNothing();
expect(key, equals(primaryKey)); tester.secondEntry.resolve(GestureDisposition.accepted);
firstRejectRan = true; tester.firstEntry.resolve(GestureDisposition.accepted);
} tester.expectSecondWin();
); });
TestGestureArenaMember second = new TestGestureArenaMember( test('Win before close is delayed to close', () {
onAcceptGesture: (int key) { GestureTester tester = new GestureTester();
expect(key, equals(primaryKey)); tester.addFirst();
secondAcceptRan = true; tester.addSecond();
}, tester.expectNothing();
onRejectGesture: (int key) { tester.firstEntry.resolve(GestureDisposition.accepted);
expect(key, equals(primaryKey)); tester.expectNothing();
secondRejectRan = true; tester.arena.close(primaryKey);
} tester.expectFirstWin();
); });
arena.add(primaryKey, first); test('Win before close is delayed to close, and only first winner should win', () {
arena.add(primaryKey, second); GestureTester tester = new GestureTester();
arena.close(primaryKey); tester.addFirst();
tester.addSecond();
tester.expectNothing();
tester.firstEntry.resolve(GestureDisposition.accepted);
tester.secondEntry.resolve(GestureDisposition.accepted);
tester.expectNothing();
tester.arena.close(primaryKey);
tester.expectFirstWin();
});
expect(firstAcceptRan, isFalse); test('Win before close is delayed to close, and only first winner should win, regardless of order', () {
expect(firstRejectRan, isFalse); GestureTester tester = new GestureTester();
expect(secondAcceptRan, isFalse); tester.addFirst();
expect(secondRejectRan, isFalse); tester.addSecond();
tester.expectNothing();
arena.hold(primaryKey); tester.secondEntry.resolve(GestureDisposition.accepted);
tester.firstEntry.resolve(GestureDisposition.accepted);
expect(firstAcceptRan, isFalse); tester.expectNothing();
expect(firstRejectRan, isFalse); tester.arena.close(primaryKey);
expect(secondAcceptRan, isFalse); tester.expectSecondWin();
expect(secondRejectRan, isFalse);
arena.release(primaryKey);
expect(firstAcceptRan, isFalse);
expect(firstRejectRan, isFalse);
expect(secondAcceptRan, isFalse);
expect(secondRejectRan, isFalse);
arena.sweep(primaryKey);
expect(firstAcceptRan, isTrue);
expect(firstRejectRan, isFalse);
expect(secondAcceptRan, isFalse);
expect(secondRejectRan, isTrue);
}); });
} }