From 3f8bbe11d96b751affa9e2bf0a1995008f9e3220 Mon Sep 17 00:00:00 2001
From: Shin'ya Ueoka <ueokande@i-beam.org>
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

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