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.template; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertSame; 21import static org.mockito.Matchers.eq; 22import static org.mockito.Mockito.doReturn; 23import static org.mockito.Mockito.spy; 24 25import android.content.Context; 26import android.content.res.XmlResourceParser; 27import android.graphics.Color; 28import android.graphics.drawable.BitmapDrawable; 29import android.graphics.drawable.ColorDrawable; 30import android.support.test.InstrumentationRegistry; 31import android.support.test.filters.SmallTest; 32import android.support.test.runner.AndroidJUnit4; 33import android.util.Xml; 34import android.widget.ImageView; 35 36import com.android.setupwizardlib.TemplateLayout; 37import com.android.setupwizardlib.test.R; 38 39import org.junit.Before; 40import org.junit.Test; 41import org.junit.runner.RunWith; 42import org.xmlpull.v1.XmlPullParserException; 43 44import java.io.IOException; 45 46@RunWith(AndroidJUnit4.class) 47@SmallTest 48public class IconMixinTest { 49 50 private Context mContext; 51 private TemplateLayout mTemplateLayout; 52 private ImageView mIconView; 53 54 @Before 55 public void setUp() { 56 mContext = InstrumentationRegistry.getContext(); 57 mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template, 58 R.id.suw_layout_content)); 59 60 mIconView = new ImageView(mContext); 61 doReturn(mIconView).when(mTemplateLayout).findManagedViewById(eq(R.id.suw_layout_icon)); 62 } 63 64 @Test 65 public void testGetIconView() { 66 IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); 67 assertSame(mIconView, mixin.getView()); 68 } 69 70 @Test 71 public void testSetIcon() { 72 final ColorDrawable drawable = new ColorDrawable(Color.CYAN); 73 IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); 74 mixin.setIcon(drawable); 75 76 assertSame(drawable, mIconView.getDrawable()); 77 } 78 79 @Test 80 public void testGetIcon() { 81 final ColorDrawable drawable = new ColorDrawable(Color.BLUE); 82 mIconView.setImageDrawable(drawable); 83 84 IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); 85 assertSame(drawable, mixin.getIcon()); 86 } 87 88 @SuppressWarnings("ResourceType") // Needed to create attribute set from layout XML. 89 @Test 90 public void testSetIconFromXml() throws IOException, XmlPullParserException { 91 final XmlResourceParser parser = 92 mContext.getResources().getXml(R.layout.test_mixin_attributes); 93 while (!TemplateLayout.class.getName().equals(parser.getName())) { 94 parser.next(); 95 } 96 new IconMixin(mTemplateLayout, Xml.asAttributeSet(parser), 0); 97 98 // Check that the bitmaps themselves are equal because BitmapDrawable does not implement 99 // equals() 100 final BitmapDrawable expected = (BitmapDrawable) mContext.getResources() 101 .getDrawable(android.R.drawable.ic_menu_add); 102 final BitmapDrawable actual = (BitmapDrawable) mIconView.getDrawable(); 103 assertEquals(expected.getBitmap(), actual.getBitmap()); 104 } 105} 106