• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.splitapp;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.fail;
22 
23 import android.app.Activity;
24 import android.content.Context;
25 import android.content.res.Resources;
26 import android.content.res.TypedArray;
27 import android.graphics.drawable.ColorDrawable;
28 import android.graphics.drawable.Drawable;
29 import android.view.ContextThemeWrapper;
30 import android.view.View;
31 import android.view.Window;
32 import android.widget.LinearLayout;
33 
34 /**
35  * A helper class to retrieve theme values of Theme_Base and Theme_Warm and Theme_Rose.
36  */
37 public class TestThemeHelper {
38 
39     public static final String THEME_WARM =
40             "com.android.cts.splitapp.feature_warm:style/Theme_Warm";
41     public static final String THEME_ROSE =
42             "com.android.cts.splitapp.feature_rose:style/Theme_Rose";
43 
44     public enum ThemeColors {
45         BASE,
46         BASE_LT,
47         WARM,
48         WARM_LT,
49         ROSE,
50         ROSE_LT
51     };
52 
53     private static final int COLOR_BLUE = 0xFF0000FF;
54     private static final int COLOR_TEAL = 0xFF008080;
55     private static final int COLOR_AQUA = 0xFF00FFFF;
56     private static final int COLOR_BLUE_LT = 0xFFADD8E6;
57     private static final int COLOR_TEAL_LT = 0xFFE0F0F0;
58     private static final int COLOR_AQUA_LT = 0xFFE0FFFF;
59     private static final int COLOR_RED = 0xFFFF0000;
60     private static final int COLOR_YELLOW = 0xFFFFFF00;
61     private static final int COLOR_RED_LT = 0xFFFFCCCB;
62     private static final int COLOR_ORANGE_LT = 0xFFFED8B1;
63     private static final int COLOR_PINK = 0xFFFFC0CB;
64     private static final int COLOR_RUBY = 0xFFCC0080;
65     private static final int COLOR_PINK_LT = 0xFFFFB6C1;
66     private static final int COLOR_ROSE_LT = 0xFFFF66CC;
67 
68     private static final int[] THEME_BASE_COLORS = {COLOR_BLUE, COLOR_TEAL, COLOR_AQUA};
69     private static final int[] THEME_BASE_LT_COLORS = {COLOR_BLUE_LT, COLOR_TEAL_LT, COLOR_AQUA_LT};
70     private static final int[] THEME_WARM_COLORS = {COLOR_RED, COLOR_TEAL, COLOR_YELLOW};
71     private static final int[] THEME_WARM_LT_COLORS =
72             {COLOR_RED_LT, COLOR_ORANGE_LT, COLOR_AQUA_LT};
73     private static final int[] THEME_ROSE_COLORS = {COLOR_PINK, COLOR_TEAL, COLOR_RUBY};
74     private static final int[] THEME_ROSE_LT_COLORS = {COLOR_PINK_LT, COLOR_ROSE_LT, COLOR_AQUA_LT};
75 
76     /** {@link com.android.cts.splitapp.R.attr.customColor} */
77     private final int mCustomColor;
78 
79     /** {#link android.R.attr.colorBackground} */
80     private final int mColorBackground;
81 
82     /** {#link android.R.attr.navigationBarColor} */
83     private final int mNavigationBarColor;
84 
85     /** {#link android.R.attr.statusBarColor} */
86     private final int mStatusBarColor;
87 
88     /** {#link android.R.attr.windowBackground} */
89     private final int mWindowBackground;
90 
TestThemeHelper(Context context, int themeResId)91     public TestThemeHelper(Context context, int themeResId) {
92         final Resources.Theme theme = new ContextThemeWrapper(context, themeResId).getTheme();
93         mCustomColor = getColor(theme, R.attr.customColor);
94         mColorBackground = getColor(theme, android.R.attr.colorBackground);
95         mNavigationBarColor = getColor(theme, android.R.attr.navigationBarColor);
96         mStatusBarColor = getColor(theme, android.R.attr.statusBarColor);
97         mWindowBackground = getDrawableColor(theme, android.R.attr.windowBackground);
98     }
99 
assertThemeValues(ThemeColors themeColors)100     public void assertThemeValues(ThemeColors themeColors) {
101         final int[] colors = getThemeColors(themeColors);
102         assertThat(themeColors).isNotNull();
103         assertThat(mCustomColor).isEqualTo(colors[0]);
104         assertThat(mNavigationBarColor).isEqualTo(colors[1]);
105         assertThat(mStatusBarColor).isEqualTo(colors[2]);
106         assertThat(mWindowBackground).isEqualTo(mCustomColor);
107     }
108 
getThemeColors(ThemeColors themeColors)109     private int[] getThemeColors(ThemeColors themeColors) {
110         switch (themeColors) {
111             case BASE: return THEME_BASE_COLORS;
112             case BASE_LT: return THEME_BASE_LT_COLORS;
113             case WARM: return THEME_WARM_COLORS;
114             case WARM_LT: return THEME_WARM_LT_COLORS;
115             case ROSE: return THEME_ROSE_COLORS;
116             case ROSE_LT: return THEME_ROSE_LT_COLORS;
117             default:
118                 break;
119         }
120         return null;
121     }
122 
assertThemeApplied(Activity activity)123     public void assertThemeApplied(Activity activity) {
124         assertLayoutBGColor(activity, mCustomColor);
125 
126         final Window window = activity.getWindow();
127         assertThat(window.getStatusBarColor()).isEqualTo(mStatusBarColor);
128         assertThat(window.getNavigationBarColor()).isEqualTo(mNavigationBarColor);
129         assertDrawableColor(window.getDecorView().getBackground(), mWindowBackground);
130 
131         assertTextViewBGColor(activity);
132     }
133 
getColor(Resources.Theme theme, int resourceId)134     private int getColor(Resources.Theme theme, int resourceId) {
135         final TypedArray ta = theme.obtainStyledAttributes(new int[] {resourceId});
136         final int color = ta.getColor(0, 0);
137         ta.recycle();
138         return color;
139     }
140 
getDrawableColor(Resources.Theme theme, int resourceId)141     private int getDrawableColor(Resources.Theme theme, int resourceId) {
142         final TypedArray ta = theme.obtainStyledAttributes(new int[] {resourceId});
143         final Drawable color = ta.getDrawable(0);
144         ta.recycle();
145         if (!(color instanceof ColorDrawable)) {
146             fail("Can't get drawable color");
147         }
148         return ((ColorDrawable) color).getColor();
149     }
150 
assertLayoutBGColor(Activity activity, int expected)151     private void assertLayoutBGColor(Activity activity, int expected) {
152         final LinearLayout layout = activity.findViewById(R.id.content);
153         final Drawable background = layout.getBackground();
154         assertDrawableColor(background, expected);
155     }
156 
assertDrawableColor(Drawable drawable, int expected)157     private void assertDrawableColor(Drawable drawable, int expected) {
158         int color = 0;
159         if (drawable instanceof ColorDrawable) {
160             color = ((ColorDrawable) drawable).getColor();
161         } else {
162             fail("Can't get drawable color");
163         }
164         assertThat(color).isEqualTo(expected);
165     }
166 
assertTextViewBGColor(Activity activity)167     private void assertTextViewBGColor(Activity activity) {
168         final View view = activity.findViewById(R.id.text);
169         final Drawable background = view.getBackground();
170         assertDrawableColor(background, mColorBackground);
171     }
172 }
173