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

it.tidalwave.bluebill.mobile.android.TestHelper 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 android.app.Instrumentation;
import android.os.Build;
import android.os.SystemClock;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Button;
import android.widget.ListView;
import com.jayway.android.robotium.solo.Solo;
import it.tidalwave.bluebill.mobile.android.impl.TestHelperNoTouch;
import it.tidalwave.bluebill.mobile.android.impl.TestHelperTouch;
import it.tidalwave.util.logging.Logger;
import java.util.List;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;

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

    protected final int timeout = 30000;

    protected final int generalDelay = 300;

    @Nonnull
    protected final ActivityInstrumentationTestCase2 instrumentationTestCase;

    @Nonnull
    protected final Solo solo;

    protected static boolean touchMode = false;

    protected final boolean emulator;

    protected final float delayFactor;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected TestHelper (final @Nonnull ActivityInstrumentationTestCase2 instrumentationTestCase,
                          final @Nonnull Solo solo)
      {
        this.instrumentationTestCase = instrumentationTestCase;
        this.solo = solo;
        emulator = Build.PRODUCT.contains("sdk");
        delayFactor = emulator ? 3 : 1;
        logger.info("************** delayFactor: %f", delayFactor);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public static TestHelper createHelper (final @Nonnull ActivityInstrumentationTestCase2 instrumentationTestCase,
                                           final @Nonnull Solo solo)
      {
        return touchMode ? new TestHelperTouch(instrumentationTestCase, solo)
                         : new TestHelperNoTouch(instrumentationTestCase, solo);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public static boolean isTouchMode()
      {
        return touchMode;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void waitForListNotEmpty (final int id, final long timeOut)
      throws InterruptedException
      {
        final long time = System.currentTimeMillis();
        final Activity activity = solo.getCurrentActivity();
        final ListView listView = (ListView)activity.findViewById(id);

        if (listView == null)
          {
            throw new AssertionError("No ListView with id " + id);
          }

        while ((listView.getAdapter() == null) || listView.getAdapter().isEmpty())
          {
            if (System.currentTimeMillis() - time >= timeOut * delayFactor)
              {
                throw new AssertionError("Time out while waiting for a ListView to become not empty");
              }

            logger.fine("Waiting for ListView #%d to become not empty...", id);
            sleep(100);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void waitForVisible (final int id)
      throws InterruptedException
      {
        final long time = System.currentTimeMillis();
        final Activity activity = solo.getCurrentActivity();
        final View view = activity.findViewById(id);

        while (view.getVisibility() != View.VISIBLE)
          {
            if (System.currentTimeMillis() - time >= timeout)
              {
                throw new AssertionError("Time out while waiting for a View to become visible");
              }

            logger.info("Waiting for View #%d to become visible...", id);
            sleep(100);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void waitForActivity (final @Nonnull Class activityClass)
      throws InterruptedException
      {
        logger.info("Waiting for %s ... (%s)", activityClass.getName(), solo.getCurrentActivity().getClass().getName());
//        solo.waitForActivity(activityClass.getName(), timeout);
        final long time = System.currentTimeMillis();

        while (!activityClass.isAssignableFrom(solo.getCurrentActivity().getClass()))
          {
            if (System.currentTimeMillis() - time >= timeout)
              {
                throw new AssertionError("Time out while waiting for " + activityClass.getName());
              }

            logger.info("Waiting for %s ... (%s)", activityClass.getName(), solo.getCurrentActivity().getClass().getName());
            sleep(100);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public abstract void clickOnImageButtonById (int id);

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void clickOnButtonById (final int id)
      {
        final List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy