first webextension test

jh-changes
Shin'ya Ueoka 7 years ago
parent bb5b6d367c
commit f6f64297aa
  1. 12
      e2e/ambassador/src/background/index.js
  2. 7
      e2e/ambassador/src/background/ipc.js
  3. 29
      e2e/ambassador/src/client/ipc.js
  4. 17
      e2e/ambassador/src/client/windows.js
  5. 11
      e2e/ambassador/src/content/index.js
  6. 31
      e2e/ambassador/src/content/ipc.js
  7. 12
      e2e/ambassador/src/shared/messages.js
  8. 19
      e2e/contents/scroll.test.js
  9. 10
      e2e/karma-delay.js
  10. 1
      e2e/karma.conf.js

@ -0,0 +1,12 @@
import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages';
import { receiveContentMessage } from './ipc';
receiveContentMessage((message) => {
switch (message.type) {
case WINDOWS_CREATE:
return browser.windows.create();
case WINDOWS_REMOVE:
return browser.windows.remove(message.windowId);
}
});

@ -0,0 +1,7 @@
const receiveContentMessage = (func) => {
browser.runtime.onMessage.addListener((message) => {
return func(message);
});
};
export { receiveContentMessage };

@ -0,0 +1,29 @@
import { METHOD_REQUEST, METHOD_RESPONSE } from '../shared/messages';
const generateId = () => {
return Math.random().toString();
};
const send = (message) => {
return new Promise((resolve) => {
let id = generateId();
let callback = (e) => {
let packet = e.data;
if (e.source !== window || packet.method !== METHOD_RESPONSE ||
packet.id !== id) {
return;
}
window.removeEventListener('message', callback);
resolve(packet.message);
};
window.addEventListener('message', callback);
window.postMessage({
id,
method: METHOD_REQUEST,
message
}, window.origin);
});
};
export { send };

@ -0,0 +1,17 @@
import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages';
import * as ipc from './ipc';
const create = () => {
return ipc.send({
type: WINDOWS_CREATE
});
};
const remove = (windowId) => {
return ipc.send({
type: WINDOWS_REMOVE,
windowId,
});
};
export { create, remove };

@ -0,0 +1,11 @@
import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages';
import * as ipc from './ipc';
ipc.receivePageMessage((message) => {
switch (message.type) {
case WINDOWS_CREATE:
return ipc.send(message);
case WINDOWS_REMOVE:
return ipc.send(message);
}
});

@ -0,0 +1,31 @@
import { METHOD_REQUEST, METHOD_RESPONSE } from '../shared/messages';
const send = (message) => {
return browser.runtime.sendMessage(message);
};
const receivePageMessage = (func) => {
window.addEventListener('message', (e) => {
let packet = e.data;
if (e.origin !== window.origin || packet.method !== METHOD_REQUEST) {
return;
}
let resp = {
id: packet.id,
method: METHOD_RESPONSE,
};
let respMessage = func(packet.message);
if (respMessage instanceof Promise) {
return respMessage.then((data) => {
resp.message = data;
e.source.postMessage(resp, e.origin);
});
} else if (respMessage) {
resp.message = respMessage;
}
e.source.postMessage(resp, e.origin);
});
};
export { send, receivePageMessage };

@ -0,0 +1,12 @@
const METHOD_REQUEST = 'request';
const METHOD_RESPONSE = 'response';
const WINDOWS_CREATE = 'windows.create';
const WINDOWS_REMOVE = 'windows.remove';
export {
METHOD_REQUEST,
METHOD_RESPONSE,
WINDOWS_CREATE,
WINDOWS_REMOVE,
};

@ -0,0 +1,19 @@
import { expect } from "chai";
import * as windows from "../ambassador/src/client/windows";
describe("scroll test", () => {
let targetWindow;
before(() => {
return windows.create().then((win) => {
targetWindow = win;
});
});
after(() => {
return windows.remove(targetWindow.id);
});
it('runs test', () => {
expect(targetWindow.id).be.a('number');
});
});

@ -0,0 +1,10 @@
'use strict';
window.__karma__.start = (function(start){
return function(){
var args = arguments
setTimeout(() => {
start(args)
}, 1000);
};
}(window.__karma__.start));

@ -4,6 +4,7 @@ module.exports = function (config) {
basePath: '',
frameworks: ['mocha'],
files: [
'karma-delay.js',
'**/*.test.js'
],