Small steps towards rolling backwards..
[ardour.git] / libs / ardour / test / lua_script_test.cc
1 #include <list>
2 #include <glibmm.h>
3
4 #include "ardour/audio_track.h"
5 #include "ardour/audioengine.h"
6 #include "ardour/luascripting.h"
7 #include "ardour/lua_script_params.h"
8 #include "ardour/plugin_manager.h"
9 #include "ardour/plugin_insert.h"
10 #include "ardour/session.h"
11
12 #include "lua_script_test.h"
13
14 using namespace ARDOUR;
15
16 CPPUNIT_TEST_SUITE_REGISTRATION(LuaScriptTest);
17
18 void
19 LuaScriptTest::session_script_test ()
20 {
21         LuaScriptList scripts (LuaScripting::instance ().scripts (LuaScriptInfo::Session));
22         printf("\n * Testing %ld Lua session scripts\n", scripts.size());
23
24         for (LuaScriptList::const_iterator s = scripts.begin(); s != scripts.end(); ++s) {
25                 const LuaScriptInfoPtr& spi (*s);
26
27                 std::string script = "";
28
29                 if (Glib::path_get_basename (spi->path).at(0) == '_') {
30                         continue;
31                 }
32
33                 try {
34                         script = Glib::file_get_contents (spi->path);
35                 } catch (Glib::FileError e) {
36                         CPPUNIT_FAIL ("Cannot read script file");
37                         continue;
38                 }
39
40                 try {
41                         LuaScriptParamList lsp = LuaScriptParams::script_params (spi, "sess_params");
42                         _session->register_lua_function ("test", script, lsp);
43                 } catch (SessionException e) {
44                         CPPUNIT_FAIL ("Cannot add script to session");
45                         continue;
46                 }
47                 CPPUNIT_ASSERT (!_session->registered_lua_functions ().empty());
48                 Glib::usleep(200000); // wait to script to execute during process()
49                 // if the script fails, it'll be removed.
50                 CPPUNIT_ASSERT (!_session->registered_lua_functions ().empty());
51                 _session->unregister_lua_function ("test");
52                 CPPUNIT_ASSERT (_session->registered_lua_functions ().empty());
53         }
54 }
55
56 void
57 LuaScriptTest::dsp_script_test ()
58 {
59         PluginManager& pm = PluginManager::instance ();
60         std::list<boost::shared_ptr<AudioTrack> > tracks;
61
62         tracks = _session->new_audio_track (2, 2, NULL, 1, "", PresentationInfo::max_order);
63         CPPUNIT_ASSERT (tracks.size() == 1);
64         boost::shared_ptr<Route> r = tracks.front ();
65
66         std::cout << "\n";
67         const PluginInfoList& plugs = pm.lua_plugin_info();
68         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
69                 std::cout << "LuaProc: " <<(*i)->name << "\n";
70
71                 PluginPtr p = (*i)->load (*_session);
72                 CPPUNIT_ASSERT (p);
73
74                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, p));
75                 processor->enable (true);
76
77                 int rv = r->add_processor (processor, boost::shared_ptr<Processor>(), 0);
78                 CPPUNIT_ASSERT (rv == 0);
79                 processor->enable (true);
80                 Glib::usleep(200000); // run process, failing plugins will be deactivated.
81                 CPPUNIT_ASSERT (processor->active());
82                 rv = r->remove_processor (processor, NULL, true);
83                 CPPUNIT_ASSERT (rv == 0);
84         }
85 }