• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019, 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 
22 import android.aidl.tests.map.Bar;
23 import android.aidl.tests.map.Foo;
24 import android.aidl.tests.map.IEmpty;
25 import android.aidl.tests.map.IntEnum;
26 import android.os.Parcel;
27 import java.util.HashMap;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.JUnit4;
31 
32 @RunWith(JUnit4.class)
33 public class MapTests {
34 
35     @Test
testWriteAndThenReadMaps()36     public void testWriteAndThenReadMaps() {
37         Parcel parcel = Parcel.obtain();
38 
39         // Write
40         Foo foo = new Foo();
41 
42         foo.intEnumArrayMap = new HashMap<>();
43         foo.intEnumArrayMap.put("Foo", new int[] {IntEnum.FOO});
44 
45         foo.intArrayMap = new HashMap<>();
46         foo.intArrayMap.put("Foo", new int[] {42});
47 
48         Bar bar = new Bar();
49         bar.a = 42;
50         bar.b = "Bar";
51         foo.barMap = new HashMap<>();
52         foo.barMap.put("Foo", bar);
53 
54         foo.barArrayMap = new HashMap<>();
55         foo.barArrayMap.put("Foo", new Bar[] {bar});
56 
57         foo.stringMap = new HashMap<>();
58         foo.stringMap.put("Foo", "Bar");
59 
60         foo.stringArrayMap = new HashMap<>();
61         foo.stringArrayMap.put("Foo", new String[] {"Bar"});
62 
63         IEmpty intf = new IEmpty.Stub() {};
64         foo.interfaceMap = new HashMap<>();
65         foo.interfaceMap.put("Foo", intf);
66 
67         foo.ibinderMap = new HashMap<>();
68         foo.ibinderMap.put("Foo", intf.asBinder());
69 
70         foo.writeToParcel(parcel, 0);
71 
72         // And then read
73         parcel.setDataPosition(0);
74 
75         Foo readFoo = new Foo();
76         readFoo.readFromParcel(parcel);
77 
78         assertThat(readFoo.intEnumArrayMap.size(), is(1));
79         assertThat(readFoo.intEnumArrayMap.get("Foo"), is(new int[] {IntEnum.FOO}));
80 
81         assertThat(readFoo.intArrayMap.size(), is(1));
82         assertThat(readFoo.intArrayMap.get("Foo"), is(new int[] {42}));
83 
84         assertThat(readFoo.barMap.containsKey("Foo"), is(true));
85         assertThat(readFoo.barMap.size(), is(1));
86 
87         Bar readBar = readFoo.barMap.get("Foo");
88         assertThat(readBar.a, is(42));
89         assertThat(readBar.b, is("Bar"));
90 
91         Bar[] bars = readFoo.barArrayMap.get("Foo");
92         assertThat(bars.length, is(1));
93         assertThat(bars[0].a, is(42));
94         assertThat(bars[0].b, is("Bar"));
95 
96         assertThat(readFoo.stringMap.size(), is(1));
97         assertThat(readFoo.stringMap.get("Foo"), is("Bar"));
98 
99         String[] strings = readFoo.stringArrayMap.get("Foo");
100         assertThat(strings.length, is(1));
101         assertThat(strings[0], is("Bar"));
102 
103         assertThat(readFoo.interfaceMap.size(), is(1));
104         assertThat(readFoo.interfaceMap.get("Foo"), is(intf));
105 
106         assertThat(readFoo.ibinderMap.size(), is(1));
107         assertThat(readFoo.ibinderMap.get("Foo"), is(intf.asBinder()));
108     }
109 }
110