summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGustavo J. A. M. Carneiro <gjc@src.gnome.org>2005-07-09 18:25:32 +0000
committerGustavo J. A. M. Carneiro <gjc@src.gnome.org>2005-07-09 18:25:32 +0000
commit3e63f0b1f0b78e9f40c00d958fa2caba296fa178 (patch)
treece37f22a555568e2e1fe3e0ccd1b905a4ccc6955 /tests
parent8e6905b418362ea56ca121e9c34868ed18c7fb91 (diff)
downloadpygtk-3e63f0b1f0b78e9f40c00d958fa2caba296fa178.tar.gz
Bug 161177: Allow creation of python classes from g_object_new
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/runtests.py2
-rw-r--r--tests/test_properties.py35
-rw-r--r--tests/test_subtype.py33
-rw-r--r--tests/testhelpermodule.c75
-rw-r--r--tests/testmodule.py9
6 files changed, 145 insertions, 11 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0f8ac7b8..25a1a6d8 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -6,7 +6,7 @@ INCLUDES = \
$(GTK_CFLAGS) \
-I$(top_srcdir)/gobject
-EXTRA_DIST = $(tests) common.py runtests.py test-thread.h test-unknown.h
+EXTRA_DIST = $(tests) common.py runtests.py test-thread.h test-unknown.h testmodule.py
noinst_LTLIBRARIES = testhelper.la
linked_LIBS = testhelper.la
diff --git a/tests/runtests.py b/tests/runtests.py
index 3ae71850..42f6a0c7 100644
--- a/tests/runtests.py
+++ b/tests/runtests.py
@@ -21,7 +21,7 @@ else:
common.importModules(buildDir=buildDir,
srcDir=srcDir)
-SKIP_FILES = ['common', 'runtests']
+SKIP_FILES = ['common', 'runtests', 'testmodule']
dir = os.path.split(os.path.abspath(__file__))[0]
os.chdir(dir)
diff --git a/tests/test_properties.py b/tests/test_properties.py
index db620eae..970b6460 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -2,27 +2,38 @@
import unittest
from common import testhelper
-from gobject import GObject, GType, PARAM_READWRITE
+from gobject import *
class PropertyObject(GObject):
__gproperties__ = {
'property': (str, 'blurb', 'description',
'default',
PARAM_READWRITE),
+ 'construct-property': (str, 'blurb', 'description',
+ 'default',
+ PARAM_READWRITE|PARAM_CONSTRUCT_ONLY),
}
def __init__(self):
GObject.__init__(self)
self._value = 'default'
+ self._construct_property = None
def do_get_property(self, pspec):
- if pspec.name != 'property':
- raise AssertionError
- return self._value
+ if pspec.name == 'property':
+ return self._value
+ elif pspec.name == 'construct-property':
+ return self._construct_property
+ raise AssertionError
+
def do_set_property(self, pspec, value):
- if pspec.name != 'property':
+ if pspec.name == 'property':
+ self._value = value
+ elif pspec.name == 'construct-property':
+ self._construct_property = value
+ else:
raise AssertionError
- self._value = value
+
class TestProperties(unittest.TestCase):
def testGetSet(self):
@@ -48,7 +59,13 @@ class TestProperties(unittest.TestCase):
for pspec in obj:
gtype = GType(pspec)
self.assertEqual(gtype.parent.name, 'GParam')
- self.assertEqual(pspec.name, 'property')
+ self.assert_(pspec.name in ['property', 'construct-property'])
+
+ self.assertEqual(len(obj), 2)
- self.assertEqual(len(obj), 1)
- self.assertEqual(list(obj), [pspec])
+ def testConstructProperty(self):
+ obj = new(PropertyObject, construct_property="123")
+ self.assertEqual(obj.props.construct_property, "123")
+ ## TODO: raise exception when setting construct-only properties
+ #obj.set_property("construct-property", "456")
+ #self.assertEqual(obj.props.construct_property, "456")
diff --git a/tests/test_subtype.py b/tests/test_subtype.py
index b2b5dcc5..ea66fab6 100644
--- a/tests/test_subtype.py
+++ b/tests/test_subtype.py
@@ -1,6 +1,7 @@
import unittest
from common import gobject, gtk, testhelper
+import testmodule
class TestSubType(unittest.TestCase):
def testSubType(self):
@@ -20,3 +21,35 @@ class TestSubType(unittest.TestCase):
treemodel = testhelper.get_tp_basicsize(gtk.TreeModel)
self.assert_(iface == treemodel)
+
+ def testBuiltinContructorRefcount(self):
+ foo = gtk.Label()
+ self.assertEqual(foo.__grefcount__, 1)
+
+ def testPyContructorRefcount(self):
+ foo = testmodule.PyLabel()
+ self.assertEqual(foo.__grefcount__, 1)
+
+ def testBuiltinObjNewRefcount(self):
+ foo = gobject.new(gtk.Label)
+ self.assertEqual(foo.__grefcount__, 1)
+
+ def testPyObjNewRefcount(self):
+ foo = gobject.new(testmodule.PyLabel)
+ self.assertEqual(foo.__grefcount__, 1)
+
+ def testPyContructorPropertyChaining(self):
+ foo = testmodule.PyLabel()
+ self.assertEqual(foo.__grefcount__, 1)
+
+ def testPyObjNewPropertyChaining(self):
+ foo = gobject.new(testmodule.PyLabel)
+ self.assertEqual(foo.props.label, "hello")
+
+ def testCPyCSubclassing1(self):
+ obj = testhelper.create_test_type()
+ self.assertEqual(obj.__grefcount__, 1)
+
+ def testCPyCSubclassing1(self):
+ refcount = testhelper.test_g_object_new()
+ self.assertEqual(refcount, 2)
diff --git a/tests/testhelpermodule.c b/tests/testhelpermodule.c
index cb9a9913..962a0dc5 100644
--- a/tests/testhelpermodule.c
+++ b/tests/testhelpermodule.c
@@ -5,6 +5,56 @@
#include "test-unknown.h"
+static GType
+py_label_get_type(void)
+{
+ static GType gtype = 0;
+ if (gtype == 0) {
+ PyObject *module;
+ if ((module = PyImport_ImportModule("testmodule")) != NULL) {
+ PyObject *moddict = PyModule_GetDict(module);
+ PyObject *py_label_type = PyDict_GetItemString(moddict, "PyLabel");
+ if (py_label_type != NULL)
+ gtype = pyg_type_from_object(py_label_type);
+ }
+ }
+ if (gtype == 0)
+ g_warning("could not get PyLabel from testmodule");
+ return gtype;
+}
+
+GType
+test_type_get_type(void)
+{
+ static GType gtype = 0;
+
+ if (gtype == 0)
+ {
+ GTypeQuery q;
+ GTypeInfo type_info = {
+ 0, /* class_size */
+
+ (GBaseInitFunc) NULL,
+ (GBaseFinalizeFunc) NULL,
+
+ (GClassInitFunc) NULL,
+ (GClassFinalizeFunc) NULL,
+ NULL, /* class_data */
+
+ 0, /* instance_size */
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) NULL
+ };
+ g_type_query(py_label_get_type(), &q);
+ type_info.class_size = q.class_size;
+ type_info.instance_size = q.instance_size;
+ gtype = g_type_register_static(py_label_get_type(), "TestType", &type_info, 0);
+ }
+ return gtype;
+}
+
+#define TYPE_TEST (test_type_get_type())
+
static PyObject *
_wrap_get_tp_basicsize (PyObject * self, PyObject * args)
{
@@ -34,10 +84,35 @@ _wrap_get_unknown (PyObject * self)
}
+static PyObject *
+_wrap_create_test_type (PyObject * self)
+{
+ GObject *obj;
+ PyObject *rv;
+ obj = g_object_new(TYPE_TEST, NULL);
+ rv = pygobject_new(obj);
+ g_object_unref(obj);
+ return rv;
+}
+
+static PyObject *
+_wrap_test_g_object_new (PyObject * self)
+{
+ GObject *obj;
+ PyObject *rv;
+
+ obj = g_object_new(py_label_get_type(), NULL);
+ rv = PyInt_FromLong(obj->ref_count); /* should be == 2 at this point */
+ g_object_unref(obj);
+ return rv;
+}
+
static PyMethodDef testhelper_methods[] = {
{ "get_tp_basicsize", _wrap_get_tp_basicsize, METH_VARARGS },
{ "get_test_thread", (PyCFunction)_wrap_get_test_thread, METH_NOARGS },
{ "get_unknown", (PyCFunction)_wrap_get_unknown, METH_NOARGS },
+ { "create_test_type", (PyCFunction)_wrap_create_test_type, METH_NOARGS },
+ { "test_g_object_new", (PyCFunction)_wrap_test_g_object_new, METH_NOARGS },
{ NULL, NULL }
};
diff --git a/tests/testmodule.py b/tests/testmodule.py
new file mode 100644
index 00000000..8761ecfc
--- /dev/null
+++ b/tests/testmodule.py
@@ -0,0 +1,9 @@
+import gobject
+import gtk
+
+class PyLabel(gtk.Label):
+ __gtype_name__ = 'PyLabel'
+
+ def __init__(self):
+ gtk.Label.__init__(self, "hello")
+