Browse Source

test(e2e): handle case that has no ClipboardItem available

Junyi Du 3 years ago
parent
commit
dc8be8cdfc
3 changed files with 33 additions and 50 deletions
  1. 3 10
      e2e-tests/editor.spec.ts
  2. 9 26
      e2e-tests/logseq_url.spec.ts
  3. 21 14
      src/main/frontend/utils.js

+ 3 - 10
e2e-tests/editor.spec.ts

@@ -150,9 +150,6 @@ test('copy & paste block ref and replace its content', async ({ page, block }) =
     await block.mustFill('Some random text')
     // FIXME: copy instantly will make content disappear
     await page.waitForTimeout(1000)
-    if (!IsWebAPIClipboardSupported){
-        promise_capture = captureConsoleWithPrefix(page, "Copy without `clipboard-write` permission:")
-    }
     if (IsMac) {
         await page.keyboard.press('Meta+c')
     } else {
@@ -160,14 +157,10 @@ test('copy & paste block ref and replace its content', async ({ page, block }) =
     }
 
     await page.press('textarea >> nth=0', 'Enter')
-    if (IsWebAPIClipboardSupported){
-        if (IsMac) {
-            await page.keyboard.press('Meta+v')
-        } else {
-            await page.keyboard.press('Control+v')
-        }
+    if (IsMac) {
+        await page.keyboard.press('Meta+v')
     } else {
-        await block.mustFill(await promise_capture)
+        await page.keyboard.press('Control+v')
     }
     await page.keyboard.press('Enter')
 

+ 9 - 26
e2e-tests/logseq_url.spec.ts

@@ -1,12 +1,11 @@
 import { expect } from '@playwright/test'
 import { test } from './fixtures'
-import { createRandomPage, lastBlock, captureConsoleWithPrefix, IsMac, IsLinux, getIsWebAPIClipboardSupported } from './utils'
+import { createRandomPage, lastBlock, IsMac, IsLinux } from './utils'
 
 test(
   "Logseq URLs (same graph)",
   async ({ page, block }) => {
     let paste_key = IsMac ? 'Meta+v' : 'Control+v'
-    let IsWebAPIClipboardSupported = await getIsWebAPIClipboardSupported(page)
     // create a page with identify block
     let identify_text = "URL redirect target"
     let page_title = await createRandomPage(page)
@@ -14,18 +13,10 @@ test(
 
     // paste current page's URL to another page, then redirect throught the URL
     await page.click('.ui__dropdown-trigger')
-    if (!IsWebAPIClipboardSupported){
-        let promise_capture = captureConsoleWithPrefix(page, "Copy without `clipboard-write` permission:")
-        await page.locator("text=Copy page URL").click()
-        let copied_text = await promise_capture
-        await createRandomPage(page)
-        await block.mustFill(copied_text)
-    } else {
-        await page.locator("text=Copy page URL").click()
-        await createRandomPage(page)
-        await block.mustFill("") // to enter editing mode
-        await page.keyboard.press(paste_key)
-    }
+    await page.locator("text=Copy page URL").click()
+    await createRandomPage(page)
+    await block.mustFill("") // to enter editing mode
+    await page.keyboard.press(paste_key)
     let cursor_locator = page.locator('textarea >> nth=0')
     expect(await cursor_locator.inputValue()).toContain("page=" + page_title)
     await cursor_locator.press("Enter")
@@ -39,18 +30,10 @@ test(
 
     // paste the identify block's URL to another page, then redirect throught the URL
     await page.click('span.bullet >> nth=0', { button: "right" })
-    if (!IsWebAPIClipboardSupported){
-        let promise_capture = captureConsoleWithPrefix(page, "Copy without `clipboard-write` permission:")
-        await page.locator("text=Copy block URL").click()
-        let copied_text = await promise_capture
-        await createRandomPage(page)
-        await block.mustFill(copied_text)
-    } else {
-        await page.locator("text=Copy block URL").click()
-        await createRandomPage(page)
-        await block.mustFill("") // to enter editing mode
-        await page.keyboard.press(paste_key)
-    }
+    await page.locator("text=Copy block URL").click()
+    await createRandomPage(page)
+    await block.mustFill("") // to enter editing mode
+    await page.keyboard.press(paste_key)
     cursor_locator = page.locator('textarea >> nth=0')
     expect(await cursor_locator.inputValue()).toContain("block-id=")
     await cursor_locator.press("Enter")

+ 21 - 14
src/main/frontend/utils.js

@@ -253,26 +253,33 @@ export const writeClipboard = (text, isHtml) => {
     navigator.permissions.query({
         name: "clipboard-write"
     }).then((result) => {
-        if (typeof ClipboardItem === 'undefined' || (result.state != "granted" && result.state != "prompt")){
+        if (result.state != "granted" && result.state != "prompt"){
             console.debug("Copy without `clipboard-write` permission:", text)
             return
         }
-        let blob = new Blob([text], {
+        let promise_written = null
+        if (typeof ClipboardItem !== 'undefined') {
+            let blob = new Blob([text], {
             type: ["text/plain"]
-        });
-        let data = [new ClipboardItem({
-            ["text/plain"]: blob
-        })];
-        if (isHtml) {
-            blob = new Blob([text], {
-                type: ["text/plain", "text/html"]
-            })
-            data = [new ClipboardItem({
-                ["text/plain"]: blob,
-                ["text/html"]: blob
+            });
+            let data = [new ClipboardItem({
+                ["text/plain"]: blob
             })];
+            if (isHtml) {
+                blob = new Blob([text], {
+                    type: ["text/plain", "text/html"]
+                })
+                data = [new ClipboardItem({
+                    ["text/plain"]: blob,
+                    ["text/html"]: blob
+                })];
+            }
+            promise_written = navigator.clipboard.write(data)
+        } else {
+            console.debug("Degraded copy without `ClipboardItem` support:", text)
+            promise_written = navigator.clipboard.writeText(text)
         }
-        navigator.clipboard.write(data).then(() => {
+        promise_written.then(() => {
             /* success */
         }).catch(e => {
             console.log(e, "fail")