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.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertNull; 23 import static org.junit.Assert.assertTrue; 24 import static org.junit.Assert.fail; 25 26 import android.content.ComponentName; 27 import android.content.Context; 28 import android.os.Parcel; 29 import android.os.Process; 30 import android.platform.test.annotations.AppModeSdkSandbox; 31 import android.platform.test.ravenwood.RavenwoodRule; 32 import android.test.mock.MockContext; 33 34 import androidx.test.InstrumentationRegistry; 35 import androidx.test.runner.AndroidJUnit4; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 /** 42 * Test {@link ComponentName}. 43 */ 44 @RunWith(AndroidJUnit4.class) 45 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 46 public class ComponentNameTest { 47 private Context mContext; 48 49 @Before setUp()50 public void setUp() { 51 if (RavenwoodRule.isOnRavenwood() || Process.isSdkSandbox()) { 52 // TODO: replace with mockito when better supported 53 mContext = new MockContext() { 54 @Override 55 public String getPackageName() { 56 return "android.content.cts"; 57 } 58 }; 59 } else { 60 mContext = InstrumentationRegistry.getTargetContext(); 61 } 62 } 63 64 @Test testConstructor()65 public void testConstructor() { 66 // new the ComponentName instance 67 new ComponentName("com.android.app", "com.android.app.InstrumentationTestActivity"); 68 69 // Test null string 70 try { 71 new ComponentName((String) null, (String) null); 72 fail("ComponentName's constructor (String, Stirng) can not accept null input values."); 73 } catch (NullPointerException e) { 74 // expected 75 } 76 77 // new the ComponentName instance: test real Context , real class name string 78 new ComponentName(mContext, "ActivityTestCase"); 79 80 // Test null Context, real class name string input, return should be null 81 try { 82 new ComponentName((Context) null, "ActivityTestCase"); 83 fail("class name is null, the constructor should throw a exception"); 84 } catch (NullPointerException e) { 85 // expected 86 } 87 88 // Test real Context, null name string input, return should not be null 89 try { 90 new ComponentName(mContext, (String) null); 91 fail("Constructor should not accept null class name."); 92 } catch (NullPointerException e) { 93 // expected 94 } 95 96 // new the ComponentName instance: real Context, real class input, return shouldn't be null 97 new ComponentName(mContext, this.getClass()); 98 99 // new the ComponentName instance: real Context, null class input, return shouldn't be null 100 try { 101 new ComponentName(mContext, (Class<?>) null); 102 fail("If class name is null, contructor should throw a exception"); 103 } catch (NullPointerException e) { 104 // expected 105 } 106 107 // new the ComponentName instance, Test null Parcel 108 try { 109 new ComponentName((Parcel) null); 110 fail("Constructor should not accept null Parcel input."); 111 } catch (NullPointerException e) { 112 // expected 113 } 114 115 // new the ComponentName instance, Test null Parcel 116 final Parcel parcel = Parcel.obtain(); 117 118 final ComponentName componentName = getComponentName(); 119 componentName.writeToParcel(parcel, 0); 120 parcel.setDataPosition(0); 121 new ComponentName(parcel); 122 } 123 124 @Test testFlattenToString()125 public void testFlattenToString() { 126 assertEquals("android.content.cts/android.content.cts.ComponentNameTest", 127 getComponentName().flattenToString()); 128 } 129 130 @Test testGetShortClassName()131 public void testGetShortClassName() { 132 // set the expected value, test normal value 133 String actual = getComponentName().getShortClassName(); 134 assertEquals(".ComponentNameTest", actual); 135 136 // Test class name which can be abbreviated 137 ComponentName componentName = new ComponentName("com.android.view", 138 "com.android.view.View"); 139 final String className = componentName.getClassName(); 140 // First, check the string return by getClassName(). 141 assertEquals("com.android.view.View", className); 142 actual = componentName.getShortClassName(); 143 // Then, check the string return by getShortClassName(). 144 assertEquals(".View", actual); 145 } 146 147 @Test testReadFromParcel()148 public void testReadFromParcel() { 149 ComponentName expected = getComponentName(); 150 final Parcel parcel1 = Parcel.obtain(); 151 expected.writeToParcel(parcel1, 0); 152 parcel1.setDataPosition(0); 153 ComponentName actual = ComponentName.readFromParcel(parcel1); 154 assertEquals(expected, actual); 155 156 // Test empty data 157 final Parcel parcel2 = Parcel.obtain(); 158 expected = ComponentName.readFromParcel(parcel2); 159 assertNull(expected); 160 } 161 162 @Test testGetPackageName()163 public void testGetPackageName() { 164 final String actual = getComponentName().getPackageName(); 165 assertEquals("android.content.cts", actual); 166 } 167 168 @Test testUnflattenFromString()169 public void testUnflattenFromString() { 170 final ComponentName componentName = getComponentName(); 171 final String flattenString = getComponentName().flattenToString(); 172 assertNotNull(flattenString); 173 ComponentName actual = ComponentName.unflattenFromString(flattenString); 174 assertEquals(componentName, actual); 175 } 176 177 @Test testFlattenToShortString()178 public void testFlattenToShortString() { 179 // Test normal 180 String actual = getComponentName().flattenToShortString(); 181 assertEquals("android.content.cts/.ComponentNameTest", actual); 182 183 // Test long class name 184 final ComponentName componentName = new ComponentName("com.android.view", 185 "com.android.view.View"); 186 final String falttenString = componentName.flattenToString(); 187 // First, compare the string return by flattenToString(). 188 assertEquals("com.android.view/com.android.view.View", falttenString); 189 actual = componentName.flattenToShortString(); 190 // Then, compare the string return by flattenToShortString(). 191 assertEquals("com.android.view/.View", actual); 192 } 193 194 @Test testEquals()195 public void testEquals() { 196 // new the ComponentName instances, both are the same. 197 final ComponentName componentName1 = getComponentName(); 198 ComponentName componentName2 = new ComponentName(componentName1.getPackageName(), 199 componentName1.getClassName()); 200 assertTrue(componentName1.equals(componentName2)); 201 202 // new the ComponentName instances, are not the same. 203 componentName2 = new ComponentName(componentName1.getPackageName(), 204 componentName1.getClassName() + "different name"); 205 assertFalse(componentName1.equals(componentName2)); 206 207 assertFalse(componentName1.equals(null)); 208 } 209 210 @Test testToString()211 public void testToString() { 212 assertNotNull(getComponentName().toString()); 213 } 214 215 @Test testToShortString()216 public void testToShortString() { 217 // Test normal string 218 final String shortString = getComponentName().toShortString(); 219 assertEquals("{android.content.cts/android.content.cts.ComponentNameTest}", shortString); 220 } 221 222 @Test testGetClassName()223 public void testGetClassName() { 224 // set the expected value 225 final String className = getComponentName().getClassName(); 226 assertEquals("android.content.cts.ComponentNameTest", className); 227 } 228 229 @Test 230 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") testHashCode()231 public void testHashCode() { 232 final ComponentName componentName = getComponentName(); 233 234 final int hashCode1 = componentName.hashCode(); 235 assertFalse(0 == hashCode1); 236 237 final ComponentName componentName2 = new ComponentName(componentName.getPackageName(), 238 componentName.getClassName()); 239 final int hashCode2 = componentName2.hashCode(); 240 assertEquals(hashCode1, hashCode2); 241 } 242 243 @Test testWriteToParcel()244 public void testWriteToParcel() { 245 // Test normal status 246 final ComponentName componentName = getComponentName(); 247 Parcel parcel = Parcel.obtain(); 248 ComponentName.writeToParcel(componentName, parcel); 249 parcel.setDataPosition(0); 250 assertFalse(0 == parcel.dataAvail()); 251 assertEquals("android.content.cts", parcel.readString()); 252 assertEquals("android.content.cts.ComponentNameTest", parcel.readString()); 253 } 254 255 @Test testWriteNullDataToParcel()256 public void testWriteNullDataToParcel() { 257 // Test null data 258 Parcel parcel = Parcel.obtain(); 259 ComponentName.writeToParcel(null, parcel); 260 assertEquals(0, parcel.dataAvail()); 261 } 262 263 @Test testDescribeContents()264 public void testDescribeContents() { 265 assertEquals(0, getComponentName().describeContents()); 266 } 267 getComponentName()268 private ComponentName getComponentName() { 269 final ComponentName componentName = new ComponentName(mContext, this.getClass()); 270 return componentName; 271 } 272 } 273