less verbose unit-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
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, false, false);
63         property->add (3, 4, false, false);
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, false, false);
85         property->add (7, 8, false, false);
86         CPPUNIT_ASSERT_EQUAL (true, property.changed());
87         delete foo;
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         delete foo;
93 }
94
95 /** Here's a StatefulDestructible class that has a AutomationListProperty */
96 class Fred : public StatefulDestructible
97 {
98 public:
99         Fred ()
100                 : _jim (_descriptor, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation))))
101
102         {
103                 add_property (_jim);
104         }
105
106         XMLNode & get_state () {
107                 XMLNode* n = new XMLNode ("State");
108                 add_properties (*n);
109                 return *n;
110         }
111
112         int set_state (XMLNode const & node, int) {
113                 set_values (node);
114                 return 0;
115         }
116
117         static void make_property_quarks () {
118                 _descriptor.property_id = g_quark_from_static_string ("FadeIn");
119         }
120
121         AutomationListProperty _jim;
122         static PropertyDescriptor<boost::shared_ptr<AutomationList> > _descriptor;
123 };
124
125 PropertyDescriptor<boost::shared_ptr<AutomationList> > Fred::_descriptor;
126
127 void
128 AutomationListPropertyTest::undoTest ()
129 {
130         list<string> ignore_properties;
131         ignore_properties.push_back ("id");
132
133         Fred::make_property_quarks ();
134
135         boost::shared_ptr<Fred> sheila (new Fred);
136
137         /* Add some data */
138         sheila->_jim->add (1, 2, false, false);
139         sheila->_jim->add (3, 4, false, false);
140
141         /* Do a `command' */
142         sheila->clear_changes ();
143         sheila->_jim->add (5, 6, false, false);
144         sheila->_jim->add (7, 8, false, false);
145         StatefulDiffCommand sdc (sheila);
146
147         std::string test_data_filename = "automation_list_property_test3.ref";
148         std::string test_data_file3 = Glib::build_filename (test_search_path().front(), test_data_filename);
149         CPPUNIT_ASSERT (Glib::file_test (test_data_file3, Glib::FILE_TEST_EXISTS));
150
151         /* Undo */
152         sdc.undo ();
153         write_automation_list_xml (&sheila->get_state(), test_data_filename);
154         check_xml (&sheila->get_state(), test_data_file3, ignore_properties);
155
156         test_data_filename = "automation_list_property_test4.ref";
157         std::string test_data_file4 = Glib::build_filename (test_search_path().front(), test_data_filename);
158         CPPUNIT_ASSERT (Glib::file_test (test_data_file4, Glib::FILE_TEST_EXISTS));
159
160         /* Redo */
161         sdc.redo ();
162         write_automation_list_xml (&sheila->get_state(), test_data_filename);
163         check_xml (&sheila->get_state(), test_data_file4, ignore_properties);
164 }