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 17 package com.android.setupwizardlib.items; 18 19 import static org.junit.Assert.assertTrue; 20 21 import android.support.test.annotation.UiThreadTest; 22 import android.support.test.filters.SmallTest; 23 import android.support.test.rule.UiThreadTestRule; 24 import android.support.test.runner.AndroidJUnit4; 25 import android.view.ViewGroup; 26 import android.widget.Button; 27 import android.widget.LinearLayout; 28 29 import com.android.setupwizardlib.R; 30 import com.android.setupwizardlib.test.util.DrawingTestHelper; 31 32 import org.junit.Before; 33 import org.junit.Rule; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 @SmallTest 38 @RunWith(AndroidJUnit4.class) 39 public class ButtonItemDrawingTest { 40 41 private static final int GOOGLE_BLUE = 0xff4285f4; 42 43 // These tests need to be run on UI thread because button uses ValueAnimator 44 @Rule 45 public UiThreadTestRule mUiThreadTestRule = new UiThreadTestRule(); 46 47 private ViewGroup mParent; 48 49 @Before 50 public void setUp() throws Exception { 51 mParent = new LinearLayout( 52 DrawingTestHelper.createCanvasActivity(R.style.SuwThemeGlif_Light)); 53 } 54 55 @Test 56 @UiThreadTest 57 public void testColoredButtonTheme() { 58 TestButtonItem item = new TestButtonItem(); 59 item.setTheme(R.style.SuwButtonItem_Colored); 60 item.setText("foobar"); 61 62 final Button button = item.createButton(mParent); 63 64 DrawingTestHelper drawingTestHelper = new DrawingTestHelper(50, 50); 65 drawingTestHelper.drawView(button); 66 67 int googleBluePixelCount = 0; 68 for (int pixel : drawingTestHelper.getPixels()) { 69 if (pixel == GOOGLE_BLUE) { 70 googleBluePixelCount++; 71 } 72 } 73 assertTrue("> 10 pixels should be Google blue. Found " + googleBluePixelCount, 74 googleBluePixelCount > 10); 75 } 76 77 private static class TestButtonItem extends ButtonItem { 78 79 @Override 80 public Button createButton(ViewGroup parent) { 81 // Make this method public for testing 82 return super.createButton(parent); 83 } 84 } 85 } 86