Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ressources/icon.svg21
-rw-r--r--tests/vaulttests.py239
2 files changed, 132 insertions, 128 deletions
diff --git a/tests/ressources/icon.svg b/tests/ressources/icon.svg
new file mode 100644
index 0000000..bb28f04
--- /dev/null
+++ b/tests/ressources/icon.svg
@@ -0,0 +1,21 @@
+<?xml version="1.0" ?><!-- Created with Inkscape (http://www.inkscape.org/) --><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' [
+ <!ENTITY stroke_color "#000000">
+ <!ENTITY fill_color "#ffffff">
+]><svg height="55px" id="svg2393" inkscape:output_extension="org.inkscape.output.svg.inkscape" inkscape:version="0.47pre1 r21720" sodipodi:docname="tutortool.svg" sodipodi:version="0.32" version="1.1" width="55px" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:svg="http://www.w3.org/2000/svg">
+ <defs id="defs2395">
+ <inkscape:perspective id="perspective2401" inkscape:persp3d-origin="16 : 10.666667 : 1" inkscape:vp_x="0 : 16 : 1" inkscape:vp_y="0 : 1000 : 0" inkscape:vp_z="32 : 16 : 1" sodipodi:type="inkscape:persp3d"/>
+ </defs>
+ <sodipodi:namedview bordercolor="#666666" borderopacity="1.0" id="base" inkscape:current-layer="layer1" inkscape:cx="3.7661233" inkscape:cy="33.132055" inkscape:document-units="px" inkscape:grid-bbox="true" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:window-height="675" inkscape:window-maximized="0" inkscape:window-width="1057" inkscape:window-x="108" inkscape:window-y="45" inkscape:zoom="3.9590209" pagecolor="#ffffff" showgrid="true"/>
+ <metadata id="metadata2398">
+ <rdf:RDF>
+ <cc:Work rdf:about="">
+ <dc:format>image/svg+xml</dc:format>
+ <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
+ </cc:Work>
+ </rdf:RDF>
+ </metadata>
+ <g id="layer1" inkscape:groupmode="layer" inkscape:label="Layer 1" transform="translate(0,23)">
+ <path d="m 38.01548,1.5478747 c 0,7.1837999 -7.3667,13.0141283 -16.443525,13.0141283 -2.269208,0 -8.124729,3.152936 -13.9608513,4.252763 C 13.382177,14.110994 11.434521,11.926642 9.9463815,10.748864 6.9701032,8.3933076 5.1284282,5.1397735 5.1284282,1.5478747 c 0,-7.1837994 7.3666998,-13.0141297 16.4435268,-13.0141297 9.076825,0 16.443525,5.8303303 16.443525,13.0141297 z" id="path2403" sodipodi:nodetypes="cscsssc" style="fill:&fill_color;;fill-opacity:1;fill-rule:nonzero;stroke:&stroke_color;;stroke-width:1.96931934;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"/>
+ <path d="m 50.150276,6.4721386 c 0,2.621116 -1.428036,4.9953144 -3.735846,6.7142344 -1.153905,0.85946 -1.824287,2.434433 1.398853,6.784273 -6.258422,-3.991066 -8.65379,-4.001712 -10.413335,-4.001712 -7.03818,0 -12.750327,-4.254565 -12.750327,-9.4967954 0,-5.2422321 5.712147,-9.4967971 12.750327,-9.4967971 7.038182,0 12.750328,4.254565 12.750328,9.4967971 z" id="path3175" sodipodi:nodetypes="cccsssc" style="fill:&fill_color;;fill-opacity:1;fill-rule:nonzero;stroke:&stroke_color;;stroke-width:1.96931934;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"/>
+ </g>
+</svg> \ No newline at end of file
diff --git a/tests/vaulttests.py b/tests/vaulttests.py
index 02c34e8..c6bd852 100644
--- a/tests/vaulttests.py
+++ b/tests/vaulttests.py
@@ -29,9 +29,10 @@ import unittest
import os
import shutil
import zipfile
+import cStringIO
from sugar.tutorius import addon
-from sugar.tutorius.core import State, FiniteStateMachine, Tutorial
+from sugar.tutorius.tutorial import Tutorial
from sugar.tutorius.actions import *
from sugar.tutorius.filters import *
from sugar.tutorius.vault import Vault, XMLSerializer, Serializer, TutorialBundler
@@ -100,18 +101,14 @@ class VaultInterfaceTest(unittest.TestCase):
ini_file2.close()
# Create a dummy fsm
- self.fsm = FiniteStateMachine("testingMachine")
+ 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")
act2 = addon.create('BubbleMessage', message="Second message", position=[250, 150], tail_pos=[1,2])
- st1 = State("INIT")
- st1.add_action(act1)
- st1.add_event_filter(ev1, 'Second')
- st2 = State("Second")
- st2.add_action(act2)
- self.fsm.add_state(st1)
- self.fsm.add_state(st2)
+ self.fsm.add_action("INIT", act1)
+ st2 = self.fsm.add_state((act2,))
+ self.fsm.add_transition("INIT",(ev1, st2))
self.tuto_guid = uuid1()
# Create a dummy metadata dictionnary
@@ -146,7 +143,8 @@ class VaultInterfaceTest(unittest.TestCase):
# Creat a dummy tutorial .xml file
serializer = XMLSerializer()
- serializer.save_fsm(self.fsm, 'tutorial.xml', test_path)
+ with file(os.path.join(test_path, 'tutorial.xml'), 'w') as fsmfile:
+ serializer.save_tutorial(self.fsm, fsmfile)
# Create a dummy tutorial metadata file
self.create_test_metadata_file(os.path.join(test_path, 'meta.ini'), self.tuto_guid)
@@ -238,16 +236,8 @@ class VaultInterfaceTest(unittest.TestCase):
reloaded_tuto = vault.loadTutorial(self.tuto_guid)
# Compare the two FSMs
- reloaded_fsm = reloaded_tuto.state_machine
-
- assert reloaded_fsm._states.get("INIT").name == self.fsm._states.get("INIT").name, \
- 'FSM underlying dictionary differ from original to reformed one'
- assert reloaded_fsm._states.get("Second").name == self.fsm._states.get("Second").name, \
+ assert reloaded_tuto.get_state_dict().keys() == self.fsm.get_state_dict().keys(), \
'FSM underlying dictionary differ from original to reformed one'
- assert reloaded_fsm._states.get("INIT").get_action_list()[0].message == \
- self.fsm._states.get("INIT").get_action_list()[0].message, \
- 'FSM underlying State underlying Action differ from original to reformed one'
- assert len(reloaded_fsm.get_action_list()) == 0, "FSM should not have any actions on itself"
def test_saveTutorial(self):
"""
@@ -256,29 +246,65 @@ class VaultInterfaceTest(unittest.TestCase):
# Save the tutorial in the vault
vault = Vault()
- tutorial = Tutorial('test', self.fsm)
+ tutorial = self.fsm
vault.saveTutorial(tutorial, self.test_metadata_dict)
# Get the tutorial back
reloaded_tuto = vault.loadTutorial(self.save_test_guid)
# Compare the two FSMs
- reloaded_fsm = reloaded_tuto.state_machine
-
- assert reloaded_fsm._states.get("INIT").name == self.fsm._states.get("INIT").name, \
+ assert reloaded_tuto.get_state_dict().keys() == self.fsm.get_state_dict().keys(), \
'FSM underlying dictionary differ from original to reformed one'
- assert reloaded_fsm._states.get("Second").name == self.fsm._states.get("Second").name, \
- 'FSM underlying dictionary differ from original to reformed one'
- assert reloaded_fsm._states.get("INIT").get_action_list()[0].message == \
- self.fsm._states.get("INIT").get_action_list()[0].message, \
- 'FSM underlying State underlying Action differ from original to reformed one'
- assert len(reloaded_fsm.get_action_list()) == 0, "FSM should not have any actions on itself"
# TODO : Compare the initial and reloaded metadata when vault.Query() will accept specifc argument
# (so we can specifiy that we want only the metadata for this particular tutorial
-
-
+
+ def test_add_delete_get_path_resource(self):
+ """
+ This test verify that the vault interface function add_resource succesfully add resource in the vault
+ and return the new resource id. It also test the deletion of the resource.
+ """
+ # Path of an image file in the test folder
+ test_path = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp')
+ if os.path.isdir(test_path) == True:
+ shutil.rmtree(os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp'))
+ os.makedirs(test_path)
+ file_path = os.path.join(test_path, 'test_resource.svg')
+ resource_file = open(file_path, 'wt')
+ resource_file.write('test')
+ resource_file.close()
+ #image_path = os.path.join(os.getcwd(), 'tests', 'resources', 'icon.svg')
+ #assert os.path.isfile(image_path), 'cannot find the test image file'
+
+ # Create and save a tutorial
+ tutorial = Tutorial('test')
+ Vault.saveTutorial(tutorial, self.test_metadata_dict)
+
+ bundler = TutorialBundler(self.save_test_guid)
+ tuto_path = bundler.get_tutorial_path(self.save_test_guid)
+
+ # add the resource to the tutorial
+ resource_id = Vault.add_resource(self.save_test_guid, file_path)
+
+ # Check that the image file is now in the vault
+ assert os.path.isfile(os.path.join(tuto_path, 'resources', resource_id)), 'image file not found in vault'
+
+ # Check if get_resource_path Vault interface function is working
+ vault_path = Vault.get_resource_path(self.save_test_guid, resource_id)
+
+ assert os.path.isfile(vault_path), 'path returned is not a file'
+ basename, extension = os.path.splitext(vault_path)
+ assert extension == '.svg', 'wrong file path have been returned'
+
+ # Delete the resource
+ Vault.delete_resource(self.save_test_guid, resource_id)
+
+ # Check that the resource is not in the vault anymore
+ assert os.path.isfile(os.path.join(tuto_path, 'resources', resource_id)) == False, 'image file found in vault when it should have been deleted.'
+
+
+
def tearDown(self):
folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data');
for file in os.listdir(folder):
@@ -298,8 +324,8 @@ class SerializerInterfaceTest(unittest.TestCase):
ser = Serializer()
try:
- ser.save_fsm(None)
- assert False, "save_fsm() should throw an unimplemented error"
+ ser.save_tutorial(None)
+ assert False, "save_tutorial() should throw an unimplemented error"
except:
pass
@@ -307,8 +333,8 @@ class SerializerInterfaceTest(unittest.TestCase):
ser = Serializer()
try:
- ser.load_fsm(str(uuid.uuid1()))
- assert False, "load_fsm() should throw an unimplemented error"
+ ser.load_tutorial(str(uuid.uuid1()))
+ assert False, "load_tutorial() should throw an unimplemented error"
except:
pass
@@ -318,100 +344,61 @@ class XMLSerializerTest(unittest.TestCase):
"""
def setUp(self):
-
- os.environ["SUGAR_BUNDLE_PATH"] = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path')
- path = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path')
- if os.path.isdir(path) != True:
- os.makedirs(path)
-
# Create the sample FSM
- self.fsm = FiniteStateMachine("testingMachine")
+ 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")
act2 = addon.create('BubbleMessage', message="Second message", position=[250, 150], tail_pos=[1,2])
- st1 = State("INIT")
- st1.add_action(act1)
- st1.add_event_filter(ev1, 'Second')
-
- st2 = State("Second")
-
- st2.add_action(act2)
-
- self.fsm.add_state(st1)
- self.fsm.add_state(st2)
+ self.fsm.add_action("INIT",act1)
+ st2 = self.fsm.add_state((act2,))
+ self.fsm.add_transition("INIT",(ev1, st2))
self.uuid = uuid1()
- # Flag to set to True if the output can be deleted after execution of
- # the test
- self.remove = True
-
def tearDown(self):
"""
- Removes the created files, if need be.
+ Nothing to do anymore.
"""
- if self.remove == True:
- shutil.rmtree(os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path'))
-
- folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data');
- for file in os.listdir(folder):
- file_path = os.path.join(folder, file)
- shutil.rmtree(file_path)
+ pass
- def create_test_metadata(self, ini_file_path, guid):
- ini_file = open(ini_file_path, 'wt')
- ini_file.write("[GENERAL_METADATA]\n")
- ini_file.write('guid=' + str(guid) + '\n')
- ini_file.write('name=TestTutorial1\n')
- ini_file.write('version=1\n')
- ini_file.write('description=This is a test tutorial 1\n')
- ini_file.write('rating=3.5\n')
- ini_file.write('category=Test\n')
- ini_file.write('publish_state=false\n')
- ini_file.write('[RELATED_ACTIVITIES]\n')
- ini_file.write('org.laptop.TutoriusActivity = 1\n')
- ini_file.write('org.laptop.Writus = 1\n')
- ini_file.close()
-
- def test_save(self):
- """
- Writes an FSM to disk, then compares the file to the expected results.
- "Remove" boolean argument specify if the test data must be removed or not
- """
- xml_ser = XMLSerializer()
- os.makedirs(os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid)))
- xml_ser.save_fsm(self.fsm, sugar.tutorius.vault.TUTORIAL_FILENAME, os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid)))
- self.create_test_metadata(os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid), 'meta.ini'), self.uuid)
-
+ def create_test_metadata(self, file_obj, guid):
+ file_obj.write("[GENERAL_METADATA]\n")
+ file_obj.write('guid=' + str(guid) + '\n')
+ file_obj.write('name=TestTutorial1\n')
+ file_obj.write('version=1\n')
+ file_obj.write('description=This is a test tutorial 1\n')
+ file_obj.write('rating=3.5\n')
+ file_obj.write('category=Test\n')
+ file_obj.write('publish_state=false\n')
+ file_obj.write('[RELATED_ACTIVITIES]\n')
+ file_obj.write('org.laptop.TutoriusActivity = 1\n')
+ file_obj.write('org.laptop.Writus = 1\n')
def test_save_and_load(self):
"""
+ Writes an FSM to disk, then compares the file to the expected results.
Load up the written FSM and compare it with the object representation.
"""
- self.test_save()
xml_ser = XMLSerializer()
-
- loaded_fsm = xml_ser.load_fsm(str(self.uuid))
+ tuto_file = cStringIO.StringIO()
+ xml_ser.save_tutorial(self.fsm, tuto_file)
+
+ xml_ser = XMLSerializer()
+ load_tuto_file = cStringIO.StringIO(tuto_file.getvalue())
+ loaded_fsm = xml_ser.load_tutorial(load_tuto_file)
# Compare the two FSMs
- assert loaded_fsm._states.get("INIT").name == self.fsm._states.get("INIT").name, \
- 'FSM underlying dictionary differ from original to reformed one'
- assert loaded_fsm._states.get("Second").name == self.fsm._states.get("Second").name, \
- 'FSM underlying dictionary differ from original to reformed one'
- assert loaded_fsm._states.get("INIT").get_action_list()[0].message == \
- self.fsm._states.get("INIT").get_action_list()[0].message, \
- 'FSM underlying State underlying Action differ from original to reformed one'
- assert len(loaded_fsm.get_action_list()) == 0, "FSM should not have any actions on itself"
+ assert loaded_fsm == self.fsm, "Loaded FSM differs from original one"
def test_all_actions(self):
"""
Inserts all the known action types in a FSM, then attempt to load it.
"""
- st = State("INIT")
-
+ 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])
act3 = addon.create('WidgetIdentifyAction')
@@ -423,26 +410,24 @@ class XMLSerializerTest(unittest.TestCase):
actions = [act1, act2, act3, act4, act5, act6, act7, act8]
for action in actions:
- st.add_action(action)
+ fsm.add_action("INIT", action)
- self.fsm.remove_state("Second")
- self.fsm.remove_state("INIT")
- self.fsm.add_state(st)
-
xml_ser = XMLSerializer()
+ xml_ser.save_tutorial(fsm, tuto_file)
+ load_tuto_file = cStringIO.StringIO(tuto_file.getvalue())
- self.test_save()
-
- reloaded_fsm = xml_ser.load_fsm(str(self.uuid))
-
- # TODO : Cannot do object equivalence, must check equality of all underlying object
- # assert self.fsm == reloaded_fsm, "Expected equivalence before saving vs after loading."
+ reloaded_fsm = xml_ser.load_tutorial(load_tuto_file)
+ # Compare the two FSMs
+ assert reloaded_fsm == fsm, "Loaded FSM differs from original one"
+ assert fsm.get_action_dict() == reloaded_fsm.get_action_dict(), \
+ "Actions should be the same"
def test_all_filters(self):
"""
Inserts all the known action filters in a FSM, then attempt to load it.
"""
- st = State("INIT")
+ fsm = Tutorial("TestFilters")
+ tuto_file = cStringIO.StringIO()
ev1 = addon.create('TimerEvent', 1000)
ev2 = addon.create('GtkWidgetEventFilter', object_id="0.0.1.1.0.0.1", event_name="clicked")
@@ -450,20 +435,18 @@ class XMLSerializerTest(unittest.TestCase):
ev4 = addon.create('GtkWidgetTypeFilter', "0.0.1.1.1.2.3", strokes="acbd")
filters = [ev1, ev2, ev3, ev4]
- for filter in filters:
- st.add_event_filter(filter, 'Second')
+ for efilter in filters:
+ fsm.add_transition("INIT", (efilter, 'END'))
- self.fsm.remove_state("INIT")
- self.fsm.add_state(st)
-
xml_ser = XMLSerializer()
-
- self.test_save()
-
- reloaded_fsm = xml_ser.load_fsm(str(self.uuid))
-
- # TODO : Cannot do object equivalence, must check equality of all underlying object
- # assert self.fsm == reloaded_fsm, "Expected equivalence before saving vs after loading."
+ xml_ser.save_tutorial(fsm, tuto_file)
+ load_tuto_file = cStringIO.StringIO(tuto_file.getvalue())
+
+ reloaded_fsm = xml_ser.load_tutorial(load_tuto_file)
+ # Compare the two FSMs
+ assert reloaded_fsm == fsm, "Loaded FSM differs from original one"
+ assert fsm.get_transition_dict() == reloaded_fsm.get_transition_dict(), \
+ "Transitions should be the same"
class TutorialBundlerTests(unittest.TestCase):
@@ -472,7 +455,7 @@ class TutorialBundlerTests(unittest.TestCase):
This module contains all the tests for the storage mecanisms for tutorials
This mean testing saving and loading tutorial, .ini file management and
- adding ressources to tutorial
+ adding resources to tutorial
"""
def setUp(self):
@@ -513,4 +496,4 @@ class TutorialBundlerTests(unittest.TestCase):
if __name__ == "__main__":
- unittest.main()
+ unittest.main()