InstrumentationProtoResultParser

public class InstrumentationProtoResultParser
extends Object implements IInstrumentationResultParser

java.lang.Object
   ↳ com.android.tradefed.result.ddmlib.InstrumentationProtoResultParser


從殼層剖析檢測設備測試執行的「protoStd 輸出模式」結果,並將結果通知 ITestRunListener。

使用「-m」選項的儀器指令會以二進位 protobuf 格式,逐步輸出測試執行狀態。輸出 protobuf 訊息為 InstrumentationData.Session,其中包含兩個欄位:1) InstrumentationData.TestStatus 的重複欄位,2) InstrumentationData.SessionStatus。am instrument 指令會在每次測試執行前後輸出測試狀態訊息。addOutput(byte, int, int) 會使用 data 引數叫用,該引數是 TestStatus 的序列化位元組。所有測試完成後,指令最後會輸出 SessionStatus

如需完整的 protobuf 定義,請參閱 https://cs.android.com/android/platform/superproject/+/master:frameworks/base/cmds/am/proto/instrumentation_data.proto。

InstrumentationProtoResultParser 是狀態機器,狀態定義於 ERROR(InstrumentationProtoResultParserState/com.android.tradefed.result.ddmlib.InstrumentationProtoResultParser.InstrumentationProtoResultParserState InstrumentationProtoResultParserState)。狀態開頭為 ERROR(InstrumentationProtoResultParserState.NOT_STARTED/com.android.tradefed.result.ddmlib.InstrumentationProtoResultParser.InstrumentationProtoResultParserState#NOT_STARTED InstrumentationProtoResultParserState.NOT_STARTED)。當看到第一個測試案例狀態時,就會移至 ERROR(InstrumentationProtoResultParserState.RUNNING/com.android.tradefed.result.ddmlib.InstrumentationProtoResultParser.InstrumentationProtoResultParserState#RUNNING InstrumentationProtoResultParserState.RUNNING)。如果所有測試都已完成,狀態會變更為 ERROR(InstrumentationProtoResultParserState.FINISHED/com.android.tradefed.result.ddmlib.InstrumentationProtoResultParser.InstrumentationProtoResultParserState#FINISHED InstrumentationProtoResultParserState.FINISHED);如果透過呼叫 cancel() 要求變更,狀態則會變更為 ERROR(InstrumentationProtoResultParserState.CANCELLED/com.android.tradefed.result.ddmlib.InstrumentationProtoResultParser.InstrumentationProtoResultParserState#CANCELLED InstrumentationProtoResultParserState.CANCELLED)

每當有新的測試狀態,系統就會通知已註冊的監聽者。

摘要

公用建構函式

InstrumentationProtoResultParser(String runName, Collection<ITestRunListener> listeners)

建構 InstrumentationProtoResultParser

公用方法

void addOutput(byte[] data, int offset, int length)

每當有新資料可用時,系統就會呼叫這個方法。

void cancel()

要求取消測試執行。

void flush()

在程序執行結束時呼叫 (除非程序已取消)。

void handleTestRunFailed(String errorMsg)

當「am instrument」指令因例外狀況而當機時,系統會呼叫這個方法。

boolean isCancelled()

Cancel 方法可停止執行遠端 shell 命令。

公用建構函式

InstrumentationProtoResultParser

public InstrumentationProtoResultParser (String runName, 
                Collection<ITestRunListener> listeners)

建構 InstrumentationProtoResultParser

參數
runName String:要提供給 ITestRunListener.testRunStarted 的測試執行名稱

listeners Collection:在測試執行期間收到測試結果通知

公用方法

addOutput

public void addOutput (byte[] data, 
                int offset, 
                int length)

每當有新資料可用時,系統就會呼叫這個方法。

參數
data byteInstrumentationData.Session 訊息的序列化資料。如果 data 是不完整的區塊,系統會將其新增至內部緩衝區,並在下一次 addOutput(byte, int, int) 呼叫中處理。

offset int:儲存在 data 中的新資料偏移

length int:要處理的 data 中新資料的位元組。

取消

public void cancel ()

要求取消測試執行。

flush

public void flush ()

在程序執行結束時呼叫 (除非程序已取消)。這可讓接收端終止並清除尚未處理的資料。

handleTestRunFailed

public void handleTestRunFailed (String errorMsg)

當「am instrument」指令因例外狀況而當機時,系統會呼叫這個方法。如果測試執行期間發生當機情形,系統應通知所有已註冊的監聽器,ITestRunListener.testRunFailed 後接 ITestRunListener.testRunEnded

參數
errorMsg String

isCancelled

public boolean isCancelled ()

取消方法,用於停止執行遠端 shell 指令。

傳回
boolean true 可取消執行指令。