Skip to content

Commit 8397e20

Browse files
committed
use mocktail
1 parent 19f8169 commit 8397e20

3 files changed

Lines changed: 57 additions & 556 deletions

File tree

wakelock_plus/pubspec.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ dev_dependencies:
3333
sdk: flutter
3434
flutter_lints: ^6.0.0
3535
pigeon: ^26.2.3 # dart run pigeon --input "pigeons/messages.dart"
36-
mockito: ^5.4.4
37-
build_runner: ^2.4.13
38-
36+
mocktail: ^1.0.4
37+
3938
# For information on the generic Dart part of this file, see the
4039
# following page: https://dart.dev/tools/pub/pubspec
4140

@@ -101,4 +100,4 @@ flutter:
101100
# https://flutter.dev/custom-fonts/#from-packages
102101

103102
assets:
104-
- packages/wakelock_plus/assets/no_sleep.js
103+
- packages/wakelock_plus/assets/no_sleep.js

wakelock_plus/test/wakelock_plus_linux_plugin_test.dart

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import 'package:dbus/dbus.dart';
22
import 'package:flutter_test/flutter_test.dart';
3-
import 'package:mockito/annotations.dart';
4-
import 'package:mockito/mockito.dart';
3+
import 'package:mocktail/mocktail.dart';
54
import 'package:wakelock_plus/src/wakelock_plus_linux_plugin.dart';
65
import 'package:wakelock_plus_platform_interface/wakelock_plus_platform_interface.dart';
76

8-
import 'wakelock_plus_linux_plugin_test.mocks.dart';
7+
class MockDBusClient extends Mock implements DBusClient {}
8+
9+
class MockDBusRemoteObject extends Mock implements DBusRemoteObject {}
10+
11+
class FakeDBusSignature extends Fake implements DBusSignature {}
912

