Skip to content

Commit e6c9ac2

Browse files
committed
binding: Add stopwatch
The stopwatch gets replaced with a testable stopwatch from clock/clock in tests. We need it for the testability of stopwatches with fake async. The "normal" stopwatch gets used when not testing. This implementation was inspired by: flutter/flutter@bbdeabf Signed-off-by: Zixuan James Li <[email protected]>
1 parent 921eacd commit e6c9ac2

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/model/binding.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ abstract class ZulipBinding {
113113
/// This wraps [url_launcher.closeInAppWebView].
114114
Future<void> closeInAppWebView();
115115

116+
/// Provides access to a new stopwatch.
117+
///
118+
/// Outside tests, this just calls the [Stopwatch] constructor.
119+
Stopwatch stopwatch();
120+
116121
/// Provides device and operating system information,
117122
/// via package:device_info_plus.
118123
///
@@ -360,6 +365,9 @@ class LiveZulipBinding extends ZulipBinding {
360365
return url_launcher.closeInAppWebView();
361366
}
362367

368+
@override
369+
Stopwatch stopwatch() => Stopwatch();
370+
363371
@override
364372
Future<BaseDeviceInfo?> get deviceInfo => _deviceInfo;
365373
late Future<BaseDeviceInfo?> _deviceInfo;

test/model/binding.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:async';
22

3+
import 'package:clock/clock.dart';
34
import 'package:firebase_core/firebase_core.dart';
45
import 'package:firebase_messaging/firebase_messaging.dart';
56
import 'package:flutter/foundation.dart';
@@ -209,6 +210,9 @@ class TestZulipBinding extends ZulipBinding {
209210
_closeInAppWebViewCallCount++;
210211
}
211212

213+
@override
214+
Stopwatch stopwatch() => clock.stopwatch();
215+
212216
/// The value that `ZulipBinding.instance.deviceInfo` should return.
213217
BaseDeviceInfo deviceInfoResult = _defaultDeviceInfoResult;
214218
static const _defaultDeviceInfoResult = AndroidDeviceInfo(sdkInt: 33, release: '13');

0 commit comments

Comments
 (0)