From 273b63e1f88463986e6e14a90afe29935a50363c Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Wed, 1 Dec 2021 00:21:12 +0000 Subject: [PATCH 1/3] (#329) Make `Screen#find` only accept `Image` or `Promise` parameter type --- lib/assert.class.spec.ts | 11 ++++++----- lib/region.class.ts | 3 +-- lib/screen.class.ts | 19 ++++--------------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/lib/assert.class.spec.ts b/lib/assert.class.spec.ts index 1fb6798c..48aa8f26 100644 --- a/lib/assert.class.spec.ts +++ b/lib/assert.class.spec.ts @@ -2,6 +2,7 @@ import {AssertClass} from "./assert.class"; import {Region} from "./region.class"; import {ScreenClass} from "./screen.class"; import providerRegistry from "./provider/provider-registry.class"; +import {imageResource} from "../index"; jest.mock('jimp', () => { }); @@ -18,7 +19,7 @@ describe("Assert", () => { // WHEN // THEN - await expect(SUT.isVisible(needle)).resolves.not.toThrowError(); + await expect(SUT.isVisible(imageResource(needle))).resolves.not.toThrowError(); }); it("isVisible should throw if a match is found.", async () => { @@ -31,7 +32,7 @@ describe("Assert", () => { // WHEN // THEN - await expect(SUT.isVisible(needle)).rejects.toThrowError(`Element '${needle}' not found`); + await expect(SUT.isVisible(imageResource(needle))).rejects.toThrowError(`Element '${needle}' not found`); }); it("isVisible should throw if a match is found.", async () => { @@ -46,7 +47,7 @@ describe("Assert", () => { // THEN await expect(SUT - .isVisible(needle, searchRegion)) + .isVisible(imageResource(needle), searchRegion)) .rejects.toThrowError(`Element '${needle}' not found in region ${searchRegion.toString()}` ); }); @@ -61,7 +62,7 @@ describe("Assert", () => { // WHEN // THEN - await expect(SUT.notVisible(needle)).rejects.toThrowError(`'${needle}' is visible`); + await expect(SUT.notVisible(imageResource(needle))).rejects.toThrowError(`'${needle}' is visible`); }); it("isVisible should throw if a match is found.", async () => { @@ -74,6 +75,6 @@ describe("Assert", () => { // WHEN // THEN - await expect(SUT.notVisible(needle)).resolves.not.toThrowError(); + await expect(SUT.notVisible(imageResource(needle))).resolves.not.toThrowError(); }); }); diff --git a/lib/region.class.ts b/lib/region.class.ts index a3e22c71..1f428948 100644 --- a/lib/region.class.ts +++ b/lib/region.class.ts @@ -29,7 +29,6 @@ export class Region { } public toString() { - return `(${this.left}, ${this.top}, ${this.left + this.width}, ${this.top + - this.height})`; + return `(${this.left}, ${this.top}, ${this.width}, ${this.height})`; } } diff --git a/lib/screen.class.ts b/lib/screen.class.ts index cb45fab7..0d413b31 100644 --- a/lib/screen.class.ts +++ b/lib/screen.class.ts @@ -8,7 +8,6 @@ import {Region} from "./region.class"; import {timeout} from "./util/timeout.function"; import {Image} from "./image.class"; import {ProviderRegistry} from "./provider/provider-registry.class"; -import {loadImageResource} from "./imageResources.function"; import {FirstArgumentType} from "./typings"; import {Point} from "./point.class"; @@ -97,7 +96,7 @@ export class ScreenClass { * @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence */ public async find( - templateImage: string | Image | Promise, + templateImage: Image | Promise, params?: LocationParameters, ): Promise { const minMatch = (params && params.confidence) || this.config.confidence; @@ -105,12 +104,7 @@ export class ScreenClass { const searchRegion = (params && params.searchRegion) || screenSize; const searchMultipleScales = (params && params.searchMultipleScales) - let needle: Image; - if (typeof templateImage === "string") { - needle = await loadImageResource(this.providerRegistry, this.config.resourceDirectory, templateImage); - } else { - needle = await templateImage; - } + const needle = await templateImage; const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion); @@ -154,7 +148,7 @@ export class ScreenClass { * @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence */ public async findAll( - templateImage: string | Image | Promise, + templateImage: Image | Promise, params?: LocationParameters, ): Promise { const minMatch = (params && params.confidence) || this.config.confidence; @@ -162,12 +156,7 @@ export class ScreenClass { const searchRegion = (params && params.searchRegion) || screenSize; const searchMultipleScales = (params && params.searchMultipleScales) - let needle: Image; - if (typeof templateImage === "string") { - needle = await loadImageResource(this.providerRegistry, this.config.resourceDirectory, templateImage); - } else { - needle = await templateImage; - } + const needle = await templateImage; const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion); From 419cba1fa5284d5389769806a5de81b025a06e57 Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Wed, 1 Dec 2021 00:23:02 +0000 Subject: [PATCH 2/3] (#329) Update docstrings --- lib/screen.class.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/screen.class.ts b/lib/screen.class.ts index 0d413b31..2034f2f0 100644 --- a/lib/screen.class.ts +++ b/lib/screen.class.ts @@ -92,11 +92,11 @@ export class ScreenClass { /** * {@link find} will search for a single occurrence of a template image on a systems main screen - * @param templateImage Filename of the template image, relative to {@link ScreenClass.config.resourceDirectory}, or an {@link Image} instance + * @param template Template {@link Image} instance * @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence */ public async find( - templateImage: Image | Promise, + template: Image | Promise, params?: LocationParameters, ): Promise { const minMatch = (params && params.confidence) || this.config.confidence; @@ -104,7 +104,7 @@ export class ScreenClass { const searchRegion = (params && params.searchRegion) || screenSize; const searchMultipleScales = (params && params.searchMultipleScales) - const needle = await templateImage; + const needle = await template; const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion); @@ -144,11 +144,11 @@ export class ScreenClass { /** * {@link findAll} will search for every occurrences of a template image on a systems main screen - * @param templateImage Filename of the template image, relative to {@link ScreenClass.config.resourceDirectory}, or an {@link Image} instance + * @param template Template {@link Image} instance * @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence */ public async findAll( - templateImage: Image | Promise, + template: FirstArgumentType, params?: LocationParameters, ): Promise { const minMatch = (params && params.confidence) || this.config.confidence; @@ -156,7 +156,7 @@ export class ScreenClass { const searchRegion = (params && params.searchRegion) || screenSize; const searchMultipleScales = (params && params.searchMultipleScales) - const needle = await templateImage; + const needle = await template; const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion); From 25b1e2adbf33988c4c1b3ecfc09b6817f80d3d3e Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Wed, 1 Dec 2021 00:41:30 +0000 Subject: [PATCH 3/3] (#329) Adjusted tests --- lib/assert.class.spec.ts | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/assert.class.spec.ts b/lib/assert.class.spec.ts index 48aa8f26..8f3b1e0b 100644 --- a/lib/assert.class.spec.ts +++ b/lib/assert.class.spec.ts @@ -2,24 +2,29 @@ import {AssertClass} from "./assert.class"; import {Region} from "./region.class"; import {ScreenClass} from "./screen.class"; import providerRegistry from "./provider/provider-registry.class"; -import {imageResource} from "../index"; +import {Image} from "../index"; +import {mockPartial} from "sneer"; jest.mock('jimp', () => { }); jest.mock("./screen.class"); +const needleId = "needleId"; + describe("Assert", () => { it("isVisible should not throw if a match is found.", async () => { // GIVEN ScreenClass.prototype.find = jest.fn(() => Promise.resolve(new Region(0, 0, 100, 100))); const screenMock = new ScreenClass(providerRegistry); const SUT = new AssertClass(screenMock); - const needle = "foo"; + const needle = mockPartial({ + id: needleId + }); // WHEN // THEN - await expect(SUT.isVisible(imageResource(needle))).resolves.not.toThrowError(); + await expect(SUT.isVisible(needle)).resolves.not.toThrowError(); }); it("isVisible should throw if a match is found.", async () => { @@ -27,12 +32,14 @@ describe("Assert", () => { ScreenClass.prototype.find = jest.fn(() => Promise.reject("foo")); const screenMock = new ScreenClass(providerRegistry); const SUT = new AssertClass(screenMock); - const needle = "foo"; + const needle = mockPartial({ + id: needleId + }); // WHEN // THEN - await expect(SUT.isVisible(imageResource(needle))).rejects.toThrowError(`Element '${needle}' not found`); + await expect(SUT.isVisible(needle)).rejects.toThrowError(`Element '${needle.id}' not found`); }); it("isVisible should throw if a match is found.", async () => { @@ -41,14 +48,16 @@ describe("Assert", () => { const screenMock = new ScreenClass(providerRegistry); const SUT = new AssertClass(screenMock); const searchRegion = new Region(10, 10, 10, 10); - const needle = "foo"; + const needle = mockPartial({ + id: needleId + }); // WHEN // THEN await expect(SUT - .isVisible(imageResource(needle), searchRegion)) - .rejects.toThrowError(`Element '${needle}' not found in region ${searchRegion.toString()}` + .isVisible(needle, searchRegion)) + .rejects.toThrowError(`Element '${needle.id}' not found in region ${searchRegion.toString()}` ); }); @@ -57,12 +66,14 @@ describe("Assert", () => { ScreenClass.prototype.find = jest.fn(() => Promise.resolve(new Region(0, 0, 100, 100))); const screenMock = new ScreenClass(providerRegistry); const SUT = new AssertClass(screenMock); - const needle = "foo"; + const needle = mockPartial({ + id: needleId + }); // WHEN // THEN - await expect(SUT.notVisible(imageResource(needle))).rejects.toThrowError(`'${needle}' is visible`); + await expect(SUT.notVisible(needle)).rejects.toThrowError(`'${needle.id}' is visible`); }); it("isVisible should throw if a match is found.", async () => { @@ -70,11 +81,13 @@ describe("Assert", () => { ScreenClass.prototype.find = jest.fn(() => Promise.reject("foo")); const screenMock = new ScreenClass(providerRegistry); const SUT = new AssertClass(screenMock); - const needle = "foo"; + const needle = mockPartial({ + id: needleId + }); // WHEN // THEN - await expect(SUT.notVisible(imageResource(needle))).resolves.not.toThrowError(); + await expect(SUT.notVisible(needle)).resolves.not.toThrowError(); }); });