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