1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# -*- Mode: Python; py-indent-offset: 4 -*-
# vim: tabstop=4 shiftwidth=4 expandtab
import unittest
import gi.overrides
from gi.repository import GLib, Gio
class TestGio(unittest.TestCase):
def test_file_enumerator(self):
self.assertEqual(Gio.FileEnumerator, gi.overrides.Gio.FileEnumerator)
f = Gio.file_new_for_path("./")
iter_info = []
for info in f.enumerate_children("standard::*", 0, None):
iter_info.append(info.get_name())
next_info = []
enumerator = f.enumerate_children("standard::*", 0, None)
while True:
info = enumerator.next_file(None)
if info is None:
break
next_info.append(info.get_name())
self.assertEqual(iter_info, next_info)
def test_menu_item(self):
menu = Gio.Menu()
item = Gio.MenuItem()
item.set_attribute([("label", "s", "Test"),
("action", "s", "app.test")])
menu.append_item(item)
value = menu.get_item_attribute_value(0, "label", GLib.VariantType.new("s"))
self.assertEqual("Test", value.unpack())
value = menu.get_item_attribute_value(0, "action", GLib.VariantType.new("s"))
self.assertEqual("app.test", value.unpack())
class TestGSettings(unittest.TestCase):
def setUp(self):
self.settings = Gio.Settings('org.gnome.test')
# we change the values in the tests, so set them to predictable start
# value
self.settings.reset('test-string')
self.settings.reset('test-array')
self.settings.reset('test-boolean')
self.settings.reset('test-enum')
def test_native(self):
self.assertTrue('test-array' in self.settings.list_keys())
# get various types
v = self.settings.get_value('test-boolean')
self.assertEqual(v.get_boolean(), True)
self.assertEqual(self.settings.get_boolean('test-boolean'), True)
v = self.settings.get_value('test-string')
self.assertEqual(v.get_string(), 'Hello')
self.assertEqual(self.settings.get_string('test-string'), 'Hello')
v = self.settings.get_value('test-array')
self.assertEqual(v.unpack(), [1, 2])
v = self.settings.get_value('test-tuple')
self.assertEqual(v.unpack(), (1, 2))
# set a value
self.settings.set_string('test-string', 'World')
self.assertEqual(self.settings.get_string('test-string'), 'World')
self.settings.set_value('test-string', GLib.Variant('s', 'Goodbye'))
self.assertEqual(self.settings.get_string('test-string'), 'Goodbye')
def test_constructor(self):
# default constructor uses path from schema
self.assertEqual(self.settings.get_property('path'), '/tests/')
# optional constructor arguments
with_path = Gio.Settings('org.gnome.nopathtest', path='/mypath/')
self.assertEqual(with_path.get_property('path'), '/mypath/')
self.assertEqual(with_path['np-int'], 42)
def test_dictionary_api(self):
self.assertEqual(len(self.settings), 5)
self.assertTrue('test-array' in self.settings)
self.assertTrue('test-array' in self.settings.keys())
self.assertFalse('nonexisting' in self.settings)
self.assertFalse(4 in self.settings)
self.assertEqual(bool(self.settings), True)
def test_get(self):
self.assertEqual(self.settings['test-boolean'], True)
self.assertEqual(self.settings['test-string'], 'Hello')
self.assertEqual(self.settings['test-enum'], 'banana')
self.assertEqual(self.settings['test-array'], [1, 2])
self.assertEqual(self.settings['test-tuple'], (1, 2))
self.assertRaises(KeyError, self.settings.__getitem__, 'unknown')
self.assertRaises(KeyError, self.settings.__getitem__, 2)
def test_set(self):
self.settings['test-boolean'] = False
self.assertEqual(self.settings['test-boolean'], False)
self.settings['test-string'] = 'Goodbye'
self.assertEqual(self.settings['test-string'], 'Goodbye')
self.settings['test-array'] = [3, 4, 5]
self.assertEqual(self.settings['test-array'], [3, 4, 5])
self.settings['test-enum'] = 'pear'
self.assertEqual(self.settings['test-enum'], 'pear')
self.assertRaises(TypeError, self.settings.__setitem__, 'test-string', 1)
self.assertRaises(ValueError, self.settings.__setitem__, 'test-enum', 'plum')
self.assertRaises(KeyError, self.settings.__setitem__, 'unknown', 'moo')
def test_empty(self):
empty = Gio.Settings('org.gnome.empty', path='/tests/')
self.assertEqual(len(empty), 0)
self.assertEqual(bool(empty), True)
self.assertEqual(empty.keys(), [])
def test_change_event(self):
changed_log = []
change_event_log = []
def on_changed(settings, key):
changed_log.append((settings, key))
def on_change_event(settings, keys, n_keys):
change_event_log.append((settings, keys, n_keys))
self.settings.connect('changed', on_changed)
self.settings.connect('change-event', on_change_event)
self.settings['test-string'] = 'Moo'
self.assertEqual(changed_log, [(self.settings, 'test-string')])
self.assertEqual(change_event_log, [(self.settings,
[GLib.quark_from_static_string('test-string')],
1)])
class TestGFile(unittest.TestCase):
def setUp(self):
self.file, self.io_stream = Gio.File.new_tmp('TestGFile.XXXXXX')
def tearDown(self):
try:
self.file.delete(None)
# test_delete and test_delete_async already remove it
except GLib.GError:
pass
def test_replace_contents(self):
content = b'hello\0world\x7F!'
succ, etag = self.file.replace_contents(content, None, False,
Gio.FileCreateFlags.NONE, None)
new_succ, new_content, new_etag = self.file.load_contents(None)
self.assertTrue(succ)
self.assertTrue(new_succ)
self.assertEqual(etag, new_etag)
self.assertEqual(content, new_content)
# https://bugzilla.gnome.org/show_bug.cgi?id=690525
def disabled_test_replace_contents_async(self):
content = b''.join(bytes(chr(i), 'utf-8') for i in range(128))
def callback(f, result, d):
# Quit so in case of failed assertations loop doesn't keep running.
main_loop.quit()
succ, etag = self.file.replace_contents_finish(result)
new_succ, new_content, new_etag = self.file.load_contents(None)
d['succ'], d['etag'] = self.file.replace_contents_finish(result)
load = self.file.load_contents(None)
d['new_succ'], d['new_content'], d['new_etag'] = load
data = {}
self.file.replace_contents_async(content, None, False,
Gio.FileCreateFlags.NONE, None,
callback, data)
main_loop = GLib.MainLoop()
main_loop.run()
self.assertTrue(data['succ'])
self.assertTrue(data['new_succ'])
self.assertEqual(data['etag'], data['new_etag'])
self.assertEqual(content, data['new_content'])
def test_tmp_exists(self):
# A simple test to check if Gio.File.new_tmp is working correctly.
self.assertTrue(self.file.query_exists(None))
def test_delete(self):
self.file.delete(None)
self.assertFalse(self.file.query_exists(None))
def test_delete_async(self):
def callback(f, result, data):
main_loop.quit()
self.file.delete_async(0, None, callback, None)
main_loop = GLib.MainLoop()
main_loop.run()
self.assertFalse(self.file.query_exists(None))
|