blob: e22cb89195946bd72cffea7b97be9f95ef504bc0 [file] [log] [blame]
Neil Fullerbede17c2017-03-16 18:29:36 +00001/*
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
Neil Fullerbede17c2017-03-16 18:29:36 +000019import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
22
23import android.os.Parcel;
Aurimas Liutikasbdbde552017-12-19 13:21:10 -080024import android.support.test.filters.LargeTest;
Neil Fullerbede17c2017-03-16 18:29:36 +000025
26import org.junit.Test;
27
28/**
29 * Tests for {@link RulesState}.
30 */
Aurimas Liutikasbdbde552017-12-19 13:21:10 -080031@LargeTest
Neil Fullerbede17c2017-03-16 18:29:36 +000032public 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
Neil Fullera1ae0252019-02-18 10:49:15 +000048 RulesState differentBaseRules = new RulesState(
Neil Fullerbede17c2017-03-16 18:29:36 +000049 "2016b", formatVersion(1, 2), false /* operationInProgress */,
50 RulesState.STAGED_OPERATION_INSTALL, rulesVersion("2016a", 3),
51 RulesState.DISTRO_STATUS_INSTALLED, rulesVersion("2016b", 2));
Neil Fullera1ae0252019-02-18 10:49:15 +000052 assertFalse(one.equals(differentBaseRules));
Neil Fullerbede17c2017-03-16 18:29:36 +000053
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 */);
Neil Fuller01534812017-06-22 17:58:02 +0100109 checkParcelableRoundTrip(rulesStateWithUnknowns);
Neil Fullerbede17c2017-03-16 18:29:36 +0000110 }
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
Neil Fullera1ae0252019-02-18 10:49:15 +0000123 public void isBaseVersionNewerThan() {
Neil Fullerbede17c2017-03-16 18:29:36 +0000124 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));
Neil Fullera1ae0252019-02-18 10:49:15 +0000128 assertTrue(rulesState.isBaseVersionNewerThan(rulesVersion("2016a", 1)));
129 assertFalse(rulesState.isBaseVersionNewerThan(rulesVersion("2016b", 1)));
130 assertFalse(rulesState.isBaseVersionNewerThan(rulesVersion("2016c", 1)));
Neil Fullerbede17c2017-03-16 18:29:36 +0000131 }
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}