Skip to content

Refactor how SkiaGoldClient is used #144047

@matanlurey

Description

@matanlurey

This is some of the most confusing/scariest code in the Android scenario_app runner:

// Start a TCP socket in the host, and forward it to the device that runs the tests.
// This allows the test process to start a connection with the host, and write the bytes
// for the screenshots.
// On LUCI, the host uploads the screenshots to Skia Gold.
SkiaGoldClient? skiaGoldClient;
late  ServerSocket server;
final List<Future<void>> pendingComparisons = <Future<void>>[];
await step('Starting server...', () async {
  server = await ServerSocket.bind(InternetAddress.anyIPv4, _tcpPort);
  if (verbose) {
    stdout.writeln('listening on host ${server.address.address}:${server.port}');
  }
  server.listen((Socket client) {
    if (verbose) {
      stdout.writeln('client connected ${client.remoteAddress.address}:${client.remotePort}');
    }
    client.transform(const ScreenshotBlobTransformer()).listen((Screenshot screenshot) {
      final String fileName = screenshot.filename;
      final Uint8List fileContent = screenshot.fileContent;
      if (verbose) {
        log('host received ${fileContent.lengthInBytes} bytes for screenshot `$fileName`');
      }
      assert(skiaGoldClient != null, 'expected Skia Gold client');
      late File goldenFile;
      try {
        goldenFile = File(join(screenshotPath, fileName))..writeAsBytesSync(fileContent, flush: true);
      } on FileSystemException catch (err) {
        panic(<String>['failed to create screenshot $fileName: $err']);
      }
      if (verbose) {
        log('wrote ${goldenFile.absolute.path}');
      }
      if (isSkiaGoldClientAvailable) {
        final Future<void> comparison = skiaGoldClient!
          .addImg(fileName, goldenFile,
                  screenshotSize: screenshot.pixelCount)
          .catchError((dynamic err) {
            panic(<String>['skia gold comparison failed: $err']);
          });
        pendingComparisons.add(comparison);
      }
    },
    onError: (dynamic err) {
      panic(<String>['error while receiving bytes: $err']);
    },
    cancelOnError: true);
  });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: tech-debtTechnical debt, code quality, testing, etc.e: scenario-appThe `testing/scenario_app` fixture in the engineengineflutter/engine related. See also e: labels.platform-androidAndroid applications specificallyteam-engineOwned by Engine teamtriaged-engineTriaged by Engine team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions