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 static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assert.fail; 23 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.content.pm.ActivityInfo; 27 import android.content.pm.PackageManager; 28 import android.content.pm.PackageManager.NameNotFoundException; 29 import android.os.Parcel; 30 import android.platform.test.annotations.AppModeSdkSandbox; 31 import android.platform.test.annotations.DisabledOnRavenwood; 32 import android.platform.test.ravenwood.RavenwoodRule; 33 import android.util.StringBuilderPrinter; 34 35 import androidx.test.platform.app.InstrumentationRegistry; 36 import androidx.test.runner.AndroidJUnit4; 37 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 /** 43 * Test {@link ActivityInfo}. 44 */ 45 @RunWith(AndroidJUnit4.class) 46 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 47 public class ActivityInfoTest { 48 @Rule 49 public final RavenwoodRule mRavenwood = new RavenwoodRule(); 50 51 private static final String TEST_PKG = "android.content.cts"; 52 private static final String TEST_ACTIVITY = TEST_PKG + ".MockActivity"; 53 54 ActivityInfo mActivityInfo; 55 getContext()56 private Context getContext() { 57 return InstrumentationRegistry.getInstrumentation().getTargetContext(); 58 } 59 60 @Test testSimple()61 public void testSimple() { 62 ActivityInfo info = new ActivityInfo(); 63 new ActivityInfo(info); 64 assertNotNull(info.toString()); 65 info.dump(new StringBuilderPrinter(new StringBuilder()), ""); 66 } 67 68 @Test testConstructor()69 public void testConstructor() { 70 new ActivityInfo(); 71 72 ActivityInfo info = new ActivityInfo(); 73 new ActivityInfo(info); 74 75 try { 76 new ActivityInfo(null); 77 fail("should throw NullPointerException."); 78 } catch (NullPointerException e) { 79 // expected 80 } 81 } 82 83 @Test 84 @DisabledOnRavenwood(blockedBy = PackageManager.class) testWriteToParcel()85 public void testWriteToParcel() throws NameNotFoundException { 86 ComponentName componentName = new ComponentName(TEST_PKG, TEST_ACTIVITY); 87 88 mActivityInfo = getContext().getPackageManager().getActivityInfo( 89 componentName, PackageManager.GET_META_DATA); 90 91 Parcel p = Parcel.obtain(); 92 mActivityInfo.writeToParcel(p, 0); 93 p.setDataPosition(0); 94 ActivityInfo info = ActivityInfo.CREATOR.createFromParcel(p); 95 assertEquals(mActivityInfo.theme, info.theme); 96 assertEquals(mActivityInfo.launchMode, info.launchMode); 97 assertEquals(mActivityInfo.permission, info.permission); 98 assertEquals(mActivityInfo.taskAffinity, info.taskAffinity); 99 assertEquals(mActivityInfo.targetActivity, info.targetActivity); 100 assertEquals(mActivityInfo.flags, info.flags); 101 assertEquals(mActivityInfo.screenOrientation, info.screenOrientation); 102 assertEquals(mActivityInfo.configChanges, info.configChanges); 103 104 try { 105 mActivityInfo.writeToParcel(null, 0); 106 fail("should throw NullPointerException"); 107 } catch (NullPointerException e) { 108 // expected 109 } 110 } 111 112 @Test 113 @DisabledOnRavenwood(blockedBy = PackageManager.class) testGetThemeResource()114 public void testGetThemeResource() throws NameNotFoundException { 115 ComponentName componentName = new ComponentName(TEST_PKG, TEST_ACTIVITY); 116 117 mActivityInfo = getContext().getPackageManager().getActivityInfo( 118 componentName, PackageManager.GET_META_DATA); 119 120 assertEquals(mActivityInfo.applicationInfo.theme, mActivityInfo.getThemeResource()); 121 mActivityInfo.theme = 1; 122 assertEquals(mActivityInfo.theme, mActivityInfo.getThemeResource()); 123 } 124 125 @Test testToString()126 public void testToString() throws NameNotFoundException { 127 mActivityInfo = new ActivityInfo(); 128 assertNotNull(mActivityInfo.toString()); 129 } 130 131 @Test 132 @DisabledOnRavenwood(blockedBy = PackageManager.class) testDescribeContents()133 public void testDescribeContents() throws NameNotFoundException { 134 mActivityInfo = new ActivityInfo(); 135 assertEquals(0, mActivityInfo.describeContents()); 136 137 ComponentName componentName = new ComponentName(TEST_PKG, TEST_ACTIVITY); 138 139 mActivityInfo = getContext().getPackageManager().getActivityInfo( 140 componentName, PackageManager.GET_META_DATA); 141 142 assertEquals(0, mActivityInfo.describeContents()); 143 } 144 145 @Test testDump()146 public void testDump() { 147 mActivityInfo = new ActivityInfo(); 148 149 StringBuilder sb = new StringBuilder(); 150 assertEquals(0, sb.length()); 151 StringBuilderPrinter p = new StringBuilderPrinter(sb); 152 153 String prefix = ""; 154 mActivityInfo.dump(p, prefix); 155 156 assertNotNull(sb.toString()); 157 assertTrue(sb.length() > 0); 158 159 try { 160 mActivityInfo.dump(null, ""); 161 fail("should throw NullPointerException."); 162 } catch (NullPointerException e) { 163 // expected 164 } 165 } 166 } 167