Merge "Fix code and test related to rules version check"

This commit is contained in:
Treehugger Robot
2017-06-22 21:23:52 +00:00
committed by Gerrit Code Review
2 changed files with 12 additions and 68 deletions

View File

@@ -174,29 +174,14 @@ public final class RulesState implements Parcelable {
}
/**
* Returns true if the distro IANA rules version supplied is newer or the same as the version in
* the system image data files.
* Returns true if the system image data files contain IANA rules data that are newer than the
* distro IANA rules version supplied, i.e. true when the version specified would be "worse"
* than the one that is in the system image. Returns false if the system image version is the
* same or older, i.e. false when the version specified would be "better" than the one that is
* in the system image.
*/
public boolean isSystemVersionOlderThan(DistroRulesVersion distroRulesVersion) {
return mSystemRulesVersion.compareTo(distroRulesVersion.getRulesVersion()) < 0;
}
public boolean isDistroInstalled() {
return mDistroStatus == DISTRO_STATUS_INSTALLED;
}
/**
* Returns true if the rules version supplied is newer than the one currently installed. If
* there is no installed distro this method throws IllegalStateException.
*/
public boolean isInstalledDistroOlderThan(DistroRulesVersion distroRulesVersion) {
if (mOperationInProgress) {
throw new IllegalStateException("Distro state not known: operation in progress.");
}
if (!isDistroInstalled()) {
throw new IllegalStateException("No distro installed.");
}
return mInstalledDistroRulesVersion.isOlderThan(distroRulesVersion);
public boolean isSystemVersionNewerThan(DistroRulesVersion distroRulesVersion) {
return mSystemRulesVersion.compareTo(distroRulesVersion.getRulesVersion()) > 0;
}
public static final Parcelable.Creator<RulesState> CREATOR =

View File

@@ -107,7 +107,7 @@ public class RulesStateTest {
"2016a", formatVersion(1, 1), true /* operationInProgress */,
RulesState.STAGED_OPERATION_UNKNOWN, null /* stagedDistroRulesVersion */,
RulesState.DISTRO_STATUS_UNKNOWN, null /* installedDistroRulesVersion */);
checkParcelableRoundTrip(rulesStateWithNulls);
checkParcelableRoundTrip(rulesStateWithUnknowns);
}
private static void checkParcelableRoundTrip(RulesState rulesState) {
@@ -121,55 +121,14 @@ public class RulesStateTest {
}
@Test
public void isSystemVersionOlderThan() {
public void isSystemVersionNewerThan() {
RulesState rulesState = new RulesState(
"2016b", formatVersion(1, 1), false /* operationInProgress */,
RulesState.STAGED_OPERATION_NONE, null /* stagedDistroRulesVersion */,
RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 3));
assertFalse(rulesState.isSystemVersionOlderThan(rulesVersion("2016a", 1)));
assertFalse(rulesState.isSystemVersionOlderThan(rulesVersion("2016b", 1)));
assertTrue(rulesState.isSystemVersionOlderThan(rulesVersion("2016c", 1)));
}
@Test
public void isInstalledDistroOlderThan() {
RulesState operationInProgress = new RulesState(
"2016b", formatVersion(1, 1), true /* operationInProgress */,
RulesState.STAGED_OPERATION_UNKNOWN, null /* stagedDistroRulesVersion */,
RulesState.STAGED_OPERATION_UNKNOWN, null /* installedDistroRulesVersion */);
try {
operationInProgress.isInstalledDistroOlderThan(rulesVersion("2016b", 1));
fail();
} catch (IllegalStateException expected) {
}
RulesState nothingInstalled = new RulesState(
"2016b", formatVersion(1, 1), false /* operationInProgress */,
RulesState.STAGED_OPERATION_NONE, null /* stagedDistroRulesVersion */,
RulesState.DISTRO_STATUS_NONE, null /* installedDistroRulesVersion */);
try {
nothingInstalled.isInstalledDistroOlderThan(rulesVersion("2016b", 1));
fail();
} catch (IllegalStateException expected) {
}
DistroRulesVersion installedVersion = rulesVersion("2016b", 3);
RulesState rulesStateWithInstalledVersion = new RulesState(
"2016b", formatVersion(1, 1), false /* operationInProgress */,
RulesState.STAGED_OPERATION_NONE, null /* stagedDistroRulesVersion */,
RulesState.DISTRO_STATUS_INSTALLED, installedVersion);
DistroRulesVersion olderRules = rulesVersion("2016a", 1);
assertEquals(installedVersion.isOlderThan(olderRules),
rulesStateWithInstalledVersion.isInstalledDistroOlderThan(olderRules));
DistroRulesVersion sameRules = rulesVersion("2016b", 1);
assertEquals(installedVersion.isOlderThan(sameRules),
rulesStateWithInstalledVersion.isInstalledDistroOlderThan(sameRules));
DistroRulesVersion newerRules = rulesVersion("2016c", 1);
assertEquals(installedVersion.isOlderThan(newerRules),
rulesStateWithInstalledVersion.isInstalledDistroOlderThan(newerRules));
assertTrue(rulesState.isSystemVersionNewerThan(rulesVersion("2016a", 1)));
assertFalse(rulesState.isSystemVersionNewerThan(rulesVersion("2016b", 1)));
assertFalse(rulesState.isSystemVersionNewerThan(rulesVersion("2016c", 1)));
}
private static void assertEqualsContract(RulesState one, RulesState two) {