Showing
19 changed files
with
665 additions
and
26 deletions
| @@ -232,7 +232,7 @@ class Get { | @@ -232,7 +232,7 @@ class Get { | ||
| 232 | /// Api from showGeneralDialog with no context | 232 | /// Api from showGeneralDialog with no context |
| 233 | static Future<T> generalDialog<T>({ | 233 | static Future<T> generalDialog<T>({ |
| 234 | @required RoutePageBuilder pageBuilder, | 234 | @required RoutePageBuilder pageBuilder, |
| 235 | - String barrierLabel, | 235 | + String barrierLabel = "Dismiss", |
| 236 | bool barrierDismissible = true, | 236 | bool barrierDismissible = true, |
| 237 | Color barrierColor = const Color(0x80000000), | 237 | Color barrierColor = const Color(0x80000000), |
| 238 | Duration transitionDuration = const Duration(milliseconds: 200), | 238 | Duration transitionDuration = const Duration(milliseconds: 200), |
| @@ -643,11 +643,16 @@ class Get { | @@ -643,11 +643,16 @@ class Get { | ||
| 643 | 643 | ||
| 644 | Map<dynamic, _FcBuilderFunc> _factory = {}; | 644 | Map<dynamic, _FcBuilderFunc> _factory = {}; |
| 645 | 645 | ||
| 646 | - static void lazyPut<S>(_FcBuilderFunc<S> builder, {String tag}) { | 646 | + static void lazyPut<S>(_FcBuilderFunc builder, {String tag}) { |
| 647 | String key = _getKey(S, tag); | 647 | String key = _getKey(S, tag); |
| 648 | Get()._factory.putIfAbsent(key, () => builder); | 648 | Get()._factory.putIfAbsent(key, () => builder); |
| 649 | } | 649 | } |
| 650 | 650 | ||
| 651 | + static Future<S> putAsync<S>(_FcBuilderFuncAsync<S> builder, | ||
| 652 | + {String tag}) async { | ||
| 653 | + return Get.put<S>(await builder(), tag: tag); | ||
| 654 | + } | ||
| 655 | + | ||
| 651 | /// Inject class on Get Instance Manager | 656 | /// Inject class on Get Instance Manager |
| 652 | static S put<S>( | 657 | static S put<S>( |
| 653 | S dependency, { | 658 | S dependency, { |
| @@ -966,3 +971,5 @@ enum SmartManagement { | @@ -966,3 +971,5 @@ enum SmartManagement { | ||
| 966 | } | 971 | } |
| 967 | 972 | ||
| 968 | typedef _FcBuilderFunc<S> = S Function(); | 973 | typedef _FcBuilderFunc<S> = S Function(); |
| 974 | + | ||
| 975 | +typedef _FcBuilderFuncAsync<S> = Future<S> Function(); |
| @@ -175,6 +175,7 @@ class GetMaterialApp extends StatelessWidget { | @@ -175,6 +175,7 @@ class GetMaterialApp extends StatelessWidget { | ||
| 175 | 175 | ||
| 176 | @override | 176 | @override |
| 177 | Widget build(BuildContext context) { | 177 | Widget build(BuildContext context) { |
| 178 | + | ||
| 178 | return GetBuilder<GetMaterialController>( | 179 | return GetBuilder<GetMaterialController>( |
| 179 | init: Get().getController, | 180 | init: Get().getController, |
| 180 | dispose: (d) { | 181 | dispose: (d) { |
| @@ -90,8 +90,6 @@ class GetObserver extends NavigatorObserver { | @@ -90,8 +90,6 @@ class GetObserver extends NavigatorObserver { | ||
| 90 | print("[CLOSE BOTTOMSHEET] ${route?.settings?.name}"); | 90 | print("[CLOSE BOTTOMSHEET] ${route?.settings?.name}"); |
| 91 | } else if ('${route?.settings?.name}' == 'dialog') { | 91 | } else if ('${route?.settings?.name}' == 'dialog') { |
| 92 | if (Get.isLogEnable) print("[CLOSE DIALOG] ${route?.settings?.name}"); | 92 | if (Get.isLogEnable) print("[CLOSE DIALOG] ${route?.settings?.name}"); |
| 93 | - } else if ('${route?.settings?.name}' == 'snackbar') { | ||
| 94 | - if (Get.isLogEnable) print("[CLOSE SNACKBAR] ${route?.settings?.name}"); | ||
| 95 | } else { | 93 | } else { |
| 96 | if (Get.isLogEnable) print("[BACK ROUTE] ${route?.settings?.name}"); | 94 | if (Get.isLogEnable) print("[BACK ROUTE] ${route?.settings?.name}"); |
| 97 | } | 95 | } |
| @@ -2,15 +2,20 @@ import 'package:get/get.dart'; | @@ -2,15 +2,20 @@ import 'package:get/get.dart'; | ||
| 2 | import 'rx_interface.dart'; | 2 | import 'rx_interface.dart'; |
| 3 | import 'utils/debouncer.dart'; | 3 | import 'utils/debouncer.dart'; |
| 4 | 4 | ||
| 5 | -void ever(RxInterface listener, Function(dynamic) callback) { | 5 | +void ever(RxInterface listener, Function(dynamic) callback, |
| 6 | + {bool condition = true}) { | ||
| 6 | listener.subject.stream.listen((event) { | 7 | listener.subject.stream.listen((event) { |
| 7 | - callback(event.$new); | 8 | + if (condition) { |
| 9 | + callback(event.$new); | ||
| 10 | + } | ||
| 8 | }); | 11 | }); |
| 9 | } | 12 | } |
| 10 | 13 | ||
| 11 | -void once(RxInterface listener, Function(dynamic) callback) { | 14 | +void once(RxInterface listener, Function(dynamic) callback, |
| 15 | + {bool condition = true}) { | ||
| 12 | int times = 0; | 16 | int times = 0; |
| 13 | listener.subject.stream.listen((event) { | 17 | listener.subject.stream.listen((event) { |
| 18 | + if (!condition) return null; | ||
| 14 | times++; | 19 | times++; |
| 15 | if (times < 2) { | 20 | if (times < 2) { |
| 16 | callback(event.$new); | 21 | callback(event.$new); |
| @@ -19,14 +24,12 @@ void once(RxInterface listener, Function(dynamic) callback) { | @@ -19,14 +24,12 @@ void once(RxInterface listener, Function(dynamic) callback) { | ||
| 19 | } | 24 | } |
| 20 | 25 | ||
| 21 | void interval(RxInterface listener, Function(dynamic) callback, | 26 | void interval(RxInterface listener, Function(dynamic) callback, |
| 22 | - {Duration time}) { | 27 | + {Duration time, bool condition = true}) { |
| 23 | bool debounceActive = false; | 28 | bool debounceActive = false; |
| 24 | - Duration timer = time ?? Duration(seconds: 1); | ||
| 25 | - | ||
| 26 | listener.subject.stream.listen((event) async { | 29 | listener.subject.stream.listen((event) async { |
| 27 | - if (debounceActive) return null; | 30 | + if (debounceActive || !condition) return null; |
| 28 | debounceActive = true; | 31 | debounceActive = true; |
| 29 | - await Future.delayed(timer); | 32 | + await Future.delayed(time ?? Duration(seconds: 1)); |
| 30 | debounceActive = false; | 33 | debounceActive = false; |
| 31 | callback(event.$new); | 34 | callback(event.$new); |
| 32 | }); | 35 | }); |
| @@ -106,16 +106,12 @@ class _GetBuilderState<T extends GetController> extends State<GetBuilder<T>> { | @@ -106,16 +106,12 @@ class _GetBuilderState<T extends GetController> extends State<GetBuilder<T>> { | ||
| 106 | } | 106 | } |
| 107 | if (widget.initState != null) widget.initState(this); | 107 | if (widget.initState != null) widget.initState(this); |
| 108 | if (isCreator) { | 108 | if (isCreator) { |
| 109 | - try { | ||
| 110 | - controller?.onInit(); | ||
| 111 | - } catch (e) { | ||
| 112 | - if (Get.isLogEnable) print("[GET] error: $e"); | ||
| 113 | - } | 109 | + controller?.onInit(); |
| 114 | } | 110 | } |
| 115 | } | 111 | } |
| 116 | 112 | ||
| 117 | @override | 113 | @override |
| 118 | - void dispose() async { | 114 | + void dispose() { |
| 119 | super.dispose(); | 115 | super.dispose(); |
| 120 | 116 | ||
| 121 | if (widget.dispose != null) widget.dispose(this); | 117 | if (widget.dispose != null) widget.dispose(this); |
| 1 | name: get | 1 | name: get |
| 2 | description: Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get. | 2 | description: Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get. |
| 3 | -version: 2.10.1 | 3 | +version: 2.10.2 |
| 4 | homepage: https://github.com/jonataslaw/get | 4 | homepage: https://github.com/jonataslaw/get |
| 5 | 5 | ||
| 6 | environment: | 6 | environment: |
| @@ -25,4 +25,48 @@ void main() { | @@ -25,4 +25,48 @@ void main() { | ||
| 25 | 25 | ||
| 26 | expect(find.byIcon(Icons.music_note), findsOneWidget); | 26 | expect(find.byIcon(Icons.music_note), findsOneWidget); |
| 27 | }); | 27 | }); |
| 28 | + | ||
| 29 | + testWidgets("Get.bottomSheet close test", (tester) async { | ||
| 30 | + await tester.pumpWidget( | ||
| 31 | + Wrapper(child: Container()), | ||
| 32 | + ); | ||
| 33 | + | ||
| 34 | + Get.bottomSheet(Container( | ||
| 35 | + child: Wrap( | ||
| 36 | + children: <Widget>[ | ||
| 37 | + ListTile( | ||
| 38 | + leading: Icon(Icons.music_note), | ||
| 39 | + title: Text('Music'), | ||
| 40 | + onTap: () => {}), | ||
| 41 | + ], | ||
| 42 | + ), | ||
| 43 | + )); | ||
| 44 | + | ||
| 45 | + expect(Get.isBottomSheetOpen, true); | ||
| 46 | + | ||
| 47 | + Get.back(); | ||
| 48 | + expect(Get.isBottomSheetOpen, false); | ||
| 49 | + | ||
| 50 | + expect(() => Get.bottomSheet(Container(), isScrollControlled: null), | ||
| 51 | + throwsAssertionError); | ||
| 52 | + | ||
| 53 | + expect(() => Get.bottomSheet(Container(), isDismissible: null), | ||
| 54 | + throwsAssertionError); | ||
| 55 | + | ||
| 56 | + expect(() => Get.bottomSheet(Container(), enableDrag: null), | ||
| 57 | + throwsAssertionError); | ||
| 58 | + | ||
| 59 | + await tester.pumpAndSettle(); | ||
| 60 | + }); | ||
| 61 | + | ||
| 62 | + testWidgets( | ||
| 63 | + "GetMaterialApp with debugShowMaterialGrid null", | ||
| 64 | + (WidgetTester testr) async { | ||
| 65 | + expect( | ||
| 66 | + () => GetMaterialApp( | ||
| 67 | + debugShowMaterialGrid: null, | ||
| 68 | + ), | ||
| 69 | + throwsAssertionError); | ||
| 70 | + }, | ||
| 71 | + ); | ||
| 28 | } | 72 | } |
| @@ -30,6 +30,18 @@ void main() { | @@ -30,6 +30,18 @@ void main() { | ||
| 30 | 30 | ||
| 31 | expect(find.byType(YourDialogWidget), findsOneWidget); | 31 | expect(find.byType(YourDialogWidget), findsOneWidget); |
| 32 | }); | 32 | }); |
| 33 | + | ||
| 34 | + testWidgets("Get.dialog close test", (tester) async { | ||
| 35 | + await tester.pumpWidget( | ||
| 36 | + Wrapper(child: Container()), | ||
| 37 | + ); | ||
| 38 | + | ||
| 39 | + Get.dialog(YourDialogWidget()); | ||
| 40 | + expect(Get.isDialogOpen, true); | ||
| 41 | + Get.back(); | ||
| 42 | + expect(Get.isDialogOpen, false); | ||
| 43 | + await tester.pumpAndSettle(); | ||
| 44 | + }); | ||
| 33 | } | 45 | } |
| 34 | 46 | ||
| 35 | class YourDialogWidget extends StatelessWidget { | 47 | class YourDialogWidget extends StatelessWidget { |
test/get_instance_test.dart
0 → 100644
| 1 | +import 'package:flutter_test/flutter_test.dart'; | ||
| 2 | +import 'package:get/get.dart'; | ||
| 3 | + | ||
| 4 | +class Mock { | ||
| 5 | + static Future<String> test() async { | ||
| 6 | + await Future.delayed(Duration.zero); | ||
| 7 | + return 'test'; | ||
| 8 | + } | ||
| 9 | +} | ||
| 10 | + | ||
| 11 | +class Controller {} | ||
| 12 | + | ||
| 13 | +abstract class Service { | ||
| 14 | + String post(); | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +class Api implements Service { | ||
| 18 | + @override | ||
| 19 | + String post() { | ||
| 20 | + return 'test'; | ||
| 21 | + } | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +void main() { | ||
| 25 | + test('Get.putAsync test', () async { | ||
| 26 | + await Get.putAsync<String>(() => Mock.test()); | ||
| 27 | + expect('test', Get.find<String>()); | ||
| 28 | + Get.reset(); | ||
| 29 | + }); | ||
| 30 | + | ||
| 31 | + test('Get.put test', () async { | ||
| 32 | + final instance = Get.put<Controller>(Controller()); | ||
| 33 | + expect(instance, Get.find<Controller>()); | ||
| 34 | + Get.reset(); | ||
| 35 | + }); | ||
| 36 | + | ||
| 37 | + test('Get.lazyPut test', () async { | ||
| 38 | + final controller = Controller(); | ||
| 39 | + Get.lazyPut<Controller>(() => controller); | ||
| 40 | + final ct1 = Get.find<Controller>(); | ||
| 41 | + expect(ct1, controller); | ||
| 42 | + Get.reset(); | ||
| 43 | + }); | ||
| 44 | + | ||
| 45 | + test('Get.lazyPut with abstract class test', () async { | ||
| 46 | + final api = Api(); | ||
| 47 | + Get.lazyPut<Service>(() => api); | ||
| 48 | + final ct1 = Get.find<Service>(); | ||
| 49 | + expect(ct1, api); | ||
| 50 | + Get.reset(); | ||
| 51 | + }); | ||
| 52 | +} |
test/get_mixin_state_test.dart
0 → 100644
| 1 | +import 'package:flutter/material.dart'; | ||
| 2 | +import 'package:flutter_test/flutter_test.dart'; | ||
| 3 | +import 'package:get/get.dart'; | ||
| 4 | + | ||
| 5 | +void main() { | ||
| 6 | + testWidgets("MixinBuilder smoke test", (tester) async { | ||
| 7 | + await tester.pumpWidget( | ||
| 8 | + MaterialApp( | ||
| 9 | + home: MixinBuilder<Controller>( | ||
| 10 | + init: Controller(), | ||
| 11 | + builder: (controller) { | ||
| 12 | + return Column( | ||
| 13 | + children: [ | ||
| 14 | + Text( | ||
| 15 | + 'Count: ${controller.counter.value}', | ||
| 16 | + ), | ||
| 17 | + Text( | ||
| 18 | + 'Count2: ${controller.count}', | ||
| 19 | + ), | ||
| 20 | + Text( | ||
| 21 | + 'Double: ${controller.doubleNum.value}', | ||
| 22 | + ), | ||
| 23 | + Text( | ||
| 24 | + 'String: ${controller.string.value}', | ||
| 25 | + ), | ||
| 26 | + Text( | ||
| 27 | + 'List: ${controller.list.length}', | ||
| 28 | + ), | ||
| 29 | + Text( | ||
| 30 | + 'Bool: ${controller.boolean.value}', | ||
| 31 | + ), | ||
| 32 | + Text( | ||
| 33 | + 'Map: ${controller.map.value.length}', | ||
| 34 | + ), | ||
| 35 | + FlatButton( | ||
| 36 | + child: Text("increment"), | ||
| 37 | + onPressed: () => controller.increment(), | ||
| 38 | + ) | ||
| 39 | + ], | ||
| 40 | + ); | ||
| 41 | + }, | ||
| 42 | + ), | ||
| 43 | + ), | ||
| 44 | + ); | ||
| 45 | + | ||
| 46 | + expect(find.text("Count: 0"), findsOneWidget); | ||
| 47 | + expect(find.text("Count2: 0"), findsOneWidget); | ||
| 48 | + expect(find.text("Double: 0.0"), findsOneWidget); | ||
| 49 | + expect(find.text("String: string"), findsOneWidget); | ||
| 50 | + expect(find.text("Bool: true"), findsOneWidget); | ||
| 51 | + expect(find.text("List: 0"), findsOneWidget); | ||
| 52 | + expect(find.text("Map: 0"), findsOneWidget); | ||
| 53 | + | ||
| 54 | + Controller.to.increment(); | ||
| 55 | + | ||
| 56 | + await tester.pump(); | ||
| 57 | + | ||
| 58 | + expect(find.text("Count: 1"), findsOneWidget); | ||
| 59 | + | ||
| 60 | + await tester.tap(find.text('increment')); | ||
| 61 | + | ||
| 62 | + await tester.pump(); | ||
| 63 | + | ||
| 64 | + expect(find.text("Count: 2"), findsOneWidget); | ||
| 65 | + }); | ||
| 66 | + | ||
| 67 | + testWidgets( | ||
| 68 | + "MixinBuilder with build null", | ||
| 69 | + (WidgetTester tester) async { | ||
| 70 | + expect( | ||
| 71 | + () => MixinBuilder<Controller>( | ||
| 72 | + init: Controller(), | ||
| 73 | + builder: null, | ||
| 74 | + ), | ||
| 75 | + throwsAssertionError); | ||
| 76 | + }, | ||
| 77 | + ); | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +class Controller extends GetController { | ||
| 81 | + static Controller get to => Get.find(); | ||
| 82 | + int count = 0; | ||
| 83 | + var counter = 0.obs; | ||
| 84 | + var doubleNum = 0.0.obs; | ||
| 85 | + var string = "string".obs; | ||
| 86 | + var list = [].obs; | ||
| 87 | + var map = {}.obs; | ||
| 88 | + var boolean = true.obs; | ||
| 89 | + | ||
| 90 | + void increment() { | ||
| 91 | + counter.value++; | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + void increment2() { | ||
| 95 | + count++; | ||
| 96 | + update(this); | ||
| 97 | + } | ||
| 98 | +} |
test/get_obx_test.dart
0 → 100644
| 1 | +import 'package:flutter/material.dart'; | ||
| 2 | +import 'package:flutter_test/flutter_test.dart'; | ||
| 3 | +import 'package:get/get.dart'; | ||
| 4 | + | ||
| 5 | +void main() { | ||
| 6 | + Controller controller = Get.put<Controller>(Controller()); | ||
| 7 | + testWidgets("GetController smoke test", (tester) async { | ||
| 8 | + await tester.pumpWidget( | ||
| 9 | + MaterialApp( | ||
| 10 | + home: Column( | ||
| 11 | + children: [ | ||
| 12 | + Obx( | ||
| 13 | + () => Column(children: [ | ||
| 14 | + Text( | ||
| 15 | + 'Count: ${controller.counter.value}', | ||
| 16 | + ), | ||
| 17 | + Text( | ||
| 18 | + 'Double: ${controller.doubleNum.value}', | ||
| 19 | + ), | ||
| 20 | + Text( | ||
| 21 | + 'String: ${controller.string.value}', | ||
| 22 | + ), | ||
| 23 | + Text( | ||
| 24 | + 'List: ${controller.list.length}', | ||
| 25 | + ), | ||
| 26 | + Text( | ||
| 27 | + 'Bool: ${controller.boolean.value}', | ||
| 28 | + ), | ||
| 29 | + Text( | ||
| 30 | + 'Map: ${controller.map.value.length}', | ||
| 31 | + ), | ||
| 32 | + FlatButton( | ||
| 33 | + child: Text("increment"), | ||
| 34 | + onPressed: () => controller.increment(), | ||
| 35 | + ), | ||
| 36 | + obx(() => Text( | ||
| 37 | + 'Obx: ${controller.map.value.length}', | ||
| 38 | + )) | ||
| 39 | + ]), | ||
| 40 | + ), | ||
| 41 | + ], | ||
| 42 | + ), | ||
| 43 | + ), | ||
| 44 | + ); | ||
| 45 | + | ||
| 46 | + expect(find.text("Count: 0"), findsOneWidget); | ||
| 47 | + expect(find.text("Double: 0.0"), findsOneWidget); | ||
| 48 | + expect(find.text("String: string"), findsOneWidget); | ||
| 49 | + expect(find.text("Bool: true"), findsOneWidget); | ||
| 50 | + expect(find.text("List: 0"), findsOneWidget); | ||
| 51 | + expect(find.text("Map: 0"), findsOneWidget); | ||
| 52 | + expect(find.text("Obx: 0"), findsOneWidget); | ||
| 53 | + | ||
| 54 | + Controller.to.increment(); | ||
| 55 | + | ||
| 56 | + await tester.pump(); | ||
| 57 | + | ||
| 58 | + expect(find.text("Count: 1"), findsOneWidget); | ||
| 59 | + | ||
| 60 | + await tester.tap(find.text('increment')); | ||
| 61 | + | ||
| 62 | + await tester.pump(); | ||
| 63 | + | ||
| 64 | + expect(find.text("Count: 2"), findsOneWidget); | ||
| 65 | + }); | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +class Controller extends RxController { | ||
| 69 | + static Controller get to => Get.find(); | ||
| 70 | + | ||
| 71 | + var counter = 0.obs; | ||
| 72 | + var doubleNum = 0.0.obs; | ||
| 73 | + var string = "string".obs; | ||
| 74 | + var list = [].obs; | ||
| 75 | + var map = {}.obs; | ||
| 76 | + var boolean = true.obs; | ||
| 77 | + | ||
| 78 | + void increment() { | ||
| 79 | + counter.value++; | ||
| 80 | + } | ||
| 81 | +} |
| @@ -3,6 +3,7 @@ import 'package:flutter_test/flutter_test.dart'; | @@ -3,6 +3,7 @@ import 'package:flutter_test/flutter_test.dart'; | ||
| 3 | import 'package:get/get.dart'; | 3 | import 'package:get/get.dart'; |
| 4 | 4 | ||
| 5 | void main() { | 5 | void main() { |
| 6 | + Get.lazyPut<Controller2>(() => Controller2()); | ||
| 6 | testWidgets("GetController smoke test", (tester) async { | 7 | testWidgets("GetController smoke test", (tester) async { |
| 7 | await tester.pumpWidget( | 8 | await tester.pumpWidget( |
| 8 | MaterialApp( | 9 | MaterialApp( |
| @@ -32,7 +33,19 @@ void main() { | @@ -32,7 +33,19 @@ void main() { | ||
| 32 | FlatButton( | 33 | FlatButton( |
| 33 | child: Text("increment"), | 34 | child: Text("increment"), |
| 34 | onPressed: () => controller.increment(), | 35 | onPressed: () => controller.increment(), |
| 35 | - ) | 36 | + ), |
| 37 | + GetX<Controller2>(builder: (controller) { | ||
| 38 | + return Text('lazy ${controller.lazy}'); | ||
| 39 | + }), | ||
| 40 | + GetX<Controller>(builder: (controller) { | ||
| 41 | + return Container(); | ||
| 42 | + }), | ||
| 43 | + GetX<ControllerNonGlobal>( | ||
| 44 | + init: ControllerNonGlobal(), | ||
| 45 | + global: false, | ||
| 46 | + builder: (controller) { | ||
| 47 | + return Text('single ${controller.nonGlobal}'); | ||
| 48 | + }) | ||
| 36 | ], | 49 | ], |
| 37 | ); | 50 | ); |
| 38 | }, | 51 | }, |
| @@ -58,9 +71,19 @@ void main() { | @@ -58,9 +71,19 @@ void main() { | ||
| 58 | await tester.pump(); | 71 | await tester.pump(); |
| 59 | 72 | ||
| 60 | expect(find.text("Count: 2"), findsOneWidget); | 73 | expect(find.text("Count: 2"), findsOneWidget); |
| 74 | + expect(find.text("lazy 0"), findsOneWidget); | ||
| 75 | + expect(find.text("single 0"), findsOneWidget); | ||
| 61 | }); | 76 | }); |
| 62 | } | 77 | } |
| 63 | 78 | ||
| 79 | +class Controller2 extends RxController { | ||
| 80 | + int lazy = 0; | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +class ControllerNonGlobal extends RxController { | ||
| 84 | + int nonGlobal = 0; | ||
| 85 | +} | ||
| 86 | + | ||
| 64 | class Controller extends RxController { | 87 | class Controller extends RxController { |
| 65 | static Controller get to => Get.find(); | 88 | static Controller get to => Get.find(); |
| 66 | 89 |
| @@ -3,8 +3,9 @@ import 'package:flutter_test/flutter_test.dart'; | @@ -3,8 +3,9 @@ import 'package:flutter_test/flutter_test.dart'; | ||
| 3 | import 'package:get/get.dart'; | 3 | import 'package:get/get.dart'; |
| 4 | 4 | ||
| 5 | void main() { | 5 | void main() { |
| 6 | - testWidgets("GetController smoke test", (tester) async { | ||
| 7 | - await tester.pumpWidget( | 6 | + Get.lazyPut<Controller2>(() => Controller2()); |
| 7 | + testWidgets("GetController smoke test", (testr) async { | ||
| 8 | + await testr.pumpWidget( | ||
| 8 | MaterialApp( | 9 | MaterialApp( |
| 9 | home: GetBuilder<Controller>( | 10 | home: GetBuilder<Controller>( |
| 10 | init: Controller(), | 11 | init: Controller(), |
| @@ -16,7 +17,28 @@ void main() { | @@ -16,7 +17,28 @@ void main() { | ||
| 16 | FlatButton( | 17 | FlatButton( |
| 17 | child: Text("increment"), | 18 | child: Text("increment"), |
| 18 | onPressed: () => controller.increment(), | 19 | onPressed: () => controller.increment(), |
| 19 | - ) | 20 | + ), |
| 21 | + FlatButton( | ||
| 22 | + child: Text("incrementWithId"), | ||
| 23 | + onPressed: () => controller.incrementWithId(), | ||
| 24 | + ), | ||
| 25 | + GetBuilder<Controller>( | ||
| 26 | + id: '1', | ||
| 27 | + didChangeDependencies: (_) { | ||
| 28 | + print("didChangeDependencies called"); | ||
| 29 | + }, | ||
| 30 | + builder: (controller) { | ||
| 31 | + return Text('id ${controller.counter}'); | ||
| 32 | + }), | ||
| 33 | + GetBuilder<Controller2>(builder: (controller) { | ||
| 34 | + return Text('lazy ${controller.test}'); | ||
| 35 | + }), | ||
| 36 | + GetBuilder<ControllerNonGlobal>( | ||
| 37 | + init: ControllerNonGlobal(), | ||
| 38 | + global: false, | ||
| 39 | + builder: (controller) { | ||
| 40 | + return Text('single ${controller.nonGlobal}'); | ||
| 41 | + }) | ||
| 20 | ], | 42 | ], |
| 21 | ), | 43 | ), |
| 22 | ), | 44 | ), |
| @@ -27,16 +49,44 @@ void main() { | @@ -27,16 +49,44 @@ void main() { | ||
| 27 | 49 | ||
| 28 | Controller.to.increment(); | 50 | Controller.to.increment(); |
| 29 | 51 | ||
| 30 | - await tester.pump(); | 52 | + await testr.pump(); |
| 31 | 53 | ||
| 32 | expect(find.text("1"), findsOneWidget); | 54 | expect(find.text("1"), findsOneWidget); |
| 33 | 55 | ||
| 34 | - await tester.tap(find.text('increment')); | 56 | + await testr.tap(find.text('increment')); |
| 35 | 57 | ||
| 36 | - await tester.pump(); | 58 | + await testr.pump(); |
| 37 | 59 | ||
| 38 | expect(find.text("2"), findsOneWidget); | 60 | expect(find.text("2"), findsOneWidget); |
| 61 | + | ||
| 62 | + await testr.tap(find.text('incrementWithId')); | ||
| 63 | + | ||
| 64 | + await testr.pump(); | ||
| 65 | + | ||
| 66 | + expect(find.text("id 3"), findsOneWidget); | ||
| 67 | + expect(find.text("lazy 0"), findsOneWidget); | ||
| 68 | + expect(find.text("single 0"), findsOneWidget); | ||
| 39 | }); | 69 | }); |
| 70 | + | ||
| 71 | + testWidgets( | ||
| 72 | + "MixinBuilder with build null", | ||
| 73 | + (WidgetTester testr) async { | ||
| 74 | + expect( | ||
| 75 | + () => GetBuilder<Controller>( | ||
| 76 | + init: Controller(), | ||
| 77 | + builder: null, | ||
| 78 | + ), | ||
| 79 | + throwsAssertionError); | ||
| 80 | + }, | ||
| 81 | + ); | ||
| 82 | + | ||
| 83 | + testWidgets( | ||
| 84 | + "GetBuilder controller error", | ||
| 85 | + (WidgetTester testr) async { | ||
| 86 | + expect(() => ControllerError().callBuild(), | ||
| 87 | + throwsA("build method can\'t be called")); | ||
| 88 | + }, | ||
| 89 | + ); | ||
| 40 | } | 90 | } |
| 41 | 91 | ||
| 42 | class Controller extends GetController { | 92 | class Controller extends GetController { |
| @@ -47,4 +97,26 @@ class Controller extends GetController { | @@ -47,4 +97,26 @@ class Controller extends GetController { | ||
| 47 | counter++; | 97 | counter++; |
| 48 | update(this); | 98 | update(this); |
| 49 | } | 99 | } |
| 100 | + | ||
| 101 | + void incrementWithId() { | ||
| 102 | + counter++; | ||
| 103 | + update(this, ['1']); | ||
| 104 | + } | ||
| 105 | +} | ||
| 106 | + | ||
| 107 | +class Controller2 extends GetController { | ||
| 108 | + int test = 0; | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | +class ControllerError extends GetController { | ||
| 112 | + int test = 0; | ||
| 113 | + | ||
| 114 | + callBuild() { | ||
| 115 | + BuildContext context; | ||
| 116 | + build(context); | ||
| 117 | + } | ||
| 118 | +} | ||
| 119 | + | ||
| 120 | +class ControllerNonGlobal extends GetController { | ||
| 121 | + int nonGlobal = 0; | ||
| 50 | } | 122 | } |
test/platform_test.dart
0 → 100644
| 1 | +import 'dart:io'; | ||
| 2 | + | ||
| 3 | +import 'package:flutter_test/flutter_test.dart'; | ||
| 4 | +import 'package:get/get.dart'; | ||
| 5 | +import 'package:get/src/platform/platform_web.dart'; | ||
| 6 | + | ||
| 7 | +void main() { | ||
| 8 | + test('Platform test', () { | ||
| 9 | + expect(GetPlatform.isAndroid, Platform.isAndroid); | ||
| 10 | + expect(GetPlatform.isIOS, Platform.isIOS); | ||
| 11 | + expect(GetPlatform.isFuchsia, Platform.isFuchsia); | ||
| 12 | + expect(GetPlatform.isLinux, Platform.isLinux); | ||
| 13 | + expect(GetPlatform.isMacOS, Platform.isMacOS); | ||
| 14 | + expect(GetPlatform.isWindows, Platform.isWindows); | ||
| 15 | + expect(GetPlatform.isWeb, false); | ||
| 16 | + expect(GeneralPlatform.isWeb, true); | ||
| 17 | + expect(GeneralPlatform.isAndroid, false); | ||
| 18 | + expect(GeneralPlatform.isIOS, false); | ||
| 19 | + expect(GeneralPlatform.isFuchsia, false); | ||
| 20 | + expect(GeneralPlatform.isLinux, false); | ||
| 21 | + expect(GeneralPlatform.isMacOS, false); | ||
| 22 | + expect(GeneralPlatform.isWindows, false); | ||
| 23 | + }); | ||
| 24 | +} |
test/root_controller_test.dart
0 → 100644
| 1 | +import 'package:flutter/material.dart'; | ||
| 2 | +import 'package:flutter_test/flutter_test.dart'; | ||
| 3 | +import 'package:get/get.dart'; | ||
| 4 | + | ||
| 5 | +import 'util/wrapper.dart'; | ||
| 6 | + | ||
| 7 | +void main() { | ||
| 8 | + testWidgets("Get.dialog close test", (tester) async { | ||
| 9 | + await tester.pumpWidget( | ||
| 10 | + Wrapper(child: Container()), | ||
| 11 | + ); | ||
| 12 | + | ||
| 13 | + expect(Get.isDarkMode, false); | ||
| 14 | + Get.changeTheme(ThemeData.dark()); | ||
| 15 | + await Future.delayed(Duration.zero); | ||
| 16 | + expect(Get.isDarkMode, true); | ||
| 17 | + await tester.pumpAndSettle(); | ||
| 18 | + }); | ||
| 19 | +} |
test/root_widget_test.dart
0 → 100644
| 1 | +import 'package:flutter_test/flutter_test.dart'; | ||
| 2 | +import 'package:get/get.dart'; | ||
| 3 | + | ||
| 4 | +void main() { | ||
| 5 | + testWidgets( | ||
| 6 | + "GetMaterialApp with routes null", | ||
| 7 | + (WidgetTester testr) async { | ||
| 8 | + expect( | ||
| 9 | + () => GetMaterialApp( | ||
| 10 | + routes: null, | ||
| 11 | + ), | ||
| 12 | + throwsAssertionError); | ||
| 13 | + }, | ||
| 14 | + ); | ||
| 15 | + | ||
| 16 | + testWidgets( | ||
| 17 | + "GetMaterialApp with navigatorObservers null", | ||
| 18 | + (WidgetTester testr) async { | ||
| 19 | + expect( | ||
| 20 | + () => GetMaterialApp( | ||
| 21 | + navigatorObservers: null, | ||
| 22 | + ), | ||
| 23 | + throwsAssertionError); | ||
| 24 | + }, | ||
| 25 | + ); | ||
| 26 | + testWidgets( | ||
| 27 | + "GetMaterialApp with title null", | ||
| 28 | + (WidgetTester testr) async { | ||
| 29 | + expect( | ||
| 30 | + () => GetMaterialApp( | ||
| 31 | + title: null, | ||
| 32 | + ), | ||
| 33 | + throwsAssertionError); | ||
| 34 | + }, | ||
| 35 | + ); | ||
| 36 | + testWidgets( | ||
| 37 | + "GetMaterialApp with debugShowMaterialGrid null", | ||
| 38 | + (WidgetTester testr) async { | ||
| 39 | + expect( | ||
| 40 | + () => GetMaterialApp( | ||
| 41 | + debugShowMaterialGrid: null, | ||
| 42 | + ), | ||
| 43 | + throwsAssertionError); | ||
| 44 | + }, | ||
| 45 | + ); | ||
| 46 | + testWidgets( | ||
| 47 | + "GetMaterialApp with showPerformanceOverlay null", | ||
| 48 | + (WidgetTester testr) async { | ||
| 49 | + expect( | ||
| 50 | + () => GetMaterialApp( | ||
| 51 | + showPerformanceOverlay: null, | ||
| 52 | + ), | ||
| 53 | + throwsAssertionError); | ||
| 54 | + }, | ||
| 55 | + ); | ||
| 56 | + testWidgets( | ||
| 57 | + "GetMaterialApp with showSemanticsDebugger null", | ||
| 58 | + (WidgetTester testr) async { | ||
| 59 | + expect( | ||
| 60 | + () => GetMaterialApp( | ||
| 61 | + showSemanticsDebugger: null, | ||
| 62 | + ), | ||
| 63 | + throwsAssertionError); | ||
| 64 | + }, | ||
| 65 | + ); | ||
| 66 | + testWidgets( | ||
| 67 | + "GetMaterialApp with debugShowCheckedModeBanner null", | ||
| 68 | + (WidgetTester testr) async { | ||
| 69 | + expect( | ||
| 70 | + () => GetMaterialApp( | ||
| 71 | + debugShowCheckedModeBanner: null, | ||
| 72 | + ), | ||
| 73 | + throwsAssertionError); | ||
| 74 | + }, | ||
| 75 | + ); | ||
| 76 | + | ||
| 77 | + testWidgets( | ||
| 78 | + "GetMaterialApp with checkerboardRasterCacheImages null", | ||
| 79 | + (WidgetTester testr) async { | ||
| 80 | + expect( | ||
| 81 | + () => GetMaterialApp( | ||
| 82 | + checkerboardRasterCacheImages: null, | ||
| 83 | + ), | ||
| 84 | + throwsAssertionError); | ||
| 85 | + }, | ||
| 86 | + ); | ||
| 87 | + | ||
| 88 | + testWidgets( | ||
| 89 | + "GetMaterialApp with checkerboardOffscreenLayers null", | ||
| 90 | + (WidgetTester testr) async { | ||
| 91 | + expect( | ||
| 92 | + () => GetMaterialApp( | ||
| 93 | + checkerboardOffscreenLayers: null, | ||
| 94 | + ), | ||
| 95 | + throwsAssertionError); | ||
| 96 | + }, | ||
| 97 | + ); | ||
| 98 | +} |
test/routes_test.dart
0 → 100644
| 1 | +import 'package:flutter/material.dart'; | ||
| 2 | +import 'package:flutter_test/flutter_test.dart'; | ||
| 3 | +import 'package:get/src/routes/get_route.dart'; | ||
| 4 | + | ||
| 5 | +void main() { | ||
| 6 | + testWidgets( | ||
| 7 | + "GetRoute page null", | ||
| 8 | + (WidgetTester testr) async { | ||
| 9 | + expect(() => GetRoute(page: null), throwsAssertionError); | ||
| 10 | + }, | ||
| 11 | + ); | ||
| 12 | + | ||
| 13 | + testWidgets( | ||
| 14 | + "GetRoute maintainState null", | ||
| 15 | + (WidgetTester testr) async { | ||
| 16 | + expect(() => GetRoute(page: Scaffold(), maintainState: null), | ||
| 17 | + throwsAssertionError); | ||
| 18 | + }, | ||
| 19 | + ); | ||
| 20 | + | ||
| 21 | + testWidgets( | ||
| 22 | + "GetRoute fullscreenDialog null", | ||
| 23 | + (WidgetTester testr) async { | ||
| 24 | + expect(() => GetRoute(page: Scaffold(), fullscreenDialog: null), | ||
| 25 | + throwsAssertionError); | ||
| 26 | + }, | ||
| 27 | + ); | ||
| 28 | +} |
test/rx_event_test.dart
0 → 100644
| 1 | +import 'package:flutter_test/flutter_test.dart'; | ||
| 2 | +import 'package:get/get.dart'; | ||
| 3 | + | ||
| 4 | +void main() { | ||
| 5 | + test('once', () async { | ||
| 6 | + final count = 0.obs; | ||
| 7 | + int result = -1; | ||
| 8 | + once(count, (_) { | ||
| 9 | + result = _; | ||
| 10 | + }); | ||
| 11 | + count.value++; | ||
| 12 | + await Future.delayed(Duration.zero); | ||
| 13 | + expect(1, result); | ||
| 14 | + count.value++; | ||
| 15 | + await Future.delayed(Duration.zero); | ||
| 16 | + expect(1, result); | ||
| 17 | + count.value++; | ||
| 18 | + await Future.delayed(Duration.zero); | ||
| 19 | + expect(1, result); | ||
| 20 | + }); | ||
| 21 | + | ||
| 22 | + test('ever', () async { | ||
| 23 | + final count = 0.obs; | ||
| 24 | + int result = -1; | ||
| 25 | + ever(count, (_) { | ||
| 26 | + result = _; | ||
| 27 | + }); | ||
| 28 | + count.value++; | ||
| 29 | + await Future.delayed(Duration.zero); | ||
| 30 | + expect(1, result); | ||
| 31 | + count.value++; | ||
| 32 | + await Future.delayed(Duration.zero); | ||
| 33 | + expect(2, result); | ||
| 34 | + count.value++; | ||
| 35 | + await Future.delayed(Duration.zero); | ||
| 36 | + expect(3, result); | ||
| 37 | + }); | ||
| 38 | + | ||
| 39 | + test('debounce', () async { | ||
| 40 | + final count = 0.obs; | ||
| 41 | + int result = -1; | ||
| 42 | + debounce(count, (_) { | ||
| 43 | + print(_); | ||
| 44 | + result = _; | ||
| 45 | + }, time: Duration(milliseconds: 100)); | ||
| 46 | + | ||
| 47 | + count.value++; | ||
| 48 | + count.value++; | ||
| 49 | + count.value++; | ||
| 50 | + count.value++; | ||
| 51 | + await Future.delayed(Duration.zero); | ||
| 52 | + expect(-1, result); | ||
| 53 | + await Future.delayed(Duration(milliseconds: 100)); | ||
| 54 | + expect(4, result); | ||
| 55 | + }); | ||
| 56 | + | ||
| 57 | + test('interval', () async { | ||
| 58 | + final count = 0.obs; | ||
| 59 | + int result = -1; | ||
| 60 | + interval(count, (_) { | ||
| 61 | + print(_); | ||
| 62 | + result = _; | ||
| 63 | + }, time: Duration(milliseconds: 100)); | ||
| 64 | + | ||
| 65 | + count.value++; | ||
| 66 | + await Future.delayed(Duration.zero); | ||
| 67 | + await Future.delayed(Duration(milliseconds: 100)); | ||
| 68 | + expect(1, result); | ||
| 69 | + count.value++; | ||
| 70 | + count.value++; | ||
| 71 | + count.value++; | ||
| 72 | + await Future.delayed(Duration.zero); | ||
| 73 | + await Future.delayed(Duration(milliseconds: 100)); | ||
| 74 | + expect(2, result); | ||
| 75 | + count.value++; | ||
| 76 | + await Future.delayed(Duration.zero); | ||
| 77 | + await Future.delayed(Duration(milliseconds: 100)); | ||
| 78 | + expect(5, result); | ||
| 79 | + }); | ||
| 80 | +} |
-
Please register or login to post a comment