From 8ff200755f6b3661c405b14fe1ff6dc5b0f17dbd Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 4 Feb 2018 17:44:40 +0900 Subject: add ambassador extension --- e2e/ambassador/src/content/index.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 e2e/ambassador/src/content/index.js (limited to 'e2e/ambassador/src/content') diff --git a/e2e/ambassador/src/content/index.js b/e2e/ambassador/src/content/index.js new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From f6f64297aa030cd3fbad4bbb9e322e469bc762e8 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 4 Feb 2018 19:27:22 +0900 Subject: first webextension test --- e2e/ambassador/src/background/index.js | 12 ++++++++++++ e2e/ambassador/src/background/ipc.js | 7 +++++++ e2e/ambassador/src/client/ipc.js | 29 +++++++++++++++++++++++++++++ e2e/ambassador/src/client/windows.js | 17 +++++++++++++++++ e2e/ambassador/src/content/index.js | 11 +++++++++++ e2e/ambassador/src/content/ipc.js | 31 +++++++++++++++++++++++++++++++ e2e/ambassador/src/shared/messages.js | 12 ++++++++++++ e2e/contents/scroll.test.js | 19 +++++++++++++++++++ e2e/karma-delay.js | 10 ++++++++++ e2e/karma.conf.js | 1 + 10 files changed, 149 insertions(+) create mode 100644 e2e/ambassador/src/background/ipc.js create mode 100644 e2e/ambassador/src/client/ipc.js create mode 100644 e2e/ambassador/src/client/windows.js create mode 100644 e2e/ambassador/src/content/ipc.js create mode 100644 e2e/ambassador/src/shared/messages.js create mode 100644 e2e/contents/scroll.test.js create mode 100644 e2e/karma-delay.js (limited to 'e2e/ambassador/src/content') 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' ], -- cgit v1.2.3 From 3f8bbe11d96b751affa9e2bf0a1995008f9e3220 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 11 Feb 2018 15:40:34 +0900 Subject: send press key to tabs --- e2e/ambassador/manifest.json | 3 ++- e2e/ambassador/src/background/index.js | 25 ++++++++++++++++++++++-- e2e/ambassador/src/client/keys.js | 29 ++++++++++++++++++++++++++++ e2e/ambassador/src/client/tabs.js | 12 ++++++++++++ e2e/ambassador/src/client/windows.js | 5 +++-- e2e/ambassador/src/content/index.js | 30 ++++++++++++++++++++++++++--- e2e/ambassador/src/content/ipc.js | 13 +++++++++++-- e2e/ambassador/src/shared/messages.js | 10 ++++++++++ e2e/contents/scroll.test.js | 19 ------------------ e2e/contents/tab.test.js | 35 ++++++++++++++++++++++++++++++++++ e2e/karma-delay.js | 2 +- 11 files changed, 153 insertions(+), 30 deletions(-) create mode 100644 e2e/ambassador/src/client/keys.js create mode 100644 e2e/ambassador/src/client/tabs.js delete mode 100644 e2e/contents/scroll.test.js create mode 100644 e2e/contents/tab.test.js (limited to 'e2e/ambassador/src/content') diff --git a/e2e/ambassador/manifest.json b/e2e/ambassador/manifest.json index b9b0331..d2253f6 100644 --- a/e2e/ambassador/manifest.json +++ b/e2e/ambassador/manifest.json @@ -22,6 +22,7 @@ "sessions", "storage", "tabs", - "clipboardRead" + "clipboardRead", + "activeTab" ] } diff --git a/e2e/ambassador/src/background/index.js b/e2e/ambassador/src/background/index.js index 027c953..9e64986 100644 --- a/e2e/ambassador/src/background/index.js +++ b/e2e/ambassador/src/background/index.js @@ -1,12 +1,33 @@ -import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages'; +import { + WINDOWS_CREATE, WINDOWS_REMOVE, TABS_CREATE, + EVENT_KEYPRESS, EVENT_KEYDOWN, EVENT_KEYUP, +} from '../shared/messages'; +import * as tabs from './tabs'; import { receiveContentMessage } from './ipc'; receiveContentMessage((message) => { switch (message.type) { case WINDOWS_CREATE: - return browser.windows.create(); + return browser.windows.create({ url: message.url }); case WINDOWS_REMOVE: return browser.windows.remove(message.windowId); + case TABS_CREATE: + return tabs.create({ + url: message.url, + windowId: message.windowId, + }); } }); + +receiveContentMessage((message) => { + switch (message.type) { + case EVENT_KEYPRESS: + case EVENT_KEYDOWN: + case EVENT_KEYUP: + return browser.tabs.sendMessage( + message.tabId, + message + ); + } +}); diff --git a/e2e/ambassador/src/client/keys.js b/e2e/ambassador/src/client/keys.js new file mode 100644 index 0000000..af0fb3d --- /dev/null +++ b/e2e/ambassador/src/client/keys.js @@ -0,0 +1,29 @@ +import { EVENT_KEYPRESS, EVENT_KEYDOWN, EVENT_KEYUP } from '../shared/messages'; +import * as ipc from './ipc'; + +const press = (tabId, key) => { + return ipc.send({ + type: EVENT_KEYPRESS, + tabId, + key, + }); +}; + +const down = (tabId, key) => { + return ipc.send({ + type: EVENT_KEYDOWN, + tabId, + key, + }); +}; + + +const up = (tabId, key) => { + return ipc.send({ + type: EVENT_KEYUP, + tabId, + key, + }); +}; + +export { press, down, up }; diff --git a/e2e/ambassador/src/client/tabs.js b/e2e/ambassador/src/client/tabs.js new file mode 100644 index 0000000..4db3c11 --- /dev/null +++ b/e2e/ambassador/src/client/tabs.js @@ -0,0 +1,12 @@ +import { TABS_CREATE } from '../shared/messages'; +import * as ipc from './ipc'; + +const create = (windowId, url) => { + return ipc.send({ + type: TABS_CREATE, + windowId, + url, + }); +}; + +export { create }; diff --git a/e2e/ambassador/src/client/windows.js b/e2e/ambassador/src/client/windows.js index 69b8905..c6ba37a 100644 --- a/e2e/ambassador/src/client/windows.js +++ b/e2e/ambassador/src/client/windows.js @@ -1,9 +1,10 @@ import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages'; import * as ipc from './ipc'; -const create = () => { +const create = (url) => { return ipc.send({ - type: WINDOWS_CREATE + type: WINDOWS_CREATE, + url, }); }; diff --git a/e2e/ambassador/src/content/index.js b/e2e/ambassador/src/content/index.js index dd46688..0931f93 100644 --- a/e2e/ambassador/src/content/index.js +++ b/e2e/ambassador/src/content/index.js @@ -1,11 +1,35 @@ -import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages'; +import { + WINDOWS_CREATE, WINDOWS_REMOVE, TABS_CREATE, + EVENT_KEYPRESS, EVENT_KEYDOWN, EVENT_KEYUP, +} 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); + case TABS_CREATE: + case EVENT_KEYPRESS: + case EVENT_KEYDOWN: + case EVENT_KEYUP: + return ipc.sendToBackground(message); } }); + +ipc.receiveBackgroundMesssage((message) => { + switch (message.type) { + case EVENT_KEYPRESS: + document.body.dispatchEvent( + new KeyboardEvent('keypress', { 'key': message.key })); + break; + case EVENT_KEYDOWN: + document.body.dispatchEvent( + new KeyboardEvent('keydown', { 'key': message.key })); + break; + case EVENT_KEYUP: + document.body.dispatchEvent( + new KeyboardEvent('keyup', { 'key': message.key })); + break; + } + return Promise.resolve({}); +}); diff --git a/e2e/ambassador/src/content/ipc.js b/e2e/ambassador/src/content/ipc.js index 62fcac1..917623c 100644 --- a/e2e/ambassador/src/content/ipc.js +++ b/e2e/ambassador/src/content/ipc.js @@ -1,9 +1,15 @@ import { METHOD_REQUEST, METHOD_RESPONSE } from '../shared/messages'; -const send = (message) => { +const sendToBackground = (message) => { return browser.runtime.sendMessage(message); }; +const receiveBackgroundMesssage = (func) => { + return browser.runtime.onMessage.addListener((message) => { + return Promise.resolve(func(message)); + }); +}; + const receivePageMessage = (func) => { window.addEventListener('message', (e) => { let packet = e.data; @@ -28,4 +34,7 @@ const receivePageMessage = (func) => { }); }; -export { send, receivePageMessage }; +export { + sendToBackground, receiveBackgroundMesssage, + receivePageMessage, +}; diff --git a/e2e/ambassador/src/shared/messages.js b/e2e/ambassador/src/shared/messages.js index e8a0438..1369260 100644 --- a/e2e/ambassador/src/shared/messages.js +++ b/e2e/ambassador/src/shared/messages.js @@ -2,6 +2,10 @@ const METHOD_REQUEST = 'request'; const METHOD_RESPONSE = 'response'; const WINDOWS_CREATE = 'windows.create'; const WINDOWS_REMOVE = 'windows.remove'; +const TABS_CREATE = 'tabs.create'; +const EVENT_KEYPRESS = 'event.keypress'; +const EVENT_KEYDOWN = 'event.keydown'; +const EVENT_KEYUP = 'event.keyup'; export { METHOD_REQUEST, @@ -9,4 +13,10 @@ export { WINDOWS_CREATE, WINDOWS_REMOVE, + + TABS_CREATE, + + EVENT_KEYPRESS, + EVENT_KEYDOWN, + EVENT_KEYUP, }; diff --git a/e2e/contents/scroll.test.js b/e2e/contents/scroll.test.js deleted file mode 100644 index 6cf0042..0000000 --- a/e2e/contents/scroll.test.js +++ /dev/null @@ -1,19 +0,0 @@ -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/contents/tab.test.js b/e2e/contents/tab.test.js new file mode 100644 index 0000000..d33a35f --- /dev/null +++ b/e2e/contents/tab.test.js @@ -0,0 +1,35 @@ +import { expect } from "chai"; +import * as windows from "../ambassador/src/client/windows"; +import * as tabs from "../ambassador/src/client/tabs"; +import * as keys from "../ambassador/src/client/keys"; + +const SERVER_URL = "localhost:11111"; + +describe("tab test", function() { + let targetWindow; + let targetTab; + + before(() => { + return windows.create().then((win) => { + targetWindow = win; + return tabs.create(win.id, SERVER_URL).then((tab) => { + targetTab = tab; + }); + }); + }); + + after(() => { + return windows.remove(targetWindow.id); + }); + + it('delete tab', (done) => { + let before = window.document.documentElement.scrollTop; + keys.press(targetTab.id, 'j').then((r) => { + }); + keys.press(targetTab.id, 'j').then((r) => { + }); + keys.press(targetTab.id, 'G').then((r) => { + }); + let after = window.document.documentElement.scrollTop; + }); +}); diff --git a/e2e/karma-delay.js b/e2e/karma-delay.js index be7b448..7d18c4a 100644 --- a/e2e/karma-delay.js +++ b/e2e/karma-delay.js @@ -5,6 +5,6 @@ return function(){ var args = arguments setTimeout(() => { start(args) - }, 1000); + }, 3000); }; }(window.__karma__.start)); -- cgit v1.2.3 From 0904f8e1ebd61d425c3536a7dd02afbffaad0550 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 13 Feb 2018 20:13:27 +0900 Subject: add tab delete test --- e2e/ambassador/src/background/index.js | 5 ++++- e2e/ambassador/src/client/windows.js | 13 +++++++++++-- e2e/ambassador/src/content/index.js | 4 +++- e2e/ambassador/src/shared/messages.js | 2 ++ e2e/contents/tab.test.js | 17 ++++++++--------- 5 files changed, 28 insertions(+), 13 deletions(-) (limited to 'e2e/ambassador/src/content') diff --git a/e2e/ambassador/src/background/index.js b/e2e/ambassador/src/background/index.js index 9e64986..f9fda7e 100644 --- a/e2e/ambassador/src/background/index.js +++ b/e2e/ambassador/src/background/index.js @@ -1,5 +1,6 @@ import { - WINDOWS_CREATE, WINDOWS_REMOVE, TABS_CREATE, + WINDOWS_CREATE, WINDOWS_REMOVE, WINDOWS_GET, + TABS_CREATE, EVENT_KEYPRESS, EVENT_KEYDOWN, EVENT_KEYUP, } from '../shared/messages'; import * as tabs from './tabs'; @@ -11,6 +12,8 @@ receiveContentMessage((message) => { return browser.windows.create({ url: message.url }); case WINDOWS_REMOVE: return browser.windows.remove(message.windowId); + case WINDOWS_GET: + return browser.windows.get(message.windowId, { populate: true }); case TABS_CREATE: return tabs.create({ url: message.url, diff --git a/e2e/ambassador/src/client/windows.js b/e2e/ambassador/src/client/windows.js index c6ba37a..f92405a 100644 --- a/e2e/ambassador/src/client/windows.js +++ b/e2e/ambassador/src/client/windows.js @@ -1,4 +1,6 @@ -import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages'; +import { + WINDOWS_CREATE, WINDOWS_REMOVE, WINDOWS_GET +} from '../shared/messages'; import * as ipc from './ipc'; const create = (url) => { @@ -15,4 +17,11 @@ const remove = (windowId) => { }); }; -export { create, remove }; +const get = (windowId) => { + return ipc.send({ + type: WINDOWS_GET, + windowId, + }); +}; + +export { create, remove, get }; diff --git a/e2e/ambassador/src/content/index.js b/e2e/ambassador/src/content/index.js index 0931f93..8573d66 100644 --- a/e2e/ambassador/src/content/index.js +++ b/e2e/ambassador/src/content/index.js @@ -1,5 +1,6 @@ import { - WINDOWS_CREATE, WINDOWS_REMOVE, TABS_CREATE, + WINDOWS_CREATE, WINDOWS_REMOVE, WINDOWS_GET, + TABS_CREATE, EVENT_KEYPRESS, EVENT_KEYDOWN, EVENT_KEYUP, } from '../shared/messages'; import * as ipc from './ipc'; @@ -8,6 +9,7 @@ ipc.receivePageMessage((message) => { switch (message.type) { case WINDOWS_CREATE: case WINDOWS_REMOVE: + case WINDOWS_GET: case TABS_CREATE: case EVENT_KEYPRESS: case EVENT_KEYDOWN: diff --git a/e2e/ambassador/src/shared/messages.js b/e2e/ambassador/src/shared/messages.js index 1369260..32b7aa2 100644 --- a/e2e/ambassador/src/shared/messages.js +++ b/e2e/ambassador/src/shared/messages.js @@ -2,6 +2,7 @@ const METHOD_REQUEST = 'request'; const METHOD_RESPONSE = 'response'; const WINDOWS_CREATE = 'windows.create'; const WINDOWS_REMOVE = 'windows.remove'; +const WINDOWS_GET = 'windows.get'; const TABS_CREATE = 'tabs.create'; const EVENT_KEYPRESS = 'event.keypress'; const EVENT_KEYDOWN = 'event.keydown'; @@ -13,6 +14,7 @@ export { WINDOWS_CREATE, WINDOWS_REMOVE, + WINDOWS_GET, TABS_CREATE, diff --git a/e2e/contents/tab.test.js b/e2e/contents/tab.test.js index d33a35f..dda572f 100644 --- a/e2e/contents/tab.test.js +++ b/e2e/contents/tab.test.js @@ -5,7 +5,7 @@ import * as keys from "../ambassador/src/client/keys"; const SERVER_URL = "localhost:11111"; -describe("tab test", function() { +describe("tab test", () => { let targetWindow; let targetTab; @@ -22,14 +22,13 @@ describe("tab test", function() { return windows.remove(targetWindow.id); }); - it('delete tab', (done) => { - let before = window.document.documentElement.scrollTop; - keys.press(targetTab.id, 'j').then((r) => { + it('delete tab', () => { + return Promise.resolve().then(() => { + return keys.press(targetTab.id, 'd'); + }).then(() => { + return windows.get(targetWindow.id); + }).then((after) => { + expect(after.tabs).to.have.lengthOf(1); }); - keys.press(targetTab.id, 'j').then((r) => { - }); - keys.press(targetTab.id, 'G').then((r) => { - }); - let after = window.document.documentElement.scrollTop; }); }); -- cgit v1.2.3