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 import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
20 
21 import static org.hamcrest.CoreMatchers.equalTo;
22 import static org.hamcrest.CoreMatchers.instanceOf;
23 
24 import android.app.Activity;
25 import android.content.res.ColorStateList;
26 import android.graphics.Bitmap;
27 import android.graphics.PorterDuffColorFilter;
28 import android.graphics.drawable.BitmapDrawable;
29 import android.graphics.drawable.ColorDrawable;
30 import android.graphics.drawable.Drawable;
31 import android.widget.ImageView;
32 import android.widget.ProgressBar;
33 
34 import com.android.managedprovisioning.R;
35 import com.android.managedprovisioning.preprovisioning.anim.ColorMatcher;
36 import com.android.managedprovisioning.preprovisioning.anim.SwiperThemeMatcher;
37 
38 import org.hamcrest.BaseMatcher;
39 import org.hamcrest.Description;
40 import org.hamcrest.Matcher;
41 
42 public class CustomizationVerifier {
43     private final Activity mActivity;
44 
CustomizationVerifier(Activity activity)45     public CustomizationVerifier(Activity activity) {
46         mActivity = activity;
47     }
48 
assertStatusBarColorCorrect(int targetColor)49     public void assertStatusBarColorCorrect(int targetColor) {
50         int statusBarColor = mActivity.getWindow().getStatusBarColor();
51         assertThat(statusBarColor, equalTo(targetColor));
52     }
53 
assertSwiperColorCorrect(int targetSwiperColor)54     public void assertSwiperColorCorrect(int targetSwiperColor) {
55         assertThat(mActivity.getThemeResId(), equalTo( // TODO: maybe replace with a screenshot test
56                 new SwiperThemeMatcher(mActivity, new ColorMatcher()).findTheme(
57                         targetSwiperColor)));
58     }
59 
assertDefaultLogoCorrect(int targetColor)60     public void assertDefaultLogoCorrect(int targetColor) {
61         Drawable actualLogo = extractLogo();
62         Drawable expectedLogo = makeDefaultLogo(targetColor);
63         assertThat(actualLogo.getConstantState(), equalTo(expectedLogo.getConstantState()));
64         assertThat(actualLogo.getColorFilter(), equalTo(expectedLogo.getColorFilter()));
65     }
66 
assertCustomLogoCorrect(Bitmap targetLogo)67     public void assertCustomLogoCorrect(Bitmap targetLogo) {
68         Bitmap actualLogo = ((BitmapDrawable) extractLogo()).getBitmap();
69         assertThat(targetLogo, bitmapEqualTo(actualLogo));
70     }
71 
assertNextButtonColorCorrect(int targetColor)72     public void assertNextButtonColorCorrect(int targetColor) {
73         Drawable buttonBackground = mActivity.findViewById(R.id.next_button).getBackground();
74         assertThat(buttonBackground, instanceOf(ColorDrawable.class));
75         assertThat(((ColorDrawable) buttonBackground).getColor(), equalTo(targetColor));
76     }
77 
assertProgressBarColorCorrect(int targetColor)78     public void assertProgressBarColorCorrect(int targetColor) {
79         ProgressBar progressBar = (ProgressBar) mActivity.findViewById(
80                 com.android.setupwizardlib.R.id.suw_layout_progress);
81 
82         ColorStateList expected = ColorStateList.valueOf(targetColor);
83         assertThat(progressBar.getIndeterminateTintList(), equalTo(expected));
84         assertThat(progressBar.getProgressBackgroundTintList(), equalTo(expected));
85     }
86 
bitmapEqualTo(Bitmap expected)87     private Matcher<Bitmap> bitmapEqualTo(Bitmap expected) {
88         return new BaseMatcher<Bitmap>() {
89             @Override
90             public void describeTo(Description description) {
91                 description.appendText("Bitmap different from expected.");
92             }
93 
94             @Override
95             public boolean matches(Object actual) {
96                 return expected.sameAs((Bitmap) actual);
97             }
98         };
99     }
100 
101     private Drawable makeDefaultLogo(int color) {
102         Drawable logo = mActivity.getDrawable(R.drawable.ic_enterprise_blue_24dp);
103         logo.setColorFilter(new PorterDuffColorFilter(color, SRC_ATOP));
104         return logo;
105     }
106 
107     private Drawable extractLogo() {
108         return ((ImageView) mActivity.findViewById(
109                 com.android.setupwizardlib.R.id.suw_layout_icon)).getDrawable();
110     }
111 }