From dc88215ddf90f668659e7f07a2f86a1789285fc3 Mon Sep 17 00:00:00 2001 From: JanBan <32238789+JanBan@users.noreply.github.com> Date: Fri, 3 Aug 2018 20:46:46 +0200 Subject: [PATCH 1/2] #315 remove selected task from plan (#326) --- .../manager_app/PlanTaskListFragmentTest.java | 35 +++++++++++++++++-- .../repository/PlanTemplateRepository.java | 17 ++++++++- .../AddTasksToPlanFragment.java | 2 -- .../PlanTaskListFragment.java | 5 ++- .../app/src/main/res/layout/item_task.xml | 6 ++-- 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/Friendly-plans/app/src/androidTest/java/pg/autyzm/friendly_plans/manager_app/PlanTaskListFragmentTest.java b/Friendly-plans/app/src/androidTest/java/pg/autyzm/friendly_plans/manager_app/PlanTaskListFragmentTest.java index 9c820623..dec8e6aa 100644 --- a/Friendly-plans/app/src/androidTest/java/pg/autyzm/friendly_plans/manager_app/PlanTaskListFragmentTest.java +++ b/Friendly-plans/app/src/androidTest/java/pg/autyzm/friendly_plans/manager_app/PlanTaskListFragmentTest.java @@ -2,6 +2,7 @@ import android.app.FragmentManager; import android.app.FragmentTransaction; +import android.content.Context; import android.os.Bundle; import android.support.test.espresso.contrib.RecyclerViewActions; import android.support.test.rule.ActivityTestRule; @@ -14,15 +15,18 @@ import org.junit.Test; import org.junit.runner.RunWith; +import database.repository.PlanTemplateRepository; import pg.autyzm.friendly_plans.R; import pg.autyzm.friendly_plans.manager_app.view.plan_create.PlanCreateActivity; import pg.autyzm.friendly_plans.manager_app.view.plan_create_task_list.PlanTaskListFragment; import pg.autyzm.friendly_plans.resource.DaoSessionResource; import pg.autyzm.friendly_plans.resource.PlanTemplateRule; import pg.autyzm.friendly_plans.resource.TaskTemplateRule; +import pg.autyzm.friendly_plans.view_actions.ViewClicker; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; @@ -36,6 +40,7 @@ public class PlanTaskListFragmentTest { private static final String PLAN_ID = "PLAN_ID"; private static final String TASK_NAME = "TASK_NAME"; private static final String PLAN_NAME = "PLAN NAME"; + private static final String DELETE_TEST_TASK = "DELETE TEST TASK"; @ClassRule public static DaoSessionResource daoSessionResource = new DaoSessionResource(); @@ -54,10 +59,15 @@ public class PlanTaskListFragmentTest { @Before public void setUp() { - PlanTaskListFragment fragment = new PlanTaskListFragment(); + Context context = activityRule.getActivity().getApplicationContext(); + PlanTemplateRepository planTemplateRepository = new PlanTemplateRepository(daoSessionResource.getSession(context)); - taskTemplateRule.createTask(TASK_NAME); + long taskId = taskTemplateRule.createTask(DELETE_TEST_TASK); long planId = planTemplateRule.createPlan(PLAN_NAME); + planTemplateRepository.setTasksWithThisPlan(planId, taskId); + taskTemplateRule.createTask(TASK_NAME); + + PlanTaskListFragment fragment = new PlanTaskListFragment(); Bundle args = new Bundle(); args.putLong(PLAN_ID, planId); @@ -95,8 +105,27 @@ public void whenAddingNewTaskToPlanExpectShowTaskOnList() { onView(withId(R.id.id_btn_add_tasks_to_plan)) .perform(click()); + recyclerView = (RecyclerView) activityRule.getActivity().findViewById(R.id.rv_create_plan_task_list); + lastPosition = recyclerView.getAdapter().getItemCount() - 1; + onView(withRecyclerView(R.id.rv_create_plan_task_list) - .atPosition(0)) + .atPosition(lastPosition)) .check(matches(hasDescendant(withText(TASK_NAME)))); } + + @Test + public void whenRemoveIconClickedExpectTaskToNoLongerBeInThisPlan(){ + final int testedTaskPosition = 0; + + onView(withId(R.id.rv_create_plan_task_list)) + .perform(RecyclerViewActions + .actionOnItemAtPosition(testedTaskPosition, + new ViewClicker(R.id.id_remove_task))); + + onView(withId(R.id.rv_create_plan_task_list)).perform( + RecyclerViewActions.scrollToPosition(testedTaskPosition)); + + onView(withRecyclerView(R.id.rv_create_plan_task_list) + .atPosition(testedTaskPosition)).check(doesNotExist()); + } } diff --git a/Friendly-plans/app/src/main/java/database/repository/PlanTemplateRepository.java b/Friendly-plans/app/src/main/java/database/repository/PlanTemplateRepository.java index 67f4e6b4..bd2d624c 100644 --- a/Friendly-plans/app/src/main/java/database/repository/PlanTemplateRepository.java +++ b/Friendly-plans/app/src/main/java/database/repository/PlanTemplateRepository.java @@ -1,5 +1,8 @@ package database.repository; + +import org.greenrobot.greendao.query.QueryBuilder; + import java.util.List; import database.entities.DaoSession; @@ -30,7 +33,6 @@ public void update(Long planId, String name) { daoSession.getPlanTemplateDao().update(planTemplate); } - public void setTasksWithThisPlan(Long planId, Long taskId) { PlanTaskTemplate planTaskTemplate = new PlanTaskTemplate(); planTaskTemplate.setTaskTemplateId(taskId); @@ -42,6 +44,19 @@ public void setTasksWithThisPlan(Long planId, Long taskId) { get(planId).resetTasksWithThisPlan(); } + public void deleteTaskFromThisPlan(Long planId, Long taskId){ + PlanTaskTemplateDao planTaskTemplateDao = daoSession.getPlanTaskTemplateDao(); + QueryBuilder queryBuilder = planTaskTemplateDao.queryBuilder(); + + PlanTaskTemplate planTaskTemplate = queryBuilder.where( + PlanTaskTemplateDao.Properties.PlanTemplateId.eq(planId), + PlanTaskTemplateDao.Properties.TaskTemplateId.eq(taskId)) + .uniqueOrThrow(); + + planTaskTemplateDao.delete(planTaskTemplate); + } + + public PlanTemplate get(Long id) { return daoSession.getPlanTemplateDao().load(id); } diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/manager_app/view/plan_create_add_tasks/AddTasksToPlanFragment.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/manager_app/view/plan_create_add_tasks/AddTasksToPlanFragment.java index 047602db..4969963e 100644 --- a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/manager_app/view/plan_create_add_tasks/AddTasksToPlanFragment.java +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/manager_app/view/plan_create_add_tasks/AddTasksToPlanFragment.java @@ -50,8 +50,6 @@ public void onRemoveTaskClick(int position){ } }; - - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { diff --git a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/manager_app/view/plan_create_task_list/PlanTaskListFragment.java b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/manager_app/view/plan_create_task_list/PlanTaskListFragment.java index e168b576..84f49cdb 100644 --- a/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/manager_app/view/plan_create_task_list/PlanTaskListFragment.java +++ b/Friendly-plans/app/src/main/java/pg/autyzm/friendly_plans/manager_app/view/plan_create_task_list/PlanTaskListFragment.java @@ -38,7 +38,10 @@ public void onTaskItemClick(int position) { @Override public void onRemoveTaskClick(int position){ - /*Item remove TODO*/ + planTemplateRepository.deleteTaskFromThisPlan( + planId, + taskListAdapter.getTaskItem(position).getId()); + taskListAdapter.removeListItem(position); } }; diff --git a/Friendly-plans/app/src/main/res/layout/item_task.xml b/Friendly-plans/app/src/main/res/layout/item_task.xml index 7bffe4b7..32bfcff9 100644 --- a/Friendly-plans/app/src/main/res/layout/item_task.xml +++ b/Friendly-plans/app/src/main/res/layout/item_task.xml @@ -57,14 +57,14 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" - android:layout_marginLeft="15dp" + android:layout_marginStart="15dp" android:orientation="vertical"> + android:layout_height="wrap_content" + android:src="@drawable/ic_delete_black_24dp" /> \ No newline at end of file From 39afac8254ded80985316913e08f873d25620d7f Mon Sep 17 00:00:00 2001 From: JanBan <32238789+JanBan@users.noreply.github.com> Date: Sun, 12 Aug 2018 12:12:52 +0200 Subject: [PATCH 2/2] #318: Add tests for removing plans --- .../manager_app/PlanListActivityTest.java | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/Friendly-plans/app/src/androidTest/java/pg/autyzm/friendly_plans/manager_app/PlanListActivityTest.java b/Friendly-plans/app/src/androidTest/java/pg/autyzm/friendly_plans/manager_app/PlanListActivityTest.java index 998a91bd..1c59ef37 100644 --- a/Friendly-plans/app/src/androidTest/java/pg/autyzm/friendly_plans/manager_app/PlanListActivityTest.java +++ b/Friendly-plans/app/src/androidTest/java/pg/autyzm/friendly_plans/manager_app/PlanListActivityTest.java @@ -130,12 +130,10 @@ public void whenPlanIsRemovedExpectPlanIsNotOnTheList() { } @Test - public void whenSearchedPlanIsRemovedExpectNoPlansInSearch(){ + public void whenSearchedPlanIsRemovedExpectNoPlansInSearch() { final int testedPlanPosition = 5; - onView(withId(R.id.menu_search)).perform(typeText(expectedName + testedPlanPosition)); closeSoftKeyboard(); - onView(withId(R.id.rv_plan_list)) .perform(RecyclerViewActions .actionOnItemAtPosition(0, @@ -144,7 +142,32 @@ public void whenSearchedPlanIsRemovedExpectNoPlansInSearch(){ onView(withRecyclerView(R.id.rv_plan_list) .atPosition(0)) - .check(doesNotExist()); + .check(doesNotExist()); + } + + @Test + public void whenMultiplePlansAreRemovedExpectListRefreshedAfterEachOneOfThem() { + final int testedFirstTaskPosition = 3; + final int testedSecondTaskPosition = 4; + onView(withId(R.id.rv_plan_list)) + .perform(RecyclerViewActions + .actionOnItemAtPosition(testedFirstTaskPosition, + new ViewClicker(R.id.id_remove_plan))); + onView(withId(R.id.rv_plan_list)).perform(scrollToPosition(testedFirstTaskPosition)); + onView(withRecyclerView(R.id.rv_plan_list) + .atPosition(testedFirstTaskPosition)) + .check(matches(not(hasDescendant(withText(expectedName + + testedFirstTaskPosition))))); + + onView(withId(R.id.rv_plan_list)) + .perform(RecyclerViewActions + .actionOnItemAtPosition(testedSecondTaskPosition - 1, + new ViewClicker(R.id.id_remove_plan))); + onView(withId(R.id.rv_plan_list)).perform(scrollToPosition(testedSecondTaskPosition)); + onView(withRecyclerView(R.id.rv_plan_list) + .atPosition(testedSecondTaskPosition)) + .check(matches(not(hasDescendant(withText(expectedName + + testedSecondTaskPosition))))); } @Test @@ -152,13 +175,17 @@ public void whenSearchPlanIsRemovedExpectItToBeRemoved(){ final int testedPlanPosition = 5; onView(withId(R.id.menu_search)).perform(typeText(expectedName + testedPlanPosition)); + closeSoftKeyboard(); onView(withId(R.id.rv_plan_list)) .perform(RecyclerViewActions .actionOnItemAtPosition(0, new ViewClicker(R.id.id_remove_plan))); + + onView(withRecyclerView(R.id.rv_plan_list) + .atPosition(0)) + .check(doesNotExist()); onView(isAssignableFrom(EditText.class)).perform(clearText()); - closeSoftKeyboard(); onView(withId(R.id.rv_plan_list)).perform(scrollToPosition(testedPlanPosition)); onView(withRecyclerView(R.id.rv_plan_list)