Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Prev Previous commit
Next Next commit
Move h5vcc monkey patching out of its separate function.
  • Loading branch information
b-luk committed Feb 4, 2022
commit 41416c1b49f7c3b405dabd4b6166b16d9b7bef8c
45 changes: 16 additions & 29 deletions lib/web_ui/test/canvaskit/h5vcc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,23 @@ void testMain() {
group('H5vcc patched CanvasKit', () {
int getH5vccSkSurfaceCalledCount = 0;

patchH5vccCanvasKit(onGetH5vccSkSurfaceCalled: () {
getH5vccSkSurfaceCalledCount++;
setUpAll(() async {
// Set `window.h5vcc` to PatchedH5vcc which uses a downloaded CanvasKit.
final CanvasKit downloadedCanvasKit = await downloadCanvasKit();
debugH5vccSetter = PatchedH5vcc(downloadedCanvasKit);

// Monkey-patch the getH5vccSkSurface function of
// `window.h5vcc.canvasKit`.
js.context['h5vcc']['canvasKit']['getH5vccSkSurface'] = () {
getH5vccSkSurfaceCalledCount++;

// Returns a fake [SkSurface] object with a minimal implementation.
return js.JsObject.jsify(<String, dynamic>{
'dispose': () {}
});
};
});

setUpCanvasKitTest();

setUp(() {
Expand Down Expand Up @@ -50,30 +64,3 @@ class PatchedH5vcc implements H5vcc {

PatchedH5vcc(this.canvasKit);
}

/// Test setup to initialize `window.h5vcc` with a patched H5vcc implementation.
///
/// The patched H5vcc implementation uses a downloaded CanvasKit implementation
/// monkey-patched with a fake getH5vccSkSurface function.
void patchH5vccCanvasKit({Function? onGetH5vccSkSurfaceCalled}) {
CanvasKit? existingCanvasKit;
SkiaFontCollection? existingSkiaFontCollection;

setUpAll(() async {
// Set `window.h5vcc` to PatchedH5vcc which uses a downloaded CanvasKit.
final CanvasKit downloadedCanvasKit = await downloadCanvasKit();
debugH5vccSetter = PatchedH5vcc(downloadedCanvasKit);

// Monkey-patch the getH5vccSkSurface function of `window.h5vcc.canvasKit`.
js.context['h5vcc']['canvasKit']['getH5vccSkSurface'] = () {
if (onGetH5vccSkSurfaceCalled != null) {
onGetH5vccSkSurfaceCalled();
}

// Returns a fake [SkSurface] object with a minimal implementation.
return js.JsObject.jsify(<String, dynamic>{
'dispose': () {}
});
};
});
}