Skip to content

Commit

Permalink
Allow taking screenshot after loadAppFonts(), before pump() (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy authored Nov 28, 2024
1 parent cfef3b3 commit f220020
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
2 changes: 0 additions & 2 deletions lib/src/screenshot/screenshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ ui.Image _captureImageSync(Element element) {
// ignore: unnecessary_cast
renderObject = renderObject.parent! as RenderObject;
}
assert(!renderObject.debugNeedsPaint);

final OffsetLayer layer = renderObject.debugLayer! as OffsetLayer;
final ui.Image image = layer.toImageSync(renderObject.paintBounds);
Expand Down Expand Up @@ -338,7 +337,6 @@ Future<ui.Image> _captureImage(Element element) async {
// ignore: unnecessary_cast
renderObject = renderObject.parent! as RenderObject;
}
assert(!renderObject.debugNeedsPaint);

final OffsetLayer layer = renderObject.debugLayer! as OffsetLayer;
final ui.Image image = await layer.toImage(renderObject.paintBounds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,32 @@ void main() {
);
});

testWidgets('Take screenshot of dirty tree', (tester) async {
tester.view.physicalSize = const Size(210, 210);
tester.view.devicePixelRatio = 1.0;

await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
// Banner listens to PaintingBinding.instance.systemFonts and requests repaint (markNeedsPaint)
child: Banner(
message: 'Hello',
location: BannerLocation.topEnd,
child: Container(color: Colors.white),
),
),
);

// FontLoader.load triggers PaintBinding.instance.systemFonts listeners
await loadAppFonts();
final renderObject =
spot<Banner>().spot<CustomPaint>().snapshotRenderObject();
expect(renderObject.debugNeedsPaint, isTrue);

// When elements are dirty, taking a screenshot should still work
await takeScreenshot();
});

group('Annotate Screenshot test', () {
testWidgets('Take screenshot with tap marker of the entire app',
(tester) async {
Expand All @@ -281,9 +307,9 @@ void main() {
);
expect(shot.file.existsSync(), isTrue);

final cyanPixelCoverage =
await percentageOfPixelsWithColor(shot.file, Color(0xFF00FFFF));
expect(cyanPixelCoverage, greaterThan(0.0));
final pinkPixelCoverage =
await percentageOfPixelsWithColor(shot.file, Color(0xFFFF00FF));
expect(pinkPixelCoverage, greaterThan(0.0));
});

testWidgets('Take screenshot with tap marker from a selector',
Expand All @@ -305,9 +331,9 @@ void main() {
);
expect(container.file.existsSync(), isTrue);

final cyanPixelCoverage =
await percentageOfPixelsWithColor(container.file, Color(0xFF00FFFF));
expect(cyanPixelCoverage, greaterThan(0.0));
final pinkPixelCoverage =
await percentageOfPixelsWithColor(container.file, Color(0xFFFF00FF));
expect(pinkPixelCoverage, greaterThan(0.0));
});

testWidgets('Take screenshot with tap marker from a snapshot',
Expand All @@ -330,9 +356,9 @@ void main() {
);
expect(container.file.existsSync(), isTrue);

final cyanPixelCoverage =
await percentageOfPixelsWithColor(container.file, Color(0xFF00FFFF));
expect(cyanPixelCoverage, greaterThan(0.0));
final pinkPixelCoverage =
await percentageOfPixelsWithColor(container.file, Color(0xFFFF00FF));
expect(pinkPixelCoverage, greaterThan(0.0));
});

testWidgets(
Expand Down Expand Up @@ -388,9 +414,9 @@ void main() {
);
expect(container.file.existsSync(), isTrue);

final cyanPixelCoverage =
await percentageOfPixelsWithColor(container.file, Color(0xFF00FFFF));
expect(cyanPixelCoverage, greaterThan(0.0));
final pinkPixelCoverage =
await percentageOfPixelsWithColor(container.file, Color(0xFFFF00FF));
expect(pinkPixelCoverage, greaterThan(0.0));
});

testWidgets(
Expand Down Expand Up @@ -434,26 +460,26 @@ Future<double> percentageOfPixelsWithColor(File file, Color color) async {
(await binding.runAsync(() => img.decodePngFile(file.absolute.path)))!;

// Count the number of red pixels in the image
int redPixelCount = 0;
int matchingPixels = 0;
final int totalPixelCount = image.width * image.height;

for (int y = 0; y < image.height; y++) {
for (int x = 0; x < image.width; x++) {
final pixel = image.getPixel(x, y);
final color = Color.fromARGB(
final c = Color.fromARGB(
pixel.a.toInt(),
pixel.r.toInt(),
pixel.g.toInt(),
pixel.b.toInt(),
);
if (color == Color(0xffff0000)) {
redPixelCount++;
if (c == color) {
matchingPixels++;
}
}
}
// Calculate the red pixel coverage percentage
final double redPixelCoverage = redPixelCount / totalPixelCount;
return redPixelCoverage;
final double coverage = matchingPixels / totalPixelCount;
return coverage;
}

/// The line number of this function call
Expand Down

0 comments on commit f220020

Please sign in to comment.