Inject tests with dagger

pull/189/head
Alex Baker 11 years ago
parent 06ba4e9441
commit 1cc6698ad7

@ -1,61 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.andlib.service;
import android.content.Context;
import java.lang.reflect.Field;
import java.util.HashMap;
/**
* A Dependency Injector knows how to inject certain dependencies based
* on the field that is passed in. You will need to write your own initialization
* code to insert this dependency injector into the DI chain.
*
* @author Tim Su <tim@todoroo.com>
*
*/
abstract public class AbstractDependencyInjector {
/**
* Initialize list of injection variables. Special care must used when
* instantiating classes that themselves depend on dependency injection
*/
protected void addInjectables(Context context) {
// your injectables here
}
// ---
/**
* Constructor
*/
protected AbstractDependencyInjector(Context context) {
addInjectables(context);
}
/**
* Dependencies this class knows how to handle
*/
protected final HashMap<String, Object> injectables = new HashMap<>();
/**
* Gets the injected object for this field. If implementing class does not
* know how to handle this dependency, it should return null
*
* @param field
* field tagged with {link Autowired} annotation
* @return object to assign to this field, or null
*/
public Object getInjection(Field field) {
return injectables.get(field.getName());
}
@Override
public String toString() {
return getClass().getSimpleName();
}
}

@ -1,24 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.andlib.service;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Autowired is an annotation that tells the dependency injector to
* set up the class as appropriate.
*
* @author Tim Su <tim@todoroo.com>
*
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {
//
}

@ -1,149 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.andlib.service;
import java.lang.reflect.Field;
import java.util.LinkedList;
/**
* Simple Dependency Injection Service for Android.
* <p>
* Add dependency injectors to the injector chain, then invoke this method
* against classes you wish to perform dependency injection for.
* <p>
* All errors encountered are handled as warnings, so if dependency injection
* seems to be failing, check the logs for more information.
*
* @author Tim Su <tim@todoroo.com>
*
*/
public class DependencyInjectionService {
/**
* Dependency injectors. Use getters and setters to modify this list
*/
private final LinkedList<AbstractDependencyInjector> injectors = new LinkedList<>();
/**
* Perform dependency injection in the caller object
*
* @param caller
* object to perform DI on
*/
public void inject(Object caller) {
// Traverse through class and all parent classes, looking for
// fields declared with the @Autowired annotation and using
// dependency injection to set them as appropriate
Class<?> cls = caller.getClass();
while(cls != null) {
String packageName = cls.getPackage().getName();
if(!isQualifiedPackage(packageName)) {
break;
}
for(Field field : cls.getDeclaredFields()) {
if(field.getAnnotation(Autowired.class) != null) {
field.setAccessible(true);
try {
handleField(caller, field);
} catch (IllegalStateException | IllegalAccessException | IllegalArgumentException e) {
throw new RuntimeException(String.format("Unable to set field '%s' of type '%s'",
field.getName(), field.getType()), e);
}
}
}
cls = cls.getSuperclass();
}
}
private boolean isQualifiedPackage(String packageName) {
if(packageName.startsWith("com.todoroo")) {
return true;
}
if(packageName.startsWith("org.weloveastrid")) {
return true;
}
if(packageName.startsWith("org.tasks")) {
return true;
}
return false;
}
/**
* This method returns the appropriate dependency object based on the type
* that this autowired field accepts
*
* @param caller
* calling object
* @param field
* field to inject
*/
private synchronized void handleField(Object caller, Field field)
throws IllegalStateException, IllegalArgumentException,
IllegalAccessException {
if(field.getType().isPrimitive()) {
throw new IllegalStateException(String.format(
"Tried to dependency-inject primative field '%s' of type '%s'",
field.getName(), field.getType()));
}
// field has already been processed, ignore
if (field.get(caller) != null) {
return;
}
for (AbstractDependencyInjector injector : injectors) {
Object injection = injector.getInjection(field);
if (injection != null) {
field.set(caller, injection);
return;
}
}
throw new IllegalStateException(
String.format("No dependency injector found for autowired " +
"field '%s' in class '%s'. Injectors: %s",
field.getName(), caller.getClass().getName(),
injectors));
}
// --- static methods
private static DependencyInjectionService instance = null;
DependencyInjectionService() {
}
/**
* Gets the singleton instance of the dependency injection service.
*/
public synchronized static DependencyInjectionService getInstance() {
if(instance == null) {
instance = new DependencyInjectionService();
}
return instance;
}
/**
* Removes the supplied injector
*/
public synchronized void removeInjector(AbstractDependencyInjector injector) {
injectors.remove(injector);
}
/**
* Adds a Dependency Injector to the front of the list
*/
public synchronized void addInjector(AbstractDependencyInjector injector) {
removeInjector(injector);
this.injectors.addFirst(injector);
}
}

