File size: 12,835 Bytes
6bcb42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* eslint-env jest */
import monitorLayoutReducer, {
    addMonitorRect, moveMonitorRect,
    resizeMonitorRect, removeMonitorRect,
    getInitialPosition, PADDING, SCREEN_WIDTH, SCREEN_HEIGHT
} from '../../../src/reducers/monitor-layout';

test('initialState', () => {
    let defaultState;

    expect(monitorLayoutReducer(defaultState /* state */, {type: 'anything'} /* action */)).toBeDefined();
    expect(monitorLayoutReducer(defaultState /* state */, {type: 'anything'} /* action */).monitors).toBeDefined();
    expect(monitorLayoutReducer(defaultState /* state */, {type: 'anything'} /* action */).savedMonitorPositions)
        .toBeDefined();
});

test('addMonitorRect', () => {
    let defaultState;
    const monitorId = 1;
    const monitorId2 = 2;
    const upperStart = {x: 100, y: 100};
    const lowerEnd = {x: 200, y: 200};

    // Add a monitor rect
    const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(monitorId, upperStart, lowerEnd));
    expect(reduxState.monitors[monitorId]).toBeDefined();
    expect(reduxState.monitors[monitorId].upperStart).toEqual(upperStart);
    expect(reduxState.monitors[monitorId].lowerEnd).toEqual(lowerEnd);
    // Add monitor rect doesn't save position
    expect(reduxState.savedMonitorPositions[monitorId]).toBeUndefined();
    const reduxState2 = monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 0, 0));

    // Add a second monitor rect
    const reduxState3 = monitorLayoutReducer(reduxState2, addMonitorRect(monitorId2, upperStart, lowerEnd));
    expect(reduxState3.monitors[monitorId]).toBeDefined();
    expect(reduxState3.monitors[monitorId2]).toBeDefined();
    expect(reduxState3.monitors[monitorId2].upperStart).toEqual(upperStart);
    expect(reduxState3.monitors[monitorId2].lowerEnd).toEqual(lowerEnd);
    // Saved positions aren't changed by adding monitor
    expect(reduxState3.savedMonitorPositions).toEqual(reduxState2.savedMonitorPositions);
});

test('addMonitorRectWithSavedPosition', () => {
    let defaultState;
    const monitorId = 1;
    const upperStart = {x: 100, y: 100};
    const lowerEnd = {x: 200, y: 200};

    // Add a monitor rect
    const reduxState = monitorLayoutReducer(defaultState,
        addMonitorRect(monitorId, upperStart, lowerEnd, true /* savePosition */));
    expect(reduxState.monitors[monitorId]).toBeDefined();
    expect(reduxState.monitors[monitorId].upperStart).toEqual(upperStart);
    expect(reduxState.monitors[monitorId].lowerEnd).toEqual(lowerEnd);
    // Save position
    expect(reduxState.savedMonitorPositions[monitorId].x).toEqual(upperStart.x);
    expect(reduxState.savedMonitorPositions[monitorId].y).toEqual(upperStart.y);
});

test('invalidRect', () => {
    let defaultState;
    const reduxState = monitorLayoutReducer(defaultState /* state */, {type: 'initialize'} /* action */);

    // Problem: x end is before x start
    expect(
        monitorLayoutReducer(reduxState,
            addMonitorRect(1, {x: 100, y: 100}, {x: 10, y: 200})))
        .toEqual(reduxState);

    // Problem: y end is before y start
    expect(
        monitorLayoutReducer(reduxState,
            addMonitorRect(1, {x: 100, y: 100}, {x: 200, y: 10})))
        .toEqual(reduxState);
});

test('invalidAddMonitorRect', () => {
    let defaultState;
    const monitorId = 1;
    const upperStart = {x: 100, y: 100};
    const lowerEnd = {x: 200, y: 200};

    // Add a monitor rect
    const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(monitorId, upperStart, lowerEnd));
    // Try to add the same one
    expect(monitorLayoutReducer(reduxState, addMonitorRect(monitorId, upperStart, lowerEnd)))
        .toEqual(reduxState);
});

