1 /*
2  * Copyright (C) 2020-2021 Alexandros Theodotou <alex at zrythm dot org>
3  *
4  * This file is part of Zrythm
5  *
6  * Zrythm is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Zrythm is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with Zrythm.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "zrythm-test-config.h"
21 
22 #include <math.h>
23 
24 #include "audio/automation_region.h"
25 #include "audio/tracklist.h"
26 #include "project.h"
27 #include "utils/flags.h"
28 #include "zrythm.h"
29 
30 #include "tests/helpers/project.h"
31 #include "tests/helpers/zrythm.h"
32 
test_new_track()33 #include <glib.h>
34 #include <locale.h>
35 
36 static void
37 create_automation_region (
38   int track_pos)
39 {
40   Track * track = TRACKLIST->tracks[track_pos];
41 
42   Position start, end;
43   position_set_to_bar (&start, 1);
44   position_set_to_bar (&end, 3);
45   ZRegion * region =
46     automation_region_new (
47       &start, &end,
48       track_get_name_hash (track), 0, 0);
49   AutomationTracklist * atl =
50     track_get_automation_tracklist (track);
51   track_add_region (
52     track, region, atl->ats[0], 0, F_GEN_NAME,
53     F_NO_PUBLISH_EVENTS);
54   arranger_object_select (
55     (ArrangerObject *) region, F_SELECT, F_NO_APPEND, F_NO_PUBLISH_EVENTS);
56   arranger_selections_action_perform_create (
57     (ArrangerSelections *) TL_SELECTIONS, NULL);
58 
59   AutomationPoint * ap =
60     automation_point_new_float (
61       0.1f, 0.1f, &start);
62   automation_region_add_ap (
63     region, ap, F_NO_PUBLISH_EVENTS);
64   arranger_object_select (
65     (ArrangerObject *) ap, F_SELECT, F_NO_APPEND, F_NO_PUBLISH_EVENTS);
66   arranger_selections_action_perform_create (
67     (ArrangerSelections *) AUTOMATION_SELECTIONS,
68     NULL);
69 }
70 
71 static void
72 test_swap_with_automation_regions ()
73 {
74   test_helper_zrythm_init ();
75 
76   track_create_with_action (
77     TRACK_TYPE_AUDIO, NULL, NULL, PLAYHEAD,
78     TRACKLIST->num_tracks, 1, NULL);
79 
80   create_automation_region (
81     TRACKLIST->num_tracks - 1);
82 
83   track_create_empty_with_action (
84     TRACK_TYPE_MIDI, NULL);
85 
86   create_automation_region (
87     TRACKLIST->num_tracks - 1);
88 
89   /* swap tracks */
90   Track * track1 =
91     TRACKLIST->tracks[TRACKLIST->num_tracks - 2];
92   Track * track2 =
93     TRACKLIST->tracks[TRACKLIST->num_tracks - 1];
94   track_select (
95     track2, F_SELECT, F_EXCLUSIVE,
96     F_NO_PUBLISH_EVENTS);
main(int argc,char * argv[])97   tracklist_selections_action_perform_move (
98     TRACKLIST_SELECTIONS,
99     PORT_CONNECTIONS_MGR, track1->pos, NULL);
100 
101   undo_manager_undo (UNDO_MANAGER, NULL);
102   undo_manager_undo (UNDO_MANAGER, NULL);
103   undo_manager_undo (UNDO_MANAGER, NULL);
104   undo_manager_undo (UNDO_MANAGER, NULL);
105   undo_manager_undo (UNDO_MANAGER, NULL);
106   undo_manager_undo (UNDO_MANAGER, NULL);
107   undo_manager_undo (UNDO_MANAGER, NULL);
108 
109   test_project_save_and_reload ();
110 
111   undo_manager_redo (UNDO_MANAGER, NULL);
112   undo_manager_redo (UNDO_MANAGER, NULL);
113   undo_manager_redo (UNDO_MANAGER, NULL);
114   undo_manager_redo (UNDO_MANAGER, NULL);
115   undo_manager_redo (UNDO_MANAGER, NULL);
116   undo_manager_redo (UNDO_MANAGER, NULL);
117   undo_manager_redo (UNDO_MANAGER, NULL);
118 
119   test_helper_zrythm_cleanup ();
120 }
121 
122 int
123 main (int argc, char *argv[])
124 {
125   g_test_init (&argc, &argv, NULL);
126 
127   yaml_set_log_level (CYAML_LOG_INFO);
128 
129 #define TEST_PREFIX "/audio/tracklist/"
130 
131   g_test_add_func (
132     TEST_PREFIX "test swap with automation regions",
133     (GTestFunc) test_swap_with_automation_regions);
134 
135   return g_test_run ();
136 }
137