testImageReader method

Future<Uint8List?> testImageReader({
  1. required TestDeviceInfo deviceInfo,
  2. required String imageId,
  3. String? suiteName,
  4. required String testName,
  5. int? testVersion,
})

Reader to read a golden image from GitHub.

Implementation

Future<Uint8List?> testImageReader({
  required TestDeviceInfo deviceInfo,
  required String imageId,
  String? suiteName,
  required String testName,
  int? testVersion,
}) async {
  await _loadData();
  final goldenId = GoldenTestImages.createId(
    deviceInfo: deviceInfo,
    suiteName: suiteName,
    testName: testName,
  );
  final actualPath = goldenImagesPath.startsWith('/')
      ? goldenImagesPath.substring(1)
      : goldenImagesPath;
  GoldenTestImages? golden;
  if (_currentGoldenTestImages?.id == goldenId) {
    golden = _currentGoldenTestImages;
  } else {
    final name = '$actualPath/$goldenId.json';
    final entry = _tree.entries!.where((e) => e.path == name);

    if (entry.isNotEmpty) {
      final response = await _github.repositories.getContents(
        slug,
        _encodeGitPath(actualPath),
      );

      if (response.file?.content != null) {
        final data = utf8.decode(base64.decode(response.file!.content!));
        final goldenJson = json.decode(data);
        golden = GoldenTestImages.fromDynamic(goldenJson);
        _currentGoldenTestImages = golden;
      }
    }
  }

  Uint8List? image;
  if (golden != null) {
    final hash = golden.goldenHashes![imageId];
    final path = '$actualPath/$goldenId/$hash.png';
    final entry = _tree.entries!.where((e) => e.path == path);
    if (entry.isNotEmpty) {
      final response = await _github.repositories.getContents(
        slug,
        _encodeGitPath(path),
      );

      if (response.file?.content != null) {
        image = base64.decode(response.file!.content!);
      }
    }
  }

  return image;
}