All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.android.taxonomy.browser.TaxonomyBrowserActivityTestHelper Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueBill Mobile - Android - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it/mobile
 * SCM: https://java.net/hg/bluebill-mobile~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.browser;

import java.util.Collection;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import java.util.Comparator;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.android.ActivityTestHelper;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.ListView;
import android.widget.TextView;
import com.jayway.android.robotium.solo.Solo;
import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;
import java.text.Collator;
import java.util.Locale;
import static android.test.ActivityInstrumentationTestCase2.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class TaxonomyBrowserActivityTestHelper extends ActivityTestHelper
  {
    private static final String CLASS = TaxonomyBrowserActivityTestHelper.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    public TaxonomyBrowserActivityTestHelper (final @Nonnull ActivityInstrumentationTestCase2 instrumentationTestCase,
                                              final @Nonnull Solo solo)
      {
        super(instrumentationTestCase, solo);
      }

    public void waitForActivity()
      throws InterruptedException
      {
        testHelper.waitForActivity(TaxonBrowserActivity.class);
      }

    public void waitForTaxaListNotEmpty (final long timeOut)
      throws InterruptedException
      {
        testHelper.waitForListNotEmpty(android.R.id.list, timeOut);
      }

    public void filterKey (final int c)
      {
        testHelper.sendFilterKeyToListView(getListView(), c);
      }

    public void filterKeys (final @Nonnull String chars)
      {
        testHelper.sendFilterKeyToListView(getListView(), chars);
      }

    public void clickOnKeyboardButton()
      {
        testHelper.clickOnImageButtonById(R.id.btFilter);
      }

    public void clickOnTaxon (final @Nonnegative int index)
      {
        testHelper.clickOnListView(getListView(), index);
      }

    public void assertTaxaCount (final @Nonnegative int count)
      {
        assertEquals(count, getListView().getAdapter().getCount());
      }

    public void assertHeaderText (final @Nonnull String text)
      {
        final TextView textView = (TextView)solo.getCurrentActivity().findViewById(R.id.tvFooter);
        assertEquals(text, textView.getText().toString());
      }

    public void assertTaxonomyLabel (final @Nonnull String text)
      {
        final TextView textView = (TextView)solo.getCurrentActivity().findViewById(R.id.tvTaxonomy);
        assertEquals(text, textView.getText().toString());
      }

    public void assertTaxaAreProperlyRendered()
      {
        // TODO: accumulate in a Set so you can assert the whole list is browsed
        // TODO: add an assertion to verify that there is at least one-item overlap at each round
        List oldContents = new ArrayList();
        
        do
          {
            final List newContents = assertTaxaInCurrentScreenAreProperlyRendered();

            if (!oldContents.isEmpty())
              {
//                assertTrue("Contents didn't overlap", containsAtLeastOne(newContents, previousContents));
                if (!containsAtLeastOne(newContents, oldContents))
                  {
                    logger.warning("Contents not overlapping: %s -> %s", oldContents, newContents);
                  }
              }
          }
        while (testHelper.scrollDownList(0));

        final List newContents = assertTaxaInCurrentScreenAreProperlyRendered();
//        assertTrue("Contents didn't overlap", containsAtLeastOne(newContents, previousContents));
        if (!containsAtLeastOne(newContents, oldContents))
          {
            logger.warning("Contents not overlapping: %s -> %s", oldContents, newContents);
          }
      }

    private List assertTaxaInCurrentScreenAreProperlyRendered()
      {
        final ListView listView = getListView();
        final ArrayList textViews = solo.getCurrentTextViews(listView);
        final List actualTexts = new ArrayList();

        for (int i = 0; i < textViews.size(); i++)
          {
            final String text = textViews.get(i).getText().toString();
            final String firstLine = text.replaceAll("\n.*$", "");
            final String secondLine = text.replaceAll("^.*\n", "");
            assertFalse("First line empty for " + text,  "".equals(firstLine));
            assertFalse("Second line empty for " + text, "".equals(secondLine));
            actualTexts.add(firstLine);
          }

        System.err.println(actualTexts);
        final List expectedTexts = sortedByPrimaryTaxonLanguage(actualTexts);
        assertEquals("Taxa not properly sorted: expected" + expectedTexts + " actual: " + actualTexts,
                     expectedTexts, actualTexts);
        
        return actualTexts;
      }

    @Nonnull
    private ListView getListView()
      {
        return (ListView)solo.getCurrentActivity().findViewById(android.R.id.list);
      }

    @Nonnull
    private List sortedByPrimaryTaxonLanguage (final @Nonnull List strings)
      {
        final long time = System.currentTimeMillis();
        final Locale primaryLocale = Locator.find(TaxonomyPreferences.class).getTaxonomyLocales().get(0);
        final Collator collator = Collator.getInstance(primaryLocale);
        final List sortedStrings = new ArrayList(strings);

        Collections.sort(sortedStrings, new Comparator()
          {
            public int compare (final @Nonnull String s1, final @Nonnull String s2)
              {
                return collator.compare(s1, s2);
              }
          });

        System.err.printf("Sort done in %d msec\n", System.currentTimeMillis() - time);

        return sortedStrings;
      }

    private static  boolean containsAtLeastOne (final @Nonnull Collection a, final @Nonnull Collection b)
      {
        for (final T object : b)
          {
            if (a.contains(object))
              {
                return true;
              }
          }

        return false;
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy