From 1725a6715bd46e7c8fea7b3f1e74e8a041e2ac8e Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 14 Jan 2014 17:05:34 -0600 Subject: [PATCH] Replace GtasksDetailExposer tests --- .../service/AbstractDependencyInjector.java | 43 ++--- .../gtasks/GtasksDetailExposerTest.java | 160 ------------------ .../com/todoroo/astrid/model/TaskTests.java | 24 --- .../test/TodorooRobolectricTestCase.java | 6 - ...odorooRobolectricTestCaseWithInjector.java | 7 +- .../gtasks/GtasksDetailExposerTest.java | 122 +++++++++++++ .../RobolectricGtasksPreferenceService.java | 17 ++ 7 files changed, 165 insertions(+), 214 deletions(-) delete mode 100644 astrid/src/instrumentTest/java/com/todoroo/astrid/gtasks/GtasksDetailExposerTest.java delete mode 100644 astrid/src/instrumentTest/java/com/todoroo/astrid/model/TaskTests.java create mode 100644 astrid/src/test/java/com/todoroo/astrid/gtasks/GtasksDetailExposerTest.java create mode 100644 astrid/src/test/java/com/todoroo/astrid/gtasks/RobolectricGtasksPreferenceService.java diff --git a/api/src/main/java/com/todoroo/andlib/service/AbstractDependencyInjector.java b/api/src/main/java/com/todoroo/andlib/service/AbstractDependencyInjector.java index 00b3bb6de..03e7f65fa 100644 --- a/api/src/main/java/com/todoroo/andlib/service/AbstractDependencyInjector.java +++ b/api/src/main/java/com/todoroo/andlib/service/AbstractDependencyInjector.java @@ -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 diff --git a/astrid/src/instrumentTest/java/com/todoroo/astrid/gtasks/GtasksDetailExposerTest.java b/astrid/src/instrumentTest/java/com/todoroo/astrid/gtasks/GtasksDetailExposerTest.java deleted file mode 100644 index c9558d361..000000000 --- a/astrid/src/instrumentTest/java/com/todoroo/astrid/gtasks/GtasksDetailExposerTest.java +++ /dev/null @@ -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 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); - } - } - -}//*/ diff --git a/astrid/src/instrumentTest/java/com/todoroo/astrid/model/TaskTests.java b/astrid/src/instrumentTest/java/com/todoroo/astrid/model/TaskTests.java deleted file mode 100644 index f91eacf13..000000000 --- a/astrid/src/instrumentTest/java/com/todoroo/astrid/model/TaskTests.java +++ /dev/null @@ -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); - } -} diff --git a/astrid/src/test/java/com/todoroo/andlib/test/TodorooRobolectricTestCase.java b/astrid/src/test/java/com/todoroo/andlib/test/TodorooRobolectricTestCase.java index 40e566c16..96ccf11b7 100644 --- a/astrid/src/test/java/com/todoroo/andlib/test/TodorooRobolectricTestCase.java +++ b/astrid/src/test/java/com/todoroo/andlib/test/TodorooRobolectricTestCase.java @@ -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()); } /** diff --git a/astrid/src/test/java/com/todoroo/andlib/test/TodorooRobolectricTestCaseWithInjector.java b/astrid/src/test/java/com/todoroo/andlib/test/TodorooRobolectricTestCaseWithInjector.java index 3a6936b0e..f991e62f6 100644 --- a/astrid/src/test/java/com/todoroo/andlib/test/TodorooRobolectricTestCaseWithInjector.java +++ b/astrid/src/test/java/com/todoroo/andlib/test/TodorooRobolectricTestCaseWithInjector.java @@ -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); } diff --git a/astrid/src/test/java/com/todoroo/astrid/gtasks/GtasksDetailExposerTest.java b/astrid/src/test/java/com/todoroo/astrid/gtasks/GtasksDetailExposerTest.java new file mode 100644 index 000000000..e0074000d --- /dev/null +++ b/astrid/src/test/java/com/todoroo/astrid/gtasks/GtasksDetailExposerTest.java @@ -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, " 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; + } +} diff --git a/astrid/src/test/java/com/todoroo/astrid/gtasks/RobolectricGtasksPreferenceService.java b/astrid/src/test/java/com/todoroo/astrid/gtasks/RobolectricGtasksPreferenceService.java new file mode 100644 index 000000000..27ecb18b5 --- /dev/null +++ b/astrid/src/test/java/com/todoroo/astrid/gtasks/RobolectricGtasksPreferenceService.java @@ -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); + } +}