<commit_before>package com.przemyslawlusnia.vocabularycreator.wordlist.presentation;

import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.matcher.BoundedMatcher;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v7.widget.RecyclerView;
import com.przemyslawlusnia.vocabularycreator.R;
import com.przemyslawlusnia.vocabularycreator.wordlist.presentation.viewholder.WordsViewHolder;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.action.ViewActions.scrollTo;
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.actionOnHolderItem;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.internal.util.Checks.checkNotNull;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.not;

@RunWith(AndroidJUnit4.class)
public class WordsActivityTest {

  public static final String TEST_WORD = "test word";
  public static final String TEST_TRANSLATION = "test translation";

  @Rule
  public ActivityTestRule<WordsActivity> activityRule =
      new ActivityTestRule<WordsActivity>(WordsActivity.class) {
        @Override
        protected void beforeActivityLaunched() {
          RealmConfiguration config = new RealmConfiguration.Builder().inMemory().name("test-realm").build();
          Realm.deleteRealm(config);
          Realm.setDefaultConfiguration(config);
        }
      };

  @Test
  public void ensureFragmentContainerIsVisible() {
    onView(ViewMatchers.withId(R.id.fragmentContainer)).check(matches(isDisplayed()));
  }

  @Test
  public void addWordAndBack_wordEditTextNotExists() {
    onView(allOf(withId(R.id.addWordFab), isDisplayed())).perform(click());
    onView(allOf(withId(R.id.wordEditTxt), isDisplayed()));
    pressBack();
    pressBack();
    onView(withId(R.id.wordEditTxt)).check(doesNotExist());
  }

  @Test
  public void addWordAndCancel_wordEditTextNotExists() {
    onView(allOf(withId(R.id.addWordFab), isDisplayed())).perform(click());
    onView(allOf(withId(R.id.wordEditTxt), isDisplayed()));
    onView(withText(R.string.cancel)).perform(click());
    onView(withId(R.id.wordEditTxt)).check(doesNotExist());
  }

  @Test
  public void addThenDeleteWord_wordDoesNotExistsInRecyclerView() {
    addTestWord(TEST_WORD, TEST_TRANSLATION);
    clickOkButton();
    clickTestWord();
    deleteTestWord();

    // check on deleted ViewHolder data
    try {
      clickTestWord();
    } catch (NoMatchingViewException e) {
      // View is not in hierarchy
    }
    // check on view - just for training
    try {
      onView(withId(R.id.word))
          .check(matches(not(withText(TEST_WORD))))
          .check(matches(isDisplayed()));
      onView(withId(R.id.translation))
          .check(matches(not(withText(TEST_TRANSLATION))))
          .check(matches(isDisplayed()));
    } catch (NoMatchingViewException e) {
      // View is not in hierarchy
    }
  }

  // edit - edited correctly

  // edit possible for single selected

  // edit impossible for none or multi selected

  // delete action possible for single and multi selected

  @Test
  public void emptyVocabularyAndWordNotSelected_ActionsNotExists() {
    //given user opens vocabulary list and vocabulary list is empty
    //when nothing
    //then
    onView(withId(R.id.action_delete)).check(doesNotExist());
    onView(withId(R.id.action_edit)).check(doesNotExist());
  }

  @Test
  public void wordSelected_ActionsVisible() {
    //given user opens vocabulary list and vocabulary list is empty
    //when
    addTestWord(TEST_WORD, TEST_TRANSLATION);
    clickOkButton();
    clickTestWord();
    //then
    onView(withId(R.id.action_delete)).check(matches(isDisplayed()));
    onView(withId(R.id.action_edit)).check(matches(isDisplayed()));
  }

  @Test
  public void deleteWordAndWordNotSelected_ActionsNotExist() {
    //given
    addTestWord(TEST_WORD, TEST_TRANSLATION);
    clickOkButton();
    clickTestWord();
    //when
    deleteTestWord();
    //then
    onView(withId(R.id.action_delete)).check(doesNotExist());
    onView(withId(R.id.action_edit)).check(doesNotExist());
  }

  @Test
  public void addEmptyWord_NotPossible() {
    addTestWord("", TEST_TRANSLATION);
    onView(allOf(withId(android.R.id.button1), withText(R.string.ok))).check(matches(not(isEnabled())));
  }

  // can add empty translation if word is

  // popup - cancel and ok hide

  // popup: 2 x enter == OK click (TDD: feature to add)<commit_msg>Espresso tests structure setup part 2<commit_after>package com.przemyslawlusnia.vocabularycreator.wordlist.presentation;

import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.matcher.BoundedMatcher;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v7.widget.RecyclerView;
import com.przemyslawlusnia.vocabularycreator.R;
import com.przemyslawlusnia.vocabularycreator.wordlist.presentation.viewholder.WordsViewHolder;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.action.ViewActions.scrollTo;
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.actionOnHolderItem;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.internal.util.Checks.checkNotNull;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.not;

@RunWith(AndroidJUnit4.class)
public class WordsActivityTest {

