| 1 |
import os |
| 2 |
import unittest |
| 3 |
|
| 4 |
try: |
| 5 |
from gi.repository import Gtk, Gdk |
| 6 |
except ImportError: |
| 7 |
Gdk = None |
| 8 |
Gtk = None |
| 9 |
|
| 10 |
from .helper import capture_glib_deprecation_warnings |
| 11 |
|
| 12 |
|
| 13 |
def is_X11(): |
| 14 |
try: |
| 15 |
from gi.repository import Gdk, GdkX11 |
| 16 |
except ImportError: |
| 17 |
return False |
| 18 |
|
| 19 |
display = Gdk.Display.get_default() |
| 20 |
return isinstance(display, GdkX11.X11Display) |
| 21 |
|
| 22 |
|
| 23 |
@unittest.skipUnless(Gdk, "Gdk not available") |
| 24 |
@unittest.skipIf(Gdk and Gdk._version == "4.0", "Gdk4 doesn't have GdkAtom") |
| 25 |
class TestGdkAtom(unittest.TestCase): |
| 26 |
def test_create(self): |
| 27 |
atom = Gdk.Atom.intern("my_string", False) |
| 28 |
self.assertEqual(atom.name(), "my_string") |
| 29 |
|
| 30 |
def test_str(self): |
| 31 |
atom = Gdk.Atom.intern("my_string", False) |
| 32 |
self.assertEqual(str(atom), "my_string") |
| 33 |
|
| 34 |
self.assertEqual(str(Gdk.SELECTION_CLIPBOARD), "CLIPBOARD") |
| 35 |
|
| 36 |
def test_repr(self): |
| 37 |
# __repr__ should generate a string which is parsable when possible |
| 38 |
# http://docs.python.org/2/reference/datamodel.html#object.__repr__ |
| 39 |
atom = Gdk.Atom.intern("my_string", False) |
| 40 |
self.assertEqual(repr(atom), 'Gdk.Atom.intern("my_string", False)') |
| 41 |
self.assertEqual(eval(repr(atom)), atom) |
| 42 |
|
| 43 |
self.assertEqual( |
| 44 |
repr(Gdk.SELECTION_CLIPBOARD), 'Gdk.Atom.intern("CLIPBOARD", False)' |
| 45 |
) |
| 46 |
|
| 47 |
@unittest.skipUnless(os.name != "nt", "not on Windows") |
| 48 |
def test_in_single(self): |
| 49 |
a_selection = Gdk.Atom.intern("test_clipboard", False) |
| 50 |
clipboard = Gtk.Clipboard.get(a_selection) |
| 51 |
clipboard.set_text("hello", 5) |
| 52 |
|
| 53 |
# needs a Gdk.Atom, not a string |
| 54 |
self.assertRaises(TypeError, Gtk.Clipboard.get, "CLIPBOARD") |
| 55 |
|
| 56 |
def test_in_array(self): |
| 57 |
a_plain = Gdk.Atom.intern("text/plain", False) |
| 58 |
a_html = Gdk.Atom.intern("text/html", False) |
| 59 |
a_jpeg = Gdk.Atom.intern("image/jpeg", False) |
| 60 |
|
| 61 |
self.assertFalse(Gtk.targets_include_text([])) |
| 62 |
self.assertTrue(Gtk.targets_include_text([a_plain, a_html])) |
| 63 |
self.assertFalse(Gtk.targets_include_text([a_jpeg])) |
| 64 |
self.assertTrue(Gtk.targets_include_text([a_jpeg, a_plain])) |
| 65 |
|
| 66 |
self.assertFalse(Gtk.targets_include_image([], False)) |
| 67 |
self.assertFalse(Gtk.targets_include_image([a_plain, a_html], False)) |
| 68 |
self.assertTrue(Gtk.targets_include_image([a_jpeg], False)) |
| 69 |
self.assertTrue(Gtk.targets_include_image([a_jpeg, a_plain], False)) |
| 70 |
|
| 71 |
@unittest.skipUnless(is_X11(), "only on X11") |
| 72 |
def test_out_array(self): |
| 73 |
a_selection = Gdk.Atom.intern("my_clipboard", False) |
| 74 |
clipboard = Gtk.Clipboard.get(a_selection) |
| 75 |
|
| 76 |
# empty |
| 77 |
(res, targets) = clipboard.wait_for_targets() |
| 78 |
self.assertEqual(res, False) |
| 79 |
self.assertEqual(targets, []) |
| 80 |
|
| 81 |
# text |
| 82 |
clipboard.set_text("hello", 5) |
| 83 |
(res, targets) = clipboard.wait_for_targets() |
| 84 |
self.assertEqual(res, True) |
| 85 |
self.assertNotEqual(targets, []) |
| 86 |
self.assertEqual(type(targets[0]), Gdk.Atom) |
| 87 |
names = [t.name() for t in targets] |
| 88 |
self.assertFalse(None in names, names) |
| 89 |
self.assertTrue("TEXT" in names, names) |
| 90 |
|
| 91 |
@unittest.skipUnless(is_X11(), "only on X11") |
| 92 |
@unittest.skipIf(not Gdk or Gdk._version == "4.0", "not in gdk4") |
| 93 |
def test_out_glist(self): |
| 94 |
display = Gdk.Display.get_default() |
| 95 |
with capture_glib_deprecation_warnings(): |
| 96 |
dm = display.get_device_manager() |
| 97 |
device = dm.get_client_pointer() |
| 98 |
axes = device.list_axes() |
| 99 |
axes_names = [atom.name() for atom in axes if atom is not None] |
| 100 |
assert all(isinstance(name, str) for name in axes_names) |