Skip to content
Permalink
Browse files

Update all tests to work with the new browserviews

  • Loading branch information...
pfrazee committed Mar 4, 2019
1 parent 220e27f commit 1fa5756a1e9f02f49bdd84fe6e289798ee2f1e64
@@ -2,6 +2,9 @@ import dgram from 'dgram'
import {ipcMain} from 'electron'
import * as beakerCore from '@beaker/core'
import * as windows from './ui/windows'
import * as viewManager from './ui/view-manager'
import * as permPrompt from './ui/subwindows/perm-prompt'
import * as modals from './ui/subwindows/modals'

const LOG_MESSAGES = false

@@ -68,59 +71,66 @@ async function onMessage (message) {

const METHODS = {
newTab () {
return execute(`
var index = pages.getAll().length
page = pages.create()
pages.setActive(page)
index
`)
var win = getActiveWindow()
var view = viewManager.create(win, undefined, {setActive: true})
return viewManager.getIndexOfView(win, view)
},

navigateTo (page, url) {
return execute(`
var page = pages.get(${page})
page.navbarEl.querySelector('.nav-location-input').value = "${url}"
page.navbarEl.querySelector('.nav-location-input').blur()
var loadPromise = new Promise(resolve => {
function onDomReady () {
page.webviewEl.removeEventListener('dom-ready', onDomReady)
resolve()
}
page.webviewEl.addEventListener('dom-ready', onDomReady)
})
page.loadURL("${url}")
loadPromise
`)
var view = viewManager.getByIndex(getActiveWindow(), page)
var loadPromise = new Promise(resolve => view.webContents.once('dom-ready', () => resolve()))
view.loadURL(url)
return loadPromise
},

getUrl (page) {
return execute(`
var page = pages.get(${page})
page.getURL()
`)
var view = viewManager.getByIndex(getActiveWindow(), page)
return view.url
},

async executeJavascriptInShell (js) {
var res = await execute(js)
var win = getActiveWindow()
var res = await win.webContents.executeJavaScript(js)
return res
},

async executeJavascriptOnPage (page, js) {
try {
var res = await execute(`
var page = pages.get(${page})
page.webviewEl.getWebContents().executeJavaScript(\`` + js + `\`)
`)
return res
} catch (e) {
console.error('Failed to execute javascript on page', js, e)
throw e
}
var view = viewManager.getByIndex(getActiveWindow(), page)
var res = await view.webContents.executeJavaScript(js)
return res
},

async executeJavascriptInPermPrompt (page, js) {
var view = viewManager.getByIndex(getActiveWindow(), page).browserView
var prompt = await waitFor(() => permPrompt.get(view))
var res = await prompt.webContents.executeJavaScript(js)
return res
},

async executeJavascriptInModal (page, js) {
var view = viewManager.getByIndex(getActiveWindow(), page).browserView
var modal = await waitFor(() => modals.get(view))
var res = await modal.webContents.executeJavaScript(js)
return res
}
}

function execute (js) {
function getActiveWindow () {
var win = windows.getActiveWindow()
return win.webContents.executeJavaScript(js)
while (win.getParentWindow()) {
win = win.getParentWindow()
}
return win
}

function waitFor (condFn) {
return new Promise(resolve => {
var i = setInterval(async () => {
var res = condFn()
if (!!res) {
clearInterval(i)
return resolve(res)
}
}, 100)
})
}
@@ -106,19 +106,23 @@ export async function create (webContents, modalName, params = {}) {
return result
}

export async function show (parentView) {
export function get (parentView) {
return windows[parentView.id]
}

export function show (parentView) {
if (parentView.id in windows) {
windows[parentView.id].show()
}
}

export async function hide (parentView) {
export function hide (parentView) {
if (parentView.id in windows) {
windows[parentView.id].hide()
}
}

export async function close (parentView) {
export function close (parentView) {
if (parentView.id in windows) {
windows[parentView.id].close()
delete windows[parentView.id]
@@ -82,19 +82,23 @@ export async function create (parentWindow, parentView, params) {
return decision
}

export async function show (parentView) {
export function get (parentView) {
return windows[parentView.id]
}

export function show (parentView) {
if (parentView.id in windows) {
windows[parentView.id].show()
}
}

export async function hide (parentView) {
export function hide (parentView) {
if (parentView.id in windows) {
windows[parentView.id].hide()
}
}

export async function close (parentView) {
export function close (parentView) {
if (parentView.id in windows) {
windows[parentView.id].close()
delete windows[parentView.id]
@@ -638,6 +638,11 @@ export function getByIndex (win, index) {
return getAll(win)[index]
}

export function getIndexOfView (win, view) {
win = getTopWindow(win)
return getAll(win).indexOf(view)
}

export function getAllPinned (win) {
win = getTopWindow(win)
return getAll(win).filter(p => p.isPinned)
@@ -197,16 +197,6 @@ export function createShellWindow (windowState) {
viewManager.loadPins(win)
}
viewManager.initializeFromSnapshot(win, state.pages)
if (isTestDriverActive) {
// HACK
// For some reason, when the sandbox is enabled, executeJavaScript doesnt work in the browser window until the devtools are opened.
// Since it's kind of handy to have the devtools open anyway, open them automatically when tests are running.
// No, I am not above this.
// -prf
setTimeout(() => {
win.webContents.openDevTools()
}, 1e3)
}
}
}

@@ -30,12 +30,16 @@ class ModalsWrapper extends LitElement {
}

async runModal (name, params) {
window.isModalActive = true
this.currentModal = name
await this.updateComplete
return new Promise((resolve, reject) => {
this.cbs = {resolve, reject}
this.shadowRoot.querySelector(`${name}-modal`).init(params, {resolve, reject})
})
}).then(
v => { window.isModalActive = false; return v },
v => { window.isModalActive = false; throw v }
)
}

render () {
@@ -21,6 +21,10 @@ class CreateArchiveModal extends LitElement {
this.type = null
this.links = null
this.networked = true

// export interface
window.createArchiveClickSubmit = () => this.shadowRoot.querySelector('button[type="submit"]').click()
window.createArchiveClickCancel = () => this.shadowRoot.querySelector('.cancel').click()
}

async init (params, cbs) {
@@ -37,6 +37,10 @@ class ForkArchiveModal extends LitElement {
this.type = null
this.links = null
this.networked = true

// export interface
window.forkArchiveClickSubmit = () => this.shadowRoot.querySelector('button[type="submit"]').click()
window.forkArchiveClickCancel = () => this.shadowRoot.querySelector('.cancel').click()
}

async init (params, cbs) {
@@ -38,6 +38,28 @@ class SelectArchiveModal extends LitElement {
this.buttonLabel = 'Select'
this.type = null
this.cbs = null

// export interface
window.selectArchiveClickSubmit = () => this.shadowRoot.querySelector('button[type="submit"]').click()
window.selectArchiveClickCancel = () => this.shadowRoot.querySelector('.cancel').click()
window.window.selectArchiveClickNewArchive = () => {
this.shadowRoot.querySelector('.btn[data-content="newArchive"]').click()
return this.requestUpdate()
}
window.selectArchiveClickItem = (key) => {
this.shadowRoot.querySelector(`li[data-key="${key}"]`).click()
this.selectedArchiveKey = key
return this.requestUpdate()
}
window.selectArchiveClickAnyItem = () => {
this.shadowRoot.querySelector(`li[data-key]`).click()
this.selectedArchiveKey = this.archives[0].key
return this.requestUpdate()
}
window.window.selectArchiveSetValueTitle = (v) => {
this.shadowRoot.querySelector('input[name="title"]').value = v
this.title = v
}
}

async init (params, cbs) {
@@ -20,10 +20,15 @@ class PermPrompt extends LitElement {
this.isPermExperimental = false

// export interface
window.isPromptActive = false
window.runPrompt = this.runPrompt.bind(this)
window.clickAccept = () => this.shadowRoot.querySelector('.prompt-accept').click()
window.clickReject = () => this.shadowRoot.querySelector('.prompt-reject').click()
}

async runPrompt ({permission, url, opts}) {
window.isPromptActive = true

// lookup the perm description. auto-deny if it's not a known perm.
this.url = url
this.permId = getPermId(permission)
@@ -52,6 +57,9 @@ class PermPrompt extends LitElement {
// setup promise
return new Promise(resolve => {
this.resolve = resolve
}).then(v => {
window.isPromptActive = false
return v
})
}

Oops, something went wrong.

0 comments on commit 1fa5756

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