File: gobject.lua

package info (click to toggle)
lua-lgi 0.9.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,388 kB
  • sloc: ansic: 5,082; makefile: 169; sh: 31
file content (347 lines) | stat: -rw-r--r-- 9,899 bytes parent folder | download | duplicates (5)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
--[[--------------------------------------------------------------------------

  LGI testsuite, GObject.Object test suite.

  Copyright (c) 2010, 2011 Pavel Holejsovsky
  Licensed under the MIT license:
  http://www.opensource.org/licenses/mit-license.php

--]]--------------------------------------------------------------------------

local lgi = require 'lgi'
local core = require 'lgi.core'

local check = testsuite.check
local checkv = testsuite.checkv

-- Basic GObject testing
local gobject = testsuite.group.new('gobject')

function gobject.env_base()
   local GObject = lgi.GObject
   local obj = GObject.Object()
   check(type(core.object.env(obj)) == 'table')
   check(core.object.env(obj) == core.object.env(obj))
   check(next(core.object.env(obj)) == nil)
end

function gobject.env_persist()
   local Gtk = lgi.Gtk
   local window = Gtk.Window()
   local label = Gtk.Label()
   local env = core.object.env(label)
   window:_method_add(label)
   label = nil
   collectgarbage()
   label = window:get_child()
   check(env == core.object.env(label))
end

function gobject.object_new()
   local GObject = lgi.GObject
   local o = GObject.Object()
   o = nil
   collectgarbage()
end

function gobject.initunk_new()
   local GObject = lgi.GObject
   local o = GObject.InitiallyUnowned()

   -- Simulate sink by external container
   o:ref_sink()
   o:unref()

   o = nil
   collectgarbage()
end

function gobject.native()
   local GObject = lgi.GObject
   local o = GObject.Object()
   local p = o._native
   check(type(p) == 'userdata')
   check(GObject.Object(p) == o)
end

function gobject.gtype_create()
   local GObject = lgi.GObject
   local Gio = lgi.Gio
   local m = GObject.Object.new(Gio.ThemedIcon, { name = 'icon' })
   check(Gio.ThemedIcon:is_type_of(m))
end

function gobject.subclass_derive1()
   local GObject = lgi.GObject
   local Derived = GObject.Object:derive('LgiTestDerived1')
   local der = Derived()
   check(Derived:is_type_of(der))
   check(not Derived:is_type_of(GObject.Object()))
end

function gobject.subclass_derive2()
   local GObject = lgi.GObject
   local Derived = GObject.Object:derive('LgiTestDerived2')
   local Subderived = Derived:derive('LgiTestSubDerived2')
   local der = Derived()
   check(Derived:is_type_of(der))
   check(not Subderived:is_type_of(der))
   local sub = Subderived()
   check(Subderived:is_type_of(sub))
   check(Derived:is_type_of(sub))
   check(GObject.Object:is_type_of(sub))
end

function gobject.subclass_override1()
   local GObject = lgi.GObject
   local Derived = GObject.Object:derive('LgiTestOverride1')
   local state = 0
   local obj
   function Derived:do_constructed()
      obj = self
      state = state + 1
   end
   function Derived:do_dispose()
      state = state + 2
   end
   check(state == 0)
   local der = Derived()
   check(der == obj)
   check(state == 1)
   der = nil
   obj = nil
   collectgarbage()
   check(state == 3)
end

function gobject.subclass_override2()
   local GObject = lgi.GObject
   local state = 0
   local Derived = GObject.Object:derive('LgiTestOverride2')
   function Derived:do_constructed()
      self.priv.id = 1
      state = state + 1
   end
   function Derived:do_dispose()
      state = state + 2
      GObject.Object.do_dispose(self)
   end
   function Derived:custom_method()
      state = state + 8
      self.priv.id = self.priv.id + 4
   end
   local Subderived = Derived:derive('LgiTestOverrideSub2')
   function Subderived:do_constructed()
      Derived.do_constructed(self)
      self.priv.id = self.priv.id + 2
      state = state + 4
   end
   check(state == 0)
   local sub = Subderived()
   check(state == 5)
   check(sub.priv.id == 3)
   sub:custom_method()
   check(state == 13)
   check(sub.priv.id == 7)
   sub = nil
   collectgarbage()
   check(state == 15)
end

