Files
openchamber-android-native/test/pet_layer_test.dart
T
blumia d25167de58 fix: float the pet over the messages and let it be dragged
The pet used to sit in the column above the composer, reserving a strip of
its own: the message list ended there, which read as an opaque band cutting
off the last row. It is now a layer over the message area, so the list keeps
its full height and shows through around the sprite. Only the body of the
sheet takes touches (contentBox, 4dp out); taps and drags on the transparent
canvas still reach the messages underneath.

Dragging the body moves the pet along the composer's top edge (or the
activity cap's) with a small lift while held and the pack's drag reaction.
The spot is stored as a fraction of the width in pet.x.v1, so a restart or a
change in width puts it back in the same place. Taps now react on tap-up so a
drag does not fire a left/right reaction.

0.4.1 (401).
2026-11-22 14:30:30 +08:00

123 lines
3.7 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:openchamber/pets/pet_library.dart';
import 'package:openchamber/pets/pet_pack.dart';
import 'package:openchamber/widgets/pet_sprite.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final library = PetLibrary.instance;
setUp(() async {
SharedPreferences.setMockInitialValues({});
library.pack = PetPack.fromJson(
jsonDecode(File('test/fixtures/deepy-pack.json').readAsStringSync())
as Map<String, dynamic>,
);
library.enabled = true;
library.size = 52;
library.position = 0.5;
});
tearDown(() {
library.pack = null;
library.enabled = false;
library.position = 0.5;
});
/// The chat's shape: a message area with the pet floating over it and the
/// composer below.
Future<int Function()> pumpChat(WidgetTester tester) async {
var taps = 0;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Column(
children: [
Expanded(
child: Stack(
children: [
Positioned.fill(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => taps++,
child: const ColoredBox(color: Color(0xFF101014)),
),
),
const Positioned.fill(child: PetLayer(state: 'idle')),
],
),
),
const SizedBox(height: 80),
],
),
),
),
);
await tester.pump();
return () => taps;
}
testWidgets('the pet floats over the messages and only its body takes taps',
(tester) async {
final taps = await pumpChat(tester);
expect(find.byType(PetSprite), findsOneWidget);
final pet = tester.getCenter(find.byType(PetSprite));
// The transparent part of the layer is not a hit target: the message
// area still receives the tap.
await tester.tapAt(Offset(pet.dx - 160, pet.dy));
await tester.pump();
expect(taps(), 1);
// The body is: tapping it reacts instead of reaching the page.
await tester.tapAt(pet);
await tester.pump();
expect(taps(), 1);
// Nothing reserved in the layout: the message area runs under the pet, so
// the pet's box reaches the composer's top edge.
final layer = tester.getRect(find.byType(PetLayer));
final sprite = tester.getRect(find.byType(PetSprite));
expect(layer.bottom, closeTo(sprite.bottom - 10, 0.5));
expect(sprite.bottom, greaterThanOrEqualTo(layer.bottom));
await tester.pumpWidget(const SizedBox());
});
testWidgets('dragging the pet moves it and keeps the spot', (tester) async {
await pumpChat(tester);
final before = tester.getTopLeft(find.byType(PetSprite)).dx;
await tester.drag(find.byType(PetSprite), const Offset(80, 0));
await tester.pump();
final after = tester.getTopLeft(find.byType(PetSprite)).dx;
expect(after, greaterThan(before + 50));
expect(library.position, greaterThan(0.5));
final prefs = await SharedPreferences.getInstance();
expect(prefs.getDouble('pet.x.v1'), library.position);
await tester.pumpWidget(const SizedBox());
});
testWidgets('nothing is drawn without an imported, enabled pet',
(tester) async {
library.enabled = false;
final taps = await pumpChat(tester);
expect(find.byType(PetSprite), findsNothing);
await tester.tapAt(const Offset(100, 100));
await tester.pump();
expect(taps(), 1);
await tester.pumpWidget(const SizedBox());
});
}