RulesStateTest.java revision bdbde55592792efe350acd6a46733f439f6a3f3d
1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.app.timezone; 18 19import static junit.framework.Assert.fail; 20 21import static org.junit.Assert.assertEquals; 22import static org.junit.Assert.assertFalse; 23import static org.junit.Assert.assertTrue; 24 25import android.os.Parcel; 26import android.support.test.filters.LargeTest; 27 28import org.junit.Test; 29 30/** 31 * Tests for {@link RulesState}. 32 */ 33@LargeTest 34public class RulesStateTest { 35 36 @Test 37 public void equalsAndHashCode() { 38 RulesState one = new RulesState( 39 "2016a", formatVersion(1, 2), false /* operationInProgress */, 40 RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3), 41 RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2)); 42 assertEqualsContract(one, one); 43 44 RulesState two = new RulesState( 45 "2016a", formatVersion(1, 2), false /* operationInProgress */, 46 RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3), 47 RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2)); 48 assertEqualsContract(one, two); 49 50 RulesState differentSystemRules = new RulesState( 51 "2016b", formatVersion(1, 2), false /* operationInProgress */, 52 RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3), 53 RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2)); 54 assertFalse(one.equals(differentSystemRules)); 55 56 RulesState differentFormatVersion = new RulesState( 57 "2016a", formatVersion(1, 1), false /* operationInProgress */, 58 RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3), 59 RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2)); 60 assertFalse(one.equals(differentFormatVersion)); 61 62 RulesState differentOperationInProgress = new RulesState( 63 "2016a", formatVersion(1, 1), true /* operationInProgress */, 64 RulesState.STAGED_OPERATION_UNKNOWN, null /* stagedDistroRulesVersion */, 65 RulesState.DISTRO_STATUS_UNKNOWN, null /* installedDistroRulesVersion */); 66 assertFalse(one.equals(differentOperationInProgress)); 67 68 RulesState differentStagedOperation = new RulesState( 69 "2016a", formatVersion(1, 1), false /* operationInProgress */, 70 RulesState.STAGED_OPERATION_UNINSTALL, null /* stagedDistroRulesVersion */, 71 RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2)); 72 assertFalse(one.equals(differentStagedOperation)); 73 74 RulesState differentStagedInstallVersion = new RulesState( 75 "2016a", formatVersion(1, 1), false /* operationInProgress */, 76 RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 4), 77 RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2)); 78 assertFalse(one.equals(differentStagedInstallVersion)); 79 80 RulesState differentInstalled = new RulesState( 81 "2016a", formatVersion(1, 1), false /* operationInProgress */, 82 RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3), 83 RulesState.DISTRO_STATUS_NONE, null /* installedDistroRulesVersion */); 84 assertFalse(one.equals(differentInstalled)); 85 86 RulesState differentInstalledVersion = new RulesState( 87 "2016a", formatVersion(1, 1), false /* operationInProgress */, 88 RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3), 89 RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 3)); 90 assertFalse(one.equals(differentInstalledVersion)); 91 } 92 93 @Test 94 public void parcelable() { 95 RulesState rulesState1 = new RulesState( 96 "2016a", formatVersion(1, 1), false /* operationInProgress */, 97 RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016b", 2), 98 RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 3)); 99 checkParcelableRoundTrip(rulesState1); 100 101 RulesState rulesStateWithNulls = new RulesState( 102 "2016a", formatVersion(1, 1), false /* operationInProgress */, 103 RulesState.STAGED_OPERATION_NONE, null /* stagedDistroRulesVersion */, 104 RulesState.DISTRO_STATUS_NONE, null /* installedDistroRulesVersion */); 105 checkParcelableRoundTrip(rulesStateWithNulls); 106 107 RulesState rulesStateWithUnknowns = new RulesState( 108 "2016a", formatVersion(1, 1), true /* operationInProgress */, 109 RulesState.STAGED_OPERATION_UNKNOWN, null /* stagedDistroRulesVersion */, 110 RulesState.DISTRO_STATUS_UNKNOWN, null /* installedDistroRulesVersion */); 111 checkParcelableRoundTrip(rulesStateWithUnknowns); 112 } 113 114 private static void checkParcelableRoundTrip(RulesState rulesState) { 115 Parcel parcel = Parcel.obtain(); 116 rulesState.writeToParcel(parcel, 0 /* flags */); 117 parcel.setDataPosition(0); 118 119 RulesState newVersion = RulesState.CREATOR.createFromParcel(parcel); 120 121 assertEquals(rulesState, newVersion); 122 } 123 124 @Test 125 public void isSystemVersionNewerThan() { 126 RulesState rulesState = new RulesState( 127 "2016b", formatVersion(1, 1), false /* operationInProgress */, 128 RulesState.STAGED_OPERATION_NONE, null /* stagedDistroRulesVersion */, 129 RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 3)); 130 assertTrue(rulesState.isSystemVersionNewerThan(rulesVersion("2016a", 1))); 131 assertFalse(rulesState.isSystemVersionNewerThan(rulesVersion("2016b", 1))); 132 assertFalse(rulesState.isSystemVersionNewerThan(rulesVersion("2016c", 1))); 133 } 134 135 private static void assertEqualsContract(RulesState one, RulesState two) { 136 assertEquals(one, two); 137 assertEquals(one.hashCode(), two.hashCode()); 138 } 139 140 private static DistroRulesVersion rulesVersion(String rulesVersion, int revision) { 141 return new DistroRulesVersion(rulesVersion, revision); 142 } 143 144 private static DistroFormatVersion formatVersion(int majorVersion, int minorVersion) { 145 return new DistroFormatVersion(majorVersion, minorVersion); 146 } 147} 148