chromium-spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. const { expect } = require('chai');
  2. const fs = require('fs');
  3. const http = require('http');
  4. const path = require('path');
  5. const url = require('url');
  6. const ChildProcess = require('child_process');
  7. const { ipcRenderer } = require('electron');
  8. const { emittedOnce, waitForEvent } = require('./events-helpers');
  9. const { ifit, ifdescribe, delay } = require('./spec-helpers');
  10. const features = process._linkedBinding('electron_common_features');
  11. /* Most of the APIs here don't use standard callbacks */
  12. /* eslint-disable standard/no-callback-literal */
  13. describe('chromium feature', () => {
  14. const fixtures = path.resolve(__dirname, 'fixtures');
  15. describe('window.open', () => {
  16. it('inherit options of parent window', async () => {
  17. const message = waitForEvent(window, 'message');
  18. const b = window.open(`file://${fixtures}/pages/window-open-size.html`, '', 'show=no');
  19. const event = await message;
  20. b.close();
  21. const width = outerWidth;
  22. const height = outerHeight;
  23. expect(event.data).to.equal(`size: ${width} ${height}`);
  24. });
  25. // FIXME(zcbenz): This test is making the spec runner hang on exit on Windows.
  26. ifit(process.platform !== 'win32')('disables node integration when it is disabled on the parent window', async () => {
  27. const windowUrl = require('url').format({
  28. pathname: `${fixtures}/pages/window-opener-no-node-integration.html`,
  29. protocol: 'file',
  30. query: {
  31. p: `${fixtures}/pages/window-opener-node.html`
  32. },
  33. slashes: true
  34. });
  35. const message = waitForEvent(window, 'message');
  36. const b = window.open(windowUrl, '', 'nodeIntegration=no,contextIsolation=no,show=no');
  37. const event = await message;
  38. b.close();
  39. expect(event.data.isProcessGlobalUndefined).to.be.true();
  40. });
  41. it('disables the <webview> tag when it is disabled on the parent window', async () => {
  42. const windowUrl = require('url').format({
  43. pathname: `${fixtures}/pages/window-opener-no-webview-tag.html`,
  44. protocol: 'file',
  45. query: {
  46. p: `${fixtures}/pages/window-opener-webview.html`
  47. },
  48. slashes: true
  49. });
  50. const message = waitForEvent(window, 'message');
  51. const b = window.open(windowUrl, '', 'webviewTag=no,contextIsolation=no,nodeIntegration=yes,show=no');
  52. const event = await message;
  53. b.close();
  54. expect(event.data.isWebViewGlobalUndefined).to.be.true();
  55. });
  56. it('does not override child options', async () => {
  57. const size = {
  58. width: 350,
  59. height: 450
  60. };
  61. const message = waitForEvent(window, 'message');
  62. const b = window.open(`file://${fixtures}/pages/window-open-size.html`, '', 'show=no,width=' + size.width + ',height=' + size.height);
  63. const event = await message;
  64. b.close();
  65. expect(event.data).to.equal(`size: ${size.width} ${size.height}`);
  66. });
  67. it('throws an exception when the arguments cannot be converted to strings', () => {
  68. expect(() => {
  69. window.open('', { toString: null });
  70. }).to.throw('Cannot convert object to primitive value');
  71. expect(() => {
  72. window.open('', '', { toString: 3 });
  73. }).to.throw('Cannot convert object to primitive value');
  74. });
  75. it('does not throw an exception when the features include webPreferences', () => {
  76. let b = null;
  77. expect(() => {
  78. b = window.open('', '', 'webPreferences=');
  79. }).to.not.throw();
  80. b.close();
  81. });
  82. });
  83. describe('window.opener', () => {
  84. it('is not null for window opened by window.open', async () => {
  85. const message = waitForEvent(window, 'message');
  86. const b = window.open(`file://${fixtures}/pages/window-opener.html`, '', 'show=no');
  87. const event = await message;
  88. b.close();
  89. expect(event.data).to.equal('object');
  90. });
  91. });
  92. describe('window.opener.postMessage', () => {
  93. it('sets source and origin correctly', async () => {
  94. const message = waitForEvent(window, 'message');
  95. const b = window.open(`file://${fixtures}/pages/window-opener-postMessage.html`, '', 'show=no');
  96. const event = await message;
  97. try {
  98. expect(event.source).to.deep.equal(b);
  99. expect(event.origin).to.equal('file://');
  100. } finally {
  101. b.close();
  102. }
  103. });
  104. it('supports windows opened from a <webview>', async () => {
  105. const webview = new WebView();
  106. const consoleMessage = waitForEvent(webview, 'console-message');
  107. webview.allowpopups = true;
  108. webview.setAttribute('webpreferences', 'contextIsolation=no');
  109. webview.src = url.format({
  110. pathname: `${fixtures}/pages/webview-opener-postMessage.html`,
  111. protocol: 'file',
  112. query: {
  113. p: `${fixtures}/pages/window-opener-postMessage.html`
  114. },
  115. slashes: true
  116. });
  117. document.body.appendChild(webview);
  118. const event = await consoleMessage;
  119. webview.remove();
  120. expect(event.message).to.equal('message');
  121. });
  122. describe('targetOrigin argument', () => {
  123. let serverURL;
  124. let server;
  125. beforeEach((done) => {
  126. server = http.createServer((req, res) => {
  127. res.writeHead(200);
  128. const filePath = path.join(fixtures, 'pages', 'window-opener-targetOrigin.html');
  129. res.end(fs.readFileSync(filePath, 'utf8'));
  130. });
  131. server.listen(0, '127.0.0.1', () => {
  132. serverURL = `http://127.0.0.1:${server.address().port}`;
  133. done();
  134. });
  135. });
  136. afterEach(() => {
  137. server.close();
  138. });
  139. it('delivers messages that match the origin', async () => {
  140. const message = waitForEvent(window, 'message');
  141. const b = window.open(serverURL, '', 'show=no,contextIsolation=no,nodeIntegration=yes');
  142. const event = await message;
  143. b.close();
  144. expect(event.data).to.equal('deliver');
  145. });
  146. });
  147. });
  148. describe('storage', () => {
  149. describe('DOM storage quota increase', () => {
  150. ['localStorage', 'sessionStorage'].forEach((storageName) => {
  151. const storage = window[storageName];
  152. it(`allows saving at least 40MiB in ${storageName}`, async () => {
  153. // Although JavaScript strings use UTF-16, the underlying
  154. // storage provider may encode strings differently, muddling the
  155. // translation between character and byte counts. However,
  156. // a string of 40 * 2^20 characters will require at least 40MiB
  157. // and presumably no more than 80MiB, a size guaranteed to
  158. // to exceed the original 10MiB quota yet stay within the
  159. // new 100MiB quota.
  160. // Note that both the key name and value affect the total size.
  161. const testKeyName = '_electronDOMStorageQuotaIncreasedTest';
  162. const length = 40 * Math.pow(2, 20) - testKeyName.length;
  163. storage.setItem(testKeyName, 'X'.repeat(length));
  164. // Wait at least one turn of the event loop to help avoid false positives
  165. // Although not entirely necessary, the previous version of this test case
  166. // failed to detect a real problem (perhaps related to DOM storage data caching)
  167. // wherein calling `getItem` immediately after `setItem` would appear to work
  168. // but then later (e.g. next tick) it would not.
  169. await delay(1);
  170. try {
  171. expect(storage.getItem(testKeyName)).to.have.lengthOf(length);
  172. } finally {
  173. storage.removeItem(testKeyName);
  174. }
  175. });
  176. it(`throws when attempting to use more than 128MiB in ${storageName}`, () => {
  177. expect(() => {
  178. const testKeyName = '_electronDOMStorageQuotaStillEnforcedTest';
  179. const length = 128 * Math.pow(2, 20) - testKeyName.length;
  180. try {
  181. storage.setItem(testKeyName, 'X'.repeat(length));
  182. } finally {
  183. storage.removeItem(testKeyName);
  184. }
  185. }).to.throw();
  186. });
  187. });
  188. });
  189. it('requesting persistent quota works', async () => {
  190. const grantedBytes = await new Promise(resolve => {
  191. navigator.webkitPersistentStorage.requestQuota(1024 * 1024, resolve);
  192. });
  193. expect(grantedBytes).to.equal(1048576);
  194. });
  195. });
  196. describe('Promise', () => {
  197. it('resolves correctly in Node.js calls', (done) => {
  198. class XElement extends HTMLElement {}
  199. customElements.define('x-element', XElement);
  200. setImmediate(() => {
  201. let called = false;
  202. Promise.resolve().then(() => {
  203. done(called ? undefined : new Error('wrong sequence'));
  204. });
  205. document.createElement('x-element');
  206. called = true;
  207. });
  208. });
  209. it('resolves correctly in Electron calls', (done) => {
  210. class YElement extends HTMLElement {}
  211. customElements.define('y-element', YElement);
  212. ipcRenderer.invoke('ping').then(() => {
  213. let called = false;
  214. Promise.resolve().then(() => {
  215. done(called ? undefined : new Error('wrong sequence'));
  216. });
  217. document.createElement('y-element');
  218. called = true;
  219. });
  220. });
  221. });
  222. describe('window.alert(message, title)', () => {
  223. it('throws an exception when the arguments cannot be converted to strings', () => {
  224. expect(() => {
  225. window.alert({ toString: null });
  226. }).to.throw('Cannot convert object to primitive value');
  227. });
  228. });
  229. describe('window.confirm(message, title)', () => {
  230. it('throws an exception when the arguments cannot be converted to strings', () => {
  231. expect(() => {
  232. window.confirm({ toString: null }, 'title');
  233. }).to.throw('Cannot convert object to primitive value');
  234. });
  235. });
  236. describe('window.history', () => {
  237. describe('window.history.go(offset)', () => {
  238. it('throws an exception when the argument cannot be converted to a string', () => {
  239. expect(() => {
  240. window.history.go({ toString: null });
  241. }).to.throw('Cannot convert object to primitive value');
  242. });
  243. });
  244. });
  245. // TODO(nornagon): this is broken on CI, it triggers:
  246. // [FATAL:speech_synthesis.mojom-shared.h(237)] The outgoing message will
  247. // trigger VALIDATION_ERROR_UNEXPECTED_NULL_POINTER at the receiving side
  248. // (null text in SpeechSynthesisUtterance struct).
  249. describe.skip('SpeechSynthesis', () => {
  250. before(function () {
  251. if (!features.isTtsEnabled()) {
  252. this.skip();
  253. }
  254. });
  255. it('should emit lifecycle events', async () => {
  256. const sentence = `long sentence which will take at least a few seconds to
  257. utter so that it's possible to pause and resume before the end`;
  258. const utter = new SpeechSynthesisUtterance(sentence);
  259. // Create a dummy utterance so that speech synthesis state
  260. // is initialized for later calls.
  261. speechSynthesis.speak(new SpeechSynthesisUtterance());
  262. speechSynthesis.cancel();
  263. speechSynthesis.speak(utter);
  264. // paused state after speak()
  265. expect(speechSynthesis.paused).to.be.false();
  266. await new Promise((resolve) => { utter.onstart = resolve; });
  267. // paused state after start event
  268. expect(speechSynthesis.paused).to.be.false();
  269. speechSynthesis.pause();
  270. // paused state changes async, right before the pause event
  271. expect(speechSynthesis.paused).to.be.false();
  272. await new Promise((resolve) => { utter.onpause = resolve; });
  273. expect(speechSynthesis.paused).to.be.true();
  274. speechSynthesis.resume();
  275. await new Promise((resolve) => { utter.onresume = resolve; });
  276. // paused state after resume event
  277. expect(speechSynthesis.paused).to.be.false();
  278. await new Promise((resolve) => { utter.onend = resolve; });
  279. });
  280. });
  281. });
  282. describe('console functions', () => {
  283. it('should exist', () => {
  284. expect(console.log, 'log').to.be.a('function');
  285. expect(console.error, 'error').to.be.a('function');
  286. expect(console.warn, 'warn').to.be.a('function');
  287. expect(console.info, 'info').to.be.a('function');
  288. expect(console.debug, 'debug').to.be.a('function');
  289. expect(console.trace, 'trace').to.be.a('function');
  290. expect(console.time, 'time').to.be.a('function');
  291. expect(console.timeEnd, 'timeEnd').to.be.a('function');
  292. });
  293. });