it.tidalwave.bluebill.mobile.android.ScenarioTestSupport 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;
import android.app.Activity;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.mobile.android.splash.SplashActivity;
import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;
import android.test.ActivityInstrumentationTestCase2;
import com.jayway.android.robotium.solo.Solo;
import it.tidalwave.util.logging.Logger;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import javax.annotation.Nonnull;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public abstract class ScenarioTestSupport extends ActivityInstrumentationTestCase2
{
private static final String CLASS = ScenarioTestSupport.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
// private static final String BLUEBILL_PACKAGE = "it.tidalwave.bluebill.mobile.android";
private static final String BLUEBILL_PACKAGE = "it.tidalwave.bluebill.mobile.android.snapshot";
protected Solo solo;
private final List> callables = new ArrayList>();
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public ScenarioTestSupport()
{
super(BLUEBILL_PACKAGE, SplashActivity.class);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override
public void setUp()
throws Exception
{
super.setUp();
logger.info("******** PREPARING TEST *******************************");
setActivityInitialTouchMode(TestHelper.isTouchMode());
this.solo = new Solo(getInstrumentation(), getActivity());
// FIXME: I'd rather to reset the application - and drop the resetTaxonomy() method
Locator.find(TaxonomyPreferences.class).resetTaxonomy();
prepareSequence();
// TODO: set language
// TODO: set taxonomy preferences (taxonomy, primary and secondary language, scientific names)
logger.info("******** STARTING TEST ********************************");
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override
public void tearDown()
throws Exception
{
logger.info("******** COMPLETED TEST *******************************");
Thread.sleep(5000);
final ArrayList allOpenedActivities = solo.getAllOpenedActivities(); // before finalize()
try
{
solo.finalize(); // this finishes all activities
}
catch (Throwable e)
{
e.printStackTrace();
}
for (final Activity activity : allOpenedActivities)
{
getInstrumentation().callActivityOnPause(activity);
getInstrumentation().callActivityOnStop(activity);
getInstrumentation().callActivityOnDestroy(activity);
}
super.tearDown();
Thread.sleep(5000);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public final void testRun()
throws Exception
{
for (final Callable callable : callables)
{
callable.call();
}
assertPostConditions();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected abstract void prepareSequence()
throws Exception;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected abstract void assertPostConditions()
throws Exception;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void step (final @Nonnull Class extends Callable> callableClass,
final @Nonnull Object ... capabilities)
{
try
{
final Constructor extends Callable> constructor = callableClass.getConstructor(ActivityInstrumentationTestCase2.class, Solo.class, Object[].class);
callables.add(constructor.newInstance(this, solo, capabilities));
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}