diff --git a/lib/src/tilt.dart b/lib/src/tilt.dart index c399c48..6ae1648 100644 --- a/lib/src/tilt.dart +++ b/lib/src/tilt.dart @@ -36,6 +36,8 @@ class Tilt extends TiltContainer { }); /// Tilt Stream Controller + /// + /// StreamController.broadcast() final async.StreamController? tiltStreamController; /// 全部禁用 diff --git a/test/tilt_widget/tilt_stream_controller_test.dart b/test/tilt_widget/tilt_stream_controller_test.dart new file mode 100644 index 0000000..cd90c81 --- /dev/null +++ b/test/tilt_widget/tilt_stream_controller_test.dart @@ -0,0 +1,392 @@ +import 'dart:async'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter_tilt/flutter_tilt.dart'; +import 'tilt_widget.dart'; + +void main() { + final Finder childFinder = find.text('Tilt'); + group('tilt TiltStreamController', () { + testWidgets('stream listen', (WidgetTester tester) async { + TiltStreamModel tiltStreamModelTest = const TiltStreamModel( + position: Offset(1, 1), + gesturesType: GesturesType.touch, + gestureUse: false, + ); + const TiltStreamModel tiltStreamModelExpect = TiltStreamModel( + position: Offset.zero, + gesturesType: GesturesType.controller, + gestureUse: true, + ); + + /// 基础 + final StreamController tiltStreamController = + StreamController.broadcast(); + await tester.pumpWidget( + TiltWidget(tiltStreamController: tiltStreamController), + ); + await tester.pumpAndSettle(); + expect(childFinder, findsOneWidget); + + /// 测试值不同 + expect(tiltStreamModelTest != tiltStreamModelExpect, true); + + await tester.pumpAndSettle(); + + /// 监听 + tiltStreamController.stream.listen((TiltStreamModel tiltStreamModel) { + tiltStreamModelTest = tiltStreamModel; + }); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + + /// 测试值相同 + expect(tiltStreamModelTest, tiltStreamModelExpect); + }); + + testWidgets('disable all gestures and use controller triggers', ( + WidgetTester tester, + ) async { + TiltStreamModel tiltStreamModelTest = const TiltStreamModel( + position: Offset(1, 1), + gesturesType: GesturesType.controller, + gestureUse: true, + ); + TiltStreamModel tiltStreamModelExpect = const TiltStreamModel( + position: Offset.zero, + gesturesType: GesturesType.controller, + gestureUse: false, + ); + TiltStreamModel? gestureMoveExpect; + TiltStreamModel? gestureLeaveExpect; + + /// 基础 回调赋值 + final StreamController tiltStreamController = + StreamController.broadcast(); + await tester.pumpWidget( + TiltWidget( + tiltStreamController: tiltStreamController, + tiltConfig: const TiltConfig( + enableGestureTouch: false, + enableGestureHover: false, + enableGestureSensors: false, + ), + onGestureMove: ( + TiltDataModel tiltDataModel, + GesturesType gesturesType, + ) { + gestureMoveExpect = TiltStreamModel( + position: tiltDataModel.position, + gesturesType: gesturesType, + gestureUse: true, + ); + }, + onGestureLeave: ( + TiltDataModel tiltDataModel, + GesturesType gesturesType, + ) { + gestureLeaveExpect = TiltStreamModel( + position: tiltDataModel.position, + gesturesType: gesturesType, + gestureUse: false, + ); + }, + ), + ); + await tester.pumpAndSettle(); + expect(childFinder, findsOneWidget); + expect(tiltStreamModelTest != tiltStreamModelExpect, true); + + /// stream 监听 + tiltStreamController.stream.listen((TiltStreamModel tiltStreamModel) { + tiltStreamModelTest = tiltStreamModel; + }); + + /// controller touch 手势移动 + gestureMoveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.touch, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect(tiltStreamModelTest, gestureMoveExpect); + + /// controller touch 手势离开 + gestureLeaveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.touch, + gestureUse: false, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect( + const TiltStreamModel( + position: Offset(5, 5), + gesturesType: GesturesType.touch, + gestureUse: false, + ), + gestureLeaveExpect, + ); + + /// controller hover 手势移动 + gestureMoveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.hover, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect(tiltStreamModelTest, gestureMoveExpect); + + /// controller hover 手势离开 + gestureLeaveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.hover, + gestureUse: false, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect( + gestureLeaveExpect, + const TiltStreamModel( + position: Offset(5, 5), + gesturesType: GesturesType.hover, + gestureUse: false, + ), + ); + + /// controller controller 手势移动 + gestureMoveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.controller, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect(tiltStreamModelTest, gestureMoveExpect); + + /// controller controller 手势离开 + gestureLeaveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.controller, + gestureUse: false, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect( + gestureLeaveExpect, + const TiltStreamModel( + position: Offset(5, 5), + gesturesType: GesturesType.controller, + gestureUse: false, + ), + ); + + /// controller sensors 手势移动 + gestureMoveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.sensors, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect(tiltStreamModelTest, gestureMoveExpect); + + /// controller sensors 手势还原 + gestureLeaveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.sensors, + gestureUse: false, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + // sensors 不会触发 onGestureLeave + expect(gestureLeaveExpect, null); + // 触发 sensors 为 0,让组件还原一点 + tiltStreamModelExpect = const TiltStreamModel( + position: Offset.zero, + gesturesType: GesturesType.sensors, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect( + gestureMoveExpect, + const TiltStreamModel( + position: Offset(9.75, 9.75), + gesturesType: GesturesType.sensors, + gestureUse: true, + ), + ); + }); + + testWidgets('gesture priority', ( + WidgetTester tester, + ) async { + TiltStreamModel tiltStreamModelTest = const TiltStreamModel( + position: Offset(1, 1), + gesturesType: GesturesType.controller, + gestureUse: true, + ); + TiltStreamModel tiltStreamModelExpect = const TiltStreamModel( + position: Offset.zero, + gesturesType: GesturesType.controller, + gestureUse: false, + ); + TiltStreamModel? gestureMoveExpect; + TiltStreamModel? gestureLeaveExpect; + + /// 基础 回调赋值 + final StreamController tiltStreamController = + StreamController.broadcast(); + await tester.pumpWidget( + TiltWidget( + tiltStreamController: tiltStreamController, + onGestureMove: ( + TiltDataModel tiltDataModel, + GesturesType gesturesType, + ) { + gestureMoveExpect = TiltStreamModel( + position: tiltDataModel.position, + gesturesType: gesturesType, + gestureUse: true, + ); + }, + onGestureLeave: ( + TiltDataModel tiltDataModel, + GesturesType gesturesType, + ) { + gestureLeaveExpect = TiltStreamModel( + position: tiltDataModel.position, + gesturesType: gesturesType, + gestureUse: false, + ); + }, + ), + ); + await tester.pumpAndSettle(); + expect(childFinder, findsOneWidget); + expect(tiltStreamModelTest != tiltStreamModelExpect, true); + + /// stream 监听 + tiltStreamController.stream.listen((TiltStreamModel tiltStreamModel) { + tiltStreamModelTest = tiltStreamModel; + }); + + /// sensors 手势移动 + gestureMoveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.sensors, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect(tiltStreamModelTest, gestureMoveExpect); + + /// controller 手势移动 + gestureMoveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.controller, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect(tiltStreamModelTest, gestureMoveExpect); + + /// hover 手势移动 + gestureMoveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.hover, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect(tiltStreamModelTest, gestureMoveExpect); + + /// touch 手势移动 + gestureMoveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.touch, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect(tiltStreamModelTest, gestureMoveExpect); + + /// 低优先级判断 + const TiltStreamModel lowPriorityData = TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.touch, + gestureUse: true, + ); + + /// hover 手势移动 + gestureMoveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.hover, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(gestureMoveExpect, lowPriorityData); + + /// touch 手势离开 + gestureMoveExpect = null; + gestureLeaveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.touch, + gestureUse: false, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect(gestureMoveExpect, null); + expect( + gestureLeaveExpect, + const TiltStreamModel( + position: Offset(5, 5), + gesturesType: GesturesType.touch, + gestureUse: false, + ), + ); + + /// hover 手势移动 + gestureMoveExpect = null; + tiltStreamModelExpect = const TiltStreamModel( + position: Offset(10, 10), + gesturesType: GesturesType.hover, + gestureUse: true, + ); + tiltStreamController.sink.add(tiltStreamModelExpect); + await tester.pumpAndSettle(); + expect(tiltStreamModelTest, tiltStreamModelExpect); + expect(tiltStreamModelTest, gestureMoveExpect); + }); + }); +} diff --git a/test/tilt_widget/tilt_widget.dart b/test/tilt_widget/tilt_widget.dart index 1d30da1..add4dfb 100644 --- a/test/tilt_widget/tilt_widget.dart +++ b/test/tilt_widget/tilt_widget.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:flutter_tilt/flutter_tilt.dart'; @@ -5,6 +7,7 @@ class TiltWidget extends StatelessWidget { const TiltWidget({ super.key, this.childLayout = const ChildLayout(), + this.tiltStreamController, this.disable = false, this.fps = 60, this.border, @@ -18,6 +21,7 @@ class TiltWidget extends StatelessWidget { }); final ChildLayout childLayout; + final StreamController? tiltStreamController; final bool disable; final int fps; final BoxBorder? border; @@ -37,6 +41,7 @@ class TiltWidget extends StatelessWidget { body: Tilt( key: const Key('tilt_widget'), childLayout: childLayout, + tiltStreamController: tiltStreamController, disable: disable, fps: fps, border: border,