[英]How to Mock BluetoothAdapter.ACTION_REQUEST_ENABLE
I'm trying to solve a problem with a the following code which i have got to test: I'm using Mockito and Robotium to solve the most testcases. 我正在尝试使用下面的代码来解决问题,我必须对其进行测试:我正在使用Mockito和Robotium解决大多数测试用例。 The problem is to mock an answer from the user by a request dialog started through: 问题是通过以下请求对话框模拟用户的答案:
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, BluetoothAdapterService.REQ_BT_ENABLE);
The answer is cached by 答案被缓存
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) { /*TODO: für 1 eine Konstante verwenden, hier und in der aufrufenden Funktion*/
if (resultCode == BluetoothAdapterService.REQ_BT_ENABLE) { /*TODO RequestCode == ResultCode dh. der COde kann hier optimiert werden*/
btAdapterService.setBluetoothState(BluetoothAdapterService.ON);
}
else {
btAdapterService.setBluetoothState(BluetoothAdapterService.OFF); /* Wenn keine Verbindung aufgebaut werden konnte, oder die Aktivierungsaufforderung des -> */ /* BT-Adapters mit nein quittiert wurde, ist der resultCode = RESULT_CANCELD = 0 */
}
}
}
I have tried some solutions to get the button clicked which shows up during the test but although Iam using robotium I cant acces the button by 我已经尝试了一些解决方案来使按钮在测试过程中显示出来,但是尽管Iam使用robotium我无法通过以下方式访问按钮
solo.clickOnText("No");
Is it also possible to paste the request into another class and call a method for it? 是否还可以将请求粘贴到另一个类中并为其调用方法? So that i could reuse the request in diffrent ways? 这样我就可以以不同的方式重用请求? I tried this out but then i got the problem that no request shows up and i get an exception. 我尝试了一下,但是后来出现了没有请求出现的问题,并且出现了异常。
I'm not an indeep programmer and i have started know with this topic. 我不是一个程序员,我已经开始了解这个话题。 I would appreciate every help. 我将不胜感激。
I don't know what is purpose of your tests. 我不知道您的考试目的是什么。 As for me it sounds like you want to be sure that your code processes user's answer correctly. 对于我来说,这听起来像是要确保您的代码正确处理用户的回答。
So for me it would be enough just to call onActivityResult(1,BluetoothAdapterService.REQ_BT_ENABLE)
and check after that bluetooth was enabled. 因此,对我来说,仅调用onActivityResult(1,BluetoothAdapterService.REQ_BT_ENABLE)
并在启用该蓝牙后进行检查就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.