 |
Gridarta Editor
|
Go to the documentation of this file.
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();
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();
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();
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);
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);
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() {
An Archetype implementation for testing purposes.
void setLocationSafe()
Checks MapCursor#setLocationSafe(Point).
int getWidth()
Returns the width of the area.
A MapModel reflects the data of a map.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
static final int ENTER_X
The enter x coordinate of newly created maps.
static void assertPreSelection(@NotNull final MapGrid grid, @NotNull final Point start, @NotNull final Point end)
Checks that a MapGrid includes a rectangle of {}.
Base package of all Gridarta classes.
static final Size2D GRID_SIZE
The size of the map grid for the tested cursor.
final void deactivate()
Cursor gets deactivated.
void dragSelect(@NotNull final SelectionMode selectionMode, final boolean forceSelect)
Leave drag mode and select pre-selection using selectionMode.
static final int GRID_FLAG_SELECTION
Selection - marks all selected squares.
final Point dragStart
Position where dragging has started.
void mapCursorChangedSize()
void dragging()
Checks the dragging related functions of MapCursor.
A GameObject implementation for testing purposes.
int changedPosCounter
The number of calls to mapCursorChangedPos(Point).
Helper class for regression tests to create MapModel instances.
SUB
All squares that are preselected get unselected.
FLIP
All squares that are preselected change state of selection.
void testGoTo()
Checks MapCursor#goTo(boolean, Direction).
void mapCursorChangedGameObject(@Nullable final MapSquare< TestGameObject, TestMapArchObject, TestArchetype > mapSquare, @Nullable final TestGameObject gameObject)
Interface for listeners listening to MapCursor related events.
static junit.framework.Test suite()
Returns a new test suite containing this test.
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 {}.
boolean setLocationSafe(@Nullable final Point p)
Move cursor to a new location.
static final int ENTER_Y
The enter y coordinate of newly created maps.
boolean dragTo(@Nullable final Point p)
When in drag mode and the point is on the map cursor is moved to this position.
GameObjects are the objects based on Archetypes found on maps.
int getHeight()
Returns the height of the area.
void setInside()
Checks that settings the cursor within the map grid behaves as expected.
MapModel< TestGameObject, TestMapArchObject, TestArchetype > newMapModel(final int w, final int h)
Creates a new MapModel instance.
boolean goTo(final boolean performAction, @NotNull final Direction dir)
Moves the cursor one square relative to current position.
static void testEvents(final int nPos, final int nMode)
Checks if the number of events fired is correct.
void mapCursorChangedPos(@NotNull final Point location)
boolean isDragging()
Returns whether the cursor is currently being dragged.
A MapCursorListener that counts the number of event callbacks.
Point getLocation()
Get position of cursor.
Modes that describe how squares get selected.
MapCursor provides methods to move and drag on map.
void setLocation(@NotNull final Point p)
Move cursor to a new location.
boolean isOnGrid(@Nullable final Point p)
Check if point is on grid.
void addMapCursorListener(@NotNull final MapCursorListener< G, A, R > listener)
Register a MapCursorListener.
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
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.
ADD
All squares that are preselected get selected.
static MapCursor< TestGameObject, TestMapArchObject, TestArchetype > createCursor(@NotNull final MapGrid grid)
Creates a new MapCursor instance.
void mapCursorChangedMode()
void setOutside()
Checks that settings the cursor outside of the grid behaves as expected.
A MapArchObject implementation for testing purposes.
static final TestMapCursorListener LISTENER
A MapCursorListener that counts the number of callbacks.
void selecting()
Checks for correct behavior of MapGrid#GRID_FLAG_SELECTING flags during selecting.
void setSameLocation()
Checks that settings the cursor to the same location does not generate excess events.
The class Size2D represents a 2d rectangular area.
void testIsOnGrid()
Checks MapCursor#isOnGrid(Point).
Dimension getDragOffset()
Get offset from start position of dragging.
void dragRelease()
Leave drag mode and undo pre-selection.
int changedModeCounter
The number of calls to mapCursorChangedMode().
static final int GRID_FLAG_SELECTING
Pre-selection - used to preselect squares.