Skip to content

Commit a49871b

Browse files
authored
Merge pull request #162 from nut-tree/fix/160/fix-find-offset
Fix/160/fix find offset
2 parents 3734567 + 12da72d commit a49871b

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

lib/screen.class.spec.ts

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -137,52 +137,54 @@ describe("Screen.", () => {
137137
});
138138

139139
it("should override default search region with parameter.", async () => {
140+
// GIVEN
140141
const customSearchRegion = new Region(10, 10, 90, 90);
141142
const matchResult = new MatchResult(0.99, searchRegion);
142-
143143
VisionAdapter.prototype.findOnScreenRegion = jest.fn(() => {
144144
return Promise.resolve(matchResult);
145145
});
146-
147146
const visionAdapterMock = new VisionAdapter();
148-
149147
const SUT = new Screen(visionAdapterMock);
150-
151148
const imagePath = "test/path/to/image.png";
152149
const parameters = new LocationParameters(customSearchRegion);
153-
await expect(SUT.find(imagePath, parameters)).resolves.toEqual(matchResult.location);
154-
const matchRequest = new MatchRequest(
155-
expect.any(Image),
156-
join(cwd(), imagePath),
157-
customSearchRegion,
158-
SUT.config.confidence,
159-
true);
160-
expect(visionAdapterMock.findOnScreenRegion).toHaveBeenCalledWith(matchRequest);
150+
const expectedMatchRequest = new MatchRequest(
151+
expect.any(Image),
152+
join(cwd(), imagePath),
153+
customSearchRegion,
154+
SUT.config.confidence,
155+
true);
156+
157+
// WHEN
158+
await SUT.find(imagePath, parameters);
159+
160+
// THEN
161+
expect(visionAdapterMock.findOnScreenRegion).toHaveBeenCalledWith(expectedMatchRequest);
161162
});
162163

163164
it("should override both confidence and search region with parameter.", async () => {
165+
// GIVEN
164166
const minMatch = 0.8;
165167
const customSearchRegion = new Region(10, 10, 90, 90);
166168
const matchResult = new MatchResult(minMatch, searchRegion);
167-
168169
VisionAdapter.prototype.findOnScreenRegion = jest.fn(() => {
169170
return Promise.resolve(matchResult);
170171
});
171-
172172
const visionAdapterMock = new VisionAdapter();
173-
174173
const SUT = new Screen(visionAdapterMock);
175-
176174
const imagePath = "test/path/to/image.png";
177175
const parameters = new LocationParameters(customSearchRegion, minMatch);
178-
await expect(SUT.find(imagePath, parameters)).resolves.toEqual(matchResult.location);
179-
const matchRequest = new MatchRequest(
180-
expect.any(Image),
181-
join(cwd(), imagePath),
182-
customSearchRegion,
183-
minMatch,
184-
true);
185-
expect(visionAdapterMock.findOnScreenRegion).toHaveBeenCalledWith(matchRequest);
176+
const expectedMatchRequest = new MatchRequest(
177+
expect.any(Image),
178+
join(cwd(), imagePath),
179+
customSearchRegion,
180+
minMatch,
181+
true);
182+
183+
// WHEN
184+
await SUT.find(imagePath, parameters);
185+
186+
// THEN
187+
expect(visionAdapterMock.findOnScreenRegion).toHaveBeenCalledWith(expectedMatchRequest);
186188
});
187189

188190
it("should return region to highlight for chaining", async () => {
@@ -214,4 +216,33 @@ describe("Screen.", () => {
214216
expect(result).toEqual(highlightRegion);
215217
});
216218

219+
it("should add search region offset to result image location", async () => {
220+
221+
// GIVEN
222+
const limitedSearchRegion = new Region(100, 200, 300, 400);
223+
const resultRegion = new Region(50, 100, 150, 200);
224+
const matchResult = new MatchResult(0.99, resultRegion);
225+
226+
const expectedMatchRegion = new Region(
227+
limitedSearchRegion.left + resultRegion.left,
228+
limitedSearchRegion.top + resultRegion.top,
229+
resultRegion.width,
230+
resultRegion.height);
231+
232+
VisionAdapter.prototype.findOnScreenRegion = jest.fn(() => {
233+
return Promise.resolve(matchResult);
234+
});
235+
const SUT = new Screen(new VisionAdapter());
236+
237+
// WHEN
238+
const matchRegion = await SUT.find(
239+
"test/path/to/image.png",
240+
{
241+
searchRegion: limitedSearchRegion
242+
});
243+
244+
// THEN
245+
expect(matchRegion).toEqual(expectedMatchRegion);
246+
})
247+
217248
});

lib/screen.class.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,16 @@ export class Screen {
105105
for (const hook of possibleHooks) {
106106
await hook(matchResult);
107107
}
108+
const resultRegion = new Region(
109+
searchRegion.left + matchResult.location.left,
110+
searchRegion.top + matchResult.location.top,
111+
matchResult.location.width,
112+
matchResult.location.height
113+
)
108114
if (this.config.autoHighlight) {
109-
resolve(this.highlight(matchResult.location));
115+
resolve(this.highlight(resultRegion));
110116
} else {
111-
resolve(matchResult.location);
117+
resolve(resultRegion);
112118
}
113119
} else {
114120
reject(

0 commit comments

Comments
 (0)