<commit_before>package org.wikipedia.espresso.reading;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import androidx.test.espresso.DataInteraction;
import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.rule.ActivityTestRule;
import androidx.test.rule.GrantPermissionRule;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wikipedia.R;
import org.wikipedia.WikipediaApp;
import org.wikipedia.espresso.page.PageActivityTest;
import org.wikipedia.espresso.search.SearchBehaviors;
import org.wikipedia.espresso.util.ScreenshotTools;
import org.wikipedia.espresso.util.ViewTools;
import org.wikipedia.main.MainActivity;
import org.wikipedia.navtab.NavTab;
import org.wikipedia.readinglist.database.ReadingListDbHelper;
import org.wikipedia.settings.PrefsIoUtil;

import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.core.AllOf.allOf;
import static org.wikipedia.espresso.util.CompareTools.assertScreenshotWithinTolerance;
import static org.wikipedia.espresso.util.InstrumentationViewUtils.switchToBlackMode;
import static org.wikipedia.espresso.util.InstrumentationViewUtils.switchToDarkMode;
import static org.wikipedia.espresso.util.ViewTools.WAIT_FOR_1000;
import static org.wikipedia.espresso.util.ViewTools.WAIT_FOR_2000;
import static org.wikipedia.espresso.util.ViewTools.WAIT_FOR_500;
import static org.wikipedia.espresso.util.ViewTools.WAIT_FOR_6000;
import static org.wikipedia.espresso.util.ViewTools.childAtPosition;
import static org.wikipedia.espresso.util.ViewTools.pressBack;
import static org.wikipedia.espresso.util.ViewTools.selectTab;
import static org.wikipedia.espresso.util.ViewTools.viewIsDisplayed;
import static org.wikipedia.espresso.util.ViewTools.waitFor;
import static org.wikipedia.espresso.util.ViewTools.whileWithMaxSteps;

