From c3ae476deb75dc4a08dbd11c3e75d356756169c3 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Mon, 8 Jul 2019 15:44:37 -0700 Subject: [PATCH] test: move some browser window specs to the main process (#19089) --- docs/api/browser-window.md | 7 +- spec-main/api-browser-window-spec.ts | 301 ++++++++++++++++++++++++- spec/api-browser-window-spec.js | 317 --------------------------- 3 files changed, 304 insertions(+), 321 deletions(-) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index e101f5157ef1..ed44ac9ec5f6 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -198,7 +198,8 @@ It creates a new `BrowserWindow` with native properties as set by the `options`. * `autoHideMenuBar` Boolean (optional) - Auto hide the menu bar unless the `Alt` key is pressed. Default is `false`. * `enableLargerThanScreen` Boolean (optional) - Enable the window to be resized larger - than screen. Default is `false`. + than screen. Only relevant for macOS, as other OSes allow + larger-than-screen windows by default. Default is `false`. * `backgroundColor` String (optional) - Window's background color as a hexadecimal value, like `#66CD00` or `#FFF` or `#80FFFFFF` (alpha in #AARRGGBB format is supported if `transparent` is set to `true`). Default is `#FFF` (white). @@ -1613,8 +1614,8 @@ Adds a window as a tab on this window, after the tab for the window instance. #### `win.setVibrancy(type)` _macOS_ -* `type` String - Can be `appearance-based`, `light`, `dark`, `titlebar`, -`selection`, `menu`, `popover`, `sidebar`, `medium-light` or `ultra-dark`. See +* `type` String | null - Can be `appearance-based`, `light`, `dark`, `titlebar`, + `selection`, `menu`, `popover`, `sidebar`, `medium-light` or `ultra-dark`. See the [macOS documentation][vibrancy-docs] for more details. Adds a vibrancy effect to the browser window. Passing `null` or an empty string diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 3346e09abd50..17d017821f60 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -2,10 +2,11 @@ import * as chai from 'chai' import * as chaiAsPromised from 'chai-as-promised' import * as path from 'path' import * as fs from 'fs' +import * as os from 'os' import * as qs from 'querystring' import * as http from 'http' import { AddressInfo } from 'net' -import { app, BrowserWindow, ipcMain, OnBeforeSendHeadersListenerDetails, screen, protocol } from 'electron' +import { app, BrowserWindow, BrowserView, ipcMain, OnBeforeSendHeadersListenerDetails, protocol, screen, webContents } from 'electron' import { emittedOnce } from './events-helpers'; import { closeWindow } from './window-helpers'; @@ -1079,4 +1080,302 @@ describe('BrowserWindow module', () => { }) }) + describe('BrowserWindow.setAutoHideCursor(autoHide)', () => { + let w = null as unknown as BrowserWindow + beforeEach(() => { + w = new BrowserWindow({show: false}) + }) + afterEach(async () => { + await closeWindow(w) + w = null as unknown as BrowserWindow + }) + + ifit(process.platform === 'darwin')('on macOS', () => { + it('allows changing cursor auto-hiding', () => { + expect(() => { + w.setAutoHideCursor(false) + w.setAutoHideCursor(true) + }).to.not.throw() + }) + }) + + ifit(process.platform !== 'darwin')('on non-macOS platforms', () => { + it('is not available', () => { + expect(w.setAutoHideCursor).to.be.undefined('setAutoHideCursor function') + }) + }) + }) + + ifdescribe(process.platform === 'darwin')('BrowserWindow.setWindowButtonVisibility()', () => { + afterEach(closeAllWindows) + + it('does not throw', () => { + const w = new BrowserWindow({show: false}) + expect(() => { + w.setWindowButtonVisibility(true) + w.setWindowButtonVisibility(false) + }).to.not.throw() + }) + + it('throws with custom title bar buttons', () => { + expect(() => { + const w = new BrowserWindow({ + show: false, + titleBarStyle: 'customButtonsOnHover', + frame: false + }) + w.setWindowButtonVisibility(true) + }).to.throw('Not supported for this window') + }) + }) + + ifdescribe(process.platform === 'darwin')('BrowserWindow.setVibrancy(type)', () => { + afterEach(closeAllWindows) + + it('allows setting, changing, and removing the vibrancy', () => { + const w = new BrowserWindow({show: false}) + expect(() => { + w.setVibrancy('light') + w.setVibrancy('dark') + w.setVibrancy(null) + w.setVibrancy('ultra-dark') + w.setVibrancy('' as any) + }).to.not.throw() + }) + }) + + ifdescribe(process.platform === 'win32')('BrowserWindow.setAppDetails(options)', () => { + afterEach(closeAllWindows) + + it('supports setting the app details', () => { + const w = new BrowserWindow({show: false}) + const iconPath = path.join(fixtures, 'assets', 'icon.ico') + + expect(() => { + w.setAppDetails({ appId: 'my.app.id' }) + w.setAppDetails({ appIconPath: iconPath, appIconIndex: 0 }) + w.setAppDetails({ appIconPath: iconPath }) + w.setAppDetails({ relaunchCommand: 'my-app.exe arg1 arg2', relaunchDisplayName: 'My app name' }) + w.setAppDetails({ relaunchCommand: 'my-app.exe arg1 arg2' }) + w.setAppDetails({ relaunchDisplayName: 'My app name' }) + w.setAppDetails({ + appId: 'my.app.id', + appIconPath: iconPath, + appIconIndex: 0, + relaunchCommand: 'my-app.exe arg1 arg2', + relaunchDisplayName: 'My app name' + }) + w.setAppDetails({}) + }).to.not.throw() + + expect(() => { + (w.setAppDetails as any)() + }).to.throw('Insufficient number of arguments.') + }) + }) + + describe('BrowserWindow.fromId(id)', () => { + afterEach(closeAllWindows) + it('returns the window with id', () => { + const w = new BrowserWindow({show: false}) + expect(BrowserWindow.fromId(w.id).id).to.equal(w.id) + }) + }) + + describe('BrowserWindow.fromWebContents(webContents)', () => { + afterEach(closeAllWindows) + + it('returns the window with the webContents', () => { + const w = new BrowserWindow({show: false}) + const found = BrowserWindow.fromWebContents(w.webContents) + expect(found.id).to.equal(w.id) + }) + + it('returns undefined for webContents without a BrowserWindow', () => { + const contents = (webContents as any).create({}) + try { + expect(BrowserWindow.fromWebContents(contents)).to.be.undefined('BrowserWindow.fromWebContents(contents)') + } finally { + contents.destroy() + } + }) + }) + + describe('BrowserWindow.openDevTools()', () => { + afterEach(closeAllWindows) + it('does not crash for frameless window', () => { + const w = new BrowserWindow({ show: false, frame: false }) + w.webContents.openDevTools() + }) + }) + + describe('BrowserWindow.fromBrowserView(browserView)', () => { + afterEach(closeAllWindows) + + it('returns the window with the browserView', () => { + const w = new BrowserWindow({ show: false }) + const bv = new BrowserView + w.setBrowserView(bv) + expect(BrowserWindow.fromBrowserView(bv)!.id).to.equal(w.id) + }) + + it('returns undefined if not attached', () => { + const bv = new BrowserView + expect(BrowserWindow.fromBrowserView(bv)).to.be.null('BrowserWindow associated with bv') + }) + }) + + describe('BrowserWindow.setOpacity(opacity)', () => { + afterEach(closeAllWindows) + it('make window with initial opacity', () => { + const w = new BrowserWindow({ show: false, opacity: 0.5 }) + expect(w.getOpacity()).to.equal(0.5) + }) + it('allows setting the opacity', () => { + const w = new BrowserWindow({ show: false }) + expect(() => { + w.setOpacity(0.0) + expect(w.getOpacity()).to.equal(0.0) + w.setOpacity(0.5) + expect(w.getOpacity()).to.equal(0.5) + w.setOpacity(1.0) + expect(w.getOpacity()).to.equal(1.0) + }).to.not.throw() + }) + }) + + describe('BrowserWindow.setShape(rects)', () => { + afterEach(closeAllWindows) + it('allows setting shape', () => { + const w = new BrowserWindow({ show: false }) + expect(() => { + w.setShape([]) + w.setShape([{ x: 0, y: 0, width: 100, height: 100 }]) + w.setShape([{ x: 0, y: 0, width: 100, height: 100 }, { x: 0, y: 200, width: 1000, height: 100 }]) + w.setShape([]) + }).to.not.throw() + }) + }) + + describe('"useContentSize" option', () => { + afterEach(closeAllWindows) + it('make window created with content size when used', () => { + const w = new BrowserWindow({ + show: false, + width: 400, + height: 400, + useContentSize: true + }) + const contentSize = w.getContentSize() + expect(contentSize).to.deep.equal([400, 400]) + }) + it('make window created with window size when not used', () => { + const w = new BrowserWindow({ + show: false, + width: 400, + height: 400, + }) + const size = w.getSize() + expect(size).to.deep.equal([400, 400]) + }) + it('works for a frameless window', () => { + const w = new BrowserWindow({ + show: false, + frame: false, + width: 400, + height: 400, + useContentSize: true + }) + const contentSize = w.getContentSize() + expect(contentSize).to.deep.equal([400, 400]) + const size = w.getSize() + expect(size).to.deep.equal([400, 400]) + }) + }) + + ifdescribe(process.platform === 'darwin' && parseInt(os.release().split('.')[0]) >= 14)('"titleBarStyle" option', () => { + afterEach(closeAllWindows) + it('creates browser window with hidden title bar', () => { + const w = new BrowserWindow({ + show: false, + width: 400, + height: 400, + titleBarStyle: 'hidden' + }) + const contentSize = w.getContentSize() + expect(contentSize).to.deep.equal([400, 400]) + }) + it('creates browser window with hidden inset title bar', () => { + const w = new BrowserWindow({ + show: false, + width: 400, + height: 400, + titleBarStyle: 'hiddenInset' + }) + const contentSize = w.getContentSize() + expect(contentSize).to.deep.equal([400, 400]) + }) + }) + + ifdescribe(process.platform === 'darwin')('"enableLargerThanScreen" option', () => { + afterEach(closeAllWindows) + it('can move the window out of screen', () => { + const w = new BrowserWindow({ show: true, enableLargerThanScreen: true }) + w.setPosition(-10, -10) + const after = w.getPosition() + expect(after).to.deep.equal([-10, -10]) + }) + it('without it, cannot move the window out of screen', () => { + const w = new BrowserWindow({ show: true, enableLargerThanScreen: false }) + w.setPosition(-10, -10) + const after = w.getPosition() + expect(after[1]).to.be.at.least(0) + }) + it('can set the window larger than screen', () => { + const w = new BrowserWindow({ show: true, enableLargerThanScreen: true }) + const size = screen.getPrimaryDisplay().size + size.width += 100 + size.height += 100 + w.setSize(size.width, size.height) + expectBoundsEqual(w.getSize(), [size.width, size.height]) + }) + it('without it, cannot set the window larger than screen', () => { + const w = new BrowserWindow({ show: true, enableLargerThanScreen: false }) + const size = screen.getPrimaryDisplay().size + size.width += 100 + size.height += 100 + w.setSize(size.width, size.height) + expect(w.getSize()[1]).to.at.most(screen.getPrimaryDisplay().size.height) + }) + }) + + ifdescribe(process.platform === 'darwin')('"zoomToPageWidth" option', () => { + afterEach(closeAllWindows) + it('sets the window width to the page width when used', () => { + const w = new BrowserWindow({ + show: false, + width: 500, + height: 400, + zoomToPageWidth: true + }) + w.maximize() + expect(w.getSize()[0]).to.equal(500) + }) + }) + + describe('"tabbingIdentifier" option', () => { + afterEach(closeAllWindows) + it('can be set on a window', () => { + expect(() => { + new BrowserWindow({ + tabbingIdentifier: 'group1' + }) + new BrowserWindow({ + tabbingIdentifier: 'group2', + frame: false + }) + }).not.to.throw() + }) + }) + }) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index fe4bb8886dd5..2a95bd2ef1f4 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -113,323 +113,6 @@ describe('BrowserWindow module', () => { afterEach(closeTheWindow) - describe('BrowserWindow.setAutoHideCursor(autoHide)', () => { - describe('on macOS', () => { - before(function () { - if (process.platform !== 'darwin') { - this.skip() - } - }) - - it('allows changing cursor auto-hiding', () => { - expect(() => { - w.setAutoHideCursor(false) - w.setAutoHideCursor(true) - }).to.not.throw() - }) - }) - - describe('on non-macOS platforms', () => { - before(function () { - if (process.platform === 'darwin') { - this.skip() - } - }) - - it('is not available', () => { - expect(w.setAutoHideCursor).to.be.undefined() - }) - }) - }) - - describe('BrowserWindow.setWindowButtonVisibility()', () => { - before(function () { - if (process.platform !== 'darwin') { - this.skip() - } - }) - - it('does not throw', () => { - expect(() => { - w.setWindowButtonVisibility(true) - w.setWindowButtonVisibility(false) - }).to.not.throw() - }) - - it('throws with custom title bar buttons', () => { - expect(() => { - w.destroy() - w = new BrowserWindow({ - show: false, - titleBarStyle: 'customButtonsOnHover', - frame: false - }) - w.setWindowButtonVisibility(true) - }).to.throw('Not supported for this window') - }) - }) - - describe('BrowserWindow.setVibrancy(type)', () => { - it('allows setting, changing, and removing the vibrancy', () => { - expect(() => { - w.setVibrancy('light') - w.setVibrancy('dark') - w.setVibrancy(null) - w.setVibrancy('ultra-dark') - w.setVibrancy('') - }).to.not.throw() - }) - }) - - describe('BrowserWindow.setAppDetails(options)', () => { - before(function () { - if (process.platform !== 'win32') { - this.skip() - } - }) - - it('supports setting the app details', () => { - const iconPath = path.join(fixtures, 'assets', 'icon.ico') - - expect(() => { - w.setAppDetails({ appId: 'my.app.id' }) - w.setAppDetails({ appIconPath: iconPath, appIconIndex: 0 }) - w.setAppDetails({ appIconPath: iconPath }) - w.setAppDetails({ relaunchCommand: 'my-app.exe arg1 arg2', relaunchDisplayName: 'My app name' }) - w.setAppDetails({ relaunchCommand: 'my-app.exe arg1 arg2' }) - w.setAppDetails({ relaunchDisplayName: 'My app name' }) - w.setAppDetails({ - appId: 'my.app.id', - appIconPath: iconPath, - appIconIndex: 0, - relaunchCommand: 'my-app.exe arg1 arg2', - relaunchDisplayName: 'My app name' - }) - w.setAppDetails({}) - }).to.not.throw() - - expect(() => { - w.setAppDetails() - }).to.throw('Insufficient number of arguments.') - }) - }) - - describe('BrowserWindow.fromId(id)', () => { - it('returns the window with id', () => { - expect(BrowserWindow.fromId(w.id).id).to.equal(w.id) - }) - }) - - describe('BrowserWindow.fromWebContents(webContents)', () => { - let contents = null - - beforeEach(() => { contents = webContents.create({}) }) - - afterEach(() => { contents.destroy() }) - - it('returns the window with the webContents', () => { - expect(BrowserWindow.fromWebContents(w.webContents).id).to.equal(w.id) - expect(BrowserWindow.fromWebContents(contents)).to.be.undefined() - }) - }) - - describe('BrowserWindow.openDevTools()', () => { - it('does not crash for frameless window', () => { - w.destroy() - w = new BrowserWindow({ show: false }) - w.openDevTools() - }) - }) - - describe('BrowserWindow.fromBrowserView(browserView)', () => { - let bv = null - - beforeEach(() => { - bv = new BrowserView() - w.setBrowserView(bv) - }) - - afterEach(() => { - w.setBrowserView(null) - bv.destroy() - }) - - it('returns the window with the browserView', () => { - expect(BrowserWindow.fromBrowserView(bv).id).to.equal(w.id) - }) - - it('returns undefined if not attached', () => { - w.setBrowserView(null) - expect(BrowserWindow.fromBrowserView(bv)).to.be.null() - }) - }) - - describe('BrowserWindow.setOpacity(opacity)', () => { - it('make window with initial opacity', () => { - w.destroy() - w = new BrowserWindow({ - show: false, - width: 400, - height: 400, - opacity: 0.5 - }) - expect(w.getOpacity()).to.equal(0.5) - }) - it('allows setting the opacity', () => { - expect(() => { - w.setOpacity(0.0) - expect(w.getOpacity()).to.equal(0.0) - w.setOpacity(0.5) - expect(w.getOpacity()).to.equal(0.5) - w.setOpacity(1.0) - expect(w.getOpacity()).to.equal(1.0) - }).to.not.throw() - }) - }) - - describe('BrowserWindow.setShape(rects)', () => { - it('allows setting shape', () => { - expect(() => { - w.setShape([]) - w.setShape([{ x: 0, y: 0, width: 100, height: 100 }]) - w.setShape([{ x: 0, y: 0, width: 100, height: 100 }, { x: 0, y: 200, width: 1000, height: 100 }]) - w.setShape([]) - }).to.not.throw() - }) - }) - - describe('"useContentSize" option', () => { - it('make window created with content size when used', () => { - w.destroy() - w = new BrowserWindow({ - show: false, - width: 400, - height: 400, - useContentSize: true - }) - const contentSize = w.getContentSize() - expect(contentSize).to.deep.equal([400, 400]) - }) - it('make window created with window size when not used', () => { - const size = w.getSize() - expect(size).to.deep.equal([400, 400]) - }) - it('works for a frameless window', () => { - w.destroy() - w = new BrowserWindow({ - show: false, - frame: false, - width: 400, - height: 400, - useContentSize: true - }) - const contentSize = w.getContentSize() - expect(contentSize).to.deep.equal([400, 400]) - const size = w.getSize() - expect(size).to.deep.equal([400, 400]) - }) - }) - - describe('"titleBarStyle" option', () => { - before(function () { - if (process.platform !== 'darwin') { - this.skip() - } - - if (parseInt(os.release().split('.')[0]) < 14) { - this.skip() - } - }) - - it('creates browser window with hidden title bar', () => { - w.destroy() - w = new BrowserWindow({ - show: false, - width: 400, - height: 400, - titleBarStyle: 'hidden' - }) - const contentSize = w.getContentSize() - expect(contentSize).to.deep.equal([400, 400]) - }) - it('creates browser window with hidden inset title bar', () => { - w.destroy() - w = new BrowserWindow({ - show: false, - width: 400, - height: 400, - titleBarStyle: 'hiddenInset' - }) - const contentSize = w.getContentSize() - expect(contentSize).to.deep.equal([400, 400]) - }) - }) - - describe('enableLargerThanScreen" option', () => { - before(function () { - if (process.platform === 'linux') { - this.skip() - } - }) - - beforeEach(() => { - w.destroy() - w = new BrowserWindow({ - show: true, - width: 400, - height: 400, - enableLargerThanScreen: true - }) - }) - - it('can move the window out of screen', () => { - w.setPosition(-10, -10) - const after = w.getPosition() - expect(after).to.deep.equal([-10, -10]) - }) - it('can set the window larger than screen', () => { - const size = screen.getPrimaryDisplay().size - size.width += 100 - size.height += 100 - w.setSize(size.width, size.height) - expectBoundsEqual(w.getSize(), [size.width, size.height]) - }) - }) - - describe('"zoomToPageWidth" option', () => { - before(function () { - if (process.platform !== 'darwin') { - this.skip() - } - }) - - it('sets the window width to the page width when used', () => { - w.destroy() - w = new BrowserWindow({ - show: false, - width: 500, - height: 400, - zoomToPageWidth: true - }) - w.maximize() - expect(w.getSize()[0]).to.equal(500) - }) - }) - - describe('"tabbingIdentifier" option', () => { - it('can be set on a window', () => { - w.destroy() - w = new BrowserWindow({ - tabbingIdentifier: 'group1' - }) - w.destroy() - w = new BrowserWindow({ - tabbingIdentifier: 'group2', - frame: false - }) - }) - }) - describe('"webPreferences" option', () => { afterEach(() => { ipcMain.removeAllListeners('answer') })