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, ); 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 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()); }); }