DistroRulesVersionTest.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 org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
22
23import android.os.Parcel;
24
25import org.junit.Test;
26
27/**
28 * Tests for {@link DistroRulesVersion}.
29 */
30public class DistroRulesVersionTest {
31
32    @Test
33    public void equalsAndHashCode() {
34        DistroRulesVersion one = new DistroRulesVersion("2016a", 2);
35        assertEqualsContract(one, one);
36
37        DistroRulesVersion two = new DistroRulesVersion("2016a", 2);
38        assertEqualsContract(one, two);
39
40        DistroRulesVersion three = new DistroRulesVersion("2016b", 1);
41        assertFalse(one.equals(three));
42    }
43
44    @Test
45    public void parcelable() {
46        DistroRulesVersion version = new DistroRulesVersion("2016a", 2);
47
48        Parcel parcel = Parcel.obtain();
49        version.writeToParcel(parcel, 0 /* flags */);
50        parcel.setDataPosition(0);
51
52        DistroRulesVersion newVersion = DistroRulesVersion.CREATOR.createFromParcel(parcel);
53
54        assertEquals(version, newVersion);
55    }
56
57    @Test
58    public void isOlderThan() {
59        DistroRulesVersion deviceVersion = new DistroRulesVersion("2016b", 2);
60        assertFalse(deviceVersion.isOlderThan(deviceVersion));
61
62        DistroRulesVersion sameVersion = new DistroRulesVersion("2016b", 2);
63        assertFalse(deviceVersion.isOlderThan(sameVersion));
64
65        DistroRulesVersion sameRulesNewerRevision = new DistroRulesVersion("2016b", 3);
66        assertTrue(deviceVersion.isOlderThan(sameRulesNewerRevision));
67
68        DistroRulesVersion sameRulesOlderRevision = new DistroRulesVersion("2016b", 1);
69        assertFalse(deviceVersion.isOlderThan(sameRulesOlderRevision));
70
71        DistroRulesVersion newerRules = new DistroRulesVersion("2016c", 2);
72        assertTrue(deviceVersion.isOlderThan(newerRules));
73
74        DistroRulesVersion olderRules = new DistroRulesVersion("2016a", 2);
75        assertFalse(deviceVersion.isOlderThan(olderRules));
76    }
77
78    private static void assertEqualsContract(DistroRulesVersion one, DistroRulesVersion two) {
79        assertEquals(one, two);
80        assertEquals(one.hashCode(), two.hashCode());
81    }
82}
83