use Dummy backend for tests
[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/textreceiver.h"
27 #include "pbd/file_utils.h"
28
29 #include "ardour/session.h"
30 #include "ardour/audioengine.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 (p->is_content(), q->is_content());
44         if (!p->is_content()) {
45                 CPPUNIT_ASSERT_EQUAL (p->name(), q->name());
46         } else {
47                 CPPUNIT_ASSERT_EQUAL (p->content(), q->content());
48         }
49
50         XMLPropertyList const & pp = p->properties ();
51         XMLPropertyList const & qp = q->properties ();
52         CPPUNIT_ASSERT_EQUAL (pp.size(), qp.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 ((*i)->name(), (*j)->name());
58                 if (find (ignore_properties.begin(), ignore_properties.end(), (*i)->name ()) == ignore_properties.end ()) {
59                         CPPUNIT_ASSERT_EQUAL ((*i)->value(), (*j)->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 (pc.size(), qc.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         return ref.write (ref_file);
96 }
97
98 class TestReceiver : public Receiver 
99 {
100 protected:
101         void receive (Transmitter::Channel chn, const char * str) {
102                 const char *prefix = "";
103                 
104                 switch (chn) {
105                 case Transmitter::Error:
106                         prefix = ": [ERROR]: ";
107                         break;
108                 case Transmitter::Info:
109                         /* ignore */
110                         return;
111                 case Transmitter::Warning:
112                         prefix = ": [WARNING]: ";
113                         break;
114                 case Transmitter::Fatal:
115                         prefix = ": [FATAL]: ";
116                         break;
117                 case Transmitter::Throw:
118                         /* this isn't supposed to happen */
119                         abort ();
120                 }
121                 
122                 /* note: iostreams are already thread-safe: no external
123                    lock required.
124                 */
125                 
126                 cout << prefix << str << endl;
127                 
128                 if (chn == Transmitter::Fatal) {
129                         exit (9);
130                 }
131         }
132 };
133
134 TestReceiver test_receiver;
135
136 /** @param dir Session directory.
137  *  @param state Session state file, without .ardour suffix.
138  */
139 Session *
140 load_session (string dir, string state)
141 {
142         SessionEvent::create_per_thread_pool ("test", 512);
143
144         test_receiver.listen_to (error);
145         test_receiver.listen_to (info);
146         test_receiver.listen_to (fatal);
147         test_receiver.listen_to (warning);
148
149         /* We can't use VSTs here as we have a stub instead of the
150            required bits in gtk2_ardour.
151         */
152         Config->set_use_lxvst (false);
153
154         AudioEngine* engine = AudioEngine::create ();
155
156         CPPUNIT_ASSERT (engine->set_backend ("Dummy", "", ""));
157
158         init_post_engine ();
159
160         CPPUNIT_ASSERT (engine->start () == 0);
161
162         Session* session = new Session (*engine, dir, state);
163         engine->set_session (session);
164         return session;
165 }
166
167 PBD::Searchpath
168 test_search_path ()
169 {
170 #ifdef PLATFORM_WINDOWS
171         std::string wsp(g_win32_get_package_installation_directory_of_module(NULL));
172         return Glib::build_filename (wsp, "ardour_testdata");
173 #else
174         return Glib::getenv("ARDOUR_TEST_PATH");
175 #endif
176 }
177
178 std::string
179 new_test_output_dir (std::string prefix)
180 {
181         return PBD::tmp_writable_directory (PACKAGE, prefix);
182 }
183
184 int
185 get_test_sample_rate ()
186 {
187         return 44100;
188 }