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 junit.framework.Assert.assertTrue; 22 23 import static org.hamcrest.CoreMatchers.equalTo; 24 import static org.hamcrest.CoreMatchers.instanceOf; 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.AnimatedVectorDrawable; 33 import android.graphics.drawable.BitmapDrawable; 34 import android.graphics.drawable.ColorDrawable; 35 import android.graphics.drawable.Drawable; 36 import android.view.ContextThemeWrapper; 37 import android.widget.ImageView; 38 import android.widget.ProgressBar; 39 40 import com.android.managedprovisioning.R; 41 import com.android.managedprovisioning.preprovisioning.anim.ColorMatcher; 42 import com.android.managedprovisioning.preprovisioning.anim.SwiperThemeMatcher; 43 44 import org.hamcrest.BaseMatcher; 45 import org.hamcrest.Description; 46 import org.hamcrest.Matcher; 47 48 public class CustomizationVerifier { 49 private final Activity mActivity; 50 CustomizationVerifier(Activity activity)51 public CustomizationVerifier(Activity activity) { 52 mActivity = activity; 53 } 54 assertStatusBarColorCorrect(int targetColor)55 public void assertStatusBarColorCorrect(int targetColor) { 56 int statusBarColor = mActivity.getWindow().getStatusBarColor(); 57 assertThat(statusBarColor, equalTo(targetColor)); 58 } 59 60 // Disabled for b/73157813. assertSwiperColorCorrect(int targetSwiperColor)61 public void assertSwiperColorCorrect(int targetSwiperColor) { 62 // Drawable expectedDrawable = makeExpectedTopInfoDrawable(targetSwiperColor); 63 // ImageView animatedView = mActivity.findViewById(R.id.animated_info); 64 // Drawable actualDrawable = animatedView.getDrawable(); 65 // assertDrawableEquals(expectedDrawable, actualDrawable); 66 } 67 assertDefaultLogoCorrect(int targetColor)68 public void assertDefaultLogoCorrect(int targetColor) { 69 Drawable actualLogo = extractLogo(); 70 Drawable expectedLogo = makeDefaultLogo(targetColor); 71 assertThat(actualLogo.getConstantState(), equalTo(expectedLogo.getConstantState())); 72 assertThat(actualLogo.getColorFilter(), equalTo(expectedLogo.getColorFilter())); 73 } 74 assertCustomLogoCorrect(Bitmap targetLogo)75 public void assertCustomLogoCorrect(Bitmap targetLogo) { 76 Bitmap actualLogo = ((BitmapDrawable) extractLogo()).getBitmap(); 77 assertThat(targetLogo, bitmapEqualTo(actualLogo)); 78 } 79 assertNextButtonColorCorrect(int targetColor)80 public void assertNextButtonColorCorrect(int targetColor) { 81 ColorStateList actual = mActivity.findViewById(R.id.next_button).getBackgroundTintList(); 82 ColorStateList expected = ColorStateList.valueOf(targetColor); 83 assertThat(actual, equalTo(expected)); 84 } 85 assertProgressBarColorCorrect(@ayoutRes int progressBarLayoutId, int targetColor)86 public void assertProgressBarColorCorrect(@LayoutRes int progressBarLayoutId, int targetColor) { 87 ProgressBar progressBar = mActivity.findViewById(progressBarLayoutId); 88 89 ColorStateList expected = ColorStateList.valueOf(targetColor); 90 assertThat(progressBar.getIndeterminateTintList(), equalTo(expected)); 91 assertThat(progressBar.getProgressBackgroundTintList(), equalTo(expected)); 92 } 93 bitmapEqualTo(Bitmap expected)94 private Matcher<Bitmap> bitmapEqualTo(Bitmap expected) { 95 return new BaseMatcher<Bitmap>() { 96 @Override 97 public void describeTo(Description description) { 98 description.appendText("Bitmap different from expected."); 99 } 100 101 @Override 102 public boolean matches(Object actual) { 103 return expected.sameAs((Bitmap) actual); 104 } 105 }; 106 } 107 108 private Drawable makeDefaultLogo(int color) { 109 Drawable logo = mActivity.getDrawable(R.drawable.ic_enterprise_blue_24dp); 110 logo.setColorFilter(new PorterDuffColorFilter(color, SRC_ATOP)); 111 return logo; 112 } 113 114 private Drawable makeExpectedTopInfoDrawable(int color) { 115 int swiperTheme = new SwiperThemeMatcher(mActivity, new ColorMatcher()).findTheme(color); 116 ContextThemeWrapper wrapper = new ContextThemeWrapper(mActivity, swiperTheme); 117 return mActivity.getResources().getDrawable( 118 R.drawable.topinfo_animation, wrapper.getTheme()); 119 } 120 121 private Drawable extractLogo() { 122 return ((ImageView) mActivity.findViewById( 123 com.android.setupwizardlib.R.id.suw_layout_icon)).getDrawable(); 124 } 125 126 private Bitmap drawableToBitmap(Drawable drawable) { 127 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), 128 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); 129 Canvas canvas = new Canvas(bitmap); 130 drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 131 drawable.draw(canvas); 132 return bitmap; 133 } 134 135 private void assertDrawableEquals(Drawable expected, Drawable actual) { 136 assertTrue(drawableToBitmap(expected).sameAs(drawableToBitmap(actual))); 137 } 138 }