• 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 android.aidl.tests.map.Bar;
20 import android.aidl.tests.map.Foo;
21 import android.aidl.tests.map.IEmpty;
22 import android.os.IBinder;
23 import android.os.Parcel;
24 import java.util.HashMap;
25 
26 class MapTests {
27   private TestLogger mLog;
28 
MapTests(TestLogger logger)29   public MapTests(TestLogger logger) { mLog = logger; }
30 
checkMap()31   public void checkMap() throws TestFailException {
32     mLog.log("Checking if data in a Map object is transferred well.");
33     Parcel parcel = Parcel.obtain();
34     IEmpty intf = new IEmpty.Stub() {};
35     {
36       Foo foo = new Foo();
37       Bar bar = new Bar();
38       bar.a = 42;
39       bar.b = "Bar";
40       foo.barMap = new HashMap<>();
41       foo.barMap.put("Foo", bar);
42 
43       foo.stringMap = new HashMap<>();
44       foo.stringMap.put("Foo", "Bar");
45 
46       foo.interfaceMap = new HashMap<>();
47       foo.interfaceMap.put("Foo", intf);
48 
49       foo.ibinderMap = new HashMap<>();
50       foo.ibinderMap.put("Foo", intf.asBinder());
51 
52       foo.writeToParcel(parcel, 0);
53     }
54     parcel.setDataPosition(0);
55     {
56       Foo foo = new Foo();
57       foo.readFromParcel(parcel);
58       if (!foo.barMap.containsKey("Foo")) {
59         mLog.logAndThrow("Map foo.a must have the element of which key is \"Foo\"");
60       }
61       if (foo.barMap.size() != 1) {
62         mLog.logAndThrow("The size of map a is expected to be 1.");
63       }
64       Bar bar = foo.barMap.get("Foo");
65       if (bar.a != 42 || !"Bar".equals(bar.b)) {
66         mLog.logAndThrow("The content of bar is expected to be {a: 42, b: \"Bar\"}.");
67       }
68 
69       if (foo.stringMap.size() != 1) {
70         mLog.logAndThrow("The size of map a is expected to be 1.");
71       }
72       String string = foo.stringMap.get("Foo");
73       if (!"Bar".equals(string)) {
74         mLog.logAndThrow("The content of string is expected to be \"Bar\".");
75       }
76 
77       if (foo.interfaceMap.size() != 1) {
78         mLog.logAndThrow("The size of map a is expected to be 1.");
79       }
80 
81       if (!intf.equals(foo.interfaceMap.get("Foo"))) {
82         mLog.logAndThrow("The content of service is expected to be same.");
83       }
84 
85       if (foo.ibinderMap.size() != 1) {
86         mLog.logAndThrow("The size of map a is expected to be 1.");
87       }
88       IBinder ibinder = foo.ibinderMap.get("Foo");
89       if (!intf.asBinder().equals(ibinder)) {
90         mLog.logAndThrow("The content of IBinder is expected to be same.");
91       }
92     }
93   }
94 
runTests()95   public void runTests() throws TestFailException { checkMap(); }
96 }
97