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.Context;
20 import android.content.ContextWrapper;
21 import android.content.pm.PackageManager;
22 import android.platform.test.annotations.AppModeFull;
23 
24 /**
25  * Test {@link ContextWrapper}.
26  *
27  * <p>
28  * This class inherits most of its test methods from its parent ContextTest.
29  * Since ContextWrapper delegates its requests to the Context, the same test cases should pass
30  * for both Context and ContextWrapper.
31  *
32  * <p>
33  * There are some tests for ContextWrapper that don't make sense for Context - those are included
34  * in this class.
35  */
36 @AppModeFull // TODO(Instant) Figure out which APIs should work.
37 public class ContextWrapperTest extends ContextTest {
38 
39     /**
40      * Returns the ContextWrapper object that's being tested.
41      */
getContextUnderTest()42     protected Context getContextUnderTest() {
43         return new ContextWrapper(getContext());
44     }
45 
testConstructor()46     public void testConstructor() {
47         new ContextWrapper(mContext);
48 
49         // null param is allowed
50         new ContextWrapper(null);
51     }
52 
testAccessBaseContext()53     public void testAccessBaseContext() throws PackageManager.NameNotFoundException {
54         Context context = getContext();
55         MockContextWrapper testContextWrapper = new MockContextWrapper(context);
56 
57         // Test getBaseContext()
58         assertSame(context, testContextWrapper.getBaseContext());
59 
60         Context secondContext = testContextWrapper.createPackageContext(getValidPackageName(),
61                 Context.CONTEXT_IGNORE_SECURITY);
62         assertNotNull(secondContext);
63 
64         // Test attachBaseContext
65         try {
66             testContextWrapper.attachBaseContext(secondContext);
67             fail("If base context has already been set, it should throw a IllegalStateException.");
68         } catch (IllegalStateException e) {
69         }
70     }
71 
72     // TODO: this mock seems unnecessary. Remove it, and just use ContextWrapper?
73     private static final class MockContextWrapper extends ContextWrapper {
MockContextWrapper(Context base)74         public MockContextWrapper(Context base) {
75             super(base);
76         }
77 
78         @Override
attachBaseContext(Context base)79         public void attachBaseContext(Context base) {
80             super.attachBaseContext(base);
81         }
82     }
83 }
84