Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Oct 16, 2024
1 parent 9025cbb commit 664a9e9
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 1 deletion.
156 changes: 156 additions & 0 deletions src/androidTest/java/de/blau/android/easyedit/ReplaceGeometryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package de.blau.android.easyedit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import androidx.test.uiautomator.UiDevice;
import de.blau.android.App;
import de.blau.android.LayerUtils;
import de.blau.android.Logic;
import de.blau.android.Main;
import de.blau.android.Map;
import de.blau.android.R;
import de.blau.android.TestUtils;
import de.blau.android.osm.Node;
import de.blau.android.osm.OsmElement;
import de.blau.android.osm.StorageDelegator;
import de.blau.android.osm.Tags;
import de.blau.android.osm.Way;
import de.blau.android.prefs.AdvancedPrefDatabase;
import de.blau.android.prefs.Preferences;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ReplaceGeometryTest {

Context context = null;
AdvancedPrefDatabase prefDB = null;
Main main = null;
UiDevice device = null;
Map map = null;
Logic logic = null;

@Rule
public ActivityTestRule<Main> mActivityRule = new ActivityTestRule<>(Main.class);

/**
* Pre-test setup
*/
@Before
public void setup() {
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
main = mActivityRule.getActivity();
Preferences prefs = new Preferences(context);
LayerUtils.removeImageryLayers(context);
map = main.getMap();
map.setPrefs(main, prefs);
TestUtils.grantPermissons(device);
TestUtils.dismissStartUpDialogs(device, main);
logic = App.getLogic();
logic.deselectAll();
logic.updateStyle();
map.getDataLayer().setVisible(true);
}

/**
*
*/
private void loadData(@NonNull String data) {
TestUtils.loadTestData(main, data);
TestUtils.stopEasyEdit(main);
map.getViewBox().fitToBoundingBox(map, map.getDataLayer().getExtent());
TestUtils.zoomToLevel(device, main, 20);
map.invalidate();
TestUtils.unlock(device);
device.waitForWindowUpdate(null, 2000);
}

/**
* Post-test teardown
*/
@After
public void teardown() {
TestUtils.stopEasyEdit(main);
TestUtils.zoomToNullIsland(logic, map);
}

/**
* Select, replace with way
*/
@Test
public void replaceNode() {
loadData("replace_geometry3.osm");
Node node = (Node) App.getDelegator().getOsmElement(Node.NAME, -14L);
assertNotNull(node);
assertTrue(node.hasTag(Tags.KEY_SHOP, "convenience"));
TestUtils.clickAtCoordinates(device, map, node.getLon(), node.getLat(), true);
assertTrue(TestUtils.findText(device, false, context.getString(R.string.actionmode_nodeselect)));
node = App.getLogic().getSelectedNode();
assertNotNull(node);
assertEquals(-14L, node.getOsmId());

assertTrue(TestUtils.clickOverflowButton(device));
TestUtils.scrollTo(context.getString(R.string.menu_replace_geometry), false);
assertTrue(TestUtils.clickText(device, false, context.getString(R.string.menu_replace_geometry), true, false));
assertTrue(TestUtils.findText(device, false, context.getString(R.string.actionmode_subtitle_replace_geometry)));
TestUtils.clickAtCoordinates(device, map, 8.3760111D, 47.3981113D);
assertTrue(TestUtils.findText(device, false, context.getString(R.string.actionmode_wayselect)));

assertFalse(node.hasTag(Tags.KEY_SHOP, "convenience"));
Way w = App.getLogic().getSelectedWay();
assertEquals(-1, w.getOsmId());
assertTrue(w.hasTag(Tags.KEY_SHOP, "convenience"));
assertTrue(w.hasNode(node));
}

/**
* Select, replace with geometry from way
*/
@Test
public void replaceWay() {
loadData("replace_geometry2.osm");
Node node = (Node) App.getDelegator().getOsmElement(Node.NAME, -14L);
assertNotNull(node);
TestUtils.clickAtCoordinates(device, map, 8.3760111D, 47.3981113D);

assertTrue(TestUtils.findText(device, false, context.getString(R.string.actionmode_wayselect)));
Way way = App.getLogic().getSelectedWay();
assertNotNull(way);
assertEquals(-1L, way.getOsmId());

assertTrue(TestUtils.clickOverflowButton(device));
TestUtils.scrollTo(context.getString(R.string.menu_replace_geometry), false);
assertTrue(TestUtils.clickText(device, false, context.getString(R.string.menu_replace_geometry), true, false));
assertTrue(TestUtils.findText(device, false, context.getString(R.string.actionmode_subtitle_replace_geometry)));
TestUtils.clickAtCoordinates(device, map, 8.3761799D, 47.3979480D);

assertTrue(TestUtils.findText(device, false, context.getString(R.string.remove_geometry_source)));
assertTrue(TestUtils.clickText(device, false, context.getString(R.string.Yes), true, false));
TestUtils.clickAwayTip(device, context);
assertTrue(TestUtils.findText(device, false, context.getString(R.string.replace_geometry_issue_title)));
assertTrue(TestUtils.clickText(device, false, context.getString(R.string.Done), true, false));
assertTrue(TestUtils.findText(device, false, context.getString(R.string.actionmode_wayselect)));

Way w = App.getLogic().getSelectedWay();
assertEquals(-1, w.getOsmId());
assertFalse(w.hasNode(node));
}
}
58 changes: 57 additions & 1 deletion src/test/java/de/blau/android/LogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -13,13 +14,15 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import androidx.annotation.NonNull;
import androidx.test.filters.LargeTest;
import de.blau.android.osm.Node;
import de.blau.android.osm.OsmElement;
import de.blau.android.osm.Result;
import de.blau.android.osm.Way;
import de.blau.android.util.Util;

