Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

297 add child search for child #409

Open
wants to merge 8 commits into
base: paulina-changes
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition;
import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
Expand Down Expand Up @@ -36,8 +38,8 @@
@RunWith(AndroidJUnit4.class)
public class ChildListActivityTest {

private static final String EXPECTED_FIRST_NAME = "FIRST NAME";
private static final String EXPECTED_LAST_NAME = "LAST NAME";
private static final String EXPECTED_FIRST_NAME = "FIRST_NAME";
private static final String EXPECTED_LAST_NAME = "LAST_NAME";

@ClassRule
public static DaoSessionResource daoSessionResource = new DaoSessionResource();
Expand Down Expand Up @@ -122,4 +124,84 @@ public void whenChildIsAddedToDBExpectProperlyDisplayedOnRecyclerView() {
.check(matches(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ testedChildPosition))));
}

@Test
public void whenSearchForAChildWithOneLetterOfNameExpectThatChildOnFirstPosition() {
final int testedChildPosition = 0;
final int firstPosition = 0;
onView(withId(R.id.menu_search)).perform(typeText(Character.toString(EXPECTED_FIRST_NAME.charAt(0))));
onView(withRecyclerView(R.id.rv_child_list)
.atPosition(firstPosition))
.check(matches(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ testedChildPosition))));
}

@Test
public void whenSearchForAChildWithNameExpectThatChildOnFirstPosition() {
final int testedChildPosition = 0;
final int firstPosition = 0;
onView(withId(R.id.menu_search)).perform(typeText(EXPECTED_FIRST_NAME));
onView(withRecyclerView(R.id.rv_child_list)
.atPosition(firstPosition))
.check(matches(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ testedChildPosition))));
}

@Test
public void whenSearchForAChildWithOneLetterOfSurnameExpectThatChildOnFirstPosition() {
final int testedChildPosition = 0;
final int firstPosition = 0;
onView(withId(R.id.menu_search)).perform(typeText(Character.toString(EXPECTED_LAST_NAME.charAt(0))));
onView(withRecyclerView(R.id.rv_child_list)
.atPosition(firstPosition))
.check(matches(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ testedChildPosition))));
}
@Test
public void whenSearchForAChildWithSurnameExpectThatChildOnFirstPosition(){
final int testedChildPosition = 3;
final int firstPosition = 0;
onView(withId(R.id.menu_search)).perform(typeText(EXPECTED_LAST_NAME + testedChildPosition));
onView(withRecyclerView(R.id.rv_child_list)
.atPosition(firstPosition))
.check(matches(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ testedChildPosition))));
}
@Test
public void whenSearchForAChildWithNameAndSurnameExpectOnlyThatChildToAppear() { //TO DO
final int testedChildPosition = 3;
final int firstPosition = 0;
final int nextPosition = 1;
onView(withId(R.id.menu_search)).perform(typeText(
EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME + testedChildPosition));
onView(withRecyclerView(R.id.rv_child_list)
.atPosition(firstPosition))
.check(matches(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ testedChildPosition))));
onView(withRecyclerView(R.id.rv_plan_list)
.atPosition(nextPosition)).check(doesNotExist());
}
@Test
public void whenSearchForAChildWithSurnameAndNameExpectOnlyThatChildToAppear(){
final int testedChildPosition = 3;
final int firstPosition = 0;
onView(withId(R.id.menu_search)).perform(typeText(
EXPECTED_LAST_NAME + testedChildPosition + " " + EXPECTED_FIRST_NAME));
onView(withRecyclerView(R.id.rv_child_list)
.atPosition(firstPosition))
.check(matches(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ testedChildPosition))));
}
@Test
public void whenSearchForAChildWithFirstLettersOfNameAndSurnameExpectThatChildOnFirstPosition(){
final int testedChildPosition = 0;
final int firstPosition = 0;
onView(withId(R.id.menu_search)).perform(typeText(
Character.toString(EXPECTED_FIRST_NAME.charAt(0)) + " "
+ Character.toString(EXPECTED_LAST_NAME.charAt(0))));
onView(withRecyclerView(R.id.rv_child_list)
.atPosition(firstPosition))
.check(matches(hasDescendant(withText(EXPECTED_FIRST_NAME + " " + EXPECTED_LAST_NAME
+ testedChildPosition))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import database.entities.Child;
import database.entities.ChildDao.Properties;
import database.entities.DaoSession;

import java.util.List;

public class ChildRepository {
Expand All @@ -27,6 +28,25 @@ public List<Child> getBySurname(String surname) {
.list();
}

public List<Child> getFilteredByFullName(String fullName) {
String[] splited = fullName.split("\\s+");
for (int i=0; i<splited.length; i++) {
splited[i] = "%" + splited[i] + "%";
}
if(splited.length == 1 ) {
String[] whereArguments = {splited[0], splited[0]};
return daoSession.getChildDao().queryRaw("WHERE name LIKE ? OR surname LIKE ?",
whereArguments);
}
else {
String[] whereArguments = {splited[0], splited[1], splited[1], splited[0]};
return daoSession.getChildDao().queryRaw(
"WHERE (name LIKE ? AND surname LIKE ?) OR (name LIKE ? AND surname LIKE ?)",
whereArguments);
}

}

public Child get(Long id) {
return daoSession.getChildDao().load(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;

import database.repository.ChildRepository;
import javax.inject.Inject;
import pg.autyzm.friendly_plans.App;
Expand All @@ -22,6 +27,8 @@ public class ChildListActivity extends AppCompatActivity implements ChildListAct
@Inject
ToastUserNotifier toastUserNotifier;

ChildRecyclerViewAdapter childListAdapter;

ChildListData childData;

@Override
Expand All @@ -44,11 +51,34 @@ protected void onCreate(Bundle savedInstanceState) {

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.plan_list_menu, menu);
MenuItem searchViewItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchViewItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
childListAdapter.setChildItems(childRepository.getFilteredByFullName(query));
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
childListAdapter.setChildItems(childRepository.getFilteredByFullName(newText));
return false;
}
});

return true;
}

private void setUpViews() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_child_list);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ChildRecyclerViewAdapter childListAdapter = new ChildRecyclerViewAdapter();
childListAdapter = new ChildRecyclerViewAdapter();
recyclerView.setAdapter(childListAdapter);
childListAdapter.setChildItems(childRepository.getAll());
}
Expand Down