-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMockScreen.ts
60 lines (55 loc) · 1.73 KB
/
MockScreen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { EventEmitter } from 'events'
import sinon from 'sinon'
import { MockDisplay } from './MockDisplay'
export class MockScreen extends EventEmitter implements Electron.Screen {
private _displays: Electron.Display[] = []
// stub all Electron.Screen methods returning logical values
getCursorScreenPoint = sinon.spy(() => ({ x: 0, y: 0 }))
getPrimaryDisplay = sinon.spy(() => this._displays[0])
getAllDisplays = sinon.spy(() => this._displays)
getDisplayMatching = sinon.spy(() => this._displays[0])
getDisplayNearestPoint = sinon.spy(() => this._displays[0])
dipToScreenPoint = sinon.spy((point: Electron.Point) => {
return point
})
dipToScreenRect = sinon.spy(
(window: Electron.BrowserWindow | null, rect: Electron.Rectangle) => {
return rect
},
)
screenToDipPoint = sinon.spy((point: Electron.Point) => {
return point
})
screenToDipRect = sinon.spy(
(window: Electron.BrowserWindow | null, rect: Electron.Rectangle) => {
return rect
},
)
constructor(displays: Electron.Display[] = []) {
super()
this._displays = displays
if (!displays.length) {
this._displays.push(new MockDisplay())
}
}
addDisplay(display?: Electron.Display) {
if (!display) {
display = new MockDisplay()
}
this._displays.push(display)
this.emit('display-added', display)
}
removeDisplay(display: Electron.Display | number) {
let d: Electron.Display
if (typeof display === 'number') {
d = this._displays.find((dd) => dd.id === display)
} else {
d = display
}
if (!d) {
throw new Error(`Display ${display} not found`)
}
this._displays = this._displays.filter((dd) => dd.id !== d.id)
this.emit('display-removed', display)
}
}