1 /* GSequencer - Advanced GTK Sequencer
2  * Copyright (C) 2018 Joël Krähemann
3  *
4  * This file is part of GSequencer.
5  *
6  * GSequencer is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU 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  * GSequencer 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSequencer.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <glib.h>
21 #include <glib-object.h>
22 
23 #include <CUnit/CUnit.h>
24 #include <CUnit/Automated.h>
25 #include <CUnit/Basic.h>
26 
27 #include <ags/libags.h>
28 #include <ags/libags-audio.h>
29 
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
34 
35 int ags_osc_front_controller_test_init_suite();
36 int ags_osc_front_controller_test_clean_suite();
37 
38 void ags_osc_front_controller_test_add_message();
39 void ags_osc_front_controller_test_remove_message();
40 void ags_osc_front_controller_test_start_delegate();
41 void ags_osc_front_controller_test_stop_delegate();
42 void ags_osc_front_controller_test_do_request();
43 
44 #define AGS_OSC_FRONT_CONTROLLER_TEST_CONFIG "[generic]\n" \
45   "autosave-thread=false\n"			       \
46   "simple-file=false\n"				       \
47   "disable-feature=experimental\n"		       \
48   "segmentation=4/4\n"				       \
49   "\n"						       \
50   "[thread]\n"					       \
51   "model=super-threaded\n"			       \
52   "super-threaded-scope=channel\n"		       \
53   "lock-global=ags-thread\n"			       \
54   "lock-parent=ags-recycling-thread\n"		       \
55   "\n"						       \
56   "[soundcard-0]\n"				       \
57   "backend=alsa\n"                                     \
58   "device=default\n"				       \
59   "samplerate=44100\n"				       \
60   "buffer-size=1024\n"				       \
61   "pcm-channels=2\n"				       \
62   "dsp-channels=2\n"				       \
63   "format=16\n"					       \
64   "\n"						       \
65   "[recall]\n"					       \
66   "auto-sense=true\n"				       \
67   "\n"
68 
69 AgsApplicationContext *application_context;
70 
71 GObject *default_soundcard;
72 
73 /* The suite initialization function.
74  * Opens the temporary file used by the tests.
75  * Returns zero on success, non-zero otherwise.
76  */
77 int
ags_osc_front_controller_test_init_suite()78 ags_osc_front_controller_test_init_suite()
79 {
80   AgsConfig *config;
81 
82   GList *start_audio;
83 
84   ags_priority_load_defaults(ags_priority_get_instance());
85 
86   config = ags_config_get_instance();
87   ags_config_load_from_data(config,
88 			    AGS_OSC_FRONT_CONTROLLER_TEST_CONFIG,
89 			    strlen(AGS_OSC_FRONT_CONTROLLER_TEST_CONFIG));
90 
91   application_context = ags_audio_application_context_new();
92   g_object_ref(application_context);
93 
94   ags_application_context_prepare(application_context);
95   ags_application_context_setup(application_context);
96 
97   default_soundcard = ags_sound_provider_get_default_soundcard(AGS_SOUND_PROVIDER(application_context));
98 
99   return(0);
100 }
101 
102 /* The suite cleanup function.
103  * Closes the temporary file used by the tests.
104  * Returns zero on success, non-zero otherwise.
105  */
106 int
ags_osc_front_controller_test_clean_suite()107 ags_osc_front_controller_test_clean_suite()
108 {
109   return(0);
110 }
111 
112 void
ags_osc_front_controller_test_add_message()113 ags_osc_front_controller_test_add_message()
114 {
115   AgsOscFrontController *front_controller;
116 
117   AgsOscMessage *message;
118 
119   front_controller = ags_osc_front_controller_new();
120 
121   CU_ASSERT(front_controller->message == NULL);
122 
123   message = ags_osc_message_new();
124 
125   CU_ASSERT(message != NULL);
126 
127   message->immediately = TRUE;
128 
129   ags_osc_front_controller_add_message(front_controller,
130 				       message);
131 
132   CU_ASSERT(front_controller->message != NULL);
133 }
134 
135 void
ags_osc_front_controller_test_remove_message()136 ags_osc_front_controller_test_remove_message()
137 {
138   AgsOscFrontController *front_controller;
139 
140   AgsOscMessage *message;
141 
142   front_controller = ags_osc_front_controller_new();
143 
144   message = ags_osc_message_new();
145 
146   CU_ASSERT(message != NULL);
147 
148   message->immediately = TRUE;
149   front_controller->message = g_list_prepend(front_controller->message,
150 					     message);
151 
152   ags_osc_front_controller_remove_message(front_controller,
153 					  message);
154 
155   CU_ASSERT(front_controller->message == NULL);
156 }
157 
158 void
ags_osc_front_controller_test_start_delegate()159 ags_osc_front_controller_test_start_delegate()
160 {
161   AgsOscServer *server;
162 
163   AgsOscFrontController *front_controller;
164 
165   server = ags_osc_server_new();
166 
167   front_controller = ags_osc_front_controller_new();
168 
169   g_object_set(front_controller,
170 	       "osc-server", server,
171 	       NULL);
172 
173   ags_osc_front_controller_start_delegate(front_controller);
174 
175   CU_ASSERT(ags_osc_front_controller_test_flags(front_controller, AGS_OSC_FRONT_CONTROLLER_DELEGATE_STARTED) == TRUE);
176 }
177 
178 void
ags_osc_front_controller_test_stop_delegate()179 ags_osc_front_controller_test_stop_delegate()
180 {
181   AgsOscServer *server;
182   AgsOscFrontController *front_controller;
183 
184   server = ags_osc_server_new();
185 
186   front_controller = ags_osc_front_controller_new();
187 
188   g_object_set(front_controller,
189 	       "osc-server", server,
190 	       NULL);
191 
192   ags_osc_front_controller_start_delegate(front_controller);
193 
194   CU_ASSERT(ags_osc_front_controller_test_flags(front_controller, AGS_OSC_FRONT_CONTROLLER_DELEGATE_STARTED) == TRUE);
195 
196   while(!ags_osc_front_controller_test_flags(front_controller, AGS_OSC_FRONT_CONTROLLER_DELEGATE_RUNNING)){
197     sleep(1);
198   }
199 
200   ags_osc_front_controller_stop_delegate(front_controller);
201 
202   CU_ASSERT(ags_osc_front_controller_test_flags(front_controller, AGS_OSC_FRONT_CONTROLLER_DELEGATE_STARTED) == FALSE);
203 }
204 
205 void
ags_osc_front_controller_test_do_request()206 ags_osc_front_controller_test_do_request()
207 {
208   //TODO:JK: implement me
209 }
210 
211 int
main(int argc,char ** argv)212 main(int argc, char **argv)
213 {
214   CU_pSuite pSuite = NULL;
215 
216   putenv("LC_ALL=C");
217   putenv("LANG=C");
218 
219   putenv("LADSPA_PATH=\"\"");
220   putenv("DSSI_PATH=\"\"");
221   putenv("LV2_PATH=\"\"");
222 
223   /* initialize the CUnit test registry */
224   if(CUE_SUCCESS != CU_initialize_registry()){
225     return CU_get_error();
226   }
227 
228   /* add a suite to the registry */
229   pSuite = CU_add_suite("AgsOscFrontControllerTest", ags_osc_front_controller_test_init_suite, ags_osc_front_controller_test_clean_suite);
230 
231   if(pSuite == NULL){
232     CU_cleanup_registry();
233 
234     return CU_get_error();
235   }
236 
237   /* add the tests to the suite */
238   if((CU_add_test(pSuite, "test of AgsOscFrontController add message", ags_osc_front_controller_test_add_message) == NULL) ||
239      (CU_add_test(pSuite, "test of AgsOscFrontController remove message", ags_osc_front_controller_test_remove_message) == NULL) ||
240      (CU_add_test(pSuite, "test of AgsOscFrontController start delegate", ags_osc_front_controller_test_start_delegate) == NULL) ||
241      (CU_add_test(pSuite, "test of AgsOscFrontController stop delegate", ags_osc_front_controller_test_stop_delegate) == NULL) ||
242      (CU_add_test(pSuite, "test of AgsOscFrontController do request", ags_osc_front_controller_test_do_request) == NULL)){
243     CU_cleanup_registry();
244 
245     return CU_get_error();
246   }
247 
248   /* Run all tests using the CUnit Basic interface */
249   CU_basic_set_mode(CU_BRM_VERBOSE);
250   CU_basic_run_tests();
251 
252   CU_cleanup_registry();
253 
254   return(CU_get_error());
255 }
256 
257