• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content.ComponentName;
20 import android.content.Context;
21 import android.os.Parcel;
22 import android.test.AndroidTestCase;
23 import dalvik.annotation.TestTargets;
24 import dalvik.annotation.TestLevel;
25 import dalvik.annotation.TestTargetNew;
26 import dalvik.annotation.TestTargetClass;
27 import dalvik.annotation.ToBeFixed;
28 
29 /**
30  * Test {@link ComponentName}.
31  */
32 @TestTargetClass(ComponentName.class)
33 public class ComponentNameTest extends AndroidTestCase {
34     @TestTargets({
35         @TestTargetNew(
36             level = TestLevel.COMPLETE,
37             method = "ComponentName",
38             args = {android.content.Context.class, java.lang.Class.class}
39         ),
40         @TestTargetNew(
41             level = TestLevel.COMPLETE,
42             method = "ComponentName",
43             args = {java.lang.String.class, java.lang.String.class}
44         ),
45         @TestTargetNew(
46             level = TestLevel.COMPLETE,
47             method = "ComponentName",
48             args = {android.content.Context.class, java.lang.String.class}
49         ),
50         @TestTargetNew(
51             level = TestLevel.COMPLETE,
52             method = "ComponentName",
53             args = {android.os.Parcel.class}
54         )
55     })
56     @ToBeFixed(bug = "1417734", explanation = "NullPointerException is not expected.")
testConstructor()57     public void testConstructor() {
58         // new the ComponentName instance
59         new ComponentName("com.android.app", "com.android.app.InstrumentationTestActivity");
60 
61         // Test null string
62         try {
63             new ComponentName((String) null, (String) null);
64             fail("ComponentName's constructor (String, Stirng) can not accept null input values.");
65         } catch (NullPointerException e) {
66             // expected
67         }
68 
69         // new the ComponentName instance: test real Context , real class name string
70         new ComponentName(mContext, "ActivityTestCase");
71 
72         // Test null Context, real class name string input, return should be null
73         try {
74             new ComponentName((Context) null, "ActivityTestCase");
75             fail("class name is null, the constructor should throw a exception");
76         } catch (NullPointerException e) {
77             // expected
78         }
79 
80         // Test real Context, null name string input, return should not be null
81         try {
82             new ComponentName(mContext, (String) null);
83             fail("Constructor should not accept null class name.");
84         } catch (NullPointerException e) {
85             // expected
86         }
87 
88         // new the ComponentName instance: real Context, real class input, return shouldn't be null
89         new ComponentName(mContext, this.getClass());
90 
91         // new the ComponentName instance: real Context, null class input, return shouldn't be null
92         try {
93             new ComponentName(mContext, (Class<?>) null);
94             fail("If class name is null, contructor should throw a exception");
95         } catch (NullPointerException e) {
96             // expected
97         }
98 
99         // new the ComponentName instance, Test null Parcel
100         try {
101             new ComponentName((Parcel) null);
102             fail("Constructor should not accept null Parcel input.");
103         } catch (NullPointerException e) {
104             // expected
105         }
106 
107         // new the ComponentName instance, Test null Parcel
108         final Parcel parcel = Parcel.obtain();
109 
110         final ComponentName componentName = getComponentName();
111         componentName.writeToParcel(parcel, 0);
112         parcel.setDataPosition(0);
113         new ComponentName(parcel);
114     }
115 
116     @TestTargetNew(
117         level = TestLevel.COMPLETE,
118         method = "flattenToString",
119         args = {}
120     )
testFlattenToString()121     public void testFlattenToString() {
122         assertEquals("com.android.cts.stub/android.content.cts.ComponentNameTest",
123                 getComponentName().flattenToString());
124     }
125 
126     @TestTargetNew(
127         level = TestLevel.COMPLETE,
128         method = "getShortClassName",
129         args = {}
130     )
testGetShortClassName()131     public void testGetShortClassName() {
132         // set the expected value, test normal value
133         String actual = getComponentName().getShortClassName();
134         assertEquals("android.content.cts.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     @TestTargets({
148         @TestTargetNew(
149             level = TestLevel.COMPLETE,
150             method = "readFromParcel",
151             args = {android.os.Parcel.class}
152         ),
153         @TestTargetNew(
154             level = TestLevel.COMPLETE,
155             method = "writeToParcel",
156             args = {android.os.Parcel.class, int.class}
157         )
158     })
testReadFromParcel()159     public void testReadFromParcel() {
160         ComponentName expected = getComponentName();
161         final Parcel parcel1 = Parcel.obtain();
162         expected.writeToParcel(parcel1, 0);
163         parcel1.setDataPosition(0);
164         ComponentName actual = ComponentName.readFromParcel(parcel1);
165         assertEquals(expected, actual);
166 
167         // Test empty data
168         final Parcel parcel2 = Parcel.obtain();
169         expected = ComponentName.readFromParcel(parcel2);
170         assertNull(expected);
171     }
172 
173     @TestTargetNew(
174         level = TestLevel.COMPLETE,
175         method = "getPackageName",
176         args = {}
177     )
testGetPackageName()178     public void testGetPackageName() {
179         final String actual = getComponentName().getPackageName();
180         assertEquals("com.android.cts.stub", actual);
181     }
182 
183     @TestTargetNew(
184         level = TestLevel.COMPLETE,
185         method = "unflattenFromString",
186         args = {java.lang.String.class}
187     )
testUnflattenFromString()188     public void testUnflattenFromString() {
189         final ComponentName componentName = getComponentName();
190         final String flattenString = getComponentName().flattenToString();
191         assertNotNull(flattenString);
192         ComponentName actual = ComponentName.unflattenFromString(flattenString);
193         assertEquals(componentName, actual);
194     }
195 
196     @TestTargetNew(
197         level = TestLevel.COMPLETE,
198         method = "flattenToShortString",
199         args = {}
200     )
testFlattenToShortString()201     public void testFlattenToShortString() {
202         // Test normal
203         String actual = getComponentName().flattenToShortString();
204         assertEquals("com.android.cts.stub/android.content.cts.ComponentNameTest", actual);
205 
206         // Test long class name
207         final ComponentName componentName = new ComponentName("com.android.view",
208                 "com.android.view.View");
209         final String falttenString = componentName.flattenToString();
210         // First, compare the string return by flattenToString().
211         assertEquals("com.android.view/com.android.view.View", falttenString);
212         actual = componentName.flattenToShortString();
213         // Then, compare the string return by flattenToShortString().
214         assertEquals("com.android.view/.View", actual);
215     }
216 
217     @TestTargetNew(
218         level = TestLevel.COMPLETE,
219         method = "equals",
220         args = {java.lang.Object.class}
221     )
testEquals()222     public void testEquals() {
223         // new the ComponentName instances, both are the same.
224         final ComponentName componentName1 = getComponentName();
225         ComponentName componentName2 = new ComponentName(componentName1.getPackageName(),
226                 componentName1.getShortClassName());
227         assertTrue(componentName1.equals(componentName2));
228 
229         // new the ComponentName instances, are not the same.
230         componentName2 = new ComponentName(componentName1.getPackageName(),
231                 componentName1.getShortClassName() + "different name");
232         assertFalse(componentName1.equals(componentName2));
233     }
234 
235     @TestTargetNew(
236         level = TestLevel.COMPLETE,
237         method = "toString",
238         args = {}
239     )
testToString()240     public void testToString() {
241         assertNotNull(getComponentName().toString());
242     }
243 
244     @TestTargetNew(
245         level = TestLevel.COMPLETE,
246         method = "toShortString",
247         args = {}
248     )
testToShortString()249     public void testToShortString() {
250         // Test normal string
251         final String shortString = getComponentName().toShortString();
252         assertEquals("{com.android.cts.stub/android.content.cts.ComponentNameTest}", shortString);
253     }
254 
255     @TestTargetNew(
256         level = TestLevel.COMPLETE,
257         method = "getClassName",
258         args = {}
259     )
testGetClassName()260     public void testGetClassName() {
261         // set the expected value
262         final String className = getComponentName().getClassName();
263         assertEquals("android.content.cts.ComponentNameTest", className);
264     }
265 
266     @TestTargetNew(
267         level = TestLevel.COMPLETE,
268         method = "hashCode",
269         args = {}
270     )
testHashCode()271     public void testHashCode() {
272         final ComponentName componentName = getComponentName();
273 
274         final int hashCode1 = componentName.hashCode();
275         assertFalse(0 == hashCode1);
276 
277         final ComponentName componentName2 = new ComponentName(componentName.getPackageName(),
278                 componentName.getClassName());
279         final int hashCode2 = componentName2.hashCode();
280         assertEquals(hashCode1, hashCode2);
281     }
282 
283     @TestTargetNew(
284         level = TestLevel.COMPLETE,
285         method = "writeToParcel",
286         args = {android.content.ComponentName.class, android.os.Parcel.class}
287     )
testWriteToParcel()288     public void testWriteToParcel() {
289         // Test normal status
290         final ComponentName componentName = getComponentName();
291         Parcel parcel = Parcel.obtain();
292         ComponentName.writeToParcel(componentName, parcel);
293         parcel.setDataPosition(0);
294         assertFalse(0 == parcel.dataAvail());
295         assertEquals("com.android.cts.stub", parcel.readString());
296         assertEquals("android.content.cts.ComponentNameTest", parcel.readString());
297 
298         // Test null data
299         parcel = Parcel.obtain();
300         ComponentName.writeToParcel(null, parcel);
301         assertEquals(0, parcel.dataAvail());
302     }
303 
304     @TestTargetNew(
305         level = TestLevel.COMPLETE,
306         method = "describeContents",
307         args = {}
308     )
testDescribeContents()309     public void testDescribeContents() {
310         assertEquals(0, getComponentName().describeContents());
311     }
312 
getComponentName()313     private ComponentName getComponentName() {
314         final ComponentName componentName = new ComponentName(mContext, this.getClass());
315         return componentName;
316     }
317 }
318