Skip to content

Commit 7c1faad

Browse files
committed
Updated interfaces
1 parent d27d31b commit 7c1faad

File tree

7 files changed

+191
-153
lines changed

7 files changed

+191
-153
lines changed

demo/mouse.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ const square = async () => {
1212
};
1313

1414
(async () => {
15-
mouse
16-
.leftClick()
17-
// await square();
15+
await mouse
16+
.leftClick();
17+
await square();
1818
try {
1919
screen.config.resourceDirectory = "./assets";
2020
const whale = await screen.findOnScreen("docker.png");
21-
mouse.move(await movement.straightTo(Location.centerOf(whale)));
21+
await mouse.move(await movement.straightTo(Location.centerOf(whale)));
2222

2323
const gitlens = await screen.findOnScreen("gitlens.png");
24-
mouse.move(await movement.straightTo(Location.centerOf(gitlens)));
25-
mouse.drag(await movement.right(600));
26-
mouse.move(await movement.straightTo(Location.centerOf(gitlens)));
27-
mouse.drag(await movement.straightTo(Location.centerOf(whale)));
24+
await mouse.move(await movement.straightTo(Location.centerOf(gitlens)));
25+
await mouse.drag(await movement.right(600));
26+
await mouse.move(await movement.straightTo(Location.centerOf(gitlens)));
27+
await mouse.drag(await movement.straightTo(Location.centerOf(whale)));
2828
} catch (error) {
2929
console.log(error);
3030
}

lib/clipboard.class.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { NativeAdapter } from "./adapter/native.adapter.class";
33
export class Clipboard {
44
constructor(private nativeAdapter: NativeAdapter) {}
55

6-
public copy(text: string): void {
7-
this.nativeAdapter.copy(text);
6+
public copy(text: string): Promise<void> {
7+
return new Promise<void>(resolve => {
8+
this.nativeAdapter.copy(text);
9+
resolve();
10+
});
811
}
912

1013
public paste(): Promise<string> {

lib/keyboard.class.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ describe("Keyboard", () => {
2020
expect(SUT.config.autoDelayMs).toEqual(500);
2121
});
2222

23-
it("should pass input strings down to the type call.", () => {
23+
it("should pass input strings down to the type call.", async () => {
2424
// GIVEN
2525
const adapterMock = new NativeAdapter();
2626
const SUT = new Keyboard(adapterMock);
2727
const payload = "Test input!";
2828

2929
// WHEN
30-
SUT.type(payload);
30+
await SUT.type(payload);
3131

3232
// THEN
3333
expect(adapterMock.type).toHaveBeenCalledTimes(payload.length);
@@ -36,14 +36,14 @@ describe("Keyboard", () => {
3636
}
3737
});
3838

39-
it("should pass multiple input strings down to the type call.", () => {
39+
it("should pass multiple input strings down to the type call.", async () => {
4040
// GIVEN
4141
const adapterMock = new NativeAdapter();
4242
const SUT = new Keyboard(adapterMock);
4343
const payload = ["Test input!", "Array test2"];
4444

4545
// WHEN
46-
SUT.type(...payload);
46+
await SUT.type(...payload);
4747

4848
// THEN
4949
expect(adapterMock.type).toHaveBeenCalledTimes(payload.join(" ").length);
@@ -52,59 +52,59 @@ describe("Keyboard", () => {
5252
}
5353
});
5454

55-
it("should pass input keys down to the click call.", () => {
55+
it("should pass input keys down to the click call.", async () => {
5656
// GIVEN
5757
const adapterMock = new NativeAdapter();
5858
const SUT = new Keyboard(adapterMock);
5959
const payload = [Key.A, Key.S, Key.D, Key.F];
6060

6161
// WHEN
62-
SUT.type(...payload);
62+
await SUT.type(...payload);
6363

6464
// THEN
6565
expect(adapterMock.click).toHaveBeenCalledTimes(1);
6666
expect(adapterMock.click).toHaveBeenCalledWith(...payload);
6767
});
6868

69-
it("should pass a list of input keys down to the click call.", () => {
69+
it("should pass a list of input keys down to the click call.", async () => {
7070
// GIVEN
7171
const adapterMock = new NativeAdapter();
7272
const SUT = new Keyboard(adapterMock);
7373
const payload = [Key.A, Key.S, Key.D, Key.F];
7474

7575
// WHEN
7676
for (const key of payload) {
77-
SUT.type(key);
77+
await SUT.type(key);
7878
}
7979

8080
// THEN
8181
expect(adapterMock.click).toHaveBeenCalledTimes(payload.length);
8282
});
8383

84-
it("should pass a list of input keys down to the pressKey call.", () => {
84+
it("should pass a list of input keys down to the pressKey call.", async () => {
8585
// GIVEN
8686
const adapterMock = new NativeAdapter();
8787
const SUT = new Keyboard(adapterMock);
8888
const payload = [Key.A, Key.S, Key.D, Key.F];
8989

9090
// WHEN
9191
for (const key of payload) {
92-
SUT.pressKey(key);
92+
await SUT.pressKey(key);
9393
}
9494

9595
// THEN
9696
expect(adapterMock.pressKey).toHaveBeenCalledTimes(payload.length);
9797
});
9898

99-
it("should pass a list of input keys down to the releaseKey call.", () => {
99+
it("should pass a list of input keys down to the releaseKey call.", async () => {
100100
// GIVEN
101101
const adapterMock = new NativeAdapter();
102102
const SUT = new Keyboard(adapterMock);
103103
const payload = [Key.A, Key.S, Key.D, Key.F];
104104

105105
// WHEN
106106
for (const key of payload) {
107-
SUT.releaseKey(key);
107+
await SUT.releaseKey(key);
108108
}
109109

110110
// THEN

lib/keyboard.class.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,46 @@ export class Keyboard {
1818
this.lastAction = Date.now();
1919
}
2020

21-
public type(...input: string[] | Key[]): Keyboard {
22-
if (Keyboard.inputIsString(input)) {
23-
for (const char of input.join(" ").split("")) {
24-
this.waitForNextTick();
25-
this.nativeAdapter.type(char);
26-
this.updateTick();
21+
public type(...input: string[] | Key[]): Promise<Keyboard> {
22+
return new Promise<Keyboard>(async resolve => {
23+
if (Keyboard.inputIsString(input)) {
24+
for (const char of input.join(" ").split("")) {
25+
await this.waitForNextTick();
26+
this.nativeAdapter.type(char);
27+
this.updateTick();
28+
}
29+
} else {
30+
this.nativeAdapter.click(...input as Key[]);
2731
}
28-
} else {
29-
this.nativeAdapter.click(...input as Key[]);
30-
}
31-
return this;
32+
resolve(this);
33+
});
3234
}
3335

34-
public pressKey(...keys: Key[]): Keyboard {
35-
this.nativeAdapter.pressKey(...keys);
36-
return this;
36+
public pressKey(...keys: Key[]): Promise<Keyboard> {
37+
return new Promise<Keyboard>(async resolve => {
38+
this.nativeAdapter.pressKey(...keys);
39+
resolve(this);
40+
});
3741
}
3842

39-
public releaseKey(...keys: Key[]): Keyboard {
40-
this.nativeAdapter.releaseKey(...keys);
41-
return this;
43+
public releaseKey(...keys: Key[]): Promise<Keyboard> {
44+
return new Promise<Keyboard>(async resolve => {
45+
this.nativeAdapter.releaseKey(...keys);
46+
resolve(this);
47+
});
4248
}
4349

4450
private updateTick() {
4551
this.lastAction = Date.now();
4652
}
4753

48-
private waitForNextTick() {
49-
let current = Date.now();
50-
while (current - this.lastAction < this.config.autoDelayMs) {
51-
current = Date.now();
52-
}
54+
private async waitForNextTick(): Promise<void> {
55+
return new Promise<void>(resolve => {
56+
let current = Date.now();
57+
while (current - this.lastAction < this.config.autoDelayMs) {
58+
current = Date.now();
59+
}
60+
resolve();
61+
});
5362
}
5463
}

lib/mouse.class.spec.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,110 +24,110 @@ describe("Mouse class", () => {
2424
expect(SUT.config.autoDelayMs).toEqual(100);
2525
});
2626

27-
it("should forward scrollLeft to the native adapter class", () => {
27+
it("should forward scrollLeft to the native adapter class", async () => {
2828
// GIVEN
2929
const nativeAdapterMock = new NativeAdapter();
3030
const SUT = new Mouse(nativeAdapterMock);
3131
const scrollAmount = 5;
3232

3333
// WHEN
34-
const result = SUT.scrollLeft(scrollAmount);
34+
const result = await SUT.scrollLeft(scrollAmount);
3535

3636
// THEN
3737
expect(nativeAdapterMock.scrollLeft).toBeCalledWith(scrollAmount);
3838
expect(result).toBe(SUT);
3939
});
4040

41-
it("should forward scrollRight to the native adapter class", () => {
41+
it("should forward scrollRight to the native adapter class", async () => {
4242
// GIVEN
4343
const nativeAdapterMock = new NativeAdapter();
4444
const SUT = new Mouse(nativeAdapterMock);
4545
const scrollAmount = 5;
4646

4747
// WHEN
48-
const result = SUT.scrollRight(scrollAmount);
48+
const result = await SUT.scrollRight(scrollAmount);
4949

5050
// THEN
5151
expect(nativeAdapterMock.scrollRight).toBeCalledWith(scrollAmount);
5252
expect(result).toBe(SUT);
5353
});
5454

55-
it("should forward scrollDown to the native adapter class", () => {
55+
it("should forward scrollDown to the native adapter class", async () => {
5656
// GIVEN
5757
const nativeAdapterMock = new NativeAdapter();
5858
const SUT = new Mouse(nativeAdapterMock);
5959
const scrollAmount = 5;
6060

6161
// WHEN
62-
const result = SUT.scrollDown(scrollAmount);
62+
const result = await SUT.scrollDown(scrollAmount);
6363

6464
// THEN
6565
expect(nativeAdapterMock.scrollDown).toBeCalledWith(scrollAmount);
6666
expect(result).toBe(SUT);
6767
});
6868

69-
it("should forward scrollUp to the native adapter class", () => {
69+
it("should forward scrollUp to the native adapter class", async () => {
7070
// GIVEN
7171
const nativeAdapterMock = new NativeAdapter();
7272
const SUT = new Mouse(nativeAdapterMock);
7373
const scrollAmount = 5;
7474

7575
// WHEN
76-
const result = SUT.scrollUp(scrollAmount);
76+
const result = await SUT.scrollUp(scrollAmount);
7777

7878
// THEN
7979
expect(nativeAdapterMock.scrollUp).toBeCalledWith(scrollAmount);
8080
expect(result).toBe(SUT);
8181
});
8282

83-
it("should forward leftClick to the native adapter class", () => {
83+
it("should forward leftClick to the native adapter class", async () => {
8484
// GIVEN
8585
const nativeAdapterMock = new NativeAdapter();
8686
const SUT = new Mouse(nativeAdapterMock);
8787

8888
// WHEN
89-
const result = SUT.leftClick();
89+
const result = await SUT.leftClick();
9090

9191
// THEN
9292
expect(nativeAdapterMock.leftClick).toBeCalled();
9393
expect(result).toBe(SUT);
9494
});
9595

96-
it("should forward rightClick to the native adapter class", () => {
96+
it("should forward rightClick to the native adapter class", async () => {
9797
// GIVEN
9898
const nativeAdapterMock = new NativeAdapter();
9999
const SUT = new Mouse(nativeAdapterMock);
100100

101101
// WHEN
102-
const result = SUT.rightClick();
102+
const result = await SUT.rightClick();
103103

104104
// THEN
105105
expect(nativeAdapterMock.rightClick).toBeCalled();
106106
expect(result).toBe(SUT);
107107
});
108108

109-
it("update mouse position along path on move", () => {
109+
it("update mouse position along path on move", async () => {
110110
// GIVEN
111111
const nativeAdapterMock = new NativeAdapter();
112112
const SUT = new Mouse(nativeAdapterMock);
113113
const path = linehelper.straightLine(new Point(0, 0), new Point(10, 10));
114114

115115
// WHEN
116-
const result = SUT.move(path);
116+
const result = await SUT.move(path);
117117

118118
// THEN
119119
expect(nativeAdapterMock.setMousePosition).toBeCalledTimes(path.length);
120120
expect(result).toBe(SUT);
121121
});
122122

123-
it("should press and hold left mouse button, move and release left mouse button on drag", () => {
123+
it("should press and hold left mouse button, move and release left mouse button on drag", async () => {
124124
// GIVEN
125125
const nativeAdapterMock = new NativeAdapter();
126126
const SUT = new Mouse(nativeAdapterMock);
127127
const path = linehelper.straightLine(new Point(0, 0), new Point(10, 10));
128128

129129
// WHEN
130-
const result = SUT.drag(path);
130+
const result = await SUT.drag(path);
131131

132132
// THEN
133133
expect(nativeAdapterMock.pressButton).toBeCalledWith(Button.LEFT);

0 commit comments

Comments
 (0)