1 /*
2  * Copyright (C) 2017 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 package com.android.managedprovisioning.common;
17 
18 import static android.graphics.PorterDuff.Mode.SRC_ATOP;
19 
20 import static androidx.test.espresso.matcher.ViewMatchers.assertThat;
21 
22 import static junit.framework.Assert.assertTrue;
23 
24 import static org.hamcrest.CoreMatchers.equalTo;
25 
26 import android.annotation.LayoutRes;
27 import android.app.Activity;
28 import android.content.res.ColorStateList;
29 import android.graphics.Bitmap;
30 import android.graphics.Canvas;
31 import android.graphics.PorterDuffColorFilter;
32 import android.graphics.drawable.BitmapDrawable;
33 import android.graphics.drawable.Drawable;
34 import android.view.ContextThemeWrapper;
35 import android.widget.ImageView;
36 import android.widget.ProgressBar;
37 
38 import com.android.managedprovisioning.R;
39 import com.android.managedprovisioning.preprovisioning.anim.ColorMatcher;
40 import com.android.managedprovisioning.preprovisioning.anim.SwiperThemeMatcher;
41 
42 import org.hamcrest.BaseMatcher;
43 import org.hamcrest.Description;
44 import org.hamcrest.Matcher;
45 
46 public class CustomizationVerifier {
47     private final Activity mActivity;
48 
CustomizationVerifier(Activity activity)49     public CustomizationVerifier(Activity activity) {
50         mActivity = activity;
51     }
52 
assertStatusBarColorCorrect(int targetColor)53     public void assertStatusBarColorCorrect(int targetColor) {
54         int statusBarColor = mActivity.getWindow().getStatusBarColor();
55         assertThat(statusBarColor, equalTo(targetColor));
56     }
57 
58     // Disabled for b/73157813.
assertSwiperColorCorrect(int targetSwiperColor)59     public void assertSwiperColorCorrect(int targetSwiperColor) {
60 //        Drawable expectedDrawable = makeExpectedTopInfoDrawable(targetSwiperColor);
61 //        ImageView animatedView = mActivity.findViewById(R.id.animated_info);
62 //        Drawable actualDrawable = animatedView.getDrawable();
63 //        assertDrawableEquals(expectedDrawable, actualDrawable);
64     }
65 
assertDefaultLogoCorrect(int targetColor)66     public void assertDefaultLogoCorrect(int targetColor) {
67         Drawable actualLogo = extractLogo();
68         Drawable expectedLogo = makeDefaultLogo(targetColor);
69         assertThat(actualLogo.getConstantState(), equalTo(expectedLogo.getConstantState()));
70         assertThat(actualLogo.getColorFilter(), equalTo(expectedLogo.getColorFilter()));
71     }
72 
assertCustomLogoCorrect(Bitmap targetLogo)73     public void assertCustomLogoCorrect(Bitmap targetLogo) {
74         Bitmap actualLogo = ((BitmapDrawable) extractLogo()).getBitmap();
75         assertThat(targetLogo, bitmapEqualTo(actualLogo));
76     }
77 
assertProgressBarColorCorrect(@ayoutRes int progressBarLayoutId, int targetColor)78     public void assertProgressBarColorCorrect(@LayoutRes int progressBarLayoutId, int targetColor) {
79         ProgressBar progressBar = mActivity.findViewById(progressBarLayoutId);
80 
81         ColorStateList expected = ColorStateList.valueOf(targetColor);
82         assertThat(progressBar.getIndeterminateTintList(), equalTo(expected));
83         assertThat(progressBar.getProgressBackgroundTintList(), equalTo(expected));
84     }
85 
bitmapEqualTo(Bitmap expected)86     private Matcher<Bitmap> bitmapEqualTo(Bitmap expected) {
87         return new BaseMatcher<Bitmap>() {
88             @Override
89             public void describeTo(Description description) {
90                 description.appendText("Bitmap different from expected.");
91             }
92 
93             @Override
94             public boolean matches(Object actual) {
95                 return expected.sameAs((Bitmap) actual);
96             }
97         };
98     }
99 
100     private Drawable makeDefaultLogo(int color) {
101         Drawable logo = mActivity.getDrawable(R.drawable.ic_enterprise_blue_24dp);
102         logo.setColorFilter(new PorterDuffColorFilter(color, SRC_ATOP));
103         return logo;
104     }
105 
106     private Drawable extractLogo() {
107         return ((ImageView) mActivity.findViewById(R.id.sud_layout_icon)).getDrawable();
108     }
109 
110     private Bitmap drawableToBitmap(Drawable drawable) {
111         Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
112                 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
113         Canvas canvas = new Canvas(bitmap);
114         drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
115         drawable.draw(canvas);
116         return bitmap;
117     }
118 
119     private void assertDrawableEquals(Drawable expected, Drawable actual) {
120         assertTrue(drawableToBitmap(expected).sameAs(drawableToBitmap(actual)));
121     }
122 }