Initial commit
[fillmore] / src / test / marina / ProjectLoading.vala
1 /* Copyright 2009 Yorba Foundation
2  *
3  * This software is licensed under the GNU Lesser General Public License
4  * (version 2.1 or later).  See the COPYING file in this distribution. 
5  */
6
7 namespace Model {
8
9 public class ClipFetcher {
10     public string error_string;
11     public ClipFile clipfile;
12     string filename;
13     
14     public ClipFetcher(string filename) {
15         this.filename = filename;
16     }
17     public string get_filename() {
18         return filename;
19     }
20     public signal void ready(ClipFetcher fetcher);
21 }
22
23 public class ClipFile {
24     public string filename;
25 }
26 }
27
28 // Describes an XML Document and if the test should consider it a valid or an invalid document
29 struct ValidDocument {
30     public bool valid;
31     public string document;
32     public ValidDocument(bool valid, string document) {
33         this.valid = valid;
34         this.document = document;
35     }
36 }
37
38 ValidDocument[] project_documents; // The set of documents for building the test suite
39 int current_document; // Index of the document for the test we are currently building
40
41 // StateChangeFixture holds the root of an XML tree describing a project, the TreeBuilder
42 // and if the XML tree is expected to be valid or not
43 struct StateChangeFixture {
44     public Model.XmlElement root;
45     public Model.ProjectBuilder project_builder;
46     public bool valid;
47 }
48
49 void state_change_fixture_buildup(void *fixture) {
50     StateChangeFixture* state_change_fixture = (StateChangeFixture*)fixture;
51     Model.XmlTreeLoader tree_loader = new Model.XmlTreeLoader(project_documents[current_document].document);
52     state_change_fixture->root = tree_loader.root;
53     state_change_fixture->project_builder = new Model.ProjectBuilder(new Model.LoaderHandler());
54     state_change_fixture->valid = project_documents[current_document].valid;
55     ++current_document;
56 }
57
58 void state_change_fixture_teardown(void *fixture) {
59     StateChangeFixture* state_change_fixture = (StateChangeFixture*)fixture;
60     state_change_fixture->root = null;
61     state_change_fixture->project_builder = null;
62 }
63
64 bool document_valid; // if a document is invalid, on_error_occurred will set this variable to false
65
66 void on_error_occurred(string? message) {
67     Test.message("received error: %s", message);
68     document_valid = false;
69 }
70
71 // The actual test code.  It builds the given document and then asserts that the result is what
72 // was expected.
73 void check_document(void *fixture) {
74     StateChangeFixture* state_change_fixture = (StateChangeFixture*)fixture;
75
76     Test.message("checking document expecting to be %s", 
77         state_change_fixture->valid ? "valid" : "invalid");
78
79     Model.XmlElement root = state_change_fixture->root;
80     Model.ProjectBuilder project_builder = state_change_fixture->project_builder;
81
82     document_valid = true;
83     project_builder.error_occurred.connect(on_error_occurred);
84     
85     // We call check project to check the integrity of the file skeleton.
86     // If it's good, then we can load all the pieces of the file (library, tracks).
87     if (project_builder.check_project(root))
88         project_builder.build_project(root);
89     assert(document_valid == state_change_fixture->valid);
90     Test.message("finished executing check document");
91 }
92
93
94 class ProjectLoaderSuite : TestSuite {
95     public ProjectLoaderSuite() {
96         base("ProjectLoaderSuite");
97
98         current_document = 0;
99         project_documents = {
100             ValidDocument(true, "<marina><library></library><tracks><track><clip /><clip /></track>"
101                     + "<track><clip /></track></tracks><preferences /></marina>"),
102             ValidDocument(true, "<marina><library /><tracks /><preferences/>"
103                     + "<maps><tempo /><time_signature /></marina>"),
104             ValidDocument(true, "<marina><library /><tracks /><preferences/><maps><tempo />"
105                     + "</marina>"),
106             ValidDocument(true, "<marina><library /><tracks /><preferences/>"
107                     + "<maps><time_signature /></marina>"),
108             ValidDocument(true, "<marina><library></library><tracks><track /></tracks>"
109                     + "<preferences/></marina>"),
110             ValidDocument(true, "<marina><library></library><tracks><track><clip /></track>"
111                     + "</tracks><preferences/></marina>"),
112             ValidDocument(true, "<marina><library/><tracks/><preferences/>"
113                     + "<maps><tempo><entry></tempo></maps></marina>"),
114             ValidDocument(true, "<marina><library/><tracks/><preferences/>"
115                     +"<maps><time_signature><entry></time_signature></maps></marina>"),
116             ValidDocument(true, "<marina><library/><tracks/><preferences/></marina>"),
117             ValidDocument(true, "<marina><library/><tracks/><preferences><click/></preferences>"
118                 + "</marina>"),
119             ValidDocument(true, "<marina><library/><tracks/><preferences><library/>" + 
120                 "</preferences></marina>"),
121             ValidDocument(true, "<marina><library/><tracks/><preferences><click/><library/>" +
122                 "</preferences></marina>"),
123             ValidDocument(false, "<marina><tracks></tracks><library></library></marina>"),
124             ValidDocument(false, "<marina><library></library><track></track></marina>"),
125             ValidDocument(false, "<marina><library><clip /></library><tracks><clipfile />"
126                     + "</tracks></marina>"),
127             ValidDocument(false, "<marina />"),            
128             ValidDocument(false, "<library />"),
129             ValidDocument(false, "<track />"),
130             ValidDocument(false, "<clip />"),
131             ValidDocument(false, "<entry />"),
132             ValidDocument(false, "<tempo />"),
133             ValidDocument(false, "<maps />"),
134             ValidDocument(false, "<preferences />"),
135             ValidDocument(false, "<click />"),
136             ValidDocument(false, "<time_signature />"),
137             ValidDocument(false, "<marina><clip /></marina>"),
138             ValidDocument(false, "<marina><track><clip><track /></clip></track></marina>"),
139             ValidDocument(false, "<unknown />"),
140             ValidDocument(false, "<marina><foo /></marina>"),
141             ValidDocument(false, "<marina><track><foo /></track></track></marina>"),
142             ValidDocument(false, "<marina><track><clip><foo /></clip></track></marina>"),
143             ValidDocument(false, "<marina><library/><tracks/><maps><foo/></maps></marina>"),
144             ValidDocument(false, "<marina><library/><tracks/><maps><time_signature>"
145                     + "<tempo/></time_signature></marina>"),
146             ValidDocument(false, "<marina><library/><tracks/><click/></marina>"),
147             ValidDocument(false, "<marina><library/><tracks/><preferences><tracks/>"
148                     +"</preferences></marina>"),
149             ValidDocument(false, "<marina><library/><tracks/><maps/><preferences/></marina>")
150         };
151         
152         int length = project_documents.length;
153         
154         for (int i = 0; i < length; ++i) {
155             if (Test.thorough() || project_documents[i].valid) {
156                 add(new TestCase("Document%d".printf(i), state_change_fixture_buildup, 
157                     check_document, state_change_fixture_teardown, sizeof(StateChangeFixture)));
158             }
159         }
160     }
161 }
162