• 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.assertThat;
21 import static org.junit.Assert.fail;
22 
23 import android.aidl.tests.immutable.Bar;
24 import android.aidl.tests.immutable.Foo;
25 import android.aidl.tests.immutable.Union;
26 import android.os.Parcel;
27 import android.os.RemoteException;
28 import java.lang.UnsupportedOperationException;
29 import java.lang.reflect.Field;
30 import java.lang.reflect.Modifier;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
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 JavaOnlyImmutableAnnotationTests {
41   @Test
testReadWriteJavaOnlyImmutableParcelable()42   public void testReadWriteJavaOnlyImmutableParcelable() {
43     Parcel parcel = Parcel.obtain();
44     List<Bar> list = new ArrayList<Bar>();
45     list.add(new Bar("aa"));
46     Map<String, Bar> map = new HashMap<String, Bar>();
47     map.put("key", new Bar("value"));
48     Bar[] array = new Bar[3];
49     array[0] = new Bar("zero");
50     array[1] = new Bar("one");
51     array[2] = new Bar("two");
52 
53     Foo foo = new Foo(7, new Bar("my"), list, map, array, Union.num(42));
54     foo.writeToParcel(parcel, 0);
55     parcel.setDataPosition(0);
56     Foo foo2 = Foo.CREATOR.createFromParcel(parcel);
57 
58     assertThat(foo.a, is(foo2.a));
59     assertThat(foo.b.s, is(foo2.b.s));
60     assertThat(foo.c.size(), is(foo.c.size()));
61     assertThat(foo.c.get(0).s, is(foo2.c.get(0).s));
62     assertThat(foo.c.size(), is(foo.c.size()));
63     assertThat(foo.d.get("key").s, is(foo2.d.get("key").s));
64     assertThat(foo.e.length, is(foo.e.length));
65 
66     for (int i = 0; i < foo.e.length; i++) {
67       assertThat(foo.e[i].s, is(foo2.e[i].s));
68     }
69 
70     assertThat(foo.u.getNum(), is(foo2.u.getNum()));
71   }
72 
73   @Test
testEveryFieldIsFinal()74   public void testEveryFieldIsFinal() {
75     for (Field f : Foo.class.getDeclaredFields()) {
76       if (!Modifier.isFinal(f.getModifiers())) {
77         fail(f.getName() + " should be final.");
78       }
79     }
80   }
81 
82   @Test(expected = UnsupportedOperationException.class)
testListIsUnmodifiable()83   public void testListIsUnmodifiable() {
84     Foo foo = new Foo(7, new Bar("my"), new ArrayList<Bar>(), new HashMap<String, Bar>(),
85         new Bar[5], Union.num(42));
86     foo.c.add(new Bar("hi"));
87     // It is supposed to fail.
88     fail("A List in an immutable parcelable isn't modifiable.");
89   }
90 
91   @Test(expected = UnsupportedOperationException.class)
testMapIsUnmodifiable()92   public void testMapIsUnmodifiable() {
93     Foo foo = new Foo(7, new Bar("my"), new ArrayList<Bar>(), new HashMap<String, Bar>(),
94         new Bar[5], Union.num(42));
95     foo.d.put("key", new Bar("value"));
96     // It is supposed to fail.
97     fail("A Map in an immutable parcelable isn't modifiable.");
98   }
99 
100   @Test
testBuilderRespectDefaultValue()101   public void testBuilderRespectDefaultValue() {
102     assertThat(new Foo.Builder().build().a, is(10));
103     assertThat(new Bar.Builder().build().s, is("bar"));
104   }
105 
106   @Test
testBuilder()107   public void testBuilder() {
108     Bar b = new Bar("my");
109     List<Bar> list = new ArrayList<Bar>();
110     list.add(new Bar("aa"));
111     Map<String, Bar> map = new HashMap<String, Bar>();
112     map.put("key", new Bar("value"));
113     Bar[] array = new Bar[3];
114     array[0] = new Bar("zero");
115     array[1] = new Bar("one");
116     array[2] = new Bar("two");
117 
118     Foo foo = new Foo(7, b, list, map, array, Union.num(42));
119     Foo foo2 = new Foo.Builder()
120                    .setA(7)
121                    .setB(b)
122                    .setC(list)
123                    .setD(map)
124                    .setE(array)
125                    .setU(Union.num(42))
126                    .build();
127 
128     assertThat(foo.a, is(foo2.a));
129     assertThat(foo.b.s, is(foo2.b.s));
130     assertThat(foo.c.size(), is(foo.c.size()));
131     assertThat(foo.c.get(0).s, is(foo2.c.get(0).s));
132     assertThat(foo.c.size(), is(foo.c.size()));
133     assertThat(foo.d.get("key").s, is(foo2.d.get("key").s));
134     assertThat(foo.e.length, is(foo.e.length));
135 
136     for (int i = 0; i < foo.e.length; i++) {
137       assertThat(foo.e[i].s, is(foo2.e[i].s));
138     }
139 
140     assertThat(foo.u.getNum(), is(foo2.u.getNum()));
141   }
142 }
143