Skip to content

Commit

Permalink
test(TM-48505): Widget Test case for custom widget having copying cap…
Browse files Browse the repository at this point in the history
…ability
  • Loading branch information
github-actions committed Nov 22, 2024
1 parent 4fba337 commit e8420e9
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/src/components/organisms/selectable_widget.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -42,6 +44,7 @@ class ZdsSelectableWidget extends StatefulWidget {

class _ZdsSelectableWidgetState extends State<ZdsSelectableWidget> {
bool isSelected = false;
Timer? _selectionTimer;

void toggleSelection() {
setState(() {
Expand All @@ -54,13 +57,20 @@ class _ZdsSelectableWidgetState extends State<ZdsSelectableWidget> {
return document.body?.text ?? '';
}

@override
void dispose() {
_selectionTimer?.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
if (!(widget.copyable ?? false)) return widget.child;
final zeta = Zeta.of(context).colors;
return GestureDetector(
child: ColoredBox(color: isSelected ? zeta.primary.surface : Colors.transparent, child: widget.child),
onLongPress: () async {
if (isSelected) return;
toggleSelection();
if (isSelected) {
var copiedText = widget.textToCopy;
Expand Down Expand Up @@ -88,8 +98,8 @@ class _ZdsSelectableWidgetState extends State<ZdsSelectableWidget> {
),
),
);
_selectionTimer = Timer(const Duration(seconds: 4), toggleSelection);
}
Future.delayed(const Duration(seconds: 4), toggleSelection);
},
);
}
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/test_app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:zds_flutter/zds_flutter.dart';

class TestApp extends StatelessWidget {
const TestApp({super.key, required this.builder});
final WidgetBuilder builder;
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: <LocalizationsDelegate<dynamic>>[
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
ComponentDelegate(testing: true),
],
home: ZetaProvider(
builder: (context, themeData, themeMode) {
return Scaffold(body: builder.call(context));
},
),
);
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(ObjectFlagProperty<WidgetBuilder>.has('builder', builder));
}
}
105 changes: 105 additions & 0 deletions test/lib/src/components/organisms/selectable_widget_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:zds_flutter/zds_flutter.dart';
import '../../../../fixtures/test_app.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
var clipboardContent = 'Selectable Text';
setUp(() {
// Mock the clipboard data
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform,
(MethodCall methodCall) async {
if (methodCall.method == 'Clipboard.getData') {
return {'text': clipboardContent};
}
return null;
});
});
tearDown(() {
// Remove the mock handler
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, null);
});
group('ZdsSelectableWidget Tests', () {
testWidgets('Displays child widget', (WidgetTester tester) async {
const childWidget = Text('Selectable Text');
clipboardContent = 'Selectable Text';
await tester.pumpWidget(
TestApp(
builder: (BuildContext context) {
return const ZdsSelectableWidget(
textToCopy: 'Selectable Text',
child: childWidget,
);
},
),
);
await tester.pump(const Duration(milliseconds: 100));
expect(find.byWidget(childWidget), findsOneWidget);
});
testWidgets('Copies plain text to clipboard on long press', (WidgetTester tester) async {
const childWidget = Text('Selectable Text');
clipboardContent = 'Selectable Text';
await tester.pumpWidget(
TestApp(
builder: (BuildContext context) {
return const ZdsSelectableWidget(
textToCopy: 'Selectable Text',
copyable: true,
child: childWidget,
);
},
),
);
await tester.pump(const Duration(milliseconds: 100));
await tester.longPress(find.byWidget(childWidget));
await tester.pumpAndSettle();
final clipboardData = await Clipboard.getData('text/plain');
expect(clipboardData?.text, clipboardContent);
});
testWidgets('Copies HTML text to clipboard as plain text on long press', (WidgetTester tester) async {
const childWidget = Text('Selectable HTML Text');
const htmlText = '<p>Selectable <b>HTML</b> Text</p>';
clipboardContent = 'Selectable HTML Text';
await tester.pumpWidget(
TestApp(
builder: (BuildContext context) {
return const ZdsSelectableWidget(
textToCopy: htmlText,
isHtmlData: true,
copyable: true,
child: childWidget,
);
},
),
);
await tester.pump(const Duration(milliseconds: 100));
await tester.longPress(find.byWidget(childWidget));
await tester.pumpAndSettle();
final clipboardData = await Clipboard.getData('text/plain');
expect(clipboardData?.text, 'Selectable HTML Text');
});
testWidgets('Does not copy text if copyable is false', (WidgetTester tester) async {
const childWidget = Text('Non-copyable Text');
clipboardContent = '';
await tester.pumpWidget(
TestApp(
builder: (BuildContext context) {
return const ZdsSelectableWidget(
textToCopy: 'Non-copyable Text',
copyable: false,
child: childWidget,
);
},
),
);
await tester.pump(const Duration(milliseconds: 100));
await tester.longPress(find.byWidget(childWidget));
await tester.pumpAndSettle();
final clipboardData = await Clipboard.getData('text/plain');
expect(clipboardData?.text, '');
});
});
}

0 comments on commit e8420e9

Please sign in to comment.