# Copyright (C) 2009, Tutorius.org # Copyright (C) 2009, Simon Poirier # # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import unittest from sugar.tutorius import addon, properties class AddonTest(unittest.TestCase): def test_create_constructor_fail(self): try: obj = addon.create("BubbleMessage", wrong_param=True, second_wrong="This", last_wrong=12, unknown=13.4) assert False, "Constructor with wrong parameter should raise an exception" except: pass def test_create_wrong_addon(self): try: obj = addon.create("Non existing addon name") assert False, "Addon creator should raise an exception when the requested addon is unknown" except: pass def test_create(self): obj = addon.create("BubbleMessage", message="Hi!", position=[12,31]) assert obj is not None def test_reload_addons(self): addon._cache = None assert len(addon.list_addons()) > 0, "Addons should be reloaded upon cache clear" def test_get_addon_meta(self): addon._cache = None meta = addon.get_addon_meta("BubbleMessage") expected = set(['mandatory_props', 'class', 'display_name', 'name', 'type', 'icon']) assert not set(meta.keys()).difference(expected), "%s == %s"%(meta.keys(), expected) def test_reverse_lookup(self): obj = addon.create("BubbleMessage", message="Hi!", position=[12,31]) assert "BubbleMessage" == addon.get_name_from_type(type(obj)) for name in addon.list_addons(): klass = addon.get_addon_meta(name)['class'] assert name == addon.get_name_from_type(klass),\ "could not reverse lookup from type '%s'"%klass.__name__ def test_addon_constructor(self): for name in addon.list_addons(): obj = addon.create(name) # __init__ can have locals, but should not initialize attributes on # self as non-properties attributes won't survive serialization # through DBUS. Assignation in do() for actions or install_handlers # for event filters is the correct way. attribs = set(obj.__dict__.keys()).difference(obj._props.keys()+['_props', '_callback']) assert not attribs,\ "assignation of attribute(s) %s detected in '%s.__init__'"%(attribs, type(obj).__name__)