DistroFormatVersionTest.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 DistroFormatVersion}.
29 */
30public class DistroFormatVersionTest {
31
32    @Test
33    public void equalsAndHashCode() {
34        DistroFormatVersion one = new DistroFormatVersion(1, 2);
35        assertEqualsContract(one, one);
36
37        DistroFormatVersion two = new DistroFormatVersion(1, 2);
38        assertEqualsContract(one, two);
39
40        DistroFormatVersion three = new DistroFormatVersion(2, 1);
41        assertFalse(one.equals(three));
42    }
43
44    @Test
45    public void parcelable() {
46        DistroFormatVersion version = new DistroFormatVersion(2, 3);
47
48        Parcel parcel = Parcel.obtain();
49        version.writeToParcel(parcel, 0 /* flags */);
50        parcel.setDataPosition(0);
51
52        DistroFormatVersion newVersion = DistroFormatVersion.CREATOR.createFromParcel(parcel);
53
54        assertEquals(version, newVersion);
55    }
56
57    @Test
58    public void supportsVersion() {
59        DistroFormatVersion deviceVersion = new DistroFormatVersion(2, 2);
60        assertTrue(deviceVersion.supports(deviceVersion));
61
62        DistroFormatVersion sameVersion = new DistroFormatVersion(2, 2);
63        assertTrue(deviceVersion.supports(sameVersion));
64
65        // Minor versions are backwards compatible.
66        DistroFormatVersion sameMajorNewerMinor = new DistroFormatVersion(2, 3);
67        assertTrue(deviceVersion.supports(sameMajorNewerMinor));
68        DistroFormatVersion sameMajorOlderMinor = new DistroFormatVersion(2, 1);
69        assertFalse(deviceVersion.supports(sameMajorOlderMinor));
70
71        // Major versions are not backwards compatible.
72        DistroFormatVersion newerMajor = new DistroFormatVersion(1, 2);
73        assertFalse(deviceVersion.supports(newerMajor));
74        DistroFormatVersion olderMajor = new DistroFormatVersion(3, 2);
75        assertFalse(deviceVersion.supports(olderMajor));
76    }
77
78    private static void assertEqualsContract(DistroFormatVersion one, DistroFormatVersion two) {
79        assertEquals(one, two);
80        assertEquals(one.hashCode(), two.hashCode());
81    }
82}
83