forked from SatoshiPortal/bullbitcoin-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightning_address_activation_screen_test.dart
More file actions
339 lines (290 loc) · 10.3 KB
/
Copy pathlightning_address_activation_screen_test.dart
File metadata and controls
339 lines (290 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import 'package:bb_mobile/core/themes/app_theme.dart';
import 'package:bb_mobile/features/lightning_address/domain/lightning_address_registration.dart';
import 'package:bb_mobile/features/lightning_address/presentation/lightning_address_activation_cubit.dart';
import 'package:bb_mobile/features/lightning_address/presentation/lightning_address_activation_state.dart';
import 'package:bb_mobile/features/lightning_address/ui/screens/lightning_address_activation_screen.dart';
import 'package:bb_mobile/generated/l10n/localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:qr_flutter/qr_flutter.dart';
void main() {
String? copiedText;
setUp(() {
copiedText = null;
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, (call) async {
if (call.method == 'Clipboard.setData') {
copiedText =
(call.arguments as Map<Object?, Object?>)['text'] as String?;
}
return null;
});
});
tearDown(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, null);
});
testWidgets('old-server state hides every name and availability control', (
tester,
) async {
await _pump(
tester,
const LightningAddressActivationState(
status: LightningAddressActivationStatus.unsupported,
),
);
expect(find.text('Permanent names unavailable'), findsOneWidget);
expect(find.byType(TextFormField), findsNothing);
expect(
find.byKey(const Key('lightning_address_online_switch')),
findsNothing,
);
});
testWidgets('first claim shows one input and the permanence explanation', (
tester,
) async {
await _pump(
tester,
const LightningAddressActivationState(
status: LightningAddressActivationStatus.idle,
permanentNamesSupported: true,
),
);
expect(find.byType(TextFormField), findsOneWidget);
expect(find.text('Claim your nym'), findsNWidgets(2));
expect(
find.text(
'A pseudonym tied to your BULL wallet. It allows anyone to send you '
'Bitcoin payments anonymously just by typing your nym in their wallet, '
'and you will receive it in your instant payments wallet. Choose wisely: '
"you can't change it without creating an entirely new Bitcoin wallet.",
),
findsOneWidget,
);
await tester.ensureVisible(find.text('Claim your nym').last);
await tester.tap(find.text('Claim your nym').last);
await tester.pumpAndSettle();
expect(find.text('Claim this permanent name?'), findsNothing);
});
testWidgets(
'connection failure uses Lightning Address language and retries',
(tester) async {
final cubit = await _pump(
tester,
const LightningAddressActivationState(
status: LightningAddressActivationStatus.failure,
failure: LightningAddressActivationFailure.capabilityUnavailable,
),
);
expect(find.text('Lightning Address unavailable'), findsOneWidget);
expect(
find.text(
'Your Lightning Address could not be loaded. '
'Check your connection and try again.',
),
findsOneWidget,
);
expect(find.textContaining('permanent-name support'), findsNothing);
await tester.tap(find.text('Try again'));
await tester.pump();
expect(cubit.loadCalls, 2);
},
);
testWidgets('active nym shows its address and hides secondary metadata', (
tester,
) async {
await _pump(tester, _ownedState(online: true));
expect(find.byType(TextFormField), findsNothing);
expect(find.text('Lightning address is active'), findsOneWidget);
expect(find.text('alice@pay2.bull-wallet.com'), findsOneWidget);
expect(find.text('Name'), findsNothing);
expect(
find.text('This lifetime name is permanent and read-only.'),
findsNothing,
);
expect(find.text('Advanced settings'), findsOneWidget);
expect(
find.byKey(const Key('lightning_address_online_switch')),
findsNothing,
);
});
testWidgets('inactive nym retains the same server-owned address', (
tester,
) async {
await _pump(tester, _ownedState(online: false));
expect(find.text('Lightning Address is inactive'), findsOneWidget);
expect(find.text('alice@pay2.bull-wallet.com'), findsOneWidget);
expect(find.text('Name'), findsNothing);
expect(find.text('Advanced settings'), findsOneWidget);
});
testWidgets(
'address tap opens QR and copy actions without explorer actions',
(tester) async {
await _pump(tester, _ownedState(online: true));
await tester.tap(find.byKey(const Key('lightning_address_tile')));
await tester.pumpAndSettle();
expect(find.byType(QrImageView), findsOneWidget);
expect(find.text('Tap to copy'), findsOneWidget);
expect(find.text('Copy Link'), findsNothing);
expect(find.text('View in Explorer'), findsNothing);
await tester.tap(find.text('Tap to copy'));
await tester.pumpAndSettle();
expect(copiedText, 'alice@pay2.bull-wallet.com');
await tester.pump(const Duration(seconds: 4));
},
);
testWidgets('long press copies the Lightning Address', (tester) async {
await _pump(tester, _ownedState(online: true));
await tester.longPress(find.byKey(const Key('lightning_address_tile')));
await tester.pump();
expect(copiedText, 'alice@pay2.bull-wallet.com');
await tester.pump(const Duration(seconds: 4));
});
testWidgets('missing lookup address is explicit and reloadable', (
tester,
) async {
final cubit = await _pump(
tester,
const LightningAddressActivationState(
status: LightningAddressActivationStatus.addressUnavailable,
nym: 'alice',
permanentNamesSupported: true,
hasPermanentNym: true,
),
);
expect(
find.text('The server did not return your Lightning Address. Try again.'),
findsOneWidget,
);
expect(find.text('Lightning address is active'), findsNothing);
await tester.tap(find.text('Try again'));
await tester.pump();
expect(cubit.loadCalls, 2);
});
testWidgets('turning off confirms first and says the other products stay', (
tester,
) async {
final cubit = await _pump(tester, _ownedState(online: true));
await _tapOnlineSwitch(tester);
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text('Turn off Lightning Address?'), findsOneWidget);
expect(
find.text(
'People will no longer be able to send funds to your Lightning '
'Address. Your permanent names remain claimed, and your Donation Page '
'and Point of Sale stay online and keep accepting payments.',
),
findsOneWidget,
);
// Nothing is deactivated until the merchant confirms.
expect(cubit.deactivateCalls, 0);
await tester.tap(find.text('Turn off'));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
expect(cubit.deactivateCalls, 1);
});
testWidgets('cancelling the turn-off confirmation leaves it on', (
tester,
) async {
final cubit = await _pump(tester, _ownedState(online: true));
await _tapOnlineSwitch(tester);
await tester.tap(find.text('Keep online'));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
expect(cubit.deactivateCalls, 0);
final switchTile = tester.widget<SwitchListTile>(
find.byKey(const Key('lightning_address_online_switch')),
);
expect(switchTile.value, isTrue);
});
testWidgets('turning on reuses the owned nym without another confirmation', (
tester,
) async {
final cubit = await _pump(tester, _ownedState(online: false));
await _tapOnlineSwitch(tester);
expect(find.byType(AlertDialog), findsNothing);
expect(cubit.activateExistingCalls, 1);
});
}
/// Opens Advanced Settings and flips the availability switch inside it.
Future<void> _tapOnlineSwitch(WidgetTester tester) async {
await tester.tap(find.text('Advanced settings'));
await tester.pumpAndSettle();
await tester.ensureVisible(
find.byKey(const Key('lightning_address_online_switch')),
);
await tester.tap(find.byKey(const Key('lightning_address_online_switch')));
await tester.pumpAndSettle();
}
LightningAddressActivationState _ownedState({required bool online}) {
return LightningAddressActivationState(
status: online
? LightningAddressActivationStatus.active
: LightningAddressActivationStatus.inactive,
nym: 'alice',
registeredAddress: 'alice@pay2.bull-wallet.com',
permanentNamesSupported: true,
hasPermanentNym: true,
permanentNameQuota: const LightningAddressPermanentNameQuota(
used: 1,
cap: 1,
remaining: 0,
),
);
}
Future<_StubCubit> _pump(
WidgetTester tester,
LightningAddressActivationState state,
) async {
final cubit = _StubCubit(state);
addTearDown(cubit.close);
await tester.pumpWidget(
MaterialApp(
theme: AppTheme.themeData(AppThemeType.light),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
locale: const Locale('en'),
home: BlocProvider<LightningAddressActivationCubit>.value(
value: cubit,
child: const LightningAddressActivationScreen(),
),
),
);
await tester.pump();
return cubit;
}
class _StubCubit extends Cubit<LightningAddressActivationState>
implements LightningAddressActivationCubit {
_StubCubit(super.initialState);
int activateExistingCalls = 0;
int deactivateCalls = 0;
int loadCalls = 0;
@override
Future<void> load() async {
loadCalls += 1;
}
@override
void nymChanged(String value) {}
@override
void showRegistrationForm() {}
@override
LightningAddressActivationFailure? validateNym(String value) => null;
@override
Future<void> submit() async {}
@override
Future<void> activateExisting() async {
activateExistingCalls += 1;
}
@override
Future<void> deactivate() async {
deactivateCalls += 1;
}
@override
Future<void> updateWalletBehavior({
required String walletId,
bool? hideOnHome,
bool? autoSweepEnabled,
}) async {}
}