Move libardour test utility code into one source file
[ardour.git] / libs / ardour / test / automation_list_property_test.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <glibmm/fileutils.h>
20 #include <glibmm/miscutils.h>
21
22 #include "pbd/properties.h"
23 #include "pbd/stateful_diff_command.h"
24 #include "ardour/automation_list.h"
25 #include "automation_list_property_test.h"
26 #include "test_util.h"
27
28 CPPUNIT_TEST_SUITE_REGISTRATION (AutomationListPropertyTest);
29
30 using namespace std;
31 using namespace PBD;
32 using namespace ARDOUR;
33
34 void
35 write_automation_list_xml (XMLNode* node, std::string filename)
36 {
37         // use the same output dir for all of them
38         static std::string test_output_dir = new_test_output_dir ("automation_list_property");
39         std::string output_file = Glib::build_filename (test_output_dir, filename);
40
41         CPPUNIT_ASSERT (write_ref (node, output_file));
42 }
43
44 void
45 AutomationListPropertyTest::basicTest ()
46 {
47         list<string> ignore_properties;
48         ignore_properties.push_back ("id");
49
50         PropertyDescriptor<boost::shared_ptr<AutomationList> > descriptor;
51         descriptor.property_id = g_quark_from_static_string ("FadeIn");
52         AutomationListProperty property (
53                 descriptor,
54                 boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))
55                 );
56
57         property.clear_changes ();
58
59         /* No change since we just cleared them */
60         CPPUNIT_ASSERT_EQUAL (false, property.changed());
61         
62         property->add (1, 2);
63         property->add (3, 4);
64
65         /* Now it has changed */
66         CPPUNIT_ASSERT_EQUAL (true, property.changed());
67
68         std::string test_data_filename = "automation_list_property_test1.ref";
69         std::string test_data_file1 = Glib::build_filename (test_search_path().front(), test_data_filename);
70         CPPUNIT_ASSERT (Glib::file_test (test_data_file1, Glib::FILE_TEST_EXISTS));
71
72         XMLNode* foo = new XMLNode ("test");
73         property.get_changes_as_xml (foo);
74         write_automation_list_xml (foo, test_data_filename);
75         check_xml (foo, test_data_file1, ignore_properties);
76
77         test_data_filename = "automation_list_property_test2.ref";
78         std::string test_data_file2 = Glib::build_filename (test_search_path().front(), test_data_filename);
79         CPPUNIT_ASSERT (Glib::file_test (test_data_file2, Glib::FILE_TEST_EXISTS));
80
81         /* Do some more */
82         property.clear_changes ();
83         CPPUNIT_ASSERT_EQUAL (false, property.changed());
84         property->add (5, 6);
85         property->add (7, 8);
86         CPPUNIT_ASSERT_EQUAL (true, property.changed());
87         foo = new XMLNode ("test");
88         property.get_changes_as_xml (foo);
89         write_automation_list_xml (foo, test_data_filename);
90         check_xml (foo, test_data_file2, ignore_properties);
91 }
92
93 /** Here's a StatefulDestructible class that has a AutomationListProperty */
94 class Fred : public StatefulDestructible
95 {
96 public:
97         Fred ()
98                 : _jim (_descriptor, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation))))
99
100         {
101                 add_property (_jim);
102         }
103
104         XMLNode & get_state () {
105                 XMLNode* n = new XMLNode ("State");
106                 add_properties (*n);
107                 return *n;
108         }
109
110         int set_state (XMLNode const & node, int) {
111                 set_values (node);
112                 return 0;
113         }
114
115         static void make_property_quarks () {
116                 _descriptor.property_id = g_quark_from_static_string ("FadeIn");
117         }
118
119         AutomationListProperty _jim;
120         static PropertyDescriptor<boost::shared_ptr<AutomationList> > _descriptor;
121 };
122
123 PropertyDescriptor<boost::shared_ptr<AutomationList> > Fred::_descriptor;
124
125 void
126 AutomationListPropertyTest::undoTest ()
127 {
128         list<string> ignore_properties;
129         ignore_properties.push_back ("id");
130
131         Fred::make_property_quarks ();
132
133         boost::shared_ptr<Fred> sheila (new Fred);
134
135         /* Add some data */
136         sheila->_jim->add (1, 2);
137         sheila->_jim->add (3, 4);
138
139         /* Do a `command' */
140         sheila->clear_changes ();
141         sheila->_jim->add (5, 6);
142         sheila->_jim->add (7, 8);
143         StatefulDiffCommand sdc (sheila);
144
145         std::string test_data_filename = "automation_list_property_test3.ref";
146         std::string test_data_file3 = Glib::build_filename (test_search_path().front(), test_data_filename);
147         CPPUNIT_ASSERT (Glib::file_test (test_data_file3, Glib::FILE_TEST_EXISTS));
148
149         /* Undo */
150         sdc.undo ();
151         write_automation_list_xml (&sheila->get_state(), test_data_filename);
152         check_xml (&sheila->get_state(), test_data_file3, ignore_properties);
153
154         test_data_filename = "automation_list_property_test4.ref";
155         std::string test_data_file4 = Glib::build_filename (test_search_path().front(), test_data_filename);
156         CPPUNIT_ASSERT (Glib::file_test (test_data_file4, Glib::FILE_TEST_EXISTS));
157
158         /* Redo */
159         sdc.redo ();
160         write_automation_list_xml (&sheila->get_state(), test_data_filename);
161         check_xml (&sheila->get_state(), test_data_file4, ignore_properties);
162 }