tests: don't test for specific device labels
[pygobject.git] / tests / test_overrides_gio.py
blobb6516f9b03a11c7104b2749933e54f90c3a32a30
1 from __future__ import absolute_import
3 import random
4 import platform
6 import pytest
8 from gi.repository import Gio, GObject
9 from gi._compat import cmp
12 class Item(GObject.Object):
13 _id = 0
15 def __init__(self, **kwargs):
16 super(Item, self).__init__(**kwargs)
17 Item._id += 1
18 self._id = self._id
20 def __repr__(self):
21 return str(self._id)
24 class NamedItem(Item):
26 name = GObject.Property(type=str, default='')
28 def __repr__(self):
29 return self.props.name
32 def test_list_store_sort():
33 store = Gio.ListStore()
34 items = [NamedItem(name=n) for n in "cabx"]
35 sorted_items = sorted(items, key=lambda i: i.props.name)
37 user_data = [object(), object()]
39 def sort_func(a, b, *args):
40 assert list(args) == user_data
41 assert isinstance(a, NamedItem)
42 assert isinstance(b, NamedItem)
43 return cmp(a.props.name, b.props.name)
45 store[:] = items
46 assert store[:] != sorted_items
47 store.sort(sort_func, *user_data)
48 assert store[:] == sorted_items
51 def test_list_store_insert_sorted():
52 store = Gio.ListStore()
53 items = [NamedItem(name=n) for n in "cabx"]
54 sorted_items = sorted(items, key=lambda i: i.props.name)
56 user_data = [object(), object()]
58 def sort_func(a, b, *args):
59 assert list(args) == user_data
60 assert isinstance(a, NamedItem)
61 assert isinstance(b, NamedItem)
62 return cmp(a.props.name, b.props.name)
64 for item in items:
65 index = store.insert_sorted(item, sort_func, *user_data)
66 assert isinstance(index, int)
67 assert store[:] == sorted_items
70 def test_list_model_len():
71 model = Gio.ListStore.new(Item)
72 assert len(model) == 0
73 assert not model
74 for i in range(1, 10):
75 model.append(Item())
76 assert len(model) == i
77 assert model
78 model.remove_all()
79 assert not model
80 assert len(model) == 0
83 def test_list_model_get_item_simple():
84 model = Gio.ListStore.new(Item)
85 with pytest.raises(IndexError):
86 model[0]
87 first_item = Item()
88 model.append(first_item)
89 assert model[0] is first_item
90 assert model[-1] is first_item
91 second_item = Item()
92 model.append(second_item)
93 assert model[1] is second_item
94 assert model[-1] is second_item
95 assert model[-2] is first_item
96 with pytest.raises(IndexError):
97 model[-3]
99 with pytest.raises(TypeError):
100 model[object()]
103 def test_list_model_get_item_slice():
104 model = Gio.ListStore.new(Item)
105 source = [Item() for i in range(30)]
106 for i in source:
107 model.append(i)
108 assert model[1:10] == source[1:10]
109 assert model[1:-2] == source[1:-2]
110 assert model[-4:-1] == source[-4:-1]
111 assert model[-100:-1] == source[-100:-1]
112 assert model[::-1] == source[::-1]
113 assert model[:] == source[:]
116 def test_list_model_contains():
117 model = Gio.ListStore.new(Item)
118 item = Item()
119 model.append(item)
120 assert item in model
121 assert Item() not in model
122 with pytest.raises(TypeError):
123 object() in model
124 with pytest.raises(TypeError):
125 None in model
128 def test_list_model_iter():
129 model = Gio.ListStore.new(Item)
130 item = Item()
131 model.append(item)
133 it = iter(model)
134 assert next(it) is item
135 repr(item)
138 def test_list_store_delitem_simple():
139 store = Gio.ListStore.new(Item)
140 store.append(Item())
141 del store[0]
142 assert not store
143 with pytest.raises(IndexError):
144 del store[0]
145 with pytest.raises(IndexError):
146 del store[-1]
148 store.append(Item())
149 with pytest.raises(IndexError):
150 del store[-2]
151 del store[-1]
152 assert not store
154 source = [Item(), Item()]
155 store.append(source[0])
156 store.append(source[1])
157 del store[-1]
158 assert store[:] == [source[0]]
160 with pytest.raises(TypeError):
161 del store[object()]
164 def test_list_store_delitem_slice():
166 def do_del(count, key):
168 events = []
170 def on_changed(m, *args):
171 events.append(args)
173 store = Gio.ListStore.new(Item)
174 source = [Item() for i in range(count)]
175 for item in source:
176 store.append(item)
177 store.connect("items-changed", on_changed)
178 source.__delitem__(key)
179 store.__delitem__(key)
180 assert source == store[:]
181 return events
183 values = [None, 1, -15, 3, -2, 0, -3, 5, 7]
184 variants = set()
185 for i in range(500):
186 start = random.choice(values)
187 stop = random.choice(values)
188 step = random.choice(values)
189 length = abs(random.choice(values) or 0)
190 if step == 0:
191 step += 1
192 variants.add((length, start, stop, step))
194 for length, start, stop, step in variants:
195 do_del(length, slice(start, stop, step))
197 # basics
198 do_del(10, slice(None, None, None))
199 do_del(10, slice(None, None, None))
200 do_del(10, slice(None, None, -1))
201 do_del(10, slice(0, 5, None))
202 do_del(10, slice(0, 10, 1))
203 do_del(10, slice(0, 10, 2))
204 do_del(10, slice(14, 2, -1))
206 # test some fast paths
207 assert do_del(100, slice(None, None, None)) == [(0, 100, 0)]
208 assert do_del(100, slice(None, None, -1)) == [(0, 100, 0)]
209 assert do_del(100, slice(0, 50, 1)) == [(0, 50, 0)]
212 def test_list_store_setitem_simple():
214 store = Gio.ListStore.new(Item)
215 first = Item()
216 store.append(first)
218 class Wrong(GObject.Object):
219 pass
221 with pytest.raises(TypeError):
222 store[0] = object()
223 with pytest.raises(TypeError):
224 store[0] = None
225 with pytest.raises(TypeError):
226 store[0] = Wrong()
228 assert store[:] == [first]
230 new = Item()
231 store[0] = new
232 assert len(store) == 1
233 store[-1] = Item()
234 assert len(store) == 1
236 with pytest.raises(IndexError):
237 store[1] = Item()
238 with pytest.raises(IndexError):
239 store[-2] = Item()
241 store = Gio.ListStore.new(Item)
242 source = [Item(), Item(), Item()]
243 for item in source:
244 store.append(item)
245 new = Item()
246 store[1] = new
247 assert store[:] == [source[0], new, source[2]]
249 with pytest.raises(TypeError):
250 store[object()] = Item()
253 def test_list_store_setitem_slice():
255 def do_set(count, key, new_count):
256 if count == 0 and key.step is not None \
257 and platform.python_implementation() == "PyPy":
258 # https://bitbucket.org/pypy/pypy/issues/2804
259 return
260 store = Gio.ListStore.new(Item)
261 source = [Item() for i in range(count)]
262 new = [Item() for i in range(new_count)]
263 for item in source:
264 store.append(item)
265 source_error = None
266 try:
267 source.__setitem__(key, new)
268 except ValueError as e:
269 source_error = type(e)
271 store_error = None
272 try:
273 store.__setitem__(key, new)
274 except Exception as e:
275 store_error = type(e)
277 assert source_error == store_error
278 assert source == store[:]
280 values = [None, 1, -15, 3, -2, 0, 3, 4, 100]
281 variants = set()
282 for i in range(500):
283 start = random.choice(values)
284 stop = random.choice(values)
285 step = random.choice(values)
286 length = abs(random.choice(values) or 0)
287 new = random.choice(values) or 0
288 if step == 0:
289 step += 1
290 variants.add((length, start, stop, step, new))
292 for length, start, stop, step, new in variants:
293 do_set(length, slice(start, stop, step), new)
295 # basics
296 do_set(10, slice(None, None, None), 20)
297 do_set(10, slice(None, None, None), 0)
298 do_set(10, slice(None, None, -1), 20)
299 do_set(10, slice(None, None, -1), 10)
300 do_set(10, slice(0, 5, None), 20)
301 do_set(10, slice(0, 10, 1), 0)
303 # test iterators
304 store = Gio.ListStore.new(Item)
305 store[:] = iter([Item() for i in range(10)])
306 assert len(store) == 10
308 # make sure we do all or nothing
309 store = Gio.ListStore.new(Item)
310 with pytest.raises(TypeError):
311 store[:] = [Item(), object()]
312 assert len(store) == 0
315 def test_action_map_add_action_entries():
316 actionmap = Gio.SimpleActionGroup()
318 test_data = []
320 def f(action, parameter, data):
321 test_data.append('test back')
323 actionmap.add_action_entries((
324 ("simple", f),
325 ("with_type", f, "i"),
326 ("with_state", f, "s", "'left'", f),
328 assert actionmap.has_action("simple")
329 assert actionmap.has_action("with_type")
330 assert actionmap.has_action("with_state")
331 actionmap.add_action_entries((
332 ("with_user_data", f),
333 ), "user_data")
334 assert actionmap.has_action("with_user_data")
336 with pytest.raises(TypeError):
337 actionmap.add_action_entries((
338 ("invaild_type_string", f, 'asdf'),
340 with pytest.raises(ValueError):
341 actionmap.add_action_entries((
342 ("stateless_with_change_state", f, None, None, f),
345 actionmap.activate_action("simple")
346 assert test_data[0] == 'test back'