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 org.hamcrest.CoreMatchers.equalTo;
23 
24 import android.app.Activity;
25 import android.graphics.Bitmap;
26 import android.graphics.PorterDuffColorFilter;
27 import android.graphics.drawable.BitmapDrawable;
28 import android.graphics.drawable.Drawable;
29 import android.widget.ImageView;
30 
31 import com.android.managedprovisioning.R;
32 
33 import org.hamcrest.BaseMatcher;
34 import org.hamcrest.Description;
35 import org.hamcrest.Matcher;
36 
37 public class CustomizationVerifier {
38     private final Activity mActivity;
39 
CustomizationVerifier(Activity activity)40     public CustomizationVerifier(Activity activity) {
41         mActivity = activity;
42     }
43 
assertDefaultLogoCorrect(int targetColor)44     public void assertDefaultLogoCorrect(int targetColor) {
45         Drawable actualLogo = extractLogo();
46         Drawable expectedLogo = makeDefaultLogo(targetColor);
47         assertThat(actualLogo.getConstantState(), equalTo(expectedLogo.getConstantState()));
48         assertThat(actualLogo.getColorFilter(), equalTo(expectedLogo.getColorFilter()));
49     }
50 
assertCustomLogoCorrect(Bitmap targetLogo)51     public void assertCustomLogoCorrect(Bitmap targetLogo) {
52         Bitmap actualLogo = ((BitmapDrawable) extractLogo()).getBitmap();
53         assertThat(targetLogo, bitmapEqualTo(actualLogo));
54     }
55 
bitmapEqualTo(Bitmap expected)56     private Matcher<Bitmap> bitmapEqualTo(Bitmap expected) {
57         return new BaseMatcher<Bitmap>() {
58             @Override
59             public void describeTo(Description description) {
60                 description.appendText("Bitmap different from expected.");
61             }
62 
63             @Override
64             public boolean matches(Object actual) {
65                 return expected.sameAs((Bitmap) actual);
66             }
67         };
68     }
69 
70     private Drawable makeDefaultLogo(int color) {
71         Drawable logo = mActivity.getDrawable(R.drawable.ic_enterprise_blue_24dp);
72         logo.setColorFilter(new PorterDuffColorFilter(color, SRC_ATOP));
73         return logo;
74     }
75 
76     private Drawable extractLogo() {
77         return ((ImageView) mActivity.findViewById(R.id.sud_layout_icon)).getDrawable();
78     }
79 }
80