@@ -12,11 +12,11 @@ import 'package:fake_async/fake_async.dart';
1212import 'package:flutter_test/flutter_test.dart' ;
1313
1414void main () {
15- group ('DebounceTimer ' , () {
15+ group ('PeriodicDebouncer ' , () {
1616 test ('the callback happens immediately' , () {
1717 fakeAsync ((async ) {
1818 int callbackCounter = 0 ;
19- DebounceTimer . periodic (const Duration (seconds: 1 ), ({
19+ PeriodicDebouncer . run (const Duration (seconds: 1 ), ({
2020 DebounceCancelledCallback ? cancelledCallback,
2121 }) async {
2222 callbackCounter++ ;
@@ -30,7 +30,7 @@ void main() {
3030 test ('only triggers another callback after the first is done' , () {
3131 fakeAsync ((async ) {
3232 int callbackCounter = 0 ;
33- DebounceTimer . periodic (const Duration (milliseconds: 500 ), ({
33+ PeriodicDebouncer . run (const Duration (milliseconds: 500 ), ({
3434 DebounceCancelledCallback ? cancelledCallback,
3535 }) async {
3636 callbackCounter++ ;
@@ -44,7 +44,7 @@ void main() {
4444 test ('calls the callback at the beginning and then once per period' , () {
4545 fakeAsync ((async ) {
4646 int callbackCounter = 0 ;
47- DebounceTimer . periodic (const Duration (seconds: 1 ), ({
47+ PeriodicDebouncer . run (const Duration (seconds: 1 ), ({
4848 DebounceCancelledCallback ? cancelledCallback,
4949 }) async {
5050 callbackCounter++ ;
@@ -60,7 +60,7 @@ void main() {
6060 () {
6161 fakeAsync ((async ) {
6262 int callbackCounter = 0 ;
63- final timer = DebounceTimer . periodic (const Duration (seconds: 1 ), ({
63+ final timer = PeriodicDebouncer . run (const Duration (seconds: 1 ), ({
6464 DebounceCancelledCallback ? cancelledCallback,
6565 }) async {
6666 callbackCounter++ ;
@@ -82,7 +82,7 @@ void main() {
8282 () {
8383 fakeAsync ((async ) {
8484 int callbackCounter = 0 ;
85- final timer = DebounceTimer . periodic (const Duration (seconds: 1 ), ({
85+ final timer = PeriodicDebouncer . run (const Duration (seconds: 1 ), ({
8686 DebounceCancelledCallback ? cancelledCallback,
8787 }) async {
8888 callbackCounter++ ;
@@ -102,7 +102,7 @@ void main() {
102102 test ('cancels the callback when cancelled during the first callback' , () {
103103 fakeAsync ((async ) {
104104 int callbackCounter = 0 ;
105- final timer = DebounceTimer . periodic (const Duration (seconds: 1 ), ({
105+ final timer = PeriodicDebouncer . run (const Duration (seconds: 1 ), ({
106106 DebounceCancelledCallback ? cancelledCallback,
107107 }) async {
108108 callbackCounter++ ;
@@ -123,7 +123,7 @@ void main() {
123123 test ('cancels the callback when cancelled during the Nth callback' , () {
124124 fakeAsync ((async ) {
125125 int callbackCounter = 0 ;
126- final timer = DebounceTimer . periodic (const Duration (seconds: 1 ), ({
126+ final timer = PeriodicDebouncer . run (const Duration (seconds: 1 ), ({
127127 DebounceCancelledCallback ? cancelledCallback,
128128 }) async {
129129 callbackCounter++ ;
@@ -142,6 +142,57 @@ void main() {
142142 });
143143 });
144144
145+ group ('Debouncer' , () {
146+ late Debouncer debouncer;
147+ late int callbackCount;
148+
149+ setUp (() {
150+ callbackCount = 0 ;
151+ debouncer = Debouncer (duration: const Duration (milliseconds: 100 ));
152+ });
153+
154+ tearDown (() {
155+ debouncer.dispose ();
156+ });
157+
158+ test ('calls callback after duration' , () async {
159+ debouncer.run (() => callbackCount++ );
160+ expect (callbackCount, 0 );
161+ await Future .delayed (const Duration (milliseconds: 150 ));
162+ expect (callbackCount, 1 );
163+ });
164+
165+ test ('debounces multiple calls' , () async {
166+ debouncer.run (() => callbackCount++ );
167+ debouncer.run (() => callbackCount++ );
168+ debouncer.run (() => callbackCount++ );
169+ expect (callbackCount, 0 );
170+ await Future .delayed (const Duration (milliseconds: 150 ));
171+ expect (callbackCount, 1 );
172+ });
173+
174+ test ('calls callback after multiple calls with delay' , () async {
175+ debouncer.run (() => callbackCount++ );
176+ await Future .delayed (const Duration (milliseconds: 50 ));
177+ debouncer.run (() => callbackCount++ );
178+ await Future .delayed (const Duration (milliseconds: 50 ));
179+ debouncer.run (() => callbackCount++ );
180+ expect (callbackCount, 0 );
181+ await Future .delayed (const Duration (milliseconds: 150 ));
182+ expect (callbackCount, 1 );
183+ });
184+
185+ test ('dispose cancels timer' , () async {
186+ debouncer.run (() => callbackCount++ );
187+ debouncer.dispose ();
188+ await Future .delayed (const Duration (milliseconds: 150 ));
189+ expect (callbackCount, 0 );
190+ debouncer.run (() => callbackCount++ );
191+ await Future .delayed (const Duration (milliseconds: 150 ));
192+ expect (callbackCount, 0 );
193+ });
194+ });
195+
145196 group ('InterruptableChunkWorker' , () {
146197 late InterruptableChunkWorker worker;
147198 late List <int > indexes;
0 commit comments