-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(TM-48505): Widget Test case for custom widget having copying cap…
…ability
- Loading branch information
github-actions
committed
Nov 22, 2024
1 parent
4fba337
commit e8420e9
Showing
3 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
105
test/lib/src/components/organisms/selectable_widget_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ''); | ||
}); | ||
}); | ||
} |