Replace GtasksDetailExposer tests

pull/73/head
Alex Baker 12 years ago
parent 099867d131
commit 1725a6715b

@ -59,30 +59,33 @@ abstract public class AbstractDependencyInjector {
* @return object to assign to this field, or null
*/
public Object getInjection(Field field) {
if(injectables.containsKey(field.getName())) {
Object injection = injectables.get(field.getName());
// if it's a class, instantiate the class
if(injection instanceof Class<?>) {
if(createdObjects.containsKey(injection) &&
createdObjects.get(injection).get() != null) {
injection = createdObjects.get(injection).get();
} else {
Class<?> cls = (Class<?>)injection;
try {
injection = cls.newInstance();
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e);
}
createdObjects.put(cls, new WeakReference<>(injection));
return getInjection(field.getName());
}
public Object getInjection(String name) {
if(!injectables.containsKey(name)) {
return null;
}
Object injection = injectables.get(name);
// if it's a class, instantiate the class
if(injection instanceof Class<?>) {
if(createdObjects.containsKey(injection) &&
createdObjects.get(injection).get() != null) {
injection = createdObjects.get(injection).get();
} else {
Class<?> cls = (Class<?>)injection;
try {
injection = cls.newInstance();
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e);
}
}
return injection;
createdObjects.put(cls, new WeakReference<>(injection));
}
}
return null;
return injection;
}
@Override

@ -1,160 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.gtasks;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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.andlib.utility.AndroidUtilities;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.test.DatabaseTestCase;
import java.util.ArrayList;
import java.util.List;
public class GtasksDetailExposerTest extends DatabaseTestCase {
@Autowired private GtasksListService gtasksListService;
private final GtasksTestPreferenceService preferences = new GtasksTestPreferenceService();
private final DetailListener detailListener = new DetailListener();
private Task task;
private String detail;
public void testExposeNotLoggedIn() {
givenTwoListSetup();
givenLoggedInStatus(false);
givenTaskWithList("listone-id");
whenRequestingDetails();
thenExpectNoDetail();
}
public void testExposeListOne() {
givenTwoListSetup();
givenLoggedInStatus(true);
givenTaskWithList("listone-id");
whenRequestingDetails();
thenExpectDetail("List One");
}
public void testExposeListTwo() {
givenTwoListSetup();
givenLoggedInStatus(true);
givenTaskWithList("listtwo-id");
whenRequestingDetails();
thenExpectDetail("List Two");
}
public void disabled_testExposeListDoesntExist() {
givenTwoListSetup();
givenLoggedInStatus(true);
givenTaskWithList("blah");
whenRequestingDetails();
thenExpectNoDetail();
}
public void disabled_testExposeListNotSet() {
givenTwoListSetup();
givenLoggedInStatus(true);
givenTaskWithList(null);
whenRequestingDetails();
thenExpectNoDetail();
}
// --- helpers
private void thenExpectNoDetail() {
assertNull("no detail", detail);
}
private void thenExpectDetail(String expected) {
assertNotNull("detail not null", detail);
assertTrue("detail was '" + detail + "', '" + expected + "' expected",
detail.contains(expected));
}
private void givenTwoListSetup() {
TaskLists lists = new TaskLists();
List<TaskList> newLists = new ArrayList<>();
TaskList list = new TaskList();
list.setId("listone-id");
list.setTitle("List One"); //new GoogleTaskListInfo("listone-id", "List One");
newLists.add(list);
list = new TaskList();
list.setId("listtwo-id");
list.setTitle("List Two"); //("listtwo-id", "List Two");
newLists.add(list);
lists.setItems(newLists);
gtasksListService.updateLists(lists);
}
private void givenTaskWithList(String list) {
Task newTask = new Task();
PluginServices.getTaskService().save(newTask);
Metadata metadata = GtasksMetadata.createEmptyMetadata(newTask.getId());
if(list != null)
metadata.setValue(GtasksMetadata.LIST_ID, list);
PluginServices.getMetadataService().save(metadata);
task = newTask;
}
@Override
protected void addInjectables() {
super.addInjectables();
testInjector.addInjectable("gtasksPreferenceService", preferences);
}
private void whenRequestingDetails() {
Intent intent = new Intent(AstridApiConstants.BROADCAST_REQUEST_DETAILS);
intent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId());
detail = null;
new GtasksDetailExposer().onReceive(getContext(), intent);
AndroidUtilities.sleepDeep(500);
}
private void givenLoggedInStatus(boolean status) {
preferences.setLoggedIn(status);
}
@Override
protected void setUp() throws Exception {
super.setUp();
getContext().registerReceiver(detailListener, new IntentFilter(AstridApiConstants.BROADCAST_SEND_DETAILS));
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
getContext().unregisterReceiver(detailListener);
}
private class DetailListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
detail = intent.getExtras().getString(AstridApiConstants.EXTRAS_RESPONSE);
}
}
}//*/

