Skip to content

Commit

Permalink
test: regression ankidroid#8762
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed May 3, 2021
1 parent 8647bfb commit dbe54f8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand Down Expand Up @@ -269,7 +270,8 @@ private EditText requireDialogInputEditText(@NonNull MaterialDialog dialog) {
return editText;
}

public void addTag(String tag) {
@VisibleForTesting
protected void addTag(String tag) {
if (!TextUtils.isEmpty(tag)) {
String feedbackText;
if (mTags.add(tag)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

import com.afollestad.materialdialogs.DialogAction;
Expand All @@ -33,10 +34,12 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.testing.FragmentScenario;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.recyclerview.widget.RecyclerView;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import static com.ichi2.anki.dialogs.tags.TagsDialogListener.*;
Expand All @@ -45,6 +48,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -87,7 +91,7 @@ public void testTagsDialogCustomStudyOptionInterface() {
assertThat(dialog, notNullValue());

final View body = dialog.getCustomView();
final RadioGroup mOptionsGroup = body.findViewById(R.id.tags_dialog_options_radiogroup);
final RadioGroup mOptionsGroup = body.findViewById(R.id.tags_dialog_options_radiogroup);

assertEquals(mOptionsGroup.getVisibility(), View.VISIBLE);

Expand All @@ -113,7 +117,6 @@ public void testTagsDialogCustomStudyOptionFragmentAPI() {
.getArguments();



FragmentScenario<TagsDialog> scenario = FragmentScenario.launch(TagsDialog.class, args, R.style.Theme_AppCompat);

scenario.moveToState(Lifecycle.State.STARTED);
Expand All @@ -123,8 +126,6 @@ public void testTagsDialogCustomStudyOptionFragmentAPI() {
assertThat(dialog, notNullValue());




AtomicReference<List<String>> returnedList = new AtomicReference<>();
AtomicInteger returnedOption = new AtomicInteger();

Expand All @@ -135,9 +136,8 @@ public void testTagsDialogCustomStudyOptionFragmentAPI() {
});



final View body = dialog.getCustomView();
final RadioGroup mOptionsGroup = body.findViewById(R.id.tags_dialog_options_radiogroup);
final RadioGroup mOptionsGroup = body.findViewById(R.id.tags_dialog_options_radiogroup);

assertEquals(mOptionsGroup.getVisibility(), View.VISIBLE);

Expand All @@ -152,4 +152,47 @@ public void testTagsDialogCustomStudyOptionFragmentAPI() {
});
}


// regression test #8762
@Test
public void test_AddNewTag_shouldBeVisibleInRecyclerView() {
final DialogType type = DialogType.ADD_TAG;
final List<String> allTags = Arrays.asList("a", "b", "d", "e");
final List<String> checkedTags = Arrays.asList("a", "b");

Bundle args = new TagsDialog(whatever())
.withArguments(type, checkedTags, allTags)
.getArguments();

final TagsDialogListener mockListener = mock(TagsDialogListener.class);

TagsDialogFactory factory = new TagsDialogFactory(mockListener);
FragmentScenario<TagsDialog> scenario = FragmentScenario.launch(TagsDialog.class, args, R.style.Theme_AppCompat, factory);

scenario.moveToState(Lifecycle.State.STARTED);

scenario.onFragment((f) -> {
MaterialDialog dialog = (MaterialDialog) f.getDialog();
assertThat(dialog, notNullValue());

final View body = dialog.getCustomView();
RecyclerView recycler = body.findViewById(R.id.tags_dialog_tags_list);

final String NEW_TAG = "c";

f.addTag(NEW_TAG);

// workaround robolectric recyclerView issue
// update recycler
recycler.measure(0, 0);
recycler.layout(0, 0, 100, 1000);

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

assertEquals(NEW_TAG, itemView.getText());
assertTrue(itemView.isChecked());
});
}

}

0 comments on commit dbe54f8

Please sign in to comment.