-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpadSpec.js
141 lines (113 loc) · 4.82 KB
/
padSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
describe('Launchpad', function(){
function cartesianProduct(as, bs) {
return as.map(function(a){
return bs.map(function(b){
return [a, b]
});
}).reduce(function(result, partialProduct){
return result.concat(partialProduct);
}, []);
};
var ANY_CHANNEL = 144;
var ANY_NOTE = 0;
var mockMidiAdapter;
beforeEach(function(){
mockMidiAdapter = new launchpad.Observable();
mockMidiAdapter.send = function(){
this.arguments = Array.prototype.slice.call(arguments);
};
});
it('should be Observable', function(){
var pad = new launchpad.Launchpad(mockMidiAdapter);
expect(pad instanceof launchpad.Observable).toBe(true);
});
describe('on [*, *, 127] midi message', function(){
it('pad should emit \'press\'', function(){
var pad = new launchpad.Launchpad(mockMidiAdapter);
var notified = false;
pad.on('press', function(){ notified = true; });
mockMidiAdapter.emit('input', ANY_CHANNEL, ANY_NOTE, 127);
expect(notified).toBe(true);
});
it('should emit corresponding button', function(){
var pad = new launchpad.Launchpad(mockMidiAdapter);
var button;
pad.on('press', function(aButton){ button = aButton; });
mockMidiAdapter.emit('input', ANY_CHANNEL, ANY_NOTE, 127);
expect(button).toBeDefined();
expect(button instanceof launchpad.Button).toBe(true);
expect(button.channel).toBe(ANY_CHANNEL);
expect(button.note).toBe(ANY_NOTE);
});
});
describe('on [*, *, 0] midi message', function(){
it('pad should emit \'release\'', function(){
var pad = new launchpad.Launchpad(mockMidiAdapter);
var notified = false;
pad.on('release', function(){ notified = true; });
mockMidiAdapter.emit('input', ANY_CHANNEL, ANY_NOTE, 0);
expect(notified).toBe(true);
});
it('should emit corresponding button', function(){
var pad = new launchpad.Launchpad(mockMidiAdapter);
var button;
pad.on('release', function(aButton){ button = aButton; });
mockMidiAdapter.emit('input', ANY_CHANNEL, ANY_NOTE, 0);
expect(button).toBeDefined();
expect(button instanceof launchpad.Button).toBe(true);
expect(button.channel).toBe(ANY_CHANNEL);
expect(button.note).toBe(ANY_NOTE);
});
});
describe('clear', function(){
it('should send [176, 0, 0] to midiAdapter', function(){
var pad = new launchpad.Launchpad(mockMidiAdapter);
pad.clear();
expect(mockMidiAdapter.arguments).toEqual([176, 0, 0]);
});
});
describe('button', function(){
it('should return a Button by regular button id', function(){
var pad = new launchpad.Launchpad(mockMidiAdapter);
[0, 1, 2, 3, 4, 5, 6, 7]
.map(function(n){ return 16*n; })
.map(function(m){
return [0, 1, 2, 3, 4, 5, 6, 7, 8].map(function(n){ return m + n; });
})
.reduce(function(result, ids){ return result.concat(ids); },[])
.map(function(id){ return { 'id': id, 'note': id }; })
.forEach(function(data){
var button = pad.button(data.id);
expect(button.channel).toBe(144);
expect(button.note).toBe(data.note);
});
});
it('should return a Button by coordinates', function(){
var pad = new launchpad.Launchpad(mockMidiAdapter);
cartesianProduct([0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8])
.map(function(product){
return {
'x': product[0],
'y': product[1]
}
})
.forEach(function(coordinates){
var button = pad.button(coordinates.x, coordinates.y);
expect(button.channel).toBe(144);
expect(button.note).toBe(coordinates.x + 16 * coordinates.y);
});
});
});
describe('controlButton', function(){
it('should return a Button by control button id', function(){
var pad = new launchpad.Launchpad(mockMidiAdapter);
[0, 1, 2, 3, 4, 5, 6, 7]
.map(function(id){ return { 'id': id, 'note': 104 + id }; })
.forEach(function(data){
var button = pad.controlButton(data.id);
expect(button.channel).toBe(176);
expect(button.note).toBe(data.note);
});
});
});
});