Add RecentSearchesFragment Unit Tests (#4675)

This commit is contained in:
Madhur Gupta 2021-11-06 15:27:23 +05:30 committed by GitHub
parent 462a2ac007
commit 5535353480
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 212 additions and 38 deletions

View file

@ -1,5 +1,7 @@
package fr.free.nrw.commons.explore.recentsearches;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@ -9,18 +11,15 @@ import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import java.util.List;
import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
import fr.free.nrw.commons.explore.SearchActivity;
import java.util.List;
import javax.inject.Inject;
/**
@ -52,24 +51,7 @@ public class RecentSearchesFragment extends CommonsDaggerSupportFragment {
}
recent_searches_delete_button.setOnClickListener(v -> {
new AlertDialog.Builder(getContext())
.setMessage(getString(R.string.delete_recent_searches_dialog))
.setPositiveButton(android.R.string.yes, (dialog, which) -> {
recentSearchesDao.deleteAll();
recent_searches_delete_button.setVisibility(View.GONE);
recent_searches_text_view.setText(R.string.no_recent_searches);
Toast.makeText(getContext(), getString(R.string.search_history_deleted),
Toast.LENGTH_SHORT).show();
recentSearches = recentSearchesDao.recentSearches(10);
adapter = new ArrayAdapter<>(getContext(), R.layout.item_recent_searches,
recentSearches);
recentSearchesList.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.dismiss();
})
.setNegativeButton(android.R.string.no, null)
.create()
.show();
showDeleteRecentAlertDialog(requireContext());
});
adapter = new ArrayAdapter<>(requireContext(), R.layout.item_recent_searches,
@ -78,26 +60,59 @@ public class RecentSearchesFragment extends CommonsDaggerSupportFragment {
recentSearchesList.setOnItemClickListener((parent, view, position, id) -> (
(SearchActivity) getContext()).updateText(recentSearches.get(position)));
recentSearchesList.setOnItemLongClickListener((parent, view, position, id) -> {
new AlertDialog.Builder(getContext())
.setMessage(R.string.delete_search_dialog)
.setPositiveButton(getString(R.string.delete).toUpperCase(), ((dialog, which) -> {
recentSearchesDao.delete(recentSearchesDao.find(recentSearches.get(position)));
recentSearches = recentSearchesDao.recentSearches(10);
adapter = new ArrayAdapter<>(getContext(), R.layout.item_recent_searches,
recentSearches);
recentSearchesList.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.dismiss();
}))
.setNegativeButton(android.R.string.cancel, null)
.create()
.show();
showDeleteAlertDialog(requireContext(), position);
return true;
});
updateRecentSearches();
return rootView;
}
private void showDeleteRecentAlertDialog(@NonNull final Context context) {
new AlertDialog.Builder(context)
.setMessage(getString(R.string.delete_recent_searches_dialog))
.setPositiveButton(android.R.string.yes,
(dialog, which) -> setDeleteRecentPositiveButton(context, dialog))
.setNegativeButton(android.R.string.no, null)
.create()
.show();
}
private void setDeleteRecentPositiveButton(@NonNull final Context context,
final DialogInterface dialog) {
recentSearchesDao.deleteAll();
recent_searches_delete_button.setVisibility(View.GONE);
recent_searches_text_view.setText(R.string.no_recent_searches);
Toast.makeText(getContext(), getString(R.string.search_history_deleted),
Toast.LENGTH_SHORT).show();
recentSearches = recentSearchesDao.recentSearches(10);
adapter = new ArrayAdapter<>(context, R.layout.item_recent_searches,
recentSearches);
recentSearchesList.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.dismiss();
}
private void showDeleteAlertDialog(@NonNull final Context context, final int position) {
new AlertDialog.Builder(context)
.setMessage(R.string.delete_search_dialog)
.setPositiveButton(getString(R.string.delete).toUpperCase(),
((dialog, which) -> setDeletePositiveButton(context, dialog, position)))
.setNegativeButton(android.R.string.cancel, null)
.create()
.show();
}
private void setDeletePositiveButton(@NonNull final Context context,
final DialogInterface dialog, final int position) {
recentSearchesDao.delete(recentSearchesDao.find(recentSearches.get(position)));
recentSearches = recentSearchesDao.recentSearches(10);
adapter = new ArrayAdapter<>(context, R.layout.item_recent_searches,
recentSearches);
recentSearchesList.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.dismiss();
}
/**
* This method is called on back press of activity so we are updating the list from database to
* refresh the recent searches list.