通过adb在打开的应用程序之间切换

时间:2014-10-22 04:16:11

标签: android testing adb

我写了一个在apk文件上运行测试的机器人测试。测试在调试模式下在apk文件上运行。测试点击并在应用程序中插入数据。

问题是,有时用户点击应用中的链接,并在设备上打开新的应用/网站,现在根应用程序不可见。

有一种方法可以通过adb显示它吗?

这是测试的主要类别:

package genericTest.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import android.annotation.SuppressLint;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.SystemClock;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;

import com.robotium.solo.Solo;

@SuppressWarnings("rawtypes")
public class Main extends ActivityInstrumentationTestCase2 {
    private FunctonsForViews mFunctonsForViews;
    private Random mRand;
    private Solo mSolo;
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.hobbyistsoftware.android.vlcremote_us.main";

    private final int DELAY = 50; 
    private final int SCREEN_SIZE = 1280;
    private final int ERROR_COUNT_LIMIT = 10;
    private final int CLICK_ON_LOOP_LIMIT = 8;
    private final int WHAITING_FOR_VIEWS_LIMIT = 10;
    private final int LOOP_LIMIT = 10000;

    private static Class launcherActivityClass;
    private static int error_count = 0;

    static {
        try {
            launcherActivityClass = Class
                    .forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressLint("NewApi")
    @SuppressWarnings("unchecked")
    public Main() throws ClassNotFoundException {
        super(null, launcherActivityClass); 
    }

    protected void setUp() throws Exception {
        setActivityInitialTouchMode(true);
        mSolo = new Solo(getInstrumentation(), getActivity());

        Context context = getActivity();
        KeyguardManager km = 
                  (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        if (km.inKeyguardRestrictedInputMode())
        {
          KeyguardManager.KeyguardLock lock = km.newKeyguardLock("some_tag");
          lock.disableKeyguard();
          SystemClock.sleep(2000);
        }
        setActivityInitialTouchMode(true);
    }

    /*
     * runs the test for the app.
     */
    public void testMethod()
    {
        mFunctonsForViews = new FunctonsForViews(mSolo);
        mSolo.sleep(DELAY * DELAY);

        mRand = new Random();
        /*
         * the test will take place in the loop, and will be limit in time.
         * in every iteration it will get the vies in activity's, and run a test on a random view.
         */
        //for(int i=0 ; i<LOOP_LIMIT ; i++)
        while(true)
        { 
            mSolo.unlockScreen();
            ArrayList Views = mSolo.getViews();
            int arraySize = Views.size();
            if (arraySize == 0)// now View in activity.
            {
                whenNoViewsInScreen(Views, arraySize);
            }
            if (arraySize != 0)
            {
                int ViewIndexInArray = mRand.nextInt(arraySize + 2);
                if (ViewIndexInArray == arraySize)
                {
                    mSolo.scrollDown();
                }

                else if (ViewIndexInArray == arraySize + 1)     
                {
                    if (!mSolo.getCurrentActivity().getClass().toString().split(" ")[1].equals
                            (LAUNCHER_ACTIVITY_FULL_CLASSNAME))
                            {
                                goingBack();
                            }
                }
                else
                {
                    View randomView = (View)(Views.get(ViewIndexInArray));
                    runTestOnOneView(randomView);
                }
            }
        }
    }

    /*
     * performing clicks onScreen()
     */
    public void myClickOnScreen()
    {
        try {
            mSolo.unlockScreen();
            mSolo.clickOnScreen(mRand.nextInt(SCREEN_SIZE), mRand.nextInt(SCREEN_SIZE));
        } catch (Exception e) {
            e.printStackTrace();
            error_count++;
        } catch (Error e2) {
            error_count++;
            e2.printStackTrace();
        }       
    }

    /*
     * there is no Views available.
     * we will try pressing on screen or the goBack function.
     */
    public void whenNoViewsInScreen(ArrayList Views, int arraySize)
    {
        for (int j = 0; j < WHAITING_FOR_VIEWS_LIMIT; j++)
        {
            for (int k= 0; k < CLICK_ON_LOOP_LIMIT; k++)
            {   
                myClickOnScreen();
            }

            Views = mSolo.getViews();
            arraySize = Views.size();
            if (arraySize != 0)
            {
                return;
            }
            mSolo.sleep(DELAY);
            Views = mSolo.getViews();
            arraySize = Views.size();
            if (arraySize != 0)
            {
                return;
            }
        }
        if (!mSolo.getCurrentActivity().getClass().toString().split(" ")[1].equals
                (LAUNCHER_ACTIVITY_FULL_CLASSNAME))
                {
                    goingBack();
                }
        mSolo.sleep(DELAY);
        return;
    }

    public void runTestOnOneView(View randomView)
    {
        String rawViewName = randomView.getClass().getName();
        String viewName = parseRawViewName(rawViewName);
        if (viewName.contains("ActionBarContainer"))
        {
            return;
        }
        MyRunnable  myRunnable = mFunctonsForViews.getMethodMap().get(viewName);
        try{
            if (myRunnable != null)
            {
                myRunnable.run((View)randomView);               
            }
            else // view not in map.
            {
                boolean inMap = false;
                Iterator it = mFunctonsForViews.getMethodMap().entrySet().iterator();
                /*
                 * iterating in case the View is a version of one of View in map
                 * example:
                 * View is "CustomEditText", and map contains o view "EditText".
                 */
                while (it.hasNext()) 
                {
                    Map.Entry pairs = (Map.Entry)it.next();
                    if (   viewName.contains((String)pairs.getKey())   )
                    {
                        inMap = true;
                        // next two lines changed
                        myRunnable = (MyRunnable)(pairs.getValue());
                        myRunnable.run((View)randomView);
                        break;
                    }
                }
                if (inMap == false)
                {

                    if (viewName.contains("Layout"))
                    {
                        return;
                    }
                    mSolo.clickOnView((View)randomView);
                }
                error_count = 0;
            }
        }catch(Exception exception)
        {
            exception.printStackTrace();
            if(error_count > ERROR_COUNT_LIMIT &&
                    !(mSolo.getCurrentActivity().getClass().toString().split(" ")[1].equals(LAUNCHER_ACTIVITY_FULL_CLASSNAME)))    
            {
                goingBack();
                error_count = 0;
            }
            return;

        }catch(Error error)
        {

            error.printStackTrace();
            error_count ++;
            if(error_count > ERROR_COUNT_LIMIT &&
                    !(mSolo.getCurrentActivity().getClass().toString().split(" ")[1].equals(LAUNCHER_ACTIVITY_FULL_CLASSNAME)))    
            {
                goingBack();
                error_count = 0;
            }
            return;
        }   
        mSolo.sleep(DELAY);
    }

    /*
     * performs a goBack command surrounded with catch/try
     */
    public void goingBack()
    {
        try {
            String currentActivity = mSolo.getCurrentActivity().getClass().toString();
            mSolo.goBack();
            if (mSolo.getCurrentActivity().getClass().toString().equals(currentActivity))
            {
                for (int i = 0; i< 20; i++)
                {
                    myClickOnScreen();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        } catch (Error e) {
            e.printStackTrace();
        }
    }

    /*
     * extract the name of View from raw View name.
     * example:
     * raw View name: android.widget.TextView
     * raw View name:TextView
     */
    public String parseRawViewName(String rawViewName)
    {
        if (rawViewName.contains(" "))
        {
            String [] array = rawViewName.split(" ");
            rawViewName = array [0];
        }

        if (rawViewName.contains(".") || rawViewName.contains("$"))
        {
            String [] array = rawViewName.split("\\.|$");
            rawViewName = array [array.length-1];
        }
        return rawViewName;
    }

    public void tearDown() throws Exception {
        mSolo.finishOpenedActivities();
    }
}

0 个答案:

没有答案