test: move content-script tests to main runner (#20056)

This commit is contained in:
Jeremy Apthorp 2019-09-03 00:01:42 -07:00 committed by Cheng Zhao
parent 6e88b6b445
commit d7161742d2

View file

@ -1,17 +1,16 @@
const { expect } = require('chai') import { expect } from 'chai'
const { remote } = require('electron') import * as path from 'path'
const path = require('path')
const { closeWindow } = require('./window-helpers') import { closeWindow } from './window-helpers'
const { emittedNTimes } = require('./events-helpers') import { emittedNTimes } from './events-helpers'
const { BrowserWindow, ipcMain } = remote import { BrowserWindow, ipcMain, WebContents } from 'electron'
describe('chrome extension content scripts', () => { describe('chrome extension content scripts', () => {
const fixtures = path.resolve(__dirname, 'fixtures') const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures')
const extensionPath = path.resolve(fixtures, 'extensions') const extensionPath = path.resolve(fixtures, 'extensions')
const addExtension = (name) => BrowserWindow.addExtension(path.resolve(extensionPath, name)) const addExtension = (name: string) => BrowserWindow.addExtension(path.resolve(extensionPath, name))
const removeAllExtensions = () => { const removeAllExtensions = () => {
Object.keys(BrowserWindow.getExtensions()).map(extName => { Object.keys(BrowserWindow.getExtensions()).map(extName => {
BrowserWindow.removeExtension(extName) BrowserWindow.removeExtension(extName)
@ -19,7 +18,7 @@ describe('chrome extension content scripts', () => {
} }
let responseIdCounter = 0 let responseIdCounter = 0
const executeJavaScriptInFrame = (webContents, frameRoutingId, code) => { const executeJavaScriptInFrame = (webContents: WebContents, frameRoutingId: number, code: string) => {
return new Promise(resolve => { return new Promise(resolve => {
const responseId = responseIdCounter++ const responseId = responseIdCounter++
ipcMain.once(`executeJavaScriptInFrame_${responseId}`, (event, result) => { ipcMain.once(`executeJavaScriptInFrame_${responseId}`, (event, result) => {
@ -29,9 +28,9 @@ describe('chrome extension content scripts', () => {
}) })
} }
const generateTests = (sandboxEnabled, contextIsolationEnabled) => { const generateTests = (sandboxEnabled: boolean, contextIsolationEnabled: boolean) => {
describe(`with sandbox ${sandboxEnabled ? 'enabled' : 'disabled'} and context isolation ${contextIsolationEnabled ? 'enabled' : 'disabled'}`, () => { describe(`with sandbox ${sandboxEnabled ? 'enabled' : 'disabled'} and context isolation ${contextIsolationEnabled ? 'enabled' : 'disabled'}`, () => {
let w let w: BrowserWindow
describe('supports "run_at" option', () => { describe('supports "run_at" option', () => {
beforeEach(async () => { beforeEach(async () => {
@ -49,7 +48,7 @@ describe('chrome extension content scripts', () => {
afterEach(() => { afterEach(() => {
removeAllExtensions() removeAllExtensions()
return closeWindow(w).then(() => { w = null }) return closeWindow(w).then(() => { w = null as unknown as BrowserWindow })
}) })
it('should run content script at document_start', () => { it('should run content script at document_start', () => {
@ -107,7 +106,7 @@ describe('chrome extension content scripts', () => {
afterEach(() => afterEach(() =>
closeWindow(w).then(() => { closeWindow(w).then(() => {
w = null w = null as unknown as BrowserWindow
}) })
) )
@ -118,7 +117,7 @@ describe('chrome extension content scripts', () => {
await Promise.all( await Promise.all(
frameEvents.map(async frameEvent => { frameEvents.map(async frameEvent => {
const [, isMainFrame, , frameRoutingId] = frameEvent const [, isMainFrame, , frameRoutingId] = frameEvent
const result = await executeJavaScriptInFrame( const result: any = await executeJavaScriptInFrame(
w.webContents, w.webContents,
frameRoutingId, frameRoutingId,
`(() => { `(() => {