Skip to content
Permalink
Browse files

test: move some browser window specs to the main process (#19089)

  • Loading branch information...
nornagon committed Jul 8, 2019
1 parent a04a458 commit c3ae476deb75dc4a08dbd11c3e75d356756169c3
Showing with 304 additions and 321 deletions.
  1. +4 −3 docs/api/browser-window.md
  2. +300 −1 spec-main/api-browser-window-spec.ts
  3. +0 −317 spec/api-browser-window-spec.js
@@ -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
@@ -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()
})
})

})

0 comments on commit c3ae476

Please sign in to comment.
You can’t perform that action at this time.