Gridarta Editor
UndoActionsTest.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.actions;
21 
22 import java.awt.GraphicsEnvironment;
23 import java.awt.Point;
38 import net.sf.gridarta.utils.Size2D;
39 import org.jetbrains.annotations.NotNull;
40 import org.junit.Assert;
41 import org.junit.Test;
42 
47 public class UndoActionsTest {
48 
54  @Test
56  if (GraphicsEnvironment.isHeadless()) {
57  return;
58  }
59 
60  final TestMapControlCreator mapControlCreator = new TestMapControlCreator();
61  final TestMapModelCreator mapModelCreator = mapControlCreator.getMapModelCreator();
62  final AutojoinListsHelper autojoinListsHelper = mapControlCreator.newAutojoinListsHelper();
63  for (int i = 0; i < 16; i++) {
64  final TestArchetype a = mapModelCreator.getArchetype("a" + i);
65  a.setAttributeString(BaseObject.FACE, "face" + i);
66  }
67  autojoinListsHelper.newAutojoinLists("a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15");
68 
69  final UndoControl<TestGameObject, TestMapArchObject, TestArchetype> undoControl = new UndoControl<>(mapControlCreator.getMapManager(), mapModelCreator.getGameObjectFactory(), mapModelCreator.getGameObjectMatchers());
70 
71  final TestMapArchObject mapArchObject = new TestMapArchObject();
72  mapArchObject.setMapSize(new Size2D(2, 1));
73  final MapControl<TestGameObject, TestMapArchObject, TestArchetype> mapControl = mapControlCreator.getMapManager().newMap(null, mapArchObject, null, true);
75  final TestMapModelHelper testMapModelHelper = mapModelCreator.newTestMapModelHelper();
76 
77  final TestArchetype a0 = mapModelCreator.getArchetype("a0");
78 
79  mapModelCreator.getMapViewSettings().setAutojoin(true);
80  mapModel.beginTransaction("TEST");
81  try {
82  testMapModelHelper.insertArchetype(mapModel, 0, 0, a0, false);
83  } finally {
84  mapModel.endTransaction();
85  }
86 
87  TestMapModelHelper.checkMapContents(mapModel, "a0|");
88  checkFace(mapModel, 0, 0, "face0");
89 
90  mapModel.beginTransaction("TEST");
91  try {
92  testMapModelHelper.insertArchetype(mapModel, 1, 0, a0, true);
93  } finally {
94  mapModel.endTransaction();
95  }
96 
97  TestMapModelHelper.checkMapContents(mapModel, "a2|a8");
98  checkFace(mapModel, 0, 0, "face2");
99  checkFace(mapModel, 1, 0, "face8");
100 
101  undoControl.undo();
102  TestMapModelHelper.checkMapContents(mapModel, "a0|");
103  checkFace(mapModel, 0, 0, "face0");
104  }
105 
113  private static void checkFace(@NotNull final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel, final int x, final int y, @NotNull final String face) {
114  final MapSquare<?, ?, ?> mapSquare = mapModel.getMapSquare(new Point(x, y));
115  final BaseObject<?, ?, ?, ?> object = mapSquare.getFirst();
116  Assert.assertNotNull(object);
117  Assert.assertEquals(face, object.getAttributeString(BaseObject.FACE));
118  }
119 
120 }
Implements the controller for undo/redo actions.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Graphical User Interface of Gridarta.
Helper class for regression tests to create MapModel instances.
Implements AutojoinList related functions.
UI parts of the implementation of an Undo / Redo in Gridarta.
void endTransaction()
End a transaction.
Helper class for creating MapModel instances for regression tests.
void undo()
"Undo" was selected.
TestMapModelCreator getMapModelCreator()
Returns the TestMapModelCreator instance.
Helper class for creating MapControl instances for regression tests.
String FACE
The attribute name of the object&#39;s face.
Definition: BaseObject.java:54
void setAutojoin(boolean autojoin)
Sets the "autojoin" state.
Regression tests for UndoActions.
void testInsert1()
Checks that undo correctly sets the "face" attribute.
A MapArchObject implementation for testing purposes.
MapModel< G, A, R > getMapModel()
Returns the map model.
Base package of all Gridarta classes.
TestGameObject insertArchetype(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, final int x, final int y, @NotNull final BaseObject< TestGameObject, TestMapArchObject, TestArchetype, ?> archetype, final boolean join)
Inserts an archetype game object into a map model.
AutojoinListsHelper newAutojoinListsHelper()
Returns a new AutojoinListsHelper instance.
An Exception indicating that an Archetype name is not unique.
GameObjects are the objects based on Archetypes found on maps.
static void checkMapContents(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final String... lines)
Checks for expected MapModel&#39;s contents.
G getFirst(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the first occurrence of a matching game object.
Definition: MapSquare.java:226
TestArchetype getArchetype(@NotNull final String archetypeName)
Returns an archetype.
TestMapModelHelper newTestMapModelHelper()
Creates a new TestMapModelHelper instance.
static void checkFace(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, final int x, final int y, @NotNull final String face)
Checks the face name of the first object in a map square.
void newAutojoinLists(@NotNull final String... archetypeNames)
Creates a new AutojoinLists instance.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
MapViewSettings getMapViewSettings()
Returns the MapViewSettings instance.
An Archetype implementation for testing purposes.
A GameObject implementation for testing purposes.
GameObjectMatchers getGameObjectMatchers()
Returns the GameObjectMatchers instance.
void beginTransaction(@NotNull String name)
Starts a new transaction.
GameObjectFactory< TestGameObject, TestMapArchObject, TestArchetype > getGameObjectFactory()
Returns the GameObjectFactory instance.
MapManager< TestGameObject, TestMapArchObject, TestArchetype > getMapManager()
Returns the MapManager.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30