Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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.content.pm.cts;
     18 
     19 import android.content.pm.ApplicationInfo;
     20 import android.content.pm.ComponentInfo;
     21 import android.content.pm.PackageManager;
     22 import android.content.pm.PackageManager.NameNotFoundException;
     23 import android.graphics.Bitmap;
     24 import android.graphics.Canvas;
     25 import android.graphics.drawable.Drawable;
     26 import android.os.Parcel;
     27 import android.test.AndroidTestCase;
     28 import android.util.Printer;
     29 import android.util.StringBuilderPrinter;
     30 
     31 import com.android.compatibility.common.util.WidgetTestUtils;
     32 
     33 import android.content.cts.R;
     34 
     35 
     36 /**
     37  * Test {@link ComponentInfo}.
     38  */
     39 public class ComponentInfoTest extends AndroidTestCase {
     40     private final String PACKAGE_NAME = "android.content.cts";
     41     private ComponentInfo mComponentInfo;
     42 
     43     public void testConstructor() {
     44         Parcel p = Parcel.obtain();
     45         ComponentInfo componentInfo = new ComponentInfo();
     46         componentInfo.applicationInfo = new ApplicationInfo();
     47         componentInfo.writeToParcel(p, 0);
     48         p.setDataPosition(0);
     49 
     50         new MyComponentInfo(p);
     51 
     52         new ComponentInfo();
     53 
     54         new ComponentInfo(componentInfo);
     55 
     56         try {
     57             new ComponentInfo((ComponentInfo) null);
     58             fail("should throw NullPointerException.");
     59         } catch (NullPointerException e) {
     60             // expected
     61         }
     62 
     63         try {
     64             new MyComponentInfo((Parcel) null);
     65             fail("should throw NullPointerException.");
     66         } catch (NullPointerException e) {
     67             // expected
     68         }
     69     }
     70 
     71     private Bitmap createIconBitmap(Drawable d) {
     72         int size = Math.round(100 * getContext().getResources().getDisplayMetrics().density) + 100;
     73         Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
     74         d.setBounds(0, 0, size, size);
     75         d.draw(new Canvas(bitmap));
     76         return bitmap;
     77     }
     78 
     79     public void testLoadIcon() {
     80         mComponentInfo = new ComponentInfo();
     81         mComponentInfo.applicationInfo = new ApplicationInfo();
     82 
     83         PackageManager pm = mContext.getPackageManager();
     84         assertNotNull(pm);
     85 
     86         Drawable defaultIcon = pm.getDefaultActivityIcon();
     87         Drawable d = null;
     88         Drawable d2 = null;
     89         d = mComponentInfo.loadIcon(pm);
     90         assertNotNull(d);
     91         assertNotSame(d, defaultIcon);
     92         WidgetTestUtils.assertEquals(createIconBitmap(d), createIconBitmap(defaultIcon));
     93 
     94         d2 = mComponentInfo.loadIcon(pm);
     95         assertNotNull(d2);
     96         assertNotSame(d, d2);
     97         WidgetTestUtils.assertEquals(createIconBitmap(d), createIconBitmap(d2));
     98 
     99         try {
    100             mComponentInfo.loadIcon(null);
    101             fail("ComponentInfo#loadIcon() throw NullPointerException");
    102         } catch (NullPointerException e) {
    103             // expected
    104         }
    105     }
    106 
    107     public void testDumpBack() {
    108         MyComponentInfo ci = new MyComponentInfo();
    109 
    110         StringBuilder sb = new StringBuilder();
    111         assertEquals(0, sb.length());
    112         StringBuilderPrinter p = new StringBuilderPrinter(sb);
    113         String prefix = "";
    114         ci.dumpBack(p, prefix);
    115 
    116         assertNotNull(sb.toString());
    117         assertTrue(sb.length() > 0);
    118 
    119         ci.applicationInfo = new ApplicationInfo();
    120         sb = new StringBuilder();
    121         assertEquals(0, sb.length());
    122         p = new StringBuilderPrinter(sb);
    123 
    124         ci.dumpBack(p, prefix);
    125         assertNotNull(sb.toString());
    126         assertTrue(sb.length() > 0);
    127 
    128         try {
    129             ci.dumpBack(null, null);
    130             fail("ComponentInfo#dumpBack() throw NullPointerException here.");
    131         } catch (NullPointerException e) {
    132             // expected
    133         }
    134     }
    135 
    136     public void testGetIconResource() {
    137         mComponentInfo = new ComponentInfo();
    138         mComponentInfo.applicationInfo = new ApplicationInfo();
    139         assertEquals(0, mComponentInfo.getIconResource());
    140 
    141         mComponentInfo.icon = R.drawable.red;
    142         assertEquals(mComponentInfo.icon, mComponentInfo.getIconResource());
    143 
    144         mComponentInfo.icon = 0;
    145         assertEquals(mComponentInfo.applicationInfo.icon, mComponentInfo.getIconResource());
    146     }
    147 
    148     public void testIsEnabled() {
    149         mComponentInfo = new ComponentInfo();
    150         mComponentInfo.applicationInfo = new ApplicationInfo();
    151         assertTrue(mComponentInfo.isEnabled());
    152 
    153         mComponentInfo.enabled = false;
    154         assertFalse(mComponentInfo.isEnabled());
    155 
    156         mComponentInfo.enabled = true;
    157         mComponentInfo.applicationInfo.enabled = false;
    158         assertFalse(mComponentInfo.isEnabled());
    159     }
    160 
    161     public void testDumpFront() {
    162         MyComponentInfo ci = new MyComponentInfo();
    163 
    164         StringBuilder sb = new StringBuilder();
    165         assertEquals(0, sb.length());
    166         StringBuilderPrinter p = new StringBuilderPrinter(sb);
    167 
    168         String prefix = "";
    169         ci.dumpFront(p, prefix);
    170         assertNotNull(sb.toString());
    171         assertTrue(sb.length() > 0);
    172 
    173         ci.applicationInfo = new ApplicationInfo();
    174 
    175         sb = new StringBuilder();
    176         p = new StringBuilderPrinter(sb);
    177         assertEquals(0, sb.length());
    178 
    179         ci.dumpFront(p, prefix);
    180         assertNotNull(sb.toString());
    181         assertTrue(sb.length() > 0);
    182 
    183         try {
    184             ci.dumpFront(null, null);
    185             fail("ComponentInfo#dumpFront() throw NullPointerException here.");
    186         } catch (NullPointerException e) {
    187             // expected
    188         }
    189     }
    190 
    191     public void testLoadLabel() throws NameNotFoundException {
    192         mComponentInfo = new ComponentInfo();
    193         mComponentInfo.applicationInfo = new ApplicationInfo();
    194 
    195         final PackageManager pm = mContext.getPackageManager();
    196 
    197         assertNotNull(mComponentInfo);
    198         mComponentInfo.packageName = PACKAGE_NAME;
    199         mComponentInfo.nonLocalizedLabel = "nonLocalizedLabel";
    200         assertEquals("nonLocalizedLabel", mComponentInfo.loadLabel(pm));
    201 
    202         mComponentInfo.nonLocalizedLabel = null;
    203         mComponentInfo.labelRes = 0;
    204         mComponentInfo.name = "name";
    205         assertEquals("name", mComponentInfo.loadLabel(pm));
    206 
    207         mComponentInfo.applicationInfo =
    208                 mContext.getPackageManager().getApplicationInfo(PACKAGE_NAME, 0);
    209 
    210         mComponentInfo.nonLocalizedLabel = null;
    211         mComponentInfo.labelRes = R.string.hello_android;
    212         assertEquals(mContext.getString(mComponentInfo.labelRes), mComponentInfo.loadLabel(pm));
    213 
    214         try {
    215             mComponentInfo.loadLabel(null);
    216             fail("ComponentInfo#loadLabel throw NullPointerException");
    217         } catch (NullPointerException e) {
    218             // expected
    219         }
    220     }
    221 
    222     public void testWriteToParcel() {
    223         Parcel p = Parcel.obtain();
    224         mComponentInfo = new ComponentInfo();
    225         mComponentInfo.applicationInfo = new ApplicationInfo();
    226         mComponentInfo.writeToParcel(p, 0);
    227         p.setDataPosition(0);
    228 
    229         MyComponentInfo ci = new MyComponentInfo(p);
    230         assertEquals(mComponentInfo.processName, ci.processName);
    231         assertEquals(mComponentInfo.enabled, ci.enabled);
    232         assertEquals(mComponentInfo.exported, ci.exported);
    233 
    234         StringBuilder sb1 = new StringBuilder();
    235         StringBuilderPrinter p1 = new StringBuilderPrinter(sb1);
    236         StringBuilder sb2 = new StringBuilder();
    237         StringBuilderPrinter p2 = new StringBuilderPrinter(sb2);
    238         mComponentInfo.applicationInfo.dump(p1, "");
    239         ci.applicationInfo.dump(p2, "");
    240         assertEquals(sb1.toString(), sb2.toString());
    241 
    242         try {
    243             mComponentInfo.writeToParcel(null, 0);
    244             fail("ComponentInfo#writeToParcel() throw NullPointerException");
    245         } catch (NullPointerException e) {
    246             // expected
    247         }
    248     }
    249 
    250     private static class MyComponentInfo extends ComponentInfo {
    251         public MyComponentInfo() {
    252             super();
    253         }
    254 
    255         public MyComponentInfo(ComponentInfo orig) {
    256             super(orig);
    257         }
    258         public MyComponentInfo(Parcel source) {
    259             super(source);
    260         }
    261 
    262         public void dumpBack(Printer pw, String prefix) {
    263             super.dumpBack(pw, prefix);
    264         }
    265 
    266         public void dumpFront(Printer pw, String prefix) {
    267             super.dumpFront(pw, prefix);
    268         }
    269     }
    270 }
    271