-
Notifications
You must be signed in to change notification settings - Fork 15k
/
api-web-contents-view-spec.ts
164 lines (147 loc) · 7.26 KB
/
api-web-contents-view-spec.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { closeAllWindows } from './lib/window-helpers';
import { expect } from 'chai';
import { BaseWindow, View, WebContentsView } from 'electron/main';
import { once } from 'node:events';
describe('WebContentsView', () => {
afterEach(closeAllWindows);
it('can be instantiated with no arguments', () => {
// eslint-disable-next-line no-new
new WebContentsView();
});
it('can be instantiated with no webPreferences', () => {
// eslint-disable-next-line no-new
new WebContentsView({});
});
it('can be used as content view', () => {
const w = new BaseWindow({ show: false });
w.setContentView(new WebContentsView());
});
it('can be removed after a close', async () => {
const w = new BaseWindow({ show: false });
const v = new View();
const wcv = new WebContentsView();
w.setContentView(v);
v.addChildView(wcv);
await wcv.webContents.loadURL('about:blank');
const destroyed = once(wcv.webContents, 'destroyed');
wcv.webContents.executeJavaScript('window.close()');
await destroyed;
expect(wcv.webContents.isDestroyed()).to.be.true();
v.removeChildView(wcv);
});
function triggerGCByAllocation () {
const arr = [];
for (let i = 0; i < 1000000; i++) {
arr.push([]);
}
return arr;
}
it('doesn\'t crash when GCed during allocation', (done) => {
// eslint-disable-next-line no-new
new WebContentsView();
setTimeout(() => {
// NB. the crash we're testing for is the lack of a current `v8::Context`
// when emitting an event in WebContents's destructor. V8 is inconsistent
// about whether or not there's a current context during garbage
// collection, and it seems that `v8Util.requestGarbageCollectionForTesting`
// causes a GC in which there _is_ a current context, so the crash isn't
// triggered. Thus, we force a GC by other means: namely, by allocating a
// bunch of stuff.
triggerGCByAllocation();
done();
});
});
it('can be fullscreened', async () => {
const w = new BaseWindow();
const v = new WebContentsView();
w.setContentView(v);
await v.webContents.loadURL('data:text/html,<div id="div">This is a simple div.</div>');
const enterFullScreen = once(w, 'enter-full-screen');
await v.webContents.executeJavaScript('document.getElementById("div").requestFullscreen()', true);
await enterFullScreen;
expect(w.isFullScreen()).to.be.true('isFullScreen');
});
describe('visibilityState', () => {
it('is initially hidden', async () => {
const v = new WebContentsView();
await v.webContents.loadURL('data:text/html,<script>initialVisibility = document.visibilityState</script>');
expect(await v.webContents.executeJavaScript('initialVisibility')).to.equal('hidden');
});
it('becomes visibile when attached', async () => {
const v = new WebContentsView();
await v.webContents.loadURL('about:blank');
expect(await v.webContents.executeJavaScript('document.visibilityState')).to.equal('hidden');
const p = v.webContents.executeJavaScript('new Promise(resolve => document.addEventListener("visibilitychange", resolve))');
// Ensure that the above listener has been registered before we add the
// view to the window, or else the visibilitychange event might be
// dispatched before the listener is registered.
// executeJavaScript calls are sequential so if this one's finished then
// the previous one must also have been finished :)
await v.webContents.executeJavaScript('undefined');
const w = new BaseWindow();
w.setContentView(v);
await p;
expect(await v.webContents.executeJavaScript('document.visibilityState')).to.equal('visible');
});
it('is initially visible if load happens after attach', async () => {
const w = new BaseWindow();
const v = new WebContentsView();
w.contentView = v;
await v.webContents.loadURL('data:text/html,<script>initialVisibility = document.visibilityState</script>');
expect(await v.webContents.executeJavaScript('initialVisibility')).to.equal('visible');
});
it('becomes hidden when parent window is hidden', async () => {
const w = new BaseWindow();
const v = new WebContentsView();
w.setContentView(v);
await v.webContents.loadURL('about:blank');
expect(await v.webContents.executeJavaScript('document.visibilityState')).to.equal('visible');
const p = v.webContents.executeJavaScript('new Promise(resolve => document.addEventListener("visibilitychange", resolve))');
// We have to wait until the listener above is fully registered before hiding the window.
// On Windows, the executeJavaScript and the visibilitychange can happen out of order
// without this.
await v.webContents.executeJavaScript('0');
w.hide();
await p;
expect(await v.webContents.executeJavaScript('document.visibilityState')).to.equal('hidden');
});
it('becomes visible when parent window is shown', async () => {
const w = new BaseWindow({ show: false });
const v = new WebContentsView();
w.setContentView(v);
await v.webContents.loadURL('about:blank');
expect(await v.webContents.executeJavaScript('document.visibilityState')).to.equal('hidden');
const p = v.webContents.executeJavaScript('new Promise(resolve => document.addEventListener("visibilitychange", resolve))');
// We have to wait until the listener above is fully registered before hiding the window.
// On Windows, the executeJavaScript and the visibilitychange can happen out of order
// without this.
await v.webContents.executeJavaScript('0');
w.show();
await p;
expect(await v.webContents.executeJavaScript('document.visibilityState')).to.equal('visible');
});
it('does not change when view is moved between two visible windows', async () => {
const w = new BaseWindow();
const v = new WebContentsView();
w.setContentView(v);
await v.webContents.loadURL('about:blank');
expect(await v.webContents.executeJavaScript('document.visibilityState')).to.equal('visible');
const p = v.webContents.executeJavaScript('new Promise(resolve => document.addEventListener("visibilitychange", () => resolve(document.visibilityState)))');
// Ensure the listener has been registered.
await v.webContents.executeJavaScript('undefined');
const w2 = new BaseWindow();
w2.setContentView(v);
// Wait for the visibility state to settle as "visible".
// On macOS one visibilitychange event is fired but visibilityState
// remains "visible". On Win/Linux, two visibilitychange events are
// fired, a "hidden" and a "visible" one. Reconcile these two models
// by waiting until at least one event has been fired, and then waiting
// until the visibility state settles as "visible".
let visibilityState = await p;
for (let attempts = 0; visibilityState !== 'visible' && attempts < 10; attempts++) {
visibilityState = await v.webContents.executeJavaScript('new Promise(resolve => document.visibilityState === "visible" ? resolve("visible") : document.addEventListener("visibilitychange", () => resolve(document.visibilityState)))');
}
expect(visibilityState).to.equal('visible');
});
});
});