From fb422aef7ee6832c85c8fa8a703e491838e74d62 Mon Sep 17 00:00:00 2001 From: Vincent Vinet Date: Fri, 04 Dec 2009 05:06:49 +0000 Subject: Add Event Sources: - Add source property in Action and EventFilter - Change TPropContainer contructor to accept keyword arguments and set properties that were given - Change every single TPropContainer subclass constructor to accept kwargs and pass them on to super init - Add a "null" option for TStringProperty Use Event Sources: - Make the probe require a source property to install or subscribe - Have ProbeProxy install and subscribe return a prefixed address - Make update, uninstall and unsubsribe extract the prefix from the address - Have the TutorialRunner set a source on actions/events before installing/subscribing instead of setting current activity on ProbeManager Test Event Sources: - Change the tests according to the new constructors and behaviors --- (limited to 'tests') diff --git a/tests/actiontests.py b/tests/actiontests.py index 7b8d1cb..80a10f8 100644 --- a/tests/actiontests.py +++ b/tests/actiontests.py @@ -50,14 +50,14 @@ class PropsTest(unittest.TestCase): def test_get_properties(self): act = PropertyAction(8) - assert act.get_properties() == test_props.keys(), "Action does not contain property 'a'" + assert set(act.get_properties()).issuperset(test_props.keys()), "Action properties should contain at least those we specified" - for prop_name in act.get_properties(): + for prop_name in test_props.keys(): assert getattr(act, prop_name) == test_props[prop_name], "Wrong initial value for property %s : %s"%(prop_name,str(getattr(act, prop_name))) class DialogMessageTest(unittest.TestCase): def setUp(self): - self.dial = addon.create('DialogMessage', "Message text", [200, 300]) + self.dial = addon.create('DialogMessage', message="Message text", position=[200, 300]) def test_properties(self): assert self.dial.message == "Message text", "Wrong start value for the message" @@ -116,7 +116,7 @@ class OnceWrapperTests(unittest.TestCase): CountAction """ act = CountAction() - wrap = addon.create('OnceWrapper', act) + wrap = addon.create('OnceWrapper', action=act) assert act.do_count == 0, "do() should not have been called in __init__()" assert act.undo_count == 0, "undo() should not have been called in __init__()" @@ -162,7 +162,7 @@ class ChainActionTest(unittest.TestCase): first = ChainTester(witness) second = ChainTester(witness) - c = addon.create('ChainAction', [first, second]) + c = addon.create('ChainAction', actions=[first, second]) assert witness == [], "Actions should not be triggered on init""" c.do() @@ -195,7 +195,7 @@ class DisableWidgetActionTests(unittest.TestCase): assert btn.props.sensitive is True, "Callback should have been called" - act = addon.create('DisableWidgetAction', "0") + act = addon.create('DisableWidgetAction', target="0") assert btn.props.sensitive is True, "Callback should have been called again" act.do() assert btn.props.sensitive is False, "Callback should not have been called again" @@ -285,7 +285,7 @@ class TypeTextActionTests(unittest.TestCase): test_text = "This is text" - action = addon.create('TypeTextAction', "0.0", test_text) + action = addon.create('TypeTextAction', widgetUAM="0.0", text=test_text) assert widget == ObjectStore().activity.get_children()[0],\ "The clickable widget isn't reachable from the object store \ @@ -309,7 +309,7 @@ class TypeTextActionTests(unittest.TestCase): test_text = "This is text" - action = addon.create('TypeTextAction', "0.0", test_text) + action = addon.create('TypeTextAction', widgetUAM="0.0", text=test_text) assert widget == ObjectStore().activity.get_children()[0],\ "The clickable widget isn't reachable from the object store \ @@ -330,7 +330,7 @@ class ClickActionTests(unittest.TestCase): activity.add_child(widget) ObjectStore().activity = activity - action = addon.create('ClickAction', "0.0") + action = addon.create('ClickAction', widget="0.0") assert widget == ObjectStore().activity.get_children()[0],\ "The clickable widget isn't reachable from the object store \ @@ -350,7 +350,7 @@ class ClickActionTests(unittest.TestCase): activity.add_child(widget) ObjectStore().activity = activity - action = addon.create('ClickAction', "0.0") + action = addon.create('ClickAction', widget="0.0") assert widget == ObjectStore().activity.get_children()[0],\ "The clickable widget isn't reachable from the object store \ diff --git a/tests/coretests.py b/tests/coretests.py index b9e04e5..1cc5431 100644 --- a/tests/coretests.py +++ b/tests/coretests.py @@ -242,7 +242,7 @@ class StateTest(unittest.TestCase): act1 = addon.create("BubbleMessage", message="Hi", position=[132,450]) act2 = addon.create("BubbleMessage", message="Hi", position=[132,450]) - event1 = addon.create("GtkWidgetEventFilter", "0.0.0.1.1.2.3.1", "clicked") + event1 = addon.create("GtkWidgetEventFilter", object_id="0.0.0.1.1.2.3.1", event_name="clicked") act3 = addon.create("DialogMessage", message="Hello again.", position=[200, 400]) @@ -276,19 +276,19 @@ class StateTest(unittest.TestCase): st2.name = "Identical" st3 = deepcopy(st1) - st3.add_action(addon.create("BubbleMessage", "Hi!", [128,264])) + st3.add_action(addon.create("BubbleMessage", message="Hi!", position=[128,264])) assert not (st1 == st3), "States having a different number of actions should be different" st4 = deepcopy(st1) - st4.add_event_filter(addon.create("GtkWidgetEventFilter", "0.0.1.1.2.2.3", "clicked"), "next_state") + st4.add_event_filter(addon.create("GtkWidgetEventFilter", object_id="0.0.1.1.2.2.3", event_name="clicked"), "next_state") assert not (st1 == st4), "States having a different number of events should be different" st5 = deepcopy(st1) st5._event_filters = [] - st5.add_event_filter(addon.create("GtkWidgetEventFilter", "0.1.2.3.4.1.2", "pressed"), "other_state") + st5.add_event_filter(addon.create("GtkWidgetEventFilter", object_id="0.1.2.3.4.1.2", event_name="pressed"), "other_state") assert not (st1 == st5), "States having the same number of event filters" \ + " but those being different should be different" @@ -502,7 +502,7 @@ class FSMTest(unittest.TestCase): st2 = State("Other State") st3 = State("Final State") - st1.add_action(addon.create("BubbleMessage", "Hi!", [132,312])) + st1.add_action(addon.create("BubbleMessage", message="Hi!", position=[132,312])) fsm.add_state(st1) fsm.add_state(st2) @@ -534,7 +534,7 @@ class FSMTest(unittest.TestCase): fsm3 = FiniteStateMachine("Identity test") - act3 = addon.create("BubbleMessage", "Hi!", [123,312]) + act3 = addon.create("BubbleMessage", message="Hi!", position=[123,312]) fsm3.add_action(act3) assert not(fsm3 == fsm), \ diff --git a/tests/filterstests.py b/tests/filterstests.py index ee6033b..5358605 100644 --- a/tests/filterstests.py +++ b/tests/filterstests.py @@ -69,7 +69,7 @@ class TestTimerEvent(unittest.TestCase): ctx = gobject.MainContext() main = gobject.MainLoop(ctx) - e = addon.create('TimerEvent', 2) # 2 seconds should be enough :s + e = addon.create('TimerEvent', timeout=2) # 2 seconds should be enough :s s = SignalCatcher() e.install_handlers(s.callback) @@ -112,7 +112,7 @@ class TestTimerEvent(unittest.TestCase): ctx = gobject.MainContext() main = gobject.MainLoop(ctx) - e = addon.create('TimerEvent', 2) # 2 seconds should be enough :s + e = addon.create('TimerEvent', timeout=2) # 2 seconds should be enough :s s = SignalCatcher() e.install_handlers(s.callback) @@ -159,7 +159,7 @@ class TestGtkWidgetEventFilter(unittest.TestCase): self.top.add(self.btn1) def test_install(self): - h = addon.create('GtkWidgetEventFilter', "0","whatever") + h = addon.create('GtkWidgetEventFilter', object_id="0", event_name="whatever") try: h.install_handlers(None) @@ -168,7 +168,7 @@ class TestGtkWidgetEventFilter(unittest.TestCase): assert True, "Install should have failed" def test_button_clicks(self): - h = addon.create('GtkWidgetEventFilter', "0.0","clicked") + h = addon.create('GtkWidgetEventFilter', object_id="0.0", event_name="clicked") s = SignalCatcher() h.install_handlers(s.callback, activity=self.top) diff --git a/tests/probetests.py b/tests/probetests.py index 357d223..f073135 100644 --- a/tests/probetests.py +++ b/tests/probetests.py @@ -71,7 +71,6 @@ class MockActivity(object): def get_id(self): return "unique_id_1" - class MockProbeProxy(object): _MockProxyCache = {} @@ -191,6 +190,8 @@ class ProbeTest(unittest.TestCase): self.activity.get_id(), service_proxy=MockServiceProxy()) #Override the eventOccured on the Probe... + # Stores events in a global "event box" + #WARNING: Depends on the implementation of TProbe!! self.old_eO = self.probe.eventOccured def newEo(event): global event_box @@ -343,51 +344,78 @@ class ProbeManagerTest(unittest.TestCase): assert len(self.probeManager.get_registered_probes_list("act1")) == 0 assert self.probeManager.get_registered_probes_list("act1") == [] - def test_actions(self): + def test_action_uninstall(self): self.probeManager.register_probe("act1", "unique_id_1") self.probeManager.register_probe("act2", "unique_id_2") act1 = self.probeManager.get_registered_probes_list("act1")[0][1] act2 = self.probeManager.get_registered_probes_list("act2")[0][1] - ad1 = MockAddon() - ad1_address = "Address1" - def callback(value): - pass - def error_cb(): - pass - #ErrorCase: install, update, uninstall without currentActivity - #Action functions should do a warning if there is no activity - self.assertRaises(RuntimeWarning, self.probeManager.install, ad1_address, ad1, callback) - self.assertRaises(RuntimeWarning, self.probeManager.update, ad1_address, ad1) - self.assertRaises(RuntimeWarning, self.probeManager.uninstall, ad1_address) - assert act1.MockAction is None, "Action should not be installed on inactive proxy" - assert act2.MockAction is None, "Action should not be installed on inactive proxy" - - self.probeManager.currentActivity = "act1" - self.probeManager.install(ad1, callback, error_cb) - assert act1.MockAction == ad1, "Action should have been installed" - assert act2.MockAction is None, "Action should not be installed on inactive proxy" + ad1 = MockAddon(source="act1") - self.probeManager.update(ad1_address, ad1) - assert act1.MockActionUpdate == ad1, "Action should have been updated" - assert act2.MockActionUpdate is None, "Should not update on inactive" + #ErrorCase: update should fail without a source + self.assertRaises(RuntimeWarning, self.probeManager.update, "SomeAddress", ad1) + #ErrorCase: update should fail if the source does not exist + self.assertRaises(RuntimeWarning, self.probeManager.update, "unique:SomeAddress", ad1) - self.probeManager.currentActivity = "act2" - self.probeManager.uninstall(ad1_address) - assert act1.MockActionAddress == ad1_address, "Action should still be installed" + self.probeManager.install(ad1, None, None) + + assert act1.MockAction == ad1, "Action should have been installed" + + self.probeManager.uninstall("unique_id_2:SomeAddress") + assert act1.MockAction == ad1, "Action should still be installed" - self.probeManager.currentActivity = "act1" - self.probeManager.uninstall(ad1_address) + self.probeManager.uninstall("unique_id_1:SomeAddress") assert act1.MockAction is None, "Action should be uninstalled" - def test_events(self): + def test_action_install(self): self.probeManager.register_probe("act1", "unique_id_1") self.probeManager.register_probe("act2", "unique_id_2") act1 = self.probeManager.get_registered_probes_list("act1")[0][1] act2 = self.probeManager.get_registered_probes_list("act2")[0][1] + ad1 = MockAddon(source="act1") + ad2 = MockAddon(source="act2") + ad3 = MockAddon(source="act3") + ad4 = MockAddon() + + #ErrorCase: install should fail without a source + self.assertRaises(RuntimeWarning, self.probeManager.install, ad4, None, None) + #ErrorCase: install should fail if the source does not exist + self.assertRaises(RuntimeWarning, self.probeManager.install, ad3, None, None) + + #install should put the action in the right probes + self.probeManager.install(ad1, None, None) + assert act1.MockAction is ad1, "Action should be on act1" + assert act2.MockAction is not ad1, "Action should not be on act2" + + self.probeManager.install(ad2, None, None) + assert act1.MockAction is not ad2, "Action should not be on act1" + assert act2.MockAction is ad2, "Action should be on act2" + + def test_action_update(self): + self.probeManager.register_probe("act1", "unique_id_1") + self.probeManager.register_probe("act2", "unique_id_2") + act1 = self.probeManager.get_registered_probes_list("act1")[0][1] + act2 = self.probeManager.get_registered_probes_list("act2")[0][1] ad1 = MockAddon() - ad2 = MockAddon() + + #ErrorCase: update should fail without a source + self.assertRaises(RuntimeWarning, self.probeManager.update, "SomeAddress", ad1) + #ErrorCase: update should fail if the source does not exist + self.assertRaises(RuntimeWarning, self.probeManager.update, "unique:SomeAddress", ad1) + + #update should update the right probe + self.probeManager.update("unique_id_1:Address", ad1) + assert act1.MockActionUpdate is ad1, "Action should be on act1" + assert act2.MockActionUpdate is not ad1, "Action should not be on act1" + + def test_event_unsubscribe(self): + self.probeManager.register_probe("act1", "unique_id_1") + self.probeManager.register_probe("act2", "unique_id_2") + act1 = self.probeManager.get_registered_probes_list("act1")[0][1] + + ad1 = MockAddon(source="act1") + ad2 = MockAddon(source="act2") ad2.i, ad2.s = (2, "test2") cb1 = lambda *args: None @@ -395,22 +423,43 @@ class ProbeManagerTest(unittest.TestCase): error_cb1 = lambda *args:None cb2 = lambda *args: None - #ErrorCase: unsubscribe and subscribe without current activity - #Event functions should do a warning if there is no activity - self.assertRaises(RuntimeWarning, self.probeManager.subscribe, ad1, cb1, install_cb1, error_cb1) - self.assertRaises(RuntimeWarning, self.probeManager.unsubscribe, None) - assert act1.MockEvent is None, "No event should be on act1" - assert act2.MockEvent is None, "No event should be on act2" + #ErrorCase: unsubscribe should fail without a source + self.assertRaises(RuntimeWarning, self.probeManager.unsubscribe, "SomeAddress") + #ErrorCase: unsubscribe should fail silently if the source does not exist + self.probeManager.unsubscribe("unique:SomeAddress") + assert act1.MockEventAddr is None, "No unsubcribe should have been called" - self.probeManager.currentActivity = "act1" self.probeManager.subscribe(ad1, cb1, install_cb1, error_cb1) assert act1.MockEvent == ad1, "Event should have been installed" assert act1.MockCB == cb1, "Callback should have been set" - assert act2.MockEvent is None, "No event should be on act2" - self.probeManager.unsubscribe("SomeAddress") + self.probeManager.unsubscribe("unique_id_1:SomeAddress") assert act1.MockEventAddr == "SomeAddress", "Unsubscribe should have been called" - assert act2.MockEventAddr is None, "Unsubscribe should not have been called" + + def test_event_subscribe(self): + self.probeManager.register_probe("act1", "unique_id_1") + self.probeManager.register_probe("act2", "unique_id_2") + act1 = self.probeManager.get_registered_probes_list("act1")[0][1] + act2 = self.probeManager.get_registered_probes_list("act2")[0][1] + + ad1 = MockAddon(source="act1") + ad2 = MockAddon(source="act2") + ad3 = MockAddon(source="act3") + ad4 = MockAddon() + + #ErrorCase: subscribe should fail without a source + self.assertRaises(RuntimeWarning, self.probeManager.subscribe, ad4, None, None, None) + #ErrorCase: subscribe should fail if the source does not exist + self.assertRaises(RuntimeWarning, self.probeManager.subscribe, ad3, None, None, None) + #subscribe should put the action in the right probes + self.probeManager.subscribe(ad1, None, None, None) + assert act1.MockEvent is ad1, "Action should be on act1" + assert act2.MockEvent is not ad1, "Action should not be on act2" + + self.probeManager.subscribe(ad2, None, None, None) + assert act1.MockEvent is not ad2, "Action should not be on act1" + assert act2.MockEvent is ad2, "Action should be on act2" + class ProbeProxyTest(unittest.TestCase): def setUp(self): diff --git a/tests/translatortests.py b/tests/translatortests.py index 3b5ca6f..05a7831 100644 --- a/tests/translatortests.py +++ b/tests/translatortests.py @@ -61,7 +61,7 @@ class ResourceTranslatorTests(unittest.TestCase): self.fsm = Tutorial("TestTutorial1") # Add a few states act1 = addon.create('BubbleMessage', message="Hi", position=[300, 450]) - ev1 = addon.create('GtkWidgetEventFilter', "0.12.31.2.2", "clicked") + ev1 = addon.create('GtkWidgetEventFilter', object_id="0.12.31.2.2", event_name="clicked") act2 = addon.create('BubbleMessage', message="Second message", position=[250, 150], tail_pos=[1,2]) self.fsm.add_action("INIT", act1) st2 = self.fsm.add_state((act2,)) diff --git a/tests/vaulttests.py b/tests/vaulttests.py index 729d36d..1e39d8c 100644 --- a/tests/vaulttests.py +++ b/tests/vaulttests.py @@ -106,7 +106,7 @@ class VaultInterfaceTest(unittest.TestCase): self.fsm = Tutorial("TestTutorial1") # Add a few states act1 = addon.create('BubbleMessage', message="Hi", position=[300, 450]) - ev1 = addon.create('GtkWidgetEventFilter', "0.12.31.2.2", "clicked") + ev1 = addon.create('GtkWidgetEventFilter', object_id="0.12.31.2.2", event_name="clicked") act2 = addon.create('BubbleMessage', message="Second message", position=[250, 150], tail_pos=[1,2]) self.fsm.add_action("INIT", act1) st2 = self.fsm.add_state((act2,)) @@ -487,7 +487,7 @@ class XMLSerializerTest(unittest.TestCase): # Add a few states act1 = addon.create('BubbleMessage', message="Hi", position=[300, 450]) - ev1 = addon.create('GtkWidgetEventFilter', "0.12.31.2.2", "clicked") + ev1 = addon.create('GtkWidgetEventFilter', object_id="0.12.31.2.2", event_name="clicked") act2 = addon.create('BubbleMessage', message="Second message", position=[250, 150], tail_pos=[1,2]) self.fsm.add_action("INIT",act1) @@ -535,12 +535,12 @@ class XMLSerializerTest(unittest.TestCase): """ fsm = Tutorial("TestActions") tuto_file = cStringIO.StringIO() - act1 = addon.create('BubbleMessage', "Hi!", position=[10,120], tail_pos=[-12,30]) - act2 = addon.create('DialogMessage', "Hello again.", position=[120,10]) + act1 = addon.create('BubbleMessage', message="Hi!", position=[10,120], tail_pos=[-12,30]) + act2 = addon.create('DialogMessage', message="Hello again.", position=[120,10]) act3 = addon.create('WidgetIdentifyAction') - act4 = addon.create('DisableWidgetAction', "0.0.0.1.0.0.0") - act5 = addon.create('TypeTextAction', "0.0.0.1.1.1.0.0", "New text") - act6 = addon.create('ClickAction', "0.0.1.0.1.1") + act4 = addon.create('DisableWidgetAction', target="0.0.0.1.0.0.0") + act5 = addon.create('TypeTextAction', widget="0.0.0.1.1.1.0.0", text="New text") + act6 = addon.create('ClickAction', widget="0.0.1.0.1.1") act7 = addon.create('OnceWrapper', action=act1) act8 = addon.create('ChainAction', actions=[act1, act2, act3, act4]) actions = [act1, act2, act3, act4, act5, act6, act7, act8] @@ -565,10 +565,10 @@ class XMLSerializerTest(unittest.TestCase): fsm = Tutorial("TestFilters") tuto_file = cStringIO.StringIO() - ev1 = addon.create('TimerEvent', 1000) + ev1 = addon.create('TimerEvent', timeout=1000) ev2 = addon.create('GtkWidgetEventFilter', object_id="0.0.1.1.0.0.1", event_name="clicked") - ev3 = addon.create('GtkWidgetTypeFilter', "0.0.1.1.1.2.3", text="Typed stuff") - ev4 = addon.create('GtkWidgetTypeFilter', "0.0.1.1.1.2.3", strokes="acbd") + ev3 = addon.create('GtkWidgetTypeFilter', object_id="0.0.1.1.1.2.3", text="Typed stuff") + ev4 = addon.create('GtkWidgetTypeFilter', object_id="0.0.1.1.1.2.3", strokes="acbd") filters = [ev1, ev2, ev3, ev4] for efilter in filters: -- cgit v0.9.1