Przeglądaj źródła

Merge pull request #1484 from Ribas160/copy_to_clipboard_unit_tests

CopyToClipboard unit tests
El RIDO 1 rok temu
rodzic
commit
4b31528fed
4 zmienionych plików z 154 dodań i 6 usunięć
  1. 19 0
      js/common.js
  2. 11 5
      js/privatebin.js
  3. 123 0
      js/test/CopyToClipboard.js
  4. 1 1
      lib/Configuration.php

+ 19 - 0
js/common.js

@@ -152,3 +152,22 @@ exports.urlToString = function (url) {
         encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') +
         encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') +
         (url.fragment ? '#' + encodeURI(url.fragment) : '');
         (url.fragment ? '#' + encodeURI(url.fragment) : '');
 };
 };
+
+exports.enableClipboard = function () {
+    navigator.clipboard = (function () {
+        let savedText = "";
+    
+        async function writeText(text) {
+            savedText = text;
+        };
+    
+        async function readText() {
+            return savedText;
+        };
+    
+        return {
+            writeText,
+            readText,
+        };
+    })();
+};

+ 11 - 5
js/privatebin.js

@@ -5454,11 +5454,11 @@ jQuery.PrivateBin = (function($, RawDeflate) {
     const CopyToClipboard = (function () {
     const CopyToClipboard = (function () {
         const me = {};
         const me = {};
 
 
-        let copyButton = $('#prettyMessageCopyBtn'),
-            copyLinkButton = $('#copyLink'),
-            copyIcon = $('#copyIcon'),
-            successIcon = $('#copySuccessIcon'),
-            shortcutHint = $('#copyShortcutHintText'),
+        let copyButton,
+            copyLinkButton,
+            copyIcon,
+            successIcon,
+            shortcutHint,
             url;
             url;
 
 
         /**
         /**
@@ -5613,6 +5613,12 @@ jQuery.PrivateBin = (function($, RawDeflate) {
          * @function
          * @function
          */
          */
         me.init = function() {
         me.init = function() {
+            copyButton = $('#prettyMessageCopyBtn');
+            copyLinkButton = $('#copyLink');
+            copyIcon = $('#copyIcon');
+            successIcon = $('#copySuccessIcon');
+            shortcutHint = $('#copyShortcutHintText');
+
             handleCopyButtonClick();
             handleCopyButtonClick();
             handleCopyLinkButtonClick();
             handleCopyLinkButtonClick();
             handleKeyboardShortcut();
             handleKeyboardShortcut();

+ 123 - 0
js/test/CopyToClipboard.js

@@ -0,0 +1,123 @@
+'use strict';
+const common = require('../common');
+
+describe('CopyToClipboard', function() {
+    this.timeout(30000);
+
+    describe ('Copy paste co clipboard', function () {
+        jsc.property('Copy with button click', common.jscFormats(), 'nestring', async function (format, text) {
+            var clean = jsdom();
+            common.enableClipboard();
+    
+            $('body').html(
+                '<div id="placeholder" class="hidden">+++ no paste text ' +
+                '+++</div><div id="prettymessage" class="hidden">' +
+                '<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
+                '<svg id="copySuccessIcon"></svg></button><pre ' +
+                'id="prettyprint" class="prettyprint linenums:1"></pre>' +
+                '</div><div id="plaintext" class="hidden"></div>'
+            );
+    
+            $.PrivateBin.PasteViewer.init();
+            $.PrivateBin.PasteViewer.setFormat(format);
+            $.PrivateBin.PasteViewer.setText(text);
+            $.PrivateBin.PasteViewer.run();
+    
+            $.PrivateBin.CopyToClipboard.init();
+            
+            $('#prettyMessageCopyBtn').trigger('click');
+
+            const savedToClipboardText = await navigator.clipboard.readText();
+    
+            clean();
+    
+            return text === savedToClipboardText;
+        });
+
+        /**
+         * Unfortunately in JSVerify impossible to check if copy with shortcut when user selected some text on the page
+         * (the copy paste to clipboard should not work in this case) due to lucking window.getSelection() in jsdom.
+         */
+        jsc.property('Copy with keyboard shortcut',  common.jscFormats(), 'nestring', async function (format, text) {
+            var clean = jsdom();
+            common.enableClipboard();
+    
+            $('body').html(
+                '<div id="placeholder">+++ no paste text ' +
+                '+++</div><div id="prettymessage" class="hidden">' +
+                '<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
+                '<svg id="copySuccessIcon"></svg></button><pre ' +
+                'id="prettyprint" class="prettyprint linenums:1"></pre>' +
+                '</div><div id="plaintext" class="hidden"></div>'
+            );
+    
+            $.PrivateBin.PasteViewer.init();
+            $.PrivateBin.PasteViewer.setFormat(format);
+            $.PrivateBin.PasteViewer.setText(text);
+            $.PrivateBin.PasteViewer.run();
+    
+            $.PrivateBin.CopyToClipboard.init();
+
+            $('body').trigger('copy');
+
+            const copiedTextWithoutSelectedText = await navigator.clipboard.readText();
+
+            clean();
+
+            return copiedTextWithoutSelectedText === text;
+        });
+    });
+
+
+    jsc.property('Copy link to clipboard', 'nestring', async function (text) {
+        var clean = jsdom();
+        common.enableClipboard();
+
+        $('body').html('<button id="copyLink"></button>');
+
+        $.PrivateBin.CopyToClipboard.init();
+        $.PrivateBin.CopyToClipboard.setUrl(text);
+
+        $('#copyLink').trigger('click');
+
+        const copiedText = await navigator.clipboard.readText();
+
+        clean();
+
+        return text === copiedText;
+    });
+
+
+    describe('Keyboard shortcut hint', function () {
+        jsc.property('Show hint', 'nestring', function (text) {
+            var clean = jsdom();
+    
+            $('body').html('<small id="copyShortcutHintText"></small>');
+    
+            $.PrivateBin.CopyToClipboard.init();
+            $.PrivateBin.CopyToClipboard.showKeyboardShortcutHint();
+    
+            const keyboardShortcutHint = $('#copyShortcutHintText').text();
+    
+            clean();
+    
+            return keyboardShortcutHint.length > 0;
+        });
+
+        jsc.property('Hide hint', 'nestring', function (text) {
+            var clean = jsdom();
+    
+            $('body').html('<small id="copyShortcutHintText">' + text + '</small>');
+    
+            $.PrivateBin.CopyToClipboard.init();
+            $.PrivateBin.CopyToClipboard.hideKeyboardShortcutHint();
+    
+            const keyboardShortcutHint = $('#copyShortcutHintText').text();
+    
+            clean();
+    
+            return keyboardShortcutHint.length === 0;
+        });
+    });
+
+});

+ 1 - 1
lib/Configuration.php

@@ -108,7 +108,7 @@ class Configuration
             'js/kjua-0.9.0.js'       => 'sha512-CVn7af+vTMBd9RjoS4QM5fpLFEOtBCoB0zPtaqIDC7sF4F8qgUSRFQQpIyEDGsr6yrjbuOLzdf20tkHHmpaqwQ==',
             'js/kjua-0.9.0.js'       => 'sha512-CVn7af+vTMBd9RjoS4QM5fpLFEOtBCoB0zPtaqIDC7sF4F8qgUSRFQQpIyEDGsr6yrjbuOLzdf20tkHHmpaqwQ==',
             'js/legacy.js'           => 'sha512-UxW/TOZKon83n6dk/09GsYKIyeO5LeBHokxyIq+r7KFS5KMBeIB/EM7NrkVYIezwZBaovnyNtY2d9tKFicRlXg==',
             'js/legacy.js'           => 'sha512-UxW/TOZKon83n6dk/09GsYKIyeO5LeBHokxyIq+r7KFS5KMBeIB/EM7NrkVYIezwZBaovnyNtY2d9tKFicRlXg==',
             'js/prettify.js'         => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
             'js/prettify.js'         => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
-            'js/privatebin.js'       => 'sha512-/hDE31uyN+PB84PJ+C7fCFcdbfPy3TeV4slm6eSFrvqNhVMn/FPh/wsjyKLdQGW7fqBdxlGfq5+CDwUfF8fY0w==',
+            'js/privatebin.js'       => 'sha512-POa+8KNXFFwJFsqp7r9APmR5Rc1w2l363y+OScSzLCySrHN7UhOOgt1VH/o8mVddFvvUozj3FZVmdkTxRlrS5g==',
             'js/purify-3.2.3.js'     => 'sha512-m8Wa/I//YoYMiIahBxDDwYfTnycl+i2DwH58nR8ps1o4KWqXzF8k1K4qHDgAz2HSQFNCNNKH/Qcbfu/jLOuhuQ==',
             'js/purify-3.2.3.js'     => 'sha512-m8Wa/I//YoYMiIahBxDDwYfTnycl+i2DwH58nR8ps1o4KWqXzF8k1K4qHDgAz2HSQFNCNNKH/Qcbfu/jLOuhuQ==',
             'js/rawinflate-0.3.js'   => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==',
             'js/rawinflate-0.3.js'   => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==',
             'js/showdown-2.1.0.js'   => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',
             'js/showdown-2.1.0.js'   => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',