Files
device-code-app/test/widget_test.dart
T
2026-10-20 12:45:50 +07:00

115 lines
3.6 KiB
Dart

import 'package:device_code_app/app.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
final fixedDate = DateTime(2026, 10, 20);
// Each test runs once per platform; the variant sets and restores
// debugDefaultTargetPlatformOverride around the test body.
const variant = TargetPlatformVariant({
TargetPlatform.android,
TargetPlatform.iOS,
});
Future<void> pumpApp(WidgetTester tester) async {
await tester.pumpWidget(DeviceCodeApp(initialDate: fixedDate));
await tester.pumpAndSettle();
}
testWidgets('uses the native widget set', (tester) async {
await pumpApp(tester);
final platform = debugDefaultTargetPlatformOverride;
if (platform == TargetPlatform.iOS) {
expect(find.byType(CupertinoApp), findsOneWidget);
expect(find.byType(CupertinoTextFormFieldRow), findsOneWidget);
expect(find.byType(MaterialApp), findsNothing);
} else {
expect(find.byType(MaterialApp), findsOneWidget);
expect(find.byType(TextField), findsOneWidget);
expect(
Theme.of(tester.element(find.byType(Scaffold))).useMaterial3,
isTrue,
);
expect(find.byType(CupertinoApp), findsNothing);
}
}, variant: variant);
testWidgets('shows the date and no code until the MAC is complete', (
tester,
) async {
await pumpApp(tester);
expect(find.textContaining('20 Oct 2026'), findsWidgets);
expect(find.textContaining('20261020'), findsWidgets);
expect(find.text('••••••••'), findsOneWidget);
expect(find.text('24720116'), findsNothing);
await tester.enterText(find.byKey(const Key('mac-field')), '742584C002');
await tester.pumpAndSettle();
expect(find.text('••••••••'), findsOneWidget);
expect(find.text('24720116'), findsNothing);
}, variant: variant);
testWidgets(
'typing unformatted uppercase 742584C0024A on 20261020 shows 24720116',
(tester) async {
await pumpApp(tester);
await tester.enterText(
find.byKey(const Key('mac-field')),
'742584C0024A',
);
await tester.pumpAndSettle();
final field = tester.widget<EditableText>(
find.descendant(
of: find.byKey(const Key('mac-field')),
matching: find.byType(EditableText),
),
);
expect(field.controller.text, '74:25:84:c0:02:4a');
final code = tester.widget<SelectableText>(
find.byKey(const Key('device-code')),
);
expect(code.data, '24720116');
expect(find.text('24720116'), findsOneWidget);
},
variant: variant,
);
testWidgets('copy button puts the code on the clipboard', (tester) async {
await pumpApp(tester);
String? copied;
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
(call) async {
if (call.method == 'Clipboard.setData') {
copied = (call.arguments as Map)['text'] as String;
}
return null;
},
);
addTearDown(
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
null,
),
);
await tester.enterText(
find.byKey(const Key('mac-field')),
'ff:ff:ff:ff:ff:ff',
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('copy-button')));
await tester.pumpAndSettle();
expect(copied, '56623865');
expect(find.text('Copied'), findsOneWidget);
}, variant: variant);
}