Android中的自動化的測試(1)

來源:互聯網
上載者:User

  Android中的自動化的測試(1)
轉載時請註明出處和作者連絡方式
文章出處:http://www.limodev.cn/blog
作者連絡方式:李先靜 <xianjimli at hotmail dot com>

這幾天做Broncho A1的Android相容性測試(CTS),研究了一下Android的自動化的測試功能。它的執行流程如下:

1.用adb shell去啟動測試程式,如:

adb shell am start -n com.google.android.contacts/.ContactsActivity

am是一個指令碼命令:

# Script to start "am" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/am.jar
exec app_process $base/bin com.android.commands.am.Am $*

不加任何參數運行am,我們可以看協助資訊:

[root@localhost ~]# adb shell am
usage: am [start|broadcast|instrument|profile]
       am start -D INTENT
       am broadcast INTENT
       am instrument [-r] [-e  ] [-p ]
                [-w]
       am profile  [start |stop]

       INTENT is described with:
                [-a ] [-d ] [-t ]
                [-c  [-c ] ...]
                [-e|--es   ...]
                [--ez   ...]
                [-e|--ei   ...]
                [-n ] [-f ] []

am 是在命令列啟動android程式的一種方法,它是在cmds/am/src/com/android/commands/am/Am.java裡實現的。

在函數runInstrument實現了instrument 命令,主要做了下面的事:
1.參數解析
2.建立一個InstrumentationWatcher對象,用來接收測試結果。
3.調用IActivityManager的startInstrumentation運行測試程式。
4.調用InstrumentationWatcher的waitForFinish函數等待測試結束。

private void runInstrument() {
...
        try {
            if (!mAm.startInstrumentation(cn, profileFile, 0, args, watcher)) {
                System.out.println("INSTRUMENTATION_FAILED: " +
                        cn.flattenToString());
                showUsage();
                return;
            }
        } catch (RemoteException e) {
        }
 
...
        if (watcher != null) {
            if (!watcher.waitForFinish()) {
                System.out.println("INSTRUMENTATION_ABORTED: System has crashed.");
            }
        }
...
}

startInstrumentation實際上是在services/java/com/android/server/am/ActivityManagerService.java裡執行的,執行過程如下:
1.調用PackageManager的getInstrumentationInfo擷取InstrumentationInfo和ApplicationInfo。
2.uninstall原來的測試程式(如果前面運行了相同的測試程式,就會停止它)
3.運行新的測試程式。

    public boolean startInstrumentation(ComponentName className,
            String profileFile, int flags, Bundle arguments,
            IInstrumentationWatcher watcher) {
...
      try {
                ii = mContext.getPackageManager().getInstrumentationInfo(
                    className, 0);
                ai = mContext.getPackageManager().getApplicationInfo(
                    ii.targetPackage, PackageManager.GET_SHARED_LIBRARY_FILES);
            } catch (PackageManager.NameNotFoundException e) {
...
            uninstallPackageLocked(ii.targetPackage, -1, true);
            ProcessRecord app = addAppLocked(ai);
...
     }

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/absurd/archive/2010/01/27/5260100.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.