From 00dff13ebaa55b71b7a700c7a257f2dd4eb82291 Mon Sep 17 00:00:00 2001 From: erick Date: Fri, 06 Nov 2009 15:50:06 +0000 Subject: Fixed probe tests --- (limited to 'tests') diff --git a/tests/probetests.py b/tests/probetests.py index e1a587b..977eda9 100644 --- a/tests/probetests.py +++ b/tests/probetests.py @@ -66,15 +66,20 @@ class MockAddon(Action): fake_addon_cache["MockAddon"] = MockAddon class MockActivity(object): - pass + def get_bundle_id(self): + return "localhost.unittest.ProbeTest" + + def get_id(self): + return "unique_id_1" + class MockProbeProxy(object): _MockProxyCache = {} - def __new__(cls, activityName): + def __new__(cls, activityName, unique_id): #For testing, use only one instance per activityName return cls._MockProxyCache.setdefault(activityName, super(MockProbeProxy, cls).__new__(cls)) - def __init__(self, activityName): + def __init__(self, activityName, unique_id): """ Constructor @param activityName unique activity id. Must be a valid dbus bus name. @@ -153,6 +158,12 @@ class MockSessionBus(object): old_SessionBus = dbus.SessionBus +class MockServiceProxy(object): + def register_probe(self, process_name, unique_id): + pass + def unregister_probe(self, unique_id): + pass + ########################################################################### # Begin Test Cases ########################################################################### @@ -170,7 +181,7 @@ class ProbeTest(unittest.TestCase): #Setup the activity and probe self.activity = MockActivity() - self.probe = TProbe("localhost.unittest.ProbeTest", self.activity) + self.probe = TProbe(self.activity, MockServiceProxy()) #Override the eventOccured on the Probe... self.old_eO = self.probe.eventOccured @@ -287,67 +298,49 @@ class ProbeManagerTest(unittest.TestCase): MockProbeProxy._MockProxyCache = {} self.probeManager = ProbeManager(proxy_class=MockProbeProxy) - def test_attach(self): - #ErrorCase: Set currentActivity to unattached activity - #Attempt to set to a non existing activity - try: - self.probeManager.currentActivity = "act1" - assert False, "Exception expected" - except RuntimeError, e: - pass - - #Attach an activity - self.probeManager.attach("act1") - - #Should have been created - assert "act1" in MockProbeProxy._MockProxyCache.keys(), "Proxy not created" - - #ErrorCase: Attach multiple times to same activity - #Try to attach again - self.assertRaises(RuntimeWarning, self.probeManager.attach, "act1") - - #Set current activity should work - self.probeManager.currentActivity = "act1" - - #TODO Fill in the alive/notalive behavior at creation time once - # it is fixed in the ProbeManager - - def test_detach(self): - #attach an activity - self.probeManager.attach("act1") - self.probeManager.currentActivity = "act1" - act1 = MockProbeProxy("act1") - - #Now we detach - self.probeManager.detach("act1") - assert act1.MockAlive == False, "ProbeProxy should have been detached" - assert self.probeManager.currentActivity is None, "Current activity should be None" - - #Attempt to detach again, should do nothing - #ErrorCase: detach already detached (currently silent fail) - self.probeManager.detach("act1") - - #Now, attach 2 activities - self.probeManager.attach("act2") - self.probeManager.attach("act3") - act2 = MockProbeProxy("act2") - act3 = MockProbeProxy("act3") - - self.probeManager.currentActivity = "act2" - - assert act2.MockAlive and act3.MockAlive, "Both ProbeProxy instances should be alive" + def test_register_probe(self): + assert len(self.probeManager.get_registered_probes_list()) == 0 + + self.probeManager.register_probe("act1", "unique_id_1") + assert len(self.probeManager.get_registered_probes_list()) == 1 + assert len(self.probeManager.get_registered_probes_list("act1")) == 1 + assert self.probeManager.get_registered_probes_list()[0][0] == "unique_id_1" + + self.probeManager.register_probe("act2","unique_id_2") + assert len(self.probeManager.get_registered_probes_list()) == 2 + assert len(self.probeManager.get_registered_probes_list("act1")) == 1 + assert self.probeManager.get_registered_probes_list("act1")[0][0] == "unique_id_1" + assert len(self.probeManager.get_registered_probes_list("act2")) == 1 + assert self.probeManager.get_registered_probes_list("act2")[0][0] == "unique_id_2" + + def test_register_multiple_probes(self): + assert len(self.probeManager.get_registered_probes_list()) == 0 + + self.probeManager.register_probe("act1", "unique_id_1") + self.probeManager.register_probe("act1","unique_id_2") + assert len(self.probeManager.get_registered_probes_list()) == 2 + assert len(self.probeManager.get_registered_probes_list("act1")) == 2 + assert self.probeManager.get_registered_probes_list("act1")[0][0] == "unique_id_1" + assert self.probeManager.get_registered_probes_list("act1")[1][0] == "unique_id_2" + + def test_unregister_probe(self): + assert len(self.probeManager.get_registered_probes_list()) == 0 + self.probeManager.register_probe("act1", "unique_id_1") + self.probeManager.register_probe("act1","unique_id_2") + + self.probeManager.unregister_probe("unique_id_1") + assert len(self.probeManager.get_registered_probes_list("act1")) == 1 + assert self.probeManager.get_registered_probes_list("act1")[0][0] == "unique_id_2" - #Detach the not active activity - self.probeManager.detach("act3") - #Check the statuses - assert act2.MockAlive and not act3.MockAlive, "Only act2 should be alive" - assert self.probeManager.currentActivity == "act2", "act2 should not have failed" + self.probeManager.unregister_probe("unique_id_2") + assert len(self.probeManager.get_registered_probes_list("act1")) == 0 + assert self.probeManager.get_registered_probes_list("act1") == [] def test_actions(self): - self.probeManager.attach("act1") - self.probeManager.attach("act2") - act1 = MockProbeProxy("act1") - act2 = MockProbeProxy("act2") + 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() #ErrorCase: install, update, uninstall without currentActivity @@ -376,10 +369,10 @@ class ProbeManagerTest(unittest.TestCase): assert act1.MockAction is None, "Action should be uninstalled" def test_events(self): - self.probeManager.attach("act1") - self.probeManager.attach("act2") - act1 = MockProbeProxy("act1") - act2 = MockProbeProxy("act2") + 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() @@ -405,16 +398,18 @@ class ProbeManagerTest(unittest.TestCase): assert act1.MockEventAddr == "SomeAddress", "Unsubscribe should have been called" assert act2.MockEventAddr is None, "Unsubscribe should not have been called" + class ProbeProxyTest(unittest.TestCase): def setUp(self): dbus.SessionBus = MockSessionBus - self.mockObj = MockProxyObject("unittest.TestCase", "/tutorius/Probe") - self.probeProxy = ProbeProxy("unittest.TestCase") + self.mockObj = MockProxyObject("unittest.TestCase", "/tutorius/Probe/unique_id_1") + self.probeProxy = ProbeProxy("unittest.TestCase", "unique_id_1") def tearDown(self): dbus.SessionBus = old_SessionBus MockProxyObject._MockProxyObjects = {} + # TODO: Clean-Up the dbus session bus ??? def test_Alive(self): self.mockObj.MockRet["ping"] = "alive" -- cgit v0.9.1