#
總覽
Name | Location | Description |
---|---|---|
libmock_ril | hardware\ril\mock-ril | A mock ril like SecRil. |
mockrilcontroller | frameworks\opt\telephony\mockril | Communicate with libmock_ril,send and receive fake RIL cmd to/from libmock_ril via socket. |
TelephonyMockRilTests | frameworks\opt\telephony\tests\telephonymockriltests | A Package communicates with libmock_ril via mockrilcontroller, it use InstrumentationTestCase framework to test libmock_ril`s mock functions. |
FrameworksTelephonyTests | frameworks\opt\telephony\tests\telephonytests | A Package using InstrumentationTestRunner and TestCase to test Telephony. |
上圖中,同一層的模塊功能相同:
RIL
Reference-ril是Android提供的參考代碼。Mock-ril則是一個模擬的Ril模塊。三者的功能相同。
Telephony
Telephony和RIL層之間通過Socket通信,mockrilcontroller功能和telephony-common類似,只不過其是與Mock-ril通信。還有一個區別是telephony-common編譯爲動態庫,而mockrilcontroller爲靜態庫
Package
TelephonyMockRilTests 模塊中包含靜態庫mockrilcontroller。通過查看Manifest可以看出TelephonyMockRilTests 測試的目標package是其本身。主要目的是測試mockrilcontroller和Mock-ril模塊。
FrameworksTelephonyTests模塊中則包含動態庫telephony-common,通過查看Manifest可以看出FrameworksTelephonyTests測試的目標package也是其本身。主要目的是測試telephony-common模塊。
FrameworksTelephonyTests 分析
根據上一章的測試實例分析,現在來看FrameworksTelephonyTests就不太複雜了。FrameworksTelephonyTests能夠同時測試Telephony-common和Mock-ril模塊(TelephonyMockRilTests只是對Mock-ril的一個簡單測試,在FrameworksTelephonyTests已經覆蓋了這部分測試,所以就不再單獨分析 TelephonyMockRilTests)。
測試是從TestRunner開始的,FrameworksTelephonyTests新建了一個自定義的TestRunner類TelephonyMockRilTestRunner,用來輔助測試Mock-ril,而對Telephony-common模塊的測試,則是直接使用的Android測試框架的的InstrumentationTestRunner。
如下圖所示,由於測試用例較多,這裏僅挑選2個典型的測試用例進行說明。
GSMPhoneTest
GSMPhoneTest繼承了AndroidTestCase類並實現了PerformanceTestCase接口,根據上文可知,AndroidTestCase 繼承於JUnit的TestCase,並沒有使用Instrumentation框架,所以GSMPhoneTest該用例不能使用Android Instrumentation框架的方法。
1 setUp():
首先初始化GSMTestHandler和GSMPhone對象,得到GSMPhone的引用。並通過GSMPhone對象的方法,註冊EVENT_POST_DIAL等一系列事件。
sc = new SimulatedCommands();
mGSMPhone = new GSMPhone(mContext, sc, new TestPhoneNotifier(), true);
不過新建GSMPhone時,傳遞的RIL接口是一個模擬的接口,這樣在調用GSMPhone方法時,並非調用到正常的RIL.java發送AT指令。而是一個模擬的ril對象SimulatedCommands上。
這樣,該測試除了ril接口和調用者是虛擬的,其他Telephony流程和正常流程一致,見下圖:
2 testOutgoingCallFailImmediately():
該測試方法用來測試呼出的呼叫立即失敗的邊緣測試(呼叫還沒有出現在call list之前就立即終結),這會導致該失敗的呼叫在ForegroundCall list中以IDLE call的形式出現。
public void testOutgoingCallFailImmediately() throws Exception {
Message msg;
mRadioControl.setNextDialFailImmediately(true);
Connection cn = mGSMPhone.dial("+13125551212");
msg = mGSMTestHandler.waitForMessage(EVENT_DISCONNECT);
assertNotNull("Message Time Out", msg);
assertEquals(PhoneConstants.State.IDLE, mGSMPhone.getState());
assertEquals(Connection.DisconnectCause.NORMAL, cn.getDisconnectCause());
assertEquals(0, mGSMPhone.getRingingCall().getConnections().size());
assertEquals(1, mGSMPhone.getForegroundCall().getConnections().size());
assertEquals(0, mGSMPhone.getBackgroundCall().getConnections().size());
assertEquals(Call.State.IDLE, mGSMPhone.getRingingCall().getState());
assertEquals(Call.State.DISCONNECTED, mGSMPhone.getForegroundCall().getState());
assertEquals(Call.State.IDLE, mGSMPhone.getBackgroundCall().getState());
assertTrue(mGSMPhone.getForegroundCall().getEarliestCreateTime() > 0);
assertEquals(0, mGSMPhone.getForegroundCall().getEarliestConnectTime());
}
3 testHangupOnOutgoing:
該測試方法用來測試通話呼出後,在不同狀態下(DIALING、ALERTING等)進行掛斷。然後判斷Phone的狀態、Call的狀態和連接斷開原因是否符合預期。
public void testHangupOnOutgoing() throws Exception {
Connection cn;
Message msg;
mRadioControl.setAutoProgressConnectingCall(false);
// Test 1: local hangup in "DIALING" state
mGSMPhone.dial("+13125551212");
do {
assertNotNull("Message Time Out", mGSMTestHandler.waitForMessage(ANY_MESSAGE));
}
while (mGSMPhone.getForegroundCall().getState() != Call.State.DIALING);
cn = mGSMPhone.getForegroundCall().getEarliestConnection();
mGSMPhone.getForegroundCall().hangup();
msg = mGSMTestHandler.waitForMessage(EVENT_DISCONNECT);
assertNotNull("Message Time Out", msg);
assertEquals(PhoneConstants.State.IDLE, mGSMPhone.getState());
assertEquals(Call.State.DISCONNECTED, mGSMPhone.getForegroundCall().getState());
assertEquals(Connection.DisconnectCause.LOCAL, cn.getDisconnectCause());
// Test 2: local hangup in "ALERTING" state
mGSMPhone.dial("+13125551212");
do {
assertNotNull("Message Time Out", mGSMTestHandler.waitForMessage(ANY_MESSAGE));
} while (mGSMPhone.getState() != PhoneConstants.State.OFFHOOK);
mRadioControl.progressConnectingCallState();
do {
assertNotNull("Message Time Out", mGSMTestHandler.waitForMessage(ANY_MESSAGE));
}
while (mGSMPhone.getForegroundCall().getState() != Call.State.ALERTING);
cn = mGSMPhone.getForegroundCall().getEarliestConnection();
mGSMPhone.getForegroundCall().hangup();
msg = mGSMTestHandler.waitForMessage(EVENT_DISCONNECT);
assertNotNull("Message Time Out", msg);
assertEquals(PhoneConstants.State.IDLE, mGSMPhone.getState());
assertEquals(Call.State.DISCONNECTED, mGSMPhone.getForegroundCall().getState());
assertEquals(Connection.DisconnectCause.LOCAL, cn.getDisconnectCause());
// Test 3: local immediate hangup before GSM index is
// assigned (CallTracker.hangupPendingMO case)
mRadioControl.pauseResponses();
cn = mGSMPhone.dial("+13125551212");
cn.hangup();
mRadioControl.resumeResponses();
msg = mGSMTestHandler.waitForMessage(EVENT_DISCONNECT);
assertNotNull("Message Time Out", msg);
assertEquals(PhoneConstants.State.IDLE, mGSMPhone.getState());
assertEquals(Call.State.DISCONNECTED, mGSMPhone.getForegroundCall().getState());
assertEquals(Connection.DisconnectCause.LOCAL, mGSMPhone.getForegroundCall().getEarliestConnection().getDisconnectCause());
}
MockRilTest
MockRilTest繼承了InstrumentationTestCase類,根據上文可知,InstrumentationTestCase繼承於JUnit的TestCase,並使用Instrumentation框架,所以MockRilTest該用例可以使用Android Instrumentation框架的方法。該用例是由自定義的TestRunner——TelephonyMockRilTestRunner運行的。並持有RilChannel對象引用。進而調用RIL層的mock-ril進行測試。
1 setUp():
首先初始化TelephonyMockRilTestRunner和RilChannel對象,得到RilChannel的引用。通過RilChannel的socket和mock-ril進行通信。
如下圖所示:
注:聚合關係的方向畫錯了,暫時沒法修改,見諒。
MockRilTest通過RilChannel和mock-ril建立socket連接並通信。MockRilTest的各個測試方法通過Msg類的靜態方法,收發RilChannel指令。
mRunner = (TelephonyMockRilTestRunner)getInstrumentation();
mMockRilChannel = mRunner.mMockRilChannel;
2 testGetRadioState():
public void testGetRadioState() throws IOException {
log("testGetRadioState E");
Msg.send(mMockRilChannel, 1, 9876, 0, null);
Msg resp = Msg.recv(mMockRilChannel);
//resp.printHeader("testGetRadioState");
assertTrue(String.format("expected cmd == 1 was %d", resp.getCmd()),
resp.getCmd() == 1);
assertTrue(String.format("expected token == 9876 was %d", resp.getToken()),
resp.getToken() == 9876);
assertTrue(String.format("expected status == 0 was %d", resp.getStatus()),
resp.getStatus() == 0);
RilCtrlCmds.CtrlRspRadioState rsp = resp.getDataAs(RilCtrlCmds.CtrlRspRadioState.class);
int state = rsp.getState();
log("testGetRadioState state=" + state);
assertTrue(String.format("expected RadioState >= 0 && RadioState <= 9 was %d", state),
((state >= 0) && (state <= 9)));
log("testGetRadioState X");
}
3 testStartIncomingCallAndHangup():
public void testStartIncomingCallAndHangup() throws IOException {
log("testStartIncomingCallAndHangup");
RilCtrlCmds.CtrlReqSetMTCall cmd = new RilCtrlCmds.CtrlReqSetMTCall();
String incomingCall = "6502889108";
// set the MT call
cmd.setPhoneNumber(incomingCall);
Msg.send(mMockRilChannel, RilCtrlCmds.CTRL_CMD_SET_MT_CALL, 0, 0, cmd);
// get response
Msg resp = Msg.recv(mMockRilChannel);
log("Get response status: " + resp.getStatus());
assertTrue("The ril is not in a proper state to set MT calls.",
resp.getStatus() == RilCtrlCmds.CTRL_STATUS_OK);
// allow the incoming call alerting for some time
try {
Thread.sleep(5000);
} catch (InterruptedException e) {}
// we are playing a trick to assume the current is 1
RilCtrlCmds.CtrlHangupConnRemote hangupCmd = new RilCtrlCmds.CtrlHangupConnRemote();
hangupCmd.setConnectionId(1);
hangupCmd.setCallFailCause(16); // normal hangup
Msg.send(mMockRilChannel, RilCtrlCmds.CTRL_CMD_HANGUP_CONN_REMOTE, 0, 0, hangupCmd);
// get response
resp = Msg.recv(mMockRilChannel);
log("Get response for hangup connection: " + resp.getStatus());
assertTrue("CTRL_CMD_HANGUP_CONN_REMOTE failed",
resp.getStatus() == RilCtrlCmds.CTRL_STATUS_OK);
}
運行測試
應用編譯並安裝到目標測試機器中後,通過ADB工具進入終端,通過指令:
am instrument -w com.android.frameworks.telephonytests /android.test.InstrumentationTestRunner
即可運行該測試應用,自動運行每一個測試用例,並將測試結果顯示在輸出中。如下所示:
com.android.internal.telephony.ATResponseParserTest:.
com.android.internal.telephony.AdnRecordTest:.
com.android.internal.telephony.ApnSettingTest:
Failure in testFromString:
junit.framework.AssertionFailedError: expected:<2> but was:<4>
at com.android.internal.telephony.ApnSettingTest.assertApnSettingEqual(ApnSettingTest.java:42)
at com.android.internal.telephony.ApnSettingTest.testFromString(ApnSettingTest.java:93)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
.
com.android.internal.telephony.GsmAlphabetTest:...
com.android.internal.telephony.GsmSmsTest:..........
Error in testFragmentTurkishText:
java.lang.IllegalAccessError: tried to access method com.android.internal.telephony.GsmAlphabet.getEnabledSingleShiftTables:()V from class com.android.internal.telephony.GsmSmsTest
at com.android.internal.telephony.GsmSmsTest.testFragmentTurkishText(GsmSmsTest.java:281)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
...
com.android.internal.telephony.IccServiceTableTest:..
com.android.internal.telephony.IntRangeManagerTest:.........
com.android.internal.telephony.MccTableTest:.....
com.android.internal.telephony.NeighboringCellInfoTest:...
com.android.internal.telephony.PhoneNumberUtilsTest:..
Failure in testCheckAndProcessPlusCode:
junit.framework.ComparisonFailure: expected:<[011]8475797000> but was:<[+]8475797000>
總結
作爲白盒測試,測試用例的設計至關重要,對於已有的代碼,其類與類之間相關性較大的情況,測試用例的設計就會增加複雜度,需要設計多個模擬類來替代相關類,必要時應該在代碼設計階段就應該考慮其可測試性。
大多是先設計測試用例,以測試推進開發。