522020346526117b595221f22a80af8e6a47cee4
[ardour.git] / libs / ardour / test / test_util.cc
1 /*
2     Copyright (C) 2011 Paul Davis
3     Copyright (C) 2011 Tim Mayberry
4
5     This program is free software; you can redistribute it and/or modify it
6     under the terms of the GNU General Public License as published by the Free
7     Software Foundation; either version 2 of the License, or (at your option)
8     any later version.
9
10     This program is distributed in the hope that it will be useful, but WITHOUT
11     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13     for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #include <fstream>
20 #include <sstream>
21
22 #include <glibmm/fileutils.h>
23 #include <glibmm/miscutils.h>
24
25 #include "pbd/xml++.h"
26 #include "pbd/file_utils.h"
27
28 #include "ardour/session.h"
29 #include "ardour/audioengine.h"
30 #include "ardour/filesystem_paths.h"
31
32 #include "test_util.h"
33
34 #include <cppunit/extensions/HelperMacros.h>
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 static void
41 check_nodes (XMLNode const * p, XMLNode const * q, list<string> const & ignore_properties)
42 {
43         CPPUNIT_ASSERT_EQUAL (q->is_content(), p->is_content());
44         if (!p->is_content()) {
45                 CPPUNIT_ASSERT_EQUAL (q->name(), p->name());
46         } else {
47                 CPPUNIT_ASSERT_EQUAL (q->content(), p->content());
48         }
49
50         XMLPropertyList const & pp = p->properties ();
51         XMLPropertyList const & qp = q->properties ();
52         CPPUNIT_ASSERT_EQUAL (qp.size(), pp.size());
53
54         XMLPropertyList::const_iterator i = pp.begin ();
55         XMLPropertyList::const_iterator j = qp.begin ();
56         while (i != pp.end ()) {
57                 CPPUNIT_ASSERT_EQUAL ((*j)->name(), (*i)->name());
58                 if (find (ignore_properties.begin(), ignore_properties.end(), (*i)->name ()) == ignore_properties.end ()) {
59                         CPPUNIT_ASSERT_EQUAL_MESSAGE ((*j)->name(), (*i)->value(), (*i)->value());
60                 }
61                 ++i;
62                 ++j;
63         }
64
65         XMLNodeList const & pc = p->children ();
66         XMLNodeList const & qc = q->children ();
67
68         CPPUNIT_ASSERT_EQUAL (qc.size(), pc.size());
69         XMLNodeList::const_iterator k = pc.begin ();
70         XMLNodeList::const_iterator l = qc.begin ();
71
72         while (k != pc.end ()) {
73                 check_nodes (*k, *l, ignore_properties);
74                 ++k;
75                 ++l;
76         }
77 }
78
79 void
80 check_xml (XMLNode* node, string ref_file, list<string> const & ignore_properties)
81 {
82         XMLTree ref (ref_file);
83
84         XMLNode* p = node;
85         XMLNode* q = ref.root ();
86
87         check_nodes (p, q, ignore_properties);
88 }
89
90 bool
91 write_ref (XMLNode* node, string ref_file)
92 {
93         XMLTree ref;
94         ref.set_root (node);
95         bool rv = ref.write (ref_file);
96         ref.set_root (0);
97         return rv;
98 }
99
100 void
101 create_and_start_dummy_backend ()
102 {
103         AudioEngine* engine = AudioEngine::create ();
104
105         CPPUNIT_ASSERT (AudioEngine::instance ());
106         CPPUNIT_ASSERT (engine);
107         CPPUNIT_ASSERT (engine->set_backend ("None (Dummy)", "Unit-Test", ""));
108
109         init_post_engine ();
110
111         CPPUNIT_ASSERT (engine->start () == 0);
112 }
113
114 void
115 stop_and_destroy_backend ()
116 {
117         AudioEngine::instance()->remove_session ();
118         AudioEngine::instance()->stop ();
119         AudioEngine::destroy ();
120 }
121
122 /** @param dir Session directory.
123  *  @param state Session state file, without .ardour suffix.
124  */
125 Session *
126 load_session (string dir, string state)
127 {
128         Session* session = new Session (*AudioEngine::instance(), dir, state);
129         AudioEngine::instance ()->set_session (session);
130         return session;
131 }
132
133 PBD::Searchpath
134 test_search_path ()
135 {
136 #ifdef PLATFORM_WINDOWS
137         if (!getenv("ARDOUR_TEST_PATH")) {
138                 std::string wsp(windows_package_directory_path());
139                 return Glib::build_filename (wsp, "ardour_testdata");
140         }
141 #endif
142         return Glib::getenv("ARDOUR_TEST_PATH");
143 }
144
145 std::string
146 new_test_output_dir (std::string prefix)
147 {
148         return PBD::tmp_writable_directory (PACKAGE, prefix);
149 }
150
151 int
152 get_test_sample_rate ()
153 {
154         return 44100;
155 }