1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
4 from __future__
import absolute_import
13 from gi
import PyGIWarning
14 from gi
.repository
import GLib
, Gio
16 from .helper
import ignore_gi_deprecation_warnings
19 class TestGio(unittest
.TestCase
):
20 def test_file_enumerator(self
):
21 self
.assertEqual(Gio
.FileEnumerator
, gi
.overrides
.Gio
.FileEnumerator
)
22 f
= Gio
.file_new_for_path("./")
25 for info
in f
.enumerate_children("standard::*", 0, None):
26 iter_info
.append(info
.get_name())
29 enumerator
= f
.enumerate_children("standard::*", 0, None)
31 info
= enumerator
.next_file(None)
34 next_info
.append(info
.get_name())
36 self
.assertEqual(iter_info
, next_info
)
38 def test_menu_item(self
):
41 item
.set_attribute([("label", "s", "Test"),
42 ("action", "s", "app.test")])
43 menu
.append_item(item
)
44 value
= menu
.get_item_attribute_value(0, "label", GLib
.VariantType
.new("s"))
45 self
.assertEqual("Test", value
.unpack())
46 value
= menu
.get_item_attribute_value(0, "action", GLib
.VariantType
.new("s"))
47 self
.assertEqual("app.test", value
.unpack())
49 def test_volume_monitor_warning(self
):
50 with warnings
.catch_warnings(record
=True) as warn
:
51 warnings
.simplefilter('always')
53 self
.assertEqual(len(warn
), 1)
54 self
.assertTrue(issubclass(warn
[0].category
, PyGIWarning
))
55 self
.assertRegexpMatches(str(warn
[0].message
),
56 '.*Gio\\.VolumeMonitor\\.get\\(\\).*')
59 class TestGSettings(unittest
.TestCase
):
61 self
.settings
= Gio
.Settings
.new('org.gnome.test')
62 # we change the values in the tests, so set them to predictable start
64 self
.settings
.reset('test-string')
65 self
.settings
.reset('test-array')
66 self
.settings
.reset('test-boolean')
67 self
.settings
.reset('test-enum')
70 assert list(self
.settings
) == [
71 'test-tuple', 'test-array', 'test-boolean', 'test-string',
72 'test-enum', 'test-range']
74 def test_get_set(self
):
75 for key
in self
.settings
:
76 old_value
= self
.settings
[key
]
77 self
.settings
[key
] = old_value
78 assert self
.settings
[key
] == old_value
80 def test_native(self
):
81 self
.assertTrue('test-array' in self
.settings
.list_keys())
84 v
= self
.settings
.get_value('test-boolean')
85 self
.assertEqual(v
.get_boolean(), True)
86 self
.assertEqual(self
.settings
.get_boolean('test-boolean'), True)
88 v
= self
.settings
.get_value('test-string')
89 self
.assertEqual(v
.get_string(), 'Hello')
90 self
.assertEqual(self
.settings
.get_string('test-string'), 'Hello')
92 v
= self
.settings
.get_value('test-array')
93 self
.assertEqual(v
.unpack(), [1, 2])
95 v
= self
.settings
.get_value('test-tuple')
96 self
.assertEqual(v
.unpack(), (1, 2))
98 v
= self
.settings
.get_value('test-range')
99 assert v
.unpack() == 123
102 self
.settings
.set_string('test-string', 'World')
103 self
.assertEqual(self
.settings
.get_string('test-string'), 'World')
105 self
.settings
.set_value('test-string', GLib
.Variant('s', 'Goodbye'))
106 self
.assertEqual(self
.settings
.get_string('test-string'), 'Goodbye')
108 def test_constructor(self
):
109 # default constructor uses path from schema
110 self
.assertEqual(self
.settings
.get_property('path'), '/tests/')
112 # optional constructor arguments
113 with_path
= Gio
.Settings
.new_with_path('org.gnome.nopathtest', '/mypath/')
114 self
.assertEqual(with_path
.get_property('path'), '/mypath/')
115 self
.assertEqual(with_path
['np-int'], 42)
117 def test_dictionary_api(self
):
118 self
.assertEqual(len(self
.settings
), 6)
119 self
.assertTrue('test-array' in self
.settings
)
120 self
.assertTrue('test-array' in self
.settings
.keys())
121 self
.assertFalse('nonexisting' in self
.settings
)
122 self
.assertFalse(4 in self
.settings
)
123 self
.assertEqual(bool(self
.settings
), True)
126 self
.assertEqual(self
.settings
['test-boolean'], True)
127 self
.assertEqual(self
.settings
['test-string'], 'Hello')
128 self
.assertEqual(self
.settings
['test-enum'], 'banana')
129 self
.assertEqual(self
.settings
['test-array'], [1, 2])
130 self
.assertEqual(self
.settings
['test-tuple'], (1, 2))
132 self
.assertRaises(KeyError, self
.settings
.__getitem
__, 'unknown')
133 self
.assertRaises(KeyError, self
.settings
.__getitem
__, 2)
136 self
.settings
['test-boolean'] = False
137 self
.assertEqual(self
.settings
['test-boolean'], False)
138 self
.settings
['test-string'] = 'Goodbye'
139 self
.assertEqual(self
.settings
['test-string'], 'Goodbye')
140 self
.settings
['test-array'] = [3, 4, 5]
141 self
.assertEqual(self
.settings
['test-array'], [3, 4, 5])
142 self
.settings
['test-enum'] = 'pear'
143 self
.assertEqual(self
.settings
['test-enum'], 'pear')
145 self
.assertRaises(TypeError, self
.settings
.__setitem
__, 'test-string', 1)
146 self
.assertRaises(ValueError, self
.settings
.__setitem
__, 'test-enum', 'plum')
147 self
.assertRaises(KeyError, self
.settings
.__setitem
__, 'unknown', 'moo')
149 def test_set_range(self
):
150 self
.settings
['test-range'] = 7
151 assert self
.settings
['test-range'] == 7
152 self
.settings
['test-range'] = 65535
153 assert self
.settings
['test-range'] == 65535
155 with pytest
.raises(ValueError, match
=".*7 - 65535.*"):
156 self
.settings
['test-range'] = 7 - 1
158 with pytest
.raises(ValueError, match
=".*7 - 65535.*"):
159 self
.settings
['test-range'] = 65535 + 1
161 def test_empty(self
):
162 empty
= Gio
.Settings
.new_with_path('org.gnome.empty', '/tests/')
163 self
.assertEqual(len(empty
), 0)
164 self
.assertEqual(bool(empty
), True)
165 self
.assertEqual(empty
.keys(), [])
167 @ignore_gi_deprecation_warnings
168 def test_change_event(self
):
170 change_event_log
= []
172 def on_changed(settings
, key
):
173 changed_log
.append((settings
, key
))
175 def on_change_event(settings
, keys
, n_keys
):
176 change_event_log
.append((settings
, keys
, n_keys
))
178 self
.settings
.connect('changed', on_changed
)
179 self
.settings
.connect('change-event', on_change_event
)
180 self
.settings
['test-string'] = 'Moo'
181 self
.assertEqual(changed_log
, [(self
.settings
, 'test-string')])
182 self
.assertEqual(change_event_log
, [(self
.settings
,
183 [GLib
.quark_from_static_string('test-string')],
187 @unittest.skipIf(os
.name
== "nt", "FIXME")
188 class TestGFile(unittest
.TestCase
):
190 self
.file, self
.io_stream
= Gio
.File
.new_tmp('TestGFile.XXXXXX')
194 self
.file.delete(None)
195 # test_delete and test_delete_async already remove it
199 def test_replace_contents(self
):
200 content
= b
'hello\0world\x7F!'
201 succ
, etag
= self
.file.replace_contents(content
, None, False,
202 Gio
.FileCreateFlags
.NONE
, None)
203 new_succ
, new_content
, new_etag
= self
.file.load_contents(None)
205 self
.assertTrue(succ
)
206 self
.assertTrue(new_succ
)
207 self
.assertEqual(etag
, new_etag
)
208 self
.assertEqual(content
, new_content
)
210 # https://bugzilla.gnome.org/show_bug.cgi?id=690525
211 def disabled_test_replace_contents_async(self
):
212 content
= b
''.join(bytes(chr(i
), 'utf-8') for i
in range(128))
214 def callback(f
, result
, d
):
215 # Quit so in case of failed assertations loop doesn't keep running.
217 succ
, etag
= self
.file.replace_contents_finish(result
)
218 new_succ
, new_content
, new_etag
= self
.file.load_contents(None)
219 d
['succ'], d
['etag'] = self
.file.replace_contents_finish(result
)
220 load
= self
.file.load_contents(None)
221 d
['new_succ'], d
['new_content'], d
['new_etag'] = load
224 self
.file.replace_contents_async(content
, None, False,
225 Gio
.FileCreateFlags
.NONE
, None,
227 main_loop
= GLib
.MainLoop()
229 self
.assertTrue(data
['succ'])
230 self
.assertTrue(data
['new_succ'])
231 self
.assertEqual(data
['etag'], data
['new_etag'])
232 self
.assertEqual(content
, data
['new_content'])
234 def test_tmp_exists(self
):
235 # A simple test to check if Gio.File.new_tmp is working correctly.
236 self
.assertTrue(self
.file.query_exists(None))
238 def test_delete(self
):
239 self
.file.delete(None)
240 self
.assertFalse(self
.file.query_exists(None))
242 def test_delete_async(self
):
243 def callback(f
, result
, data
):
246 self
.file.delete_async(0, None, callback
, None)
247 main_loop
= GLib
.MainLoop()
249 self
.assertFalse(self
.file.query_exists(None))
252 @unittest.skipIf(os
.name
== "nt", "crashes on Windows")
253 class TestGApplication(unittest
.TestCase
):
254 def test_command_line(self
):
255 class App(Gio
.Application
):
259 super(App
, self
).__init
__(flags
=Gio
.ApplicationFlags
.HANDLES_COMMAND_LINE
)
261 def do_command_line(self
, cmdline
):
262 self
.args
= cmdline
.get_arguments()
266 res
= app
.run(['spam', 'eggs'])
268 self
.assertEqual(res
, 42)
269 self
.assertSequenceEqual(app
.args
, ['spam', 'eggs'])
271 def test_local_command_line(self
):
272 class App(Gio
.Application
):
276 super(App
, self
).__init
__(flags
=Gio
.ApplicationFlags
.HANDLES_COMMAND_LINE
)
278 def do_local_command_line(self
, args
):
279 self
.local_args
= args
[:] # copy
282 # True skips do_command_line being called.
283 return True, args
, 42
286 res
= app
.run(['spam', 'eggs'])
288 self
.assertEqual(res
, 42)
289 self
.assertSequenceEqual(app
.local_args
, ['spam', 'eggs'])
291 def test_local_and_remote_command_line(self
):
292 class App(Gio
.Application
):
297 super(App
, self
).__init
__(flags
=Gio
.ApplicationFlags
.HANDLES_COMMAND_LINE
)
299 def do_command_line(self
, cmdline
):
300 self
.args
= cmdline
.get_arguments()
303 def do_local_command_line(self
, args
):
304 self
.local_args
= args
[:] # copy
307 # False causes do_command_line to be called with args.
308 return False, args
, 0
311 res
= app
.run(['spam', 'eggs'])
313 self
.assertEqual(res
, 42)
314 self
.assertSequenceEqual(app
.args
, ['spam'])
315 self
.assertSequenceEqual(app
.local_args
, ['spam', 'eggs'])
317 @unittest.skipUnless(hasattr(Gio
.Application
, 'add_main_option'),
318 'Requires newer version of GLib')
319 def test_add_main_option(self
):
322 def on_handle_local_options(app
, options
):
323 stored_options
.append(options
)
324 return 0 # Return 0 if options have been handled
326 def on_activate(app
):
329 app
= Gio
.Application()
330 app
.add_main_option(long_name
='string',
333 arg
=GLib
.OptionArg
.STRING
,
334 description
='some string')
336 app
.connect('activate', on_activate
)
337 app
.connect('handle-local-options', on_handle_local_options
)
338 app
.run(['app', '-s', 'test string'])
340 self
.assertEqual(len(stored_options
), 1)
341 options
= stored_options
[0]
342 self
.assertTrue(options
.contains('string'))
343 self
.assertEqual(options
.lookup_value('string').unpack(),