Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2007 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.os;
     18 
     19 import android.os.IInterface;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 import com.google.android.collect.Lists;
     24 import junit.framework.TestCase;
     25 
     26 import java.util.List;
     27 
     28 public class AidlTest extends TestCase {
     29 
     30     private IAidlTest mRemote;
     31 
     32     @Override
     33     protected void setUp() throws Exception {
     34         super.setUp();
     35         AidlObject mLocal = new AidlObject();
     36         mRemote = IAidlTest.Stub.asInterface(mLocal);
     37     }
     38 
     39     private static boolean check(TestParcelable p, int n, String s) {
     40         return p.mAnInt == n &&
     41                 ((s == null && p.mAString == null) || s.equals(p.mAString));
     42     }
     43 
     44     public static class TestParcelable implements Parcelable {
     45         public int mAnInt;
     46         public String mAString;
     47 
     48         public TestParcelable() {
     49         }
     50 
     51         public TestParcelable(int i, String s) {
     52             mAnInt = i;
     53             mAString = s;
     54         }
     55 
     56         public int describeContents() {
     57             return 0;
     58         }
     59 
     60         public void writeToParcel(Parcel parcel, int flags) {
     61             parcel.writeInt(mAnInt);
     62             parcel.writeString(mAString);
     63         }
     64 
     65         public void readFromParcel(Parcel parcel) {
     66             mAnInt = parcel.readInt();
     67             mAString = parcel.readString();
     68         }
     69 
     70         public static final Parcelable.Creator<TestParcelable> CREATOR
     71                 = new Parcelable.Creator<TestParcelable>() {
     72             public TestParcelable createFromParcel(Parcel parcel) {
     73                 return new TestParcelable(parcel.readInt(),
     74                         parcel.readString());
     75             }
     76 
     77             public TestParcelable[] newArray(int size) {
     78                 return new TestParcelable[size];
     79             }
     80         };
     81 
     82         public String toString() {
     83             return super.toString() + " {" + mAnInt + "/" + mAString + "}";
     84         }
     85     }
     86 
     87     private static class AidlObject extends IAidlTest.Stub {
     88         public IInterface queryLocalInterface(String descriptor) {
     89             // overriding this to return null makes asInterface always
     90             // generate a proxy
     91             return null;
     92         }
     93 
     94         public int intMethod(int a) {
     95             return a;
     96         }
     97 
     98         public TestParcelable parcelableIn(TestParcelable p) {
     99             p.mAnInt++;
    100             return p;
    101         }
    102 
    103         public TestParcelable parcelableOut(TestParcelable p) {
    104             p.mAnInt = 44;
    105             return p;
    106         }
    107 
    108         public TestParcelable parcelableInOut(TestParcelable p) {
    109             p.mAnInt++;
    110             return p;
    111         }
    112 
    113         public TestParcelable listParcelableLonger(List<TestParcelable> list, int index) {
    114             list.add(list.get(index));
    115             return list.get(index);
    116         }
    117 
    118         public int listParcelableShorter(List<TestParcelable> list, int index) {
    119             list.remove(index);
    120             return list.size();
    121         }
    122 
    123         public boolean[] booleanArray(boolean[] a0, boolean[] a1, boolean[] a2) {
    124             for (int i = 0; i < a0.length && i < a2.length; i++) {
    125                 a2[i] = a0[i];
    126             }
    127             for (int i = 0; i < a0.length && i < a1.length; i++) {
    128                 a1[i] = a0[i];
    129             }
    130             return a0;
    131         }
    132 
    133         public char[] charArray(char[] a0, char[] a1, char[] a2) {
    134             for (int i = 0; i < a0.length && i < a2.length; i++) {
    135                 a2[i] = a0[i];
    136             }
    137             for (int i = 0; i < a0.length && i < a1.length; i++) {
    138                 a1[i] = a0[i];
    139             }
    140             return a0;
    141         }
    142 
    143         public int[] intArray(int[] a0, int[] a1, int[] a2) {
    144             for (int i = 0; i < a0.length && i < a2.length; i++) {
    145                 a2[i] = a0[i];
    146             }
    147             for (int i = 0; i < a0.length && i < a1.length; i++) {
    148                 a1[i] = a0[i];
    149             }
    150             return a0;
    151         }
    152 
    153         public long[] longArray(long[] a0, long[] a1, long[] a2) {
    154             for (int i = 0; i < a0.length && i < a2.length; i++) {
    155                 a2[i] = a0[i];
    156             }
    157             for (int i = 0; i < a0.length && i < a1.length; i++) {
    158                 a1[i] = a0[i];
    159             }
    160             return a0;
    161         }
    162 
    163         public float[] floatArray(float[] a0, float[] a1, float[] a2) {
    164             for (int i = 0; i < a0.length && i < a2.length; i++) {
    165                 a2[i] = a0[i];
    166             }
    167             for (int i = 0; i < a0.length && i < a1.length; i++) {
    168                 a1[i] = a0[i];
    169             }
    170             return a0;
    171         }
    172 
    173         public double[] doubleArray(double[] a0, double[] a1, double[] a2) {
    174             for (int i = 0; i < a0.length && i < a2.length; i++) {
    175                 a2[i] = a0[i];
    176             }
    177             for (int i = 0; i < a0.length && i < a1.length; i++) {
    178                 a1[i] = a0[i];
    179             }
    180             return a0;
    181         }
    182 
    183         public String[] stringArray(String[] a0, String[] a1, String[] a2) {
    184             for (int i = 0; i < a0.length && i < a2.length; i++) {
    185                 a2[i] = a0[i];
    186             }
    187             for (int i = 0; i < a0.length && i < a1.length; i++) {
    188                 a1[i] = a0[i];
    189             }
    190             return a0;
    191         }
    192 
    193         public TestParcelable[] parcelableArray(TestParcelable[] a0,
    194                 TestParcelable[] a1, TestParcelable[] a2) {
    195             return null;
    196         }
    197 
    198         public void voidSecurityException() {
    199             throw new SecurityException("gotcha!");
    200         }
    201 
    202         public int intSecurityException() {
    203             throw new SecurityException("gotcha!");
    204         }
    205     }
    206 
    207     @SmallTest
    208     public void testInt() throws Exception {
    209         int result = mRemote.intMethod(42);
    210         assertEquals(42, result);
    211     }
    212 
    213     @SmallTest
    214     public void testParcelableIn() throws Exception {
    215         TestParcelable arg = new TestParcelable(43, "hi");
    216         TestParcelable result = mRemote.parcelableIn(arg);
    217         assertNotSame(arg, result);
    218 
    219         assertEquals(43, arg.mAnInt);
    220         assertEquals(44, result.mAnInt);
    221     }
    222 
    223     @SmallTest
    224     public void testParcelableOut() throws Exception {
    225         TestParcelable arg = new TestParcelable(43, "hi");
    226         TestParcelable result = mRemote.parcelableOut(arg);
    227         assertNotSame(arg, result);
    228         assertEquals(44, arg.mAnInt);
    229     }
    230 
    231     @SmallTest
    232     public void testParcelableInOut() throws Exception {
    233         TestParcelable arg = new TestParcelable(43, "hi");
    234         TestParcelable result = mRemote.parcelableInOut(arg);
    235         assertNotSame(arg, result);
    236         assertEquals(44, arg.mAnInt);
    237     }
    238 
    239     @SmallTest
    240     public void testListParcelableLonger() throws Exception {
    241         List<TestParcelable> list = Lists.newArrayList();
    242         list.add(new TestParcelable(33, "asdf"));
    243         list.add(new TestParcelable(34, "jkl;"));
    244 
    245         TestParcelable result = mRemote.listParcelableLonger(list, 1);
    246 
    247 //        System.out.println("result=" + result);
    248 //        for (TestParcelable p : list) {
    249 //            System.out.println("longer: " + p);
    250 //        }
    251 
    252         assertEquals("jkl;", result.mAString);
    253         assertEquals(34, result.mAnInt);
    254 
    255         assertEquals(3, list.size());
    256         assertTrue("out parameter 0: " + list.get(0), check(list.get(0), 33, "asdf"));
    257         assertTrue("out parameter 1: " + list.get(1), check(list.get(1), 34, "jkl;"));
    258         assertTrue("out parameter 2: " + list.get(2), check(list.get(2), 34, "jkl;"));
    259 
    260         assertNotSame(list.get(1), list.get(2));
    261     }
    262 
    263     @SmallTest
    264     public void testListParcelableShorter() throws Exception {
    265         List<TestParcelable> list = Lists.newArrayList();
    266         list.add(new TestParcelable(33, "asdf"));
    267         list.add(new TestParcelable(34, "jkl;"));
    268         list.add(new TestParcelable(35, "qwerty"));
    269 
    270         int result = mRemote.listParcelableShorter(list, 2);
    271 
    272 //        System.out.println("result=" + result);
    273 //        for (TestParcelable p : list) {
    274 //            System.out.println("shorter: " + p);
    275 //        }
    276 
    277         assertEquals(2, result);
    278         assertEquals(2, list.size());
    279         assertTrue("out parameter 0: " + list.get(0), check(list.get(0), 33, "asdf"));
    280         assertTrue("out parameter 1: " + list.get(1), check(list.get(1), 34, "jkl;"));
    281 
    282         assertNotSame(list.get(0), list.get(1));
    283     }
    284 
    285     @SmallTest
    286     public void testArrays() throws Exception {
    287         // boolean
    288         boolean[] b0 = new boolean[]{true};
    289         boolean[] b1 = new boolean[]{false, true};
    290         boolean[] b2 = new boolean[]{true, false, true};
    291         boolean[] br = mRemote.booleanArray(b0, b1, b2);
    292 
    293         assertEquals(1, br.length);
    294         assertTrue(br[0]);
    295 
    296         assertTrue(b1[0]);
    297         assertFalse(b1[1]);
    298 
    299         assertTrue(b2[0]);
    300         assertFalse(b2[1]);
    301         assertTrue(b2[2]);
    302 
    303         // char
    304         char[] c0 = new char[]{'a'};
    305         char[] c1 = new char[]{'b', 'c'};
    306         char[] c2 = new char[]{'d', 'e', 'f'};
    307         char[] cr = mRemote.charArray(c0, c1, c2);
    308 
    309         assertEquals(1, cr.length);
    310         assertEquals('a', cr[0]);
    311 
    312         assertEquals('a', c1[0]);
    313         assertEquals('\0', c1[1]);
    314 
    315         assertEquals('a', c2[0]);
    316         assertEquals('e', c2[1]);
    317         assertEquals('f', c2[2]);
    318 
    319         // int
    320         int[] i0 = new int[]{34};
    321         int[] i1 = new int[]{38, 39};
    322         int[] i2 = new int[]{42, 43, 44};
    323         int[] ir = mRemote.intArray(i0, i1, i2);
    324 
    325         assertEquals(1, ir.length);
    326         assertEquals(34, ir[0]);
    327 
    328         assertEquals(34, i1[0]);
    329         assertEquals(0, i1[1]);
    330 
    331         assertEquals(34, i2[0]);
    332         assertEquals(43, i2[1]);
    333         assertEquals(44, i2[2]);
    334 
    335         // long
    336         long[] l0 = new long[]{50};
    337         long[] l1 = new long[]{51, 52};
    338         long[] l2 = new long[]{53, 54, 55};
    339         long[] lr = mRemote.longArray(l0, l1, l2);
    340 
    341         assertEquals(1, lr.length);
    342         assertEquals(50, lr[0]);
    343 
    344         assertEquals(50, l1[0]);
    345         assertEquals(0, l1[1]);
    346 
    347         assertEquals(50, l2[0]);
    348         assertEquals(54, l2[1]);
    349         assertEquals(55, l2[2]);
    350 
    351         // float
    352         float[] f0 = new float[]{90.1f};
    353         float[] f1 = new float[]{90.2f, 90.3f};
    354         float[] f2 = new float[]{90.4f, 90.5f, 90.6f};
    355         float[] fr = mRemote.floatArray(f0, f1, f2);
    356 
    357         assertEquals(1, fr.length);
    358         assertEquals(90.1f, fr[0]);
    359 
    360         assertEquals(90.1f, f1[0]);
    361         assertEquals(0f, f1[1], 0.0f);
    362 
    363         assertEquals(90.1f, f2[0]);
    364         assertEquals(90.5f, f2[1]);
    365         assertEquals(90.6f, f2[2]);
    366 
    367         // double
    368         double[] d0 = new double[]{100.1};
    369         double[] d1 = new double[]{100.2, 100.3};
    370         double[] d2 = new double[]{100.4, 100.5, 100.6};
    371         double[] dr = mRemote.doubleArray(d0, d1, d2);
    372 
    373         assertEquals(1, dr.length);
    374         assertEquals(100.1, dr[0]);
    375 
    376         assertEquals(100.1, d1[0]);
    377         assertEquals(0, d1[1], 0.0);
    378 
    379         assertEquals(100.1, d2[0]);
    380         assertEquals(100.5, d2[1]);
    381         assertEquals(100.6, d2[2]);
    382 
    383         // String
    384         String[] s0 = new String[]{"s0[0]"};
    385         String[] s1 = new String[]{"s1[0]", "s1[1]"};
    386         String[] s2 = new String[]{"s2[0]", "s2[1]", "s2[2]"};
    387         String[] sr = mRemote.stringArray(s0, s1, s2);
    388 
    389         assertEquals(1, sr.length);
    390         assertEquals("s0[0]", sr[0]);
    391 
    392         assertEquals("s0[0]", s1[0]);
    393         assertNull(s1[1]);
    394 
    395         assertEquals("s0[0]", s2[0]);
    396         assertEquals("s2[1]", s2[1]);
    397         assertEquals("s2[2]", s2[2]);
    398     }
    399 
    400     @SmallTest
    401     public void testVoidSecurityException() throws Exception {
    402         boolean good = false;
    403         try {
    404             mRemote.voidSecurityException();
    405         } catch (SecurityException e) {
    406             good = true;
    407         }
    408         assertEquals(good, true);
    409     }
    410 
    411     @SmallTest
    412     public void testIntSecurityException() throws Exception {
    413         boolean good = false;
    414         try {
    415             mRemote.intSecurityException();
    416         } catch (SecurityException e) {
    417             good = true;
    418         }
    419         assertEquals(good, true);
    420     }
    421 }
    422 
    423