test('moveMonitorRect', () => {
    let defaultState;
    const monitorId = 1;
    const monitorId2 = 2;
    const width = 102;
    const height = 101;
    const upperStart = {x: 100, y: 100};
    const lowerEnd = {x: upperStart.x + width, y: upperStart.y + height};
    const movedToPosition = {x: 0, y: 0};
    const movedToPosition2 = {x: 543, y: 2};

    // Add a monitor rect and move it. Expect it to be in monitors state and saved positions.
    const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(monitorId, upperStart, lowerEnd));
    const reduxState2 = monitorLayoutReducer(reduxState,
        moveMonitorRect(monitorId, movedToPosition.x, movedToPosition.y));
    expect(reduxState2.monitors[monitorId]).toBeDefined();
    expect(reduxState2.monitors[monitorId].upperStart).toEqual(movedToPosition);
    expect(reduxState2.monitors[monitorId].lowerEnd.x).toEqual(movedToPosition.x + width);
    expect(reduxState2.monitors[monitorId].lowerEnd.y).toEqual(movedToPosition.y + height);
    expect(reduxState2.savedMonitorPositions[monitorId]).toBeDefined();
    expect(reduxState2.savedMonitorPositions[monitorId].x).toEqual(movedToPosition.x);
    expect(reduxState2.savedMonitorPositions[monitorId].y).toEqual(movedToPosition.y);

    // Add a second monitor rect and move it. Expect there to now be 2 saved positions.
    const reduxState3 = monitorLayoutReducer(reduxState2, addMonitorRect(monitorId2, upperStart, lowerEnd));
    const reduxState4 = monitorLayoutReducer(reduxState3,
        moveMonitorRect(monitorId2, movedToPosition2.x, movedToPosition2.y));
    expect(reduxState4.savedMonitorPositions[monitorId]).toEqual(reduxState2.savedMonitorPositions[monitorId]);
    expect(reduxState4.savedMonitorPositions[monitorId2].x).toEqual(movedToPosition2.x);
    expect(reduxState4.savedMonitorPositions[monitorId2].y).toEqual(movedToPosition2.y);
});

test('invalidMoveMonitorRect', () => {
    let defaultState;
    let reduxState = monitorLayoutReducer(defaultState, {type: 'initialize'} /* action */);
    const monitorId = 1;

    // Try to move a monitor rect that doesn't exist
    expect(monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 1 /* newX */, 1 /* newY */)))
        .toEqual(reduxState);

    // Add the monitor to move
    reduxState = monitorLayoutReducer(reduxState, addMonitorRect(monitorId, {x: 100, y: 100}, {x: 200, y: 200}));

    // Invalid newX
    expect(monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 'Oregon' /* newX */, 1 /* newY */)))
        .toEqual(reduxState);

    // Invalid newY
    expect(monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 1 /* newX */)))
        .toEqual(reduxState);
});

test('resizeMonitorRect', () => {
    let defaultState;
    const monitorId = 1;
    const upperStart = {x: 100, y: 100};
    const newWidth = 10;
    const newHeight = 20;

    // Add a monitor rect and resize it
    const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(monitorId, upperStart, {x: 200, y: 200}));
    const reduxState2 = monitorLayoutReducer(reduxState,
        resizeMonitorRect(monitorId, newWidth, newHeight));
    expect(reduxState2.monitors[monitorId]).toBeDefined();
    expect(reduxState2.monitors[monitorId].upperStart).toEqual(upperStart);
    expect(reduxState2.monitors[monitorId].lowerEnd.x).toEqual(upperStart.x + newWidth);
    expect(reduxState2.monitors[monitorId].lowerEnd.y).toEqual(upperStart.y + newHeight);
    // Saved positions aren't changed by resizing monitor
    expect(reduxState2.savedMonitorPositions).toEqual(reduxState.savedMonitorPositions);
});

test('invalidResizeMonitorRect', () => {
    let defaultState;
    let reduxState = monitorLayoutReducer(defaultState, {type: 'initialize'} /* action */);
    const monitorId = 1;

    // Try to resize a monitor rect that doesn't exist
    expect(monitorLayoutReducer(reduxState, resizeMonitorRect(monitorId, 1 /* newWidth */, 1 /* newHeight */)))
        .toEqual(reduxState);

    // Add the monitor to resize
    reduxState = monitorLayoutReducer(reduxState, addMonitorRect(monitorId, {x: 100, y: 100}, {x: 200, y: 200}));

    // Invalid newWidth
    expect(monitorLayoutReducer(reduxState, resizeMonitorRect(monitorId, 'Oregon' /* newWidth */, 1 /* newHeight */)))
        .toEqual(reduxState);

    // Invalid newHeight
    expect(monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 1 /* newWidth */)))
        .toEqual(reduxState);

    // newWidth < 0
    expect(monitorLayoutReducer(reduxState, resizeMonitorRect(monitorId, -1 /* newWidth */, 1 /* newHeight */)))
        .toEqual(reduxState);

    // newHeight < 0
    expect(monitorLayoutReducer(reduxState, resizeMonitorRect(monitorId, 1 /* newWidth */, -1 /* newHeight */)))
        .toEqual(reduxState);
});

