Skip to content

Commit

Permalink
Add test for selection upload
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Sep 8, 2024
1 parent 4838c04 commit 829a2ab
Showing 1 changed file with 77 additions and 26 deletions.
103 changes: 77 additions & 26 deletions src/androidTest/java/de/blau/android/osm/UploadConflictTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,50 @@ public void teardown() {
*/
@Test
public void versionConflictUseLocal() {
versionConflict("conflict1", new String[] { "conflictdownload1" }, false, R.string.upload_conflict_message_version);
conflict("conflict1", new String[] { "conflictdownload1" }, false, R.string.upload_conflict_message_version);
assertTrue(TestUtils.clickText(device, false, main.getString(R.string.resolve), true));
assertTrue(TestUtils.clickText(device, false, main.getString(R.string.use_local_version), true));
assertTrue(TestUtils.findText(device, false, main.getString(R.string.confirm_upload_title), 5000));
Node n = (Node) App.getDelegator().getOsmElement(Node.NAME, 101792984L);
assertEquals(7, n.getOsmVersion()); // version should now be the same as the server
}

/**
* Version conflict use the local element, just uploading a selection
*/
@Test
public void versionConflictUseLocalSelection() {
loadDataAndFixtures("conflict1", new String[] { "conflictdownload1" }, false);

UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

TestUtils.unlock(device);
Node n = (Node) App.getDelegator().getOsmElement(Node.NAME, 101792984L);
TestUtils.clickAtCoordinates(device, main.getMap(), n.getLon(), n.getLat());

assertTrue(TestUtils.findText(device, false, main.getString(R.string.actionmode_nodeselect), 1000));

assertTrue(TestUtils.clickOverflowButton(device));
TestUtils.scrollTo(main.getString(R.string.menu_upload_element), false);
assertTrue(TestUtils.clickText(device, false, main.getString(R.string.menu_upload_element), true));


uploadDialog(R.string.upload_conflict_message_version, device);

assertTrue(TestUtils.clickText(device, false, main.getString(R.string.resolve), true));
assertTrue(TestUtils.clickText(device, false, main.getString(R.string.use_local_version), true));
assertTrue(TestUtils.findText(device, false, main.getString(R.string.confirm_upload_title), 5000));
assertFalse(TestUtils.findText(device, false, "Kindhauserstrasse"));
n = (Node) App.getDelegator().getOsmElement(Node.NAME, 101792984L);
assertEquals(7, n.getOsmVersion()); // version should now be the same as the server
}

/**
* Version conflict use the server element
*/
@Test
public void versionConflictUseServer() {
versionConflict("conflict1", new String[] { "conflictdownload1" }, false, R.string.upload_conflict_message_version);
conflict("conflict1", new String[] { "conflictdownload1" }, false, R.string.upload_conflict_message_version);
Node n = (Node) App.getDelegator().getOsmElement(Node.NAME, 101792984L);
assertNotNull(n);
assertEquals(6, n.getOsmVersion()); // version should now be server and not in the API
Expand All @@ -151,7 +181,7 @@ public void versionConflictUseServer() {
*/
@Test
public void severElementAlreadyDeleted() {
versionConflict("conflict2", new String[] { "410", "200" }, false, -1);
conflict("conflict2", new String[] { "410", "200" }, false, -1);
try {
final MockWebServer server = mockServer.server();
server.takeRequest(10L, TimeUnit.SECONDS);
Expand All @@ -171,7 +201,7 @@ public void severElementAlreadyDeleted() {
*/
@Test
public void severElementInUse() {
versionConflict("conflict3", new String[] { "way-210461100", "way-210461100-nodes", "relation-12345", "relation-12345", "empty" }, false, -1);
conflict("conflict3", new String[] { "way-210461100", "way-210461100-nodes", "relation-12345", "relation-12345", "empty" }, false, -1);
Way w = App.getDelegator().getApiStorage().getWay(210461100L);
assertNotNull(w);
assertEquals(OsmElement.STATE_DELETED, w.getState());
Expand All @@ -191,45 +221,38 @@ public void severElementInUse() {
*/
@Test
public void referencesMissing() {
versionConflict("conflict4", new String[] { "way-27009604", "way-27009604-nodes", "nodes-deleted" }, false,
conflict("conflict4", new String[] { "way-27009604", "way-27009604-nodes", "nodes-deleted" }, false,
R.string.upload_conflict_message_missing_references);
Way w = App.getDelegator().getApiStorage().getWay(27009604L);
assertTrue(TestUtils.clickText(device, false, main.getString(R.string.cancel), true));
}

/**
* Upload to changes (mock-)server and wait for version conflict dialog
* Upload to changes (mock-)server and wait for conflict dialog
*
* @param conflictReponse the response
* @param fixtures name of additional fixtures with the response to the upload
* @param userDetails if true enqueue user details
* @param waitForDialog wait for the conflict dialog if true
* @param titleRes title to expect
*/
private void versionConflict(@NonNull String conflictReponse, @NonNull String[] fixtures, boolean userDetails, int titleRes) {
final CountDownLatch signal = new CountDownLatch(1);
Logic logic = App.getLogic();

ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream is = loader.getResourceAsStream("test1.osm");
logic.readOsmFile(main, is, false, new SignalHandler(signal));
SignalUtils.signalAwait(signal, TIMEOUT);

mockServer.enqueue("capabilities1"); // for whatever reason this gets asked for twice
mockServer.enqueue("capabilities1");
mockServer.enqueue("changeset1");
mockServer.enqueue(conflictReponse);
if (userDetails) {
mockServer.enqueue("userdetails");
}
for (String fixture : fixtures) {
mockServer.enqueue(fixture);
}
private void conflict(@NonNull String conflictReponse, @NonNull String[] fixtures, boolean userDetails, int titleRes) {
loadDataAndFixtures(conflictReponse, fixtures, userDetails);

TestUtils.clickMenuButton(device, main.getString(R.string.menu_transfer), false, true);
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

TestUtils.clickText(device, false, main.getString(R.string.menu_transfer_upload), true, false); // menu item

uploadDialog(titleRes, device);
}

/**
* FIllout comment and source fields and upload
*
* @param titleRes title to expect
* @param device UiDevice
*/
private void uploadDialog(int titleRes, UiDevice device) {
UiSelector uiSelector = new UiSelector().className("android.widget.Button").instance(1); // dialog upload button
UiObject button = device.findObject(uiSelector);
try {
Expand All @@ -248,6 +271,34 @@ private void versionConflict(@NonNull String conflictReponse, @NonNull String[]
}
}

/**
* Load the data and fixtures
*
* @param conflictReponse the response
* @param fixtures name of additional fixtures with the response to the upload
* @param userDetails if true enqueue user details
*/
private void loadDataAndFixtures(String conflictReponse, String[] fixtures, boolean userDetails) {
final CountDownLatch signal = new CountDownLatch(1);
Logic logic = App.getLogic();

ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream is = loader.getResourceAsStream("test1.osm");
logic.readOsmFile(main, is, false, new SignalHandler(signal));
SignalUtils.signalAwait(signal, TIMEOUT);

mockServer.enqueue("capabilities1"); // for whatever reason this gets asked for twice
mockServer.enqueue("capabilities1");
mockServer.enqueue("changeset1");
mockServer.enqueue(conflictReponse);
if (userDetails) {
mockServer.enqueue("userdetails");
}
for (String fixture : fixtures) {
mockServer.enqueue(fixture);
}
}

/**
* Fill our comment and source fields
*
Expand Down

0 comments on commit 829a2ab

Please sign in to comment.