Shelley Vohr
2023-10-02 10:35:00 +02:00
committed by GitHub
parent 371e83a8d2
commit 5643e86956
3 changed files with 158 additions and 66 deletions

View File

@@ -1039,33 +1039,77 @@ describe('chrome extensions', () => {
});
});
it('update', async () => {
await w.loadURL(url);
describe('update', () => {
it('can update muted status', async () => {
await w.loadURL(url);
const message = { method: 'update', args: [{ muted: true }] };
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
const message = { method: 'update', args: [{ muted: true }] };
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
const [,, responseString] = await once(w.webContents, 'console-message');
const response = JSON.parse(responseString);
const [,, responseString] = await once(w.webContents, 'console-message');
const response = JSON.parse(responseString);
expect(response).to.have.property('url').that.is.a('string');
expect(response).to.have.property('title').that.is.a('string');
expect(response).to.have.property('active').that.is.a('boolean');
expect(response).to.have.property('autoDiscardable').that.is.a('boolean');
expect(response).to.have.property('discarded').that.is.a('boolean');
expect(response).to.have.property('groupId').that.is.a('number');
expect(response).to.have.property('highlighted').that.is.a('boolean');
expect(response).to.have.property('id').that.is.a('number');
expect(response).to.have.property('incognito').that.is.a('boolean');
expect(response).to.have.property('index').that.is.a('number');
expect(response).to.have.property('pinned').that.is.a('boolean');
expect(response).to.have.property('selected').that.is.a('boolean');
expect(response).to.have.property('windowId').that.is.a('number');
expect(response).to.have.property('mutedInfo').that.is.a('object');
const { mutedInfo } = response;
expect(mutedInfo).to.deep.eq({
muted: true,
reason: 'user'
expect(response).to.have.property('mutedInfo').that.is.a('object');
const { mutedInfo } = response;
expect(mutedInfo).to.deep.eq({
muted: true,
reason: 'user'
});
});
it('fails when navigating to an invalid url', async () => {
await w.loadURL(url);
const message = { method: 'update', args: [{ url: 'chrome://crash' }] };
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
const [,, responseString] = await once(w.webContents, 'console-message');
const { error } = JSON.parse(responseString);
expect(error).to.eq('I\'m sorry. I\'m afraid I can\'t do that.');
});
it('fails when navigating to prohibited url', async () => {
await w.loadURL(url);
const message = { method: 'update', args: [{ url: 'chrome://crash' }] };
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
const [,, responseString] = await once(w.webContents, 'console-message');
const { error } = JSON.parse(responseString);
expect(error).to.eq('I\'m sorry. I\'m afraid I can\'t do that.');
});
it('fails when navigating to a devtools url without permission', async () => {
await w.loadURL(url);
const message = { method: 'update', args: [{ url: 'devtools://blah' }] };
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
const [, , responseString] = await once(w.webContents, 'console-message');
const { error } = JSON.parse(responseString);
expect(error).to.eq('Cannot navigate to a devtools:// page without either the devtools or debugger permission.');
});
it('fails when navigating to a chrome-untrusted url', async () => {
await w.loadURL(url);
const message = { method: 'update', args: [{ url: 'chrome-untrusted://blah' }] };
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
const [, , responseString] = await once(w.webContents, 'console-message');
const { error } = JSON.parse(responseString);
expect(error).to.eq('Cannot navigate to a chrome-untrusted:// page.');
});
it('fails when navigating to a file url withotut file access', async () => {
await w.loadURL(url);
const message = { method: 'update', args: [{ url: 'file://blah' }] };
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
const [, , responseString] = await once(w.webContents, 'console-message');
const { error } = JSON.parse(responseString);
expect(error).to.eq('Cannot navigate to a file URL without local file access.');
});
});

View File

@@ -1,6 +1,6 @@
/* global chrome */
const handleRequest = (request, sender, sendResponse) => {
const handleRequest = async (request, sender, sendResponse) => {
const { method, args = [] } = request;
const tabId = sender.tab.id;
@@ -53,7 +53,12 @@ const handleRequest = (request, sender, sendResponse) => {
case 'update': {
const [params] = args;
chrome.tabs.update(tabId, params).then(sendResponse);
try {
const response = await chrome.tabs.update(tabId, params);
sendResponse(response);
} catch (error) {
sendResponse({ error: error.message });
}
break;
}
}