function gobject.subclass_derive3()
   local GObject = lgi.GObject
   local history = {}
   local Derived = GObject.InitiallyUnowned:derive('LgiTestDerived3')
   function Derived:_class_init()
      history[#history + 1] = 'class_init'
      check(self == Derived)
      collectgarbage()
   end
   function Derived:_init()
      history[#history + 1] = 'init'
      collectgarbage()
   end
   function Derived:do_constructed()
      history[#history + 1] = 'constructed'
      Derived._parent.do_constructed(self)
      collectgarbage()
   end
--   function Derived:do_dispose()
--      history[#history + 1] = 'dispose'
--      Derived._parent.do_dispose(self)
--   end
   local obj = Derived()
   check(history[1] == 'class_init')
   check(history[2] == 'init')
   check(history[3] == 'constructed')
   obj = nil
   collectgarbage()
--   check(history[4] == 'dispose')
end

function gobject.iface_virtual()
   local Gio = lgi.Gio
   local file = Gio.File.new_for_path('hey')
   check(file:is_native() == file:do_is_native())
   check(file:get_basename() == file:do_get_basename())
end

function gobject.iface_impl()
   local GObject = lgi.GObject
   local Gio = lgi.Gio
   local FakeFile = GObject.Object:derive('LgiTestFakeFile1', { Gio.File })
   function FakeFile:do_get_basename()
      return self.priv.basename
   end
   function FakeFile:set_basename(basename)
      self.priv.basename = basename
   end
   local fakefile = FakeFile()
   fakefile:set_basename('fakename')
   check(fakefile:get_basename() == 'fakename')
end

function gobject.treemodel_impl()
   local GObject = lgi.GObject
   local Gtk = lgi.Gtk
   local Model = GObject.Object:derive('LgiTestModel1', { Gtk.TreeModel })
   function Model:do_get_n_columns()
      return 2
   end

   function Model:do_get_column_type(index)
      return index == 0 and GObject.Type.STRING or GObject.Type.INT
   end

   function Model:do_get_iter(path)
      local iter = Gtk.TreeIter()
      iter.user_data = path._native
      return iter
   end

   function Model:do_get_value(iter, column)
      return GObject.Value(self:get_column_type(column), 1)
   end

   local model = Model()
   check(model:get_n_columns() == 2)
   check(model:get_column_type(0) == GObject.Type.STRING)
   check(model:get_column_type(1) == GObject.Type.INT)

   local p = Gtk.TreePath.new_from_string('0')
   local i = model:get_iter(p)
   check(i.user_data == p._native)

   check(model[Gtk.TreeIter()][1] == '1')
   check(model[Gtk.TreeIter()][2] == 1)
end

-- Lua 5.3 and 5.1 seems to have some problem with aggressive GC settings used
-- here and completing this test takes literally ages, so skip it.  Enable only if explicitely asked for.
if rawget(_G, '_LGI_ENABLE_ALL_TESTS') then
function gobject.ctor_gc()
   local Gtk = lgi.Gtk

   local oldpause = collectgarbage('setpause', 10)
   local oldstepmul = collectgarbage('setstepmul', 10000)

   for i = 1, 1000 do
      local window = Gtk.Window {
	 width = 800,
	 height = 600,
	 title = "Test",
      }
   end

   collectgarbage('setpause', oldpause)
   collectgarbage('setstepmul', oldstepmul)
end
end

function gobject.subclass_prop1()
   local GObject = lgi.GObject
   local Derived = GObject.Object:derive('LgiTestDerivedProp1')
   Derived._property.string = GObject.ParamSpecString(
      'string', 'Nick string', 'Blurb string', 'string-default',
      { 'READABLE', 'WRITABLE', 'CONSTRUCT' }
   )
   local der = Derived()
   checkv(der.string, 'string-default', 'string')
   der.string = 'assign'
   checkv(der.string, 'assign', 'string')
   checkv(der.priv.string, 'assign', 'string')
end

function gobject.subclass_prop_inherit()
   local GObject = lgi.GObject
   local Gio = lgi.Gio
   local FakeMonitor = GObject.Object:derive(
      'LgiTestFakeMonitor1', { Gio.Initable, Gio.NetworkMonitor })
   FakeMonitor._property.network_available =
      GObject.ParamSpecBoolean('network-available',
			       'LgiTestFakeMonitor1NetworkAvailable',
			       'Whether the network is available.',
			       false, { GObject.ParamFlags.READABLE })
   FakeMonitor._property.connectivity =
      GObject.ParamSpecEnum('connectivity',
			    'LgiTestFakeMonitor1Connectivity',
			    'Type of connectivity.',
			    Gio.NetworkConnectivity,
			    Gio.NetworkConnectivity.LOCAL,
			    { GObject.ParamFlags.READABLE })
   function FakeMonitor:do_init(cancellable)
      self.priv.inited = true
      return true
   end
   local fakemonitor = FakeMonitor()

   -- Gio.Initable is invoked automatically by lgi after object
   -- construction.
   check(fakemonitor.priv.inited)

   -- If the object is not constructed yet, it defaults to false
   check(fakemonitor:get_network_available() == false)
   check(fakemonitor.network_available == false)

   fakemonitor.priv.network_available = true
   check(fakemonitor.network_available == true)
   check(fakemonitor:get_network_available() == true)

   fakemonitor.priv.network_available = false
   check(fakemonitor.network_available == false)
   check(fakemonitor:get_network_available() == false)
end

function gobject.subclass_prop_getset()
   local GObject = lgi.GObject
   local Derived = GObject.Object:derive('LgiTestDerivedPropGetSet')
   Derived._property.str = GObject.ParamSpecString(
      'str', 'Nick string', 'Blurb string', 'string-default',
      { 'READABLE', 'WRITABLE', 'CONSTRUCT' }
   )
   local propval
   function Derived._property_set:str(new_value)
      propval = new_value
   end
   function Derived._property_get:str()
      return propval
   end

   local der = Derived()
   checkv(der.str, 'string-default', 'string')
   der.str = 'assign'
   checkv(der.str, 'assign', 'string')
   checkv(propval, 'assign', 'string')
end

function gobject.signal_query()
   local GObject = lgi.GObject
   local id = GObject.signal_lookup('notify', GObject.Object)
   check(id ~= 0)
   local query = GObject.signal_query(id)
   check(query.signal_id == id)
   check(query.signal_name == 'notify')
   check(query.n_params == 1)
   check(#query.param_types == 1)
   check(query.param_types[1] == GObject.Type.name(GObject.Type.PARAM))
end