Skip to content

Commit

Permalink
NF: refactor TagsDialogTest
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed May 4, 2021
1 parent 89bfe32 commit e1aaa7c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,29 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.recyclerview.widget.RecyclerView;

import static androidx.annotation.VisibleForTesting.*;

public class TagsArrayAdapter extends RecyclerView.Adapter<TagsArrayAdapter.ViewHolder> implements Filterable {
public static class ViewHolder extends RecyclerView.ViewHolder {
private final CheckedTextView mTagItemCheckedTextView;
public ViewHolder(CheckedTextView ctv) {
super(ctv);
mTagItemCheckedTextView = ctv;
}

@VisibleForTesting(otherwise = NONE)
public String getText() {
return ((CheckedTextView) itemView).getText().toString();
}


@VisibleForTesting(otherwise = NONE)
public boolean isChecked() {
return ((CheckedTextView) itemView).isChecked();
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import android.os.Bundle;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.RadioGroup;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.ichi2.anki.R;
import com.ichi2.anki.dialogs.tags.TagsDialog.DialogType;
import com.ichi2.testutils.RecyclerViewUtils;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -190,8 +190,7 @@ public void test_AddNewTag_shouldBeVisibleInRecyclerView() {
recycler.measure(0, 0);
recycler.layout(0, 0, 100, 1000);

TagsArrayAdapter.ViewHolder vh = (TagsArrayAdapter.ViewHolder) recycler.findViewHolderForAdapterPosition(2);
CheckedTextView itemView = (CheckedTextView) vh.itemView;
TagsArrayAdapter.ViewHolder itemView = RecyclerViewUtils.viewHolderAt(recycler, 2);

assertEquals(NEW_TAG, itemView.getText());
assertTrue(itemView.isChecked());
Expand Down
27 changes: 27 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/testutils/RecyclerViewUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright (c) 2021 Tarek Mohamed Abdalla <[email protected]>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.testutils;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

public class RecyclerViewUtils {

public static <VH extends ViewHolder> VH viewHolderAt(RecyclerView recyclerView, int position) {
return (VH) recyclerView.findViewHolderForAdapterPosition(position);
}

}

0 comments on commit e1aaa7c

Please sign in to comment.