@ -1,45 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.andlib.service;
public class TestDependencyInjector extends AbstractDependencyInjector {
private String name;
public TestDependencyInjector(String name) {
super(null);
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 TestDependencyInjector initialize(String name) {
TestDependencyInjector instance = new TestDependencyInjector(name);
DependencyInjectionService.getInstance().addInjector(instance);
return instance;
}
/**
* Remove an installed TestDependencyInjector
*/
public static void deinitialize(TestDependencyInjector instance) {
DependencyInjectionService.getInstance().removeInjector(instance);
}
}

@ -9,9 +9,7 @@ import android.content.res.Configuration;
import android.test.AndroidTestCase;
import android.util.DisplayMetrics;
import com.todoroo.astrid.service.AstridDependencyInjector;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService;
import java.util.Locale;
@ -21,21 +19,27 @@ import java.util.Locale;
* @author Tim Su <tim@todoroo.com>
*
*/
public class TodorooTestCase extends AndroidTestCase {
public abstract class TodorooTestCase extends AndroidTestCase {
@Override
protected void setUp() throws Exception {
protected void setUp() {
try {
super.setUp();
} catch(Exception e) {
throw new RuntimeException(e);
}
ContextManager.setContext(this.getContext());
AstridDependencyInjector.reset(getContext());
DependencyInjectionService.getInstance().inject(this);
ContextManager.setContext(getContext());
setLocale(Locale.ENGLISH);
}
@Override
protected void tearDown() throws Exception {
protected void tearDown() {
try {
super.tearDown();
} catch(Exception e) {
throw new RuntimeException(e);
}
setLocale(Locale.getDefault());
}

@ -1,37 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.andlib.test;
import com.todoroo.andlib.service.TestDependencyInjector;
/**
* Base test case for Astrid tests that need a separate injection context
*
* @author Tim Su <tim@todoroo.com>
*
*/
abstract public class TodorooTestCaseWithInjector extends TodorooTestCase {
protected TestDependencyInjector testInjector;
abstract protected void addInjectables();
@Override
protected void setUp() throws Exception {
testInjector = TestDependencyInjector.initialize("test");
addInjectables();
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
TestDependencyInjector.deinitialize(testInjector);
}
}

@ -5,7 +5,6 @@
*/
package com.todoroo.astrid.backup;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.astrid.backup.BackupService.BackupDirectorySetting;
import com.todoroo.astrid.dao.TaskDao;
@ -20,6 +19,8 @@ import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.inject.Inject;
import static org.tasks.date.DateTimeUtils.newDate;
public class BackupServiceTests extends DatabaseTestCase {
@ -28,8 +29,8 @@ public class BackupServiceTests extends DatabaseTestCase {
File temporaryDirectory = null;
@Autowired private TaskDao taskDao;
@Autowired private Preferences preferences;
@Inject TaskDao taskDao;
@Inject Preferences preferences;
BackupDirectorySetting setting = new BackupDirectorySetting() {
public File getBackupDirectory() {
@ -38,15 +39,19 @@ public class BackupServiceTests extends DatabaseTestCase {
};
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
temporaryDirectory = File.createTempFile("backup",
Long.toString(System.nanoTime()));
try {
temporaryDirectory = File.createTempFile("backup", Long.toString(System.nanoTime()));
} catch (IOException e) {
throw new RuntimeException(e);
}
if (!(temporaryDirectory.delete()))
throw new IOException("Could not delete temp file: " + temporaryDirectory.getAbsolutePath());
throw new RuntimeException("Could not delete temp file: " + temporaryDirectory.getAbsolutePath());
if (!(temporaryDirectory.mkdir()))
throw new IOException("Could not create temp directory: " + temporaryDirectory.getAbsolutePath());
throw new RuntimeException("Could not create temp directory: " + temporaryDirectory.getAbsolutePath());
// make a temporary task
Task task = new Task();
@ -55,7 +60,7 @@ public class BackupServiceTests extends DatabaseTestCase {
}
@Override
protected void tearDown() throws Exception {
protected void tearDown() {
super.tearDown();
if (temporaryDirectory != null) {
@ -73,7 +78,6 @@ public class BackupServiceTests extends DatabaseTestCase {
preferences.setBoolean(R.string.backup_BPr_auto_key, setting);
}
/** Test backup works */
public void testBackup() {
assertEquals(0, temporaryDirectory.list().length);
@ -102,7 +106,6 @@ public class BackupServiceTests extends DatabaseTestCase {
}
}
/** Test no backup */
public void disabled_testNoBackup() {
assertEquals(0, temporaryDirectory.list().length);
System.err.println("test no backup");

@ -7,18 +7,18 @@ package com.todoroo.astrid.dao;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.test.DatabaseTestCase;
public class MetadataDaoTests extends DatabaseTestCase {
import javax.inject.Inject;
@Autowired MetadataDao metadataDao;
public class MetadataDaoTests extends DatabaseTestCase {
@Autowired TaskDao taskDao;
@Inject MetadataDao metadataDao;
@Inject TaskDao taskDao;
public static Property<?>[] KEYS = new Property<?>[] { Metadata.ID,
Metadata.KEY };

@ -7,7 +7,6 @@ package com.todoroo.astrid.dao;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.sql.Order;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
@ -15,6 +14,8 @@ import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.test.DatabaseTestCase;
import javax.inject.Inject;
public class TaskDaoTests extends DatabaseTestCase {
public static Property<?>[] IDS = new Property<?>[] { Task.ID };
@ -22,7 +23,7 @@ public class TaskDaoTests extends DatabaseTestCase {
public static Property<?>[] TITLES = new Property<?>[] { Task.ID,
Task.TITLE };
@Autowired TaskDao taskDao;
@Inject TaskDao taskDao;
/**
* Test basic task creation, fetch, and save

@ -7,7 +7,6 @@ package com.todoroo.astrid.gtasks;
import com.google.api.services.tasks.model.TaskList;
import com.google.api.services.tasks.model.TaskLists;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.StoreObject;
import com.todoroo.astrid.data.Task;
@ -18,14 +17,16 @@ import com.todoroo.astrid.test.DatabaseTestCase;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
@SuppressWarnings("nls")
public class GtasksIndentActionTest extends DatabaseTestCase {
@Autowired private GtasksMetadataService gtasksMetadataService;
@Autowired private GtasksListService gtasksListService;
@Autowired private GtasksTaskListUpdater gtasksTaskListUpdater;
@Autowired private MetadataService metadataService;
@Autowired TaskService taskService;
@Inject GtasksMetadataService gtasksMetadataService;
@Inject GtasksListService gtasksListService;
@Inject GtasksTaskListUpdater gtasksTaskListUpdater;
@Inject MetadataService metadataService;
@Inject TaskService taskService;
private Task task;
private StoreObject storeList;
@ -136,7 +137,7 @@ public class GtasksIndentActionTest extends DatabaseTestCase {
}
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
TaskLists lists = new TaskLists();

@ -8,7 +8,6 @@ package com.todoroo.astrid.gtasks;
import android.content.Context;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.data.Metadata;
@ -17,13 +16,36 @@ import com.todoroo.astrid.service.MetadataService;
import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.test.DatabaseTestCase;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import dagger.Module;
import dagger.Provides;
@SuppressWarnings("nls")
public class GtasksMetadataServiceTest extends DatabaseTestCase {
private final GtasksTestPreferenceService preferences = new GtasksTestPreferenceService();
@Autowired private GtasksMetadataService gtasksMetadataService;
@Autowired private MetadataService metadataService;
@Autowired TaskService taskService;
@Module(complete = false, overrides = true, injects = { GtasksMetadataServiceTest.class })
static class GtasksMetadataServiceTestModule {
private final GtasksTestPreferenceService service = new GtasksTestPreferenceService();
@Provides
public GtasksTestPreferenceService getGtasksTestPreferenceService() {
return service;
}
@Provides
public GtasksPreferenceService getGtasksPreferenceService() {
return service;
}
}
@Inject GtasksTestPreferenceService preferences;
@Inject GtasksMetadataService gtasksMetadataService;
@Inject MetadataService metadataService;
@Inject TaskService taskService;
private Task task;
private Metadata metadata;
@ -156,12 +178,6 @@ public class GtasksMetadataServiceTest extends DatabaseTestCase {
return task;
}
@Override
protected void addInjectables() {
super.addInjectables();
testInjector.addInjectable("gtasksPreferenceService", preferences);
}
@Override
public void setContext(Context context) {
super.setContext(context);
@ -169,4 +185,10 @@ public class GtasksMetadataServiceTest extends DatabaseTestCase {
Preferences.setString(GtasksPreferenceService.PREF_DEFAULT_LIST, "list");
}
@Override
protected List<Object> getModules() {
return new ArrayList<Object>() {{
add(new GtasksMetadataServiceTestModule());
}};
}
}

@ -7,7 +7,6 @@ package com.todoroo.astrid.gtasks;
import com.google.api.services.tasks.model.TaskList;
import com.google.api.services.tasks.model.TaskLists;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.StoreObject;
import com.todoroo.astrid.data.Task;
@ -18,14 +17,16 @@ import com.todoroo.astrid.test.DatabaseTestCase;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
@SuppressWarnings("nls")
public class GtasksTaskListUpdaterTest extends DatabaseTestCase {
@Autowired private GtasksTaskListUpdater gtasksTaskListUpdater;
@Autowired private GtasksListService gtasksListService;
@Autowired private GtasksMetadataService gtasksMetadataService;
@Autowired private MetadataService metadataService;
@Autowired TaskService taskService;
@Inject GtasksTaskListUpdater gtasksTaskListUpdater;
@Inject GtasksListService gtasksListService;
@Inject GtasksMetadataService gtasksMetadataService;
@Inject MetadataService metadataService;
@Inject TaskService taskService;
public void testBasicParentComputation() {
Task[] tasks = givenTasksABCDE();
@ -123,7 +124,7 @@ public class GtasksTaskListUpdaterTest extends DatabaseTestCase {
}
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
TaskLists lists = new TaskLists();

@ -7,7 +7,6 @@ package com.todoroo.astrid.gtasks;
import com.google.api.services.tasks.model.TaskList;
import com.google.api.services.tasks.model.TaskLists;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.StoreObject;
import com.todoroo.astrid.data.Task;
@ -18,14 +17,16 @@ import com.todoroo.astrid.test.DatabaseTestCase;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
@SuppressWarnings("nls")
public class GtasksTaskMovingTest extends DatabaseTestCase {
@Autowired private GtasksListService gtasksListService;
@Autowired private GtasksMetadataService gtasksMetadataService;
@Autowired private GtasksTaskListUpdater gtasksTaskListUpdater;
@Autowired private MetadataService metadataService;
@Autowired TaskService taskService;
@Inject GtasksListService gtasksListService;
@Inject GtasksMetadataService gtasksMetadataService;
@Inject GtasksTaskListUpdater gtasksTaskListUpdater;
@Inject MetadataService metadataService;
@Inject TaskService taskService;
private Task A, B, C, D, E, F;
private StoreObject list;
@ -240,7 +241,7 @@ public class GtasksTaskMovingTest extends DatabaseTestCase {
}
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
TaskLists lists = new TaskLists();

@ -3,23 +3,26 @@ package com.todoroo.astrid.model;
import android.content.ContentValues;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.test.TodorooTestCase;
import org.tasks.injection.InjectingTestCase;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.TaskService;
import org.tasks.Snippet;
import org.tasks.preferences.Preferences;
import javax.inject.Inject;
import static org.tasks.Freeze.freezeClock;
import static org.tasks.RemoteModelHelpers.asQueryProperties;
import static org.tasks.RemoteModelHelpers.compareRemoteModel;
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
public class TaskTest extends TodorooTestCase {
public class TaskTest extends InjectingTestCase {
@Autowired private TaskService taskService;
@Autowired private Preferences preferences;
@Inject TaskService taskService;
@Inject Preferences preferences;
public void testNewTaskHasNoCreationDate() {
assertFalse(new Task().containsValue(Task.CREATION_DATE));

@ -26,7 +26,7 @@ public class Astrid3ProviderTests extends DatabaseTestCase {
};
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
// set up database

@ -8,7 +8,6 @@ package com.todoroo.astrid.reminders;
import android.app.Notification;
import android.content.Intent;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.NotificationManager;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
@ -18,24 +17,26 @@ import com.todoroo.astrid.test.DatabaseTestCase;
import org.tasks.R;
import javax.inject.Inject;
import static org.tasks.date.DateTimeUtils.newDate;
public class NotificationTests extends DatabaseTestCase {
@Autowired TaskDao taskDao;
@Inject TaskDao taskDao;
public class MutableBoolean {
boolean value = false;
}
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
Notifications.forceNotificationManager();
}
@Override
protected void tearDown() throws Exception {
protected void tearDown() {
Notifications.setNotificationManager(null);
}

@ -2,7 +2,6 @@ package com.todoroo.astrid.reminders;
import android.annotation.SuppressLint;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.test.TodorooTestCase;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.dao.TaskDao;
@ -14,6 +13,8 @@ import org.tasks.R;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import static com.todoroo.astrid.reminders.ReminderService.NO_ALARM;
import static org.tasks.Freeze.freezeAt;
import static org.tasks.Freeze.thaw;
@ -24,21 +25,21 @@ public class NotifyAtDeadlineTest extends TodorooTestCase {
@SuppressLint("NewApi")
private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1);
@Autowired private TaskDao taskDao;
@Autowired private ReminderService reminderService;
@Inject TaskDao taskDao;
@Inject ReminderService reminderService;
private final Task dueAtNoon = new Task() {{
setDueDate(Task.URGENCY_SPECIFIC_DAY, newDate(2014, 1, 27).getTime());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
}};
public void setUp() throws Exception {
public void setUp() {
super.setUp();
freezeAt(new DateTime(2014, 1, 24, 17, 23, 37));
reminderService.setPreferenceDefaults();
}
public void tearDown() throws Exception {
public void tearDown() {
thaw();
}

@ -7,28 +7,29 @@ package com.todoroo.astrid.reminders;
import android.content.Context;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.test.TodorooTestCase;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.reminders.ReminderService.AlarmScheduler;
import javax.inject.Inject;
import static org.tasks.Freeze.freezeClock;
import static org.tasks.Freeze.thaw;
import static org.tasks.date.DateTimeUtils.newDate;
public class ReminderServiceTest extends TodorooTestCase {
@Autowired TaskDao taskDao;
@Autowired ReminderService reminderService;
@Inject TaskDao taskDao;
@Inject ReminderService reminderService;
public void setUp() throws Exception {
public void setUp() {
super.setUp();
freezeClock();
}
public void tearDown() throws Exception {
public void tearDown() {
thaw();
}

@ -33,7 +33,7 @@ public class AdvancedRepeatTest extends TodorooTestCase {
private RRule rrule;
@Override
public void setUp() throws Exception {
public void setUp() {
super.setUp();
task = new Task();
task.setCompletionDate(DateUtilities.now());

@ -12,7 +12,6 @@ import com.google.ical.values.RRule;
import com.google.ical.values.Weekday;
import com.google.ical.values.WeekdayNum;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
@ -31,14 +30,16 @@ import java.util.Date;
import java.util.HashSet;
import java.util.List;
import javax.inject.Inject;
import static org.tasks.date.DateTimeUtils.newDate;
public class NewRepeatTests<REMOTE_MODEL> extends DatabaseTestCase {
@Autowired protected TaskDao taskDao;
@Inject TaskDao taskDao;
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
Preferences.setStringFromInteger(R.string.p_default_urgency_key, 0);
}

@ -1,186 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.service;
import android.content.Context;
import com.todoroo.andlib.service.AbstractDependencyInjector;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.MetadataDao;
import com.todoroo.astrid.dao.StoreObjectDao;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TagMetadataDao;
import com.todoroo.astrid.dao.TaskAttachmentDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskListMetadataDao;
import com.todoroo.astrid.dao.UserActivityDao;
import com.todoroo.astrid.gtasks.GtasksListService;
import com.todoroo.astrid.gtasks.GtasksMetadataService;
import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.gtasks.GtasksScheduler;
import com.todoroo.astrid.gtasks.GtasksTaskListUpdater;
import com.todoroo.astrid.gtasks.sync.GtasksSyncService;
import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider;
import com.todoroo.astrid.reminders.ReminderService;
import com.todoroo.astrid.tags.TagService;
import org.tasks.Broadcaster;
import org.tasks.filters.FilterCounter;
import org.tasks.injection.TestInjector;
import org.tasks.preferences.Preferences;
import org.tasks.scheduling.RefreshScheduler;
import org.tasks.widget.WidgetHelper;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.tasks.injection.TasksModule.ForApplication;
/**
* Astrid application dependency injector loads classes in Astrid with the
* appropriate instantiated objects necessary for their operation. For
* more information on Dependency Injection, see {@link com.todoroo.andlib.service.DependencyInjectionService}
* and {@link com.todoroo.andlib.service.AbstractDependencyInjector}.
*
* @author Tim Su <tim@todoroo.com>
*
*/
public class AstridDependencyInjector extends AbstractDependencyInjector {
@Module(
injects = {
AstridDependencyInjector.class
}
)
public static class TestModule {
private Context context;
public TestModule(Context context) {
this.context = context;
}
@Singleton
@Provides
@ForApplication
public Context getContext() {
return context;
}
}
/**
* Boolean bit to prevent multiple copies of this injector to be loaded
*/
private static AstridDependencyInjector instance = null;
@Inject Database database;
@Inject MetadataDao metadataDao;
@Inject TagDataDao tagDataDao;
@Inject Broadcaster broadcaster;
@Inject TaskDao taskDao;
@Inject TagMetadataDao tagMetadataDao;
@Inject StoreObjectDao storeObjectDao;
@Inject UserActivityDao userActivityDao;
@Inject TaskAttachmentDao taskAttachmentDao;
@Inject TaskListMetadataDao taskListMetadataDao;
@Inject TagDataService tagDataService;
@Inject MetadataService metadataService;
@Inject SyncV2Service syncV2Service;
@Inject FilterCounter filterCounter;
@Inject RefreshScheduler refreshScheduler;
@Inject TaskService taskService;
@Inject TagService tagService;
@Inject UpgradeService upgradeService;
@Inject GtasksPreferenceService gtasksPreferenceService;
@Inject GtasksListService gtasksListService;
@Inject GtasksMetadataService gtasksMetadataService;
@Inject GtasksSyncService gtasksSyncService;
@Inject GtasksTaskListUpdater gtasksTaskListUpdater;
@Inject GtasksSyncV2Provider gtasksSyncV2Provider;
@Inject WidgetHelper widgetHelper;
@Inject GtasksScheduler gtasksScheduler;
@Inject ReminderService reminderService;
@Inject Preferences preferences;
/**
* Initialize list of injectables. Special care must used when
* instantiating classes that themselves depend on dependency injection
*/
@Override
protected void addInjectables(Context context) {
new TestInjector(context)
.inject(this, new TestModule(context));
// com.todoroo.astrid.dao
injectables.put("database", database);
injectables.put("taskDao", taskDao);
injectables.put("metadataDao", metadataDao);
injectables.put("tagMetadataDao", tagMetadataDao);
injectables.put("tagDataDao", tagDataDao);
injectables.put("storeObjectDao", storeObjectDao);
injectables.put("userActivityDao", userActivityDao);
injectables.put("taskAttachmentDao", taskAttachmentDao);
injectables.put("taskListMetadataDao", taskListMetadataDao);
// com.todoroo.astrid.service
injectables.put("taskService", taskService);
injectables.put("metadataService", metadataService);
injectables.put("tagDataService", tagDataService);
injectables.put("upgradeService", upgradeService);
injectables.put("syncService", syncV2Service);
// com.todoroo.astrid.gtasks
injectables.put("gtasksPreferenceService", gtasksPreferenceService);
injectables.put("gtasksListService", gtasksListService);
injectables.put("gtasksMetadataService", gtasksMetadataService);
injectables.put("gtasksTaskListUpdater", gtasksTaskListUpdater);
injectables.put("gtasksSyncService", gtasksSyncService);
injectables.put("gtasksSyncV2Provider", gtasksSyncV2Provider);
// com.todoroo.astrid.tags
injectables.put("tagService", tagService);
injectables.put("broadcaster", broadcaster);
injectables.put("filterCounter", filterCounter);
injectables.put("refreshScheduler", refreshScheduler);
injectables.put("widgetHelper", widgetHelper);
injectables.put("gtasksScheduler", gtasksScheduler);
injectables.put("reminderService", reminderService);
injectables.put("preferences", preferences);
}
/**
* Install this service as the default Dependency Injector
*/
private static void initialize(Context context) {
if(instance != null) {
return;
}
synchronized(AstridDependencyInjector.class) {
if(instance == null) {
instance = new AstridDependencyInjector(context);
}
DependencyInjectionService.getInstance().addInjector(instance);
}
}
AstridDependencyInjector(Context context) {
// prevent instantiation
super(context);
}
/**
* Flush dependency injection cache. Useful for unit tests.
*/
public synchronized static void reset(Context context) {
instance = null;
initialize(context);
}
}

@ -5,7 +5,6 @@
*/
package com.todoroo.astrid.service;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.tags.TagService;
import com.todoroo.astrid.test.DatabaseTestCase;
@ -14,12 +13,14 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.inject.Inject;
public class QuickAddMarkupTest extends DatabaseTestCase {
@Autowired TagService tagService;
@Inject TagService tagService;
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
}

@ -8,7 +8,6 @@ package com.todoroo.astrid.service;
import com.google.ical.values.Frequency;
import com.google.ical.values.RRule;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.tags.TagService;
@ -21,18 +20,20 @@ import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.inject.Inject;
import static org.tasks.date.DateTimeUtils.newDate;
public class TitleParserTest extends DatabaseTestCase {
@Autowired TaskService taskService;
@Inject TaskService taskService;
@Autowired TagService tagService;
@Inject TagService tagService;
@Autowired MetadataService metadataService;
@Inject MetadataService metadataService;
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
Preferences.setStringFromInteger(R.string.p_default_urgency_key, 0);
}

@ -1,16 +1,17 @@
package com.todoroo.astrid.subtasks;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskListMetadata;
import com.todoroo.astrid.service.TaskService;
import javax.inject.Inject;
public class SubtasksHelperTest extends SubtasksTestCase {
@Autowired TaskService taskService;
@Inject TaskService taskService;
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
createTasks();
TaskListMetadata m = new TaskListMetadata();

@ -1,13 +1,14 @@
package com.todoroo.astrid.subtasks;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskListMetadata;
import com.todoroo.astrid.service.TaskService;
import javax.inject.Inject;
public class SubtasksMovingTest extends SubtasksTestCase {
@Autowired TaskService taskService;
@Inject TaskService taskService;
private Task A, B, C, D, E, F;

@ -1,6 +1,5 @@
package com.todoroo.astrid.subtasks;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.core.CoreFilterExposer;
import com.todoroo.astrid.dao.TaskListMetadataDao;
@ -12,6 +11,8 @@ import com.todoroo.astrid.test.DatabaseTestCase;
import org.tasks.preferences.Preferences;
import javax.inject.Inject;
/**
* Contains useful methods common to all subtasks tests
* @author Sam
@ -19,9 +20,9 @@ import org.tasks.preferences.Preferences;
*/
public class SubtasksTestCase extends DatabaseTestCase {
@Autowired TaskListMetadataDao taskListMetadataDao;
@Autowired TaskService taskService;
@Autowired Preferences preferences;
@Inject TaskListMetadataDao taskListMetadataDao;
@Inject TaskService taskService;
@Inject Preferences preferences;
protected SubtasksUpdater<TaskListMetadata> updater;
protected Filter filter;
@ -38,7 +39,7 @@ public class SubtasksTestCase extends DatabaseTestCase {
public static final String DEFAULT_SERIALIZED_TREE = "[-1, [1, 2, [3, 4]], 5, 6]".replaceAll("\\s", "");
@Override
protected void setUp() throws Exception {
protected void setUp() {
super.setUp();
filter = CoreFilterExposer.buildInboxFilter(getContext().getResources());
preferences.clear(SubtasksUpdater.ACTIVE_TASKS_ORDER);

@ -1,20 +1,17 @@
package com.todoroo.astrid.sync;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.test.DatabaseTestCase;
import javax.inject.Inject;
public class NewSyncTestCase extends DatabaseTestCase {
@Autowired protected TaskDao taskDao;
@Autowired protected TagDataDao tagDataDao;
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Inject TaskDao taskDao;
@Inject TagDataDao tagDataDao;
protected Task createTask(String title) {
Task task = new Task();

@ -5,49 +5,27 @@
*/
package com.todoroo.astrid.test;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.test.TodorooTestCaseWithInjector;
import org.tasks.injection.InjectingTestCase;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.provider.Astrid3ContentProvider;
/**
* Test case that automatically sets up and tears down a test database
*
* @author Tim Su <tim@todoroo.com>
*
*/
public class DatabaseTestCase extends TodorooTestCaseWithInjector {
import javax.inject.Inject;
public static Database database = new TestDatabase();
public class DatabaseTestCase extends InjectingTestCase {
@Override
protected void addInjectables() {
testInjector.addInjectable("database", database);
}
@Inject protected Database database;
@Override
protected void setUp() throws Exception {
// call upstream setup, which invokes dependency injector
protected void setUp() {
super.setUp();
// empty out test databases
assertNotNull(ContextManager.getContext());
database.clear();
database.openForWriting();
Astrid3ContentProvider.setDatabaseOverride(database);
}
@Override
protected void tearDown() throws Exception {
protected void tearDown() {
database.close();
super.tearDown();
}
public static class TestDatabase extends Database {
@Override
public String getName() {
return "databasetest";
}
}
}

@ -0,0 +1,21 @@
package org.tasks.injection;
import com.todoroo.andlib.test.TodorooTestCase;
import java.util.List;
import static java.util.Collections.emptyList;
public abstract class InjectingTestCase extends TodorooTestCase {
@Override
protected void setUp() {
super.setUp();
new TestInjector(getContext()).inject(this, getModules().toArray());
}
protected List<Object> getModules() {
return emptyList();
}
}

@ -9,7 +9,7 @@ public class TestInjector implements Injector {
ObjectGraph objectGraph;
public TestInjector(Context context) {
objectGraph = ObjectGraph.create(new TasksModule(context));
objectGraph = ObjectGraph.create(new TestModule(context));
}
@Override

@ -0,0 +1,66 @@
package org.tasks.injection;
import android.content.Context;
import com.todoroo.astrid.backup.BackupServiceTests;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.MetadataDaoTests;
import com.todoroo.astrid.dao.TaskDaoTests;
import com.todoroo.astrid.gtasks.GtasksIndentActionTest;
import com.todoroo.astrid.gtasks.GtasksTaskListUpdaterTest;
import com.todoroo.astrid.gtasks.GtasksTaskMovingTest;
import com.todoroo.astrid.model.TaskTest;
import com.todoroo.astrid.provider.Astrid3ProviderTests;
import com.todoroo.astrid.reminders.NotificationTests;
import com.todoroo.astrid.repeats.NewRepeatTests;
import com.todoroo.astrid.service.QuickAddMarkupTest;
import com.todoroo.astrid.service.TitleParserTest;
import com.todoroo.astrid.subtasks.SubtasksTestCase;
import com.todoroo.astrid.sync.NewSyncTestCase;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module(injects = {
BackupServiceTests.class,
MetadataDaoTests.class,
TaskDaoTests.class,
GtasksIndentActionTest.class,
GtasksTaskListUpdaterTest.class,
GtasksTaskMovingTest.class,
Astrid3ProviderTests.class,
NotificationTests.class,
NewRepeatTests.class,
QuickAddMarkupTest.class,
TitleParserTest.class,
SubtasksTestCase.class,
NewSyncTestCase.class,
TaskTest.class
})
public class TestModule {
private Context context;
public TestModule(Context context) {
this.context = context;
}
@Singleton
@Provides
public Database getDatabase() {
return new Database() {
@Override
public String getName() {
return "databasetest";
}
};
}
@Singleton
@Provides
@TasksModule.ForApplication
public Context getContext() {
return context;
}
}
Loading…
Cancel
Save