test('removeMonitorRect', () => {
    let defaultState;
    const monitorId = 1;

    // Add a monitor rect, move it, and remove it
    const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(monitorId,
        {x: 100, y: 100},
        {x: 200, y: 200}
    ));
    const reduxState2 = monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 0, 0));
    const reduxState3 = monitorLayoutReducer(reduxState2, removeMonitorRect(monitorId));
    expect(reduxState3.monitors[monitorId]).toBeUndefined();
    // Check that saved positions aren't changed by removing monitor
    expect(reduxState3.savedMonitorPositions).toEqual(reduxState2.savedMonitorPositions);
});

test('invalidRemoveMonitorRect', () => {
    let defaultState;
    const reduxState = monitorLayoutReducer(defaultState, {type: 'initialize'} /* action */);

    // Try to remove a monitor rect that doesn't exist
    expect(monitorLayoutReducer(reduxState, resizeMonitorRect(1)))
        .toEqual(reduxState);
});

test('getInitialPosition_lineUpTopLeft', () => {
    let defaultState;
    const width = 100;
    const height = 200;
    // Add monitors to right and bottom, but there is a space in the top left
    let reduxState = monitorLayoutReducer(defaultState, addMonitorRect(1,
        {x: width + PADDING, y: 0},
        {x: 100, y: height}
    ));
    reduxState = monitorLayoutReducer(defaultState, addMonitorRect(2,
        {x: 0, y: height + PADDING},
        {x: width, y: 100}
    ));

    // Check that the added monitor appears in the space
    const rect = getInitialPosition(reduxState, 3, width, height);
    expect(rect.upperStart).toBeDefined();
    expect(rect.lowerEnd).toBeDefined();
    expect(rect.lowerEnd.x - rect.upperStart.x).toEqual(width);
    expect(rect.lowerEnd.y - rect.upperStart.y).toEqual(height);
    expect(rect.upperStart.x).toEqual(PADDING);
    expect(rect.upperStart.y).toEqual(PADDING);
});

test('getInitialPosition_savedPosition', () => {
    const monitorId = 1;
    const savedX = 100;
    const savedY = 200;
    const width = 7;
    const height = 8;
    const reduxState = {
        monitors: {},
        savedMonitorPositions: {[monitorId]: {x: savedX, y: savedY}}
    };

    // Check that initial position uses saved state
    const rect = getInitialPosition(reduxState, monitorId, width, height);
    expect(rect.upperStart).toBeDefined();
    expect(rect.lowerEnd).toBeDefined();
    expect(rect.lowerEnd.x - rect.upperStart.x).toEqual(width);
    expect(rect.lowerEnd.y - rect.upperStart.y).toEqual(height);
    expect(rect.upperStart.x).toEqual(savedX);
    expect(rect.upperStart.y).toEqual(savedY);
});

test('getInitialPosition_lineUpLeft', () => {
    let defaultState;
    const monitor1EndY = 60;
    // Add a monitor that takes up the upper left corner
    const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(1, {x: 0, y: 0}, {x: 100, y: monitor1EndY}));

    // Check that added monitor is under it and lines up left
    const rect = getInitialPosition(reduxState, 2, 20 /* width */, 20 /* height */);
    expect(rect.upperStart.y >= monitor1EndY + PADDING).toBeTruthy();
});

test('getInitialPosition_lineUpTop', () => {
    let defaultState;
    const monitor1EndX = 100;
    // Add a monitor that takes up the whole left side
    const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(1,
        {x: 0, y: 0},
        {x: monitor1EndX, y: SCREEN_HEIGHT}
    ));

    // Check that added monitor is to the right of it and lines up top
    const rect = getInitialPosition(reduxState, 2, 20 /* width */, 20 /* height */);
    expect(rect.upperStart.y).toEqual(PADDING);
    expect(rect.upperStart.x >= monitor1EndX + PADDING).toBeTruthy();
});

test('getInitialPosition_noRoom', () => {
    let defaultState;
    const width = 7;
    const height = 8;
    // Add a monitor that takes up the whole screen
    const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(1,
        {x: 0, y: 0},
        {x: SCREEN_WIDTH, y: SCREEN_HEIGHT}
    ));

    // Check that added monitor exists somewhere (we don't care where)
    const rect = getInitialPosition(reduxState, 2, width, height);
    expect(rect.upperStart).toBeDefined();
    expect(rect.lowerEnd.x - rect.upperStart.x).toEqual(width);
    expect(rect.lowerEnd.y - rect.upperStart.y).toEqual(height);
});