Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: provide diffBuffer in ImageDiffError (backport to v7) #788

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"glob-extra": "^5.0.2",
"inherit": "^2.2.2",
"lodash": "^4.17.21",
"looks-same": "^8.1.0",
"looks-same": "^8.2.1",
"micromatch": "^4.0.5",
"mocha": "^10.2.0",
"plugins-loader": "^1.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.handleNoRefImage = (currImg, refImg, state) => {
};

exports.handleImageDiff = (currImg, refImg, state, opts) => {
const { tolerance, antialiasingTolerance, canHaveCaret, diffAreas, config } = opts;
const { tolerance, antialiasingTolerance, canHaveCaret, diffAreas, config, diffBuffer } = opts;
const {
buildDiffOpts,
system: { diffColor },
Expand All @@ -25,5 +25,5 @@ exports.handleImageDiff = (currImg, refImg, state, opts) => {
...buildDiffOpts,
};

return Promise.reject(ImageDiffError.create(state, currImg, refImg, diffOpts, diffAreas));
return Promise.reject(ImageDiffError.create(state, currImg, refImg, diffOpts, diffAreas, diffBuffer));
};
18 changes: 13 additions & 5 deletions src/browser/commands/assert-view/errors/image-diff-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@ module.exports = class ImageDiffError extends BaseStateError {

static fromObject(data) {
const { diffBounds, diffClusters } = data;
return new ImageDiffError(data.stateName, data.currImg, data.refImg, data.diffOpts, {
diffBounds,
diffClusters,
});
return new ImageDiffError(
data.stateName,
data.currImg,
data.refImg,
data.diffOpts,
{
diffBounds,
diffClusters,
},
data.diffBuffer,
);
}

constructor(stateName, currImg, refImg, diffOpts, { diffBounds, diffClusters } = {}) {
constructor(stateName, currImg, refImg, diffOpts, { diffBounds, diffClusters } = {}, diffBuffer) {
super(stateName, currImg, refImg);

this.message = `images are different for "${stateName}" state`;
this.diffOpts = diffOpts;
this.diffBounds = diffBounds;
this.diffClusters = diffClusters;
this.diffBuffer = diffBuffer;
}

saveDiffTo(diffPath) {
Expand Down
12 changes: 11 additions & 1 deletion src/browser/commands/assert-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,24 @@ module.exports = browser => {
equal,
diffBounds,
diffClusters,
diffImage,
metaInfo = {},
} = await Image.compare(refBuffer, currBuffer, imageCompareOpts);
Object.assign(refImg, metaInfo.refImg);

if (!equal) {
const diffBuffer = await diffImage.createBuffer("png");
const diffAreas = { diffBounds, diffClusters };
const { tolerance, antialiasingTolerance } = opts;
const imageDiffOpts = { tolerance, antialiasingTolerance, canHaveCaret, diffAreas, config, emitter };
const imageDiffOpts = {
tolerance,
antialiasingTolerance,
canHaveCaret,
diffAreas,
config,
emitter,
diffBuffer,
};

await fs.outputFile(currImg.path, currBuffer);

Expand Down
1 change: 1 addition & 0 deletions src/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ module.exports = class Image {
ignoreCaret: opts.canHaveCaret,
pixelRatio: opts.pixelRatio,
...opts.compareOpts,
createDiffImage: true,
};
["tolerance", "antialiasingTolerance"].forEach(option => {
if (option in opts) {
Expand Down
23 changes: 16 additions & 7 deletions test/src/browser/commands/assert-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("assertView command", () => {

beforeEach(() => {
sandbox.stub(Image, "create").returns(Object.create(Image.prototype));
sandbox.stub(Image, "compare").resolves(true);
sandbox.stub(Image, "compare").resolves({ diffImage: { createBuffer: sandbox.stub() } });
sandbox.stub(Image.prototype, "getSize");

sandbox.stub(fs, "readFileSync");
Expand Down Expand Up @@ -494,7 +494,7 @@ describe("assertView command", () => {

describe("image compare", () => {
it("should add opts from runtime config to temp", async () => {
Image.compare.resolves({ equal: true });
Image.compare.resolves({ equal: true, diffImage: { createBuffer: sandbox.stub() } });
RuntimeConfig.getInstance.returns({ tempOpts: { some: "opts" } });
const browser = await initBrowser_();

Expand All @@ -506,7 +506,7 @@ describe("assertView command", () => {
it("should compare a current image with a reference", async () => {
const config = mkConfig_({ getScreenshotPath: () => "/ref/path" });
const image = stubImage_();
Image.compare.resolves({ equal: true });
Image.compare.resolves({ equal: true, diffImage: { createBuffer: sandbox.stub() } });
temp.path.returns("/curr/path");
fs.readFile.withArgs("/ref/path").resolves("refPngBuffer");
ScreenShooter.prototype.capture.resolves(image);
Expand All @@ -525,7 +525,9 @@ describe("assertView command", () => {
});
fs.readFile.withArgs("/ref/path").resolves("refPngBuffer");
image.toPngBuffer.resolves("currPngBuffer");
Image.compare.withArgs("refPngBuffer", "currPngBuffer").resolves({ equal: true });
Image.compare
.withArgs("refPngBuffer", "currPngBuffer")
.resolves({ equal: true, diffImage: { createBuffer: sandbox.stub() } });
ScreenShooter.prototype.capture.resolves(image);

await fn(browser);
Expand Down Expand Up @@ -589,7 +591,7 @@ describe("assertView command", () => {

describe("if images are not equal", () => {
beforeEach(() => {
Image.compare.resolves({ equal: false });
Image.compare.resolves({ equal: false, diffImage: { createBuffer: sandbox.stub() } });
});

describe("assert refs", () => {
Expand All @@ -613,6 +615,7 @@ describe("assertView command", () => {
Image.compare.resolves({
equal: false,
metaInfo: { refImg: { size: { width: 300, height: 400 } } },
diffImage: { createBuffer: sandbox.stub() },
});

await fn(browser, "state");
Expand Down Expand Up @@ -664,7 +667,10 @@ describe("assertView command", () => {
});

it("should pass diff bounds to error", async () => {
Image.compare.resolves({ diffBounds: { left: 0, top: 0, right: 10, bottom: 10 } });
Image.compare.resolves({
diffBounds: { left: 0, top: 0, right: 10, bottom: 10 },
diffImage: { createBuffer: sandbox.stub() },
});
const browser = await initBrowser_();

await fn(browser);
Expand All @@ -675,7 +681,10 @@ describe("assertView command", () => {
});

it("should pass diff clusters to error", async () => {
Image.compare.resolves({ diffClusters: [{ left: 0, top: 0, right: 10, bottom: 10 }] });
Image.compare.resolves({
diffClusters: [{ left: 0, top: 0, right: 10, bottom: 10 }],
diffImage: { createBuffer: sandbox.stub() },
});
const browser = await initBrowser_();

await fn(browser);
Expand Down
1 change: 1 addition & 0 deletions test/src/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ describe("Image", () => {
tolerance: 250,
antialiasingTolerance: 100500,
stopOnFirstFail: true,
createDiffImage: true,
});
});

Expand Down
Loading