<commit_before>/*
 * Copyright (c) <NAME> <<EMAIL>>
 * distributed under the terms of the MIT license
 */

package org.barbon.mangaget;

import android.app.Activity;
import android.app.Instrumentation;
import android.app.SearchManager;

import android.content.Context;
import android.content.Intent;

import android.database.Cursor;

import android.test.ActivityInstrumentationTestCase2;

import android.widget.ListView;

import org.barbon.mangaget.data.DB;

import org.barbon.mangaget.tests.UiUtils;
import org.barbon.mangaget.tests.Utils;

// TODO test paging

public class SearchTest extends ActivityInstrumentationTestCase2<MangaSearch> {
    private MangaSearch activity;

    public SearchTest() {
        super("org.barbon.mangaget", MangaSearch.class);
    }

    public Intent searchIntent(String query) {
        Context targetContext = getInstrumentation().getTargetContext();
        Intent intent = new Intent(targetContext, MangaSearch.class);

        intent.setAction(Intent.ACTION_SEARCH);
        intent.putExtra(SearchManager.QUERY, query);

        return intent;
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        UiUtils.setInstrumentation(getInstrumentation());
        Utils.setupTestAnimeaEnvironment(this);

        setActivityInitialTouchMode(false);
    }

    public void testSimpleSearch() {
        setActivityIntent(searchIntent(""));
        activity = (MangaSearch) getActivity();
        ListView resultList = activity.getListView();

        while (resultList.getCount() == 0)
            UiUtils.sleep(500);

        assertEquals(49, resultList.getCount());
    }

    private int mangaCount() {
        DB db = DB.getInstance(null);
        Cursor manga = db.getMangaList();
        int count = manga.getCount();

        manga.close();

        return count;
    }

    public void testMangaAdd() {
        Instrumentation instr = getInstrumentation();

        setActivityIntent(searchIntent(""));
        activity = (MangaSearch) getActivity();
        ListView resultList = activity.getListView();

        while (resultList.getCount() == 0)
            UiUtils.sleep(500);

        assertEquals(49, resultList.getCount());

        UiUtils.selectListAndMoveToTop(resultList);
        UiUtils.moveDown();
        UiUtils.selectCurrent();

        int prev_count = mangaCount();<commit_msg>Fix search tests broken by moving results list to an activity.<commit_after>/*
 * Copyright (c) <NAME> <<EMAIL>>
 * distributed under the terms of the MIT license
 */

package org.barbon.mangaget;

import android.app.Activity;
import android.app.Instrumentation;
import android.app.SearchManager;

import android.content.Context;
import android.content.Intent;

import android.database.Cursor;

import android.test.ActivityInstrumentationTestCase2;

import android.widget.ListView;

import android.support.v4.app.FragmentManager;

import org.barbon.mangaget.data.DB;

import org.barbon.mangaget.fragments.MangaSearchResults;

import org.barbon.mangaget.tests.UiUtils;
import org.barbon.mangaget.tests.Utils;

// TODO test paging

public class SearchTest extends ActivityInstrumentationTestCase2<MangaSearch> {
    private MangaSearch activity;
    private MangaSearchResults results;
    private FragmentManager manager;

    public SearchTest() {
        super("org.barbon.mangaget", MangaSearch.class);
    }

    private void refreshMembers() {
        activity = (MangaSearch) getActivity();
        manager = activity.getSupportFragmentManager();

        results = (MangaSearchResults)
            manager.findFragmentById(R.id.search_results);
    }

    public Intent searchIntent(String query) {
        Context targetContext = getInstrumentation().getTargetContext();
        Intent intent = new Intent(targetContext, MangaSearch.class);

        intent.setAction(Intent.ACTION_SEARCH);
        intent.putExtra(SearchManager.QUERY, query);

        return intent;
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        UiUtils.setInstrumentation(getInstrumentation());
        Utils.setupTestAnimeaEnvironment(this);
        Utils.setupTestAnimeaDatabase(this);

        setActivityInitialTouchMode(false);
    }

    public void testSimpleSearch() {
        setActivityIntent(searchIntent(""));
        refreshMembers();
        ListView resultList = results.getListView();

        while (resultList.getCount() == 0)
            UiUtils.sleep(500);

        assertEquals(49, resultList.getCount());
    }

    private int mangaCount() {
        DB db = DB.getInstance(null);
        Cursor manga = db.getMangaList();
        int count = manga.getCount();

        manga.close();

        return count;
    }

    public void testMangaAdd() {
        Instrumentation instr = getInstrumentation();

        setActivityIntent(searchIntent(""));
        setActivity(UiUtils.forceHorizontal(getActivity()));
        refreshMembers();
        ListView resultList = results.getListView();

        while (resultList.getCount() == 0)
            UiUtils.sleep(500);

        assertEquals(49, resultList.getCount());

        UiUtils.selectListAndMoveToTop(resultList);
        UiUtils.moveDown();
        UiUtils.selectCurrent();

        int prev_count = mangaCount();
{'fraction_non_alphanumeric': 0.10328549848942598, 'fraction_numerical': 0.00547583081570997, 'mean_word_length': 3.1906645569620253, 'pattern_counts': {'":': 0, '<': 11, '<?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': '361863', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1427, 'n_words': 298}