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.drawable.BitmapDrawable; 24 import android.graphics.drawable.Drawable; 25 import android.os.Parcel; 26 import android.test.AndroidTestCase; 27 import android.util.Printer; 28 import android.util.StringBuilderPrinter; 29 import android.widget.cts.WidgetTestUtils; 30 31 import com.android.cts.stub.R; 32 33 import dalvik.annotation.TestTargets; 34 import dalvik.annotation.TestLevel; 35 import dalvik.annotation.TestTargetNew; 36 import dalvik.annotation.TestTargetClass; 37 import dalvik.annotation.ToBeFixed; 38 39 /** 40 * Test {@link ComponentInfo}. 41 */ 42 @TestTargetClass(ComponentInfo.class) 43 public class ComponentInfoTest extends AndroidTestCase { 44 private final String PACKAGE_NAME = "com.android.cts.stub"; 45 private ComponentInfo mComponentInfo; 46 47 @TestTargets({ 48 @TestTargetNew( 49 level = TestLevel.COMPLETE, 50 method = "ComponentInfo", 51 args = {} 52 ), 53 @TestTargetNew( 54 level = TestLevel.COMPLETE, 55 method = "ComponentInfo", 56 args = {android.content.pm.ComponentInfo.class} 57 ), 58 @TestTargetNew( 59 level = TestLevel.COMPLETE, 60 method = "ComponentInfo", 61 args = {android.os.Parcel.class} 62 ) 63 }) 64 @ToBeFixed(bug = "1695243", explanation = "ComponentInfo#ComponentInfo(ComponentInfo), " + 65 "ComponentInfo#ComponentInfo(Parcel), should check whether the input is null") 66 public void testConstructor() { 67 Parcel p = Parcel.obtain(); 68 ComponentInfo componentInfo = new ComponentInfo(); 69 componentInfo.applicationInfo = new ApplicationInfo(); 70 componentInfo.writeToParcel(p, 0); 71 p.setDataPosition(0); 72 73 new MyComponentInfo(p); 74 75 new ComponentInfo(); 76 77 new ComponentInfo(componentInfo); 78 79 try { 80 new ComponentInfo((ComponentInfo) null); 81 fail("should throw NullPointerException."); 82 } catch (NullPointerException e) { 83 // expected 84 } 85 86 try { 87 new MyComponentInfo((Parcel) null); 88 fail("should throw NullPointerException."); 89 } catch (NullPointerException e) { 90 // expected 91 } 92 } 93 94 @TestTargetNew( 95 level = TestLevel.COMPLETE, 96 method = "loadIcon", 97 args = {android.content.pm.PackageManager.class} 98 ) 99 @ToBeFixed(bug = "1695243", explanation = "NullPointerException is not expected.") 100 public void testLoadIcon() { 101 mComponentInfo = new ComponentInfo(); 102 mComponentInfo.applicationInfo = new ApplicationInfo(); 103 104 PackageManager pm = mContext.getPackageManager(); 105 assertNotNull(pm); 106 107 Drawable defaultIcon = pm.getDefaultActivityIcon(); 108 Drawable d = null; 109 Drawable d2 = null; 110 d = mComponentInfo.loadIcon(pm); 111 assertNotNull(d); 112 assertNotSame(d, defaultIcon); 113 WidgetTestUtils.assertEquals(((BitmapDrawable) d).getBitmap(), 114 ((BitmapDrawable) defaultIcon).getBitmap()); 115 116 d2 = mComponentInfo.loadIcon(pm); 117 assertNotNull(d2); 118 assertNotSame(d, d2); 119 WidgetTestUtils.assertEquals(((BitmapDrawable) d).getBitmap(), 120 ((BitmapDrawable) d2).getBitmap()); 121 122 try { 123 mComponentInfo.loadIcon(null); 124 fail("ComponentInfo#loadIcon() throw NullPointerException"); 125 } catch (NullPointerException e) { 126 // expected 127 } 128 } 129 130 @TestTargetNew( 131 level = TestLevel.COMPLETE, 132 method = "dumpBack", 133 args = {android.util.Printer.class, java.lang.String.class} 134 ) 135 @ToBeFixed(bug = "1695243", explanation = "NullPointerException is not expected.") 136 public void testDumpBack() { 137 MyComponentInfo ci = new MyComponentInfo(); 138 139 StringBuilder sb = new StringBuilder(); 140 assertEquals(0, sb.length()); 141 StringBuilderPrinter p = new StringBuilderPrinter(sb); 142 String prefix = ""; 143 ci.dumpBack(p, prefix); 144 145 assertNotNull(sb.toString()); 146 assertTrue(sb.length() > 0); 147 148 ci.applicationInfo = new ApplicationInfo(); 149 sb = new StringBuilder(); 150 assertEquals(0, sb.length()); 151 p = new StringBuilderPrinter(sb); 152 153 ci.dumpBack(p, prefix); 154 assertNotNull(sb.toString()); 155 assertTrue(sb.length() > 0); 156 157 try { 158 ci.dumpBack(null, null); 159 fail("ComponentInfo#dumpBack() throw NullPointerException here."); 160 } catch (NullPointerException e) { 161 // expected 162 } 163 } 164 165 @TestTargetNew( 166 level = TestLevel.COMPLETE, 167 method = "getIconResource", 168 args = {} 169 ) 170 public void testGetIconResource() { 171 mComponentInfo = new ComponentInfo(); 172 mComponentInfo.applicationInfo = new ApplicationInfo(); 173 assertEquals(0, mComponentInfo.getIconResource()); 174 175 mComponentInfo.icon = R.drawable.red; 176 assertEquals(mComponentInfo.icon, mComponentInfo.getIconResource()); 177 178 mComponentInfo.icon = 0; 179 assertEquals(mComponentInfo.applicationInfo.icon, mComponentInfo.getIconResource()); 180 } 181 182 @TestTargetNew( 183 level = TestLevel.COMPLETE, 184 method = "isEnabled", 185 args = {} 186 ) 187 public void testIsEnabled() { 188 mComponentInfo = new ComponentInfo(); 189 mComponentInfo.applicationInfo = new ApplicationInfo(); 190 assertTrue(mComponentInfo.isEnabled()); 191 192 mComponentInfo.enabled = false; 193 assertFalse(mComponentInfo.isEnabled()); 194 195 mComponentInfo.enabled = true; 196 mComponentInfo.applicationInfo.enabled = false; 197 assertFalse(mComponentInfo.isEnabled()); 198 } 199 200 @TestTargetNew( 201 level = TestLevel.COMPLETE, 202 method = "dumpFront", 203 args = {android.util.Printer.class, java.lang.String.class} 204 ) 205 @ToBeFixed(bug = "1695243", explanation = "NullPointerException is not expected.") 206 public void testDumpFront() { 207 MyComponentInfo ci = new MyComponentInfo(); 208 209 StringBuilder sb = new StringBuilder(); 210 assertEquals(0, sb.length()); 211 StringBuilderPrinter p = new StringBuilderPrinter(sb); 212 213 String prefix = ""; 214 ci.dumpFront(p, prefix); 215 assertNotNull(sb.toString()); 216 assertTrue(sb.length() > 0); 217 218 ci.applicationInfo = new ApplicationInfo(); 219 220 sb = new StringBuilder(); 221 p = new StringBuilderPrinter(sb); 222 assertEquals(0, sb.length()); 223 224 ci.dumpFront(p, prefix); 225 assertNotNull(sb.toString()); 226 assertTrue(sb.length() > 0); 227 228 try { 229 ci.dumpFront(null, null); 230 fail("ComponentInfo#dumpFront() throw NullPointerException here."); 231 } catch (NullPointerException e) { 232 // expected 233 } 234 } 235 236 @TestTargetNew( 237 level = TestLevel.COMPLETE, 238 method = "loadLabel", 239 args = {android.content.pm.PackageManager.class} 240 ) 241 @ToBeFixed(bug = "1695243", explanation = "NullPointerException is not expected.") 242 public void testLoadLabel() throws NameNotFoundException { 243 mComponentInfo = new ComponentInfo(); 244 mComponentInfo.applicationInfo = new ApplicationInfo(); 245 246 final PackageManager pm = mContext.getPackageManager(); 247 248 assertNotNull(mComponentInfo); 249 mComponentInfo.packageName = PACKAGE_NAME; 250 mComponentInfo.nonLocalizedLabel = "nonLocalizedLabel"; 251 assertEquals("nonLocalizedLabel", mComponentInfo.loadLabel(pm)); 252 253 mComponentInfo.nonLocalizedLabel = null; 254 mComponentInfo.labelRes = 0; 255 mComponentInfo.name = "name"; 256 assertEquals("name", mComponentInfo.loadLabel(pm)); 257 258 mComponentInfo.applicationInfo = 259 mContext.getPackageManager().getApplicationInfo(PACKAGE_NAME, 0); 260 261 mComponentInfo.nonLocalizedLabel = null; 262 mComponentInfo.labelRes = R.string.hello_android; 263 assertEquals(mContext.getString(mComponentInfo.labelRes), mComponentInfo.loadLabel(pm)); 264 265 try { 266 mComponentInfo.loadLabel(null); 267 fail("ComponentInfo#loadLabel throw NullPointerException"); 268 } catch (NullPointerException e) { 269 // expected 270 } 271 } 272 273 @TestTargetNew( 274 level = TestLevel.COMPLETE, 275 method = "writeToParcel", 276 args = {android.os.Parcel.class, int.class} 277 ) 278 @ToBeFixed(bug = "1695243", explanation = "NullPointerException is not expected.") 279 public void testWriteToParcel() { 280 Parcel p = Parcel.obtain(); 281 mComponentInfo = new ComponentInfo(); 282 mComponentInfo.applicationInfo = new ApplicationInfo(); 283 mComponentInfo.writeToParcel(p, 0); 284 p.setDataPosition(0); 285 286 MyComponentInfo ci = new MyComponentInfo(p); 287 assertEquals(mComponentInfo.processName, ci.processName); 288 assertEquals(mComponentInfo.enabled, ci.enabled); 289 assertEquals(mComponentInfo.exported, ci.exported); 290 291 StringBuilder sb1 = new StringBuilder(); 292 StringBuilderPrinter p1 = new StringBuilderPrinter(sb1); 293 StringBuilder sb2 = new StringBuilder(); 294 StringBuilderPrinter p2 = new StringBuilderPrinter(sb2); 295 mComponentInfo.applicationInfo.dump(p1, ""); 296 ci.applicationInfo.dump(p2, ""); 297 assertEquals(sb1.toString(), sb2.toString()); 298 299 try { 300 mComponentInfo.writeToParcel(null, 0); 301 fail("ComponentInfo#writeToParcel() throw NullPointerException"); 302 } catch (NullPointerException e) { 303 // expected 304 } 305 } 306 307 private static class MyComponentInfo extends ComponentInfo { 308 public MyComponentInfo() { 309 super(); 310 } 311 312 public MyComponentInfo(ComponentInfo orig) { 313 super(orig); 314 } 315 public MyComponentInfo(Parcel source) { 316 super(source); 317 } 318 319 public void dumpBack(Printer pw, String prefix) { 320 super.dumpBack(pw, prefix); 321 } 322 323 public void dumpFront(Printer pw, String prefix) { 324 super.dumpFront(pw, prefix); 325 } 326 } 327 } 328