Robolectric packagemanager没有为getNameForUid返回正确的值?

时间:2015-10-09 22:32:16

标签: robolectric android-package-managers

即使添加了包,我仍然会变为null。我查看了源代码,结果发现StubPackageManager总是为此返回null,并且无法覆盖整个PackageManager类。

1 个答案:

答案 0 :(得分:1)

在设置中:

MockPackageManager mockPackageManager = new MockPackageManager(
    Robolectric.getShadowsAdapter());
RuntimeEnvironment.setRobolectricPackageManager(mockPackageManager);

子类:

class MockPackageManager extends DefaultPackageManager {

  public MockPackageManager(ShadowsAdapter shadowsAdapter) {
    super(shadowsAdapter);
  }

  @Override
  public String getNameForUid(int uid) {
    switch (uid) {
      case UID_A:
        return NAME_A;
      case UID_B:
        return NAME_B;
      default:
        return null;
    }
  }

  @Override
  public boolean isPermissionRevokedByPolicy(String s, String s1) {
    return false;
  }
}