12831318df83781bb5bada256e35726612d225052Maurice Lam/*
22831318df83781bb5bada256e35726612d225052Maurice Lam * Copyright (C) 2017 The Android Open Source Project
32831318df83781bb5bada256e35726612d225052Maurice Lam *
42831318df83781bb5bada256e35726612d225052Maurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
52831318df83781bb5bada256e35726612d225052Maurice Lam * you may not use this file except in compliance with the License.
62831318df83781bb5bada256e35726612d225052Maurice Lam * You may obtain a copy of the License at
72831318df83781bb5bada256e35726612d225052Maurice Lam *
82831318df83781bb5bada256e35726612d225052Maurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
92831318df83781bb5bada256e35726612d225052Maurice Lam *
102831318df83781bb5bada256e35726612d225052Maurice Lam * Unless required by applicable law or agreed to in writing, software
112831318df83781bb5bada256e35726612d225052Maurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
122831318df83781bb5bada256e35726612d225052Maurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132831318df83781bb5bada256e35726612d225052Maurice Lam * See the License for the specific language governing permissions and
142831318df83781bb5bada256e35726612d225052Maurice Lam * limitations under the License.
152831318df83781bb5bada256e35726612d225052Maurice Lam */
162831318df83781bb5bada256e35726612d225052Maurice Lam
172831318df83781bb5bada256e35726612d225052Maurice Lampackage com.android.setupwizardlib.items;
182831318df83781bb5bada256e35726612d225052Maurice Lam
192831318df83781bb5bada256e35726612d225052Maurice Lamimport static org.junit.Assert.assertTrue;
202831318df83781bb5bada256e35726612d225052Maurice Lam
21e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lamimport android.support.annotation.StyleRes;
222831318df83781bb5bada256e35726612d225052Maurice Lamimport android.support.test.annotation.UiThreadTest;
232831318df83781bb5bada256e35726612d225052Maurice Lamimport android.support.test.filters.SmallTest;
242831318df83781bb5bada256e35726612d225052Maurice Lamimport android.support.test.rule.UiThreadTestRule;
252831318df83781bb5bada256e35726612d225052Maurice Lamimport android.support.test.runner.AndroidJUnit4;
262831318df83781bb5bada256e35726612d225052Maurice Lamimport android.view.ViewGroup;
272831318df83781bb5bada256e35726612d225052Maurice Lamimport android.widget.Button;
282831318df83781bb5bada256e35726612d225052Maurice Lamimport android.widget.LinearLayout;
292831318df83781bb5bada256e35726612d225052Maurice Lam
302831318df83781bb5bada256e35726612d225052Maurice Lamimport com.android.setupwizardlib.R;
312831318df83781bb5bada256e35726612d225052Maurice Lamimport com.android.setupwizardlib.test.util.DrawingTestHelper;
322831318df83781bb5bada256e35726612d225052Maurice Lam
332831318df83781bb5bada256e35726612d225052Maurice Lamimport org.junit.Rule;
342831318df83781bb5bada256e35726612d225052Maurice Lamimport org.junit.Test;
352831318df83781bb5bada256e35726612d225052Maurice Lamimport org.junit.runner.RunWith;
362831318df83781bb5bada256e35726612d225052Maurice Lam
372831318df83781bb5bada256e35726612d225052Maurice Lam@SmallTest
382831318df83781bb5bada256e35726612d225052Maurice Lam@RunWith(AndroidJUnit4.class)
392831318df83781bb5bada256e35726612d225052Maurice Lampublic class ButtonItemDrawingTest {
402831318df83781bb5bada256e35726612d225052Maurice Lam
41e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam    private static final int GLIF_ACCENT_COLOR = 0xff4285f4;
42e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam    private static final int GLIF_V3_ACCENT_COLOR = 0xff1a73e8;
432831318df83781bb5bada256e35726612d225052Maurice Lam
442831318df83781bb5bada256e35726612d225052Maurice Lam    // These tests need to be run on UI thread because button uses ValueAnimator
452831318df83781bb5bada256e35726612d225052Maurice Lam    @Rule
462831318df83781bb5bada256e35726612d225052Maurice Lam    public UiThreadTestRule mUiThreadTestRule = new UiThreadTestRule();
472831318df83781bb5bada256e35726612d225052Maurice Lam
48e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam    @Test
49e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam    @UiThreadTest
50e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam    public void drawButton_glif_shouldHaveAccentColoredButton()
51e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam            throws InstantiationException, IllegalAccessException {
52e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        Button button = createButton(R.style.SuwThemeGlif_Light);
53e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam
54e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        DrawingTestHelper drawingTestHelper = new DrawingTestHelper(50, 50);
55e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        drawingTestHelper.drawView(button);
562831318df83781bb5bada256e35726612d225052Maurice Lam
57e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        int accentPixelCount =
58e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam                countPixelsWithColor(drawingTestHelper.getPixels(), GLIF_ACCENT_COLOR);
59e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        assertTrue("> 10 pixels should be #4285f4. Found " + accentPixelCount,
60e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam                accentPixelCount > 10);
612831318df83781bb5bada256e35726612d225052Maurice Lam    }
622831318df83781bb5bada256e35726612d225052Maurice Lam
632831318df83781bb5bada256e35726612d225052Maurice Lam    @Test
642831318df83781bb5bada256e35726612d225052Maurice Lam    @UiThreadTest
65e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam    public void drawButton_glifV3_shouldHaveAccentColoredButton()
66e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam            throws InstantiationException, IllegalAccessException {
67e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        Button button = createButton(R.style.SuwThemeGlifV3_Light);
68e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam
69e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        DrawingTestHelper drawingTestHelper = new DrawingTestHelper(50, 50);
70e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        drawingTestHelper.drawView(button);
71e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam
72e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        int accentPixelCount =
73e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam                countPixelsWithColor(drawingTestHelper.getPixels(), GLIF_V3_ACCENT_COLOR);
74e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        assertTrue("> 10 pixels should be #1a73e8. Found " + accentPixelCount,
75e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam                accentPixelCount > 10);
76e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam    }
77e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam
78e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam    private Button createButton(@StyleRes int theme)
79e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam            throws InstantiationException, IllegalAccessException {
80e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        final ViewGroup parent = new LinearLayout(DrawingTestHelper.createCanvasActivity(theme));
812831318df83781bb5bada256e35726612d225052Maurice Lam        TestButtonItem item = new TestButtonItem();
822831318df83781bb5bada256e35726612d225052Maurice Lam        item.setTheme(R.style.SuwButtonItem_Colored);
832831318df83781bb5bada256e35726612d225052Maurice Lam        item.setText("foobar");
842831318df83781bb5bada256e35726612d225052Maurice Lam
85e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        return item.createButton(parent);
86e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam    }
872831318df83781bb5bada256e35726612d225052Maurice Lam
88e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam    private int countPixelsWithColor(int[] pixels, int color) {
89e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        int count = 0;
90e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        for (int pixel : pixels) {
91e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam            if (pixel == color) {
92e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam                count++;
932831318df83781bb5bada256e35726612d225052Maurice Lam            }
942831318df83781bb5bada256e35726612d225052Maurice Lam        }
95e22d7c2b358d9c0f362627d3194b051719f2ba5cMaurice Lam        return count;
962831318df83781bb5bada256e35726612d225052Maurice Lam    }
972831318df83781bb5bada256e35726612d225052Maurice Lam
982831318df83781bb5bada256e35726612d225052Maurice Lam    private static class TestButtonItem extends ButtonItem {
992831318df83781bb5bada256e35726612d225052Maurice Lam
1002831318df83781bb5bada256e35726612d225052Maurice Lam        @Override
1012831318df83781bb5bada256e35726612d225052Maurice Lam        public Button createButton(ViewGroup parent) {
1022831318df83781bb5bada256e35726612d225052Maurice Lam            // Make this method public for testing
1032831318df83781bb5bada256e35726612d225052Maurice Lam            return super.createButton(parent);
1042831318df83781bb5bada256e35726612d225052Maurice Lam        }
1052831318df83781bb5bada256e35726612d225052Maurice Lam    }
1062831318df83781bb5bada256e35726612d225052Maurice Lam}
107