From 7035c6e281332b1688c59877ac78516a0dd4635d Mon Sep 17 00:00:00 2001 From: JCTutorius Date: Fri, 06 Nov 2009 01:05:12 +0000 Subject: Merge branch 'master' of gitorious@git.sugarlabs.org:tutorius/mainline --- (limited to 'tests') diff --git a/tests/enginetests.py b/tests/enginetests.py new file mode 100644 index 0000000..30d68de --- /dev/null +++ b/tests/enginetests.py @@ -0,0 +1,116 @@ +# Copyright (C) 2009, Tutorius.org +# Copyright (C) 2009, Erick Lavoie +# +# 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 +""" +Engine Tests + + + +Usage of actions and event filters is tested, but not the concrete actions +and event filters. Those are in their separate test module + +""" + +import unittest + +from sugar.tutorius.tutorial import Tutorial +from sugar.tutorius.engine import TutorialRunner +from sugar.tutorius.filters import EventFilter + +from actiontests import CountAction + +class MockProbeMgr(object): + def __init__(self): + self.action = None + self.event = None + self.cB = None + + def doCB(self): + self.cB(self.event) + + currentActivity = property(fget=lambda s:s, fset=lambda s, v: v) + + def install(self, action, block=False): + self.action = action + + def update(self, action, newaction, block=False): + self.action = newaction + + def uninstall(self, action, block=False): + self.action = None + + def subscribe(self, event, callback): + self.event = event + self.cB = callback + self.event.install_handlers(callback) + return str(event) + + def unsubscribe(self, address): + self.event = None + +class MockEvent(EventFilter): + pass + + + +class TutorialRunnerTest(unittest.TestCase): + """ + This class needs to test the TutorialRunner + """ + def setUp(self): + self.pM = MockProbeMgr() + + + def tearDown(self): + self.pM = None + + # Basic interface cases + def testOneStateTutorial(self): + tutorial = Tutorial("TutorialRunner") + state_name = tutorial.add_state() + tutorial.update_transition(Tutorial.INITIAL_TRANSITION_NAME, + None, state_name) + event = MockEvent() + tutorial.add_transition(state_name, (event, Tutorial.END)) + + runner = TutorialRunner(tutorial, self.pM) + runner.start() + + assert runner._state == state_name, "Current state is: %s"%runner._state + assert self.pM.action == None + assert self.pM.event == event + + event.do_callback() + assert runner._state == Tutorial.END, "Current state is: %s"%runner._state + assert self.pM.action == None, "Current action is %s"%str(self.pM.action) + assert self.pM.event == None, "Current event is %s"%str(self.pM.event) + + + + # Limit cases + def testEmptyTutorial(self): + tutorial = Tutorial("TutorialRunner") + runner = TutorialRunner(tutorial, self.pM) + runner.start() + + assert runner._state == Tutorial.END, "Current state is: %s"%runner._state + assert self.pM.action == None + assert self.pM.event == None + + # Error cases + +if __name__ == "__main__": + unittest.main() diff --git a/tests/probetests.py b/tests/probetests.py index e1a587b..59072e5 100644 --- a/tests/probetests.py +++ b/tests/probetests.py @@ -47,7 +47,7 @@ class MockAddon(Action): i = TIntProperty(0) s = TStringProperty("test") - def do(self): + def do(self, **kwargs): global message_box message_box = (self.i, self.s) @@ -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,12 +398,13 @@ 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 diff --git a/tests/skip b/tests/skip index 3868383..028ecaf 100644 --- a/tests/skip +++ b/tests/skip @@ -2,3 +2,4 @@ utils.py run-tests.py overlaytests.py viewer.py +coretests.py diff --git a/tests/storetests.py b/tests/storetests.py index 0c36973..3f1b73c 100644 --- a/tests/storetests.py +++ b/tests/storetests.py @@ -31,13 +31,11 @@ class StoreProxyTest(unittest.TestCase): def tearDown(self): pass - @catch_unimplemented def test_get_categories(self): categories = self.store.get_categories() assert isinstance(categories, list), "categories should be a list" - @catch_unimplemented def test_get_tutorials(self): self.store.get_tutorials() @@ -46,17 +44,14 @@ class StoreProxyTest(unittest.TestCase): assert isinstance(version_dict, dict) - @catch_unimplemented def test_download_tutorial(self): tutorial = self.store.download_tutorial(g_other_id) assert tutorial is not None - @catch_unimplemented def test_login(self): assert self.store.login("benoit.tremblay1@gmail.com", "tutorius12") - @catch_unimplemented def test_register_new_user(self): random_num = str(random.randint(0, 999999999)) user_info = { @@ -69,29 +64,24 @@ class StoreProxyTest(unittest.TestCase): class StoreProxyLoginTest(unittest.TestCase): - @catch_unimplemented def setUp(self): self.store = StoreProxy("http://bobthebuilder.mine.nu/tutorius/en-US/tutorius") self.store.login("nobody@mozilla.org", "tutorius12") - @catch_unimplemented def tearDown(self): session_id = self.store.get_session_id() if session_id is not None: self.store.close_session() - @catch_unimplemented def test_get_session_id(self): session_id = self.store.get_session_id() assert session_id is not None - @catch_unimplemented def test_rate(self): assert self.store.rate(5, g_tutorial_id) - @catch_unimplemented def test_publish(self): # TODO : We need to send in a real tutorial loaded from # the Vault @@ -108,7 +98,6 @@ class StoreProxyLoginTest(unittest.TestCase): } assert self.store.publish('This should be a real tutorial...', tutorial_info) != -1 - @catch_unimplemented def test_unpublish(self): assert self.store.unpublish(g_tutorial_id) @@ -118,7 +107,6 @@ class StoreProxyLoginTest(unittest.TestCase): def test_republish(self): assert self.store.publish(None, None, g_tutorial_id) - @catch_unimplemented def test_update_published_tutorial(self): # TODO : Run these tests with files from the Vault #self.store.publish([g_tutorial_id, 'Fake tutorial']) -- cgit v0.9.1