如何通过 Robolectric 对位置提供程序进行单元测试?
How to unit test location provider by Robolectric?
有很多关于单元测试位置的好文档,例如 Android - Robolectric - Unit testing request location updates (LocationManager),但遗憾的是没有找到任何关于位置提供程序的文档。由于我是 Robolectric 的新手,所以我仍然不清楚它是如何工作的。任何想法将不胜感激。
以下代码是我 activity 中的一个方法。如果此方法 return 为 false,我将显示一个 cardView,否则它是不可见的。所以我实际上想测试此视图的可见性,但在此之前我需要将位置提供程序模拟为 return 我想要的。这是我第一步要寻找的东西。谢谢
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 允许调用 Settings
class 并设置值:
Settings.Secure.putString(contentResolver, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "locationProvider");
把你想要的放在那里,它应该return你在测试中设置的。
有很多关于单元测试位置的好文档,例如 Android - Robolectric - Unit testing request location updates (LocationManager),但遗憾的是没有找到任何关于位置提供程序的文档。由于我是 Robolectric 的新手,所以我仍然不清楚它是如何工作的。任何想法将不胜感激。
以下代码是我 activity 中的一个方法。如果此方法 return 为 false,我将显示一个 cardView,否则它是不可见的。所以我实际上想测试此视图的可见性,但在此之前我需要将位置提供程序模拟为 return 我想要的。这是我第一步要寻找的东西。谢谢
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 允许调用 Settings
class 并设置值:
Settings.Secure.putString(contentResolver, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "locationProvider");
把你想要的放在那里,它应该return你在测试中设置的。