20 package net.sf.gridarta.model.mapcursor;
22 import java.awt.Dimension;
23 import java.awt.Point;
24 import junit.framework.JUnit4TestAdapter;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 import org.junit.Assert;
38 import org.junit.Test;
79 final Size2D gridSize = grid.getSize();
97 final Point p =
new Point();
98 final int width = GRID_SIZE.
getWidth();
100 for (
int i = -2; i < width + 2; i++) {
101 p.setLocation(i, -1);
104 Assert.assertEquals(
new Point(Math.max(0, Math.min(width - 1, i)), 0), cursor.
getLocation());
106 p.setLocation(i, height);
109 Assert.assertEquals(
new Point(Math.max(0, Math.min(width - 1, i)), height - 1), cursor.
getLocation());
112 for (
int i = -2; i < height + 2; i++) {
113 p.setLocation(-1, i);
116 Assert.assertEquals(
new Point(0, Math.max(0, Math.min(height - 1, i))), cursor.
getLocation());
118 p.setLocation(width, i);
121 Assert.assertEquals(
new Point(width - 1, Math.max(0, Math.min(height - 1, i))), cursor.
getLocation());
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++) {
141 Assert.assertEquals(
"getLocation()", p, res);
177 final Point p =
new Point(-1, -1);
178 Assert.assertFalse(
"setLocationSafe(null) should return false", cursor.
setLocationSafe(null));
180 Assert.assertFalse(
"setLocationSafe(" + p +
") should return false", cursor.
setLocationSafe(p));
183 Assert.assertTrue(
"setLocationSafe(" + p +
") should return true", cursor.
setLocationSafe(p));
185 Assert.assertFalse(
"setLocationSafe(" + p +
") should return false", cursor.
setLocationSafe(p));
187 p.setLocation(-1, -1);
188 Assert.assertFalse(
"setLocationSafe(" + p +
") should return false", cursor.
setLocationSafe(p));
190 Assert.assertFalse(
"setLocationSafe(null) should return false", cursor.
setLocationSafe(null));
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++) {
206 Assert.assertTrue(p +
" should be on the grid.", cursor.
isOnGrid(p));
208 Assert.assertFalse(p +
" should not be on the grid.", cursor.
isOnGrid(p));
212 Assert.assertFalse(
"Null should not be on the grid.", cursor.
isOnGrid(null));
222 final Point pStart =
new Point(2, 3);
223 final Point p =
new Point(pStart);
228 Assert.assertFalse(
"go(" + dir +
") should return false. (Maybe the grid was too small.)", cursor.
goTo(
true, dir));
231 Assert.assertTrue(
"go(" + dir +
") should return true. (Maybe the grid was too small.)", cursor.
goTo(
true, dir));
236 Assert.assertEquals(
"Moving cursor.", p, cursor.
getLocation());
238 Assert.assertEquals(
"Moving in a circle.", pStart, cursor.
getLocation());
248 Assert.assertFalse(
"MapCursor should not drag while deactivated.", cursor.
isDragging());
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);
259 Assert.assertTrue(
"MapCursor should be in drag mode.", cursor.
isDragging());
260 Assert.assertEquals(
"Wrong offset", offset, cursor.
getDragOffset());
263 Assert.assertFalse(
"MapCursor should not drag while deactivated.", cursor.
isDragging());
264 Assert.assertNull(
"Drag offset should be null", cursor.
getDragOffset());
269 Assert.assertEquals(
"Wrong offset", offset, cursor.
getDragOffset());
290 final Point d =
new Point(dir.getDx(), dir.getDy());
293 Assert.assertTrue(
"dragTo(" + p +
")", cursor.dragTo(p));
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());
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) {
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++) {
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);
371 Assert.assertTrue(
"MapCursor should be in drag mode.", cursor.
isDragging());
374 Assert.assertFalse(
"MapCursor should not be in drag mode.", cursor.
isDragging());
381 Assert.assertTrue(
"MapCursor should be in drag mode.", cursor.
isDragging());
384 Assert.assertFalse(
"MapCursor should not be in drag mode.", cursor.
isDragging());
391 Assert.assertTrue(
"MapCursor should be in drag mode.", cursor.
isDragging());
416 start.setLocation(3, 4);
417 end.setLocation(5, 1);
439 private static void testEvents(
final int nPos,
final int nMode) {
451 public static junit.framework.Test
suite() {
477 changedModeCounter++;
Dimension getDragOffset()
Get offset from start position of dragging.
void setLocationSafe()
Checks MapCursor#setLocationSafe(Point).
void dragSelect(@NotNull final SelectionMode selectionMode, final boolean forceSelect)
Leave drag mode and select pre-selection using selectionMode.
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.
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.
MapCursor provides methods to move and drag on map.
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.
void mapCursorChangedSize()
boolean isOnGrid(@Nullable final Point p)
Check if point is on grid.
boolean setLocationSafe(@Nullable final Point p)
Move cursor to a new location.
Point getLocation()
Get position of cursor.
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.
void setLocation(@NotNull final Point p)
Move cursor to a new location.
Base package of all Gridarta classes.
void addMapCursorListener(@NotNull final MapCursorListener< G, A, R > listener)
Register a MapCursorListener.
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.
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
FLIP
All squares that are preselected change state of selection.
void dragRelease()
Leave drag mode and undo pre-selection.
final Point dragStart
Position where dragging has started.
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.
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.
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.
boolean isDragging()
Returns whether the cursor is currently being dragged.
void mapCursorChangedMode()
void mapCursorChangedPos(@NotNull final Point location)
final void deactivate()
Cursor gets deactivated.
void dragging()
Checks the dragging related functions of MapCursor.
The class Size2D represents a 2d rectangular area.
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.