我想在ServiceTestCase中获取共享首选项:
SharedPreferences preferences = context.getSharedPreferences("preferences_name", Context.MODE_PRIVATE);
并做类似的事情:
int value = preferences.getInt("key", 0);
preferences.edit().putInt("key", ++value).commit();
int newValue = preferences.getInt("key", 0);
assertTrue(value != newValue);
然而,这不起作用。 测试失败了。
我尝试过不同的语境:
getContext();
和
getSystemContext();
以及
getContext().createPackageContext(this.getClass().getPackage().getName(), Context.CONTEXT_IGNORE_SECURITY);
最后
getSystemContext().createPackageContext(this.getClass().getPackage().getName(), Context.CONTEXT_IGNORE_SECURITY);
我做错了吗?
或者也许不可能在ServiceTestCase中获取自定义共享首选项文件?
如何在活动测试课程中实现这一目标?