  public static final String TEST_WORD = "test word";
  public static final String TEST_TRANSLATION = "test translation";

  @Rule
  public ActivityTestRule<WordsActivity> activityRule =
      new ActivityTestRule<WordsActivity>(WordsActivity.class) {
        @Override
        protected void beforeActivityLaunched() {
          RealmConfiguration config = new RealmConfiguration.Builder().inMemory().name("test-realm").build();
          Realm.deleteRealm(config);
          Realm.setDefaultConfiguration(config);
        }
      };

  @Test
  public void ensureFragmentContainerIsVisible() {
    //given user opens activity
    //when nothing
    //then
    onView(ViewMatchers.withId(R.id.fragmentContainer)).check(matches(isDisplayed()));
  }

  @Test
  public void showAddWordDialogAndBackPressTwice_wordEditTextNotExists() {
    //given user opens activity
    onView(allOf(withId(R.id.addWordFab), isDisplayed())).perform(click());
    onView(allOf(withId(R.id.wordEditTxt), isDisplayed()));
    //when
    pressBack();
    pressBack();
    //then
    onView(withId(R.id.wordEditTxt)).check(doesNotExist());
  }

  @Test
  public void showAddWordDialogAndCancel_wordEditTextNotExists() {
    //given user opens activity
    onView(allOf(withId(R.id.addWordFab), isDisplayed())).perform(click());
    onView(allOf(withId(R.id.wordEditTxt), isDisplayed()));
    //when
    onView(withText(R.string.cancel)).perform(click());
    //then
    onView(withId(R.id.wordEditTxt)).check(doesNotExist());
  }

  @Test
  public void addThenDeleteWord_wordDoesNotExistsInRecyclerView() {
    //given user opens activity and vocabulary list is empty

    //when
    addTestWord(TEST_WORD, TEST_TRANSLATION);
    clickOkButton();
    clickTestWord();
    deleteTestWord();

    //then
    //check on deleted ViewHolder data
    try {
      clickTestWord();
    } catch (NoMatchingViewException e) {
      //ViewHolder data is not in RecyclerView data. It's good.
    }
    //additionally check on view
    try {
      onView(withId(R.id.word))
          .check(matches(not(withText(TEST_WORD))))
          .check(matches(isDisplayed()));
      onView(withId(R.id.translation))
          .check(matches(not(withText(TEST_TRANSLATION))))
          .check(matches(isDisplayed()));
    } catch (NoMatchingViewException e) {
      //View is not in hierarchy. It's good.
    }
  }

  // edit - edited correctly

  // edit possible for single selected

  // edit impossible for none or multi selected

  // delete action possible for single and multi selected

  @Test
  public void emptyVocabularyAndWordNotSelected_ActionsNotExists() {
    //given user opens activity and vocabulary list is empty
    //when nothing
    //then
    onView(withId(R.id.action_delete)).check(doesNotExist());
    onView(withId(R.id.action_edit)).check(doesNotExist());
  }

  @Test
  public void wordSelected_ActionsVisible() {
    //given user opens activity and vocabulary list is empty
    //when
    addTestWord(TEST_WORD, TEST_TRANSLATION);
    clickOkButton();
    clickTestWord();
    //then
    onView(withId(R.id.action_delete)).check(matches(isDisplayed()));
    onView(withId(R.id.action_edit)).check(matches(isDisplayed()));
  }

  @Test
  public void deleteWordAndWordNotSelected_ActionsNotExist() {
    //given
    addTestWord(TEST_WORD, TEST_TRANSLATION);
    clickOkButton();
    clickTestWord();
    //when
    deleteTestWord();
    //then
    onView(withId(R.id.action_delete)).check(doesNotExist());
    onView(withId(R.id.action_edit)).check(doesNotExist());
  }

  @Test
  public void addEmptyWord_NotPossible() {
    //given user opens activity
    //when
    addTestWord("", TEST_TRANSLATION);
    //then
    onView(allOf(withId(android.R.id.button1), withText(R.string.ok))).check(matches(not(isEnabled())));
  }

  // can add empty translation if word is

  // popup - cancel and ok hide

  // popup: 2 x enter == OK click (TDD: feature to add)
{'fraction_non_alphanumeric': 0.11396427344555135, 'fraction_numerical': 0.0009446925455170044, 'mean_word_length': 4.413761041376104, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3878831', 'n_tokens_mistral': 3654, 'n_tokens_neox': 3572, 'n_words': 634}