| 1 |
#!/usr/bin/env python3 |
| 2 |
# coding: utf-8 |
| 3 |
from xdg.DesktopEntry import DesktopEntry |
| 4 |
from xdg.Exceptions import ValidationError, ParsingError, NoKeyError |
| 5 |
from xdg.util import u |
| 6 |
|
| 7 |
import resources |
| 8 |
|
| 9 |
import io |
| 10 |
import os |
| 11 |
import shutil |
| 12 |
import re |
| 13 |
import tempfile |
| 14 |
import unittest |
| 15 |
|
| 16 |
class DesktopEntryTest(unittest.TestCase): |
| 17 |
def setUp(self): |
| 18 |
self.tmpdir = tempfile.mkdtemp() |
| 19 |
self.test_file = os.path.join(self.tmpdir, "gedit.desktop") |
| 20 |
with io.open(self.test_file, "w", encoding='utf-8') as f: |
| 21 |
f.write(resources.gedit_desktop) |
| 22 |
|
| 23 |
def tearDown(self): |
| 24 |
shutil.rmtree(self.tmpdir) |
| 25 |
|
| 26 |
def test_write_file(self): |
| 27 |
de = DesktopEntry() |
| 28 |
de.parse(self.test_file) |
| 29 |
de.removeKey("Name") |
| 30 |
de.addGroup("Hallo") |
| 31 |
de.set("key", "value", "Hallo") |
| 32 |
|
| 33 |
new_file = os.path.join(self.tmpdir, "test.desktop") |
| 34 |
de.write(new_file, trusted=True) |
| 35 |
|
| 36 |
with io.open(new_file, encoding='utf-8') as f: |
| 37 |
contents = f.read() |
| 38 |
|
| 39 |
assert "[Hallo]" in contents, contents |
| 40 |
assert re.search(r"key\s*=\s*value", contents), contents |
| 41 |
|
| 42 |
# This is missing the Name key, and has an unknown Hallo group, so it |
| 43 |
# shouldn't validate. |
| 44 |
new_entry = DesktopEntry(new_file) |
| 45 |
self.assertRaises(ValidationError, new_entry.validate) |
| 46 |
|
| 47 |
def test_validate(self): |
| 48 |
entry = DesktopEntry(self.test_file) |
| 49 |
entry.validate() |
| 50 |
|
| 51 |
def test_values(self): |
| 52 |
entry = DesktopEntry(self.test_file) |
| 53 |
self.assertEqual(entry.getName(), 'gedit') |
| 54 |
self.assertEqual(entry.getGenericName(), 'Text Editor') |
| 55 |
self.assertEqual(entry.getNoDisplay(), False) |
| 56 |
self.assertEqual(entry.getComment(), 'Edit text files') |
| 57 |
self.assertEqual(entry.getIcon(), 'accessories-text-editor') |
| 58 |
self.assertEqual(entry.getHidden(), False) |
| 59 |
self.assertEqual(entry.getOnlyShowIn(), []) |
| 60 |
self.assertEqual(entry.getExec(), 'gedit %U') |
| 61 |
self.assertEqual(entry.getTerminal(), False) |
| 62 |
self.assertEqual(entry.getMimeTypes(), ['text/plain']) |
| 63 |
self.assertEqual(entry.getCategories(), ['GNOME', 'GTK', 'Utility', 'TextEditor']) |
| 64 |
self.assertEqual(entry.getTerminal(), False) |
| 65 |
|
| 66 |
def test_basic(self): |
| 67 |
entry = DesktopEntry(self.test_file) |
| 68 |
assert entry.hasKey("Categories") |
| 69 |
assert not entry.hasKey("TryExec") |
| 70 |
|
| 71 |
assert entry.hasGroup("Desktop Action Window") |
| 72 |
assert not entry.hasGroup("Desktop Action Door") |
| 73 |
|
| 74 |
def test_unicode_name(self): |
| 75 |
with io.open(self.test_file, "w", encoding='utf-8') as f: |
| 76 |
f.write(resources.unicode_desktop) |
| 77 |
|
| 78 |
entry = DesktopEntry(self.test_file) |
| 79 |
self.assertEqual(entry.getName(), u('Abc€þ')) |
| 80 |
|
| 81 |
def test_invalid(self): |
| 82 |
test_file = os.path.join(self.tmpdir, "spout.desktop") |
| 83 |
with io.open(test_file, "w", encoding='utf-8') as f: |
| 84 |
f.write(resources.spout_desktop) |
| 85 |
|
| 86 |
self.assertRaises(ParsingError, DesktopEntry, test_file) |
| 87 |
|
| 88 |
def test_invalid_unicode(self): |
| 89 |
test_file = os.path.join(self.tmpdir, "gnome-alsamixer.desktop") |
| 90 |
with io.open(test_file, "w", encoding='latin-1') as f: |
| 91 |
f.write(resources.gnome_alsamixer_desktop) |
| 92 |
|
| 93 |
# Just check this doesn't throw a UnicodeError. |
| 94 |
DesktopEntry(test_file) |
| 95 |
|
| 96 |
class TestTryExec(unittest.TestCase): |
| 97 |
def setUp(self): |
| 98 |
self.tmpdir = tempfile.mkdtemp() |
| 99 |
self.test_file = os.path.join(self.tmpdir, "foo.desktop") |
| 100 |
|
| 101 |
def test_present(self): |
| 102 |
with io.open(self.test_file, "w", encoding='utf-8') as f: |
| 103 |
f.write(resources.python_desktop) |
| 104 |
|
| 105 |
entry = DesktopEntry(self.test_file) |
| 106 |
res = entry.findTryExec() |
| 107 |
assert res, repr(res) |
| 108 |
|
| 109 |
def test_absent(self): |
| 110 |
with io.open(self.test_file, "w", encoding='utf-8') as f: |
| 111 |
f.write(resources.unicode_desktop) |
| 112 |
|
| 113 |
entry = DesktopEntry(self.test_file) |
| 114 |
res = entry.findTryExec() |
| 115 |
assert res is None, repr(res) |
| 116 |
|
| 117 |
def test_no_TryExec(self): |
| 118 |
with io.open(self.test_file, "w", encoding='utf-8') as f: |
| 119 |
f.write(resources.gedit_desktop) |
| 120 |
|
| 121 |
entry = DesktopEntry(self.test_file) |
| 122 |
self.assertRaises(NoKeyError, entry.findTryExec) |