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 */
16package com.android.managedprovisioning.common;
17
18import static android.graphics.PorterDuff.Mode.SRC_ATOP;
19import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
20
21import static org.hamcrest.CoreMatchers.equalTo;
22import static org.hamcrest.CoreMatchers.instanceOf;
23
24import android.app.Activity;
25import android.content.res.ColorStateList;
26import android.graphics.Bitmap;
27import android.graphics.PorterDuffColorFilter;
28import android.graphics.drawable.BitmapDrawable;
29import android.graphics.drawable.ColorDrawable;
30import android.graphics.drawable.Drawable;
31import android.widget.ImageView;
32import android.widget.ProgressBar;
33
34import com.android.managedprovisioning.R;
35import com.android.managedprovisioning.preprovisioning.anim.ColorMatcher;
36import com.android.managedprovisioning.preprovisioning.anim.SwiperThemeMatcher;
37
38import org.hamcrest.BaseMatcher;
39import org.hamcrest.Description;
40import org.hamcrest.Matcher;
41
42public class CustomizationVerifier {
43    private final Activity mActivity;
44
45    public CustomizationVerifier(Activity activity) {
46        mActivity = activity;
47    }
48
49    public void assertStatusBarColorCorrect(int targetColor) {
50        int statusBarColor = mActivity.getWindow().getStatusBarColor();
51        assertThat(statusBarColor, equalTo(targetColor));
52    }
53
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
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
67    public void assertCustomLogoCorrect(Bitmap targetLogo) {
68        Bitmap actualLogo = ((BitmapDrawable) extractLogo()).getBitmap();
69        assertThat(targetLogo, bitmapEqualTo(actualLogo));
70    }
71
72    public void assertNextButtonColorCorrect(int targetColor) {
73        ColorStateList actual = mActivity.findViewById(R.id.next_button).getBackgroundTintList();
74        ColorStateList expected = ColorStateList.valueOf(targetColor);
75        assertThat(actual, equalTo(expected));
76    }
77
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
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}