Fix build of evoral tests
[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 #include "test_common.h"
28
29 CPPUNIT_TEST_SUITE_REGISTRATION (AutomationListPropertyTest);
30
31 using namespace std;
32 using namespace PBD;
33 using namespace ARDOUR;
34
35 void
36 write_automation_list_xml (XMLNode* node, std::string filename)
37 {
38         // use the same output dir for all of them
39         static std::string test_output_dir = new_test_output_dir ("automation_list_property");
40         std::string output_file = Glib::build_filename (test_output_dir, filename);
41
42         CPPUNIT_ASSERT (write_ref (node, output_file));
43 }
44
45 void
46 AutomationListPropertyTest::basicTest ()
47 {
48         list<string> ignore_properties;
49         ignore_properties.push_back ("id");
50
51         PropertyDescriptor<boost::shared_ptr<AutomationList> > descriptor;
52         descriptor.property_id = g_quark_from_static_string ("FadeIn");
53         AutomationListProperty property (
54                 descriptor,
55                 boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))
56                 );
57
58         property.clear_changes ();
59
60         /* No change since we just cleared them */
61         CPPUNIT_ASSERT_EQUAL (false, property.changed());
62         
63         property->add (1, 2);
64         property->add (3, 4);
65
66         /* Now it has changed */
67         CPPUNIT_ASSERT_EQUAL (true, property.changed());
68
69         std::string test_data_filename = "automation_list_property_test1.ref";
70         std::string test_data_file1 = Glib::build_filename (test_search_path().front(), test_data_filename);
71         CPPUNIT_ASSERT (Glib::file_test (test_data_file1, Glib::FILE_TEST_EXISTS));
72
73         XMLNode* foo = new XMLNode ("test");
74         property.get_changes_as_xml (foo);
75         write_automation_list_xml (foo, test_data_filename);
76         check_xml (foo, test_data_file1, ignore_properties);
77
78         test_data_filename = "automation_list_property_test2.ref";
79         std::string test_data_file2 = Glib::build_filename (test_search_path().front(), test_data_filename);
80         CPPUNIT_ASSERT (Glib::file_test (test_data_file2, Glib::FILE_TEST_EXISTS));
81
82         /* Do some more */
83         property.clear_changes ();
84         CPPUNIT_ASSERT_EQUAL (false, property.changed());
85         property->add (5, 6);
86         property->add (7, 8);
87         CPPUNIT_ASSERT_EQUAL (true, property.changed());
88         foo = new XMLNode ("test");
89         property.get_changes_as_xml (foo);
90         write_automation_list_xml (foo, test_data_filename);
91         check_xml (foo, test_data_file2, ignore_properties);
92 }
93
94 /** Here's a StatefulDestructible class that has a AutomationListProperty */
95 class Fred : public StatefulDestructible
96 {
97 public:
98         Fred ()
99                 : _jim (_descriptor, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation))))
100
101         {
102                 add_property (_jim);
103         }
104
105         XMLNode & get_state () {
106                 XMLNode* n = new XMLNode ("State");
107                 add_properties (*n);
108                 return *n;
109         }
110
111         int set_state (XMLNode const & node, int) {
112                 set_values (node);
113                 return 0;
114         }
115
116         static void make_property_quarks () {
117                 _descriptor.property_id = g_quark_from_static_string ("FadeIn");
118         }
119
120         AutomationListProperty _jim;
121         static PropertyDescriptor<boost::shared_ptr<AutomationList> > _descriptor;
122 };
123
124 PropertyDescriptor<boost::shared_ptr<AutomationList> > Fred::_descriptor;
125
126 void
127 AutomationListPropertyTest::undoTest ()
128 {
129         list<string> ignore_properties;
130         ignore_properties.push_back ("id");
131
132         Fred::make_property_quarks ();
133
134         boost::shared_ptr<Fred> sheila (new Fred);
135
136         /* Add some data */
137         sheila->_jim->add (1, 2);
138         sheila->_jim->add (3, 4);
139
140         /* Do a `command' */
141         sheila->clear_changes ();
142         sheila->_jim->add (5, 6);
143         sheila->_jim->add (7, 8);
144         StatefulDiffCommand sdc (sheila);
145
146         std::string test_data_filename = "automation_list_property_test3.ref";
147         std::string test_data_file3 = Glib::build_filename (test_search_path().front(), test_data_filename);
148         CPPUNIT_ASSERT (Glib::file_test (test_data_file3, Glib::FILE_TEST_EXISTS));
149
150         /* Undo */
151         sdc.undo ();
152         write_automation_list_xml (&sheila->get_state(), test_data_filename);
153         check_xml (&sheila->get_state(), test_data_file3, ignore_properties);
154
155         test_data_filename = "automation_list_property_test4.ref";
156         std::string test_data_file4 = Glib::build_filename (test_search_path().front(), test_data_filename);
157         CPPUNIT_ASSERT (Glib::file_test (test_data_file4, Glib::FILE_TEST_EXISTS));
158
159         /* Redo */
160         sdc.redo ();
161         write_automation_list_xml (&sheila->get_state(), test_data_filename);
162         check_xml (&sheila->get_state(), test_data_file4, ignore_properties);
163 }