Skip to content

Feature/329/find image parameter type #330

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

Merged
merged 3 commits into from
Dec 1, 2021
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
30 changes: 22 additions & 8 deletions lib/assert.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import {AssertClass} from "./assert.class";
import {Region} from "./region.class";
import {ScreenClass} from "./screen.class";
import providerRegistry from "./provider/provider-registry.class";
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<Image>({
id: needleId
});

// WHEN

Expand All @@ -26,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<Image>({
id: needleId
});

// WHEN

// THEN
await expect(SUT.isVisible(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 () => {
Expand All @@ -40,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<Image>({
id: needleId
});

// WHEN

// THEN
await expect(SUT
.isVisible(needle, searchRegion))
.rejects.toThrowError(`Element '${needle}' not found in region ${searchRegion.toString()}`
.rejects.toThrowError(`Element '${needle.id}' not found in region ${searchRegion.toString()}`
);
});

Expand All @@ -56,20 +66,24 @@ 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<Image>({
id: needleId
});

// WHEN

// THEN
await expect(SUT.notVisible(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 () => {
// GIVEN
ScreenClass.prototype.find = jest.fn(() => Promise.reject("foo"));
const screenMock = new ScreenClass(providerRegistry);
const SUT = new AssertClass(screenMock);
const needle = "foo";
const needle = mockPartial<Image>({
id: needleId
});

// WHEN

Expand Down
3 changes: 1 addition & 2 deletions lib/region.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`;
}
}
23 changes: 6 additions & 17 deletions lib/screen.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -93,24 +92,19 @@ 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: string | Image | Promise<Image>,
template: Image | Promise<Image>,
params?: LocationParameters,
): Promise<Region> {
const minMatch = (params && params.confidence) || this.config.confidence;
const screenSize = await this.providerRegistry.getScreen().screenSize();
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 template;

const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion);

Expand Down Expand Up @@ -150,24 +144,19 @@ 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: string | Image | Promise<Image>,
template: FirstArgumentType<typeof ScreenClass.prototype.find>,
params?: LocationParameters,
): Promise<Region[]> {
const minMatch = (params && params.confidence) || this.config.confidence;
const screenSize = await this.providerRegistry.getScreen().screenSize();
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 template;

const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion);

Expand Down