1 /*
2  * Copyright (C) 2020 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 com.android.cts.isolatedsplitapp;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.content.res.TypedArray;
24 import android.graphics.drawable.ColorDrawable;
25 import android.graphics.drawable.Drawable;
26 import android.view.ContextThemeWrapper;
27 
28 /**
29  * A helper class to retrieve theme values of Theme_Base, Theme_Feature_A, Theme_Feature_B or
30  * Theme_Feature_C.
31  */
32 public class TestTheme {
33 
34     public static final String THEME_FEATURE_A =
35             "com.android.cts.isolatedsplitapp.feature_a:style/Theme_Feature_A";
36     public static final String THEME_FEATURE_B =
37             "com.android.cts.isolatedsplitapp.feature_b:style/Theme_Feature_B";
38     public static final String THEME_FEATURE_C =
39             "com.android.cts.isolatedsplitapp.feature_c:style/Theme_Feature_C";
40 
41     public static final int COLOR_BLACK = 0xFF000000;
42     public static final int COLOR_RED = 0xFFFF0000;
43     public static final int COLOR_GREEN = 0xFF00FF00;
44     public static final int COLOR_BLUE = 0xFF0000FF;
45     public static final int COLOR_GRAY = 0xFF888888;
46     public static final int COLOR_LTGRAY = 0xFF444444;
47     public static final int COLOR_DKGRAY = 0xFFCCCCCC;
48     public static final int COLOR_YELLOW = 0xFFFFFF00;
49     public static final int COLOR_PURPLE = 0xFF800080;
50     public static final int COLOR_PINK = 0xFFFFC0CB;
51     public static final int COLOR_ORANGE = 0xFFFFA500;
52     public static final int COLOR_LINEN = 0xFFFAF0E6;
53     public static final int COLOR_MINTCREAM = 0xFFF5FFFA;
54     public static final int COLOR_CYAN = 0xFF00FFFF;
55     public static final int COLOR_MAGENTA = 0xFFFF00FF;
56     public static final int COLOR_MAROON = 0xFF800000;
57     public static final int COLOR_NAVY = 0xFF000080;
58     public static final int COLOR_OLIVE = 0xFF808000;
59 
60     private static final String ATTR_THEME_PRIMARY_COLOR =
61             "com.android.cts.isolatedsplitapp.feature_a:attr/themePrimaryColor";
62     private static final String ATTR_THEME_SECONDARY_COLOR =
63             "com.android.cts.isolatedsplitapp.feature_b:attr/themeSecondaryColor";
64     private static final String ATTR_THEME_TERTIARY_COLOR =
65             "com.android.cts.isolatedsplitapp.feature_c:attr/themeTertiaryColor";
66 
67     /** {@link R.attr.themeName} */
68     public String mName;
69 
70     /** {@link R.attr.themeBaseColor} */
71     public int mBaseColor;
72 
73     /** {@link #ATTR_THEME_PRIMARY_COLOR} */
74     public int mPrimaryColor;
75 
76     /** {@link #ATTR_THEME_SECONDARY_COLOR} */
77     public int mSecondaryColor;
78 
79     /** {@link #ATTR_THEME_TERTIARY_COLOR} */
80     public int mTertiaryColor;
81 
82     /** {#link android.R.attr.colorBackground} */
83     public int mColorBackground;
84 
85     /** {#link android.R.attr.navigationBarColor} */
86     public int mNavigationBarColor;
87 
88     /** {#link android.R.attr.statusBarColor} */
89     public int mStatusBarColor;
90 
91     /** {#link android.R.attr.windowBackground} */
92     public int mWindowBackground;
93 
TestTheme(Context context, String nameOfIdentifier)94     public TestTheme(Context context, String nameOfIdentifier) {
95         setTheme(context, nameOfIdentifier);
96     }
97 
TestTheme(Context context, int themeId)98     public TestTheme(Context context, int themeId) {
99         setTheme(context, themeId);
100     }
101 
assertThemeBaseValues()102     public void assertThemeBaseValues() {
103         assertThat(mName).isEqualTo("Base Theme");
104         assertThat(mBaseColor).isEqualTo(COLOR_BLACK);
105         assertThat(mNavigationBarColor).isEqualTo(COLOR_RED);
106         assertThat(mStatusBarColor).isEqualTo(COLOR_GREEN);
107         assertThat(mWindowBackground).isEqualTo(mBaseColor);
108     }
109 
assertThemeBaseValues_pl()110     public void assertThemeBaseValues_pl() {
111         assertThat(mName).isEqualTo("Base Theme Polish");
112         assertThat(mBaseColor).isEqualTo(COLOR_GREEN);
113         assertThat(mNavigationBarColor).isEqualTo(COLOR_BLACK);
114         assertThat(mStatusBarColor).isEqualTo(COLOR_RED);
115         assertThat(mWindowBackground).isEqualTo(mBaseColor);
116     }
117 
assertThemeFeatureAValues()118     public void assertThemeFeatureAValues() {
119         assertThat(mName).isEqualTo("Feature A Theme");
120         assertThat(mBaseColor).isEqualTo(COLOR_BLUE);
121         assertThat(mPrimaryColor).isEqualTo(COLOR_GRAY);
122         assertThat(mColorBackground).isEqualTo(COLOR_LTGRAY);
123         assertThat(mNavigationBarColor).isEqualTo(COLOR_DKGRAY);
124         assertThat(mStatusBarColor).isEqualTo(COLOR_YELLOW);
125         assertThat(mWindowBackground).isEqualTo(mPrimaryColor);
126     }
127 
assertThemeFeatureAValues_pl()128     public void assertThemeFeatureAValues_pl() {
129         assertThat(mName).isEqualTo("Feature A Theme Polish");
130         assertThat(mBaseColor).isEqualTo(COLOR_YELLOW);
131         assertThat(mPrimaryColor).isEqualTo(COLOR_DKGRAY);
132         assertThat(mColorBackground).isEqualTo(COLOR_GRAY);
133         assertThat(mNavigationBarColor).isEqualTo(COLOR_LTGRAY);
134         assertThat(mStatusBarColor).isEqualTo(COLOR_BLUE);
135         assertThat(mWindowBackground).isEqualTo(mPrimaryColor);
136     }
137 
assertThemeFeatureAValuesDiffRev()138     public void assertThemeFeatureAValuesDiffRev() {
139         assertThat(mName).isEqualTo("Feature A Theme Diff Revision");
140         assertThat(mBaseColor).isEqualTo(COLOR_YELLOW);
141         assertThat(mPrimaryColor).isEqualTo(COLOR_BLUE);
142         assertThat(mColorBackground).isEqualTo(COLOR_GRAY);
143         assertThat(mNavigationBarColor).isEqualTo(COLOR_LTGRAY);
144         assertThat(mStatusBarColor).isEqualTo(COLOR_DKGRAY);
145         assertThat(mWindowBackground).isEqualTo(mPrimaryColor);
146     }
147 
assertThemeFeatureBValues()148     public void assertThemeFeatureBValues() {
149         assertThat(mName).isEqualTo("Feature B Theme");
150         assertThat(mBaseColor).isEqualTo(COLOR_PURPLE);
151         assertThat(mSecondaryColor).isEqualTo(COLOR_PINK);
152         assertThat(mColorBackground).isEqualTo(COLOR_ORANGE);
153         assertThat(mNavigationBarColor).isEqualTo(COLOR_LINEN);
154         assertThat(mStatusBarColor).isEqualTo(COLOR_MINTCREAM);
155         assertThat(mWindowBackground).isEqualTo(mSecondaryColor);
156     }
157 
assertThemeFeatureBValues_pl()158     public void assertThemeFeatureBValues_pl() {
159         assertThat(mName).isEqualTo("Feature B Theme Polish");
160         assertThat(mBaseColor).isEqualTo(COLOR_MINTCREAM);
161         assertThat(mSecondaryColor).isEqualTo(COLOR_LINEN);
162         assertThat(mColorBackground).isEqualTo(COLOR_PINK);
163         assertThat(mNavigationBarColor).isEqualTo(COLOR_ORANGE);
164         assertThat(mStatusBarColor).isEqualTo(COLOR_PURPLE);
165         assertThat(mWindowBackground).isEqualTo(mSecondaryColor);
166     }
167 
assertThemeFeatureCValues()168     public void assertThemeFeatureCValues() {
169         assertThat(mName).isEqualTo("Feature C Theme");
170         assertThat(mBaseColor).isEqualTo(COLOR_CYAN);
171         assertThat(mTertiaryColor).isEqualTo(COLOR_MAGENTA);
172         assertThat(mColorBackground).isEqualTo(COLOR_MAROON);
173         assertThat(mNavigationBarColor).isEqualTo(COLOR_NAVY);
174         assertThat(mStatusBarColor).isEqualTo(COLOR_OLIVE);
175         assertThat(mWindowBackground).isEqualTo(mTertiaryColor);
176     }
177 
assertThemeFeatureCValues_pl()178     public void assertThemeFeatureCValues_pl() {
179         assertThat(mName).isEqualTo("Feature C Theme Polish");
180         assertThat(mBaseColor).isEqualTo(COLOR_OLIVE);
181         assertThat(mTertiaryColor).isEqualTo(COLOR_NAVY);
182         assertThat(mColorBackground).isEqualTo(COLOR_MAGENTA);
183         assertThat(mNavigationBarColor).isEqualTo(COLOR_MAROON);
184         assertThat(mStatusBarColor).isEqualTo(COLOR_CYAN);
185         assertThat(mWindowBackground).isEqualTo(mTertiaryColor);
186     }
187 
setTheme(Context context, String nameOfIdentifier)188     private void setTheme(Context context, String nameOfIdentifier) {
189         final int themeId = resolveResourceId(context , nameOfIdentifier);
190         if (themeId == 0) {
191             throw new IllegalArgumentException("Failed to a resource identifier for the "
192                     + nameOfIdentifier);
193         }
194         setTheme(context, themeId);
195     }
196 
setTheme(Context context, int themeId)197     private void setTheme(Context context, int themeId) {
198         final Resources.Theme theme = new ContextThemeWrapper(context, themeId).getTheme();
199         mName = getString(theme, R.attr.themeName);
200         mBaseColor = getColor(theme, R.attr.themeBaseColor);
201         mPrimaryColor = getColor(theme, resolveResourceId(context, ATTR_THEME_PRIMARY_COLOR));
202         mSecondaryColor = getColor(theme, resolveResourceId(context, ATTR_THEME_SECONDARY_COLOR));
203         mTertiaryColor = getColor(theme, resolveResourceId(context, ATTR_THEME_TERTIARY_COLOR));
204         mColorBackground = getColor(theme, android.R.attr.colorBackground);
205         mNavigationBarColor = getColor(theme, android.R.attr.navigationBarColor);
206         mStatusBarColor = getColor(theme, android.R.attr.statusBarColor);
207         mWindowBackground = getDrawableColor(theme, android.R.attr.windowBackground);
208     }
209 
resolveResourceId(Context context, String nameOfIdentifier)210     private int resolveResourceId(Context context, String nameOfIdentifier) {
211         return context.getResources().getIdentifier(nameOfIdentifier, null, null);
212     }
213 
getString(Resources.Theme theme, int resourceId)214     private String getString(Resources.Theme theme, int resourceId) {
215         final TypedArray ta = theme.obtainStyledAttributes(new int[] {resourceId});
216         final String string = ta.getString(0);
217         ta.recycle();
218         return string;
219     }
220 
getColor(Resources.Theme theme, int resourceId)221     private int getColor(Resources.Theme theme, int resourceId) {
222         if (resourceId == 0) {
223             return 0;
224         }
225         final TypedArray ta = theme.obtainStyledAttributes(new int[] {resourceId});
226         final int color = ta.getColor(0, 0);
227         ta.recycle();
228         return color;
229     }
230 
getDrawableColor(Resources.Theme theme, int resourceId)231     private int getDrawableColor(Resources.Theme theme, int resourceId) {
232         final TypedArray ta = theme.obtainStyledAttributes(new int[] {resourceId});
233         final Drawable color = ta.getDrawable(0);
234         ta.recycle();
235         if (!(color instanceof ColorDrawable)) {
236             return 0;
237         }
238         return ((ColorDrawable) color).getColor();
239     }
240 }
241