monitor-layout-reducer.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* eslint-env jest */
  2. import monitorLayoutReducer from '../../../src/reducers/monitor-layout';
  3. import {addMonitorRect, moveMonitorRect} from '../../../src/reducers/monitor-layout';
  4. import {resizeMonitorRect, removeMonitorRect} from '../../../src/reducers/monitor-layout';
  5. import {getInitialPosition, PADDING, SCREEN_WIDTH, SCREEN_HEIGHT} from '../../../src/reducers/monitor-layout';
  6. test('initialState', () => {
  7. let defaultState;
  8. expect(monitorLayoutReducer(defaultState /* state */, {type: 'anything'} /* action */)).toBeDefined();
  9. expect(monitorLayoutReducer(defaultState /* state */, {type: 'anything'} /* action */).monitors).toBeDefined();
  10. expect(monitorLayoutReducer(defaultState /* state */, {type: 'anything'} /* action */).savedMonitorPositions)
  11. .toBeDefined();
  12. });
  13. test('addMonitorRect', () => {
  14. let defaultState;
  15. const monitorId = 1;
  16. const monitorId2 = 2;
  17. const upperStart = {x: 100, y: 100};
  18. const lowerEnd = {x: 200, y: 200};
  19. // Add a monitor rect
  20. const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(monitorId, upperStart, lowerEnd));
  21. expect(reduxState.monitors[monitorId]).toBeDefined();
  22. expect(reduxState.monitors[monitorId].upperStart).toEqual(upperStart);
  23. expect(reduxState.monitors[monitorId].lowerEnd).toEqual(lowerEnd);
  24. // Add monitor rect doesn't save position
  25. expect(reduxState.savedMonitorPositions[monitorId]).toBeUndefined();
  26. const reduxState2 = monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 0, 0));
  27. // Add a second monitor rect
  28. const reduxState3 = monitorLayoutReducer(reduxState2, addMonitorRect(monitorId2, upperStart, lowerEnd));
  29. expect(reduxState3.monitors[monitorId]).toBeDefined();
  30. expect(reduxState3.monitors[monitorId2]).toBeDefined();
  31. expect(reduxState3.monitors[monitorId2].upperStart).toEqual(upperStart);
  32. expect(reduxState3.monitors[monitorId2].lowerEnd).toEqual(lowerEnd);
  33. // Saved positions aren't changed by adding monitor
  34. expect(reduxState3.savedMonitorPositions).toEqual(reduxState2.savedMonitorPositions);
  35. });
  36. test('addMonitorRectWithSavedPosition', () => {
  37. let defaultState;
  38. const monitorId = 1;
  39. const upperStart = {x: 100, y: 100};
  40. const lowerEnd = {x: 200, y: 200};
  41. // Add a monitor rect
  42. const reduxState = monitorLayoutReducer(defaultState,
  43. addMonitorRect(monitorId, upperStart, lowerEnd, true /* savePosition */));
  44. expect(reduxState.monitors[monitorId]).toBeDefined();
  45. expect(reduxState.monitors[monitorId].upperStart).toEqual(upperStart);
  46. expect(reduxState.monitors[monitorId].lowerEnd).toEqual(lowerEnd);
  47. // Save position
  48. expect(reduxState.savedMonitorPositions[monitorId].x).toEqual(upperStart.x);
  49. expect(reduxState.savedMonitorPositions[monitorId].y).toEqual(upperStart.y);
  50. });
  51. test('invalidRect', () => {
  52. let defaultState;
  53. const reduxState = monitorLayoutReducer(defaultState /* state */, {type: 'initialize'} /* action */);
  54. // Problem: x end is before x start
  55. expect(
  56. monitorLayoutReducer(reduxState,
  57. addMonitorRect(1, {x: 100, y: 100}, {x: 10, y: 200})))
  58. .toEqual(reduxState);
  59. // Problem: y end is before y start
  60. expect(
  61. monitorLayoutReducer(reduxState,
  62. addMonitorRect(1, {x: 100, y: 100}, {x: 200, y: 10})))
  63. .toEqual(reduxState);
  64. });
  65. test('invalidAddMonitorRect', () => {
  66. let defaultState;
  67. const monitorId = 1;
  68. const upperStart = {x: 100, y: 100};
  69. const lowerEnd = {x: 200, y: 200};
  70. // Add a monitor rect
  71. const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(monitorId, upperStart, lowerEnd));
  72. // Try to add the same one
  73. expect(monitorLayoutReducer(reduxState, addMonitorRect(monitorId, upperStart, lowerEnd)))
  74. .toEqual(reduxState);
  75. });
  76. test('moveMonitorRect', () => {
  77. let defaultState;
  78. const monitorId = 1;
  79. const monitorId2 = 2;
  80. const width = 102;
  81. const height = 101;
  82. const upperStart = {x: 100, y: 100};
  83. const lowerEnd = {x: upperStart.x + width, y: upperStart.y + height};
  84. const movedToPosition = {x: 0, y: 0};
  85. const movedToPosition2 = {x: 543, y: 2};
  86. // Add a monitor rect and move it. Expect it to be in monitors state and saved positions.
  87. const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(monitorId, upperStart, lowerEnd));
  88. const reduxState2 = monitorLayoutReducer(reduxState,
  89. moveMonitorRect(monitorId, movedToPosition.x, movedToPosition.y));
  90. expect(reduxState2.monitors[monitorId]).toBeDefined();
  91. expect(reduxState2.monitors[monitorId].upperStart).toEqual(movedToPosition);
  92. expect(reduxState2.monitors[monitorId].lowerEnd.x).toEqual(movedToPosition.x + width);
  93. expect(reduxState2.monitors[monitorId].lowerEnd.y).toEqual(movedToPosition.y + height);
  94. expect(reduxState2.savedMonitorPositions[monitorId]).toBeDefined();
  95. expect(reduxState2.savedMonitorPositions[monitorId].x).toEqual(movedToPosition.x);
  96. expect(reduxState2.savedMonitorPositions[monitorId].y).toEqual(movedToPosition.y);
  97. // Add a second monitor rect and move it. Expect there to now be 2 saved positions.
  98. const reduxState3 = monitorLayoutReducer(reduxState2, addMonitorRect(monitorId2, upperStart, lowerEnd));
  99. const reduxState4 = monitorLayoutReducer(reduxState3,
  100. moveMonitorRect(monitorId2, movedToPosition2.x, movedToPosition2.y));
  101. expect(reduxState4.savedMonitorPositions[monitorId]).toEqual(reduxState2.savedMonitorPositions[monitorId]);
  102. expect(reduxState4.savedMonitorPositions[monitorId2].x).toEqual(movedToPosition2.x);
  103. expect(reduxState4.savedMonitorPositions[monitorId2].y).toEqual(movedToPosition2.y);
  104. });
  105. test('invalidMoveMonitorRect', () => {
  106. let defaultState;
  107. let reduxState = monitorLayoutReducer(defaultState, {type: 'initialize'} /* action */);
  108. const monitorId = 1;
  109. // Try to move a monitor rect that doesn't exist
  110. expect(monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 1 /* newX */, 1 /* newY */)))
  111. .toEqual(reduxState);
  112. // Add the monitor to move
  113. reduxState = monitorLayoutReducer(reduxState, addMonitorRect(monitorId, {x: 100, y: 100}, {x: 200, y: 200}));
  114. // Invalid newX
  115. expect(monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 'Oregon' /* newX */, 1 /* newY */)))
  116. .toEqual(reduxState);
  117. // Invalid newY
  118. expect(monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 1 /* newX */)))
  119. .toEqual(reduxState);
  120. });
  121. test('resizeMonitorRect', () => {
  122. let defaultState;
  123. const monitorId = 1;
  124. const upperStart = {x: 100, y: 100};
  125. const newWidth = 10;
  126. const newHeight = 20;
  127. // Add a monitor rect and resize it
  128. const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(monitorId, upperStart, {x: 200, y: 200}));
  129. const reduxState2 = monitorLayoutReducer(reduxState,
  130. resizeMonitorRect(monitorId, newWidth, newHeight));
  131. expect(reduxState2.monitors[monitorId]).toBeDefined();
  132. expect(reduxState2.monitors[monitorId].upperStart).toEqual(upperStart);
  133. expect(reduxState2.monitors[monitorId].lowerEnd.x).toEqual(upperStart.x + newWidth);
  134. expect(reduxState2.monitors[monitorId].lowerEnd.y).toEqual(upperStart.y + newHeight);
  135. // Saved positions aren't changed by resizing monitor
  136. expect(reduxState2.savedMonitorPositions).toEqual(reduxState.savedMonitorPositions);
  137. });
  138. test('invalidResizeMonitorRect', () => {
  139. let defaultState;
  140. let reduxState = monitorLayoutReducer(defaultState, {type: 'initialize'} /* action */);
  141. const monitorId = 1;
  142. // Try to resize a monitor rect that doesn't exist
  143. expect(monitorLayoutReducer(reduxState, resizeMonitorRect(monitorId, 1 /* newWidth */, 1 /* newHeight */)))
  144. .toEqual(reduxState);
  145. // Add the monitor to resize
  146. reduxState = monitorLayoutReducer(reduxState, addMonitorRect(monitorId, {x: 100, y: 100}, {x: 200, y: 200}));
  147. // Invalid newWidth
  148. expect(monitorLayoutReducer(reduxState, resizeMonitorRect(monitorId, 'Oregon' /* newWidth */, 1 /* newHeight */)))
  149. .toEqual(reduxState);
  150. // Invalid newHeight
  151. expect(monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 1 /* newWidth */)))
  152. .toEqual(reduxState);
  153. // newWidth < 0
  154. expect(monitorLayoutReducer(reduxState, resizeMonitorRect(monitorId, -1 /* newWidth */, 1 /* newHeight */)))
  155. .toEqual(reduxState);
  156. // newHeight < 0
  157. expect(monitorLayoutReducer(reduxState, resizeMonitorRect(monitorId, 1 /* newWidth */, -1 /* newHeight */)))
  158. .toEqual(reduxState);
  159. });
  160. test('removeMonitorRect', () => {
  161. let defaultState;
  162. const monitorId = 1;
  163. // Add a monitor rect, move it, and remove it
  164. const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(monitorId,
  165. {x: 100, y: 100},
  166. {x: 200, y: 200}
  167. ));
  168. const reduxState2 = monitorLayoutReducer(reduxState, moveMonitorRect(monitorId, 0, 0));
  169. const reduxState3 = monitorLayoutReducer(reduxState2, removeMonitorRect(monitorId));
  170. expect(reduxState3.monitors[monitorId]).toBeUndefined();
  171. // Check that saved positions aren't changed by removing monitor
  172. expect(reduxState3.savedMonitorPositions).toEqual(reduxState2.savedMonitorPositions);
  173. });
  174. test('invalidRemoveMonitorRect', () => {
  175. let defaultState;
  176. const reduxState = monitorLayoutReducer(defaultState, {type: 'initialize'} /* action */);
  177. // Try to remove a monitor rect that doesn't exist
  178. expect(monitorLayoutReducer(reduxState, resizeMonitorRect(1)))
  179. .toEqual(reduxState);
  180. });
  181. test('getInitialPosition_lineUpTopLeft', () => {
  182. let defaultState;
  183. const width = 100;
  184. const height = 200;
  185. // Add monitors to right and bottom, but there is a space in the top left
  186. let reduxState = monitorLayoutReducer(defaultState, addMonitorRect(1,
  187. {x: width + PADDING, y: 0},
  188. {x: 100, y: height}
  189. ));
  190. reduxState = monitorLayoutReducer(defaultState, addMonitorRect(2,
  191. {x: 0, y: height + PADDING},
  192. {x: width, y: 100}
  193. ));
  194. // Check that the added monitor appears in the space
  195. const rect = getInitialPosition(reduxState, 3, width, height);
  196. expect(rect.upperStart).toBeDefined();
  197. expect(rect.lowerEnd).toBeDefined();
  198. expect(rect.lowerEnd.x - rect.upperStart.x).toEqual(width);
  199. expect(rect.lowerEnd.y - rect.upperStart.y).toEqual(height);
  200. expect(rect.upperStart.x).toEqual(PADDING);
  201. expect(rect.upperStart.y).toEqual(PADDING);
  202. });
  203. test('getInitialPosition_savedPosition', () => {
  204. const monitorId = 1;
  205. const savedX = 100;
  206. const savedY = 200;
  207. const width = 7;
  208. const height = 8;
  209. const reduxState = {
  210. monitors: {},
  211. savedMonitorPositions: {[monitorId]: {x: savedX, y: savedY}}
  212. };
  213. // Check that initial position uses saved state
  214. const rect = getInitialPosition(reduxState, monitorId, width, height);
  215. expect(rect.upperStart).toBeDefined();
  216. expect(rect.lowerEnd).toBeDefined();
  217. expect(rect.lowerEnd.x - rect.upperStart.x).toEqual(width);
  218. expect(rect.lowerEnd.y - rect.upperStart.y).toEqual(height);
  219. expect(rect.upperStart.x).toEqual(savedX);
  220. expect(rect.upperStart.y).toEqual(savedY);
  221. });
  222. test('getInitialPosition_lineUpLeft', () => {
  223. let defaultState;
  224. const monitor1EndY = 60;
  225. // Add a monitor that takes up the upper left corner
  226. const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(1, {x: 0, y: 0}, {x: 100, y: monitor1EndY}));
  227. // Check that added monitor is under it and lines up left
  228. const rect = getInitialPosition(reduxState, 2, 20 /* width */, 20 /* height */);
  229. expect(rect.upperStart.y >= monitor1EndY + PADDING).toBeTruthy();
  230. });
  231. test('getInitialPosition_lineUpTop', () => {
  232. let defaultState;
  233. const monitor1EndX = 100;
  234. // Add a monitor that takes up the whole left side
  235. const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(1,
  236. {x: 0, y: 0},
  237. {x: monitor1EndX, y: SCREEN_HEIGHT}
  238. ));
  239. // Check that added monitor is to the right of it and lines up top
  240. const rect = getInitialPosition(reduxState, 2, 20 /* width */, 20 /* height */);
  241. expect(rect.upperStart.y).toEqual(PADDING);
  242. expect(rect.upperStart.x >= monitor1EndX + PADDING).toBeTruthy();
  243. });
  244. test('getInitialPosition_noRoom', () => {
  245. let defaultState;
  246. const width = 7;
  247. const height = 8;
  248. // Add a monitor that takes up the whole screen
  249. const reduxState = monitorLayoutReducer(defaultState, addMonitorRect(1,
  250. {x: 0, y: 0},
  251. {x: SCREEN_WIDTH, y: SCREEN_HEIGHT}
  252. ));
  253. // Check that added monitor exists somewhere (we don't care where)
  254. const rect = getInitialPosition(reduxState, 2, width, height);
  255. expect(rect.upperStart).toBeDefined();
  256. expect(rect.lowerEnd.x - rect.upperStart.x).toEqual(width);
  257. expect(rect.lowerEnd.y - rect.upperStart.y).toEqual(height);
  258. });