Skip to content

Commit

Permalink
Rewrite Clipboard class using pyperclip + add tests
Browse files Browse the repository at this point in the history
See issue t4ngo#15.
- This makes the clipboard functionality work on other platforms.
- The only supported Windows clipboard format is Unicode text.
- The class should be backwards-compatibile assuming Unicode text
  is the only clipboard format used.
  • Loading branch information
drmfinlay committed May 9, 2018
1 parent 9fe1d04 commit 1b8369c
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 145 deletions.
4 changes: 0 additions & 4 deletions dragonfly/os_dependent_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ def __init__(self, *args, **kwargs):
pass


class Clipboard(MockBase):
pass


class HardwareInput(MockBase):
pass

Expand Down
96 changes: 96 additions & 0 deletions dragonfly/test/test_clipboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#
# This file is part of Dragonfly.
# (c) Copyright 2007, 2008 by Christo Butcher
# Licensed under the LGPL.
#
# Dragonfly is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Dragonfly is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Dragonfly. If not, see
# <http://www.gnu.org/licenses/>.
#

import unittest

from dragonfly import Clipboard


class TestClipboard(unittest.TestCase):
def setUp(self):
# Clear the clipboard before each test.
Clipboard.clear_clipboard()

def test_system_text_methods(self):
text = "testing testing"
Clipboard.set_system_text(text)
self.assertEqual(Clipboard.get_system_text(), text)

def test_clear_clipboard(self):
# Put something on the system clipboard.
Clipboard.set_system_text("something")

# Clear it.
Clipboard.clear_clipboard()

# Then test that it has been cleared.
self.assertEqual(Clipboard.get_system_text(), "")

def test_empty(self):
# A new clipboard has no content for the unicode format.
c = Clipboard()
self.assertFalse(c.has_format(Clipboard.format_unicode))

# Neither does a new clipboard have text.
self.assertFalse(c.has_text())

def test_from_system_argument(self):
# Test the optional from_system argument of Clipboard.__init__
text = "something"
Clipboard.set_system_text(text)
c = Clipboard(from_system=True)
self.assertEqual(c.text, text)
self.assertTrue(c.has_format(Clipboard.format_unicode))
self.assertTrue(c.has_text())

def test_copy_from_system(self):
text = "testing"
Clipboard.set_system_text(text)
c = Clipboard()

# Test the method with clear=False (default)
c.copy_from_system(clear=False)
self.assertEqual(c.text, text)
self.assertEqual(Clipboard.get_system_text(), text)

# Test again with clear=True
c = Clipboard()
c.copy_from_system(clear=True)
self.assertEqual(c.text, text)
self.assertEqual(Clipboard.get_system_text(), "")

def test_copy_to_system(self):
text = "testing"
c = Clipboard(text=text)
c.copy_to_system()
self.assertEqual(Clipboard.get_system_text(), text)

def test_set_text(self):
c = Clipboard()
text = "test"
c.set_text(text)
self.assertTrue(c.has_text())
self.assertTrue(c.has_format(Clipboard.format_unicode))
self.assertEqual(c.get_text(), text)
self.assertEqual(c.text, text)


if __name__ == '__main__':
unittest.main()
3 changes: 1 addition & 2 deletions dragonfly/windows/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
# OS agnostic imports
from rectangle import Rectangle, unit
from point import Point
from .clipboard import Clipboard

# Windows-specific
if sys.platform.startswith("win"):
from window import Window
from monitor import Monitor, monitors
from clipboard import Clipboard
else: # Mock imports
from ..os_dependent_mock import Window
from ..os_dependent_mock import Monitor, monitors
from ..os_dependent_mock import Clipboard
Loading

0 comments on commit 1b8369c

Please sign in to comment.