it.tidalwave.bluebill.mobile.android.taxonomy.browser.TaxonomyBrowserActivityTestHelper Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - open source birdwatching
* ==========================================
*
* Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
* http://bluebill.tidalwave.it/mobile/
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* $Id: TaxonomyBrowserActivityTestHelper.java,v f32689862d36 2010/08/17 19:14:05 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.browser;
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.taxonomy.TaxonomyPreferences;
import java.text.Collator;
import java.util.Locale;
import static android.test.ActivityInstrumentationTestCase2.*;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class TaxonomyBrowserActivityTestHelper extends ActivityTestHelper
{
public TaxonomyBrowserActivityTestHelper (final @Nonnull ActivityInstrumentationTestCase2 instrumentationTestCase,
final @Nonnull Solo solo)
{
super(instrumentationTestCase, solo);
}
public void waitForActivity()
throws InterruptedException
{
testHelper.waitForActivity(TaxonomyBrowserActivity.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
do
{
assertTaxaInCurrentScreenAreProperlyRendered();
}
while (testHelper.scrollDownList(0));
assertTaxaInCurrentScreenAreProperlyRendered();
}
private void assertTaxaInCurrentScreenAreProperlyRendered()
{
final ListView listView = getListView();
final ArrayList textViews = solo.getCurrentTextViews(listView);
final List actualTexts = new ArrayList();
for (int j = 0; j < textViews.size(); j++)
{
final String text = textViews.get(j).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);
}
@Nonnull
private ListView getListView()
{
return (ListView)solo.getCurrentActivity().findViewById(android.R.id.list);
}
@Nonnull
private List sortedByPrimaryTaxonLanguage (final @Nonnull List strings)
{
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);
}
});
return sortedStrings;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy