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.view.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotSame; 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assert.fail; 23 24 import android.content.Context; 25 import android.content.res.Configuration; 26 import android.content.res.Resources; 27 import android.content.res.Resources.Theme; 28 import android.content.res.TypedArray; 29 import android.view.ContextThemeWrapper; 30 31 import androidx.test.InstrumentationRegistry; 32 import androidx.test.filters.SmallTest; 33 import androidx.test.runner.AndroidJUnit4; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 @SmallTest 40 @RunWith(AndroidJUnit4.class) 41 public class ContextThemeWrapperTest { 42 private static final int SYSTEM_DEFAULT_THEME = 0; 43 44 private Context mContext; 45 46 private static class MockContextThemeWrapper extends ContextThemeWrapper { 47 public boolean isOnApplyThemeResourceCalled; 48 public MockContextThemeWrapper(Context base, int themeres) { 49 super(base, themeres); 50 } 51 52 @Override 53 protected void onApplyThemeResource(Theme theme, int resid, boolean first) { 54 isOnApplyThemeResourceCalled = true; 55 super.onApplyThemeResource(theme, resid, first); 56 } 57 } 58 59 @Before 60 public void setup() { 61 mContext = InstrumentationRegistry.getTargetContext(); 62 } 63 64 @Test 65 public void testConstructor() { 66 new ContextThemeWrapper(); 67 68 new ContextThemeWrapper(mContext, R.style.TextAppearance); 69 70 new ContextThemeWrapper(mContext, mContext.getTheme()); 71 } 72 73 @Test 74 public void testAccessTheme() { 75 ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper( 76 mContext, SYSTEM_DEFAULT_THEME); 77 // set Theme to TextAppearance 78 contextThemeWrapper.setTheme(R.style.TextAppearance); 79 TypedArray ta = 80 contextThemeWrapper.getTheme().obtainStyledAttributes(R.styleable.TextAppearance); 81 82 // assert theme style of TextAppearance 83 verifyIdenticalTextAppearanceStyle(ta); 84 } 85 86 @Test 87 public void testSetTheme() { 88 ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper( 89 mContext, SYSTEM_DEFAULT_THEME); 90 Theme theme = mContext.getResources().newTheme(); 91 theme.applyStyle(R.style.TextAppearance, true); 92 contextThemeWrapper.setTheme(theme); 93 94 TypedArray ta = 95 contextThemeWrapper.getTheme().obtainStyledAttributes(R.styleable.TextAppearance); 96 verifyIdenticalTextAppearanceStyle(ta); 97 } 98 99 @Test 100 public void testGetSystemService() { 101 // Note that we can't use Mockito since ContextThemeWrapper.onApplyThemeResource is 102 // protected 103 final MockContextThemeWrapper contextThemeWrapper = 104 new MockContextThemeWrapper(mContext, R.style.TextAppearance); 105 contextThemeWrapper.getTheme(); 106 assertTrue(contextThemeWrapper.isOnApplyThemeResourceCalled); 107 108 // All service get from contextThemeWrapper just the same as this context get, 109 // except Context.LAYOUT_INFLATER_SERVICE. 110 assertEquals(mContext.getSystemService(Context.ACTIVITY_SERVICE), 111 contextThemeWrapper.getSystemService(Context.ACTIVITY_SERVICE)); 112 assertNotSame(mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE), 113 contextThemeWrapper.getSystemService(Context.LAYOUT_INFLATER_SERVICE)); 114 } 115 116 @Test 117 public void testAttachBaseContext() { 118 assertTrue((new ContextThemeWrapper() { 119 public boolean test() { 120 // Set two different context to ContextThemeWrapper 121 // it should throw a exception when set it at second time. 122 // As ContextThemeWrapper is a context, we will attachBaseContext to 123 // two different ContextThemeWrapper instances. 124 try { 125 attachBaseContext(new ContextThemeWrapper(mContext, 126 R.style.TextAppearance)); 127 } catch(IllegalStateException e) { 128 fail("test attachBaseContext fail"); 129 } 130 131 try { 132 attachBaseContext(new ContextThemeWrapper()); 133 fail("test attachBaseContext fail"); 134 } catch(IllegalStateException e) { 135 // expected 136 } 137 return true; 138 } 139 }).test()); 140 } 141 142 @Test 143 public void testApplyOverrideConfiguration() { 144 final int realDensity = mContext.getResources().getConfiguration().densityDpi; 145 final int expectedDensity = realDensity + 1; 146 147 ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper( 148 mContext, SYSTEM_DEFAULT_THEME); 149 150 Configuration overrideConfig = new Configuration(); 151 overrideConfig.densityDpi = expectedDensity; 152 contextThemeWrapper.applyOverrideConfiguration(overrideConfig); 153 154 Configuration actualConfiguration = contextThemeWrapper.getResources().getConfiguration(); 155 assertEquals(expectedDensity, actualConfiguration.densityDpi); 156 } 157 158 private void verifyIdenticalTextAppearanceStyle(TypedArray ta) { 159 final int defValue = -1; 160 // get Theme and assert 161 Resources.Theme expected = mContext.getResources().newTheme(); 162 expected.setTo(mContext.getTheme()); 163 expected.applyStyle(R.style.TextAppearance, true); 164 TypedArray expectedTa = expected.obtainStyledAttributes(R.styleable.TextAppearance); 165 assertEquals(expectedTa.getIndexCount(), ta.getIndexCount()); 166 assertEquals(expectedTa.getColor(R.styleable.TextAppearance_textColor, defValue), 167 ta.getColor(R.styleable.TextAppearance_textColor, defValue)); 168 assertEquals(expectedTa.getColor(R.styleable.TextAppearance_textColorHint, defValue), 169 ta.getColor(R.styleable.TextAppearance_textColorHint, defValue)); 170 assertEquals(expectedTa.getColor(R.styleable.TextAppearance_textColorLink, defValue), 171 ta.getColor(R.styleable.TextAppearance_textColorLink, defValue)); 172 assertEquals(expectedTa.getColor(R.styleable.TextAppearance_textColorHighlight, defValue), 173 ta.getColor(R.styleable.TextAppearance_textColorHighlight, defValue)); 174 assertEquals(expectedTa.getDimension(R.styleable.TextAppearance_textSize, defValue), 175 ta.getDimension(R.styleable.TextAppearance_textSize, defValue), 0.0f); 176 assertEquals(expectedTa.getInt(R.styleable.TextAppearance_textStyle, defValue), 177 ta.getInt(R.styleable.TextAppearance_textStyle, defValue)); 178 } 179 } 180