/*
 * Copyright (c) Mattia Barbon <mattia@barbon.org>
 * 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();

        instr.invokeContextMenuAction(activity, R.id.add_manga, 0);

        while (mangaCount() == prev_count)
            UiUtils.sleep(500);

        assertEquals(prev_count + 1, mangaCount());
    }
}

@@ -428,35 +428,141 @@
 ort 
-org.barbon.mangaget.data.DB
+android.support.v4.app.FragmentManager;%0A%0Aimport org.barbon.mangaget.data.DB;%0A%0Aimport org.barbon.mangaget.fragments.MangaSearchResults
 ;%0A%0Ai
@@ -777,16 +777,93 @@
 ctivity;
+%0A    private MangaSearchResults results;%0A    private FragmentManager manager;
 %0A%0A    pu
@@ -938,32 +938,278 @@
 .class);%0A    %7D%0A%0A
+    private void refreshMembers() %7B%0A        activity = (MangaSearch) getActivity();%0A        manager = activity.getSupportFragmentManager();%0A%0A        results = (MangaSearchResults)%0A            manager.findFragmentById(R.id.search_results);%0A    %7D%0A%0A
     public Inten
@@ -1704,16 +1704,61 @@
 t(this);
+%0A        Utils.setupTestAnimeaDatabase(this);
 %0A%0A      
@@ -1896,44 +1896,22 @@
     
-activity = (MangaSearch) getActivity
+refreshMembers
 ();%0A
@@ -1936,32 +1936,31 @@
 esultList = 
-activity
+results
 .getListView
@@ -2444,44 +2444,83 @@
     
-a
+setA
 ctivity
- = (MangaSearch) getActivity
+(UiUtils.forceHorizontal(getActivity()));%0A        refreshMembers
 ();%0A
@@ -2549,24 +2549,23 @@
 tList = 
-activity
+results
 .getList

Fix search tests broken by moving results list to an activity.