-
-
Notifications
You must be signed in to change notification settings - Fork 576
fix : Add tests for custom painters #3306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 8 commits
c0a9348
9a215b5
95b5010
71ef8ab
0cbb227
66c3cc0
d80832c
c1775aa
8f105b0
932f9e2
c59e385
fe56b58
d91487e
f2c9fe4
bc5ecf1
292efc3
4cb01e5
edb0316
de496a7
af4c7f9
7b76e00
c29c133
3c109dc
4e66e2d
18d79c0
8d44035
0df7e63
16f87b0
9e21e03
b245f99
2d2b9f5
04d6f2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:flutter/material.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:flutter_test/flutter_test.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:talawa/custom_painters/language_icon.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'painter_test_helpers.dart'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void main() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group('LanguageIcon Painter Tests', () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testWidgets('should render LanguageIcon correctly (golden test)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (WidgetTester tester) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const key = ValueKey('language_icon_painter'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await tester.pumpWidget( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| buildPainterTestWidget( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| painter: LanguageIcon(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key: key, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await tester.pumpAndSettle(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expectLater( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| find.byKey(key), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| matchesGoldenFile('goldens/language_icon.png'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('LanguageIcon shouldRepaint returns false', () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final painter1 = LanguageIcon(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final painter2 = LanguageIcon(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(painter1.shouldRepaint(painter2), isFalse); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| runPaintSmokeTests(() => LanguageIcon()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
1
to
36
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import 'package:flutter/material.dart'; | |
| import 'package:flutter_test/flutter_test.dart'; | |
| import 'package:talawa/custom_painters/language_icon.dart'; | |
| void main() { | |
| group('LanguageIcon Painter Tests', () { | |
| testWidgets('should render LanguageIcon correctly (golden test)', | |
| (WidgetTester tester) async { | |
| // Build a CustomPaint widget with LanguageIcon painter | |
| const key = ValueKey('language_icon_painter'); | |
| await tester.pumpWidget( | |
| MaterialApp( | |
| home: Scaffold( | |
| body: Center( | |
| child: SizedBox( | |
| width: 200, | |
| height: 200, | |
| child: CustomPaint( | |
| key: key, | |
| painter: LanguageIcon(), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| // Wait for the widget to settle | |
| await tester.pumpAndSettle(); | |
| // Compare with golden file | |
| await expectLater( | |
| find.byKey(key), | |
| matchesGoldenFile('goldens/language_icon.png'), | |
| ); | |
| }); | |
| test('LanguageIcon shouldRepaint returns false', () { | |
| // Create two instances of LanguageIcon | |
| final painter1 = LanguageIcon(); | |
| final painter2 = LanguageIcon(); | |
| // LanguageIcon should return false (never repaints) | |
| expect(painter1.shouldRepaint(painter2), isFalse); | |
| }); | |
| test('LanguageIcon shouldRepaint with same instance returns false', () { | |
| // Create one instance | |
| final painter = LanguageIcon(); | |
| // Even with the same instance, should return false | |
| expect(painter.shouldRepaint(painter), isFalse); | |
| }); | |
| }); | |
| } | |
| // This file previously contained duplicate tests for LanguageIcon. | |
| // The comprehensive tests now reside in test/custom_painters/language_icon_test.dart. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import 'dart:ui' as ui; | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| /// Helper function to build a test widget with a CustomPaint painter. | ||
| /// | ||
| /// This helper creates a standard MaterialApp scaffold with a CustomPaint | ||
| /// widget for testing custom painters in a consistent way across all tests. | ||
| /// | ||
| /// **params**: | ||
| /// * `painter`: The CustomPainter instance to test. | ||
| /// * `key`: A unique key to identify the CustomPaint widget in tests. | ||
| /// * `width`: The width of the canvas (default: 200). | ||
| /// * `height`: The height of the canvas (default: 200). | ||
| Widget buildPainterTestWidget({ | ||
| required CustomPainter painter, | ||
| required Key key, | ||
| double width = 200, | ||
| double height = 200, | ||
| }) { | ||
| return MaterialApp( | ||
| home: Scaffold( | ||
| body: Center( | ||
| child: SizedBox( | ||
| width: width, | ||
| height: height, | ||
| child: CustomPaint( | ||
| key: key, | ||
| painter: painter, | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /// Runs smoke tests for the [paint] method of a [CustomPainter]. | ||
| /// | ||
| /// Verifies that the [paint] method completes without throwing exceptions | ||
| /// for various standard and edge-case canvas sizes. | ||
| /// | ||
| /// **params**: | ||
| /// * `createPainter`: A factory function that returns a new instance of the [CustomPainter] to test. | ||
| void runPaintSmokeTests(CustomPainter Function() createPainter) { | ||
| group('paint() method smoke tests', () { | ||
| final sizes = [ | ||
| const MapEntry('normal size', Size(200, 200)), | ||
| const MapEntry('zero size', Size.zero), | ||
| const MapEntry('minimal size', Size(1, 1)), | ||
| const MapEntry('oversized', Size(1000, 1000)), | ||
| const MapEntry('non-square', Size(300, 150)), | ||
| ]; | ||
|
|
||
| for (final entry in sizes) { | ||
| test('paint() completes without throwing on ${entry.key} canvas', () { | ||
| final painter = createPainter(); | ||
| final recorder = ui.PictureRecorder(); | ||
| final canvas = Canvas(recorder); | ||
| final size = entry.value; | ||
|
|
||
| expect(() => painter.paint(canvas, size), returnsNormally); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:talawa/custom_painters/talawa_logo.dart'; | ||
|
|
||
| import 'painter_test_helpers.dart'; | ||
|
|
||
| void main() { | ||
| group('AppLogo Painter Tests', () { | ||
| testWidgets('should render AppLogo correctly (golden test)', | ||
| (WidgetTester tester) async { | ||
| const key = ValueKey('app_logo_painter'); | ||
| await tester.pumpWidget( | ||
| buildPainterTestWidget( | ||
| painter: AppLogo(), | ||
| key: key, | ||
| ), | ||
| ); | ||
|
|
||
| await tester.pumpAndSettle(); | ||
|
|
||
| await expectLater( | ||
| find.byKey(key), | ||
| matchesGoldenFile('goldens/talawa_logo.png'), | ||
|
||
| ); | ||
| }); | ||
|
|
||
| test('AppLogo shouldRepaint returns true', () { | ||
| final painter1 = AppLogo(); | ||
| final painter2 = AppLogo(); | ||
|
|
||
| expect(painter1.shouldRepaint(painter2), isTrue); | ||
| }); | ||
|
|
||
| runPaintSmokeTests(() => AppLogo()); | ||
| }); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:talawa/custom_painters/telegram_logo.dart'; | ||
|
|
||
| import 'painter_test_helpers.dart'; | ||
|
|
||
| void main() { | ||
| group('TelegramLogo Painter Tests', () { | ||
| testWidgets('should render TelegramLogo correctly (golden test)', | ||
| (WidgetTester tester) async { | ||
| const key = ValueKey('telegram_logo_painter'); | ||
| await tester.pumpWidget( | ||
| buildPainterTestWidget( | ||
| painter: TelegramLogo(), | ||
| key: key, | ||
| ), | ||
| ); | ||
|
|
||
| await tester.pumpAndSettle(); | ||
|
|
||
| await expectLater( | ||
| find.byKey(key), | ||
| matchesGoldenFile('goldens/telegram_logo.png'), | ||
|
||
| ); | ||
| }); | ||
|
|
||
| test('TelegramLogo shouldRepaint returns true', () { | ||
| final painter1 = TelegramLogo(); | ||
| final painter2 = TelegramLogo(); | ||
|
|
||
| expect(painter1.shouldRepaint(painter2), isTrue); | ||
| }); | ||
|
|
||
| runPaintSmokeTests(() => TelegramLogo()); | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:talawa/custom_painters/whatsapp_logo.dart'; | ||
|
|
||
| import 'painter_test_helpers.dart'; | ||
|
|
||
| void main() { | ||
| group('WhatsappLogo Painter Tests', () { | ||
| testWidgets('should render WhatsappLogo correctly (golden test)', | ||
| (WidgetTester tester) async { | ||
| const key = ValueKey('whatsapp_logo_painter'); | ||
| await tester.pumpWidget( | ||
| buildPainterTestWidget( | ||
| painter: WhatsappLogo(), | ||
| key: key, | ||
| ), | ||
| ); | ||
|
|
||
| await tester.pumpAndSettle(); | ||
|
|
||
| await expectLater( | ||
| find.byKey(key), | ||
| matchesGoldenFile('goldens/whatsapp_logo.png'), | ||
|
||
| ); | ||
| }); | ||
|
|
||
| test('WhatsappLogo shouldRepaint returns true', () { | ||
| final painter1 = WhatsappLogo(); | ||
| final painter2 = WhatsappLogo(); | ||
|
|
||
| expect(painter1.shouldRepaint(painter2), isTrue); | ||
| }); | ||
|
|
||
| runPaintSmokeTests(() => WhatsappLogo()); | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The golden file name 'language_icon.png' is inconsistent with the existing golden file naming convention. The existing test uses 'language_icon_golden.png' (test/custom_painters/goldens/language_icon_golden.png).
For consistency with the existing codebase pattern, consider renaming:
And update the corresponding matchesGoldenFile calls in the tests.