|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// @dart = 2.6 |
| 6 | +import 'dart:html' as html; |
| 7 | +import 'package:ui/ui.dart' as ui; |
| 8 | +import 'package:ui/src/engine.dart'; |
| 9 | +import 'package:web_engine_tester/golden_tester.dart'; |
| 10 | +import 'package:test/test.dart'; |
| 11 | + |
| 12 | +/// Commit a recording canvas to a bitmap, and compare with the expected. |
| 13 | +Future<void> canvasScreenshot(RecordingCanvas rc, String fileName, |
| 14 | + {ui.Rect region = const ui.Rect.fromLTWH(0, 0, 600, 800), |
| 15 | + double maxDiffRatePercent = 0.0, bool write: false}) async { |
| 16 | + final EngineCanvas engineCanvas = BitmapCanvas(region); |
| 17 | + |
| 18 | + rc.endRecording(); |
| 19 | + rc.apply(engineCanvas, region); |
| 20 | + |
| 21 | + // Wrap in <flt-scene> so that our CSS selectors kick in. |
| 22 | + final html.Element sceneElement = html.Element.tag('flt-scene'); |
| 23 | + try { |
| 24 | + sceneElement.append(engineCanvas.rootElement); |
| 25 | + html.document.body.append(sceneElement); |
| 26 | + await matchGoldenFile('$fileName.png', |
| 27 | + region: region, maxDiffRatePercent: maxDiffRatePercent, write: write); |
| 28 | + } finally { |
| 29 | + // The page is reused across tests, so remove the element after taking the |
| 30 | + // Scuba screenshot. |
| 31 | + sceneElement.remove(); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// Configures the test to use bundled Roboto and Ahem fonts to avoid golden |
| 36 | +/// screenshot differences due to differences in the preinstalled system fonts. |
| 37 | +void setUpStableTestFonts() { |
| 38 | + setUp(() async { |
| 39 | + await ui.webOnlyInitializePlatform(); |
| 40 | + ui.webOnlyFontCollection.debugRegisterTestFonts(); |
| 41 | + await ui.webOnlyFontCollection.ensureFontsLoaded(); |
| 42 | + }); |
| 43 | +} |
0 commit comments