From d66690142b643401429bfff84d42acac002e1980 Mon Sep 17 00:00:00 2001 From: mike Date: Thu, 16 Apr 2009 18:54:50 +0000 Subject: LP 348570 Core : Integrating properties to actions; adding tests --- diff --git a/src/sugar/tutorius/actions.py b/src/sugar/tutorius/actions.py index ad91fb4..2d630be 100644 --- a/src/sugar/tutorius/actions.py +++ b/src/sugar/tutorius/actions.py @@ -23,12 +23,13 @@ from dialog import TutoriusDialog import overlayer from sugar.tutorius.editor import WidgetIdentifier from sugar.tutorius.services import ObjectStore - +from sugar.tutorius.properties import * class Action(object): """Base class for Actions""" def __init__(self): object.__init__(self) + self.properties = None def do(self, **kwargs): """ @@ -43,12 +44,17 @@ class Action(object): pass #Should raise NotImplemented? def get_properties(self): - if not hasattr(self, "_props") or self._props is None: - self._props = [] - for i in dir(self.__class__): - if type(getattr(self.__class__,i)) is property: - self._props.append(i) - return self._props + """ + Fills self.property with a dict of TutoriusProperty and return the list + of property names. get_properties has to be called before accessing + self.property + """ + if self.properties is None: + self.properties = {} + for i in dir(self): + if isinstance(getattr(self,i), TutoriusProperty): + self.properties[i] = getattr(self,i) + return self.properties.keys() class OnceWrapper(object): """ @@ -89,36 +95,21 @@ class DialogMessage(Action): @param message A string to display to the user @param pos A list of the form [x, y] """ - def __init__(self, message, pos=[0,0]): + def __init__(self, message, pos=None): super(DialogMessage, self).__init__() - self._message = message - self._position = pos self._dialog = None - - def set_message(self, msg): - self._message = msg - def get_message(self): - return self._message - - message = property(fget=get_message, fset=set_message) - - def set_pos(self, x, y): - self._position = [x, y] - - def get_pos(self): - return self._position - - position = property(fget=get_pos, fset=set_pos) + self.message = TStringProperty(message) + self.position = TArrayProperty(pos or [0, 0], 2, 2) def do(self): """ Show the dialog """ - self._dialog = TutoriusDialog(self._message) + self._dialog = TutoriusDialog(self.message.value) self._dialog.set_button_clicked_cb(self._dialog.close_self) self._dialog.set_modal(False) - self._dialog.move(self.position[0], self.position[1]) + self._dialog.move(self.position.value[0], self.position.value[1]) self._dialog.show() def undo(self): @@ -137,34 +128,20 @@ class BubbleMessage(Action): @param message A string to display to the user @param pos A list of the form [x, y] @param speaker treeish representation of the speaking widget + @param tailpos The position of the tail of the bubble; useful to point to + specific elements of the interface """ def __init__(self, message, pos=[0,0], speaker=None, tailpos=None): Action.__init__(self) - self._message = message - self._position = pos - + self.message = TStringProperty(message) + # Create the position as an array of fixed-size 2 + self.position = TArrayProperty(pos, 2, 2) + # Do the same for the tail position + self.tail_pos = TArrayProperty(tailpos, 2, 2) + self.overlay = None self._bubble = None self._speaker = None - self._tailpos = tailpos - - def set_message(self, msg): - self._message = msg - def get_message(self): - return self._message - message = property(fget=get_message, fset=set_message, doc="Message displayed to the user") - - def set_pos(self, x, y): - self._position = [x, y] - def get_pos(self): - return self.position - position = property(fget=get_pos, fset=set_pos, doc="Position in [x, y] on the screen") - - def set_tail_pos(self, x, y): - self._tailpos = [x, y] - def get_tail_pos(self): - return self._tailpos - tail_pos = property(fget=get_tail_pos, fset=set_tail_pos, doc="Position the tail of the bubble must point to") def do(self): """ @@ -245,53 +222,54 @@ class DisableWidgetAction(Action): self._widget = gtkutils.find_widget(os.activity, self._target) if self._widget: self._widget.set_sensitive(False) - - def undo(self): + + def undo(self): """Action undo""" if self._widget: self._widget.set_sensitive(True) - + + class TypeTextAction(Action): - """ + """ Simulate a user typing text in a widget Work on any widget that implements a insert_text method - + @param widget The treehish representation of the widget @param text the text that is typed - """ + """ def __init__(self, widget, text): Action.__init__(self) - + self._widget = widget self._text = text - + def do(self, **kwargs): - """ - Type the text - """ + """ + Type the text + """ widget = gtkutils.find_widget(ObjectStore().activity, self._widget) if hasattr(widget, "insert_text"): widget.insert_text(self._text, -1) - - def undo(self): - """ - no undo - """ - pass - + + def undo(self): + """ + no undo + """ + pass + class ClickAction(Action): - """ + """ Action that simulate a click on a widget Work on any widget that implements a clicked() method - + @param widget The threehish representation of the widget - """ + """ def __init__(self, widget): Action.__init__(self) self._widget = widget - - def do(self): - """ + + def do(self): + """ click the widget """ widget = gtkutils.find_widget(ObjectStore().activity, self._widget) @@ -303,3 +281,4 @@ class ClickAction(Action): No undo """ pass + diff --git a/src/sugar/tutorius/properties.py b/src/sugar/tutorius/properties.py index 5be7e1c..52993b8 100644 --- a/src/sugar/tutorius/properties.py +++ b/src/sugar/tutorius/properties.py @@ -87,7 +87,7 @@ class TIntProperty(TutoriusProperty): class TFloatProperty(TutoriusProperty): """ - Represents a floting point number. Can have an upper value limit and/or + Represents a floating point number. Can have an upper value limit and/or a lower value limit. """ def __init__(self, value, lower_limit=None, upper_limit=None): diff --git a/src/sugar/tutorius/tests/actiontests.py b/src/sugar/tutorius/tests/actiontests.py index ab9cdba..bce753e 100644 --- a/src/sugar/tutorius/tests/actiontests.py +++ b/src/sugar/tutorius/tests/actiontests.py @@ -27,24 +27,32 @@ import gtk from sugar.tutorius.actions import * from sugar.tutorius.services import ObjectStore +test_props = {"prop_a":8, "prop_b":3, "prop_c":"Hi"} + class PropertyAction(Action): def __init__(self, na): - self._a = na - - def set_a(self, na): - self._a = na - - def get_a(self): - return self._a - - a = property(fget=get_a, fset=set_a) - + Action.__init__(self) + self.prop_a = TIntProperty(test_props["prop_a"]) + self.prop_b = TIntProperty(test_props["prop_b"]) + self.prop_c = TStringProperty(test_props["prop_c"]) + +def has_function(obj, function_name): + """ + Checks whether the object has a function by that name. + """ + if hasattr(obj, function_name) and hasattr(obj.__getattribute__(function_name), "__call__"): + return True + return False + class PropsTest(unittest.TestCase): - def test_get_properties(self): - prop = PropertyAction(8) + act = PropertyAction(8) + + assert act.get_properties() == test_props.keys(), "Action does not contain property 'a'" + + for prop_name in act.get_properties(): + assert act.properties[prop_name].value == test_props[prop_name], "Wrong initial value for property %s : %s"%(prop_name,str(act.properties[prop_name])) - assert prop.get_properties() == ['a'], "Action does not contain property 'a'" class CountAction(Action): """ diff --git a/src/sugar/tutorius/tests/propertiestests.py b/src/sugar/tutorius/tests/propertiestests.py index 52a9a75..4f4caac 100644 --- a/src/sugar/tutorius/tests/propertiestests.py +++ b/src/sugar/tutorius/tests/propertiestests.py @@ -286,6 +286,8 @@ class TBooleanPropertyTest(unittest.TestCase): def test_basic_boolean(self): assert self.prop.value == False, "Could not set initial value via constructor" + assert self.prop.type == "boolean", "Wrong type for TBooleanProperty : %s"%self.prop.type + self.prop.set(True) assert self.prop.value == True, "Could not change the value via set" @@ -313,6 +315,8 @@ class TEnumPropertyTest(unittest.TestCase): def test_basic_enum(self): assert self.prop.value == "hello", "Could not set initial value on property" + assert self.prop.type == "enum", "Wrong type for TEnumProperty : %s"%self.prop.type + self.prop.set(True) assert self.prop.value, "Could not change the value via set" @@ -333,6 +337,8 @@ class TFilePropertyTest(unittest.TestCase): def test_basic_file(self): assert self.prop.value == "propertiestests.py", "Could not set initial value" + assert self.prop.type == "file", "Wrong type for TFileProperty : %s"%self.prop.type + self.prop.set("run-tests.py") assert self.prop.value == "run-tests.py", "Could not change value" diff --git a/src/sugar/tutorius/tests/run-tests.py b/src/sugar/tutorius/tests/run-tests.py index 63a8de1..87edd57 100755 --- a/src/sugar/tutorius/tests/run-tests.py +++ b/src/sugar/tutorius/tests/run-tests.py @@ -40,7 +40,6 @@ if __name__=='__main__': import filterstests import constraintstests import propertiestests - suite = unittest.TestSuite() suite.addTests(unittest.findTestCases(coretests)) suite.addTests(unittest.findTestCases(servicestests)) @@ -52,7 +51,6 @@ if __name__=='__main__': suite.addTests(unittest.findTestCases(filterstests)) suite.addTests(unittest.findTestCases(constraintstests)) suite.addTests(unittest.findTestCases(propertiestests)) - runner = unittest.TextTestRunner() runner.run(suite) @@ -69,5 +67,8 @@ if __name__=='__main__': from linear_creatortests import * from uamtests import * from filterstests import * + from constraintstests import * + from propertiestests import * + from actiontests import * unittest.main() -- cgit v0.9.1