From 9c8f11676df75fa6d1797f388c334b6a8e44ed88 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Sun, 8 Jan 2012 15:41:57 +0100 Subject: [PATCH 6/6] Add Python test script for GI binding Run this with LD_LIBRARY_PATH=libxklavier/.libs/ tests/test_gi.py to test the locally built GI binding. --- tests/Makefile.am | 1 + tests/test_gi.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 0 deletions(-) create mode 100755 tests/test_gi.py diff --git a/tests/Makefile.am b/tests/Makefile.am index 32e1d71..5c098f7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -8,3 +8,4 @@ AM_CFLAGS=-Wall -I$(top_srcdir) $(X_CFLAGS) $(GLIB_CFLAGS) LDADD=$(top_builddir)/libxklavier/libxklavier.la $(X_LIBS) $(GLIB_LIBS) +EXTRA_DIST=test_gi.py diff --git a/tests/test_gi.py b/tests/test_gi.py new file mode 100755 index 0000000..0602fbd --- /dev/null +++ b/tests/test_gi.py @@ -0,0 +1,103 @@ +#!/usr/bin/python3 +# +# Test GI binding of libxklavier +# Copyright (C) 2011 Martin Pitt +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. + +import sys +import os + +# use source tree typelib +os.environ['GI_TYPELIB_PATH'] = 'libxklavier:' + os.environ.get('GI_TYPELIB_PATH', '') + +from gi.repository import Xkl, Gdk, GdkX11 + +def item_str(s): + '''Convert a zero-terminated byte array to a proper str''' + + i = s.find(b'\x00') + return s[:i].decode() + +display = GdkX11.x11_get_default_xdisplay() + +print('== Engine ==') +engine = Xkl.Engine.get_instance(display) + +print('indicator names:', engine.get_indicators_names()) +print('group names:', engine.get_groups_names()) +print('default layout:', engine.get_groups_names()[engine.get_default_group()]) +print('features: %X' % engine.get_features()) + +# check ConfigItem ctor with data +i = Xkl.ConfigItem() +assert item_str(i.name) == '' +i.set_name('fr') +assert item_str(i.name) == 'fr' + +# load registry +registry = Xkl.ConfigRegistry.get_instance(engine) + +if not registry.load(False): + print('Failed to load registry') + sys.exit(1) + +print('\n== Available Layouts ==') +def layout_iter(registry, item, data): + print('[%s] %s, ' % (item_str(item.name), item_str(item.description)), end='') + +registry.foreach_layout(layout_iter, None) +print() + +print('\n== ConfigRec ==') +rec = Xkl.ConfigRec() +if not rec.get_from_server(engine): + print('Failed to get configuration from server') + sys.exit(1) + +print('Curent configuration:') +print(' Model:', rec.model) +print(' Layouts:', rec.layouts) +print(' Variants:', rec.variants) +print(' Options:', rec.options) + +print('Adding Danish layout...') +rec.set_layouts(rec.layouts + ['dk']) +rec.set_variants(rec.variants + ['']) +if not rec.activate(engine): + print('Failed to activate new configuration') + +print('Curent configuration:') +rec = Xkl.ConfigRec() +if not rec.get_from_server(engine): + print('Failed to get configuration from server') + sys.exit(1) +print(' Model:', rec.model) +print(' Layouts:', rec.layouts) +print(' Variants:', rec.variants) +print(' Options:', rec.options) + +print('Removing Danish layout...') +rec.set_layouts(rec.layouts[:-1]) +rec.set_variants(rec.variants[:-1]) +if not rec.activate(engine): + print('Failed to activate new configuration') + +print('Curent configuration:') +rec = Xkl.ConfigRec() +if not rec.get_from_server(engine): + print('Failed to get configuration from server') + sys.exit(1) +print(' Model:', rec.model) +print(' Layouts:', rec.layouts) +print(' Variants:', rec.variants) +print(' Options:', rec.options) + -- 1.7.7.3