2
#include <gtest/gtest.h>
4
#include <QApplication>
9
#include "controlobject.h"
10
#include "controlpotmeter.h"
11
#include "configobject.h"
12
#include "controllers/controllerengine.h"
16
class ControllerEngineTest : public testing::Test {
18
virtual void SetUp() {
22
static char* argv[2] = { "test", NULL };
23
QThread::currentThread()->setObjectName("Main");
24
app = new QApplication(argc, argv);
25
new ControlPotmeter(ConfigKey("[Test]", "potmeter"),-1.,1.);
26
Controller* pController = NULL;
27
cEngine = new ControllerEngine(pController);
28
cEngine->setDebug(true);
29
cEngine->setPopups(false);
32
virtual void TearDown() {
33
qDebug() << "TearDown";
34
cEngine->gracefulShutdown();
40
ControllerEngine *cEngine;
43
TEST_F(ControllerEngineTest, commonScriptHasNoErrors) {
44
// ConfigObject<ConfigValue> config("~/.mixxx/mixxx.cfg");
45
// QString commonScript = config.getConfigPath() + "/" +
46
// "/midi/midi-mappings-scripts.js";
47
QString commonScript = "./res/controllers/common-controller-scripts.js";
48
cEngine->evaluate(commonScript);
49
EXPECT_FALSE(cEngine->hasErrors(commonScript));
52
TEST_F(ControllerEngineTest, scriptSetValue) {
53
QString script = "test.js";
55
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
56
f.write("setValue = function() { engine.setValue('[Channel1]', 'co', 1.0); }\n");
59
cEngine->evaluate(script);
60
EXPECT_FALSE(cEngine->hasErrors(script));
62
ControlObject *co = new ControlObject(ConfigKey("[Channel1]", "co"));
64
cEngine->execute("setValue");
65
ControlObject::sync();
66
EXPECT_DOUBLE_EQ(co->get(), 1.0f);
74
TEST_F(ControllerEngineTest, scriptGetSetValue) {
75
QString script = "test.js";
77
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
78
f.write("getSetValue = function() { var val = engine.getValue('[Channel1]', 'co'); engine.setValue('[Channel1]', 'co', val + 1); }\n");
81
cEngine->evaluate(script);
82
EXPECT_FALSE(cEngine->hasErrors(script));
84
ControlObject *co = new ControlObject(ConfigKey("[Channel1]", "co"));
86
cEngine->execute("getSetValue");
87
ControlObject::sync();
88
EXPECT_DOUBLE_EQ(co->get(), 1.0f);
96
TEST_F(ControllerEngineTest, scriptConnectDisconnectControlNamedFunction) {
97
QString script = "test.js";
99
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
101
"var executed = false;\n"
103
"testConnectDisconnectControlCallback = function() {\n"
104
" executed = true;\n"
106
"testConnectDisconnectControl = function() { \n"
107
" connection = engine.connectControl('[Test]', 'potmeter', \n"
108
" 'testConnectDisconnectControlCallback');\n"
109
" engine.trigger('[Test]', 'potmeter');\n"
112
"checkConnectDisconnectControl = function() {\n"
113
" connection.disconnect();\n"
114
" if (!executed) {\n"
115
" throw 'Did Not Execute Callback';\n"
117
" return executed;\n"
122
cEngine->evaluate(script);
123
EXPECT_FALSE(cEngine->hasErrors(script));
125
EXPECT_TRUE(cEngine->execute("testConnectDisconnectControl"));
126
EXPECT_TRUE(cEngine->execute("checkConnectDisconnectControl"));
131
TEST_F(ControllerEngineTest, scriptConnectDisconnectControlClosure) {
132
QString script = "test.js";
134
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
136
"var executed = false;\n"
138
"testConnectDisconnectControl = function() { \n"
139
" connection = engine.connectControl('[Test]', 'potmeter', \n"
140
" function() { executed = true; }\n"
142
" engine.trigger('[Test]', 'potmeter');\n"
145
"checkConnectDisconnectControl = function() {\n"
146
" connection.disconnect();\n"
147
" if (!executed) {\n"
148
" throw 'Did Not Execute Callback';\n"
150
" return executed;\n"
155
cEngine->evaluate(script);
156
EXPECT_FALSE(cEngine->hasErrors(script));
158
EXPECT_TRUE(cEngine->execute("testConnectDisconnectControl"));
159
EXPECT_TRUE(cEngine->execute("checkConnectDisconnectControl"));
164
TEST_F(ControllerEngineTest, scriptConnectDisconnectControlIsDisconnected) {
165
QString script = "test.js";
167
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
169
"var executed = false;\n"
171
"testConnectDisconnectControl = function() { \n"
172
" connection = engine.connectControl('[Test]', 'potmeter', \n"
173
" function() { executed = true; }\n"
175
" if (typeof connection == 'undefined')\n"
176
" throw 'Unable to Connect controller';\n"
177
" connection.disconnect();\n"
178
" engine.trigger('[Test]', 'potmeter');\n"
181
"checkConnectDisconnectControl = function() {\n"
183
" throw 'Callback was executed';\n"
185
" return executed==false;\n"
190
cEngine->evaluate(script);
191
EXPECT_FALSE(cEngine->hasErrors(script));
193
EXPECT_TRUE(cEngine->execute("testConnectDisconnectControl"));
194
EXPECT_TRUE(cEngine->execute("checkConnectDisconnectControl"));
199
TEST_F(ControllerEngineTest, scriptConnectDisconnectControlIsDisconnectedByName) {
200
QString script = "test.js";
202
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
204
"var executed = false;\n"
206
"connectionCallback = function() { executed = true; }\n"
207
"testConnectDisconnectControl = function() { \n"
208
" connection = engine.connectControl('[Test]', 'potmeter', 'connectionCallback');\n"
209
" if (typeof connection == 'undefined')\n"
210
" throw 'Unable to Connect controller';\n"
211
" engine.connectControl('[Test]', 'potmeter', 'connectionCallback', 1);\n"
212
" engine.trigger('[Test]', 'potmeter');\n"
215
"checkConnectDisconnectControl = function() {\n"
217
" throw 'Callback was executed';\n"
219
" return executed==false;\n"
224
cEngine->evaluate(script);
225
EXPECT_FALSE(cEngine->hasErrors(script));
227
EXPECT_TRUE(cEngine->execute("testConnectDisconnectControl"));
228
EXPECT_TRUE(cEngine->execute("checkConnectDisconnectControl"));
233
TEST_F(ControllerEngineTest, scriptConnectDisconnectControlIsDisconnectedByObject) {
234
QString script = "test.js";
236
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
238
"var executed = false;\n"
240
"testConnectDisconnectControl = function() { \n"
241
" connection = engine.connectControl('[Test]', 'potmeter', \n"
242
" function() { executed = true; }\n"
244
" if (typeof connection == 'undefined')\n"
245
" throw 'Unable to Connect controller';\n"
246
" engine.connectControl('[Test]', 'potmeter', connection, 1);\n"
247
" engine.trigger('[Test]', 'potmeter');\n"
250
"checkConnectDisconnectControl = function() {\n"
252
" throw 'Callback was executed';\n"
254
" return executed==false;\n"
259
cEngine->evaluate(script);
260
EXPECT_FALSE(cEngine->hasErrors(script));
262
EXPECT_TRUE(cEngine->execute("testConnectDisconnectControl"));
263
EXPECT_TRUE(cEngine->execute("checkConnectDisconnectControl"));
268
TEST_F(ControllerEngineTest, setInvalidControlObject) {
269
QString script = "test.js";
271
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
272
f.write("setValue = function() { engine.setValue('[Nothing]', 'nothing', 1.0); }\n");
275
cEngine->evaluate(script);
276
EXPECT_FALSE(cEngine->hasErrors(script));
278
EXPECT_TRUE(cEngine->execute("setValue"));
283
TEST_F(ControllerEngineTest, getInvalidControlObject) {
284
QString script = "test.js";
286
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
287
f.write("getValue = function() { return engine.getValue('[Nothing]', 'nothing'); }\n");
290
cEngine->evaluate(script);
291
EXPECT_FALSE(cEngine->hasErrors(script));
293
EXPECT_TRUE(cEngine->execute("getValue"));
298
TEST_F(ControllerEngineTest, automaticReaction) {
299
QString script = "test.js";
301
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
302
f.write("setUp = function() { engine.connectControl('[Channel1]','co','reaction'); }\n");
303
f.write("reaction = function(value) { if (value == 2.5) print('TEST PASSED: '+value);\
304
else print('TEST FAILED! TEST FAILED! TEST FAILED: '+value);\
308
cEngine->evaluate(script);
309
EXPECT_FALSE(cEngine->hasErrors(script));
311
ControlObject *co = new ControlObject(ConfigKey("[Channel1]", "co"));
313
EXPECT_TRUE(cEngine->execute("setUp"));
314
ControlObject::sync();
317
// TODO: Have the JS call a function in this test class so the test framework
318
// can tell if it actually passed or not
320
ControlObject::sync();
325
// Can't test timed reactions without an event loop!
327
TEST_F(ControllerEngineTest, oneShotTimedReaction) {
328
QString script = "test.js";
330
f.open(QIODevice::ReadWrite | QIODevice::Truncate);
331
f.write("function global() {}\n");
332
f.write("setUp = function() { global.date = new Date(); engine.beginTimer(250,'reaction()',true); }\n");
333
f.write("reaction = function() { if ((new Date()-global.date) == 250) print('TEST PASSED');\
334
else print('TEST FAILED! TEST FAILED! TEST FAILED: '+(new Date()-global.date));\
335
return (new Date()-global.date); }\n");
338
cEngine->evaluate(script);
339
EXPECT_FALSE(cEngine->hasErrors(script));
341
EXPECT_TRUE(cEngine->execute("setUp"));
344
// Test passes if the JS prints it after 250ms
345
// TODO: Have the JS call a function in this test class