mirror of https://github.com/tasks/tasks
First robolectric db test
parent
b92702dec8
commit
099867d131
@ -0,0 +1,16 @@
|
|||||||
|
package org.tasks;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
|
||||||
|
import com.todoroo.andlib.service.ContextManager;
|
||||||
|
|
||||||
|
public class Broadcaster {
|
||||||
|
|
||||||
|
public void sendOrderedBroadcast(Intent intent) {
|
||||||
|
Context context = ContextManager.getContext();
|
||||||
|
if(context != null) {
|
||||||
|
context.sendOrderedBroadcast(intent, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
package com.todoroo.andlib.service;
|
||||||
|
|
||||||
|
public class RobolectricTestDependencyInjector extends AbstractDependencyInjector {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public RobolectricTestDependencyInjector(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addInjectable(String field, Object injection) {
|
||||||
|
injectables.put(field, injection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "TestDI:" + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- static stuff
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Install TestDependencyInjector above other injectors
|
||||||
|
*/
|
||||||
|
public synchronized static RobolectricTestDependencyInjector initialize(String name) {
|
||||||
|
RobolectricTestDependencyInjector instance = new RobolectricTestDependencyInjector(name);
|
||||||
|
DependencyInjectionService.getInstance().addInjector(instance);
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an installed TestDependencyInjector
|
||||||
|
*/
|
||||||
|
public static void deinitialize(RobolectricTestDependencyInjector instance) {
|
||||||
|
DependencyInjectionService.getInstance().removeInjector(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,32 @@
|
|||||||
|
package com.todoroo.andlib.test;
|
||||||
|
|
||||||
|
import com.todoroo.andlib.service.RobolectricTestDependencyInjector;
|
||||||
|
|
||||||
|
import org.tasks.Broadcaster;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
abstract public class TodorooRobolectricTestCaseWithInjector extends TodorooRobolectricTestCase {
|
||||||
|
|
||||||
|
protected RobolectricTestDependencyInjector testInjector;
|
||||||
|
|
||||||
|
protected void addInjectables() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
testInjector = RobolectricTestDependencyInjector.initialize("test");
|
||||||
|
testInjector.addInjectable("broadcaster", mock(Broadcaster.class));
|
||||||
|
addInjectables();
|
||||||
|
|
||||||
|
super.setUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
super.tearDown();
|
||||||
|
|
||||||
|
RobolectricTestDependencyInjector.deinitialize(testInjector);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
package com.todoroo.astrid.model;
|
||||||
|
|
||||||
|
import com.todoroo.andlib.data.Property;
|
||||||
|
import com.todoroo.andlib.test.TodorooRobolectricTestCaseWithInjector;
|
||||||
|
import com.todoroo.astrid.data.Task;
|
||||||
|
import com.todoroo.astrid.service.TaskService;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.tasks.Snippet;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.tasks.Freeze.freezeClock;
|
||||||
|
import static org.tasks.RemoteModelHelpers.compareRemoteModel;
|
||||||
|
import static org.tasks.RemoteModelHelpers.asQueryProperties;
|
||||||
|
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
public class TaskTest extends TodorooRobolectricTestCaseWithInjector {
|
||||||
|
|
||||||
|
private TaskService taskService;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() {
|
||||||
|
taskService = new TaskService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void newTaskHasNoCreationDate() {
|
||||||
|
assertFalse(new Task().containsValue(Task.CREATION_DATE));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void savedTaskHasCreationDate() {
|
||||||
|
freezeClock().thawAfter(new Snippet() {{
|
||||||
|
Task task = new Task();
|
||||||
|
taskService.save(task);
|
||||||
|
assertEquals(currentTimeMillis(), (long) task.getValue(Task.CREATION_DATE));
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void readTaskFromDb() {
|
||||||
|
Task task = new Task();
|
||||||
|
taskService.save(task);
|
||||||
|
Property[] properties = asQueryProperties(Task.TABLE, task.getDatabaseValues());
|
||||||
|
final Task fromDb = taskService.fetchById(task.getId(), properties);
|
||||||
|
compareRemoteModel(task, fromDb);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,53 @@
|
|||||||
|
package org.tasks;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
import android.content.ContentValues;
|
||||||
|
|
||||||
|
import com.todoroo.andlib.data.Property;
|
||||||
|
import com.todoroo.andlib.data.Table;
|
||||||
|
import com.todoroo.astrid.data.RemoteModel;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
|
public class RemoteModelHelpers {
|
||||||
|
public static Property[] asQueryProperties(Table table, ContentValues contentValues) {
|
||||||
|
Set<String> keys = contentValues.keySet();
|
||||||
|
Property[] result = new Property[keys.size()];
|
||||||
|
int index = 0;
|
||||||
|
for(String key : keys) {
|
||||||
|
result[index++] = new Property.StringProperty(table, key);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void compareRemoteModel(RemoteModel expected, RemoteModel actual) {
|
||||||
|
compareContentValues(expected.getSetValues(), actual.getSetValues());
|
||||||
|
compareContentValues(expected.getDatabaseValues(), actual.getDatabaseValues());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void compareContentValues(ContentValues expected, ContentValues actual) {
|
||||||
|
if(expected == null && actual == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(expected == null || actual == null) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
for(String key : expected.keySet()) {
|
||||||
|
Object entry = expected.get(key);
|
||||||
|
if(entry instanceof Integer) {
|
||||||
|
assertEquals(entry, actual.getAsInteger(key));
|
||||||
|
} else if(entry instanceof String) {
|
||||||
|
assertEquals(entry, actual.getAsString(key));
|
||||||
|
} else if(entry instanceof Long) {
|
||||||
|
assertEquals(entry, actual.getAsLong(key));
|
||||||
|
} else {
|
||||||
|
fail("Unhandled property type: " + entry.getClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue