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 17package com.android.setupwizardlib.items; 18 19import static org.junit.Assert.assertTrue; 20 21import android.support.annotation.StyleRes; 22import android.support.test.annotation.UiThreadTest; 23import android.support.test.filters.SmallTest; 24import android.support.test.rule.UiThreadTestRule; 25import android.support.test.runner.AndroidJUnit4; 26import android.view.ViewGroup; 27import android.widget.Button; 28import android.widget.LinearLayout; 29 30import com.android.setupwizardlib.R; 31import com.android.setupwizardlib.test.util.DrawingTestHelper; 32 33import org.junit.Rule; 34import org.junit.Test; 35import org.junit.runner.RunWith; 36 37@SmallTest 38@RunWith(AndroidJUnit4.class) 39public class ButtonItemDrawingTest { 40 41 private static final int GLIF_ACCENT_COLOR = 0xff4285f4; 42 private static final int GLIF_V3_ACCENT_COLOR = 0xff1a73e8; 43 44 // These tests need to be run on UI thread because button uses ValueAnimator 45 @Rule 46 public UiThreadTestRule mUiThreadTestRule = new UiThreadTestRule(); 47 48 @Test 49 @UiThreadTest 50 public void drawButton_glif_shouldHaveAccentColoredButton() 51 throws InstantiationException, IllegalAccessException { 52 Button button = createButton(R.style.SuwThemeGlif_Light); 53 54 DrawingTestHelper drawingTestHelper = new DrawingTestHelper(50, 50); 55 drawingTestHelper.drawView(button); 56 57 int accentPixelCount = 58 countPixelsWithColor(drawingTestHelper.getPixels(), GLIF_ACCENT_COLOR); 59 assertTrue("> 10 pixels should be #4285f4. Found " + accentPixelCount, 60 accentPixelCount > 10); 61 } 62 63 @Test 64 @UiThreadTest 65 public void drawButton_glifV3_shouldHaveAccentColoredButton() 66 throws InstantiationException, IllegalAccessException { 67 Button button = createButton(R.style.SuwThemeGlifV3_Light); 68 69 DrawingTestHelper drawingTestHelper = new DrawingTestHelper(50, 50); 70 drawingTestHelper.drawView(button); 71 72 int accentPixelCount = 73 countPixelsWithColor(drawingTestHelper.getPixels(), GLIF_V3_ACCENT_COLOR); 74 assertTrue("> 10 pixels should be #1a73e8. Found " + accentPixelCount, 75 accentPixelCount > 10); 76 } 77 78 private Button createButton(@StyleRes int theme) 79 throws InstantiationException, IllegalAccessException { 80 final ViewGroup parent = new LinearLayout(DrawingTestHelper.createCanvasActivity(theme)); 81 TestButtonItem item = new TestButtonItem(); 82 item.setTheme(R.style.SuwButtonItem_Colored); 83 item.setText("foobar"); 84 85 return item.createButton(parent); 86 } 87 88 private int countPixelsWithColor(int[] pixels, int color) { 89 int count = 0; 90 for (int pixel : pixels) { 91 if (pixel == color) { 92 count++; 93 } 94 } 95 return count; 96 } 97 98 private static class TestButtonItem extends ButtonItem { 99 100 @Override 101 public Button createButton(ViewGroup parent) { 102 // Make this method public for testing 103 return super.createButton(parent); 104 } 105 } 106} 107