diff --git a/e2e/ambassador/src/background/index.js b/e2e/ambassador/src/background/index.js index e69de29..027c953 100644 --- a/e2e/ambassador/src/background/index.js +++ b/e2e/ambassador/src/background/index.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); + } +}); + diff --git a/e2e/ambassador/src/background/ipc.js b/e2e/ambassador/src/background/ipc.js new file mode 100644 index 0000000..95d2164 --- /dev/null +++ b/e2e/ambassador/src/background/ipc.js @@ -0,0 +1,7 @@ +const receiveContentMessage = (func) => { + browser.runtime.onMessage.addListener((message) => { + return func(message); + }); +}; + +export { receiveContentMessage }; diff --git a/e2e/ambassador/src/client/ipc.js b/e2e/ambassador/src/client/ipc.js new file mode 100644 index 0000000..9f232ea --- /dev/null +++ b/e2e/ambassador/src/client/ipc.js @@ -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 }; diff --git a/e2e/ambassador/src/client/windows.js b/e2e/ambassador/src/client/windows.js new file mode 100644 index 0000000..69b8905 --- /dev/null +++ b/e2e/ambassador/src/client/windows.js @@ -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 }; diff --git a/e2e/ambassador/src/content/index.js b/e2e/ambassador/src/content/index.js index e69de29..dd46688 100644 --- a/e2e/ambassador/src/content/index.js +++ b/e2e/ambassador/src/content/index.js @@ -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); + } +}); diff --git a/e2e/ambassador/src/content/ipc.js b/e2e/ambassador/src/content/ipc.js new file mode 100644 index 0000000..62fcac1 --- /dev/null +++ b/e2e/ambassador/src/content/ipc.js @@ -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 }; diff --git a/e2e/ambassador/src/shared/messages.js b/e2e/ambassador/src/shared/messages.js new file mode 100644 index 0000000..e8a0438 --- /dev/null +++ b/e2e/ambassador/src/shared/messages.js @@ -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, +}; diff --git a/e2e/contents/scroll.test.js b/e2e/contents/scroll.test.js new file mode 100644 index 0000000..6cf0042 --- /dev/null +++ b/e2e/contents/scroll.test.js @@ -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'); + }); +}); diff --git a/e2e/karma-delay.js b/e2e/karma-delay.js new file mode 100644 index 0000000..be7b448 --- /dev/null +++ b/e2e/karma-delay.js @@ -0,0 +1,10 @@ +'use strict'; + +window.__karma__.start = (function(start){ +return function(){ + var args = arguments + setTimeout(() => { + start(args) + }, 1000); +}; +}(window.__karma__.start)); diff --git a/e2e/karma.conf.js b/e2e/karma.conf.js index 259eb17..a6c115e 100644 --- a/e2e/karma.conf.js +++ b/e2e/karma.conf.js @@ -4,6 +4,7 @@ module.exports = function (config) { basePath: '', frameworks: ['mocha'], files: [ + 'karma-delay.js', '**/*.test.js' ],