mirror of
https://github.com/chenasraf/flame_ui.git
synced 2026-05-18 01:39:01 +00:00
225 lines
6.1 KiB
Dart
225 lines
6.1 KiB
Dart
import 'package:flame/components.dart';
|
|
import 'package:flame/game.dart';
|
|
import 'package:flame_test/flame_test.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:flame_ui/flame_ui.dart';
|
|
|
|
void main() {
|
|
group('ModalComponent', () {
|
|
testWithFlameGame('stores constructor parameters', (game) async {
|
|
final content = RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
);
|
|
|
|
final modal = ModalComponent(
|
|
scrollContent: content,
|
|
size: Vector2(200, 200),
|
|
position: Vector2(50, 50),
|
|
title: 'Test Modal',
|
|
padding: const EdgeInsets.all(16),
|
|
titleSpacing: 8,
|
|
);
|
|
|
|
await game.ensureAdd(modal);
|
|
|
|
expect(modal.size, Vector2(200, 200));
|
|
expect(modal.position, Vector2(50, 50));
|
|
expect(modal.title, 'Test Modal');
|
|
expect(modal.padding, const EdgeInsets.all(16));
|
|
expect(modal.titleSpacing, 8);
|
|
});
|
|
|
|
testWithFlameGame('default padding is 8', (game) async {
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
);
|
|
|
|
expect(modal.padding, const EdgeInsets.all(8));
|
|
});
|
|
|
|
testWithFlameGame('default titleSpacing is 2', (game) async {
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
);
|
|
|
|
expect(modal.titleSpacing, 2);
|
|
});
|
|
|
|
testWithFlameGame('creates title component when title is set', (
|
|
game,
|
|
) async {
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
title: 'Hello',
|
|
);
|
|
|
|
await game.ensureAdd(modal);
|
|
|
|
expect(modal.titleComponent, isNotNull);
|
|
expect(modal.titleComponent!.text, 'Hello');
|
|
});
|
|
|
|
testWithFlameGame('titleComponent is null when no title', (game) async {
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
);
|
|
|
|
await game.ensureAdd(modal);
|
|
|
|
expect(modal.titleComponent, isNull);
|
|
});
|
|
|
|
testWithFlameGame('creates scrollArea on load', (game) async {
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
);
|
|
|
|
await game.ensureAdd(modal);
|
|
|
|
expect(modal.scrollArea, isA<ScrollableAreaComponent>());
|
|
});
|
|
|
|
testWithFlameGame('autoContentHeight defaults to true', (game) async {
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
);
|
|
|
|
expect(modal.autoContentHeight, isTrue);
|
|
});
|
|
|
|
testWithFlameGame('default scrollDamping is 500', (game) async {
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
);
|
|
|
|
expect(modal.scrollDamping, 500.0);
|
|
});
|
|
|
|
testWithFlameGame('defaultFooterHeight defaults to 32', (game) async {
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
);
|
|
|
|
expect(modal.defaultFooterHeight, 32);
|
|
});
|
|
|
|
testWithFlameGame('onAfterLoad callback is invoked', (game) async {
|
|
var called = false;
|
|
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
onAfterLoad: () => called = true,
|
|
);
|
|
|
|
await game.ensureAdd(modal);
|
|
|
|
expect(called, isTrue);
|
|
});
|
|
|
|
testWithFlameGame('title setter triggers rebuild', (game) async {
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
);
|
|
|
|
await game.ensureAdd(modal);
|
|
|
|
expect(modal.titleComponent, isNull);
|
|
|
|
modal.title = 'New Title';
|
|
// rebuild is called, but we need to wait for it to process
|
|
await game.ready();
|
|
|
|
expect(modal.title, 'New Title');
|
|
});
|
|
|
|
testWithFlameGame('creates background on load', (game) async {
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
);
|
|
|
|
await game.ensureAdd(modal);
|
|
|
|
expect(modal.background, isA<RectangleComponent>());
|
|
});
|
|
|
|
testWithFlameGame('uses custom background', (game) async {
|
|
final customBg = RectangleComponent(
|
|
size: Vector2(200, 200),
|
|
paint: Paint()..color = Colors.purple,
|
|
);
|
|
|
|
final modal = ModalComponent(
|
|
scrollContent: RectangleComponent(
|
|
size: Vector2(100, 100),
|
|
paint: Paint()..color = Colors.white,
|
|
),
|
|
size: Vector2(200, 200),
|
|
position: Vector2.zero(),
|
|
background: customBg,
|
|
);
|
|
|
|
await game.ensureAdd(modal);
|
|
|
|
expect(modal.background, customBg);
|
|
});
|
|
});
|
|
}
|