@ -1,24 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.model;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.test.DatabaseTestCase;
public class TaskTests extends DatabaseTestCase {
@Autowired
TaskService taskService;
/** Check task gets a creation date at some point */
public void testCreationDate() {
Task task = new Task();
taskService.save(task);
assertTrue(task.getValue(Task.CREATION_DATE) > 0);
}
}

@ -30,12 +30,6 @@ public class TodorooRobolectricTestCase {
ContextManager.setContext(getContext());
AstridDependencyInjector.flush();
DependencyInjectionService.getInstance().inject(this);
setLocale(Locale.ENGLISH);
}
@After
public void tearDown() throws Exception {
setLocale(Locale.getDefault());
}
/**

@ -2,6 +2,7 @@ package com.todoroo.andlib.test;
import com.todoroo.andlib.service.RobolectricTestDependencyInjector;
import org.junit.After;
import org.tasks.Broadcaster;
import static org.mockito.Mockito.mock;
@ -22,10 +23,8 @@ abstract public class TodorooRobolectricTestCaseWithInjector extends TodorooRobo
super.setUp();
}
@Override
public void tearDown() throws Exception {
super.tearDown();
@After
public void after() throws Exception {
RobolectricTestDependencyInjector.deinitialize(testInjector);
}

@ -0,0 +1,122 @@
package com.todoroo.astrid.gtasks;
import android.content.Context;
import android.content.Intent;
import com.google.api.services.tasks.model.TaskList;
import com.todoroo.andlib.test.TodorooRobolectricTestCaseWithInjector;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.MetadataService;
import com.todoroo.astrid.service.TaskService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@RunWith(RobolectricTestRunner.class)
public class GtasksDetailExposerTest extends TodorooRobolectricTestCaseWithInjector {
private Context context;
private RobolectricGtasksPreferenceService gtasksPreferenceService;
@Override
protected void addInjectables() {
testInjector.addInjectable("gtasksPreferenceService", gtasksPreferenceService);
}
@Override
public void setUp() throws Exception {
gtasksPreferenceService = new RobolectricGtasksPreferenceService();
context = mock(Context.class);
new GtasksListService().addNewList(
new TaskList().setId("list_id").setTitle("list_title"));
super.setUp();
}
@Override
public void after() throws Exception {
verify(context).getApplicationContext();
verifyNoMoreInteractions(context);
super.after();
}
@Test
public void dontBroadcastDetailsWhenNotLoggedIn() {
gtasksPreferenceService.logout();
getDetails(newTask("list_id"));
}
@Test
public void dontBroadcastDetailsForInvalidTaskId() {
getDetails(0);
}
@Test
public void dontBroadcastDetailsWithNoList() {
getDetails(newTask());
}
@Test
public void dontBroadcastDetailsWithNullList() {
getDetails(newTask(null));
}
@Test
public void dontBroadcastDetailsWithDefaultList() {
getDetails(newTask(GtasksPreferenceService.PREF_DEFAULT_LIST));
}
@Test
public void dontBroadcastDetailsForNonexistentList() {
getDetails(newTask("invalid_list_id"));
}
@Test
public void broadcastDetails() {
Task task = newTask("list_id");
getDetails(task);
verify(context).sendBroadcast(
new Intent(AstridApiConstants.BROADCAST_SEND_DETAILS)
.putExtra(AstridApiConstants.EXTRAS_ADDON, GtasksPreferenceService.IDENTIFIER)
.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId())
.putExtra(AstridApiConstants.EXTRAS_RESPONSE, "<img src='gtasks_detail'/> list_title"),
AstridApiConstants.PERMISSION_READ);
}
private void getDetails(Task task) {
getDetails(task.getId());
}
private void getDetails(long taskId) {
Intent intent = new Intent().putExtra(AstridApiConstants.EXTRAS_TASK_ID, taskId);
new GtasksDetailExposer().onReceive(context, intent);
}
private Task newTask() {
Task task = new Task();
new TaskService().save(task);
Metadata metadata = GtasksMetadata.createEmptyMetadata(task.getId());
new MetadataService().save(metadata);
return task;
}
private Task newTask(String list) {
Task task = new Task();
new TaskService().save(task);
Metadata metadata = GtasksMetadata.createEmptyMetadata(task.getId());
metadata.setValue(GtasksMetadata.LIST_ID, list);
new MetadataService().save(metadata);
return task;
}
}

@ -0,0 +1,17 @@
package com.todoroo.astrid.gtasks;
public class RobolectricGtasksPreferenceService extends GtasksPreferenceService {
public RobolectricGtasksPreferenceService() {
setToken("");
}
@Override
public String getIdentifier() {
return "test";
}
public void logout() {
setToken(null);
}
}
Loading…
Cancel
Save