-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathboard_test.dart
1008 lines (855 loc) · 33.8 KB
/
board_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
92
93
94
95
96
97
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import 'dart:async';
import 'package:chessground/src/widgets/shape.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:dartchess/dartchess.dart' as dc;
import 'package:chessground/chessground.dart';
const boardSize = 200.0;
const squareSize = boardSize / 8;
void main() {
group('Non-interactable board', () {
const viewOnlyBoard = Directionality(
textDirection: TextDirection.ltr,
child: Board(
size: boardSize,
data: BoardData(
interactableSide: InteractableSide.none,
orientation: Side.white,
fen: dc.kInitialFEN,
),
settings: BoardSettings(
drawShape: DrawShapeOptions(enable: true),
),
),
);
testWidgets('initial position display', (WidgetTester tester) async {
await tester.pumpWidget(viewOnlyBoard);
expect(find.byType(Board), findsOneWidget);
expect(find.byType(PieceWidget), findsNWidgets(32));
});
testWidgets('cannot select piece', (WidgetTester tester) async {
await tester.pumpWidget(viewOnlyBoard);
await tester.tap(find.byKey(const Key('e2-whitePawn')));
await tester.pump();
expect(find.byKey(const Key('e2-selected')), findsNothing);
});
});
group('Interactable board', () {
testWidgets('selecting and deselecting a square',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(initialInteractableSide: InteractableSide.both),
);
await tester.tap(find.byKey(const Key('a2-whitePawn')));
await tester.pump();
expect(find.byKey(const Key('a2-selected')), findsOneWidget);
expect(find.byType(MoveDest), findsNWidgets(2));
// selecting same deselects
await tester.tap(find.byKey(const Key('a2-whitePawn')));
await tester.pump();
expect(find.byKey(const Key('a2-selected')), findsNothing);
expect(find.byType(MoveDest), findsNothing);
// selecting another square
await tester.tap(find.byKey(const Key('a1-whiteRook')));
await tester.pump();
expect(find.byKey(const Key('a1-selected')), findsOneWidget);
expect(find.byType(MoveDest), findsNothing);
// selecting an opposite piece deselects
await tester.tap(find.byKey(const Key('e7-blackPawn')));
await tester.pump();
expect(find.byKey(const Key('a1-selected')), findsNothing);
expect(find.byType(MoveDest), findsNothing);
// selecting an empty square deselects
await tester.tap(find.byKey(const Key('a1-whiteRook')));
await tester.pump();
expect(find.byKey(const Key('a1-selected')), findsOneWidget);
await tester.tapAt(squareOffset('c4'));
await tester.pump();
expect(find.byKey(const Key('a1-selected')), findsNothing);
// cannot select a piece whose side is not the turn to move
await tester.tap(find.byKey(const Key('e7-blackPawn')));
await tester.pump();
expect(find.byKey(const Key('e7-selected')), findsNothing);
});
testWidgets('play e2-e4 move by tap', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(initialInteractableSide: InteractableSide.both),
);
await tester.tap(find.byKey(const Key('e2-whitePawn')));
await tester.pump();
expect(find.byKey(const Key('e2-selected')), findsOneWidget);
expect(find.byType(MoveDest), findsNWidgets(2));
await tester.tapAt(squareOffset('e4'));
await tester.pump();
expect(find.byKey(const Key('e2-selected')), findsNothing);
expect(find.byType(MoveDest), findsNothing);
expect(find.byKey(const Key('e4-whitePawn')), findsOneWidget);
expect(find.byKey(const Key('e2-whitePawn')), findsNothing);
expect(find.byKey(const Key('e2-lastMove')), findsOneWidget);
expect(find.byKey(const Key('e4-lastMove')), findsOneWidget);
});
testWidgets('Cannot move by tap if piece shift method is drag',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
pieceShiftMethod: PieceShiftMethod.drag,
),
);
await tester.tap(find.byKey(const Key('e2-whitePawn')));
await tester.pump();
// Tapping a square should have no effect...
expect(find.byKey(const Key('e2-selected')), findsNothing);
expect(find.byType(MoveDest), findsNothing);
// ... but move by drag should work
await tester.dragFrom(
squareOffset('e2'),
const Offset(0, -(squareSize * 2)),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('e4-whitePawn')), findsOneWidget);
expect(find.byKey(const Key('e2-whitePawn')), findsNothing);
expect(find.byKey(const Key('e2-lastMove')), findsOneWidget);
expect(find.byKey(const Key('e4-lastMove')), findsOneWidget);
});
testWidgets('castling by taping king then rook is possible',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen:
'r1bqk1nr/pppp1ppp/2n5/2b1p3/2B1P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 4 4',
initialInteractableSide: InteractableSide.both,
),
);
await tester.tap(find.byKey(const Key('e1-whiteKing')));
await tester.pump();
await tester.tap(find.byKey(const Key('h1-whiteRook')));
await tester.pump();
expect(find.byKey(const Key('e1-whiteKing')), findsNothing);
expect(find.byKey(const Key('h1-whiteRook')), findsNothing);
expect(find.byKey(const Key('g1-whiteKing')), findsOneWidget);
expect(find.byKey(const Key('f1-whiteRook')), findsOneWidget);
expect(find.byKey(const Key('e1-lastMove')), findsOneWidget);
expect(find.byKey(const Key('h1-lastMove')), findsOneWidget);
});
testWidgets('dragging off target', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(initialInteractableSide: InteractableSide.both),
);
final e2 = squareOffset('e2');
await tester.dragFrom(e2, const Offset(0, -(squareSize * 4)));
await tester.pumpAndSettle();
expect(find.byKey(const Key('e2-whitePawn')), findsOneWidget);
expect(find.byKey(const Key('e2-selected')), findsNothing);
expect(find.byType(MoveDest), findsNothing);
});
testWidgets('dragging off board', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(initialInteractableSide: InteractableSide.both),
);
await tester.dragFrom(
squareOffset('e2'),
squareOffset('e2') + const Offset(0, -boardSize + squareSize),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('e2-whitePawn')), findsOneWidget);
expect(find.byKey(const Key('e2-selected')), findsNothing);
expect(find.byType(MoveDest), findsNothing);
});
testWidgets('e2-e4 drag move', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(initialInteractableSide: InteractableSide.both),
);
await tester.dragFrom(
squareOffset('e2'),
const Offset(0, -(squareSize * 2)),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('e4-whitePawn')), findsOneWidget);
expect(find.byKey(const Key('e2-whitePawn')), findsNothing);
expect(find.byKey(const Key('e2-lastMove')), findsOneWidget);
expect(find.byKey(const Key('e4-lastMove')), findsOneWidget);
});
testWidgets('Cannot move by drag if piece shift method is tapTwoSquares', (
WidgetTester tester,
) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
pieceShiftMethod: PieceShiftMethod.tapTwoSquares,
),
);
await tester.dragFrom(
squareOffset('e2'),
const Offset(0, -(squareSize * 2)),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('e4-whitePawn')), findsNothing);
expect(find.byKey(const Key('e2-whitePawn')), findsOneWidget);
expect(find.byKey(const Key('e2-lastMove')), findsNothing);
expect(find.byKey(const Key('e4-lastMove')), findsNothing);
// Original square is still selected after drag attempt
expect(find.byKey(const Key('e2-selected')), findsOneWidget);
expect(find.byType(MoveDest), findsNWidgets(2));
// ...so we can still tap to move
await tester.tapAt(squareOffset('e4'));
await tester.pump();
expect(find.byKey(const Key('e2-selected')), findsNothing);
expect(find.byType(MoveDest), findsNothing);
expect(find.byKey(const Key('e4-whitePawn')), findsOneWidget);
expect(find.byKey(const Key('e2-whitePawn')), findsNothing);
expect(find.byKey(const Key('e2-lastMove')), findsOneWidget);
expect(find.byKey(const Key('e4-lastMove')), findsOneWidget);
});
testWidgets(
'2 simultaneous pointer down events will cancel current drag/selection',
(
WidgetTester tester,
) async {
await tester.pumpWidget(
buildBoard(initialInteractableSide: InteractableSide.both),
);
await TestAsyncUtils.guard<void>(() async {
await tester.startGesture(squareOffset('e2'));
await tester.pump();
expect(find.byKey(const Key('e2-selected')), findsOneWidget);
await tester.startGesture(squareOffset('e4'));
await tester.pump();
// move is cancelled
expect(find.byKey(const Key('e4-whitePawn')), findsNothing);
expect(find.byKey(const Key('e2-whitePawn')), findsOneWidget);
// selection is cancelled
expect(find.byKey(const Key('e2-selected')), findsNothing);
});
});
testWidgets('while dragging a piece, other pointer events will cancel', (
WidgetTester tester,
) async {
await tester.pumpWidget(
buildBoard(initialInteractableSide: InteractableSide.both),
);
// drag a piece and tap on another own square while dragging
await TestAsyncUtils.guard<void>(() async {
final dragGesture = await tester.startGesture(squareOffset('e2'));
await tester.pump();
// trigger a piece drag by moving the pointer by 4 pixels
await dragGesture.moveTo(const Offset(0, -1));
await dragGesture.moveTo(const Offset(0, -1));
await dragGesture.moveTo(const Offset(0, -1));
await dragGesture.moveTo(const Offset(0, -1));
expect(find.byKey(const Key('e2-selected')), findsOneWidget);
await tester.tap(find.byKey(const Key('d2-whitePawn')));
// finish the move as to release the piece
await dragGesture.moveTo(squareOffset('e4'));
await dragGesture.up();
});
await tester.pump();
// the piece should not have moved
expect(find.byKey(const Key('e4-whitePawn')), findsNothing);
expect(find.byKey(const Key('e2-whitePawn')), findsOneWidget);
// the piece should not be selected
expect(find.byKey(const Key('e2-selected')), findsNothing);
// drag a piece and tap on an empty square while dragging
await TestAsyncUtils.guard<void>(() async {
final dragGesture = await tester.startGesture(squareOffset('d2'));
await tester.pump();
// trigger a piece drag by moving the pointer by 4 pixels
await dragGesture.moveTo(const Offset(0, -1));
await dragGesture.moveTo(const Offset(0, -1));
await dragGesture.moveTo(const Offset(0, -1));
await dragGesture.moveTo(const Offset(0, -1));
expect(find.byKey(const Key('d2-selected')), findsOneWidget);
// tap on an empty square
await tester.tapAt(squareOffset('f5'));
// finish the move as to release the piece
await dragGesture.moveTo(squareOffset('d4'));
await dragGesture.up();
});
await tester.pump();
// the piece should not have moved
expect(find.byKey(const Key('d4-whitePawn')), findsNothing);
expect(find.byKey(const Key('d2-whitePawn')), findsOneWidget);
// the piece should not be selected
expect(find.byKey(const Key('d2-selected')), findsNothing);
});
testWidgets('dragging an already selected piece should not deselect it', (
WidgetTester tester,
) async {
await tester.pumpWidget(
buildBoard(initialInteractableSide: InteractableSide.both),
);
final e2 = squareOffset('e2');
await tester.tapAt(e2);
await tester.pump();
final dragFuture = tester.timedDragFrom(
e2,
const Offset(0, -(squareSize * 2)),
const Duration(milliseconds: 200),
);
expectSync(find.byKey(const Key('e2-whitePawn')), findsOneWidget);
expectSync(find.byKey(const Key('e2-selected')), findsOneWidget);
await dragFuture;
await tester.pumpAndSettle();
expectSync(find.byKey(const Key('e2-selected')), findsNothing);
});
testWidgets('promotion', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
initialFen: '8/5P2/2RK2P1/8/4k3/8/8/7r w - - 0 1',
),
);
await tester.tap(find.byKey(const Key('f7-whitePawn')));
await tester.pump();
await tester.tapAt(squareOffset('f8'));
await tester.pump();
await tester.tapAt(squareOffset('f7'));
await tester.pump();
expect(find.byKey(const Key('f8-whiteKnight')), findsOneWidget);
expect(find.byKey(const Key('f7-whitePawn')), findsNothing);
});
testWidgets('promotion, auto queen enabled', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
settings: const BoardSettings(autoQueenPromotion: true),
initialInteractableSide: InteractableSide.both,
initialFen: '8/5P2/2RK2P1/8/4k3/8/8/7r w - - 0 1',
),
);
await tester.tap(find.byKey(const Key('f7-whitePawn')));
await tester.pump();
await tester.tapAt(squareOffset('f8'));
await tester.pump();
expect(find.byKey(const Key('f8-whiteQueen')), findsOneWidget);
expect(find.byKey(const Key('f7-whitePawn')), findsNothing);
});
testWidgets('king check square black', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen:
'rnbqkbnr/ppp2ppp/3p4/4p3/3PP3/8/PPP2PPP/RNBQKBNR w KQkq - 0 3',
initialInteractableSide: InteractableSide.white,
),
);
await makeMove(tester, 'f1', 'b5');
expect(find.byKey(const Key('e8-check')), findsOneWidget);
});
testWidgets('king check square white', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen:
'rnbqkbnr/pppp1ppp/8/4p3/3P4/4P3/PPP2PPP/RNBQKBNR b KQkq - 0 2',
initialInteractableSide: InteractableSide.black,
),
);
await makeMove(tester, 'f8', 'b4');
expect(find.byKey(const Key('e1-check')), findsOneWidget);
});
testWidgets(
'cancel piece selection if board is made non interactable again',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen: dc.kInitialBoardFEN,
initialInteractableSide: InteractableSide.white,
),
);
await tester.tapAt(squareOffset('e2'));
await tester.pump();
expect(find.byKey(const Key('e2-selected')), findsOneWidget);
await tester.pumpWidget(
buildBoard(
initialFen: dc.kInitialBoardFEN,
initialInteractableSide: InteractableSide.none,
),
);
expect(find.byKey(const Key('e2-selected')), findsNothing);
});
testWidgets(
'cancel piece current pointer event if board is made non interactable again',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen: dc.kInitialBoardFEN,
initialInteractableSide: InteractableSide.white,
),
);
await TestAsyncUtils.guard<void>(() async {
await tester.startGesture(squareOffset('e2'));
await tester.pump();
expect(find.byKey(const Key('e2-selected')), findsOneWidget);
});
// make board non interactable in the middle of the gesture
await tester.pumpWidget(
buildBoard(
initialFen: dc.kInitialBoardFEN,
initialInteractableSide: InteractableSide.none,
),
);
expect(find.byKey(const Key('e2-selected')), findsNothing);
// board is not interactable, so the piece should not be selected
await tester.tapAt(squareOffset('e2'));
await tester.pump();
expect(find.byKey(const Key('e2-selected')), findsNothing);
// make board interactable again
await tester.pumpWidget(
buildBoard(
initialFen: dc.kInitialBoardFEN,
initialInteractableSide: InteractableSide.white,
),
);
// the piece selection should work (which would not be the case if the
// pointer event was not cancelled)
await tester.tapAt(squareOffset('e2'));
await tester.pump();
expect(find.byKey(const Key('e2-selected')), findsOneWidget);
});
});
group('premoves', () {
testWidgets('select and deselect with empty square',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen:
'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1',
initialInteractableSide: InteractableSide.white,
),
);
await tester.tapAt(squareOffset('f1'));
await tester.pump();
expect(find.byKey(const Key('f1-selected')), findsOneWidget);
expect(find.byType(MoveDest), findsNWidgets(7));
await tester.tapAt(squareOffset('b4'));
await tester.pump();
expect(find.byKey(const Key('e4-selected')), findsNothing);
expect(find.byType(MoveDest), findsNothing);
});
testWidgets('select and deselect with opponent piece',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen:
'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1',
initialInteractableSide: InteractableSide.white,
),
);
await tester.tapAt(squareOffset('f1'));
await tester.pump();
expect(find.byKey(const Key('f1-selected')), findsOneWidget);
expect(find.byType(MoveDest), findsNWidgets(7));
await tester.tapAt(squareOffset('f8'));
await tester.pump();
expect(find.byKey(const Key('e4-selected')), findsNothing);
expect(find.byType(MoveDest), findsNothing);
});
testWidgets('select and deselect with same piece',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen:
'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1',
initialInteractableSide: InteractableSide.white,
),
);
await tester.tapAt(squareOffset('f1'));
await tester.pump();
expect(find.byKey(const Key('f1-selected')), findsOneWidget);
expect(find.byType(MoveDest), findsNWidgets(7));
await tester.tapAt(squareOffset('f1'));
await tester.pump();
expect(find.byKey(const Key('e4-selected')), findsNothing);
expect(find.byType(MoveDest), findsNothing);
});
testWidgets('set/unset', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen:
'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1',
initialInteractableSide: InteractableSide.white,
),
);
// set premove
await makeMove(tester, 'e4', 'f5');
expect(find.byKey(const Key('e4-premove')), findsOneWidget);
expect(find.byKey(const Key('f5-premove')), findsOneWidget);
// unset by tapping empty square
await tester.tapAt(squareOffset('c5'));
await tester.pump();
expect(find.byKey(const Key('e4-premove')), findsNothing);
expect(find.byKey(const Key('f5-premove')), findsNothing);
// unset by tapping opponent's piece
await makeMove(tester, 'd1', 'f3');
expect(find.byKey(const Key('d1-premove')), findsOneWidget);
expect(find.byKey(const Key('f3-premove')), findsOneWidget);
await tester.tapAt(squareOffset('g8'));
await tester.pump();
expect(find.byKey(const Key('d1-premove')), findsNothing);
expect(find.byKey(const Key('f3-premove')), findsNothing);
});
testWidgets('set and change', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen:
'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1',
initialInteractableSide: InteractableSide.white,
),
);
await makeMove(tester, 'd1', 'f3');
expect(find.byKey(const Key('d1-premove')), findsOneWidget);
expect(find.byKey(const Key('f3-premove')), findsOneWidget);
await tester.tapAt(squareOffset('d2'));
await tester.pump();
// premove is still set
expect(find.byKey(const Key('d1-premove')), findsOneWidget);
expect(find.byKey(const Key('f3-premove')), findsOneWidget);
expect(find.byType(MoveDest), findsNWidgets(4));
await tester.tapAt(squareOffset('d4'));
await tester.pump();
// premove is changed
expect(find.byKey(const Key('d1-premove')), findsNothing);
expect(find.byKey(const Key('f3-premove')), findsNothing);
expect(find.byKey(const Key('d2-premove')), findsOneWidget);
expect(find.byKey(const Key('d4-premove')), findsOneWidget);
});
testWidgets('drag to set', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen:
'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1',
initialInteractableSide: InteractableSide.white,
),
);
await tester.dragFrom(
squareOffset('e4'),
const Offset(0, -squareSize),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('e4-premove')), findsOneWidget);
expect(find.byKey(const Key('e5-premove')), findsOneWidget);
expect(find.byKey(const Key('e4-selected')), findsNothing);
});
testWidgets('select pieces from same side', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialFen:
'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1',
initialInteractableSide: InteractableSide.white,
),
);
await makeMove(tester, 'd1', 'c2');
expect(find.byKey(const Key('d1-premove')), findsOneWidget);
expect(find.byKey(const Key('c2-premove')), findsOneWidget);
});
testWidgets('play premove', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.white,
shouldPlayOpponentMove: true,
),
);
await makeMove(tester, 'e2', 'e4');
await makeMove(tester, 'd1', 'f3');
expect(find.byKey(const Key('d1-premove')), findsOneWidget);
expect(find.byKey(const Key('f3-premove')), findsOneWidget);
// wait for opponent move to be played
await tester.pump(const Duration(milliseconds: 200));
// opponent move is played
expect(find.byKey(const Key('a5-blackPawn')), findsOneWidget);
// wait for the premove to be played
await tester.pumpAndSettle();
expect(find.byKey(const Key('d1-premove')), findsNothing);
expect(find.byKey(const Key('f3-premove')), findsNothing);
// premove has been played
expect(find.byKey(const Key('d1-whiteQueen')), findsNothing);
expect(find.byKey(const Key('f3-whiteQueen')), findsOneWidget);
});
});
group('drawing shapes', () {
testWidgets('preconfigure board to draw a circle',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
initialShapes:
ISet({const Circle(orig: 'e4', color: Color(0xFF0000FF))}),
),
);
expect(
find.byType(ShapeWidget),
paints..path(color: const Color(0xFF0000FF)),
);
});
testWidgets('preconfigure board to draw an arrow',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
initialShapes: ISet({
const Arrow(
orig: 'e2',
dest: 'e4',
color: Color(0xFF0000FF),
),
}),
),
);
expect(
find.byType(ShapeWidget),
paints
..line(color: const Color(0xFF0000FF))
..path(color: const Color(0xFF0000FF)),
);
});
testWidgets('preconfigure board to draw a piece shape',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
initialShapes: ISet({
const PieceShape(
orig: 'e4',
role: Role.pawn,
color: Color(0xFF0000FF),
),
}),
),
);
expect(find.byType(ShapeWidget), findsOneWidget);
});
testWidgets('cannot draw if not enabled', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
),
);
await TestAsyncUtils.guard<void>(() async {
// keep pressing an empty square to enable drawing shapes
final pressGesture = await tester.startGesture(squareOffset('a3'));
// drawing a circle with another tap
final tapGesture = await tester.startGesture(squareOffset('e4'));
await tapGesture.up();
await pressGesture.up();
});
expect(find.byType(ShapeWidget), findsNothing);
});
testWidgets('draw a circle by hand', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
enableDrawingShapes: true,
),
);
await TestAsyncUtils.guard<void>(() async {
// keep pressing an empty square to enable drawing shapes
final pressGesture = await tester.startGesture(squareOffset('a3'));
// drawing a circle with another tap
final tapGesture = await tester.startGesture(squareOffset('e4'));
await tapGesture.up();
await pressGesture.up();
});
// wait for the double tap delay to expire
await tester.pump(const Duration(milliseconds: 210));
expect(
find.byType(ShapeWidget),
paints..path(color: const Color(0xFF0000FF)),
);
});
testWidgets('draw an arrow by hand', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
enableDrawingShapes: true,
),
);
// keep pressing an empty square to enable drawing shapes
final pressGesture = await tester.startGesture(squareOffset('a3'));
await tester.dragFrom(
squareOffset('e2'),
const Offset(0, -(squareSize * 2)),
);
await pressGesture.up();
// wait for the double tap delay to expire
await tester.pump(const Duration(milliseconds: 210));
expect(
find.byType(ShapeWidget),
paints
..line(color: const Color(0xFF0000FF))
..path(color: const Color(0xFF0000FF)),
);
});
testWidgets('can draw shapes on an non-interactable board',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.none,
enableDrawingShapes: true,
),
);
// keep pressing an empty square to enable drawing shapes
final pressGesture = await tester.startGesture(squareOffset('a3'));
await tester.dragFrom(
squareOffset('e2'),
const Offset(0, -(squareSize * 2)),
);
await pressGesture.up();
// wait for the double tap delay to expire
await tester.pump(const Duration(milliseconds: 210));
expect(
find.byType(ShapeWidget),
paints
..line(color: const Color(0xFF0000FF))
..path(color: const Color(0xFF0000FF)),
);
});
testWidgets('double tap to clear shapes', (WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
enableDrawingShapes: true,
),
);
await TestAsyncUtils.guard<void>(() async {
// keep pressing an empty square to enable drawing shapes
final pressGesture = await tester.startGesture(squareOffset('a3'));
// drawing a circle with another tap
final tapGesture = await tester.startGesture(squareOffset('e4'));
await tapGesture.up();
await pressGesture.up();
});
// wait for the double tap delay to expire
await tester.pump(const Duration(milliseconds: 210));
// keep pressing an empty square to enable drawing shapes
final pressGesture = await tester.startGesture(squareOffset('a3'));
await tester.dragFrom(
squareOffset('e2'),
const Offset(0, -(squareSize * 2)),
);
await pressGesture.up();
// wait for the double tap delay to expire
await tester.pump(const Duration(milliseconds: 210));
expect(find.byType(ShapeWidget), findsNWidgets(2));
await tester.tapAt(squareOffset('a3'));
await tester.tapAt(squareOffset('a3'));
await tester.pump();
expect(find.byType(ShapeWidget), findsNothing);
});
testWidgets('selecting one piece should clear shapes',
(WidgetTester tester) async {
await tester.pumpWidget(
buildBoard(
initialInteractableSide: InteractableSide.both,
enableDrawingShapes: true,
),
);
await TestAsyncUtils.guard<void>(() async {
// keep pressing an empty square to enable drawing shapes
final pressGesture = await tester.startGesture(squareOffset('a3'));
// drawing a circle with another tap
final tapGesture = await tester.startGesture(squareOffset('e4'));
await tapGesture.up();
await pressGesture.up();
});
// wait for the double tap delay to expire
await tester.pump(const Duration(milliseconds: 210));
expect(find.byType(ShapeWidget), findsOneWidget);
await tester.tapAt(squareOffset('e2'));
await tester.pump();
expect(find.byType(ShapeWidget), findsNothing);
});
});
}
Future<void> makeMove(WidgetTester tester, String from, String to) async {
await tester.tapAt(squareOffset(from));
await tester.pump();
await tester.tapAt(squareOffset(to));
await tester.pump();
}
Widget buildBoard({
required InteractableSide initialInteractableSide,
BoardSettings? settings,
Side orientation = Side.white,
String initialFen = dc.kInitialFEN,
ISet<Shape>? initialShapes,
bool enableDrawingShapes = false,
PieceShiftMethod pieceShiftMethod = PieceShiftMethod.either,
/// play the first available move for the opponent after a delay of 200ms
bool shouldPlayOpponentMove = false,
}) {
InteractableSide interactableSide = initialInteractableSide;
dc.Position<dc.Chess> position =
dc.Chess.fromSetup(dc.Setup.parseFen(initialFen));
Move? lastMove;
Move? premove;
ISet<Shape> shapes = initialShapes ?? ISet();
return MaterialApp(
home: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
final defaultSettings = BoardSettings(
drawShape: DrawShapeOptions(
enable: enableDrawingShapes,
onCompleteShape: (shape) {
setState(() => shapes = shapes.add(shape));
},
onClearShapes: () {
setState(() => shapes = ISet());
},
newShapeColor: const Color(0xFF0000FF),
),
pieceShiftMethod: pieceShiftMethod,
);
return Board(
size: boardSize,
settings: settings ?? defaultSettings,
data: BoardData(
interactableSide: interactableSide,
orientation: orientation,
fen: position.fen,
lastMove: lastMove,
isCheck: position.isCheck,
sideToMove:
position.turn == dc.Side.white ? Side.white : Side.black,
validMoves: dc.algebraicLegalMoves(position),
premove: premove,
shapes: shapes,
),
onMove: (Move move, {bool? isDrop, bool? isPremove}) {
setState(() {
position = position.playUnchecked(dc.Move.fromUci(move.uci)!);
if (position.isGameOver) {
interactableSide = InteractableSide.none;
}
lastMove = move;
});
if (shouldPlayOpponentMove) {
Timer(const Duration(milliseconds: 200), () {
setState(() {
final allMoves = [
for (final entry in position.legalMoves.entries)
for (final dest in entry.value.squares)
dc.NormalMove(from: entry.key, to: dest),
];
final opponentMove = allMoves.first;
position = position.playUnchecked(opponentMove);
if (position.isGameOver) {
interactableSide = InteractableSide.none;
}
lastMove = Move.fromUci(opponentMove.uci);
});
});
}
},
onPremove: (Move? move) {
setState(() {
premove = move;
});
},
);
},