有没有办法在AndroidTestCase中打开和关闭连接?

时间:2011-02-18 17:46:45

标签: android unit-testing android-emulator automated-tests

我打算开始编写一些android测试用例,我想知道是否有一种方法可以在模拟器中以编程方式打开和关闭连接。

// High level puesdo code

Test: is Message Pushed to Local Storage if Phone is Not Connected to Network:

    Connectivity = off

    Create message

    try push message to webapp

    assert message in local storage

    Connectivity = on

如果答案很明显,那么在android中测试用例的新功能如此道歉。

1 个答案:

答案 0 :(得分:1)

要删除连接,您应删除已定义的APN,具体如下:http://www.mail-archive.com/android-developers@googlegroups.com/msg62089.html

此存储库应该包含您需要的所有内容:http://code.google.com/p/apndroid/

无论如何,下面是我发现的一些片段......

阅读当前的APN:

Cursor cursor = 
getContentResolver().(Uri.parse("content://telephony/
carriers"),
null, null, 
null, null);
StringBuffer sb = new StringBuffer();

cursor.moveToFirst();
do{
sb.append("_id =").append(cursor.getString(0)).append(" , ");
sb.append("name=").append(cursor.getString(1)).append(" , ");
sb.append("numeric=").append(cursor.getString(2)).append(" , ");
sb.append("mcc=").append(cursor.getString(3)).append(" , ");
sb.append("mnc=").append(cursor.getString(4)).append(" , ");
sb.append("apn=").append(cursor.getString(5)).append(" , ");
sb.append("user=").append(cursor.getString(6)).append(" , ");
sb.append("server=").append(cursor.getString(7)).append(" , ");
sb.append("password=").append(cursor.getString(8)).append(" , 
");
sb.append("proxy=").append(cursor.getString(9)).append(" , ");
sb.append("port=").append(cursor.getString(10)).append(" , ");
sb.append("mmsproxy=").append(cursor.getString(11)).append(" , 
");
sb.append("mmsport=").append(cursor.getString(12)).append(" , 
");
sb.append("mmsc=").append(cursor.getString(13)).append(" , ");
sb.append("type=").append(cursor.getString(14)).append(" , ");
sb.append("current=").append(cursor.getString(15)).append(" , 
");

sb.append("\n--- BLOCK ---\n");

}while(cursor.moveToNext());

要添加APN:

//Trying to Insert a Custom Setting to Telephony db
ContentValues values = new ContentValues();
values.put("_id","3");
values.put("name","Ramesh");
values.put("numeric","310995");
values.put("mcc","315");
values.put("mnc","995");
values.put("apn","email");
values.put("user","elkjop");
values.put("server","www.moota.com");
values.put("password","elkjop");
values.put("proxy","12");
values.put("port","12");
values.put("mmsproxy","12");
values.put("mmsport","12");
values.put("mmsc","12");
values.put("type","a");
values.put("current","a");

//Adding Values using the Content Resolver
getContentResolver().insert(Uri.parse("content://telephony/
carriers"), values);

我在这里找到了这个:https://groups.google.com/forum/#!topic/android-developers/fYwf_mhEX3Y

通过该ContentResolver,你也可以删除它们。

然后您将删除APN,然后对其进行恢复,这样就可以做到你想要的。