@Config(shadows = { ShadowWorkManager.class }, sdk=33)
@Config(shadows = { ShadowWorkManager.class }, sdk = 33)
@RunWith(RobolectricTestRunner.class)
@LargeTest
public class LogicTest {
Expand Down Expand Up @@ -71,4 +74,57 @@ public void mergeNodeToWay() {
App.getLogic().performJoinNodeToWays(null, list, n1);
assertEquals(1, way.getNodes().indexOf(n1));
}

/**
* Replace way geometry by new one, this should simply adjust the position of the three nodes and delete the fourth
* one
*/
@Test
public void replaceGeometry1() {
assertTrue(replaceGeometry("replace_geometry.osm", -1, -2).isEmpty());
}

/**
* Replace way geometry by new one, this should simply adjust the position of the four existing nodes and add two
* more
*/
@Test
public void replaceGeometry2() {
assertTrue(replaceGeometry("replace_geometry.osm", -1, -3).isEmpty());
}

/**
* Replace way geometry by new one, this should simply adjust the position of the four existing nodes and add two
* more
*/
@Test
public void replaceGeometry3() {
List<Result> result = replaceGeometry("replace_geometry2.osm", -1, -3);
assertEquals(1, result.size());
Result r = result.get(0);
assertEquals(-14, r.getElement().getOsmId());
assertTrue(App.getLogic().getWaysForNode((Node) r.getElement()).isEmpty());
}

/**
* Replace way geometry by new one
*/
private List<Result> replaceGeometry(@NonNull String input, int targetId, int sourceId) {
UnitTestUtils.loadTestData(getClass(), input);
Way target = (Way) App.getDelegator().getOsmElement(Way.NAME, targetId);
assertNotNull(target);
Way source = (Way) App.getDelegator().getOsmElement(Way.NAME, sourceId);
assertNotNull(source);
List<Result> result = App.getLogic().performReplaceGeometry(null, target, source.getNodes());
assertEquals(target.nodeCount(), source.nodeCount());
List<Node> sourceNodes = source.getNodes();
List<Node> targetNodes = target.getNodes();
for (int i = 0; i < source.nodeCount(); i++) {
Node s = sourceNodes.get(i);
Node t = targetNodes.get(i);
assertEquals(s.getLon(), t.getLon());
assertEquals(s.getLat(), t.getLat());
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ public void polgonsToPolygonMergeTest() {
Result r = results.get(0);
assertTrue(r.getElement() instanceof Way);
}

/**
* Replace the nodes of one way by those of another
*/
@Test
public void replaceWayNodesTest() {
StorageDelegator d = RelationUtilTest.loadTestData(getClass());
Way w = RelationUtilTest.getWay(d, -1L);
Way w2 = RelationUtilTest.getWay(d, -8L);
List<Node> nodes = w2.getNodes();
d.replaceWayNodes(nodes, w);
assertEquals(nodes.size(), w.nodeCount());
final List<Node> wayNodes = w.getNodes();
for (int i = 0;i< nodes.size();i++) {
assertEquals(nodes.get(i), wayNodes.get(i));
}
}

/**
* Get a Map instance
Expand Down
1 change: 1 addition & 0 deletions src/testCommon/resources/replace_geometry.osm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version='1.0' encoding='UTF-8' ?><osm generator="Vespucci/20.1.0.0" version="0.6" upload="true"><node id="-1" action="modify" version="1" timestamp="2024-10-15T13:30:51Z" visible="true" lat="47.3981634" lon="8.3759256" /><node id="-2" action="modify" version="1" timestamp="2024-10-15T13:30:54Z" visible="true" lat="47.3982848" lon="8.3760962" /><node id="-3" action="modify" version="1" timestamp="2024-10-15T13:30:59Z" visible="true" lat="47.3981725" lon="8.3762705" /><node id="-4" action="modify" version="1" timestamp="2024-10-15T13:31:01Z" visible="true" lat="47.3980511" lon="8.3760998" /><node id="-5" action="modify" version="1" timestamp="2024-10-15T13:31:37Z" visible="true" lat="47.3979933" lon="8.3759868" /><node id="-6" action="modify" version="1" timestamp="2024-10-15T13:31:39Z" visible="true" lat="47.3978709" lon="8.3758232" /><node id="-7" action="modify" version="1" timestamp="2024-10-15T13:31:42Z" visible="true" lat="47.3978534" lon="8.376082" /><node id="-8" action="modify" version="1" timestamp="2024-10-15T13:32:00Z" visible="true" lat="47.3979862" lon="8.3761558" /><node id="-9" action="modify" version="1" timestamp="2024-10-15T13:32:02Z" visible="true" lat="47.3980389" lon="8.3762294" /><node id="-10" action="modify" version="1" timestamp="2024-10-15T13:32:07Z" visible="true" lat="47.3980361" lon="8.376311" /><node id="-11" action="modify" version="1" timestamp="2024-10-15T13:32:09Z" visible="true" lat="47.3979769" lon="8.3763604" /><node id="-12" action="modify" version="1" timestamp="2024-10-15T13:32:11Z" visible="true" lat="47.3979089" lon="8.3763099" /><node id="-13" action="modify" version="1" timestamp="2024-10-15T13:32:13Z" visible="true" lat="47.3979071" lon="8.3762115" /><way id="-1" action="modify" version="1" timestamp="2024-10-15T13:31:04Z" visible="true"><nd ref="-1" /><nd ref="-2" /><nd ref="-3" /><nd ref="-4" /><nd ref="-1" /></way><way id="-2" action="modify" version="1" timestamp="2024-10-15T13:31:44Z" visible="true"><nd ref="-5" /><nd ref="-6" /><nd ref="-7" /><nd ref="-5" /></way><way id="-3" action="modify" version="1" timestamp="2024-10-15T13:32:14Z" visible="true"><nd ref="-8" /><nd ref="-9" /><nd ref="-10" /><nd ref="-11" /><nd ref="-12" /><nd ref="-13" /><nd ref="-8" /></way></osm>
1 change: 1 addition & 0 deletions src/testCommon/resources/replace_geometry2.osm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version='1.0' encoding='UTF-8' ?><osm generator="Vespucci/20.1.0.0" version="0.6" upload="true"><bounds origin="" maxlon="8.3763604" maxlat="47.3982848" minlon="8.3758232" minlat="47.3978534" /><node id="-1" action="modify" version="1" timestamp="2024-10-16T09:06:49Z" visible="true" lat="47.3981634" lon="8.3759256" /><node id="-2" action="modify" version="1" timestamp="2024-10-16T09:06:49Z" visible="true" lat="47.3982848" lon="8.3760962" /><node id="-3" action="modify" version="1" timestamp="2024-10-16T09:06:49Z" visible="true" lat="47.3981725" lon="8.3762705" /><node id="-4" action="modify" version="1" timestamp="2024-10-16T09:06:49Z" visible="true" lat="47.3980511" lon="8.3760998" /><node id="-5" action="modify" version="1" timestamp="2024-10-15T13:31:37Z" visible="true" lat="47.3979933" lon="8.3759868" /><node id="-6" action="modify" version="1" timestamp="2024-10-15T13:31:39Z" visible="true" lat="47.3978709" lon="8.3758232" /><node id="-7" action="modify" version="1" timestamp="2024-10-15T13:31:42Z" visible="true" lat="47.3978534" lon="8.376082" /><node id="-8" action="modify" version="1" timestamp="2024-10-15T13:32:00Z" visible="true" lat="47.3979862" lon="8.3761558" /><node id="-9" action="modify" version="1" timestamp="2024-10-15T13:32:02Z" visible="true" lat="47.3980389" lon="8.3762294" /><node id="-10" action="modify" version="1" timestamp="2024-10-15T13:32:07Z" visible="true" lat="47.3980361" lon="8.376311" /><node id="-11" action="modify" version="1" timestamp="2024-10-15T13:32:09Z" visible="true" lat="47.3979769" lon="8.3763604" /><node id="-12" action="modify" version="1" timestamp="2024-10-15T13:32:11Z" visible="true" lat="47.3979089" lon="8.3763099" /><node id="-13" action="modify" version="1" timestamp="2024-10-15T13:32:13Z" visible="true" lat="47.3979071" lon="8.3762115" /><node id="-14" action="modify" version="1" timestamp="2024-10-16T12:10:03Z" visible="true" lat="47.3981108" lon="8.3761838"><tag k="shop" v="convenience" /></node><way id="-1" action="modify" version="1" timestamp="2024-10-16T09:06:49Z" visible="true"><nd ref="-1" /><nd ref="-2" /><nd ref="-3" /><nd ref="-14" /><nd ref="-4" /><nd ref="-1" /></way><way id="-2" action="modify" version="1" timestamp="2024-10-15T13:31:44Z" visible="true"><nd ref="-5" /><nd ref="-6" /><nd ref="-7" /><nd ref="-5" /></way><way id="-3" action="modify" version="1" timestamp="2024-10-15T13:32:14Z" visible="true"><nd ref="-8" /><nd ref="-9" /><nd ref="-10" /><nd ref="-11" /><nd ref="-12" /><nd ref="-13" /><nd ref="-8" /></way></osm>

0 comments on commit 664a9e9

Please sign in to comment.