[英]How to unit test location provider by Robolectric?
There are a lot of good docs regard unit testing location such as Android - Robolectric - Unit testing request location updates (LocationManager) but unfortunately didn't find anything regard location provider. 有很多关于单元测试位置的优秀文档,例如Android-Robolectric-单元测试请求位置更新(LocationManager),但不幸的是没有找到任何有关位置提供程序的信息。 Since I'm new to Robolectric I still have no clear insight how it works.
由于我是Robolectric的新手,所以我仍然不清楚它是如何工作的。 Any idea would be appreciated.
任何想法将不胜感激。
Following code is a method I have in my activity. 以下代码是我从事活动的一种方法。 I display a cardView if this method returns false otherwise it is invisible.
如果此方法返回false,我将显示cardView,否则将不可见。 So I actually want to test visibility of this view but before this I need to mock location provider to return what I want.
因此,我实际上想测试此视图的可见性,但在此之前,我需要模拟位置提供程序以返回所需的内容。 This is the thing that I'm looking for in first step.
这是我第一步要寻找的东西。 Thanks
谢谢
private boolean isLocationEnabled()
{
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
try
{
locationMode = Settings.Secure.getInt(this.getContentResolver(), Settings.Secure.LOCATION_MODE);
}
catch (Settings.SettingNotFoundException e)
{
Logger.logException(e);
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}
else
{
locationProviders = Settings.Secure.getString(this.getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
Robolectric allows you to call into the Settings
class and set values: Robolectric允许您调用
Settings
类并设置值:
Settings.Secure.putString(contentResolver, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "locationProvider");
Put what you want in there and it should return what you've set in the tests. 将您想要的内容放到那里,它应该返回您在测试中设置的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.