10-
@GenerateMocks([DBusClient, DBusRemoteObject])
1113
void main() {
1214
TestWidgetsFlutterBinding.ensureInitialized();
1315

16+
setUpAll(() {
17+
registerFallbackValue(FakeDBusSignature());
18+
});
19+
1420
group('WakelockPlusLinuxPlugin', () {
1521
late MockDBusClient mockClient;
1622
late MockDBusRemoteObject mockPortalObject;
@@ -50,22 +56,22 @@ void main() {
5056
]);
5157

5258
when(
53-
mockPortalObject.callMethod(
59+
() => mockPortalObject.callMethod(
5460
'org.freedesktop.portal.Inhibit',
5561
'Inhibit',
56-
any,
57-
replySignature: anyNamed('replySignature'),
62+
any(),
63+
replySignature: any(named: 'replySignature'),
5864
),
5965
).thenAnswer((_) async => mockResponse);
6066

6167
await plugin.toggle(enable: true);
6268

6369
verify(
64-
mockPortalObject.callMethod(
70+
() => mockPortalObject.callMethod(
6571
'org.freedesktop.portal.Inhibit',
6672
'Inhibit',
67-
argThat(
68-
isA<List>()
73+
any(
74+
that: isA<List>()
6975
.having((l) => l.length, 'length', 3)
7076
.having((l) => l[0], 'window', isA<DBusString>())
7177
.having((l) => l[1], 'flags', isA<DBusUint32>())
@@ -84,26 +90,26 @@ void main() {
8490
]);
8591

8692
when(
87-
mockPortalObject.callMethod(
88-
any,
89-
any,
90-
any,
91-
replySignature: anyNamed('replySignature'),
93+
() => mockPortalObject.callMethod(
94+
any(),
95+
any(),
96+
any(),
97+
replySignature: any(named: 'replySignature'),
9298
),
9399
).thenAnswer((_) async => mockResponse);
94100

95101
await plugin.toggle(enable: true);
96102

97103
final captured =
98104
verify(
99-
mockPortalObject.callMethod(
100-
any,
101-
any,
102-
captureAny,
103-
replySignature: anyNamed('replySignature'),
105+
() => mockPortalObject.callMethod(
106+
any(),
107+
any(),
108+
captureAny(),
109+
replySignature: any(named: 'replySignature'),
104110
),
105111
).captured.single
106-
as List;
112+
as List<dynamic>;
107113

108114
final flags = captured[1] as DBusUint32;
109115
expect(flags.value, equals(8)); // 8 = Idle flag
@@ -115,26 +121,26 @@ void main() {
115121
]);
116122

117123
when(
118-
mockPortalObject.callMethod(
119-
any,
120-
any,
121-
any,
122-
replySignature: anyNamed('replySignature'),
124+
() => mockPortalObject.callMethod(
125+
any(),
126+
any(),
127+
any(),
128+
replySignature: any(named: 'replySignature'),
123129
),
124130
).thenAnswer((_) async => mockResponse);
125131

126132
await plugin.toggle(enable: true);
127133

128134
final captured =
129135
verify(
130-
mockPortalObject.callMethod(
131-
any,
132-
any,
133-
captureAny,
134-
replySignature: anyNamed('replySignature'),
136+
() => mockPortalObject.callMethod(
137+
any(),
138+
any(),
139+
captureAny(),
140+
replySignature: any(named: 'replySignature'),
135141
),
136142
).captured.single
137-
as List;
143+
as List<dynamic>;
138144

139145
final options = captured[2] as DBusDict;
140146
expect(options.children.containsKey(DBusString('reason')), isTrue);
@@ -150,22 +156,22 @@ void main() {
150156
final mockCloseResponse = DBusMethodSuccessResponse([]);
151157

152158
when(
153-
mockPortalObject.callMethod(
159+
() => mockPortalObject.callMethod(
154160
'org.freedesktop.portal.Inhibit',
155161
'Inhibit',
156-
any,
157-
replySignature: anyNamed('replySignature'),
162+
any(),
163+
replySignature: any(named: 'replySignature'),
158164
),
159165
).thenAnswer((_) async => mockInhibitResponse);
160166

161167
// Mock the Close call on DBusClient
162168
when(
163-
mockClient.callMethod(
169+
() => mockClient.callMethod(
164170
destination: 'org.freedesktop.portal.Desktop',
165171
path: handlePath,
166172
interface: 'org.freedesktop.portal.Request',
167173
name: 'Close',
168-
values: [],
174+
values: any(named: 'values'),
169175
replySignature: DBusSignature.empty,
170176
),
171177
).thenAnswer((_) async => mockCloseResponse);
@@ -180,7 +186,7 @@ void main() {
180186

181187
// Verify Close was called
182188
verify(
183-
mockClient.callMethod(
189+
() => mockClient.callMethod(
184190
destination: 'org.freedesktop.portal.Desktop',
185191
path: handlePath,
186192
interface: 'org.freedesktop.portal.Request',
@@ -195,11 +201,11 @@ void main() {
195201
await plugin.toggle(enable: false);
196202
expect(await plugin.enabled, isFalse);
197203
verifyNever(
198-
mockPortalObject.callMethod(
199-
any,
200-
any,
201-
any,
202-
replySignature: anyNamed('replySignature'),
204+
() => mockPortalObject.callMethod(
205+
any(),
206+
any(),
207+
any(),
208+
replySignature: any(named: 'replySignature'),
203209
),
204210
);
205211
});
@@ -216,11 +222,11 @@ void main() {
216222
]);
217223

218224
when(
219-
mockPortalObject.callMethod(
220-
any,
221-
any,
222-
any,
223-
replySignature: anyNamed('replySignature'),
225+
() => mockPortalObject.callMethod(
226+
any(),
227+
any(),
228+
any(),
229+
replySignature: any(named: 'replySignature'),
224230
),
225231
).thenAnswer((_) async => mockResponse);
226232

0 commit comments

Comments
 (0)