Gridarta Editor
MapCursorTest.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.mapcursor;
21 
22 import java.awt.Dimension;
23 import java.awt.Point;
24 import junit.framework.JUnit4TestAdapter;
34 import net.sf.gridarta.utils.Size2D;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 import org.junit.Assert;
38 import org.junit.Test;
39 
46 public class MapCursorTest {
47 
51  private static final int ENTER_X = 1;
52 
56  private static final int ENTER_Y = 2;
57 
61  @NotNull
62  private static final Size2D GRID_SIZE = new Size2D(6, 7);
63 
67  @NotNull
69 
76  @NotNull
78  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
79  final Size2D gridSize = grid.getSize();
80  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(gridSize.getWidth(), gridSize.getHeight());
81  mapModel.getMapArchObject().setEnterX(ENTER_X);
82  mapModel.getMapArchObject().setEnterY(ENTER_Y);
84  cursor.addMapCursorListener(LISTENER);
85  LISTENER.changedPosCounter = 0;
86  LISTENER.changedModeCounter = 0;
87  return cursor;
88  }
89 
93  @Test
94  public void setOutside() {
95  final MapGrid grid = new MapGrid(GRID_SIZE);
97  final Point p = new Point();
98  final int width = GRID_SIZE.getWidth();
99  final int height = GRID_SIZE.getHeight();
100  for (int i = -2; i < width + 2; i++) {
101  p.setLocation(i, -1);
102  cursor.setLocation(p);
103  testEvents(1, 0);
104  Assert.assertEquals(new Point(Math.max(0, Math.min(width - 1, i)), 0), cursor.getLocation());
105  testEvents(0, 0);
106  p.setLocation(i, height);
107  cursor.setLocation(p);
108  testEvents(1, 0);
109  Assert.assertEquals(new Point(Math.max(0, Math.min(width - 1, i)), height - 1), cursor.getLocation());
110  testEvents(0, 0);
111  }
112  for (int i = -2; i < height + 2; i++) {
113  p.setLocation(-1, i);
114  cursor.setLocation(p);
115  testEvents(1, 0);
116  Assert.assertEquals(new Point(0, Math.max(0, Math.min(height - 1, i))), cursor.getLocation());
117  testEvents(0, 0);
118  p.setLocation(width, i);
119  cursor.setLocation(p);
120  testEvents(1, 0);
121  Assert.assertEquals(new Point(width - 1, Math.max(0, Math.min(height - 1, i))), cursor.getLocation());
122  testEvents(0, 0);
123  }
124  }
125 
129  @Test
130  public void setInside() {
131  final MapGrid grid = new MapGrid(GRID_SIZE);
133  final Point p = new Point();
134  for (int j = 0; j < GRID_SIZE.getHeight(); j++) {
135  for (int i = 0; i < GRID_SIZE.getWidth(); i++) {
136  p.setLocation(i, j);
137  cursor.setLocation(p);
138  testEvents(1, 0);
139  final Point res = cursor.getLocation();
140  testEvents(0, 0);
141  Assert.assertEquals("getLocation()", p, res);
142  }
143  }
144  }
145 
150  @Test
151  public void setSameLocation() {
152  final MapGrid grid = new MapGrid(GRID_SIZE);
154  cursor.setLocation(new Point(3, 4));
155  testEvents(1, 0);
156  cursor.setLocation(new Point(3, 4));
157  testEvents(0, 0);
158  cursor.setLocation(new Point(1, 0));
159  testEvents(1, 0);
160  cursor.setLocation(new Point(1, -1));
161  testEvents(0, 0);
162  cursor.setLocation(new Point(-3, -2));
163  testEvents(1, 0);
164  cursor.setLocation(new Point(-2, -1));
165  testEvents(0, 0);
166  cursor.setLocation(new Point(0, 0));
167  testEvents(0, 0);
168  }
169 
173  @Test
174  public void setLocationSafe() {
175  final MapGrid grid = new MapGrid(GRID_SIZE);
177  final Point p = new Point(-1, -1);
178  Assert.assertFalse("setLocationSafe(null) should return false", cursor.setLocationSafe(null));
179  testEvents(0, 0);
180  Assert.assertFalse("setLocationSafe(" + p + ") should return false", cursor.setLocationSafe(p));
181  testEvents(0, 0);
182  p.setLocation(3, 4);
183  Assert.assertTrue("setLocationSafe(" + p + ") should return true", cursor.setLocationSafe(p));
184  testEvents(1, 0);
185  Assert.assertFalse("setLocationSafe(" + p + ") should return false", cursor.setLocationSafe(p));
186  testEvents(0, 0);
187  p.setLocation(-1, -1);
188  Assert.assertFalse("setLocationSafe(" + p + ") should return false", cursor.setLocationSafe(p));
189  testEvents(0, 0);
190  Assert.assertFalse("setLocationSafe(null) should return false", cursor.setLocationSafe(null));
191  testEvents(0, 0);
192  }
193 
197  @Test
198  public void testIsOnGrid() {
199  final MapGrid grid = new MapGrid(GRID_SIZE);
201  final Point p = new Point();
202  for (int j = -2; j < GRID_SIZE.getHeight() + 2; j++) {
203  for (int i = -2; i < GRID_SIZE.getWidth() + 2; i++) {
204  p.setLocation(i, j);
205  if (i >= 0 && i < GRID_SIZE.getWidth() && j >= 0 && j < GRID_SIZE.getHeight()) {
206  Assert.assertTrue(p + " should be on the grid.", cursor.isOnGrid(p));
207  } else {
208  Assert.assertFalse(p + " should not be on the grid.", cursor.isOnGrid(p));
209  }
210  }
211  }
212  Assert.assertFalse("Null should not be on the grid.", cursor.isOnGrid(null));
213  }
214 
218  @Test
219  public void testGoTo() {
220  final MapGrid grid = new MapGrid(GRID_SIZE);
222  final Point pStart = new Point(2, 3);
223  final Point p = new Point(pStart);
224  cursor.setLocation(p);
225  testEvents(1, 0);
226  for (final Direction dir : Direction.values()) {
227  if (dir == Direction.UP || dir == Direction.DOWN) {
228  Assert.assertFalse("go(" + dir + ") should return false. (Maybe the grid was too small.)", cursor.goTo(true, dir));
229  testEvents(0, 0);
230  } else {
231  Assert.assertTrue("go(" + dir + ") should return true. (Maybe the grid was too small.)", cursor.goTo(true, dir));
232  testEvents(1, 0);
233  }
234  p.x += dir.getDx();
235  p.y += dir.getDy();
236  Assert.assertEquals("Moving cursor.", p, cursor.getLocation());
237  }
238  Assert.assertEquals("Moving in a circle.", pStart, cursor.getLocation());
239  }
240 
244  @Test
245  public void dragging() {
246  final MapGrid grid = new MapGrid(GRID_SIZE);
248  Assert.assertFalse("MapCursor should not drag while deactivated.", cursor.isDragging());
249  cursor.dragStart();
250  testEvents(0, 1);
251  Assert.assertEquals(new Dimension(0, 0), cursor.getDragOffset());
252  final Point dragStart = new Point(3, 4);
253  final Point p = new Point(dragStart);
254  final Dimension offset = new Dimension(0, 0);
255  cursor.setLocation(dragStart);
256  testEvents(1, 0);
257  cursor.dragStart();
258  testEvents(0, 0);
259  Assert.assertTrue("MapCursor should be in drag mode.", cursor.isDragging());
260  Assert.assertEquals("Wrong offset", offset, cursor.getDragOffset());
261  cursor.deactivate();
262  testEvents(0, 1);
263  Assert.assertFalse("MapCursor should not drag while deactivated.", cursor.isDragging());
264  Assert.assertNull("Drag offset should be null", cursor.getDragOffset());
265  cursor.setLocation(dragStart);
266  testEvents(0, 0);
267  cursor.dragStart();
268  testEvents(0, 1);
269  Assert.assertEquals("Wrong offset", offset, cursor.getDragOffset());
270  dragTo(cursor, grid, Direction.WEST, dragStart, p, offset);
271  dragTo(cursor, grid, Direction.EAST, dragStart, p, offset);
272  dragTo(cursor, grid, Direction.EAST, dragStart, p, offset);
273  dragTo(cursor, grid, Direction.NORTH_WEST, dragStart, p, offset);
274  dragTo(cursor, grid, Direction.SOUTH, dragStart, p, offset);
275  dragTo(cursor, grid, Direction.SOUTH, dragStart, p, offset);
276  dragTo(cursor, grid, Direction.WEST, dragStart, p, offset);
277  dragTo(cursor, grid, Direction.NORTH_EAST, dragStart, p, offset);
278  }
279 
289  private static void dragTo(@NotNull final MapCursor<TestGameObject, TestMapArchObject, TestArchetype> cursor, @NotNull final MapGrid grid, @NotNull final Direction dir, @NotNull final Point start, @NotNull final Point p, @NotNull final Dimension offset) {
290  final Point d = new Point(dir.getDx(), dir.getDy());
291  p.x += d.x;
292  p.y += d.y;
293  Assert.assertTrue("dragTo(" + p + ")", cursor.dragTo(p));
294  testEvents(1, 0);
295  Assert.assertTrue("MapCursor should be in drag mode.", cursor.isDragging());
296  Assert.assertEquals("Wrong position", p, cursor.getLocation());
297  offset.width = p.x - start.x;
298  offset.height = p.y - start.y;
299  Assert.assertEquals("Wrong offset", offset, cursor.getDragOffset());
300  assertPreSelection(grid, start, p);
301  }
302 
310  private static void assertPreSelection(@NotNull final MapGrid grid, @NotNull final Point start, @NotNull final Point end) {
311  final int minX = Math.min(start.x, end.x);
312  final int maxX = Math.max(start.x, end.x);
313  final int minY = Math.min(start.y, end.y);
314  final int maxY = Math.max(start.y, end.y);
315  final int height = GRID_SIZE.getHeight();
316  final int width = GRID_SIZE.getWidth();
317  for (int j = 0; j < height; j++) {
318  for (int i = 0; i < width; i++) {
319  if (i < minX || i > maxX || j < minY || j > maxY) {
320  //Not preselected
321  Assert.assertFalse("Pre-selection", (grid.getFlags(i, j) & MapGrid.GRID_FLAG_SELECTING) > 0);
322  } else {
323  //Preselected
324  Assert.assertTrue("Pre-selection", (grid.getFlags(i, j) & MapGrid.GRID_FLAG_SELECTING) > 0);
325  }
326  }
327 
328  }
329  }
330 
340  private static void assertSelection(@NotNull final MapGrid grid, @NotNull final Point start, @NotNull final Point end, final boolean flag) {
341  final int minX = Math.min(start.x, end.x);
342  final int maxX = Math.max(start.x, end.x);
343  final int minY = Math.min(start.y, end.y);
344  final int maxY = Math.max(start.y, end.y);
345  for (int j = minY; j <= maxY; j++) {
346  for (int i = minX; i <= maxX; i++) {
347  Assert.assertSame("Selection", flag, (grid.getFlags(i, j) & MapGrid.GRID_FLAG_SELECTION) > 0);
348  }
349  }
350  }
351 
356  @Test
357  public void selecting() {
358  final MapGrid grid = new MapGrid(GRID_SIZE);
360  final Point start = new Point(2, 3);
361  final Point end = new Point(4, 5);
362  final Point gridMaxIndex = new Point(GRID_SIZE.getWidth() - 1, GRID_SIZE.getHeight() - 1);
363  cursor.dragRelease();
364  testEvents(0, 0);
365  cursor.setLocation(start);
366  testEvents(1, 0);
367  cursor.dragStart();
368  testEvents(0, 1);
369  cursor.dragTo(end);
370  testEvents(1, 0);
371  Assert.assertTrue("MapCursor should be in drag mode.", cursor.isDragging());
372  cursor.dragRelease();
373  testEvents(0, 1);
374  Assert.assertFalse("MapCursor should not be in drag mode.", cursor.isDragging());
375  cursor.setLocation(start);
376  testEvents(1, 0);
377  cursor.dragStart();
378  testEvents(0, 1);
379  cursor.dragTo(end);
380  testEvents(1, 0);
381  Assert.assertTrue("MapCursor should be in drag mode.", cursor.isDragging());
382  cursor.dragRelease();
383  testEvents(0, 1);
384  Assert.assertFalse("MapCursor should not be in drag mode.", cursor.isDragging());
385  cursor.setLocation(start);
386  testEvents(1, 0);
387  cursor.dragStart();
388  testEvents(0, 1);
389  cursor.dragTo(end);
390  testEvents(1, 0);
391  Assert.assertTrue("MapCursor should be in drag mode.", cursor.isDragging());
392  assertSelection(grid, new Point(0, 0), gridMaxIndex, false);
393  cursor.dragSelect(SelectionMode.ADD, true);
394  testEvents(0, 1);
395  assertSelection(grid, start, end, true);
396  //Check if nothing is preselected
397  assertPreSelection(grid, new Point(-1, -1), new Point(-1, -1));
398  cursor.setLocation(start);
399  testEvents(1, 0);
400  cursor.dragStart();
401  testEvents(0, 1);
402  cursor.dragTo(end);
403  testEvents(1, 0);
404  cursor.dragSelect(SelectionMode.SUB, true);
405  testEvents(0, 1);
406  assertSelection(grid, new Point(0, 0), gridMaxIndex, false);
407  cursor.setLocation(start);
408  testEvents(1, 0);
409  cursor.dragStart();
410  testEvents(0, 1);
411  cursor.dragTo(end);
412  testEvents(1, 0);
413  cursor.dragSelect(SelectionMode.FLIP, true);
414  testEvents(0, 1);
415  assertSelection(grid, start, end, true);
416  start.setLocation(3, 4);
417  end.setLocation(5, 1);
418  cursor.setLocation(start);
419  testEvents(1, 0);
420  cursor.dragStart();
421  testEvents(0, 1);
422  cursor.dragTo(end);
423  testEvents(1, 0);
424  cursor.dragSelect(SelectionMode.FLIP, true);
425  testEvents(0, 1);
426  assertSelection(grid, start, new Point(4, 4), false);
427  assertSelection(grid, new Point(3, 2), end, true);
428  assertSelection(grid, new Point(5, 3), new Point(5, 4), true);
429  cursor.deactivate();
430  testEvents(0, 0);
431  assertSelection(grid, new Point(0, 0), gridMaxIndex, false);
432  }
433 
439  private static void testEvents(final int nPos, final int nMode) {
440  Assert.assertEquals("Position change event", nPos, LISTENER.changedPosCounter);
441  LISTENER.changedPosCounter = 0;
442  Assert.assertEquals("Mode change event", nMode, LISTENER.changedModeCounter);
443  LISTENER.changedModeCounter = 0;
444  }
445 
450  @NotNull
451  public static junit.framework.Test suite() {
452  return new JUnit4TestAdapter(MapCursorTest.class);
453  }
454 
458  private static class TestMapCursorListener implements MapCursorListener<TestGameObject, TestMapArchObject, TestArchetype> {
459 
463  private int changedPosCounter;
464 
468  private int changedModeCounter;
469 
470  @Override
471  public void mapCursorChangedPos(@NotNull final Point location) {
472  changedPosCounter++;
473  }
474 
475  @Override
476  public void mapCursorChangedMode() {
477  changedModeCounter++;
478  }
479 
480  @Override
481  public void mapCursorChangedGameObject(@Nullable final MapSquare<TestGameObject, TestMapArchObject, TestArchetype> mapSquare, @Nullable final TestGameObject gameObject) {
482  // ignore
483  }
484 
485  @Override
486  public void mapCursorChangedSize() {
487  // ignore
488  }
489 
490  }
491 
492 }
Dimension getDragOffset()
Get offset from start position of dragging.
Definition: MapCursor.java:370
void setLocationSafe()
Checks MapCursor#setLocationSafe(Point).
void dragSelect(@NotNull final SelectionMode selectionMode, final boolean forceSelect)
Leave drag mode and select pre-selection using selectionMode.
Definition: MapCursor.java:337
static final Size2D GRID_SIZE
The size of the map grid for the tested cursor.
MapModel< TestGameObject, TestMapArchObject, TestArchetype > newMapModel(final int w, final int h)
Creates a new MapModel instance.
static MapCursor< TestGameObject, TestMapArchObject, TestArchetype > createCursor(@NotNull final MapGrid grid)
Creates a new MapCursor instance.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
static junit.framework.Test suite()
Returns a new test suite containing this test.
void setSameLocation()
Checks that settings the cursor to the same location does not generate excess events.
Helper class for regression tests to create MapModel instances.
static void testEvents(final int nPos, final int nMode)
Checks if the number of events fired is correct.
void setInside()
Checks that settings the cursor within the map grid behaves as expected.
static final int GRID_FLAG_SELECTING
Pre-selection - used to preselect squares.
Definition: MapGrid.java:103
MapCursor provides methods to move and drag on map.
Definition: MapCursor.java:57
static void dragTo(@NotNull final MapCursor< TestGameObject, TestMapArchObject, TestArchetype > cursor, @NotNull final MapGrid grid, @NotNull final Direction dir, @NotNull final Point start, @NotNull final Point p, @NotNull final Dimension offset)
Calls MapCursor#dragTo(Point) and checks for expected results.
static void assertSelection(@NotNull final MapGrid grid, @NotNull final Point start, @NotNull final Point end, final boolean flag)
Checks that a MapGrid includes a rectangle of MapGrid#GRID_FLAG_SELECTION.
static void assertPreSelection(@NotNull final MapGrid grid, @NotNull final Point start, @NotNull final Point end)
Checks that a MapGrid includes a rectangle of MapGrid#GRID_FLAG_SELECTING.
A MapArchObject implementation for testing purposes.
boolean isOnGrid(@Nullable final Point p)
Check if point is on grid.
Definition: MapCursor.java:379
boolean setLocationSafe(@Nullable final Point p)
Move cursor to a new location.
Definition: MapCursor.java:254
Point getLocation()
Get position of cursor.
Definition: MapCursor.java:226
int changedModeCounter
The number of calls to mapCursorChangedMode().
boolean dragTo(@Nullable final Point p)
When in drag mode and the point is on the map cursor is moved to this position.
Definition: MapCursor.java:298
void setLocation(@NotNull final Point p)
Move cursor to a new location.
Definition: MapCursor.java:235
Base package of all Gridarta classes.
void addMapCursorListener(@NotNull final MapCursorListener< G, A, R > listener)
Register a MapCursorListener.
Definition: MapCursor.java:419
void setOutside()
Checks that settings the cursor outside of the grid behaves as expected.
void testGoTo()
Checks MapCursor#goTo(boolean, Direction).
GameObjects are the objects based on Archetypes found on maps.
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
Definition: MapGrid.java:45
FLIP
All squares that are preselected change state of selection.
void dragRelease()
Leave drag mode and undo pre-selection.
Definition: MapCursor.java:318
final Point dragStart
Position where dragging has started.
Definition: MapCursor.java:70
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
boolean goTo(final boolean performAction, @NotNull final Direction dir)
Moves the cursor one square relative to current position.
Definition: MapCursor.java:389
void mapCursorChangedGameObject(@Nullable final MapSquare< TestGameObject, TestMapArchObject, TestArchetype > mapSquare, @Nullable final TestGameObject gameObject)
int changedPosCounter
The number of calls to mapCursorChangedPos(Point).
ADD
All squares that are preselected get selected.
An Archetype implementation for testing purposes.
A GameObject implementation for testing purposes.
A MapCursorListener that counts the number of event callbacks.
Modes that describe how squares get selected.
static final int GRID_FLAG_SELECTION
Selection - marks all selected squares.
Definition: MapGrid.java:98
SUB
All squares that are preselected get unselected.
static final TestMapCursorListener LISTENER
A MapCursorListener that counts the number of callbacks.
static final int ENTER_Y
The enter y coordinate of newly created maps.
Interface for listeners listening to MapCursor related events.
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
boolean isDragging()
Returns whether the cursor is currently being dragged.
Definition: MapCursor.java:411
final void deactivate()
Cursor gets deactivated.
Definition: MapCursor.java:354
void dragging()
Checks the dragging related functions of MapCursor.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
static final int ENTER_X
The enter x coordinate of newly created maps.
void testIsOnGrid()
Checks MapCursor#isOnGrid(Point).
void selecting()
Checks for correct behavior of MapGrid#GRID_FLAG_SELECTING flags during selecting.