m3_lightmeter/test_driver/screenshot_driver.dart

143 lines
4.6 KiB
Dart
Raw Normal View History

import 'dart:io';
2024-05-11 12:59:28 +00:00
import 'dart:typed_data';
import 'package:image/image.dart';
import 'package:integration_test/integration_test_driver_extended.dart';
2024-05-11 12:59:28 +00:00
import '../screenshots/devices_config.dart';
import '../screenshots/screenshot_args.dart';
2024-05-11 14:07:03 +00:00
import '../screenshots/screenshot_config.dart';
import 'utils/grant_camera_permission.dart';
Future<void> main() async {
await grantCameraPermission();
await integrationDriver(
2024-05-11 12:59:28 +00:00
onScreenshot: (name, bytes, [_]) async {
final screenshotArgs = ScreenshotArgs.fromString(name);
final backgroundColor = ColorRgba8(
screenshotArgs.backgroundColor.r,
screenshotArgs.backgroundColor.g,
screenshotArgs.backgroundColor.b,
screenshotArgs.backgroundColor.a,
);
final platform =
screenshotArgs.platformFolder == 'ios' ? ScreenshotDevicePlatform.ios : ScreenshotDevicePlatform.ios;
final deviceName = screenshotArgs.deviceName;
final file = await File(screenshotArgs.toPath()).create(recursive: true);
// switch (platform) {
// case ScreenshotDevicePlatform.ios:
// final device = screenshotDevicesIos.firstWhere(
// (device) => device.name == deviceName,
// orElse: () => ScreenshotDevice(name: '', platform: platform),
// );
// Image screenshot = decodePng(Uint8List.fromList(bytes))!;
// screenshot = screenshot.addSystemOverlay(device, isDark: screenshotArgs.isDark);
// screenshot = screenshot.addDeviceFrame(device, backgroundColor);
// file.writeAsBytesSync(encodePng(screenshot));
// case ScreenshotDevicePlatform.android:
// file.writeAsBytesSync(bytes);
// }
file.writeAsBytesSync(bytes);
return true;
},
);
}
2024-05-11 12:59:28 +00:00
extension ScreenshotImage on Image {
Image addSystemOverlay(ScreenshotDevice device, {required bool isDark}) {
final path = isDark ? device.systemOverlayPathDark : device.systemOverlayPathLight;
final statusBar = copyResize(
decodePng(File(path).readAsBytesSync())!,
width: width,
);
return compositeImage(this, statusBar);
}
Image addDeviceFrame(ScreenshotDevice device, Color backgroundColor) {
final screenshotRounded = copyCrop(
this,
x: 0,
y: 0,
width: width,
height: height,
);
final frame = decodePng(File(device.deviceFramePath).readAsBytesSync())!;
final expandedScreenshot = copyExpandCanvas(
copyExpandCanvas(
screenshotRounded,
newWidth: screenshotRounded.width + device.screenshotFrameOffset.dx,
newHeight: screenshotRounded.height + device.screenshotFrameOffset.dy,
position: ExpandCanvasPosition.bottomRight,
backgroundColor: backgroundColor,
),
newWidth: frame.width,
newHeight: frame.height,
position: ExpandCanvasPosition.topLeft,
backgroundColor: backgroundColor,
);
return compositeImage(expandedScreenshot, frame);
}
2024-05-11 14:07:03 +00:00
Image applyLayout(ScreenshotLayout layout) {
final scaledScreenshot = copyResize(
this,
width: layout.size.width - (layout.contentPadding.left + layout.contentPadding.right),
);
return copyExpandCanvas(
copyExpandCanvas(
scaledScreenshot,
newWidth: scaledScreenshot.width + layout.contentPadding.right,
newHeight: scaledScreenshot.height + layout.contentPadding.bottom,
position: ExpandCanvasPosition.topLeft,
backgroundColor: getPixel(0, 0),
),
newWidth: layout.size.width,
newHeight: layout.size.height,
position: ExpandCanvasPosition.bottomRight,
backgroundColor: getPixel(0, 0),
);
}
2024-05-11 15:02:25 +00:00
Image addText(ScreenshotLayout layout, String title, String subtitle) {
final titleFont = BitmapFont.fromZip(File(layout.titleFontPath).readAsBytesSync());
final subtitleFont = BitmapFont.fromZip(File(layout.subtitleFontPath).readAsBytesSync());
final textImage = fill(
Image(
height: titleFont.lineHeight + 36 + subtitleFont.lineHeight * 2,
width: layout.size.width - (layout.contentPadding.left + layout.contentPadding.right),
),
color: getPixel(0, 0),
);
drawString(
textImage,
title,
font: titleFont,
y: 0,
);
int subtitleDy = titleFont.lineHeight + 36;
subtitle.split('\n').forEach((line) {
drawString(
textImage,
line,
font: subtitleFont,
y: subtitleDy,
);
subtitleDy += subtitleFont.lineHeight;
});
return compositeImage(
this,
textImage,
dstX: layout.contentPadding.left,
dstY: layout.contentPadding.top,
);
}
2024-05-11 12:59:28 +00:00
}