Add new SharedStatefulProperty which manages a shared_ptr to
[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 "pbd/properties.h"
20 #include "pbd/stateful_diff_command.h"
21 #include "ardour/automation_list.h"
22 #include "automation_list_property_test.h"
23 #include "test_util.h"
24
25 CPPUNIT_TEST_SUITE_REGISTRATION (AutomationListPropertyTest);
26
27 using namespace std;
28 using namespace PBD;
29 using namespace ARDOUR;
30
31 void
32 AutomationListPropertyTest::basicTest ()
33 {
34         PropertyDescriptor<boost::shared_ptr<AutomationList> > descriptor;
35         descriptor.property_id = g_quark_from_static_string ("FadeIn");
36         AutomationListProperty property (
37                 descriptor,
38                 boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))
39                 );
40
41         property.clear_changes ();
42
43         /* No change since we just cleared them */
44         CPPUNIT_ASSERT_EQUAL (false, property.changed());
45         
46         property->add (1, 2);
47         property->add (3, 4);
48
49         /* Now it has changed */
50         CPPUNIT_ASSERT_EQUAL (true, property.changed());
51
52         XMLNode* foo = new XMLNode ("test");
53         property.get_changes_as_xml (foo);
54         check_xml (foo, "../libs/ardour/test/data/automation_list_property_test1.ref");
55
56         /* Do some more */
57         property.clear_changes ();
58         CPPUNIT_ASSERT_EQUAL (false, property.changed());
59         property->add (5, 6);
60         property->add (7, 8);
61         CPPUNIT_ASSERT_EQUAL (true, property.changed());
62         foo = new XMLNode ("test");
63         property.get_changes_as_xml (foo);
64         check_xml (foo, "../libs/ardour/test/data/automation_list_property_test2.ref");
65 }
66
67 /** Here's a StatefulDestructible class that has a AutomationListProperty */
68 class Fred : public StatefulDestructible
69 {
70 public:
71         Fred ()
72                 : _jim (_descriptor, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation))))
73
74         {
75                 add_property (_jim);
76         }
77
78         XMLNode & get_state () {
79                 XMLNode* n = new XMLNode ("State");
80                 add_properties (*n);
81                 return *n;
82         }
83
84         int set_state (XMLNode const & node, int) {
85                 set_values (node);
86                 return 0;
87         }
88
89         static void make_property_quarks () {
90                 _descriptor.property_id = g_quark_from_static_string ("FadeIn");
91         }
92
93         AutomationListProperty _jim;
94         static PropertyDescriptor<boost::shared_ptr<AutomationList> > _descriptor;
95 };
96
97 PropertyDescriptor<boost::shared_ptr<AutomationList> > Fred::_descriptor;
98
99 void
100 AutomationListPropertyTest::undoTest ()
101 {
102         Fred::make_property_quarks ();
103
104         boost::shared_ptr<Fred> sheila (new Fred);
105
106         /* Add some data */
107         sheila->_jim->add (1, 2);
108         sheila->_jim->add (3, 4);
109
110         /* Do a `command' */
111         sheila->clear_changes ();
112         sheila->_jim->add (5, 6);
113         sheila->_jim->add (7, 8);
114         StatefulDiffCommand sdc (sheila);
115
116         /* Undo */
117         sdc.undo ();
118         check_xml (&sheila->get_state(), "../libs/ardour/test/data/automation_list_property_test3.ref");
119
120         /* Redo */
121         sdc.redo ();
122         check_xml (&sheila->get_state(), "../libs/ardour/test/data/automation_list_property_test4.ref");
123 }