~mixxxdevelopers/mixxx/trunk

« back to all changes in this revision

Viewing changes to mixxx/src/test/controllerengine_test.cpp

Merging features_controllerAbstraction. Migration of mappings from .mixxx/midi/ to controllers/ only works on a version upgrade (end-users.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 
8
8
#include "controlobject.h"
9
9
#include "configobject.h"
10
 
#include "midi/midiscriptengine.h"
 
10
#include "controllers/controllerengine.h"
11
11
 
12
12
namespace {
13
13
 
14
 
class MidiScriptEngineTest : public testing::Test {
 
14
class ControllerEngineTest : public testing::Test {
15
15
  protected:
16
16
    virtual void SetUp() {
17
17
        qDebug() << "SetUp";
18
18
        static int argc = 1;
19
19
        static char** argv = NULL;
20
20
        app = new QApplication(argc, argv);
21
 
        MidiDevice* pDevice = NULL;
22
 
        scriptEngine = new MidiScriptEngine(pDevice);
23
 
        scriptEngine->setMidiDebug(false);
24
 
        scriptEngine->setMidiPopups(false);
25
 
        scriptEngine->moveToThread(scriptEngine);
26
 
        scriptEngine->start();
27
 
        while(!scriptEngine->isReady()) { }
 
21
        Controller* pController = NULL;
 
22
        cEngine = new ControllerEngine(pController);
 
23
        cEngine->setDebug(true);
 
24
        cEngine->setPopups(false);
28
25
    }
29
26
 
30
27
    virtual void TearDown() {
31
28
        qDebug() << "TearDown";
32
 
        scriptEngine->gracefulShutdown(QList<QString>());
33
 
        scriptEngine->wait();
34
 
        delete scriptEngine;
 
29
        cEngine->gracefulShutdown();
 
30
        delete cEngine;
35
31
        delete app;
36
32
    }
37
33
 
38
34
    QApplication *app;
39
 
    MidiScriptEngine *scriptEngine;
 
35
    ControllerEngine *cEngine;
40
36
};
41
37
 
42
 
TEST_F(MidiScriptEngineTest, commonScriptHasNoErrors) {
 
38
TEST_F(ControllerEngineTest, commonScriptHasNoErrors) {
43
39
    // ConfigObject<ConfigValue> config("~/.mixxx/mixxx.cfg");
44
40
    // QString commonScript = config.getConfigPath() + "/" +
45
41
    //         "/midi/midi-mappings-scripts.js";
46
 
    QString commonScript = "./res/midi/midi-mappings-scripts.js";
47
 
    scriptEngine->evaluate(commonScript);
48
 
    EXPECT_FALSE(scriptEngine->hasErrors(commonScript));
 
42
    QString commonScript = "./res/controllers/common-controller-scripts.js";
 
43
    cEngine->evaluate(commonScript);
 
44
    EXPECT_FALSE(cEngine->hasErrors(commonScript));
49
45
}
50
46
 
51
 
TEST_F(MidiScriptEngineTest, scriptSetValue) {
 
47
TEST_F(ControllerEngineTest, scriptSetValue) {
52
48
    QString script = "test.js";
53
49
    QFile f(script);
54
50
    f.open(QIODevice::ReadWrite | QIODevice::Truncate);
55
51
    f.write("setValue = function() { engine.setValue('[Channel1]', 'co', 1.0); }\n");
56
52
    f.close();
57
53
 
58
 
    scriptEngine->evaluate(script);;
59
 
    EXPECT_FALSE(scriptEngine->hasErrors(script));
 
54
    cEngine->evaluate(script);
 
55
    EXPECT_FALSE(cEngine->hasErrors(script));
60
56
 
61
57
    ControlObject *co = new ControlObject(ConfigKey("[Channel1]", "co"));
62
58
    co->set(0.0);
63
 
    scriptEngine->execute("setValue");
 
59
    cEngine->execute("setValue");
64
60
    ControlObject::sync();
65
61
    EXPECT_DOUBLE_EQ(co->get(), 1.0f);
66
62
 
70
66
    f.remove();
71
67
}
72
68
 
73
 
TEST_F(MidiScriptEngineTest, scriptGetSetValue) {
 
69
TEST_F(ControllerEngineTest, scriptGetSetValue) {
74
70
    QString script = "test.js";
75
71
    QFile f(script);
76
72
    f.open(QIODevice::ReadWrite | QIODevice::Truncate);
77
73
    f.write("getSetValue = function() { var val = engine.getValue('[Channel1]', 'co'); engine.setValue('[Channel1]', 'co', val + 1); }\n");
78
74
    f.close();
79
75
 
80
 
    scriptEngine->evaluate(script);;
81
 
    EXPECT_FALSE(scriptEngine->hasErrors(script));
 
76
    cEngine->evaluate(script);
 
77
    EXPECT_FALSE(cEngine->hasErrors(script));
82
78
 
83
79
    ControlObject *co = new ControlObject(ConfigKey("[Channel1]", "co"));
84
80
    co->set(0.0);
85
 
    scriptEngine->execute("getSetValue");
 
81
    cEngine->execute("getSetValue");
86
82
    ControlObject::sync();
87
83
    EXPECT_DOUBLE_EQ(co->get(), 1.0f);
88
84
 
92
88
    f.remove();
93
89
}
94
90
 
 
91
TEST_F(ControllerEngineTest, setInvalidControlObject) {
 
92
    QString script = "test.js";
 
93
    QFile f(script);
 
94
    f.open(QIODevice::ReadWrite | QIODevice::Truncate);
 
95
    f.write("setValue = function() { engine.setValue('[Nothing]', 'nothing', 1.0); }\n");
 
96
    f.close();
 
97
 
 
98
    cEngine->evaluate(script);
 
99
    EXPECT_FALSE(cEngine->hasErrors(script));
 
100
 
 
101
    EXPECT_TRUE(cEngine->execute("setValue"));
 
102
 
 
103
    f.remove();
 
104
}
 
105
 
 
106
TEST_F(ControllerEngineTest, getInvalidControlObject) {
 
107
    QString script = "test.js";
 
108
    QFile f(script);
 
109
    f.open(QIODevice::ReadWrite | QIODevice::Truncate);
 
110
    f.write("getValue = function() { return engine.getValue('[Nothing]', 'nothing'); }\n");
 
111
    f.close();
 
112
    
 
113
    cEngine->evaluate(script);
 
114
    EXPECT_FALSE(cEngine->hasErrors(script));
 
115
 
 
116
    EXPECT_TRUE(cEngine->execute("getValue"));
 
117
    
 
118
    f.remove();
 
119
}
 
120
 
 
121
TEST_F(ControllerEngineTest, automaticReaction) {
 
122
    QString script = "test.js";
 
123
    QFile f(script);
 
124
    f.open(QIODevice::ReadWrite | QIODevice::Truncate);
 
125
    f.write("setUp = function() { engine.connectControl('[Channel1]','co','reaction'); }\n");
 
126
    f.write("reaction = function(value) { if (value == 2.5) print('TEST PASSED: '+value);\
 
127
                                          else print('TEST FAILED!  TEST FAILED!  TEST FAILED: '+value);\
 
128
                                          return value; }\n");
 
129
    f.close();
 
130
 
 
131
    cEngine->evaluate(script);
 
132
    EXPECT_FALSE(cEngine->hasErrors(script));
 
133
 
 
134
    ControlObject *co = new ControlObject(ConfigKey("[Channel1]", "co"));
 
135
    co->set(0.0);
 
136
    EXPECT_TRUE(cEngine->execute("setUp"));
 
137
    ControlObject::sync();
 
138
 
 
139
    // The actual test
 
140
    //  TODO: Have the JS call a function in this test class so the test framework
 
141
    //  can tell if it actually passed or not
 
142
    co->set(2.5);
 
143
    ControlObject::sync();
 
144
 
 
145
    f.remove();
 
146
}
 
147
 
 
148
// Can't test timed reactions without an event loop!
 
149
/*
 
150
TEST_F(ControllerEngineTest, oneShotTimedReaction) {
 
151
    QString script = "test.js";
 
152
    QFile f(script);
 
153
    f.open(QIODevice::ReadWrite | QIODevice::Truncate);
 
154
    f.write("function global() {}\n");
 
155
    f.write("setUp = function() { global.date = new Date(); engine.beginTimer(250,'reaction()',true); }\n");
 
156
    f.write("reaction = function() { if ((new Date()-global.date) == 250) print('TEST PASSED');\
 
157
                                     else print('TEST FAILED!  TEST FAILED!  TEST FAILED: '+(new Date()-global.date));\
 
158
                                     return (new Date()-global.date); }\n");
 
159
    f.close();
 
160
    
 
161
    cEngine->evaluate(script);
 
162
    EXPECT_FALSE(cEngine->hasErrors(script));
 
163
 
 
164
    EXPECT_TRUE(cEngine->execute("setUp"));
 
165
    usleep(500*1000);
 
166
 
 
167
    // Test passes if the JS prints it after 250ms
 
168
    //  TODO: Have the JS call a function in this test class
 
169
 
 
170
    f.remove();
 
171
}
 
172
*/
 
173
 
95
174
}