Skip to content

Commit

Permalink
Merge branch 'main' into chore/flutter-3.22
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasleyba committed Jun 27, 2024
2 parents 23deeec + a89a511 commit 0e0f505
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generate_flutter_news_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
run: git stash pop

- name: Create Pull Request With Required Changes
uses: peter-evans/create-pull-request@v6.0.5
uses: peter-evans/create-pull-request@v6.1.0
with:
branch: chore/generate-flutter-news-template
commit-message: "chore: generate flutter_news_template"
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/news_blocks_ui.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,3 @@ jobs:
flutter_version: 3.22.2
working_directory: flutter_news_example/packages/news_blocks_ui
coverage_excludes: "lib/src/generated/*.dart"
# Due to incompability with custom goldenFileComparator,
# reference: flutter_news_example/packages/news_blocks_ui/test/helpers/tolerant_comparator.dart
test_optimization: false
14 changes: 7 additions & 7 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@tsconfig/docusaurus": "^2.0.3",
"eslint": "^8.57.0",
"prettier": "^3.3.2",
"typescript": "^5.4.5"
"typescript": "^5.5.2"
},
"browserslist": {
"production": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,44 @@
// ignore_for_file: avoid_print

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

/// A comparator for golden tests that allows for a small difference in pixels.
///
/// This comparator performs a pixel-for-pixel comparison of the decoded PNGs,
/// returning true only if the difference is less than [differenceTolerance]%.
/// In case of failure this comparator will provide output to illustrate the
/// difference.
class TolerantComparator extends LocalFileComparator {
TolerantComparator.from(
super.testFile, {
required LocalFileComparator comparator,
}) : _comparator = comparator;

/// The difference tolerance that this comparator allows for.
///
/// If compared files produce less than [differenceTolerance]% difference,
/// then the test is accepted. Otherwise, the test fails.
static const differenceTolerance = .06;

final LocalFileComparator _comparator;
class _TolerantGoldenFileComparator extends LocalFileComparator {
_TolerantGoldenFileComparator(
super.testFile,
);

@override
Uri getTestUri(Uri key, int? version) => _comparator.getTestUri(key, version);
/// How much the golden image can differ from the test image.
///
/// It is expected to be between 0 and 1.
/// Where 0 is no difference (same image)
/// and 1 is the maximum difference (completely different images).
static const double _precisionTolerance = .06;

@override
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
final result = await GoldenFileComparator.compareLists(
imageBytes,
await getGoldenBytes(golden),
);
if (!result.passed) {
final error = await generateFailureOutput(result, golden, basedir);
if (result.diffPercent >= differenceTolerance) {
throw FlutterError(error);
} else {
print(
'Warning - golden differed less than $differenceTolerance% '
'(${result.diffPercent}%). Ignoring failure but logging the error.',
);
print(error);
}

final passed = result.passed || result.diffPercent <= _precisionTolerance;
if (passed) {
result.dispose();
return true;
}
return true;

final error = await generateFailureOutput(result, golden, basedir);
result.dispose();
throw FlutterError(error);
}
}

/// Sets [TolerantComparator] as the default golden file comparator in tests.
void setUpTolerantComparator() {
final oldComparator = goldenFileComparator as LocalFileComparator;
final newComparator = TolerantComparator.from(
comparator: oldComparator,
Uri.parse('${oldComparator.basedir}test'),
);
expect(oldComparator.basedir, newComparator.basedir);
/// Sets [_TolerantGoldenFileComparator] as the default golden file comparator
/// in tests.
void setUpTolerantComparator(String testPath) {
final oldComparator = goldenFileComparator;
final newComparator = _TolerantGoldenFileComparator(Uri.parse(testPath));

goldenFileComparator = newComparator;

addTearDown(() => goldenFileComparator = oldComparator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ void main() {
const premiumText = 'Subscriber Exclusive';

group('ArticleIntroduction', () {
setUpAll(setUpTolerantComparator);
setUpAll(
() => setUpTolerantComparator('test/src/article_introduction_test.dart'),
);

final technologyArticleIntroduction = ArticleIntroductionBlock(
category: category,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import '../helpers/helpers.dart';

void main() {
group('DividerHorizontal', () {
setUpAll(setUpTolerantComparator);
setUpAll(
() => setUpTolerantComparator('test/src/divider_horizontal_test.dart'),
);

testWidgets('renders correctly', (tester) async {
final widget = Center(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ void main() {
'and prices are finally falling';

group('PostLarge', () {
setUpAll(setUpTolerantComparator);
setUpAll(
() => setUpTolerantComparator('test/src/post_large/post_large_test.dart'),
);

group('renders correctly overlaid ', () {
testWidgets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ void main() {
// Change the default factory
databaseFactory = databaseFactoryFfi;

setUpTolerantComparator();
setUpTolerantComparator(
'test/src/post_medium/post_medium_test.dart',
);
setUpMockPathProvider();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() {
// Change the default factory
databaseFactory = databaseFactoryFfi;

setUpTolerantComparator();
setUpTolerantComparator('test/src/post_small_test.dart');
setUpMockPathProvider();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import '../helpers/helpers.dart';

void main() {
group('SectionHeader', () {
setUpAll(setUpTolerantComparator);
setUpAll(
() => setUpTolerantComparator('test/src/section_header_test.dart'),
);

testWidgets('renders correctly without action', (tester) async {
const widget = Center(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import '../helpers/helpers.dart';

void main() {
group('Spacer', () {
setUpAll(setUpTolerantComparator);
setUpAll(
() => setUpTolerantComparator('test/src/spacer_test.dart'),
);

testWidgets('renders correctly for extraSmall spacing', (tester) async {
final widget = ColoredBox(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import '../helpers/helpers.dart';

void main() {
group('TextCaption', () {
setUpAll(setUpTolerantComparator);
setUpAll(
() => setUpTolerantComparator('test/src/text_caption_test.dart'),
);

testWidgets(
'renders correctly '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import '../helpers/helpers.dart';

void main() {
group('TextHeadline', () {
setUpAll(setUpTolerantComparator);
setUpAll(
() => setUpTolerantComparator('test/src/text_headline_test.dart'),
);

testWidgets('renders correctly', (tester) async {
final widget = Center(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import '../helpers/helpers.dart';

void main() {
group('TextLeadParagraph', () {
setUpAll(setUpTolerantComparator);
setUpAll(
() => setUpTolerantComparator('test/src/text_lead_paragraph_test.dart'),
);

testWidgets('renders correctly', (tester) async {
final widget = Center(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import '../helpers/helpers.dart';

void main() {
group('TextParagraph', () {
setUpAll(setUpTolerantComparator);

setUpAll(
() => setUpTolerantComparator('test/src/text_paragraph_test.dart'),
);
testWidgets('renders correctly', (tester) async {
final widget = Center(
child: TextParagraph(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main() {
// Change the default factory
databaseFactory = databaseFactoryFfi;

setUpTolerantComparator();
setUpTolerantComparator('test/src/trending_story_test.dart');
setUpMockPathProvider();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ void main() {
() {
final fakeVideoPlayerPlatform = FakeVideoPlayerPlatform();
VideoPlayerPlatform.instance = fakeVideoPlayerPlatform;
setUpTolerantComparator();
setUpTolerantComparator(
'test/src/video_introduction_test.dart',
);
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,28 +152,26 @@ void main() {
});

testWidgets('renders AdWidget when ad is loaded', (tester) async {
await tester.runAsync(() async {
ad = BannerAd(
size: AdSize.banner,
adUnitId: BannerAdContent.androidTestUnitId,
listener: BannerAdListener(),
request: AdRequest(),
);
ad = BannerAd(
size: AdSize.banner,
adUnitId: BannerAdContent.androidTestUnitId,
listener: BannerAdListener(),
request: AdRequest(),
);

await tester.pumpApp(
BannerAdContent(
size: BannerAdSize.normal,
adBuilder: adBuilder,
currentPlatform: platform,
),
);
await tester.pumpApp(
BannerAdContent(
size: BannerAdSize.normal,
adBuilder: adBuilder,
currentPlatform: platform,
),
);

capturedListener.onAdLoaded!(ad);
await tester.pumpAndSettle();
capturedListener.onAdLoaded!(ad);
await tester.pumpAndSettle();

expect(find.byType(AdWidget), findsOneWidget);
expect(find.byType(ProgressIndicator), findsNothing);
});
expect(find.byType(AdWidget), findsOneWidget);
expect(find.byType(ProgressIndicator), findsNothing);
});

testWidgets('uses AdSize.banner for BannerAdSize.normal', (tester) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import '../../helpers/helpers.dart';

void main() {
group('PostFooter', () {
setUpAll(setUpTolerantComparator);
setUpAll(
() => setUpTolerantComparator('test/src/widgets/post_footer_test.dart'),
);

testWidgets('renders correctly', (tester) async {
await tester.pumpApp(
Expand Down

0 comments on commit 0e0f505

Please sign in to comment.