forked from cschiller/zhongwen
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
246 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as utils from "./utils"; | ||
|
||
let browser = null; | ||
let worker = null | ||
|
||
beforeEach(async () => { | ||
const setupData = await utils.setupBrowser() | ||
browser = setupData.browser | ||
worker = setupData.worker | ||
}); | ||
|
||
afterEach(async () => { | ||
await browser.close(); | ||
browser = null; | ||
worker = null | ||
}); | ||
|
||
test("extension is disabled by default", async () => { | ||
const page = await browser.newPage(); | ||
await page.goto("https://www.google.com/", { waitUntil: ['domcontentloaded', "networkidle2"] }); | ||
await page.bringToFront(); | ||
|
||
const status = await utils.getExtensionStatus(worker) | ||
expect(status.storage).toEqual({}) | ||
expect(status.badgeData).toEqual({ | ||
"text": "", "color": [0, 0, 0, 0] | ||
}) | ||
}) | ||
|
||
test("toggling the extension on/off two times works", async () => { | ||
const page = await browser.newPage(); | ||
await page.goto("https://www.google.com/", { waitUntil: ['domcontentloaded', "networkidle2"] }); | ||
await page.bringToFront(); | ||
|
||
for (let i = 0; i < 2; i++) { | ||
// On | ||
await utils.toggleExtension(worker) | ||
// Need a slight wait for the extension to trigger | ||
await utils.wait(200) | ||
let status = await utils.getExtensionStatus(worker) | ||
expect(status.storage).toEqual({ "enabled": true }) | ||
expect(status.badgeData).toEqual({ | ||
"text": "On", "color": [255, 0, 0, 255] | ||
}) | ||
|
||
// Off | ||
await utils.toggleExtension(worker) | ||
await utils.wait(200) | ||
status = await utils.getExtensionStatus(worker) | ||
expect(status.storage).toEqual({ "enabled": false }) | ||
expect(status.badgeData).toEqual({ | ||
"text": "", "color": [0, 0, 0, 0] | ||
}) | ||
} | ||
|
||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import path from 'path'; | ||
import * as utils from "./utils"; | ||
|
||
|
||
let browser = null; | ||
let worker = null | ||
|
||
beforeEach(async () => { | ||
const setupData = await utils.setupBrowser() | ||
browser = setupData.browser | ||
worker = setupData.worker | ||
}); | ||
|
||
afterEach(async () => { | ||
await browser.close(); | ||
browser = null; | ||
worker = null | ||
}); | ||
|
||
test("popup appears when hovering over text in plain html", async () => { | ||
const page = await browser.newPage(); | ||
await page.goto(`file://${path.resolve()}/browser_tests/testdata/plain.html`, { waitUntil: ['domcontentloaded', "networkidle2"] }); | ||
await page.bringToFront(); | ||
|
||
await utils.toggleExtension(worker) | ||
await utils.wait(200) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
滚滚长江东逝水,浪花淘尽英雄。是非成败转头空:青山依旧在,几度夕阳红。白发渔樵江渚上,惯看秋月春风。一壶浊酒喜相逢:古今多少事,都付笑谈中。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as puppeteer from "puppeteer"; | ||
|
||
const EXTENSION_PATH = './'; | ||
|
||
async function setupBrowser() { | ||
const browser = await puppeteer.launch({ | ||
headless: false, | ||
args: [ | ||
`--disable-extensions-except=${EXTENSION_PATH}`, | ||
`--load-extension=${EXTENSION_PATH}` | ||
] | ||
}); | ||
|
||
const workerTarget = await browser.waitForTarget( | ||
// Assumes that there is only one service worker created by the extension and its URL ends with background.js. | ||
target => | ||
target.type() === 'service_worker' | ||
); | ||
const worker = await workerTarget.worker() | ||
return { browser, worker } | ||
} | ||
|
||
async function getExtensionStatus(worker) { | ||
const storage = await worker.evaluate(async () => { | ||
return await chrome.storage.local.get(["enabled"]) | ||
}) | ||
const badgeData = await worker.evaluate(async () => { | ||
return { | ||
"text": await chrome.action.getBadgeText({}), | ||
"color": await chrome.action.getBadgeBackgroundColor({}), | ||
} | ||
}) | ||
|
||
return { | ||
storage, | ||
badgeData | ||
} | ||
} | ||
|
||
async function toggleExtension(worker) { | ||
await worker.evaluate(async () => { | ||
const tabs = await chrome.tabs.query({ active: true }) | ||
await chrome.action.onClicked.dispatch(tabs[0]); | ||
}); | ||
} | ||
|
||
async function wait(miliseconds) { | ||
await new Promise((r) => setTimeout(r, miliseconds)); | ||
} | ||
|
||
export { | ||
getExtensionStatus, setupBrowser, toggleExtension, wait | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.