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