@RunWith(AndroidJUnit4.class)
@SuppressWarnings("checkstyle:methodlength")
public final class ReadingListsTest {
    @Rule
    public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Rule
    public GrantPermissionRule runtimePermissionRule = GrantPermissionRule.grant(
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    @Test
    public void testReadingLists() throws Exception {
        navigateToReadingListsFromExploreTab();
        runTests("");
        clearLists();
        switchToDarkMode();
        navigateToReadingListsFromExploreTab();
        runTests("_Dark");
        clearLists();
        switchToBlackMode();
        navigateToReadingListsFromExploreTab();
        runTests("_Black");
        clearLists();
        runComparisons("");
        runComparisons("_Dark");
        runComparisons("_Black");
    }

    private void clearLists() {
        navigateToExploreTab();

        ReadingListDbHelper.instance().resetToDefaults();
        PrefsIoUtil.setBoolean(R.string.preference_key_toc_tutorial_enabled, false);
        PrefsIoUtil.setBoolean(R.string.preference_key_description_edit_tutorial_enabled, false);
    }

    private void runTests(String mode) {
        if (viewIsDisplayed(R.id.view_onboarding_action_negative)) {
            onView(ViewTools.first(withId(R.id.view_onboarding_action_negative))).perform(click());
        }

        captureInitialReadingTabViews(mode);
        captureAddingArticleToDefaultList(mode);

        navigateToReadingListsFromExploreTab();

        captureDefaultListViews(mode);
        pressBack();

        captureMyListsTabWithUserCreatedLists(mode);
    }


    private void captureMyListsTabWithUserCreatedLists(String mode) {
        onView(withId(R.id.menu_search_lists)).perform(click());

        onView(withId(android.support.design.R.id.search_src_text)).perform(typeText("obama"));
        waitFor(WAIT_FOR_1000);

        ScreenshotTools.snap("ReadingListSearchWithResults" + mode);
        waitFor(WAIT_FOR_1000);

        onView(withId(android.support.design.R.id.search_src_text)).perform(typeText("oooo"));
        ScreenshotTools.snap("ReadingListSearchWithNoResults" + mode);

        pressBack();
        pressBack();
        waitFor(WAIT_FOR_1000);
        onView(withId(R.id.menu_sort_options)).perform(click());
        ScreenshotTools.snap("ReadingListsSortMenu" + mode);
        waitFor(WAIT_FOR_1000);

        pressBack();
        waitFor(WAIT_FOR_1000);
        onView(withId(R.id.menu_search_lists)).perform(click());
        onView(withId(android.support.design.R.id.search_src_text)).perform(typeText("my"));
        waitFor(WAIT_FOR_1000);
        ScreenshotTools.snap("ReadingListsSearchWithResults" + mode);
        waitFor(WAIT_FOR_1000);

        onView(withId(android.support.design.R.id.search_src_text)).perform(typeText("ooo"));
        ScreenshotTools.snap("ReadingListsSearchWithNoResults" + mode);
        waitFor(WAIT_FOR_1000);

        pressBack();
        waitFor(WAIT_FOR_1000);
        ScreenshotTools.snap("ReadingListsFragmentWithNoEmptyStateMessages" + mode);
    }

    private void navigateToExploreTab() {
        whileWithMaxSteps(
                () -> !viewIsDisplayed(R.id.fragment_main_nav_tab_layout),
                () -> waitFor(WAIT_FOR_2000));

        onView(withId(R.id.fragment_main_nav_tab_layout))<commit_msg>Use correct resources for the espresso test class.<commit_after>package org.wikipedia.espresso.reading;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import androidx.test.espresso.DataInteraction;
import androidx.test.espresso.contrib.RecyclerViewActions;
import androidx.test.rule.ActivityTestRule;
import androidx.test.rule.GrantPermissionRule;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wikipedia.R;
import org.wikipedia.WikipediaApp;
import org.wikipedia.espresso.page.PageActivityTest;
import org.wikipedia.espresso.search.SearchBehaviors;
import org.wikipedia.espresso.util.ScreenshotTools;
import org.wikipedia.espresso.util.ViewTools;
import org.wikipedia.main.MainActivity;
import org.wikipedia.navtab.NavTab;
import org.wikipedia.readinglist.database.ReadingListDbHelper;
import org.wikipedia.settings.PrefsIoUtil;

import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.core.AllOf.allOf;
import static org.wikipedia.espresso.util.CompareTools.assertScreenshotWithinTolerance;
import static org.wikipedia.espresso.util.InstrumentationViewUtils.switchToBlackMode;
import static org.wikipedia.espresso.util.InstrumentationViewUtils.switchToDarkMode;
import static org.wikipedia.espresso.util.ViewTools.WAIT_FOR_1000;
import static org.wikipedia.espresso.util.ViewTools.WAIT_FOR_2000;
import static org.wikipedia.espresso.util.ViewTools.WAIT_FOR_500;
import static org.wikipedia.espresso.util.ViewTools.WAIT_FOR_6000;
import static org.wikipedia.espresso.util.ViewTools.childAtPosition;
import static org.wikipedia.espresso.util.ViewTools.pressBack;
import static org.wikipedia.espresso.util.ViewTools.selectTab;
import static org.wikipedia.espresso.util.ViewTools.viewIsDisplayed;
import static org.wikipedia.espresso.util.ViewTools.waitFor;
import static org.wikipedia.espresso.util.ViewTools.whileWithMaxSteps;

@RunWith(AndroidJUnit4.class)
@SuppressWarnings("checkstyle:methodlength")
public final class ReadingListsTest {
    @Rule
    public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Rule
    public GrantPermissionRule runtimePermissionRule = GrantPermissionRule.grant(
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    @Test
    public void testReadingLists() throws Exception {
        navigateToReadingListsFromExploreTab();
        runTests("");
        clearLists();
        switchToDarkMode();
        navigateToReadingListsFromExploreTab();
        runTests("_Dark");
        clearLists();
        switchToBlackMode();
        navigateToReadingListsFromExploreTab();
        runTests("_Black");
        clearLists();
        runComparisons("");
        runComparisons("_Dark");
        runComparisons("_Black");
    }

    private void clearLists() {
        navigateToExploreTab();

        ReadingListDbHelper.instance().resetToDefaults();
        PrefsIoUtil.setBoolean(R.string.preference_key_toc_tutorial_enabled, false);
        PrefsIoUtil.setBoolean(R.string.preference_key_description_edit_tutorial_enabled, false);
    }

    private void runTests(String mode) {
        if (viewIsDisplayed(R.id.view_onboarding_action_negative)) {
            onView(ViewTools.first(withId(R.id.view_onboarding_action_negative))).perform(click());
        }

        captureInitialReadingTabViews(mode);
        captureAddingArticleToDefaultList(mode);

        navigateToReadingListsFromExploreTab();

        captureDefaultListViews(mode);
        pressBack();

        captureMyListsTabWithUserCreatedLists(mode);
    }


    private void captureMyListsTabWithUserCreatedLists(String mode) {
        onView(withId(R.id.menu_search_lists)).perform(click());

        onView(withId(R.id.search_src_text)).perform(typeText("obama"));
        waitFor(WAIT_FOR_1000);

        ScreenshotTools.snap("ReadingListSearchWithResults" + mode);
        waitFor(WAIT_FOR_1000);

        onView(withId(R.id.search_src_text)).perform(typeText("oooo"));
        ScreenshotTools.snap("ReadingListSearchWithNoResults" + mode);

        pressBack();
        pressBack();
        waitFor(WAIT_FOR_1000);
        onView(withId(R.id.menu_sort_options)).perform(click());
        ScreenshotTools.snap("ReadingListsSortMenu" + mode);
        waitFor(WAIT_FOR_1000);

        pressBack();
        waitFor(WAIT_FOR_1000);
        onView(withId(R.id.menu_search_lists)).perform(click());
        onView(withId(R.id.search_src_text)).perform(typeText("my"));
        waitFor(WAIT_FOR_1000);
        ScreenshotTools.snap("ReadingListsSearchWithResults" + mode);
        waitFor(WAIT_FOR_1000);

        onView(withId(R.id.search_src_text)).perform(typeText("ooo"));
        ScreenshotTools.snap("ReadingListsSearchWithNoResults" + mode);
        waitFor(WAIT_FOR_1000);

        pressBack();
        waitFor(WAIT_FOR_1000);
        ScreenshotTools.snap("ReadingListsFragmentWithNoEmptyStateMessages" + mode);
    }

    private void navigateToExploreTab() {
        whileWithMaxSteps(
                () -> !viewIsDisplayed(R.id.fragment_main_nav_tab_layout),
                () -> waitFor(WAIT_FOR_2000));

        onView(withId(R.id.fragment_main_nav_tab_layout))
{'fraction_non_alphanumeric': 0.10106426862470093, 'fraction_numerical': 0.009405164590380332, 'mean_word_length': 4.677751756440281, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '5938838', 'n_tokens_mistral': 3798, 'n_tokens_neox': 3583, 'n_words': 456}