Skip to content

Commit 86416d4

Browse files
committed
add tests for crazyhouse in analysis and games
1 parent 5536075 commit 86416d4

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

lib/src/widgets/pockets.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class _Pocket extends StatelessWidget {
9999
return IgnorePointer(
100100
ignoring: !interactive || count == 0,
101101
child: Draggable(
102+
key: ValueKey('pocket-${side.name}${role.name}'),
102103
data: Piece(role: role, color: side),
103104
feedback: RotatedBox(
104105
quarterTurns: isUpsideDown ? 2 : 0,

test/test_helpers.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ Future<void> playMove(
111111
await tester.pump();
112112
}
113113

114+
/// Plays a drop move on the board.
115+
Future<void> playDropMove(
116+
WidgetTester tester,
117+
Side side,
118+
Role role,
119+
String to, {
120+
Rect? boardRect,
121+
Side orientation = Side.white,
122+
}) async {
123+
final rect = boardRect ?? tester.getRect(find.byType(Chessboard));
124+
final fromOffset = tester.getCenter(find.byKey(ValueKey('pocket-${side.name}${role.name}')));
125+
await tester.dragFrom(
126+
fromOffset,
127+
squareOffset(Square.fromName(to), rect, orientation: orientation) - fromOffset,
128+
);
129+
await tester.pumpAndSettle();
130+
}
131+
114132
// --
115133

116134
class _SameRequest extends Matcher {

test/view/analysis/analysis_screen_test.dart

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import 'package:lichess_mobile/src/view/engine/engine_lines.dart';
2727
import 'package:lichess_mobile/src/view/more/more_tab_screen.dart';
2828
import 'package:lichess_mobile/src/widgets/bottom_bar.dart';
2929
import 'package:lichess_mobile/src/widgets/pgn.dart';
30+
import 'package:lichess_mobile/src/widgets/pockets.dart';
3031
import 'package:multistockfish/multistockfish.dart';
3132

3233
import '../../model/engine/fake_stockfish.dart';
@@ -127,6 +128,8 @@ void main() {
127128

128129
await tester.pumpWidget(app);
129130

131+
expect(find.byType(PocketsMenu), findsNothing);
132+
130133
await tester.tap(find.bySemanticsLabel('Menu'));
131134
await tester.pumpAndSettle(); // wait for menu to open
132135

@@ -146,16 +149,62 @@ void main() {
146149
expect(find.textContaining('Atomic'), findsNothing);
147150
expect(find.textContaining('Horde'), findsOneWidget);
148151
expect(find.textContaining('Racing Kings'), findsOneWidget);
149-
expect(find.textContaining('Crazyhouse'), findsNothing);
152+
expect(find.textContaining('Crazyhouse'), findsOneWidget);
150153

151154
await tester.tap(find.textContaining('Horde'));
152155
await tester.pumpAndSettle(); // wait for dialog to close and new variant to be loaded
153156

157+
expect(find.byType(PocketsMenu), findsNothing);
158+
154159
// Horde starting position should be loaded:
155160
expect(find.byKey(const ValueKey('b5-whitepawn')), findsOneWidget);
161+
162+
// Change to crazhouse, pockets should be displayed:
163+
await tester.tap(find.bySemanticsLabel('Menu'));
164+
await tester.pumpAndSettle(); // wait for menu to open
165+
await tester.tap(find.text('Change variant'));
166+
await tester.pumpAndSettle(); // wait for dialog to open
167+
await tester.tap(find.textContaining('Crazyhouse'));
168+
await tester.pumpAndSettle(); // wait for dialog to close and new variant to be loaded
169+
170+
// One for white, one for black
171+
expect(find.byType(PocketsMenu), findsNWidgets(2));
156172
}
157173
});
158174
}
175+
176+
testWidgets('Crazyhouse support DropMoves for both sides', (tester) async {
177+
final app = await makeTestProviderScopeApp(
178+
tester,
179+
home: const AnalysisScreen(
180+
options: AnalysisOptions.standalone(
181+
id: StringId('standalone'),
182+
orientation: Side.white,
183+
pgn: '''
184+
[Variant "Crazyhouse"]
185+
[FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR[] w KQkq - 0 1"]
186+
1. e4 d5 2. exd5 Qxd5
187+
''',
188+
isComputerAnalysisAllowed: false,
189+
variant: Variant.crazyhouse,
190+
),
191+
),
192+
);
193+
194+
await tester.pumpWidget(app);
195+
196+
expect(find.byType(PocketsMenu), findsNWidgets(2));
197+
198+
await playDropMove(tester, Side.white, Role.pawn, 'a4');
199+
expect(find.byKey(const ValueKey('a4-whitepawn')), findsOneWidget);
200+
201+
// Illegal drop move for black, should not be played
202+
await playDropMove(tester, Side.black, Role.queen, 'h5');
203+
expect(find.byKey(const ValueKey('h5-blackqueen')), findsNothing);
204+
205+
await playDropMove(tester, Side.black, Role.pawn, 'h5');
206+
expect(find.byKey(const ValueKey('h5-blackpawn')), findsOneWidget);
207+
});
159208
});
160209

161210
group('Analysis Tree View', () {

test/widgets/game_layout_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:lichess_mobile/src/constants.dart';
88
import 'package:lichess_mobile/src/model/common/chess.dart';
99
import 'package:lichess_mobile/src/model/game/game_board_params.dart';
1010
import 'package:lichess_mobile/src/widgets/game_layout.dart';
11+
import 'package:lichess_mobile/src/widgets/pockets.dart';
1112

1213
import '../test_helpers.dart';
1314
import '../test_provider_scope.dart';
@@ -137,4 +138,38 @@ void main() {
137138
}
138139
}
139140
}, variant: kPlatformVariant);
141+
142+
testWidgets('Crazyhouse displays pockets and supports drop moves', (WidgetTester tester) async {
143+
final playedMoves = <Move>[];
144+
final app = await makeTestProviderScope(
145+
tester,
146+
child: MaterialApp(
147+
home: GameLayout(
148+
orientation: Side.white,
149+
boardParams: GameBoardParams.interactive(
150+
variant: Variant.crazyhouse,
151+
position: Crazyhouse.fromSetup(
152+
Setup.parseFen('rnb1kbnr/ppp1pppp/8/3q4/8/8/PPPP1PPP/RNBQKBNR[Pp] w KQkq - 0 3'),
153+
),
154+
playerSide: PlayerSide.white,
155+
onMove: (move, {viaDragAndDrop}) {
156+
playedMoves.add(move);
157+
},
158+
onPromotionSelection: (_) {},
159+
premovable: null,
160+
promotionMove: null,
161+
),
162+
),
163+
),
164+
);
165+
await tester.pumpWidget(app);
166+
167+
expect(find.byType(PocketsMenu), findsNWidgets(2));
168+
169+
// Only the pockets of the player side should be interactive.
170+
await playDropMove(tester, Side.white, Role.pawn, 'a4');
171+
await playDropMove(tester, Side.black, Role.pawn, 'a3');
172+
173+
expect(playedMoves, [const DropMove(to: Square.a4, role: Role.pawn)]);
174+
});
140175
}

0 commit comments

Comments
 (0)