RulesStateTest.java revision 8f4a5361147fef049f4437bdcd54fd4a114c8766
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;
26
27import org.junit.Test;
28
29/**
30 * Tests for {@link RulesState}.
31 */
32public class RulesStateTest {
33
34    @Test
35    public void equalsAndHashCode() {
36        RulesState one = new RulesState(
37                "2016a", formatVersion(1, 2), false /* operationInProgress */,
38                RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3),
39                RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2));
40        assertEqualsContract(one, one);
41
42        RulesState two = new RulesState(
43                "2016a", formatVersion(1, 2), false /* operationInProgress */,
44                RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3),
45                RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2));
46        assertEqualsContract(one, two);
47
48        RulesState differentSystemRules = new RulesState(
49                "2016b", formatVersion(1, 2), false /* operationInProgress */,
50                RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3),
51                RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2));
52        assertFalse(one.equals(differentSystemRules));
53
54        RulesState differentFormatVersion = new RulesState(
55                "2016a", formatVersion(1, 1), false /* operationInProgress */,
56                RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3),
57                RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2));
58        assertFalse(one.equals(differentFormatVersion));
59
60        RulesState differentOperationInProgress = new RulesState(
61                "2016a", formatVersion(1, 1), true /* operationInProgress */,
62                RulesState.STAGED_OPERATION_UNKNOWN, null /* stagedDistroRulesVersion */,
63                RulesState.DISTRO_STATUS_UNKNOWN, null /* installedDistroRulesVersion */);
64        assertFalse(one.equals(differentOperationInProgress));
65
66        RulesState differentStagedOperation = new RulesState(
67                "2016a", formatVersion(1, 1), false /* operationInProgress */,
68                RulesState.STAGED_OPERATION_UNINSTALL, null /* stagedDistroRulesVersion */,
69                RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2));
70        assertFalse(one.equals(differentStagedOperation));
71
72        RulesState differentStagedInstallVersion = new RulesState(
73                "2016a", formatVersion(1, 1), false /* operationInProgress */,
74                RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 4),
75                RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2));
76        assertFalse(one.equals(differentStagedInstallVersion));
77
78        RulesState differentInstalled = new RulesState(
79                "2016a", formatVersion(1, 1), false /* operationInProgress */,
80                RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3),
81                RulesState.DISTRO_STATUS_NONE, null /* installedDistroRulesVersion */);
82        assertFalse(one.equals(differentInstalled));
83
84        RulesState differentInstalledVersion = new RulesState(
85                "2016a", formatVersion(1, 1), false /* operationInProgress */,
86                RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3),
87                RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 3));
88        assertFalse(one.equals(differentInstalledVersion));
89    }
90
91    @Test
92    public void parcelable() {
93        RulesState rulesState1 = new RulesState(
94                "2016a", formatVersion(1, 1), false /* operationInProgress */,
95                RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016b", 2),
96                RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 3));
97        checkParcelableRoundTrip(rulesState1);
98
99        RulesState rulesStateWithNulls = new RulesState(
100                "2016a", formatVersion(1, 1), false /* operationInProgress */,
101                RulesState.STAGED_OPERATION_NONE, null /* stagedDistroRulesVersion */,
102                RulesState.DISTRO_STATUS_NONE, null /* installedDistroRulesVersion */);
103        checkParcelableRoundTrip(rulesStateWithNulls);
104
105        RulesState rulesStateWithUnknowns = new RulesState(
106                "2016a", formatVersion(1, 1), true /* operationInProgress */,
107                RulesState.STAGED_OPERATION_UNKNOWN, null /* stagedDistroRulesVersion */,
108                RulesState.DISTRO_STATUS_UNKNOWN, null /* installedDistroRulesVersion */);
109        checkParcelableRoundTrip(rulesStateWithUnknowns);
110    }
111
112    private static void checkParcelableRoundTrip(RulesState rulesState) {
113        Parcel parcel = Parcel.obtain();
114        rulesState.writeToParcel(parcel, 0 /* flags */);
115        parcel.setDataPosition(0);
116
117        RulesState newVersion = RulesState.CREATOR.createFromParcel(parcel);
118
119        assertEquals(rulesState, newVersion);
120    }
121
122    @Test
123    public void isSystemVersionNewerThan() {
124        RulesState rulesState = new RulesState(
125                "2016b", formatVersion(1, 1), false /* operationInProgress */,
126                RulesState.STAGED_OPERATION_NONE, null /* stagedDistroRulesVersion */,
127                RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 3));
128        assertTrue(rulesState.isSystemVersionNewerThan(rulesVersion("2016a", 1)));
129        assertFalse(rulesState.isSystemVersionNewerThan(rulesVersion("2016b", 1)));
130        assertFalse(rulesState.isSystemVersionNewerThan(rulesVersion("2016c", 1)));
131    }
132
133    private static void assertEqualsContract(RulesState one, RulesState two) {
134        assertEquals(one, two);
135        assertEquals(one.hashCode(), two.hashCode());
136    }
137
138    private static DistroRulesVersion rulesVersion(String rulesVersion, int revision) {
139        return new DistroRulesVersion(rulesVersion, revision);
140    }
141
142    private static DistroFormatVersion formatVersion(int majorVersion, int minorVersion) {
143        return new DistroFormatVersion(majorVersion, minorVersion);
144    }
145}
146