public void testUpdate() {

    insertInstalledApp("com.example.app1", 10, "1.0");
    insertInstalledApp("com.example.app2", 10, "1.0");

    assertResultCount(2, InstalledAppProvider.getContentUri());
    assertIsInstalledVersionInDb("com.example.app2", 10, "1.0");

    try {
      getMockContentResolver()
          .update(
              InstalledAppProvider.getAppUri("com.example.app2"),
              createContentValues(11, "1.1"),
              null,
              null);
      fail();
    } catch (UnsupportedOperationException e) {
      // We expect this to happen, because we should be using insert() instead.
    }

    getMockContentResolver()
        .insert(
            InstalledAppProvider.getContentUri(),
            createContentValues("com.example.app2", 11, "1.1"));

    assertResultCount(2, InstalledAppProvider.getContentUri());
    assertIsInstalledVersionInDb("com.example.app2", 11, "1.1");
  }
  public void testUris() {
    assertInvalidUri(InstalledAppProvider.getAuthority());
    assertInvalidUri(RepoProvider.getContentUri());
    assertInvalidUri(AppProvider.getContentUri());
    assertInvalidUri(ApkProvider.getContentUri());
    assertInvalidUri("blah");

    assertValidUri(InstalledAppProvider.getContentUri());
    assertValidUri(InstalledAppProvider.getAppUri("com.example.com"));
    assertValidUri(InstalledAppProvider.getAppUri("blah"));
  }
  public void testDelete() {

    insertInstalledApp("com.example.app1", 10, "1.0");
    insertInstalledApp("com.example.app2", 10, "1.0");

    assertResultCount(2, InstalledAppProvider.getContentUri());

    getMockContentResolver().delete(InstalledAppProvider.getAppUri("com.example.app1"), null, null);

    assertResultCount(1, InstalledAppProvider.getContentUri());
    assertIsInstalledVersionInDb("com.example.app2", 10, "1.0");
  }
  public void testUpdateWithBroadcast() {

    installAndBroadcast("com.example.toUpgrade", 1, "v0.1");

    assertResultCount(1, InstalledAppProvider.getContentUri());
    assertIsInstalledVersionInDb("com.example.toUpgrade", 1, "v0.1");

    upgradeAndBroadcast("com.example.toUpgrade", 2, "v0.2");

    assertResultCount(1, InstalledAppProvider.getContentUri());
    assertIsInstalledVersionInDb("com.example.toUpgrade", 2, "v0.2");
  }
  public void testInsert() {

    assertResultCount(0, InstalledAppProvider.getContentUri());

    insertInstalledApp("com.example.com1", 1, "v1");
    insertInstalledApp("com.example.com2", 2, "v2");
    insertInstalledApp("com.example.com3", 3, "v3");

    assertResultCount(3, InstalledAppProvider.getContentUri());
    assertIsInstalledVersionInDb("com.example.com1", 1, "v1");
    assertIsInstalledVersionInDb("com.example.com2", 2, "v2");
    assertIsInstalledVersionInDb("com.example.com3", 3, "v3");
  }
  public void testDeleteWithBroadcast() {

    installAndBroadcast("com.example.toKeep", 1, "v0.1");
    installAndBroadcast("com.example.toDelete", 1, "v0.1");

    assertResultCount(2, InstalledAppProvider.getContentUri());
    assertIsInstalledVersionInDb("com.example.toKeep", 1, "v0.1");
    assertIsInstalledVersionInDb("com.example.toDelete", 1, "v0.1");

    removeAndBroadcast("com.example.toDelete");

    assertResultCount(1, InstalledAppProvider.getContentUri());
    assertIsInstalledVersionInDb("com.example.toKeep", 1, "v0.1");
  }
  public void testInsertWithBroadcast() {

    installAndBroadcast("com.example.broadcasted1", 10, "v1.0");
    installAndBroadcast("com.example.broadcasted2", 105, "v1.05");

    assertResultCount(2, InstalledAppProvider.getContentUri());
    assertIsInstalledVersionInDb("com.example.broadcasted1", 10, "v1.0");
    assertIsInstalledVersionInDb("com.example.broadcasted2", 105, "v1.05");
  }
 private void insertInstalledApp(String appId, int versionCode, String versionNumber) {
   ContentValues values = createContentValues(appId, versionCode, versionNumber);
   getMockContentResolver().insert(InstalledAppProvider.getContentUri(), values);
 }
 public InstalledAppProviderTest() {
   super(InstalledAppProvider.class, InstalledAppProvider.getAuthority());
 }