Wrote test for subtasks migration

pull/14/head
Sam Bosley 13 years ago
parent b230e45491
commit 4146038c77

@ -98,7 +98,7 @@ public class SubtasksMetadataMigration {
try {
JSONArray array = new JSONArray();
AstridOrderedListUpdater.recursivelySerializeChildren(root, new JSONArray());
AstridOrderedListUpdater.recursivelySerializeChildren(root, array);
return array.toString();
} catch (JSONException e) {
return "[]"; //$NON-NLS-1$

@ -0,0 +1,55 @@
package com.todoroo.astrid.subtasks;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.data.Metadata;
@SuppressWarnings("deprecation")
public class SubtasksMigrationTest extends SubtasksTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
Preferences.clear(SubtasksUpdater.ACTIVE_TASKS_ORDER);
createLegacyMetadata();
}
/* Starting State (see SubtasksTestCase):
*
* A
* B
* C
* D
* E
* F
*/
private void createLegacyMetadata() {
createSubtasksMetadata(1, 1, 0);
createSubtasksMetadata(2, 2, 1);
createSubtasksMetadata(3, 3, 1);
createSubtasksMetadata(4, 4, 2);
createSubtasksMetadata(5, 5, 0);
createSubtasksMetadata(6, 6, 0);
}
private void createSubtasksMetadata(long taskId, long order, int indent) {
Metadata m = new Metadata();
m.setValue(Metadata.KEY, SubtasksMetadata.METADATA_KEY);
m.setValue(Metadata.TASK, taskId);
m.setValue(SubtasksMetadata.TAG, SubtasksMetadata.LIST_ACTIVE_TASKS);
m.setValue(SubtasksMetadata.ORDER, order);
m.setValue(SubtasksMetadata.INDENT, indent);
PluginServices.getMetadataService().save(m);
}
public void testMigration() {
SubtasksMetadataMigration migrator = new SubtasksMetadataMigration();
migrator.performMigration();
String newSerializedTree = Preferences.getStringValue(SubtasksUpdater.ACTIVE_TASKS_ORDER).replaceAll("\\s", "");
String expectedSerializedTree = DEFAULT_SERIALIZED_TREE.replaceAll("\\s", "");
assertEquals(expectedSerializedTree, newSerializedTree);
}
}

