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