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.annotation.SuppressLint; 26import android.content.Context; 27import android.content.res.XmlResourceParser; 28import android.support.test.InstrumentationRegistry; 29import android.support.test.filters.SmallTest; 30import android.support.test.runner.AndroidJUnit4; 31import android.util.Xml; 32import android.widget.TextView; 33 34import com.android.setupwizardlib.TemplateLayout; 35import com.android.setupwizardlib.test.R; 36 37import org.junit.Before; 38import org.junit.Test; 39import org.junit.runner.RunWith; 40import org.xmlpull.v1.XmlPullParserException; 41 42import java.io.IOException; 43 44@RunWith(AndroidJUnit4.class) 45@SmallTest 46public class HeaderMixinTest { 47 48 private Context mContext; 49 private TemplateLayout mTemplateLayout; 50 private TextView mHeaderTextView; 51 52 @Before 53 public void setUp() { 54 mContext = InstrumentationRegistry.getTargetContext(); 55 mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template, 56 R.id.suw_layout_content)); 57 58 mHeaderTextView = new TextView(mContext); 59 doReturn(mHeaderTextView).when(mTemplateLayout) 60 .findManagedViewById(eq(R.id.suw_layout_title)); 61 } 62 63 @Test 64 public void testGetTextView() { 65 HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); 66 assertSame(mHeaderTextView, mixin.getTextView()); 67 } 68 69 @Test 70 public void testSetTextId() { 71 HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); 72 mixin.setText(R.string.suw_next_button_label); 73 74 assertEquals("Next", mHeaderTextView.getText()); 75 } 76 77 @Test 78 public void testSetText() { 79 HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); 80 mixin.setText("Foobar"); 81 82 assertEquals("Foobar", mHeaderTextView.getText()); 83 } 84 85 @SuppressLint("SetTextI18n") // It's OK, this is a test 86 @Test 87 public void testGetText() { 88 mHeaderTextView.setText("Lorem ipsum"); 89 90 HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); 91 assertEquals("Lorem ipsum", mixin.getText()); 92 } 93 94 @SuppressWarnings("ResourceType") // Needed to create attribute set from layout XML. 95 @Test 96 public void testSetTextFromXml() throws IOException, XmlPullParserException { 97 final XmlResourceParser parser = 98 mContext.getResources().getXml(R.layout.test_mixin_attributes); 99 while (!TemplateLayout.class.getName().equals(parser.getName())) { 100 parser.next(); 101 } 102 new HeaderMixin(mTemplateLayout, Xml.asAttributeSet(parser), 0); 103 104 assertEquals("lorem ipsum", mHeaderTextView.getText()); 105 } 106} 107