• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020, 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 
17 package android.aidl.tests;
18 
19 import static org.hamcrest.core.Is.is;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertThat;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.aidl.tests.Union;
25 import android.aidl.tests.UnionWithFd;
26 import android.aidl.tests.unions.EnumUnion;
27 import android.os.Parcel;
28 import android.os.ParcelFileDescriptor;
29 import android.os.Parcelable;
30 import java.io.File;
31 import java.io.FileDescriptor;
32 import java.io.FileNotFoundException;
33 import java.util.Arrays;
34 import java.util.List;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.JUnit4;
38 
39 @RunWith(JUnit4.class)
40 public class UnionTests {
41   @Test
defaultConstructorInitsWithFirstField()42   public void defaultConstructorInitsWithFirstField() {
43     assertThat(new Union(), is(Union.ns(new int[] {}))); // int[] ns = {}
44     assertThat(
45         new EnumUnion(), is(EnumUnion.intEnum(IntEnum.FOO))); // IntEnum intEnum = IntEnum.FOO
46   }
47 
48   @Test
updatesUnionWithSetter()49   public void updatesUnionWithSetter() {
50     Union u = new Union();
51     u.setNs(new int[] {1, 2, 3});
52     assertThat(u.getTag(), is(Union.ns));
53     assertThat(u.getNs(), is(new int[] {1, 2, 3}));
54   }
55 
56   @Test(expected = IllegalStateException.class)
gettingWrongFieldThrowsException()57   public void gettingWrongFieldThrowsException() {
58     Union u = new Union();
59     u.getSs();
60   }
61 
62   @Test
readWriteViaParcel()63   public void readWriteViaParcel() {
64     List<String> ss = Arrays.asList("hello", "world");
65 
66     Union u = Union.ss(ss);
67     Parcel parcel = Parcel.obtain();
68     u.writeToParcel(parcel, 0);
69     parcel.setDataPosition(0);
70 
71     Union v = Union.CREATOR.createFromParcel(parcel);
72 
73     assertThat(v.getTag(), is(Union.ss));
74     assertThat(v.getSs(), is(ss));
75 
76     parcel.recycle();
77   }
78 
79   @Test
unionDescribeContents()80   public void unionDescribeContents() {
81     UnionWithFd u = UnionWithFd.num(0);
82     assertTrue((u.describeContents() & Parcelable.CONTENTS_FILE_DESCRIPTOR) == 0);
83 
84     final Parcel parcel = Parcel.obtain();
85     try {
86       u.setPfd(ParcelFileDescriptor.open(new File("/system"), ParcelFileDescriptor.MODE_READ_ONLY));
87     } catch (FileNotFoundException e) {
88       throw new RuntimeException("can't open /system", e);
89     }
90     assertTrue((u.describeContents() & Parcelable.CONTENTS_FILE_DESCRIPTOR) != 0);
91 
92     u.writeToParcel(parcel, 0);
93 
94     UnionWithFd v = UnionWithFd.num(0);
95     parcel.setDataPosition(0);
96     v.readFromParcel(parcel);
97     assertTrue((v.describeContents() & Parcelable.CONTENTS_FILE_DESCRIPTOR) != 0);
98 
99     parcel.recycle();
100   }
101 
shouldBeTheSame(Union a, Union b)102   private void shouldBeTheSame(Union a, Union b) {
103     assertTrue(a.equals(b));
104     assertTrue(b.equals(a));
105     assertTrue(a.equals(a));
106     assertTrue(b.equals(b));
107     assertTrue(a.hashCode() == b.hashCode());
108   }
109 
shouldBeDifferent(Union a, Union b)110   private void shouldBeDifferent(Union a, Union b) {
111     assertFalse(a.equals(b));
112     assertFalse(b.equals(a));
113     assertFalse(a.hashCode() == b.hashCode());
114   }
115 
116   @Test
equalsAndHashCode()117   public void equalsAndHashCode() {
118     // same tag, same value
119     shouldBeTheSame(Union.s("hello"), Union.s("hello"));
120 
121     // different tag, same value
122     shouldBeDifferent(Union.m(10), Union.n(10));
123 
124     // same tag, different value
125     shouldBeDifferent(Union.s("hello"), Union.s("world"));
126 
127     // with array
128     shouldBeTheSame(Union.ns(new int[]{1, 2, 3}),Union.ns(new int[]{1, 2, 3}));
129     shouldBeDifferent(Union.ns(new int[]{1, 2, 3}), Union.ns(new int[]{1, 2, 4}));
130   }
131 }
132