@ -1,28 +1,17 @@
package com.todoroo.astrid.subtasks;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.core.CoreFilterExposer;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.subtasks.AstridOrderedListUpdater.Node;
import com.todoroo.astrid.test.DatabaseTestCase;
public class SubtasksMovingTest extends DatabaseTestCase {
public class SubtasksMovingTest extends SubtasksTestCase {
private SubtasksUpdater updater;
private Filter filter;
private Task A, B, C, D, E, F;
@Override
protected void setUp() throws Exception {
super.setUp();
filter = CoreFilterExposer.buildInboxFilter(getContext().getResources());
Preferences.clear(SubtasksUpdater.ACTIVE_TASKS_ORDER);
updater = new SubtasksUpdater();
createTasks();
updater.initializeFromSerializedTree(null, filter, getSerializedTree());
updater.initializeFromSerializedTree(null, filter, DEFAULT_SERIALIZED_TREE);
}
private void createTasks() {
@ -46,15 +35,7 @@ public class SubtasksMovingTest extends DatabaseTestCase {
updater.moveTo(null, filter, target.getId(), beforeId);
}
private void thenExpectParentAndPosition(Task task, Task parent, int positionInParent) {
long parentId = (parent == null ? -1 : parent.getId());
Node n = updater.findNodeForTask(task.getId());
assertNotNull("No node found for task " + task.getValue(Task.TITLE), n);
assertEquals("Parent mismatch", parentId, n.parent.taskId);
assertEquals("Position mismatch", positionInParent, n.parent.children.indexOf(n));
}
/* Starting State:
/* Starting State (see SubtasksTestCase):
*
* A
* B
@ -63,62 +44,59 @@ public class SubtasksMovingTest extends DatabaseTestCase {
* E
* F
*/
private String getSerializedTree() {
return "[{\"1\":[{\"2\":[]}, {\"3\":[{\"4\":[]}]}]}, {\"5\":[]}, {\"6\":[]}]";
}
public void testMoveBeforeIntoSelf() { // Should have no effect
whenTriggerMoveBefore(A, B);
thenExpectParentAndPosition(A, null, 0);
thenExpectParentAndPosition(B, A, 0);
thenExpectParentAndPosition(C, A, 1);
thenExpectParentAndPosition(D, C, 0);
thenExpectParentAndPosition(E, null, 1);
thenExpectParentAndPosition(F, null, 2);
expectParentAndPosition(A, null, 0);
expectParentAndPosition(B, A, 0);
expectParentAndPosition(C, A, 1);
expectParentAndPosition(D, C, 0);
expectParentAndPosition(E, null, 1);
expectParentAndPosition(F, null, 2);
}
public void testMoveIntoDescendant() { // Should have no effect
whenTriggerMoveBefore(A, C);
thenExpectParentAndPosition(A, null, 0);
thenExpectParentAndPosition(B, A, 0);
thenExpectParentAndPosition(C, A, 1);
thenExpectParentAndPosition(D, C, 0);
thenExpectParentAndPosition(E, null, 1);
thenExpectParentAndPosition(F, null, 2);
expectParentAndPosition(A, null, 0);
expectParentAndPosition(B, A, 0);
expectParentAndPosition(C, A, 1);
expectParentAndPosition(D, C, 0);
expectParentAndPosition(E, null, 1);
expectParentAndPosition(F, null, 2);
}
public void testMoveToEndOfChildren() { // Should have no effect
whenTriggerMoveBefore(A, E);
thenExpectParentAndPosition(A, null, 0);
thenExpectParentAndPosition(B, A, 0);
thenExpectParentAndPosition(C, A, 1);
thenExpectParentAndPosition(D, C, 0);
thenExpectParentAndPosition(E, null, 1);
thenExpectParentAndPosition(F, null, 2);
expectParentAndPosition(A, null, 0);
expectParentAndPosition(B, A, 0);
expectParentAndPosition(C, A, 1);
expectParentAndPosition(D, C, 0);
expectParentAndPosition(E, null, 1);
expectParentAndPosition(F, null, 2);
}
public void testStandardMove() {
whenTriggerMoveBefore(A, F);
thenExpectParentAndPosition(A, null, 1);
thenExpectParentAndPosition(B, A, 0);
thenExpectParentAndPosition(C, A, 1);
thenExpectParentAndPosition(D, C, 0);
thenExpectParentAndPosition(E, null, 0);
thenExpectParentAndPosition(F, null, 2);
expectParentAndPosition(A, null, 1);
expectParentAndPosition(B, A, 0);
expectParentAndPosition(C, A, 1);
expectParentAndPosition(D, C, 0);
expectParentAndPosition(E, null, 0);
expectParentAndPosition(F, null, 2);
}
public void testMoveToEndOfList() {
whenTriggerMoveBefore(A, null);
thenExpectParentAndPosition(A, null, 2);
thenExpectParentAndPosition(B, A, 0);
thenExpectParentAndPosition(C, A, 1);
thenExpectParentAndPosition(D, C, 0);
thenExpectParentAndPosition(E, null, 0);
thenExpectParentAndPosition(F, null, 1);
expectParentAndPosition(A, null, 2);
expectParentAndPosition(B, A, 0);
expectParentAndPosition(C, A, 1);
expectParentAndPosition(D, C, 0);
expectParentAndPosition(E, null, 0);
expectParentAndPosition(F, null, 1);
}
}

@ -0,0 +1,48 @@
package com.todoroo.astrid.subtasks;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.core.CoreFilterExposer;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.subtasks.AstridOrderedListUpdater.Node;
import com.todoroo.astrid.test.DatabaseTestCase;
/**
* Contains useful methods common to all subtasks tests
* @author Sam
*
*/
public class SubtasksTestCase extends DatabaseTestCase {
protected SubtasksUpdater updater;
protected Filter filter;
/* Starting State:
*
* A
* B
* C
* D
* E
* F
*/
public static final String DEFAULT_SERIALIZED_TREE =
"[{\"1\":[{\"2\":[]}, {\"3\":[{\"4\":[]}]}]}, {\"5\":[]}, {\"6\":[]}]";
@Override
protected void setUp() throws Exception {
super.setUp();
filter = CoreFilterExposer.buildInboxFilter(getContext().getResources());
Preferences.clear(SubtasksUpdater.ACTIVE_TASKS_ORDER);
updater = new SubtasksUpdater();
}
protected void expectParentAndPosition(Task task, Task parent, int positionInParent) {
long parentId = (parent == null ? -1 : parent.getId());
Node n = updater.findNodeForTask(task.getId());
assertNotNull("No node found for task " + task.getValue(Task.TITLE), n);
assertEquals("Parent mismatch", parentId, n.parent.taskId);
assertEquals("Position mismatch", positionInParent, n.parent.children.indexOf(n));
}
}
Loading…
Cancel
Save