diff options
Diffstat (limited to 'library/test/instrumentation')
30 files changed, 2420 insertions, 2469 deletions
diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/TemplateLayoutTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/TemplateLayoutTest.java index ddce677..a5026ee 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/TemplateLayoutTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/TemplateLayoutTest.java @@ -23,16 +23,14 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.template.HeaderMixin; import com.android.setupwizardlib.test.R; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,65 +39,64 @@ import org.junit.runner.RunWith; @SmallTest public class TemplateLayoutTest { - private Context mContext; + private Context mContext; - @Before - public void setUp() throws Exception { - mContext = InstrumentationRegistry.getContext(); - } + @Before + public void setUp() throws Exception { + mContext = InstrumentationRegistry.getContext(); + } - @Test - public void testAddView() { - TemplateLayout layout = new TemplateLayout(mContext, R.layout.test_template, - R.id.suw_layout_content); - TextView tv = new TextView(mContext); - tv.setId(R.id.test_view_id); - layout.addView(tv); - View view = layout.findViewById(R.id.test_view_id); - assertSame("The view added should be the same text view", tv, view); - } + @Test + public void testAddView() { + TemplateLayout layout = + new TemplateLayout(mContext, R.layout.test_template, R.id.suw_layout_content); + TextView tv = new TextView(mContext); + tv.setId(R.id.test_view_id); + layout.addView(tv); + View view = layout.findViewById(R.id.test_view_id); + assertSame("The view added should be the same text view", tv, view); + } - @Test - public void testInflateFromXml() { - LayoutInflater inflater = LayoutInflater.from(mContext); - TemplateLayout layout = - (TemplateLayout) inflater.inflate(R.layout.test_template_layout, null); - View content = layout.findViewById(R.id.test_content); - assertTrue("@id/test_content should be a TextView", content instanceof TextView); - } + @Test + public void testInflateFromXml() { + LayoutInflater inflater = LayoutInflater.from(mContext); + TemplateLayout layout = (TemplateLayout) inflater.inflate(R.layout.test_template_layout, null); + View content = layout.findViewById(R.id.test_content); + assertTrue("@id/test_content should be a TextView", content instanceof TextView); + } - @Test - public void testTemplate() { - TemplateLayout layout = new TemplateLayout(mContext, R.layout.test_template, - R.id.suw_layout_content); - View templateView = layout.findViewById(R.id.test_template_view); - assertNotNull("@id/test_template_view should exist in template", templateView); + @Test + public void testTemplate() { + TemplateLayout layout = + new TemplateLayout(mContext, R.layout.test_template, R.id.suw_layout_content); + View templateView = layout.findViewById(R.id.test_template_view); + assertNotNull("@id/test_template_view should exist in template", templateView); - TextView tv = new TextView(mContext); - tv.setId(R.id.test_view_id); - layout.addView(tv); + TextView tv = new TextView(mContext); + tv.setId(R.id.test_view_id); + layout.addView(tv); - templateView = layout.findViewById(R.id.test_template_view); - assertNotNull("@id/test_template_view should exist in template", templateView); - View contentView = layout.findViewById(R.id.test_view_id); - assertSame("The view added should be the same text view", tv, contentView); - } + templateView = layout.findViewById(R.id.test_template_view); + assertNotNull("@id/test_template_view should exist in template", templateView); + View contentView = layout.findViewById(R.id.test_view_id); + assertSame("The view added should be the same text view", tv, contentView); + } - @Test - public void testNoTemplate() { - try { - new TemplateLayout(mContext, 0, 0); - fail("Inflating TemplateLayout without template should throw exception"); - } catch (IllegalArgumentException e) { - // Expected IllegalArgumentException - } + @Test + public void testNoTemplate() { + try { + new TemplateLayout(mContext, 0, 0); + fail("Inflating TemplateLayout without template should throw exception"); + } catch (IllegalArgumentException e) { + // Expected IllegalArgumentException } + } - @Test - public void testGetMixin() { - TemplateLayout layout = new TemplateLayout(mContext, R.layout.test_template, - R.id.suw_layout_content); - final HeaderMixin mixin = layout.getMixin(HeaderMixin.class); - assertNull("getMixin for a mixin that doesn't exist should return null", mixin); - } + @Test + public void testGetMixin() { + TemplateLayout layout = + new TemplateLayout(mContext, R.layout.test_template, R.id.suw_layout_content); + final HeaderMixin mixin = layout.getMixin(HeaderMixin.class); + assertNull("getMixin for a mixin that doesn't exist should return null", mixin); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/template/ButtonFooterMixinTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/template/ButtonFooterMixinTest.java index 08f5958..971211b 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/template/ButtonFooterMixinTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/template/ButtonFooterMixinTest.java @@ -24,9 +24,7 @@ import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; +import androidx.annotation.IdRes; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.View; @@ -34,12 +32,11 @@ import android.view.ViewStub; import android.widget.Button; import android.widget.FrameLayout; import android.widget.LinearLayout; - -import androidx.annotation.IdRes; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.TemplateLayout; import com.android.setupwizardlib.test.R; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -48,125 +45,128 @@ import org.junit.runner.RunWith; @SmallTest public class ButtonFooterMixinTest { - private Context mContext; - private TemplateLayout mTemplateLayout; - - // The parent view to contain the view stub and views it inflates. - private FrameLayout mStubParent; - private ViewStub mFooterStub; - - @Before - public void setUp() { - mContext = InstrumentationRegistry.getTargetContext(); - mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template, - R.id.suw_layout_content)); - - mFooterStub = new ViewStub(mContext, R.layout.suw_glif_footer_button_bar); - mStubParent = new FrameLayout(mContext); - mStubParent.addView(mFooterStub); - doReturn(mFooterStub).when(mTemplateLayout).findManagedViewById(eq(R.id.suw_layout_footer)); - } - - @Test - public void testAddButton() { - ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); - final Button button = mixin.addButton("foobar", R.style.SuwGlifButton_Primary); - - assertNotNull(button); - @IdRes final int id = 12345; - button.setId(id); - assertNotNull(mStubParent.findViewById(id)); - - assertEquals("foobar", button.getText()); - - // Make sure the style is applied by checking the paddings - assertEquals(dp2Px(16), button.getPaddingLeft()); - assertEquals(dp2Px(16), button.getPaddingRight()); - } - - @Test - public void testAddButtonTextRes() { - ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); - final Button button = mixin.addButton(R.string.suw_next_button_label, - R.style.SuwGlifButton_Primary); - - assertNotNull(button); - button.setTag("button"); - assertNotNull(mStubParent.findViewWithTag("button")); - - assertEquals("Next", button.getText()); - - // Make sure the style is applied by checking the paddings - assertEquals(dp2Px(16), button.getPaddingLeft()); - assertEquals(dp2Px(16), button.getPaddingRight()); - } - - @Test - public void testAddSpace() { - ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); - mixin.addButton("foo", R.style.SuwGlifButton_Secondary); - final View space = mixin.addSpace(); - mixin.addButton("bar", R.style.SuwGlifButton_Primary); - - space.setTag("space"); - assertNotNull(mStubParent.findViewWithTag("space")); - assertEquals("Space should have weight of 1", - 1f, ((LinearLayout.LayoutParams) space.getLayoutParams()).weight, 0.001); - } - - @Test - public void testRemoveButton() { - ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); - final Button fooButton = mixin.addButton("foo", R.style.SuwGlifButton_Secondary); - final Button barButton = mixin.addButton("bar", R.style.SuwGlifButton_Secondary); - - fooButton.setTag("foo"); - barButton.setTag("bar"); - assertNotNull("Foo button should exist", mStubParent.findViewWithTag("foo")); - assertNotNull("Bar button should exist", mStubParent.findViewWithTag("bar")); - - mixin.removeButton(fooButton); - - assertNull("Foo button should be removed", mStubParent.findViewWithTag("foo")); - assertNotNull("Bar button should not be removed", mStubParent.findViewWithTag("bar")); - } - - @Test - public void testRemoveSpace() { - ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); - final Button fooButton = mixin.addButton("foo", R.style.SuwGlifButton_Secondary); - final View space = mixin.addSpace(); - - fooButton.setTag("foo"); - space.setTag("space"); - assertNotNull("Foo button should exist", mStubParent.findViewWithTag("foo")); - assertNotNull("space should exist", mStubParent.findViewWithTag("space")); - - mixin.removeSpace(space); - - assertNotNull("Foo button should not be removed", mStubParent.findViewWithTag("foo")); - assertNull("Space should be removed", mStubParent.findViewWithTag("space")); - } - - @Test - public void testRemoveAllViews() { - ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); - final Button fooButton = mixin.addButton("foo", R.style.SuwGlifButton_Secondary); - final View space = mixin.addSpace(); - - fooButton.setTag("foo"); - space.setTag("space"); - assertNotNull("Foo button should exist", mStubParent.findViewWithTag("foo")); - assertNotNull("space should exist", mStubParent.findViewWithTag("space")); - - mixin.removeAllViews(); - - assertNull("Foo button should be removed", mStubParent.findViewWithTag("foo")); - assertNull("Space should be removed", mStubParent.findViewWithTag("space")); - } - - private int dp2Px(float dp) { - DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); - return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics); - } + private Context mContext; + private TemplateLayout mTemplateLayout; + + // The parent view to contain the view stub and views it inflates. + private FrameLayout mStubParent; + private ViewStub mFooterStub; + + @Before + public void setUp() { + mContext = InstrumentationRegistry.getTargetContext(); + mTemplateLayout = + spy(new TemplateLayout(mContext, R.layout.test_template, R.id.suw_layout_content)); + + mFooterStub = new ViewStub(mContext, R.layout.suw_glif_footer_button_bar); + mStubParent = new FrameLayout(mContext); + mStubParent.addView(mFooterStub); + doReturn(mFooterStub).when(mTemplateLayout).findManagedViewById(eq(R.id.suw_layout_footer)); + } + + @Test + public void testAddButton() { + ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); + final Button button = mixin.addButton("foobar", R.style.SuwGlifButton_Primary); + + assertNotNull(button); + @IdRes final int id = 12345; + button.setId(id); + assertNotNull(mStubParent.findViewById(id)); + + assertEquals("foobar", button.getText()); + + // Make sure the style is applied by checking the paddings + assertEquals(dp2Px(16), button.getPaddingLeft()); + assertEquals(dp2Px(16), button.getPaddingRight()); + } + + @Test + public void testAddButtonTextRes() { + ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); + final Button button = + mixin.addButton(R.string.suw_next_button_label, R.style.SuwGlifButton_Primary); + + assertNotNull(button); + button.setTag("button"); + assertNotNull(mStubParent.findViewWithTag("button")); + + assertEquals("Next", button.getText()); + + // Make sure the style is applied by checking the paddings + assertEquals(dp2Px(16), button.getPaddingLeft()); + assertEquals(dp2Px(16), button.getPaddingRight()); + } + + @Test + public void testAddSpace() { + ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); + mixin.addButton("foo", R.style.SuwGlifButton_Secondary); + final View space = mixin.addSpace(); + mixin.addButton("bar", R.style.SuwGlifButton_Primary); + + space.setTag("space"); + assertNotNull(mStubParent.findViewWithTag("space")); + assertEquals( + "Space should have weight of 1", + 1f, + ((LinearLayout.LayoutParams) space.getLayoutParams()).weight, + 0.001); + } + + @Test + public void testRemoveButton() { + ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); + final Button fooButton = mixin.addButton("foo", R.style.SuwGlifButton_Secondary); + final Button barButton = mixin.addButton("bar", R.style.SuwGlifButton_Secondary); + + fooButton.setTag("foo"); + barButton.setTag("bar"); + assertNotNull("Foo button should exist", mStubParent.findViewWithTag("foo")); + assertNotNull("Bar button should exist", mStubParent.findViewWithTag("bar")); + + mixin.removeButton(fooButton); + + assertNull("Foo button should be removed", mStubParent.findViewWithTag("foo")); + assertNotNull("Bar button should not be removed", mStubParent.findViewWithTag("bar")); + } + + @Test + public void testRemoveSpace() { + ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); + final Button fooButton = mixin.addButton("foo", R.style.SuwGlifButton_Secondary); + final View space = mixin.addSpace(); + + fooButton.setTag("foo"); + space.setTag("space"); + assertNotNull("Foo button should exist", mStubParent.findViewWithTag("foo")); + assertNotNull("space should exist", mStubParent.findViewWithTag("space")); + + mixin.removeSpace(space); + + assertNotNull("Foo button should not be removed", mStubParent.findViewWithTag("foo")); + assertNull("Space should be removed", mStubParent.findViewWithTag("space")); + } + + @Test + public void testRemoveAllViews() { + ButtonFooterMixin mixin = new ButtonFooterMixin(mTemplateLayout); + final Button fooButton = mixin.addButton("foo", R.style.SuwGlifButton_Secondary); + final View space = mixin.addSpace(); + + fooButton.setTag("foo"); + space.setTag("space"); + assertNotNull("Foo button should exist", mStubParent.findViewWithTag("foo")); + assertNotNull("space should exist", mStubParent.findViewWithTag("space")); + + mixin.removeAllViews(); + + assertNull("Foo button should be removed", mStubParent.findViewWithTag("foo")); + assertNull("Space should be removed", mStubParent.findViewWithTag("space")); + } + + private int dp2Px(float dp) { + DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/template/ColoredHeaderMixinTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/template/ColoredHeaderMixinTest.java index 1c86af1..3ea8f6e 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/template/ColoredHeaderMixinTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/template/ColoredHeaderMixinTest.java @@ -25,67 +25,62 @@ import android.content.Context; import android.content.res.ColorStateList; import android.content.res.XmlResourceParser; import android.graphics.Color; +import android.util.Xml; +import android.widget.TextView; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; -import android.util.Xml; -import android.widget.TextView; - import com.android.setupwizardlib.TemplateLayout; import com.android.setupwizardlib.test.R; - +import java.io.IOException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.xmlpull.v1.XmlPullParserException; -import java.io.IOException; - @RunWith(AndroidJUnit4.class) @SmallTest public class ColoredHeaderMixinTest { - private Context mContext; - private TemplateLayout mTemplateLayout; - private TextView mHeaderTextView; - - @Before - public void setUp() { - mContext = InstrumentationRegistry.getTargetContext(); - mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template, - R.id.suw_layout_content)); - - mHeaderTextView = new TextView(mContext); - doReturn(mHeaderTextView).when(mTemplateLayout) - .findManagedViewById(eq(R.id.suw_layout_title)); + private Context mContext; + private TemplateLayout mTemplateLayout; + private TextView mHeaderTextView; + + @Before + public void setUp() { + mContext = InstrumentationRegistry.getTargetContext(); + mTemplateLayout = + spy(new TemplateLayout(mContext, R.layout.test_template, R.id.suw_layout_content)); + + mHeaderTextView = new TextView(mContext); + doReturn(mHeaderTextView).when(mTemplateLayout).findManagedViewById(eq(R.id.suw_layout_title)); + } + + @Test + public void testSetColor() { + ColoredHeaderMixin mixin = new ColoredHeaderMixin(mTemplateLayout, null, 0); + mixin.setColor(ColorStateList.valueOf(Color.MAGENTA)); + + assertEquals(ColorStateList.valueOf(Color.MAGENTA), mHeaderTextView.getTextColors()); + } + + @Test + public void testGetColor() { + ColoredHeaderMixin mixin = new ColoredHeaderMixin(mTemplateLayout, null, 0); + mHeaderTextView.setTextColor(ColorStateList.valueOf(Color.GREEN)); + + assertEquals(ColorStateList.valueOf(Color.GREEN), mixin.getColor()); + } + + @SuppressWarnings("ResourceType") // Needed to create attribute set from layout XML. + @Test + public void testSetColorFromXml() throws IOException, XmlPullParserException { + final XmlResourceParser parser = mContext.getResources().getXml(R.layout.test_mixin_attributes); + while (!TemplateLayout.class.getName().equals(parser.getName())) { + parser.next(); } + new ColoredHeaderMixin(mTemplateLayout, Xml.asAttributeSet(parser), 0); - @Test - public void testSetColor() { - ColoredHeaderMixin mixin = new ColoredHeaderMixin(mTemplateLayout, null, 0); - mixin.setColor(ColorStateList.valueOf(Color.MAGENTA)); - - assertEquals(ColorStateList.valueOf(Color.MAGENTA), mHeaderTextView.getTextColors()); - } - - @Test - public void testGetColor() { - ColoredHeaderMixin mixin = new ColoredHeaderMixin(mTemplateLayout, null, 0); - mHeaderTextView.setTextColor(ColorStateList.valueOf(Color.GREEN)); - - assertEquals(ColorStateList.valueOf(Color.GREEN), mixin.getColor()); - } - - @SuppressWarnings("ResourceType") // Needed to create attribute set from layout XML. - @Test - public void testSetColorFromXml() throws IOException, XmlPullParserException { - final XmlResourceParser parser = - mContext.getResources().getXml(R.layout.test_mixin_attributes); - while (!TemplateLayout.class.getName().equals(parser.getName())) { - parser.next(); - } - new ColoredHeaderMixin(mTemplateLayout, Xml.asAttributeSet(parser), 0); - - assertEquals(ColorStateList.valueOf(Color.RED), mHeaderTextView.getTextColors()); - } + assertEquals(ColorStateList.valueOf(Color.RED), mHeaderTextView.getTextColors()); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/template/HeaderMixinTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/template/HeaderMixinTest.java index a1b4b59..211f95f 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/template/HeaderMixinTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/template/HeaderMixinTest.java @@ -25,82 +25,77 @@ import static org.mockito.Mockito.spy; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.XmlResourceParser; +import android.util.Xml; +import android.widget.TextView; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; -import android.util.Xml; -import android.widget.TextView; - import com.android.setupwizardlib.TemplateLayout; import com.android.setupwizardlib.test.R; - +import java.io.IOException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.xmlpull.v1.XmlPullParserException; -import java.io.IOException; - @RunWith(AndroidJUnit4.class) @SmallTest public class HeaderMixinTest { - private Context mContext; - private TemplateLayout mTemplateLayout; - private TextView mHeaderTextView; - - @Before - public void setUp() { - mContext = InstrumentationRegistry.getTargetContext(); - mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template, - R.id.suw_layout_content)); - - mHeaderTextView = new TextView(mContext); - doReturn(mHeaderTextView).when(mTemplateLayout) - .findManagedViewById(eq(R.id.suw_layout_title)); + private Context mContext; + private TemplateLayout mTemplateLayout; + private TextView mHeaderTextView; + + @Before + public void setUp() { + mContext = InstrumentationRegistry.getTargetContext(); + mTemplateLayout = + spy(new TemplateLayout(mContext, R.layout.test_template, R.id.suw_layout_content)); + + mHeaderTextView = new TextView(mContext); + doReturn(mHeaderTextView).when(mTemplateLayout).findManagedViewById(eq(R.id.suw_layout_title)); + } + + @Test + public void testGetTextView() { + HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); + assertSame(mHeaderTextView, mixin.getTextView()); + } + + @Test + public void testSetTextId() { + HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); + mixin.setText(R.string.suw_next_button_label); + + assertEquals("Next", mHeaderTextView.getText()); + } + + @Test + public void testSetText() { + HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); + mixin.setText("Foobar"); + + assertEquals("Foobar", mHeaderTextView.getText()); + } + + @SuppressLint("SetTextI18n") // It's OK, this is a test + @Test + public void testGetText() { + mHeaderTextView.setText("Lorem ipsum"); + + HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); + assertEquals("Lorem ipsum", mixin.getText()); + } + + @SuppressWarnings("ResourceType") // Needed to create attribute set from layout XML. + @Test + public void testSetTextFromXml() throws IOException, XmlPullParserException { + final XmlResourceParser parser = mContext.getResources().getXml(R.layout.test_mixin_attributes); + while (!TemplateLayout.class.getName().equals(parser.getName())) { + parser.next(); } + new HeaderMixin(mTemplateLayout, Xml.asAttributeSet(parser), 0); - @Test - public void testGetTextView() { - HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); - assertSame(mHeaderTextView, mixin.getTextView()); - } - - @Test - public void testSetTextId() { - HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); - mixin.setText(R.string.suw_next_button_label); - - assertEquals("Next", mHeaderTextView.getText()); - } - - @Test - public void testSetText() { - HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); - mixin.setText("Foobar"); - - assertEquals("Foobar", mHeaderTextView.getText()); - } - - @SuppressLint("SetTextI18n") // It's OK, this is a test - @Test - public void testGetText() { - mHeaderTextView.setText("Lorem ipsum"); - - HeaderMixin mixin = new HeaderMixin(mTemplateLayout, null, 0); - assertEquals("Lorem ipsum", mixin.getText()); - } - - @SuppressWarnings("ResourceType") // Needed to create attribute set from layout XML. - @Test - public void testSetTextFromXml() throws IOException, XmlPullParserException { - final XmlResourceParser parser = - mContext.getResources().getXml(R.layout.test_mixin_attributes); - while (!TemplateLayout.class.getName().equals(parser.getName())) { - parser.next(); - } - new HeaderMixin(mTemplateLayout, Xml.asAttributeSet(parser), 0); - - assertEquals("lorem ipsum", mHeaderTextView.getText()); - } + assertEquals("lorem ipsum", mHeaderTextView.getText()); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/template/IconMixinTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/template/IconMixinTest.java index 5a36f4a..001fe33 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/template/IconMixinTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/template/IconMixinTest.java @@ -17,7 +17,6 @@ package com.android.setupwizardlib.template; import static com.google.common.truth.Truth.assertThat; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import static org.mockito.Matchers.eq; @@ -30,115 +29,111 @@ import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; import android.util.Xml; import android.view.View; import android.widget.ImageView; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.TemplateLayout; import com.android.setupwizardlib.test.R; - +import java.io.IOException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.xmlpull.v1.XmlPullParserException; -import java.io.IOException; - @RunWith(AndroidJUnit4.class) @SmallTest public class IconMixinTest { - private Context mContext; - private TemplateLayout mTemplateLayout; - private ImageView mIconView; - - @Before - public void setUp() { - mContext = InstrumentationRegistry.getContext(); - mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template, - R.id.suw_layout_content)); - - mIconView = new ImageView(mContext); - doReturn(mIconView).when(mTemplateLayout).findManagedViewById(eq(R.id.suw_layout_icon)); - } - - @Test - public void testGetIconView() { - IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); - assertSame(mIconView, mixin.getView()); - } - - @Test - public void testSetIcon() { - final ColorDrawable drawable = new ColorDrawable(Color.CYAN); - IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); - mixin.setIcon(drawable); - - assertSame(drawable, mIconView.getDrawable()); - assertEquals(View.VISIBLE, mIconView.getVisibility()); - } - - @Test - public void setIcon_resourceId_shouldSetIcon() { - int icon = android.R.drawable.ic_menu_add; - IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); - mixin.setIcon(icon); - - Drawable drawable = mIconView.getDrawable(); - assertThat(drawable).isInstanceOf(BitmapDrawable.class); - assertEquals(View.VISIBLE, mIconView.getVisibility()); - } - - @Test - public void setIcon_shouldSetVisibilityToGone_whenIconIsNull() { - IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); - mixin.setIcon(null); - - assertEquals(View.GONE, mIconView.getVisibility()); - } - - @Test - public void testGetIcon() { - final ColorDrawable drawable = new ColorDrawable(Color.BLUE); - mIconView.setImageDrawable(drawable); - - IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); - assertSame(drawable, mixin.getIcon()); - } - - @SuppressWarnings("ResourceType") // Needed to create attribute set from layout XML. - @Test - public void testSetIconFromXml() throws IOException, XmlPullParserException { - final XmlResourceParser parser = - mContext.getResources().getXml(R.layout.test_mixin_attributes); - while (!TemplateLayout.class.getName().equals(parser.getName())) { - parser.next(); - } - new IconMixin(mTemplateLayout, Xml.asAttributeSet(parser), 0); - - // Check that the bitmaps themselves are equal because BitmapDrawable does not implement - // equals() - final BitmapDrawable expected = (BitmapDrawable) mContext.getResources() - .getDrawable(android.R.drawable.ic_menu_add); - final BitmapDrawable actual = (BitmapDrawable) mIconView.getDrawable(); - assertEquals(expected.getBitmap(), actual.getBitmap()); - assertEquals(View.VISIBLE, mIconView.getVisibility()); - } - - @Test - public void setContentDescription_shouldSetContentDescriptionOnIconView() { - IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); - mixin.setContentDescription("hello world"); - assertThat(mIconView.getContentDescription()).isEqualTo("hello world"); - } - - @Test - public void getContentDescription_shouldReturnContentDescriptionFromView() { - IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); - mIconView.setContentDescription("aloha"); - assertThat(mixin.getContentDescription()).isEqualTo("aloha"); + private Context mContext; + private TemplateLayout mTemplateLayout; + private ImageView mIconView; + + @Before + public void setUp() { + mContext = InstrumentationRegistry.getContext(); + mTemplateLayout = + spy(new TemplateLayout(mContext, R.layout.test_template, R.id.suw_layout_content)); + + mIconView = new ImageView(mContext); + doReturn(mIconView).when(mTemplateLayout).findManagedViewById(eq(R.id.suw_layout_icon)); + } + + @Test + public void testGetIconView() { + IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); + assertSame(mIconView, mixin.getView()); + } + + @Test + public void testSetIcon() { + final ColorDrawable drawable = new ColorDrawable(Color.CYAN); + IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); + mixin.setIcon(drawable); + + assertSame(drawable, mIconView.getDrawable()); + assertEquals(View.VISIBLE, mIconView.getVisibility()); + } + + @Test + public void setIcon_resourceId_shouldSetIcon() { + int icon = android.R.drawable.ic_menu_add; + IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); + mixin.setIcon(icon); + + Drawable drawable = mIconView.getDrawable(); + assertThat(drawable).isInstanceOf(BitmapDrawable.class); + assertEquals(View.VISIBLE, mIconView.getVisibility()); + } + + @Test + public void setIcon_shouldSetVisibilityToGone_whenIconIsNull() { + IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); + mixin.setIcon(null); + + assertEquals(View.GONE, mIconView.getVisibility()); + } + + @Test + public void testGetIcon() { + final ColorDrawable drawable = new ColorDrawable(Color.BLUE); + mIconView.setImageDrawable(drawable); + + IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); + assertSame(drawable, mixin.getIcon()); + } + + @SuppressWarnings("ResourceType") // Needed to create attribute set from layout XML. + @Test + public void testSetIconFromXml() throws IOException, XmlPullParserException { + final XmlResourceParser parser = mContext.getResources().getXml(R.layout.test_mixin_attributes); + while (!TemplateLayout.class.getName().equals(parser.getName())) { + parser.next(); } + new IconMixin(mTemplateLayout, Xml.asAttributeSet(parser), 0); + + // Check that the bitmaps themselves are equal because BitmapDrawable does not implement + // equals() + final BitmapDrawable expected = + (BitmapDrawable) mContext.getResources().getDrawable(android.R.drawable.ic_menu_add); + final BitmapDrawable actual = (BitmapDrawable) mIconView.getDrawable(); + assertEquals(expected.getBitmap(), actual.getBitmap()); + assertEquals(View.VISIBLE, mIconView.getVisibility()); + } + + @Test + public void setContentDescription_shouldSetContentDescriptionOnIconView() { + IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); + mixin.setContentDescription("hello world"); + assertThat(mIconView.getContentDescription()).isEqualTo("hello world"); + } + + @Test + public void getContentDescription_shouldReturnContentDescriptionFromView() { + IconMixin mixin = new IconMixin(mTemplateLayout, null, 0); + mIconView.setContentDescription("aloha"); + assertThat(mixin.getContentDescription()).isEqualTo("aloha"); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/template/ListMixinTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/template/ListMixinTest.java index 30d68f1..e73e2bc 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/template/ListMixinTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/template/ListMixinTest.java @@ -32,16 +32,14 @@ import android.graphics.drawable.Drawable; import android.graphics.drawable.InsetDrawable; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.TemplateLayout; import com.android.setupwizardlib.test.R; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -52,140 +50,138 @@ import org.mockito.MockitoAnnotations; @SmallTest public class ListMixinTest { - private Context mContext; - private TemplateLayout mTemplateLayout; + private Context mContext; + private TemplateLayout mTemplateLayout; - private ListView mListView; + private ListView mListView; - @Mock - private ListAdapter mAdapter; + @Mock private ListAdapter mAdapter; - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); - mContext = InstrumentationRegistry.getTargetContext(); - mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template, - R.id.suw_layout_content)); + mContext = InstrumentationRegistry.getTargetContext(); + mTemplateLayout = + spy(new TemplateLayout(mContext, R.layout.test_template, R.id.suw_layout_content)); - mListView = mock(ListView.class, delegatesTo(new ListView(mContext))); - doReturn(1).when(mAdapter).getViewTypeCount(); + mListView = mock(ListView.class, delegatesTo(new ListView(mContext))); + doReturn(1).when(mAdapter).getViewTypeCount(); - doReturn(mListView).when(mTemplateLayout) - .findManagedViewById(eq(android.R.id.list)); - doReturn(true).when(mTemplateLayout).isLayoutDirectionResolved(); - } + doReturn(mListView).when(mTemplateLayout).findManagedViewById(eq(android.R.id.list)); + doReturn(true).when(mTemplateLayout).isLayoutDirectionResolved(); + } - @Test - public void testGetListView() { - ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); - assertSame(mListView, mixin.getListView()); - } + @Test + public void testGetListView() { + ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); + assertSame(mListView, mixin.getListView()); + } - @Test - public void testGetAdapter() { - mListView.setAdapter(mAdapter); + @Test + public void testGetAdapter() { + mListView.setAdapter(mAdapter); - ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); - assertSame(mAdapter, mixin.getAdapter()); - } + ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); + assertSame(mAdapter, mixin.getAdapter()); + } - @Test - public void testSetAdapter() { - assertNull(mListView.getAdapter()); + @Test + public void testSetAdapter() { + assertNull(mListView.getAdapter()); - ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); - mixin.setAdapter(mAdapter); + ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); + mixin.setAdapter(mAdapter); - assertSame(mAdapter, mListView.getAdapter()); - } + assertSame(mAdapter, mListView.getAdapter()); + } - @Test - public void testDividerInsetLegacy() { - ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); - mixin.setDividerInset(123); + @Test + public void testDividerInsetLegacy() { + ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); + mixin.setDividerInset(123); - assertEquals(123, mixin.getDividerInset()); + assertEquals(123, mixin.getDividerInset()); - final Drawable divider = mListView.getDivider(); - InsetDrawable insetDrawable = (InsetDrawable) divider; - Rect rect = new Rect(); - insetDrawable.getPadding(rect); + final Drawable divider = mListView.getDivider(); + InsetDrawable insetDrawable = (InsetDrawable) divider; + Rect rect = new Rect(); + insetDrawable.getPadding(rect); - assertEquals(new Rect(123, 0, 0, 0), rect); - } + assertEquals(new Rect(123, 0, 0, 0), rect); + } - @Test - public void testDividerInsets() { - ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); - mixin.setDividerInsets(123, 456); + @Test + public void testDividerInsets() { + ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); + mixin.setDividerInsets(123, 456); - assertEquals(123, mixin.getDividerInsetStart()); - assertEquals(456, mixin.getDividerInsetEnd()); + assertEquals(123, mixin.getDividerInsetStart()); + assertEquals(456, mixin.getDividerInsetEnd()); - final Drawable divider = mListView.getDivider(); - InsetDrawable insetDrawable = (InsetDrawable) divider; - Rect rect = new Rect(); - insetDrawable.getPadding(rect); + final Drawable divider = mListView.getDivider(); + InsetDrawable insetDrawable = (InsetDrawable) divider; + Rect rect = new Rect(); + insetDrawable.getPadding(rect); - assertEquals(new Rect(123, 0, 456, 0), rect); - } + assertEquals(new Rect(123, 0, 456, 0), rect); + } - @Test - public void testDividerInsetLegacyRtl() { - if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { - doReturn(View.LAYOUT_DIRECTION_RTL).when(mTemplateLayout).getLayoutDirection(); + @Test + public void testDividerInsetLegacyRtl() { + if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { + doReturn(View.LAYOUT_DIRECTION_RTL).when(mTemplateLayout).getLayoutDirection(); - ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); - mixin.setDividerInset(123); + ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); + mixin.setDividerInset(123); - assertEquals(123, mixin.getDividerInset()); + assertEquals(123, mixin.getDividerInset()); - final Drawable divider = mListView.getDivider(); - InsetDrawable insetDrawable = (InsetDrawable) divider; - Rect rect = new Rect(); - insetDrawable.getPadding(rect); + final Drawable divider = mListView.getDivider(); + InsetDrawable insetDrawable = (InsetDrawable) divider; + Rect rect = new Rect(); + insetDrawable.getPadding(rect); - assertEquals(new Rect(0, 0, 123, 0), rect); - } - // else the test passes + assertEquals(new Rect(0, 0, 123, 0), rect); } + // else the test passes + } - @Test - public void testDividerInsetsRtl() { - if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { - doReturn(View.LAYOUT_DIRECTION_RTL).when(mTemplateLayout).getLayoutDirection(); + @Test + public void testDividerInsetsRtl() { + if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { + doReturn(View.LAYOUT_DIRECTION_RTL).when(mTemplateLayout).getLayoutDirection(); - ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); - mixin.setDividerInsets(123, 456); + ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); + mixin.setDividerInsets(123, 456); - assertEquals(123, mixin.getDividerInsetStart()); - assertEquals(456, mixin.getDividerInsetEnd()); + assertEquals(123, mixin.getDividerInsetStart()); + assertEquals(456, mixin.getDividerInsetEnd()); - final Drawable divider = mListView.getDivider(); - InsetDrawable insetDrawable = (InsetDrawable) divider; - Rect rect = new Rect(); - insetDrawable.getPadding(rect); + final Drawable divider = mListView.getDivider(); + InsetDrawable insetDrawable = (InsetDrawable) divider; + Rect rect = new Rect(); + insetDrawable.getPadding(rect); - assertEquals(new Rect(456, 0, 123, 0), rect); - } - // else the test passes + assertEquals(new Rect(456, 0, 123, 0), rect); } + // else the test passes + } - @Test - public void testNoList() { - doReturn(null).when(mTemplateLayout).findManagedViewById(eq(android.R.id.list)); + @Test + public void testNoList() { + doReturn(null).when(mTemplateLayout).findManagedViewById(eq(android.R.id.list)); - ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); + ListMixin mixin = new ListMixin(mTemplateLayout, null, 0); - mixin.setAdapter(mAdapter); - mixin.setDividerInset(123); + mixin.setAdapter(mAdapter); + mixin.setDividerInset(123); - assertNull(mixin.getListView()); - assertNull(mixin.getAdapter()); - mixin.getDividerInset(); // Test that it doesn't crash. The return value is not significant. - assertNull(mixin.getDivider()); + assertNull(mixin.getListView()); + assertNull(mixin.getAdapter()); + mixin.getDividerInset(); // Test that it doesn't crash. The return value is not significant. + assertNull(mixin.getDivider()); - verifyNoMoreInteractions(mListView); - } + verifyNoMoreInteractions(mListView); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/template/NavigationBarMixinTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/template/NavigationBarMixinTest.java index aca6084..1e2aff3 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/template/NavigationBarMixinTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/template/NavigationBarMixinTest.java @@ -29,12 +29,10 @@ import android.content.Context; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; - import com.android.setupwizardlib.TemplateLayout; import com.android.setupwizardlib.test.R; import com.android.setupwizardlib.view.NavigationBar; import com.android.setupwizardlib.view.NavigationBar.NavigationBarListener; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,56 +41,57 @@ import org.junit.runner.RunWith; @SmallTest public class NavigationBarMixinTest { - private Context mContext; - private TemplateLayout mTemplateLayout; - private NavigationBar mNavigationBar; - - @Before - public void setUp() { - mContext = InstrumentationRegistry.getContext(); - mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template, - R.id.suw_layout_content)); - - mNavigationBar = new NavigationBar(mContext); - doReturn(mNavigationBar).when(mTemplateLayout) - .findManagedViewById(eq(R.id.suw_layout_navigation_bar)); - } - - @Test - public void testGetNavigationBar() { - NavigationBarMixin mixin = new NavigationBarMixin(mTemplateLayout); - assertSame(mNavigationBar, mixin.getNavigationBar()); - } - - @Test - public void testSetNextButtonText() { - NavigationBarMixin mixin = new NavigationBarMixin(mTemplateLayout); - mixin.setNextButtonText(R.string.suw_more_button_label); - assertEquals("More", mNavigationBar.getNextButton().getText()); - - mixin.setNextButtonText("Foobar"); - assertEquals("Foobar", mNavigationBar.getNextButton().getText()); - } - - @SuppressLint("SetTextI18n") // It's OK, this is just a test - @Test - public void testGetNextButtonText() { - mNavigationBar.getNextButton().setText("lorem ipsum"); - - NavigationBarMixin mixin = new NavigationBarMixin(mTemplateLayout); - assertSame("lorem ipsum", mixin.getNextButtonText()); - } - - @Test - public void testSetNavigationBarListener() { - final NavigationBarListener listener = mock(NavigationBarListener.class); - NavigationBarMixin mixin = new NavigationBarMixin(mTemplateLayout); - mixin.setNavigationBarListener(listener); - - mNavigationBar.getNextButton().performClick(); - verify(listener).onNavigateNext(); - - mNavigationBar.getBackButton().performClick(); - verify(listener).onNavigateBack(); - } + private Context mContext; + private TemplateLayout mTemplateLayout; + private NavigationBar mNavigationBar; + + @Before + public void setUp() { + mContext = InstrumentationRegistry.getContext(); + mTemplateLayout = + spy(new TemplateLayout(mContext, R.layout.test_template, R.id.suw_layout_content)); + + mNavigationBar = new NavigationBar(mContext); + doReturn(mNavigationBar) + .when(mTemplateLayout) + .findManagedViewById(eq(R.id.suw_layout_navigation_bar)); + } + + @Test + public void testGetNavigationBar() { + NavigationBarMixin mixin = new NavigationBarMixin(mTemplateLayout); + assertSame(mNavigationBar, mixin.getNavigationBar()); + } + + @Test + public void testSetNextButtonText() { + NavigationBarMixin mixin = new NavigationBarMixin(mTemplateLayout); + mixin.setNextButtonText(R.string.suw_more_button_label); + assertEquals("More", mNavigationBar.getNextButton().getText()); + + mixin.setNextButtonText("Foobar"); + assertEquals("Foobar", mNavigationBar.getNextButton().getText()); + } + + @SuppressLint("SetTextI18n") // It's OK, this is just a test + @Test + public void testGetNextButtonText() { + mNavigationBar.getNextButton().setText("lorem ipsum"); + + NavigationBarMixin mixin = new NavigationBarMixin(mTemplateLayout); + assertSame("lorem ipsum", mixin.getNextButtonText()); + } + + @Test + public void testSetNavigationBarListener() { + final NavigationBarListener listener = mock(NavigationBarListener.class); + NavigationBarMixin mixin = new NavigationBarMixin(mTemplateLayout); + mixin.setNavigationBarListener(listener); + + mNavigationBar.getNextButton().performClick(); + verify(listener).onNavigateNext(); + + mNavigationBar.getBackButton().performClick(); + verify(listener).onNavigateBack(); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/template/ProgressBarMixinTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/template/ProgressBarMixinTest.java index 5b2fb50..78ebe1e 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/template/ProgressBarMixinTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/template/ProgressBarMixinTest.java @@ -29,16 +29,14 @@ import android.graphics.Canvas; import android.graphics.Color; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; import android.view.ContextThemeWrapper; import android.view.View; import android.widget.ProgressBar; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.TemplateLayout; import com.android.setupwizardlib.test.R; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -47,111 +45,107 @@ import org.junit.runner.RunWith; @SmallTest public class ProgressBarMixinTest { - private TemplateLayout mTemplateLayout; - - @Before - public void setUp() { - Context context = new ContextThemeWrapper(InstrumentationRegistry.getContext(), - R.style.SuwThemeMaterial_Light); - mTemplateLayout = new TemplateLayout( - context, - R.layout.test_progress_bar_template, R.id.suw_layout_content); - } - - @Test - public void testSetShown() { - ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); - mixin.setShown(true); - - ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById( - R.id.suw_layout_progress); - assertNotNull("Progress bar should be available after setting to shown", progressBar); - assertEquals(View.VISIBLE, progressBar.getVisibility()); - } - - @Test - public void testNotShown() { - ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); - mixin.setShown(true); - mixin.setShown(false); - - ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById( - R.id.suw_layout_progress); - assertNotEquals(View.VISIBLE, progressBar.getVisibility()); + private TemplateLayout mTemplateLayout; + + @Before + public void setUp() { + Context context = + new ContextThemeWrapper( + InstrumentationRegistry.getContext(), R.style.SuwThemeMaterial_Light); + mTemplateLayout = + new TemplateLayout(context, R.layout.test_progress_bar_template, R.id.suw_layout_content); + } + + @Test + public void testSetShown() { + ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); + mixin.setShown(true); + + ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById(R.id.suw_layout_progress); + assertNotNull("Progress bar should be available after setting to shown", progressBar); + assertEquals(View.VISIBLE, progressBar.getVisibility()); + } + + @Test + public void testNotShown() { + ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); + mixin.setShown(true); + mixin.setShown(false); + + ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById(R.id.suw_layout_progress); + assertNotEquals(View.VISIBLE, progressBar.getVisibility()); + } + + @Test + public void testIsShown() { + ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); + + mixin.setShown(true); + assertTrue(mixin.isShown()); + + mixin.setShown(false); + assertFalse(mixin.isShown()); + } + + @Test + public void testPeekProgressBar() { + ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); + assertNull( + "PeekProgressBar should return null when stub not inflated yet", mixin.peekProgressBar()); + + mixin.setShown(true); + assertNotNull( + "PeekProgressBar should be available after setting to shown", mixin.peekProgressBar()); + } + + @Test + public void testSetColorBeforeSetShown() { + ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); + mixin.setColor(ColorStateList.valueOf(Color.MAGENTA)); + + mixin.setShown(true); + + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + ProgressBar progressBar = + (ProgressBar) mTemplateLayout.findViewById(R.id.suw_layout_progress); + assertEquals(ColorStateList.valueOf(Color.MAGENTA), progressBar.getIndeterminateTintList()); + assertEquals( + ColorStateList.valueOf(Color.MAGENTA), progressBar.getProgressBackgroundTintList()); } - - @Test - public void testIsShown() { - ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); - - mixin.setShown(true); - assertTrue(mixin.isShown()); - - mixin.setShown(false); - assertFalse(mixin.isShown()); + // this method is a no-op on versions < lollipop. Just check that it doesn't crash. + } + + @Test + public void testSetColorAfterSetShown() { + ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); + mixin.setShown(true); + + mixin.setColor(ColorStateList.valueOf(Color.YELLOW)); + + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + ProgressBar progressBar = + (ProgressBar) mTemplateLayout.findViewById(R.id.suw_layout_progress); + assertEquals(ColorStateList.valueOf(Color.YELLOW), progressBar.getIndeterminateTintList()); + assertEquals( + ColorStateList.valueOf(Color.YELLOW), progressBar.getProgressBackgroundTintList()); } - - @Test - public void testPeekProgressBar() { - ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); - assertNull("PeekProgressBar should return null when stub not inflated yet", - mixin.peekProgressBar()); - - mixin.setShown(true); - assertNotNull("PeekProgressBar should be available after setting to shown", - mixin.peekProgressBar()); - } - - @Test - public void testSetColorBeforeSetShown() { - ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); - mixin.setColor(ColorStateList.valueOf(Color.MAGENTA)); - - mixin.setShown(true); - - if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { - ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById( - R.id.suw_layout_progress); - assertEquals(ColorStateList.valueOf(Color.MAGENTA), - progressBar.getIndeterminateTintList()); - assertEquals(ColorStateList.valueOf(Color.MAGENTA), - progressBar.getProgressBackgroundTintList()); - } - // this method is a no-op on versions < lollipop. Just check that it doesn't crash. - } - - @Test - public void testSetColorAfterSetShown() { - ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); - mixin.setShown(true); - - mixin.setColor(ColorStateList.valueOf(Color.YELLOW)); - - if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { - ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById( - R.id.suw_layout_progress); - assertEquals(ColorStateList.valueOf(Color.YELLOW), - progressBar.getIndeterminateTintList()); - assertEquals(ColorStateList.valueOf(Color.YELLOW), - progressBar.getProgressBackgroundTintList()); - } - // this method is a no-op on versions < lollipop. Just check that it doesn't crash. - } - - @Test - public void testDeterminateProgressBarNullTint() { - ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); - mixin.setShown(true); - mixin.peekProgressBar().setIndeterminate(false); - - mixin.setColor(null); - - if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { - ProgressBar progressBar = (ProgressBar) mTemplateLayout.findViewById( - R.id.suw_layout_progress); - assertEquals(null, progressBar.getProgressBackgroundTintList()); - progressBar.draw(new Canvas()); - } - // setColor is a no-op on versions < lollipop. Just check that it doesn't crash. + // this method is a no-op on versions < lollipop. Just check that it doesn't crash. + } + + @Test + public void testDeterminateProgressBarNullTint() { + ProgressBarMixin mixin = new ProgressBarMixin(mTemplateLayout); + mixin.setShown(true); + mixin.peekProgressBar().setIndeterminate(false); + + mixin.setColor(null); + + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + ProgressBar progressBar = + (ProgressBar) mTemplateLayout.findViewById(R.id.suw_layout_progress); + assertEquals(null, progressBar.getProgressBackgroundTintList()); + progressBar.draw(new Canvas()); } + // setColor is a no-op on versions < lollipop. Just check that it doesn't crash. + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/template/TemplateLayoutMixinTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/template/TemplateLayoutMixinTest.java index 7cc934a..6adebc6 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/template/TemplateLayoutMixinTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/template/TemplateLayoutMixinTest.java @@ -24,10 +24,8 @@ import android.content.Context; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; - import com.android.setupwizardlib.TemplateLayout; import com.android.setupwizardlib.test.R; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -36,35 +34,38 @@ import org.junit.runner.RunWith; @SmallTest public class TemplateLayoutMixinTest { - private TestTemplateLayout mLayout; + private TestTemplateLayout mLayout; - @Before - public void setUp() throws Exception { - mLayout = new TestTemplateLayout(InstrumentationRegistry.getContext()); - } + @Before + public void setUp() throws Exception { + mLayout = new TestTemplateLayout(InstrumentationRegistry.getContext()); + } - @Test - public void testGetMixin() { - final TestMixin mixin = mLayout.getMixin(TestMixin.class); - assertNotNull("TestMixin should not be null", mixin); - assertTrue("TestMixin should be an instance of TestMixinSubclass. " - + "Found " + mixin.getClass() + " instead.", - mixin instanceof TestMixinSubclass); + @Test + public void testGetMixin() { + final TestMixin mixin = mLayout.getMixin(TestMixin.class); + assertNotNull("TestMixin should not be null", mixin); + assertTrue( + "TestMixin should be an instance of TestMixinSubclass. " + + "Found " + + mixin.getClass() + + " instead.", + mixin instanceof TestMixinSubclass); - // Mixin must be retrieved using the interface it's registered with, not the concrete class, - // although they are often the same. - assertNull("TestMixinSubclass should be null", mLayout.getMixin(TestMixinSubclass.class)); - } + // Mixin must be retrieved using the interface it's registered with, not the concrete class, + // although they are often the same. + assertNull("TestMixinSubclass should be null", mLayout.getMixin(TestMixinSubclass.class)); + } - private static class TestTemplateLayout extends TemplateLayout { + private static class TestTemplateLayout extends TemplateLayout { - TestTemplateLayout(Context context) { - super(context, R.layout.test_template, R.id.suw_layout_content); - registerMixin(TestMixin.class, new TestMixinSubclass()); - } + TestTemplateLayout(Context context) { + super(context, R.layout.test_template, R.id.suw_layout_content); + registerMixin(TestMixin.class, new TestMixinSubclass()); } + } - private static class TestMixin implements Mixin {} + private static class TestMixin implements Mixin {} - private static class TestMixinSubclass extends TestMixin {} + private static class TestMixinSubclass extends TestMixin {} } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/BottomScrollViewTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/BottomScrollViewTest.java index 1a8eb21..4f9487c 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/BottomScrollViewTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/BottomScrollViewTest.java @@ -21,13 +21,11 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import android.content.Context; +import android.view.View; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; -import android.view.View; - import com.android.setupwizardlib.view.BottomScrollView; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -36,101 +34,101 @@ import org.junit.runner.RunWith; @SmallTest public class BottomScrollViewTest { - private TestBottomScrollListener mListener; + private TestBottomScrollListener mListener; - @Before - public void setUp() throws Exception { - mListener = new TestBottomScrollListener(); - } + @Before + public void setUp() throws Exception { + mListener = new TestBottomScrollListener(); + } - @Test - public void testNoNeedScroll() { - createScrollView(20); - assertTrue("Scroll should not be required", mListener.mScrolledToBottom); - } + @Test + public void testNoNeedScroll() { + createScrollView(20); + assertTrue("Scroll should not be required", mListener.mScrolledToBottom); + } - @Test - public void testNeedScroll() { - createScrollView(110); - assertFalse("Scroll should be required", mListener.mScrolledToBottom); - } + @Test + public void testNeedScroll() { + createScrollView(110); + assertFalse("Scroll should be required", mListener.mScrolledToBottom); + } - @Test - public void testScrollToBottom() { - final BottomScrollView bottomScrollView = createScrollView(110); + @Test + public void testScrollToBottom() { + final BottomScrollView bottomScrollView = createScrollView(110); - assertFalse("Scroll should be required", mListener.mScrolledToBottom); + assertFalse("Scroll should be required", mListener.mScrolledToBottom); - bottomScrollView.scrollTo(0, 10); - assertTrue("Should already be scrolled to bottom", mListener.mScrolledToBottom); - } + bottomScrollView.scrollTo(0, 10); + assertTrue("Should already be scrolled to bottom", mListener.mScrolledToBottom); + } - @Test - public void testScrollThreshold() { - final BottomScrollView bottomScrollView = createScrollView(110); - assertEquals("Scroll threshold should be 10", 10, bottomScrollView.getScrollThreshold()); - } + @Test + public void testScrollThreshold() { + final BottomScrollView bottomScrollView = createScrollView(110); + assertEquals("Scroll threshold should be 10", 10, bottomScrollView.getScrollThreshold()); + } - private BottomScrollView createScrollView(final int childHeight) { - final Context context = InstrumentationRegistry.getContext(); - final BottomScrollView bottomScrollView = new TestBottomScrollView(context); - bottomScrollView.setBottomScrollListener(mListener); + private BottomScrollView createScrollView(final int childHeight) { + final Context context = InstrumentationRegistry.getContext(); + final BottomScrollView bottomScrollView = new TestBottomScrollView(context); + bottomScrollView.setBottomScrollListener(mListener); - final View child = new TestChildView(context, childHeight); + final View child = new TestChildView(context, childHeight); - child.measure(0, 0); // TestChildView's measured dimensions doesn't depend on the arguments - bottomScrollView.addView(child); - bottomScrollView.layout(0, 0, 100, 100); + child.measure(0, 0); // TestChildView's measured dimensions doesn't depend on the arguments + bottomScrollView.addView(child); + bottomScrollView.layout(0, 0, 100, 100); - return bottomScrollView; - } + return bottomScrollView; + } - private static class TestChildView extends View { + private static class TestChildView extends View { - private static final int WIDTH = 10; - private int mHeight; + private static final int WIDTH = 10; + private int mHeight; - TestChildView(Context context, int height) { - super(context); - mHeight = height; - } + TestChildView(Context context, int height) { + super(context); + mHeight = height; + } - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - setMeasuredDimension(WIDTH, mHeight); - } + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + setMeasuredDimension(WIDTH, mHeight); + } - public void setHeight(int height) { - mHeight = height; - } + public void setHeight(int height) { + mHeight = height; } + } - private static class TestBottomScrollView extends BottomScrollView { + private static class TestBottomScrollView extends BottomScrollView { - TestBottomScrollView(Context context) { - super(context); - } + TestBottomScrollView(Context context) { + super(context); + } - @Override - public boolean post(Runnable action) { - // Post all runnables synchronously so that tests can check the callbacks. - action.run(); - return true; - } + @Override + public boolean post(Runnable action) { + // Post all runnables synchronously so that tests can check the callbacks. + action.run(); + return true; } + } - private static class TestBottomScrollListener implements BottomScrollView.BottomScrollListener { + private static class TestBottomScrollListener implements BottomScrollView.BottomScrollListener { - boolean mScrolledToBottom = true; + boolean mScrolledToBottom = true; - @Override - public void onScrolledToBottom() { - mScrolledToBottom = true; - } + @Override + public void onScrolledToBottom() { + mScrolledToBottom = true; + } - @Override - public void onRequiresScroll() { - mScrolledToBottom = false; - } + @Override + public void onRequiresScroll() { + mScrolledToBottom = false; } + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/ButtonBarItemTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/ButtonBarItemTest.java index 18c295e..aacffeb 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/ButtonBarItemTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/ButtonBarItemTest.java @@ -20,18 +20,16 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.items.ButtonBarItem; import com.android.setupwizardlib.items.ButtonItem; import com.android.setupwizardlib.items.Item; import com.android.setupwizardlib.items.ItemHierarchy; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -40,87 +38,92 @@ import org.junit.runner.RunWith; @SmallTest public class ButtonBarItemTest { - private ButtonItem mChild1; - private ButtonItem mChild2; - private ButtonItem mChild3; - - @Before - public void setUp() throws Exception { - mChild1 = new ButtonItem(); - mChild2 = new ButtonItem(); - mChild3 = new ButtonItem(); + private ButtonItem mChild1; + private ButtonItem mChild2; + private ButtonItem mChild3; + + @Before + public void setUp() throws Exception { + mChild1 = new ButtonItem(); + mChild2 = new ButtonItem(); + mChild3 = new ButtonItem(); + } + + @Test + public void testFindItemById() { + ButtonBarItem item = new ButtonBarItem(); + item.setId(888); + + mChild1.setId(123); + mChild2.setId(456); + mChild3.setId(789); + item.addChild(mChild1); + item.addChild(mChild2); + item.addChild(mChild3); + + assertEquals("Finding 123 should return child1", mChild1, item.findItemById(123)); + assertEquals("Finding 456 should return child2", mChild2, item.findItemById(456)); + assertEquals("Finding 789 should return child3", mChild3, item.findItemById(789)); + + assertEquals("Finding 888 should return ButtonBarItem itself", item, item.findItemById(888)); + + assertNull("Finding 999 should return null", item.findItemById(999)); + } + + @Test + public void testBindEmpty() { + ButtonBarItem item = new ButtonBarItem(); + final ViewGroup layout = createLayout(); + item.onBindView(layout); + + assertEquals( + "Binding empty ButtonBar should not create any children", 0, layout.getChildCount()); + } + + @Test + public void testBind() { + ButtonBarItem item = new ButtonBarItem(); + + item.addChild(mChild1); + mChild1.setText("child1"); + item.addChild(mChild2); + mChild2.setText("child2"); + item.addChild(mChild3); + mChild3.setText("child3"); + + final ViewGroup layout = createLayout(); + item.onBindView(layout); + + assertEquals("Binding ButtonBar should create 3 children", 3, layout.getChildCount()); + assertEquals( + "First button should have text \"child1\"", + "child1", + ((Button) layout.getChildAt(0)).getText()); + assertEquals( + "Second button should have text \"child2\"", + "child2", + ((Button) layout.getChildAt(1)).getText()); + assertEquals( + "Third button should have text \"child3\"", + "child3", + ((Button) layout.getChildAt(2)).getText()); + } + + @Test + public void testAddInvalidChild() { + ButtonBarItem item = new ButtonBarItem(); + + ItemHierarchy invalidChild = new Item(); + + try { + item.addChild(invalidChild); + fail("Adding non ButtonItem to ButtonBarItem should throw exception"); + } catch (UnsupportedOperationException e) { + // pass } + } - @Test - public void testFindItemById() { - ButtonBarItem item = new ButtonBarItem(); - item.setId(888); - - mChild1.setId(123); - mChild2.setId(456); - mChild3.setId(789); - item.addChild(mChild1); - item.addChild(mChild2); - item.addChild(mChild3); - - assertEquals("Finding 123 should return child1", mChild1, item.findItemById(123)); - assertEquals("Finding 456 should return child2", mChild2, item.findItemById(456)); - assertEquals("Finding 789 should return child3", mChild3, item.findItemById(789)); - - assertEquals("Finding 888 should return ButtonBarItem itself", item, - item.findItemById(888)); - - assertNull("Finding 999 should return null", item.findItemById(999)); - } - - @Test - public void testBindEmpty() { - ButtonBarItem item = new ButtonBarItem(); - final ViewGroup layout = createLayout(); - item.onBindView(layout); - - assertEquals("Binding empty ButtonBar should not create any children", 0, - layout.getChildCount()); - } - - @Test - public void testBind() { - ButtonBarItem item = new ButtonBarItem(); - - item.addChild(mChild1); - mChild1.setText("child1"); - item.addChild(mChild2); - mChild2.setText("child2"); - item.addChild(mChild3); - mChild3.setText("child3"); - - final ViewGroup layout = createLayout(); - item.onBindView(layout); - - assertEquals("Binding ButtonBar should create 3 children", 3, layout.getChildCount()); - assertEquals("First button should have text \"child1\"", "child1", - ((Button) layout.getChildAt(0)).getText()); - assertEquals("Second button should have text \"child2\"", "child2", - ((Button) layout.getChildAt(1)).getText()); - assertEquals("Third button should have text \"child3\"", "child3", - ((Button) layout.getChildAt(2)).getText()); - } - - @Test - public void testAddInvalidChild() { - ButtonBarItem item = new ButtonBarItem(); - - ItemHierarchy invalidChild = new Item(); - - try { - item.addChild(invalidChild); - fail("Adding non ButtonItem to ButtonBarItem should throw exception"); - } catch (UnsupportedOperationException e) { - // pass - } - } - - private ViewGroup createLayout() { - return new LinearLayout(InstrumentationRegistry.getContext()); - } + private ViewGroup createLayout() { + return new LinearLayout(InstrumentationRegistry.getContext()); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/DrawableLayoutDirectionHelperTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/DrawableLayoutDirectionHelperTest.java index 95245b0..1445660 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/DrawableLayoutDirectionHelperTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/DrawableLayoutDirectionHelperTest.java @@ -28,117 +28,124 @@ import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.InsetDrawable; import android.os.Build; +import android.view.View; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; -import android.view.View; - import com.android.setupwizardlib.util.DrawableLayoutDirectionHelper; - +import java.util.Locale; import org.junit.Test; import org.junit.runner.RunWith; -import java.util.Locale; - @RunWith(AndroidJUnit4.class) @SmallTest public class DrawableLayoutDirectionHelperTest { - @Test - public void testCreateRelativeInsetDrawableLtr() { - final Drawable drawable = new ColorDrawable(Color.RED); - @SuppressLint("InlinedApi") // Testing with inlined constant is OK here - final InsetDrawable insetDrawable = - DrawableLayoutDirectionHelper.createRelativeInsetDrawable(drawable, - 1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */, - View.LAYOUT_DIRECTION_LTR); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - assertSame("Drawable from getDrawable() should be same as passed in", drawable, - insetDrawable.getDrawable()); - } - Rect outRect = new Rect(); - insetDrawable.getPadding(outRect); - assertEquals("InsetDrawable padding should be same as inset", new Rect(1, 2, 3, 4), - outRect); + @Test + public void testCreateRelativeInsetDrawableLtr() { + final Drawable drawable = new ColorDrawable(Color.RED); + @SuppressLint("InlinedApi") // Testing with inlined constant is OK here + final InsetDrawable insetDrawable = + DrawableLayoutDirectionHelper.createRelativeInsetDrawable( + drawable, + 1 /* start */, + 2 /* top */, + 3 /* end */, + 4 /* bottom */, + View.LAYOUT_DIRECTION_LTR); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + assertSame( + "Drawable from getDrawable() should be same as passed in", + drawable, + insetDrawable.getDrawable()); } + Rect outRect = new Rect(); + insetDrawable.getPadding(outRect); + assertEquals("InsetDrawable padding should be same as inset", new Rect(1, 2, 3, 4), outRect); + } - @Test - public void testCreateRelativeInsetDrawableRtl() { - final Drawable drawable = new ColorDrawable(Color.RED); - @SuppressLint("InlinedApi") // Testing with inlined constant is OK here - final InsetDrawable insetDrawable = - DrawableLayoutDirectionHelper.createRelativeInsetDrawable(drawable, - 1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */, - View.LAYOUT_DIRECTION_RTL); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - assertSame("Drawable from getDrawable() should be same as passed in", drawable, - insetDrawable.getDrawable()); - } - Rect outRect = new Rect(); - insetDrawable.getPadding(outRect); - assertEquals("InsetDrawable padding should be same as inset", new Rect(3, 2, 1, 4), - outRect); + @Test + public void testCreateRelativeInsetDrawableRtl() { + final Drawable drawable = new ColorDrawable(Color.RED); + @SuppressLint("InlinedApi") // Testing with inlined constant is OK here + final InsetDrawable insetDrawable = + DrawableLayoutDirectionHelper.createRelativeInsetDrawable( + drawable, + 1 /* start */, + 2 /* top */, + 3 /* end */, + 4 /* bottom */, + View.LAYOUT_DIRECTION_RTL); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + assertSame( + "Drawable from getDrawable() should be same as passed in", + drawable, + insetDrawable.getDrawable()); } + Rect outRect = new Rect(); + insetDrawable.getPadding(outRect); + assertEquals("InsetDrawable padding should be same as inset", new Rect(3, 2, 1, 4), outRect); + } - @Test - public void testCreateRelativeInsetDrawableViewRtl() { - final Drawable drawable = new ColorDrawable(Color.RED); - final View view = new ForceRtlView(InstrumentationRegistry.getContext()); - final InsetDrawable insetDrawable = - DrawableLayoutDirectionHelper.createRelativeInsetDrawable(drawable, - 1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */, view); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - assertSame("Drawable from getDrawable() should be same as passed in", drawable, - insetDrawable.getDrawable()); - } - Rect outRect = new Rect(); - insetDrawable.getPadding(outRect); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - assertEquals("InsetDrawable padding should be same as inset", new Rect(3, 2, 1, 4), - outRect); - } else { - assertEquals("InsetDrawable padding should be same as inset", new Rect(1, 2, 3, 4), - outRect); - } + @Test + public void testCreateRelativeInsetDrawableViewRtl() { + final Drawable drawable = new ColorDrawable(Color.RED); + final View view = new ForceRtlView(InstrumentationRegistry.getContext()); + final InsetDrawable insetDrawable = + DrawableLayoutDirectionHelper.createRelativeInsetDrawable( + drawable, 1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */, view); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + assertSame( + "Drawable from getDrawable() should be same as passed in", + drawable, + insetDrawable.getDrawable()); + } + Rect outRect = new Rect(); + insetDrawable.getPadding(outRect); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + assertEquals("InsetDrawable padding should be same as inset", new Rect(3, 2, 1, 4), outRect); + } else { + assertEquals("InsetDrawable padding should be same as inset", new Rect(1, 2, 3, 4), outRect); } + } - @Test - public void testCreateRelativeInsetDrawableContextRtl() { - Context context = InstrumentationRegistry.getContext(); - final Drawable drawable = new ColorDrawable(Color.RED); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - final Configuration config = new Configuration(); - config.setLayoutDirection(new Locale("fa", "IR")); - context = context.createConfigurationContext(config); - } - final InsetDrawable insetDrawable = - DrawableLayoutDirectionHelper.createRelativeInsetDrawable(drawable, - 1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */, context); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - assertSame("Drawable from getDrawable() should be same as passed in", drawable, - insetDrawable.getDrawable()); - } - Rect outRect = new Rect(); - insetDrawable.getPadding(outRect); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - assertEquals("InsetDrawable padding should be same as inset", new Rect(3, 2, 1, 4), - outRect); - } else { - assertEquals("InsetDrawable padding should be same as inset", new Rect(1, 2, 3, 4), - outRect); - } + @Test + public void testCreateRelativeInsetDrawableContextRtl() { + Context context = InstrumentationRegistry.getContext(); + final Drawable drawable = new ColorDrawable(Color.RED); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + final Configuration config = new Configuration(); + config.setLayoutDirection(new Locale("fa", "IR")); + context = context.createConfigurationContext(config); + } + final InsetDrawable insetDrawable = + DrawableLayoutDirectionHelper.createRelativeInsetDrawable( + drawable, 1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */, context); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + assertSame( + "Drawable from getDrawable() should be same as passed in", + drawable, + insetDrawable.getDrawable()); } + Rect outRect = new Rect(); + insetDrawable.getPadding(outRect); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + assertEquals("InsetDrawable padding should be same as inset", new Rect(3, 2, 1, 4), outRect); + } else { + assertEquals("InsetDrawable padding should be same as inset", new Rect(1, 2, 3, 4), outRect); + } + } - private static class ForceRtlView extends View { + private static class ForceRtlView extends View { - ForceRtlView(Context context) { - super(context); - } + ForceRtlView(Context context) { + super(context); + } - @Override - @SuppressLint("InlinedApi") // Testing with inlined constant is OK here - public int getLayoutDirection() { - return View.LAYOUT_DIRECTION_RTL; - } + @Override + @SuppressLint("InlinedApi") // Testing with inlined constant is OK here + public int getLayoutDirection() { + return View.LAYOUT_DIRECTION_RTL; } + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifLayoutTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifLayoutTest.java index e12b31d..f8aae5a 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifLayoutTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifLayoutTest.java @@ -26,9 +26,6 @@ import android.content.res.ColorStateList; import android.graphics.Color; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; import android.view.ContextThemeWrapper; import android.view.Gravity; import android.view.LayoutInflater; @@ -36,9 +33,10 @@ import android.view.View; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.GlifLayout; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -47,94 +45,99 @@ import org.junit.runner.RunWith; @SmallTest public class GlifLayoutTest { - private Context mContext; - - @Before - public void setUp() throws Exception { - mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(), - R.style.SuwThemeGlif_Light); + private Context mContext; + + @Before + public void setUp() throws Exception { + mContext = + new ContextThemeWrapper(InstrumentationRegistry.getContext(), R.style.SuwThemeGlif_Light); + } + + @Test + public void testInflateFromXml() { + LayoutInflater inflater = LayoutInflater.from(mContext); + GlifLayout layout = (GlifLayout) inflater.inflate(R.layout.test_glif_layout, null); + assertDefaultTemplateInflated(layout); + View content = layout.findViewById(R.id.test_content); + assertTrue("@id/test_content should be a TextView", content instanceof TextView); + } + + @Test + public void testPrimaryColorFromXml() { + LayoutInflater inflater = LayoutInflater.from(mContext); + GlifLayout layout = + (GlifLayout) inflater.inflate(R.layout.test_glif_layout_primary_color, null); + assertDefaultTemplateInflated(layout); + + assertEquals(ColorStateList.valueOf(Color.RED), layout.getPrimaryColor()); + } + + @Test + public void testSetProgressBarShownInvalid() { + GlifLayout layout = new GlifLayout(mContext, R.layout.test_template); + layout.setProgressBarShown(true); + // This is a no-op because there is no progress bar stub + } + + @Test + public void testGlifTheme() { + mContext = + new ContextThemeWrapper(InstrumentationRegistry.getContext(), R.style.SuwThemeGlif_Light); + final GlifLayout glifLayout = new GlifLayout(mContext); + + if (VERSION.SDK_INT >= VERSION_CODES.M) { + // Scroll indicators are only available on versions >= M + assertEquals(View.SCROLL_INDICATOR_BOTTOM, glifLayout.getScrollView().getScrollIndicators()); } - - @Test - public void testInflateFromXml() { - LayoutInflater inflater = LayoutInflater.from(mContext); - GlifLayout layout = (GlifLayout) inflater.inflate(R.layout.test_glif_layout, null); - assertDefaultTemplateInflated(layout); - View content = layout.findViewById(R.id.test_content); - assertTrue("@id/test_content should be a TextView", content instanceof TextView); - } - - @Test - public void testPrimaryColorFromXml() { - LayoutInflater inflater = LayoutInflater.from(mContext); - GlifLayout layout = - (GlifLayout) inflater.inflate(R.layout.test_glif_layout_primary_color, null); - assertDefaultTemplateInflated(layout); - - assertEquals(ColorStateList.valueOf(Color.RED), layout.getPrimaryColor()); + } + + @Test + public void testGlifV2Theme() { + mContext = + new ContextThemeWrapper(InstrumentationRegistry.getContext(), R.style.SuwThemeGlifV2_Light); + final GlifLayout glifLayout = new GlifLayout(mContext); + final TextView titleView = (TextView) glifLayout.findManagedViewById(R.id.suw_layout_title); + if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { + assertEquals(View.TEXT_ALIGNMENT_GRAVITY, titleView.getTextAlignment()); } - - @Test - public void testSetProgressBarShownInvalid() { - GlifLayout layout = new GlifLayout(mContext, R.layout.test_template); - layout.setProgressBarShown(true); - // This is a no-op because there is no progress bar stub - } - - @Test - public void testGlifTheme() { - mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(), - R.style.SuwThemeGlif_Light); - final GlifLayout glifLayout = new GlifLayout(mContext); - - if (VERSION.SDK_INT >= VERSION_CODES.M) { - // Scroll indicators are only available on versions >= M - assertEquals(View.SCROLL_INDICATOR_BOTTOM, - glifLayout.getScrollView().getScrollIndicators()); - } + assertEquals( + "Title text should be center aligned on GLIF v2 theme", + Gravity.CENTER_HORIZONTAL, + titleView.getGravity() & Gravity.CENTER_HORIZONTAL); + + if (VERSION.SDK_INT >= VERSION_CODES.N) { + // LinearLayout.getGravity is only available on versions >= N + final View iconView = glifLayout.findManagedViewById(R.id.suw_layout_icon); + final LinearLayout parent = (LinearLayout) iconView.getParent(); + assertEquals( + "Icon should be center aligned on GLIF v2 theme", + Gravity.CENTER_HORIZONTAL, + parent.getGravity() & Gravity.CENTER_HORIZONTAL); } - @Test - public void testGlifV2Theme() { - mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(), - R.style.SuwThemeGlifV2_Light); - final GlifLayout glifLayout = new GlifLayout(mContext); - final TextView titleView = (TextView) glifLayout.findManagedViewById(R.id.suw_layout_title); - if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { - assertEquals(View.TEXT_ALIGNMENT_GRAVITY, titleView.getTextAlignment()); - } - assertEquals("Title text should be center aligned on GLIF v2 theme", - Gravity.CENTER_HORIZONTAL, titleView.getGravity() & Gravity.CENTER_HORIZONTAL); - - if (VERSION.SDK_INT >= VERSION_CODES.N) { - // LinearLayout.getGravity is only available on versions >= N - final View iconView = glifLayout.findManagedViewById(R.id.suw_layout_icon); - final LinearLayout parent = (LinearLayout) iconView.getParent(); - assertEquals("Icon should be center aligned on GLIF v2 theme", - Gravity.CENTER_HORIZONTAL, parent.getGravity() & Gravity.CENTER_HORIZONTAL); - } - - assertEquals("Status bar color should be white in GLIF v2 theme", - "ffffffff", - Integer.toHexString(glifLayout.getBackgroundBaseColor().getDefaultColor())); - assertFalse("GLIF v2 theme shuold not have patterned background", - glifLayout.isBackgroundPatterned()); - - if (VERSION.SDK_INT >= VERSION_CODES.M) { - // Scroll indicators are only available on versions >= M - assertEquals(View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM, - glifLayout.getScrollView().getScrollIndicators()); - } + assertEquals( + "Status bar color should be white in GLIF v2 theme", + "ffffffff", + Integer.toHexString(glifLayout.getBackgroundBaseColor().getDefaultColor())); + assertFalse( + "GLIF v2 theme shuold not have patterned background", glifLayout.isBackgroundPatterned()); + + if (VERSION.SDK_INT >= VERSION_CODES.M) { + // Scroll indicators are only available on versions >= M + assertEquals( + View.SCROLL_INDICATOR_TOP | View.SCROLL_INDICATOR_BOTTOM, + glifLayout.getScrollView().getScrollIndicators()); } + } - private void assertDefaultTemplateInflated(GlifLayout layout) { - View title = layout.findViewById(R.id.suw_layout_title); - assertNotNull("@id/suw_layout_title should not be null", title); + private void assertDefaultTemplateInflated(GlifLayout layout) { + View title = layout.findViewById(R.id.suw_layout_title); + assertNotNull("@id/suw_layout_title should not be null", title); - View icon = layout.findViewById(R.id.suw_layout_icon); - assertNotNull("@id/suw_layout_icon should not be null", icon); + View icon = layout.findViewById(R.id.suw_layout_icon); + assertNotNull("@id/suw_layout_icon should not be null", icon); - View scrollView = layout.findViewById(R.id.suw_scroll_view); - assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView); - } + View scrollView = layout.findViewById(R.id.suw_scroll_view); + assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifListLayoutTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifListLayoutTest.java index c2e932c..0665bfe 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifListLayoutTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifListLayoutTest.java @@ -26,9 +26,6 @@ import android.content.Context; import android.graphics.drawable.Drawable; import android.graphics.drawable.InsetDrawable; import android.os.Build; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.View; @@ -36,9 +33,10 @@ import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.GlifListLayout; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -47,102 +45,100 @@ import org.junit.runner.RunWith; @SmallTest public class GlifListLayoutTest { - private Context mContext; - - @Before - public void setUp() throws Exception { - mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(), - R.style.SuwThemeGlif_Light); + private Context mContext; + + @Before + public void setUp() throws Exception { + mContext = + new ContextThemeWrapper(InstrumentationRegistry.getContext(), R.style.SuwThemeGlif_Light); + } + + @Test + public void testDefaultTemplate() { + GlifListLayout layout = new GlifListLayout(mContext); + assertListTemplateInflated(layout); + } + + @Test + public void testAddView() { + GlifListLayout layout = new GlifListLayout(mContext); + TextView tv = new TextView(mContext); + try { + layout.addView(tv); + fail("Adding view to ListLayout should throw"); + } catch (UnsupportedOperationException e) { + // Expected exception } - - @Test - public void testDefaultTemplate() { - GlifListLayout layout = new GlifListLayout(mContext); - assertListTemplateInflated(layout); + } + + @Test + public void testInflateFromXml() { + LayoutInflater inflater = LayoutInflater.from(mContext); + GlifListLayout layout = (GlifListLayout) inflater.inflate(R.layout.test_glif_list_layout, null); + assertListTemplateInflated(layout); + } + + @Test + public void testGetListView() { + GlifListLayout layout = new GlifListLayout(mContext); + assertListTemplateInflated(layout); + assertNotNull("getListView should not be null", layout.getListView()); + } + + @Test + public void testAdapter() { + GlifListLayout layout = new GlifListLayout(mContext); + assertListTemplateInflated(layout); + + final ArrayAdapter<String> adapter = + new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1); + adapter.add("Abracadabra"); + layout.setAdapter(adapter); + + final ListAdapter gotAdapter = layout.getAdapter(); + // Note: the wrapped adapter should be returned directly, not the HeaderViewListAdapter. + assertSame("Adapter got from GlifListLayout should be same as set", adapter, gotAdapter); + } + + @Test + public void testDividerInsetLegacy() { + GlifListLayout layout = new GlifListLayout(mContext); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); } + assertListTemplateInflated(layout); - @Test - public void testAddView() { - GlifListLayout layout = new GlifListLayout(mContext); - TextView tv = new TextView(mContext); - try { - layout.addView(tv); - fail("Adding view to ListLayout should throw"); - } catch (UnsupportedOperationException e) { - // Expected exception - } - } + layout.setDividerInset(10); + assertEquals("Divider inset should be 10", 10, layout.getDividerInset()); - @Test - public void testInflateFromXml() { - LayoutInflater inflater = LayoutInflater.from(mContext); - GlifListLayout layout = (GlifListLayout) - inflater.inflate(R.layout.test_glif_list_layout, null); - assertListTemplateInflated(layout); - } + final Drawable divider = layout.getDivider(); + assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); + } - @Test - public void testGetListView() { - GlifListLayout layout = new GlifListLayout(mContext); - assertListTemplateInflated(layout); - assertNotNull("getListView should not be null", layout.getListView()); + @Test + public void testDividerInsets() { + GlifListLayout layout = new GlifListLayout(mContext); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); } + assertListTemplateInflated(layout); - @Test - public void testAdapter() { - GlifListLayout layout = new GlifListLayout(mContext); - assertListTemplateInflated(layout); + layout.setDividerInsets(10, 15); + assertEquals("Divider inset should be 10", 10, layout.getDividerInsetStart()); + assertEquals("Divider inset should be 15", 15, layout.getDividerInsetEnd()); - final ArrayAdapter<String> adapter = - new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1); - adapter.add("Abracadabra"); - layout.setAdapter(adapter); + final Drawable divider = layout.getDivider(); + assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); + } - final ListAdapter gotAdapter = layout.getAdapter(); - // Note: the wrapped adapter should be returned directly, not the HeaderViewListAdapter. - assertSame("Adapter got from GlifListLayout should be same as set", - adapter, gotAdapter); - } - - @Test - public void testDividerInsetLegacy() { - GlifListLayout layout = new GlifListLayout(mContext); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); - } - assertListTemplateInflated(layout); - - layout.setDividerInset(10); - assertEquals("Divider inset should be 10", 10, layout.getDividerInset()); - - final Drawable divider = layout.getDivider(); - assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); - } - - @Test - public void testDividerInsets() { - GlifListLayout layout = new GlifListLayout(mContext); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); - } - assertListTemplateInflated(layout); + private void assertListTemplateInflated(GlifListLayout layout) { + View title = layout.findViewById(R.id.suw_layout_title); + assertNotNull("@id/suw_layout_title should not be null", title); - layout.setDividerInsets(10, 15); - assertEquals("Divider inset should be 10", 10, layout.getDividerInsetStart()); - assertEquals("Divider inset should be 15", 15, layout.getDividerInsetEnd()); + View icon = layout.findViewById(R.id.suw_layout_icon); + assertNotNull("@id/suw_layout_icon should not be null", icon); - final Drawable divider = layout.getDivider(); - assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); - } - - private void assertListTemplateInflated(GlifListLayout layout) { - View title = layout.findViewById(R.id.suw_layout_title); - assertNotNull("@id/suw_layout_title should not be null", title); - - View icon = layout.findViewById(R.id.suw_layout_icon); - assertNotNull("@id/suw_layout_icon should not be null", icon); - - View listView = layout.findViewById(android.R.id.list); - assertTrue("@android:id/list should be a ListView", listView instanceof ListView); - } + View listView = layout.findViewById(android.R.id.list); + assertTrue("@android:id/list should be a ListView", listView instanceof ListView); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifPatternDrawableTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifPatternDrawableTest.java index 37ac41a..1783d4e 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifPatternDrawableTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifPatternDrawableTest.java @@ -25,14 +25,11 @@ import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.os.Debug; +import android.util.Log; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; -import android.util.Log; - import com.android.setupwizardlib.GlifPatternDrawable; - import junit.framework.AssertionFailedError; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,141 +38,144 @@ import org.junit.runner.RunWith; @SmallTest public class GlifPatternDrawableTest { - private static final String TAG = "GlifPatternDrawableTest"; + private static final String TAG = "GlifPatternDrawableTest"; - @Before - public void setUp() throws Exception { - GlifPatternDrawable.invalidatePattern(); - } + @Before + public void setUp() throws Exception { + GlifPatternDrawable.invalidatePattern(); + } - @Test - public void testDraw() { - final Bitmap bitmap = Bitmap.createBitmap(1366, 768, Bitmap.Config.ARGB_8888); - final Canvas canvas = new Canvas(bitmap); + @Test + public void testDraw() { + final Bitmap bitmap = Bitmap.createBitmap(1366, 768, Bitmap.Config.ARGB_8888); + final Canvas canvas = new Canvas(bitmap); - final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); - drawable.setBounds(0, 0, 1366, 768); - drawable.draw(canvas); + final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); + drawable.setBounds(0, 0, 1366, 768); + drawable.draw(canvas); - assertSameColor("Top left pixel should be #e61a1a", 0xffe61a1a, bitmap.getPixel(0, 0)); - assertSameColor("Center pixel should be #d90d0d", 0xffd90d0d, bitmap.getPixel(683, 384)); - assertSameColor("Bottom right pixel should be #d40808", 0xffd40808, - bitmap.getPixel(1365, 767)); - } + assertSameColor("Top left pixel should be #e61a1a", 0xffe61a1a, bitmap.getPixel(0, 0)); + assertSameColor("Center pixel should be #d90d0d", 0xffd90d0d, bitmap.getPixel(683, 384)); + assertSameColor("Bottom right pixel should be #d40808", 0xffd40808, bitmap.getPixel(1365, 767)); + } - @Test - public void testDrawTwice() { - // Test that the second time the drawable is drawn is also correct, to make sure caching is - // done correctly. + @Test + public void testDrawTwice() { + // Test that the second time the drawable is drawn is also correct, to make sure caching is + // done correctly. - final Bitmap bitmap = Bitmap.createBitmap(1366, 768, Bitmap.Config.ARGB_8888); - final Canvas canvas = new Canvas(bitmap); + final Bitmap bitmap = Bitmap.createBitmap(1366, 768, Bitmap.Config.ARGB_8888); + final Canvas canvas = new Canvas(bitmap); - final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); - drawable.setBounds(0, 0, 1366, 768); - drawable.draw(canvas); + final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); + drawable.setBounds(0, 0, 1366, 768); + drawable.draw(canvas); - Paint paint = new Paint(); - paint.setColor(Color.WHITE); - canvas.drawRect(0, 0, 1366, 768, paint); // Erase the entire canvas + Paint paint = new Paint(); + paint.setColor(Color.WHITE); + canvas.drawRect(0, 0, 1366, 768, paint); // Erase the entire canvas - drawable.draw(canvas); + drawable.draw(canvas); - assertSameColor("Top left pixel should be #e61a1a", 0xffe61a1a, bitmap.getPixel(0, 0)); - assertSameColor("Center pixel should be #d90d0d", 0xffd90d0d, bitmap.getPixel(683, 384)); - assertSameColor("Bottom right pixel should be #d40808", 0xffd40808, - bitmap.getPixel(1365, 767)); - } + assertSameColor("Top left pixel should be #e61a1a", 0xffe61a1a, bitmap.getPixel(0, 0)); + assertSameColor("Center pixel should be #d90d0d", 0xffd90d0d, bitmap.getPixel(683, 384)); + assertSameColor("Bottom right pixel should be #d40808", 0xffd40808, bitmap.getPixel(1365, 767)); + } - @Test - public void testScaleToCanvasSquare() { - final Canvas canvas = new Canvas(); - Matrix expected = new Matrix(canvas.getMatrix()); + @Test + public void testScaleToCanvasSquare() { + final Canvas canvas = new Canvas(); + Matrix expected = new Matrix(canvas.getMatrix()); - Bitmap mockBitmapCache = Bitmap.createBitmap(1366, 768, Bitmap.Config.ALPHA_8); + Bitmap mockBitmapCache = Bitmap.createBitmap(1366, 768, Bitmap.Config.ALPHA_8); - final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); - drawable.setBounds(0, 0, 683, 384); // half each side of the view box - drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); + final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); + drawable.setBounds(0, 0, 683, 384); // half each side of the view box + drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); - expected.postScale(0.5f, 0.5f); + expected.postScale(0.5f, 0.5f); - assertEquals("Matrices should match", expected, canvas.getMatrix()); - } + assertEquals("Matrices should match", expected, canvas.getMatrix()); + } - @Test - public void testScaleToCanvasTall() { - final Canvas canvas = new Canvas(); - final Matrix expected = new Matrix(canvas.getMatrix()); + @Test + public void testScaleToCanvasTall() { + final Canvas canvas = new Canvas(); + final Matrix expected = new Matrix(canvas.getMatrix()); - Bitmap mockBitmapCache = Bitmap.createBitmap(1366, 768, Bitmap.Config.ALPHA_8); + Bitmap mockBitmapCache = Bitmap.createBitmap(1366, 768, Bitmap.Config.ALPHA_8); - final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); - drawable.setBounds(0, 0, 683, 768); // half the width only - drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); + final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); + drawable.setBounds(0, 0, 683, 768); // half the width only + drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); - expected.postScale(1f, 1f); - expected.postTranslate(-99.718f, 0f); + expected.postScale(1f, 1f); + expected.postTranslate(-99.718f, 0f); - assertEquals("Matrices should match", expected, canvas.getMatrix()); - } + assertEquals("Matrices should match", expected, canvas.getMatrix()); + } - @Test - public void testScaleToCanvasWide() { - final Canvas canvas = new Canvas(); - final Matrix expected = new Matrix(canvas.getMatrix()); + @Test + public void testScaleToCanvasWide() { + final Canvas canvas = new Canvas(); + final Matrix expected = new Matrix(canvas.getMatrix()); - Bitmap mockBitmapCache = Bitmap.createBitmap(1366, 768, Bitmap.Config.ALPHA_8); + Bitmap mockBitmapCache = Bitmap.createBitmap(1366, 768, Bitmap.Config.ALPHA_8); - final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); - drawable.setBounds(0, 0, 1366, 384); // half the height only - drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); + final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); + drawable.setBounds(0, 0, 1366, 384); // half the height only + drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); - expected.postScale(1f, 1f); - expected.postTranslate(0f, -87.552f); + expected.postScale(1f, 1f); + expected.postTranslate(0f, -87.552f); - assertEquals("Matrices should match", expected, canvas.getMatrix()); - } + assertEquals("Matrices should match", expected, canvas.getMatrix()); + } - @Test - public void testScaleToCanvasMaxSize() { - final Canvas canvas = new Canvas(); - final Matrix expected = new Matrix(canvas.getMatrix()); + @Test + public void testScaleToCanvasMaxSize() { + final Canvas canvas = new Canvas(); + final Matrix expected = new Matrix(canvas.getMatrix()); - Bitmap mockBitmapCache = Bitmap.createBitmap(2049, 1152, Bitmap.Config.ALPHA_8); + Bitmap mockBitmapCache = Bitmap.createBitmap(2049, 1152, Bitmap.Config.ALPHA_8); - final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); - drawable.setBounds(0, 0, 1366, 768); // original viewbox size - drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); + final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); + drawable.setBounds(0, 0, 1366, 768); // original viewbox size + drawable.scaleCanvasToBounds(canvas, mockBitmapCache, drawable.getBounds()); - expected.postScale(1 / 1.5f, 1 / 1.5f); - expected.postTranslate(0f, 0f); + expected.postScale(1 / 1.5f, 1 / 1.5f); + expected.postTranslate(0f, 0f); - assertEquals("Matrices should match", expected, canvas.getMatrix()); - } + assertEquals("Matrices should match", expected, canvas.getMatrix()); + } - @Test - public void testMemoryAllocation() { - Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo(); - Debug.getMemoryInfo(memoryInfo); - final long memoryBefore = memoryInfo.getTotalPss(); // Get memory usage in KB + @Test + public void testMemoryAllocation() { + Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo(); + Debug.getMemoryInfo(memoryInfo); + final long memoryBefore = memoryInfo.getTotalPss(); // Get memory usage in KB - final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); - drawable.setBounds(0, 0, 1366, 768); - drawable.createBitmapCache(2049, 1152); + final GlifPatternDrawable drawable = new GlifPatternDrawable(Color.RED); + drawable.setBounds(0, 0, 1366, 768); + drawable.createBitmapCache(2049, 1152); - Debug.getMemoryInfo(memoryInfo); - final long memoryAfter = memoryInfo.getTotalPss(); - Log.i(TAG, "Memory allocated for bitmap cache: " + (memoryAfter - memoryBefore)); - assertTrue("Memory allocation should not exceed 5MB", memoryAfter < memoryBefore + 5000); - } + Debug.getMemoryInfo(memoryInfo); + final long memoryAfter = memoryInfo.getTotalPss(); + Log.i(TAG, "Memory allocated for bitmap cache: " + (memoryAfter - memoryBefore)); + assertTrue("Memory allocation should not exceed 5MB", memoryAfter < memoryBefore + 5000); + } - private void assertSameColor(String message, int expected, int actual) { - try { - assertEquals(expected, actual); - } catch (AssertionFailedError e) { - throw new AssertionFailedError(message + " expected <#" + Integer.toHexString(expected) - + "> but found <#" + Integer.toHexString(actual) + "> instead"); - } + private void assertSameColor(String message, int expected, int actual) { + try { + assertEquals(expected, actual); + } catch (AssertionFailedError e) { + throw new AssertionFailedError( + message + + " expected <#" + + Integer.toHexString(expected) + + "> but found <#" + + Integer.toHexString(actual) + + "> instead"); } + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/IllustrationTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/IllustrationTest.java index a4b6f27..253893e 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/IllustrationTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/IllustrationTest.java @@ -23,13 +23,11 @@ import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; +import android.view.View; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; -import android.view.View; - import com.android.setupwizardlib.view.Illustration; - import org.junit.Test; import org.junit.runner.RunWith; @@ -37,28 +35,29 @@ import org.junit.runner.RunWith; @SmallTest public class IllustrationTest { - @Test - public void testWillDraw() { - final Illustration illustration = new Illustration(InstrumentationRegistry.getContext()); - assertFalse("The illustration needs to be drawn", illustration.willNotDraw()); - } - - @Test - public void testAspectRatio() { - final Context context = InstrumentationRegistry.getContext(); - // Force the context to be xhdpi - context.getResources().getDisplayMetrics().density = 2.0f; - - final Illustration illustration = new Illustration(context); - illustration.setAspectRatio(3.0f); - final Drawable backgroundDrawable = new ColorDrawable(Color.RED); - final Drawable illustrationDrawable = new ColorDrawable(Color.BLUE); - illustration.setBackgroundDrawable(backgroundDrawable); - illustration.setIllustration(illustrationDrawable); - - illustration.measure(View.MeasureSpec.makeMeasureSpec(300, View.MeasureSpec.EXACTLY), - View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); - // (300px / 3) round down to nearest mod (8dp = 16px) = 96px - assertEquals("Top padding should be 96", 96, illustration.getPaddingTop()); - } + @Test + public void testWillDraw() { + final Illustration illustration = new Illustration(InstrumentationRegistry.getContext()); + assertFalse("The illustration needs to be drawn", illustration.willNotDraw()); + } + + @Test + public void testAspectRatio() { + final Context context = InstrumentationRegistry.getContext(); + // Force the context to be xhdpi + context.getResources().getDisplayMetrics().density = 2.0f; + + final Illustration illustration = new Illustration(context); + illustration.setAspectRatio(3.0f); + final Drawable backgroundDrawable = new ColorDrawable(Color.RED); + final Drawable illustrationDrawable = new ColorDrawable(Color.BLUE); + illustration.setBackgroundDrawable(backgroundDrawable); + illustration.setIllustration(illustrationDrawable); + + illustration.measure( + View.MeasureSpec.makeMeasureSpec(300, View.MeasureSpec.EXACTLY), + View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); + // (300px / 3) round down to nearest mod (8dp = 16px) = 96px + assertEquals("Top padding should be 96", 96, illustration.getPaddingTop()); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemAdapterTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemAdapterTest.java index e5875e4..63180dc 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemAdapterTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemAdapterTest.java @@ -24,78 +24,74 @@ import static org.mockito.Mockito.mock; import android.database.DataSetObserver; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; - import com.android.setupwizardlib.items.Item; import com.android.setupwizardlib.items.ItemAdapter; import com.android.setupwizardlib.items.ItemGroup; import com.android.setupwizardlib.items.ItemHierarchy; - +import java.util.Arrays; +import java.util.HashSet; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InOrder; -import java.util.Arrays; -import java.util.HashSet; - @RunWith(AndroidJUnit4.class) @SmallTest public class ItemAdapterTest { - private Item[] mItems = new Item[5]; - private ItemGroup mItemGroup = new ItemGroup(); - - @Before - public void setUp() throws Exception { - for (int i = 0; i < 5; i++) { - Item item = new Item(); - item.setTitle("TestTitle" + i); - item.setId(i); - item.setLayoutResource(((i % 3) + 1) * 10); - mItems[i] = item; - mItemGroup.addChild(item); - } - } - - @Test - public void testAdapter() { - ItemAdapter adapter = new ItemAdapter(mItemGroup); - assertEquals("Adapter should have 5 items", 5, adapter.getCount()); - assertEquals("Adapter should return the first item", mItems[0], adapter.getItem(0)); - assertEquals("ID should be same as position", 2, adapter.getItemId(2)); - - // Each test item has its own layout resource, and therefore its own view type - assertEquals("Should have 3 different view types", 3, adapter.getViewTypeCount()); - HashSet<Integer> viewTypes = new HashSet<>(3); - viewTypes.add(adapter.getItemViewType(0)); - viewTypes.add(adapter.getItemViewType(1)); - viewTypes.add(adapter.getItemViewType(2)); - - assertEquals("View types should be 0, 1, 2", - new HashSet<>(Arrays.asList(0, 1, 2)), viewTypes); - } - - @Test - public void testGetRootItemHierarchy() { - ItemAdapter adapter = new ItemAdapter(mItemGroup); - ItemHierarchy root = adapter.getRootItemHierarchy(); - assertSame("Root item hierarchy should be mItemGroup", mItemGroup, root); - } - - @Test - public void testAdapterNotifications() { - ItemAdapter adapter = new ItemAdapter(mItemGroup); - final DataSetObserver observer = mock(DataSetObserver.class); - adapter.registerDataSetObserver(observer); - final InOrder inOrder = inOrder(observer); - - mItems[0].setTitle("Child 1"); - inOrder.verify(observer).onChanged(); - - mItemGroup.removeChild(mItems[1]); - inOrder.verify(observer).onChanged(); - - mItemGroup.addChild(mItems[1]); - inOrder.verify(observer).onChanged(); + private Item[] mItems = new Item[5]; + private ItemGroup mItemGroup = new ItemGroup(); + + @Before + public void setUp() throws Exception { + for (int i = 0; i < 5; i++) { + Item item = new Item(); + item.setTitle("TestTitle" + i); + item.setId(i); + item.setLayoutResource(((i % 3) + 1) * 10); + mItems[i] = item; + mItemGroup.addChild(item); } + } + + @Test + public void testAdapter() { + ItemAdapter adapter = new ItemAdapter(mItemGroup); + assertEquals("Adapter should have 5 items", 5, adapter.getCount()); + assertEquals("Adapter should return the first item", mItems[0], adapter.getItem(0)); + assertEquals("ID should be same as position", 2, adapter.getItemId(2)); + + // Each test item has its own layout resource, and therefore its own view type + assertEquals("Should have 3 different view types", 3, adapter.getViewTypeCount()); + HashSet<Integer> viewTypes = new HashSet<>(3); + viewTypes.add(adapter.getItemViewType(0)); + viewTypes.add(adapter.getItemViewType(1)); + viewTypes.add(adapter.getItemViewType(2)); + + assertEquals("View types should be 0, 1, 2", new HashSet<>(Arrays.asList(0, 1, 2)), viewTypes); + } + + @Test + public void testGetRootItemHierarchy() { + ItemAdapter adapter = new ItemAdapter(mItemGroup); + ItemHierarchy root = adapter.getRootItemHierarchy(); + assertSame("Root item hierarchy should be mItemGroup", mItemGroup, root); + } + + @Test + public void testAdapterNotifications() { + ItemAdapter adapter = new ItemAdapter(mItemGroup); + final DataSetObserver observer = mock(DataSetObserver.class); + adapter.registerDataSetObserver(observer); + final InOrder inOrder = inOrder(observer); + + mItems[0].setTitle("Child 1"); + inOrder.verify(observer).onChanged(); + + mItemGroup.removeChild(mItems[1]); + inOrder.verify(observer).onChanged(); + + mItemGroup.addChild(mItems[1]); + inOrder.verify(observer).onChanged(); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemInflaterTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemInflaterTest.java index 20fd2cc..9e96bae 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemInflaterTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemInflaterTest.java @@ -22,12 +22,10 @@ import static org.junit.Assert.assertTrue; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; - import com.android.setupwizardlib.items.Item; import com.android.setupwizardlib.items.ItemGroup; import com.android.setupwizardlib.items.ItemHierarchy; import com.android.setupwizardlib.items.ItemInflater; - import org.junit.Test; import org.junit.runner.RunWith; @@ -35,25 +33,26 @@ import org.junit.runner.RunWith; @SmallTest public class ItemInflaterTest { - @Test - public void testDefaultPackage() { - ItemInflater inflater = new ItemInflater(InstrumentationRegistry.getContext()); - assertEquals("Default package should be the one containing Item class", - "com.android.setupwizardlib.items.", inflater.getDefaultPackage()); - } - - @Test - public void testInflate() { - ItemInflater inflater = new ItemInflater(InstrumentationRegistry.getContext()); - ItemHierarchy item = inflater.inflate(R.xml.test_items); - assertTrue("Inflated item should be ItemGroup", item instanceof ItemGroup); - ItemGroup itemGroup = (ItemGroup) item; - - Item child0 = (Item) itemGroup.getItemAt(0); - Item child1 = (Item) itemGroup.getItemAt(1); - assertEquals("Title of first child should be Title1", "Title1", child0.getTitle()); - assertEquals("ID of second child should be test_item_2", R.id.test_item_2, child1.getId()); - assertEquals("Summary of second child should be Summary2", "Summary2", - child1.getSummary()); - } + @Test + public void testDefaultPackage() { + ItemInflater inflater = new ItemInflater(InstrumentationRegistry.getContext()); + assertEquals( + "Default package should be the one containing Item class", + "com.android.setupwizardlib.items.", + inflater.getDefaultPackage()); + } + + @Test + public void testInflate() { + ItemInflater inflater = new ItemInflater(InstrumentationRegistry.getContext()); + ItemHierarchy item = inflater.inflate(R.xml.test_items); + assertTrue("Inflated item should be ItemGroup", item instanceof ItemGroup); + ItemGroup itemGroup = (ItemGroup) item; + + Item child0 = (Item) itemGroup.getItemAt(0); + Item child1 = (Item) itemGroup.getItemAt(1); + assertEquals("Title of first child should be Title1", "Title1", child0.getTitle()); + assertEquals("ID of second child should be test_item_2", R.id.test_item_2, child1.getId()); + assertEquals("Summary of second child should be Summary2", "Summary2", child1.getSummary()); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemLayoutTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemLayoutTest.java index 85876b4..dbf71b2 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemLayoutTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemLayoutTest.java @@ -17,26 +17,22 @@ package com.android.setupwizardlib.test; import static android.support.test.InstrumentationRegistry.getTargetContext; - import static org.junit.Assert.assertNotNull; import android.content.Context; -import android.support.test.filters.SmallTest; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.widget.FrameLayout; - +import android.support.test.filters.SmallTest; import com.android.setupwizardlib.R; import com.android.setupwizardlib.items.Item; - +import java.util.ArrayList; +import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -import java.util.ArrayList; -import java.util.List; - /** * Sanity test for all the item layouts to make sure they won't crash when being inflated in * different themes. @@ -45,50 +41,50 @@ import java.util.List; @SmallTest public class ItemLayoutTest { - @Parameters - public static Iterable<Object[]> data() { - int[] themes = new int[] { - R.style.SuwThemeMaterial_Light, - R.style.SuwThemeMaterial, - R.style.SuwThemeGlif_Light, - R.style.SuwThemeGlif, - R.style.SuwThemeGlifV2_Light, - R.style.SuwThemeGlifV2 + @Parameters + public static Iterable<Object[]> data() { + int[] themes = + new int[] { + R.style.SuwThemeMaterial_Light, + R.style.SuwThemeMaterial, + R.style.SuwThemeGlif_Light, + R.style.SuwThemeGlif, + R.style.SuwThemeGlifV2_Light, + R.style.SuwThemeGlifV2 }; - int[] layouts = new int[] { - R.layout.suw_items_default, - R.layout.suw_items_verbose, - R.layout.suw_items_description + int[] layouts = + new int[] { + R.layout.suw_items_default, R.layout.suw_items_verbose, R.layout.suw_items_description }; - // Test all the possible combinations of themes and layouts. - List<Object[]> params = new ArrayList<>(); - for (int theme : themes) { - for (int layout : layouts) { - params.add(new Object[] { theme, layout }); - } - } - return params; + // Test all the possible combinations of themes and layouts. + List<Object[]> params = new ArrayList<>(); + for (int theme : themes) { + for (int layout : layouts) { + params.add(new Object[] {theme, layout}); + } } + return params; + } - private final Context mContext; - private final FrameLayout mParent; - private final Item mItem; + private final Context mContext; + private final FrameLayout mParent; + private final Item mItem; - public ItemLayoutTest(int theme, int layout) { - mContext = new ContextThemeWrapper(getTargetContext(), theme); - mParent = new FrameLayout(mContext); - mItem = new Item(); - mItem.setLayoutResource(layout); - } + public ItemLayoutTest(int theme, int layout) { + mContext = new ContextThemeWrapper(getTargetContext(), theme); + mParent = new FrameLayout(mContext); + mItem = new Item(); + mItem.setLayoutResource(layout); + } - @Test - public void testInflateLayoutHasBasicViews() { - LayoutInflater.from(mContext).inflate(mItem.getLayoutResource(), mParent, true); - mItem.onBindView(mParent); + @Test + public void testInflateLayoutHasBasicViews() { + LayoutInflater.from(mContext).inflate(mItem.getLayoutResource(), mParent, true); + mItem.onBindView(mParent); - assertNotNull("Title should exist", mParent.findViewById(R.id.suw_items_title)); - assertNotNull("Summary should exist", mParent.findViewById(R.id.suw_items_summary)); - assertNotNull("Icon should exist", mParent.findViewById(R.id.suw_items_icon)); - } + assertNotNull("Title should exist", mParent.findViewById(R.id.suw_items_title)); + assertNotNull("Summary should exist", mParent.findViewById(R.id.suw_items_summary)); + assertNotNull("Icon should exist", mParent.findViewById(R.id.suw_items_icon)); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemTest.java index b4ebabb..84990dd 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/ItemTest.java @@ -28,19 +28,17 @@ import static org.mockito.Mockito.verify; import android.content.Context; import android.graphics.drawable.Drawable; import android.graphics.drawable.ShapeDrawable; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.R; import com.android.setupwizardlib.items.Item; import com.android.setupwizardlib.items.ItemHierarchy.Observer; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -52,149 +50,149 @@ import org.mockito.MockitoAnnotations; @SmallTest public class ItemTest { - private TextView mTitleView; - private TextView mSummaryView; - private ImageView mIconView; - private FrameLayout mIconContainer; - - @Mock - private Observer mObserver; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - } - - @Test - public void testOnBindView() { - Item item = new Item(); - item.setTitle("TestTitle"); - item.setSummary("TestSummary"); - Drawable icon = new ShapeDrawable(); - icon.setLevel(4); - item.setIcon(icon); - View view = createLayout(); - - mIconView.setImageLevel(1); - Drawable recycledIcon = new ShapeDrawable(); - mIconView.setImageDrawable(recycledIcon); - - item.onBindView(view); - - assertEquals("Title should be \"TestTitle\"", "TestTitle", mTitleView.getText().toString()); - assertEquals("Summary should be \"TestSummary\"", "TestSummary", - mSummaryView.getText().toString()); - assertSame("Icon should be the icon shape drawable", icon, mIconView.getDrawable()); - assertEquals("Recycled icon level should not change", 1, recycledIcon.getLevel()); - assertEquals("Icon should be level 4", 4, icon.getLevel()); - } - - @Test - public void testSingleLineItem() { - Item item = new Item(); - item.setTitle("TestTitle"); - View view = createLayout(); - - item.onBindView(view); - - assertEquals("Title should be \"TestTitle\"", "TestTitle", mTitleView.getText().toString()); - assertEquals("Summary should be gone", View.GONE, mSummaryView.getVisibility()); - assertEquals("IconContainer should be gone", View.GONE, mIconContainer.getVisibility()); - } - - @Test - public void testProperties() { - Item item = new Item(); - item.registerObserver(mObserver); - final InOrder inOrder = inOrder(mObserver); - - item.setTitle("TestTitle"); - inOrder.verify(mObserver).onItemRangeChanged(eq(item), eq(0), eq(1)); - - item.setSummary("TestSummary"); - inOrder.verify(mObserver).onItemRangeChanged(eq(item), eq(0), eq(1)); - - item.setEnabled(false); - inOrder.verify(mObserver).onItemRangeChanged(eq(item), eq(0), eq(1)); - - ShapeDrawable icon = new ShapeDrawable(); - item.setIcon(icon); - inOrder.verify(mObserver).onItemRangeChanged(eq(item), eq(0), eq(1)); - - item.setId(12345); - - item.setLayoutResource(56789); - inOrder.verify(mObserver).onItemRangeChanged(eq(item), eq(0), eq(1)); - - assertEquals("Title should be \"TestTitle\"", "TestTitle", item.getTitle()); - assertEquals("Summary should be \"TestSummary\"", "TestSummary", item.getSummary()); - assertFalse("Enabled should be false", item.isEnabled()); - assertSame("Icon should be same as set", icon, item.getIcon()); - assertEquals("ID should be 12345", 12345, item.getId()); - assertEquals("Layout resource should be 56789", 56789, item.getLayoutResource()); - } - - @Test - public void testDefaultValues() { - Item item = new Item(); - - assertNull("Default title should be null", item.getTitle()); - assertNull("Default summary should be null", item.getSummary()); - assertNull("Default icon should be null", item.getIcon()); - assertTrue("Default enabled should be true", item.isEnabled()); - assertEquals("Default ID should be 0", 0, item.getId()); - assertEquals("Default layout resource should be R.layout.suw_items_text", - R.layout.suw_items_default, item.getLayoutResource()); - assertTrue("Default visible should be true", item.isVisible()); - } - - @Test - public void testHierarchyImplementation() { - Item item = new Item(); - item.setId(12345); - - assertEquals("getCount should be 1", 1, item.getCount()); - assertSame("getItemAt should return itself", item, item.getItemAt(0)); - assertSame("findItemById with same ID should return itself", item, - item.findItemById(12345)); - assertNull("findItemById with different ID should return null", item.findItemById(34567)); - } - - @Test - public void testVisible() { - Item item = new Item(); - item.registerObserver(mObserver); - item.setVisible(false); - - assertFalse("Item should not be visible", item.isVisible()); - assertEquals("Item count should be 0 when not visible", 0, item.getCount()); - - verify(mObserver).onItemRangeRemoved(eq(item), eq(0), eq(1)); - - item.setVisible(true); - verify(mObserver).onItemRangeInserted(eq(item), eq(0), eq(1)); - } - - private ViewGroup createLayout() { - Context context = InstrumentationRegistry.getContext(); - ViewGroup root = new FrameLayout(context); - - mTitleView = new TextView(context); - mTitleView.setId(R.id.suw_items_title); - root.addView(mTitleView); - - mSummaryView = new TextView(context); - mSummaryView.setId(R.id.suw_items_summary); - root.addView(mSummaryView); - - mIconContainer = new FrameLayout(context); - mIconContainer.setId(R.id.suw_items_icon_container); - root.addView(mIconContainer); - - mIconView = new ImageView(context); - mIconView.setId(R.id.suw_items_icon); - mIconContainer.addView(mIconView); - - return root; - } + private TextView mTitleView; + private TextView mSummaryView; + private ImageView mIconView; + private FrameLayout mIconContainer; + + @Mock private Observer mObserver; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testOnBindView() { + Item item = new Item(); + item.setTitle("TestTitle"); + item.setSummary("TestSummary"); + Drawable icon = new ShapeDrawable(); + icon.setLevel(4); + item.setIcon(icon); + View view = createLayout(); + + mIconView.setImageLevel(1); + Drawable recycledIcon = new ShapeDrawable(); + mIconView.setImageDrawable(recycledIcon); + + item.onBindView(view); + + assertEquals("Title should be \"TestTitle\"", "TestTitle", mTitleView.getText().toString()); + assertEquals( + "Summary should be \"TestSummary\"", "TestSummary", mSummaryView.getText().toString()); + assertSame("Icon should be the icon shape drawable", icon, mIconView.getDrawable()); + assertEquals("Recycled icon level should not change", 1, recycledIcon.getLevel()); + assertEquals("Icon should be level 4", 4, icon.getLevel()); + } + + @Test + public void testSingleLineItem() { + Item item = new Item(); + item.setTitle("TestTitle"); + View view = createLayout(); + + item.onBindView(view); + + assertEquals("Title should be \"TestTitle\"", "TestTitle", mTitleView.getText().toString()); + assertEquals("Summary should be gone", View.GONE, mSummaryView.getVisibility()); + assertEquals("IconContainer should be gone", View.GONE, mIconContainer.getVisibility()); + } + + @Test + public void testProperties() { + Item item = new Item(); + item.registerObserver(mObserver); + final InOrder inOrder = inOrder(mObserver); + + item.setTitle("TestTitle"); + inOrder.verify(mObserver).onItemRangeChanged(eq(item), eq(0), eq(1)); + + item.setSummary("TestSummary"); + inOrder.verify(mObserver).onItemRangeChanged(eq(item), eq(0), eq(1)); + + item.setEnabled(false); + inOrder.verify(mObserver).onItemRangeChanged(eq(item), eq(0), eq(1)); + + ShapeDrawable icon = new ShapeDrawable(); + item.setIcon(icon); + inOrder.verify(mObserver).onItemRangeChanged(eq(item), eq(0), eq(1)); + + item.setId(12345); + + item.setLayoutResource(56789); + inOrder.verify(mObserver).onItemRangeChanged(eq(item), eq(0), eq(1)); + + assertEquals("Title should be \"TestTitle\"", "TestTitle", item.getTitle()); + assertEquals("Summary should be \"TestSummary\"", "TestSummary", item.getSummary()); + assertFalse("Enabled should be false", item.isEnabled()); + assertSame("Icon should be same as set", icon, item.getIcon()); + assertEquals("ID should be 12345", 12345, item.getId()); + assertEquals("Layout resource should be 56789", 56789, item.getLayoutResource()); + } + + @Test + public void testDefaultValues() { + Item item = new Item(); + + assertNull("Default title should be null", item.getTitle()); + assertNull("Default summary should be null", item.getSummary()); + assertNull("Default icon should be null", item.getIcon()); + assertTrue("Default enabled should be true", item.isEnabled()); + assertEquals("Default ID should be 0", 0, item.getId()); + assertEquals( + "Default layout resource should be R.layout.suw_items_text", + R.layout.suw_items_default, + item.getLayoutResource()); + assertTrue("Default visible should be true", item.isVisible()); + } + + @Test + public void testHierarchyImplementation() { + Item item = new Item(); + item.setId(12345); + + assertEquals("getCount should be 1", 1, item.getCount()); + assertSame("getItemAt should return itself", item, item.getItemAt(0)); + assertSame("findItemById with same ID should return itself", item, item.findItemById(12345)); + assertNull("findItemById with different ID should return null", item.findItemById(34567)); + } + + @Test + public void testVisible() { + Item item = new Item(); + item.registerObserver(mObserver); + item.setVisible(false); + + assertFalse("Item should not be visible", item.isVisible()); + assertEquals("Item count should be 0 when not visible", 0, item.getCount()); + + verify(mObserver).onItemRangeRemoved(eq(item), eq(0), eq(1)); + + item.setVisible(true); + verify(mObserver).onItemRangeInserted(eq(item), eq(0), eq(1)); + } + + private ViewGroup createLayout() { + Context context = InstrumentationRegistry.getContext(); + ViewGroup root = new FrameLayout(context); + + mTitleView = new TextView(context); + mTitleView.setId(R.id.suw_items_title); + root.addView(mTitleView); + + mSummaryView = new TextView(context); + mSummaryView.setId(R.id.suw_items_summary); + root.addView(mSummaryView); + + mIconContainer = new FrameLayout(context); + mIconContainer.setId(R.id.suw_items_icon_container); + root.addView(mIconContainer); + + mIconView = new ImageView(context); + mIconView.setId(R.id.suw_items_icon); + mIconContainer.addView(mIconView); + + return root; + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/ReflectionInflaterTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/ReflectionInflaterTest.java index 137a146..69e5882 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/ReflectionInflaterTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/ReflectionInflaterTest.java @@ -20,64 +20,59 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; +import androidx.annotation.NonNull; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.ScaleAnimation; - -import androidx.annotation.NonNull; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.items.ReflectionInflater; - +import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; -import java.util.List; - @SmallTest @RunWith(AndroidJUnit4.class) public class ReflectionInflaterTest { - @Test - public void testInflateXml() { - final Context context = InstrumentationRegistry.getContext(); - TestInflater inflater = new TestInflater(context); - final Animation result = inflater.inflate(R.xml.reflection_inflater_test); + @Test + public void testInflateXml() { + final Context context = InstrumentationRegistry.getContext(); + TestInflater inflater = new TestInflater(context); + final Animation result = inflater.inflate(R.xml.reflection_inflater_test); - assertTrue(result instanceof AnimationSet); - final AnimationSet set = (AnimationSet) result; - final List<Animation> animations = set.getAnimations(); - assertEquals(1, animations.size()); - assertTrue(animations.get(0) instanceof ScaleAnimation); - } + assertTrue(result instanceof AnimationSet); + final AnimationSet set = (AnimationSet) result; + final List<Animation> animations = set.getAnimations(); + assertEquals(1, animations.size()); + assertTrue(animations.get(0) instanceof ScaleAnimation); + } - @Test - public void testDefaultPackage() { - final Context context = InstrumentationRegistry.getContext(); - TestInflater inflater = new TestInflater(context); - inflater.setDefaultPackage("android.view.animation."); - final Animation result = - inflater.inflate(R.xml.reflection_inflater_test_with_default_package); + @Test + public void testDefaultPackage() { + final Context context = InstrumentationRegistry.getContext(); + TestInflater inflater = new TestInflater(context); + inflater.setDefaultPackage("android.view.animation."); + final Animation result = inflater.inflate(R.xml.reflection_inflater_test_with_default_package); - assertTrue(result instanceof AnimationSet); - final AnimationSet set = (AnimationSet) result; - final List<Animation> animations = set.getAnimations(); - assertEquals(1, animations.size()); - assertTrue(animations.get(0) instanceof ScaleAnimation); - } + assertTrue(result instanceof AnimationSet); + final AnimationSet set = (AnimationSet) result; + final List<Animation> animations = set.getAnimations(); + assertEquals(1, animations.size()); + assertTrue(animations.get(0) instanceof ScaleAnimation); + } - private static class TestInflater extends ReflectionInflater<Animation> { + private static class TestInflater extends ReflectionInflater<Animation> { - protected TestInflater(@NonNull Context context) { - super(context); - } + protected TestInflater(@NonNull Context context) { + super(context); + } - @Override - protected void onAddChildItem(Animation parent, Animation child) { - final AnimationSet group = (AnimationSet) parent; - group.addAnimation(child); - } + @Override + protected void onAddChildItem(Animation parent, Animation child) { + final AnimationSet group = (AnimationSet) parent; + group.addAnimation(child); } + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/SetupWizardLayoutTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/SetupWizardLayoutTest.java index 531d69e..9d2f784 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/SetupWizardLayoutTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/SetupWizardLayoutTest.java @@ -27,9 +27,7 @@ import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Parcelable; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; +import androidx.annotation.IdRes; import android.util.SparseArray; import android.view.AbsSavedState; import android.view.ContextThemeWrapper; @@ -37,15 +35,14 @@ import android.view.LayoutInflater; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; - -import androidx.annotation.IdRes; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.SetupWizardLayout; import com.android.setupwizardlib.template.HeaderMixin; import com.android.setupwizardlib.template.NavigationBarMixin; import com.android.setupwizardlib.template.ProgressBarMixin; import com.android.setupwizardlib.view.NavigationBar; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -54,191 +51,196 @@ import org.junit.runner.RunWith; @SmallTest public class SetupWizardLayoutTest { - @IdRes - private static final int ID1234 = 1234; - - private Context mContext; - - @Before - public void setUp() throws Exception { - mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(), - R.style.SuwThemeMaterial_Light); - } - - @Test - public void testDefaultTemplate() { - SetupWizardLayout layout = new SetupWizardLayout(mContext); - assertDefaultTemplateInflated(layout); - } - - @Test - public void testSetHeaderText() { - SetupWizardLayout layout = new SetupWizardLayout(mContext); - TextView title = (TextView) layout.findViewById(R.id.suw_layout_title); - layout.setHeaderText("Abracadabra"); - assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText()); - } - - @Test - public void testAddView() { - SetupWizardLayout layout = new SetupWizardLayout(mContext); - TextView tv = new TextView(mContext); - tv.setId(R.id.test_view_id); - layout.addView(tv); - assertDefaultTemplateInflated(layout); - View view = layout.findViewById(R.id.test_view_id); - assertSame("The view added should be the same text view", tv, view); - } - - @Test - public void testInflateFromXml() { - LayoutInflater inflater = LayoutInflater.from(mContext); - SetupWizardLayout layout = (SetupWizardLayout) inflater.inflate(R.layout.test_layout, null); - assertDefaultTemplateInflated(layout); - View content = layout.findViewById(R.id.test_content); - assertTrue("@id/test_content should be a TextView", content instanceof TextView); - } - - @Test - public void testCustomTemplate() { - SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template); - View templateView = layout.findViewById(R.id.test_template_view); - assertNotNull("@id/test_template_view should exist in template", templateView); - - TextView tv = new TextView(mContext); - tv.setId(R.id.test_view_id); - layout.addView(tv); - - templateView = layout.findViewById(R.id.test_template_view); - assertNotNull("@id/test_template_view should exist in template", templateView); - View contentView = layout.findViewById(R.id.test_view_id); - assertSame("The view added should be the same text view", tv, contentView); - - // The following methods should be no-ops because the custom template doesn't contain the - // corresponding optional views. Just check that they don't throw exceptions. - layout.setHeaderText("Abracadabra"); - layout.setIllustration(new ColorDrawable(Color.MAGENTA)); - layout.setLayoutBackground(new ColorDrawable(Color.RED)); - } - - @Test - public void testGetNavigationBar() { - final SetupWizardLayout layout = new SetupWizardLayout(mContext); - final NavigationBar navigationBar = layout.getNavigationBar(); - assertEquals("Navigation bar should have ID = @id/suw_layout_navigation_bar", - R.id.suw_layout_navigation_bar, navigationBar.getId()); - } - - @Test - public void testGetNavigationBarNull() { - // test_template does not have navigation bar so getNavigationBar() should return null. - final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template); - final NavigationBar navigationBar = layout.getNavigationBar(); - assertNull("getNavigationBar() in test_template should return null", navigationBar); - } - - @Test - public void testShowProgressBar() { - final SetupWizardLayout layout = new SetupWizardLayout(mContext); - layout.showProgressBar(); - assertTrue("Progress bar should be shown", layout.isProgressBarShown()); - final View progressBar = layout.findViewById(R.id.suw_layout_progress); - assertTrue("Progress bar view should be shown", - progressBar instanceof ProgressBar && progressBar.getVisibility() == View.VISIBLE); - } - - @Test - public void testHideProgressBar() { - final SetupWizardLayout layout = new SetupWizardLayout(mContext); - layout.showProgressBar(); - assertTrue("Progress bar should be shown", layout.isProgressBarShown()); - layout.hideProgressBar(); - assertFalse("Progress bar should be hidden", layout.isProgressBarShown()); - final View progressBar = layout.findViewById(R.id.suw_layout_progress); - assertTrue("Progress bar view should exist", - progressBar == null || progressBar.getVisibility() != View.VISIBLE); - } - - @Test - public void testShowProgressBarNotExist() { - // test_template does not have progress bar, so showNavigationBar() should do nothing. - final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template); - layout.showProgressBar(); - assertFalse("Progress bar should not be shown", layout.isProgressBarShown()); - } - - @Test - public void testNonMaterialTheme() { - mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(), - android.R.style.Theme); - new SetupWizardLayout(mContext); - // Inflating with a non-Material theme should not crash - } - - @Test - public void testOnRestoreFromInstanceState() { - final SetupWizardLayout layout = new SetupWizardLayout(mContext); - layout.setId(ID1234); - - SparseArray<Parcelable> container = new SparseArray<>(); - layout.saveHierarchyState(container); - - final SetupWizardLayout layout2 = new SetupWizardLayout(mContext); - layout2.setId(ID1234); - layout2.restoreHierarchyState(container); - - assertFalse("Progress bar should not be shown", layout2.isProgressBarShown()); - } - - @Test - public void testOnRestoreFromInstanceStateProgressBarShown() { - final SetupWizardLayout layout = new SetupWizardLayout(mContext); - layout.setId(ID1234); - - layout.setProgressBarShown(true); - - SparseArray<Parcelable> container = new SparseArray<>(); - layout.saveHierarchyState(container); - - final SetupWizardLayout layout2 = new SetupWizardLayout(mContext); - layout2.setId(ID1234); - layout2.restoreHierarchyState(container); - - assertTrue("Progress bar should be shown", layout2.isProgressBarShown()); - } - - @Test - public void testOnRestoreFromIncompatibleInstanceState() { - final SetupWizardLayout layout = new SetupWizardLayout(mContext); - layout.setId(ID1234); - - SparseArray<Parcelable> container = new SparseArray<>(); - container.put(1234, AbsSavedState.EMPTY_STATE); - layout.restoreHierarchyState(container); - - // SetupWizardLayout shouldn't crash with incompatible Parcelable - - assertFalse("Progress bar should not be shown", layout.isProgressBarShown()); - } - - @Test - public void testGetMixins() { - final SetupWizardLayout layout = new SetupWizardLayout(mContext); - assertNotNull("SetupWizardLayout should have header mixin", - layout.getMixin(HeaderMixin.class)); - assertNotNull("SetupWizardLayout should have progress bar mixin", - layout.getMixin(ProgressBarMixin.class)); - assertNotNull("SetupWizardLayout should have navigation bar mixin", - layout.getMixin(NavigationBarMixin.class)); - } - - private void assertDefaultTemplateInflated(SetupWizardLayout layout) { - View decorView = layout.findViewById(R.id.suw_layout_decor); - View navbar = layout.findViewById(R.id.suw_layout_navigation_bar); - View title = layout.findViewById(R.id.suw_layout_title); - assertNotNull("@id/suw_layout_decor_view should not be null", decorView); - assertTrue("@id/suw_layout_navigation_bar should be an instance of NavigationBar", - navbar instanceof NavigationBar); - assertNotNull("@id/suw_layout_title should not be null", title); - } + @IdRes private static final int ID1234 = 1234; + + private Context mContext; + + @Before + public void setUp() throws Exception { + mContext = + new ContextThemeWrapper( + InstrumentationRegistry.getContext(), R.style.SuwThemeMaterial_Light); + } + + @Test + public void testDefaultTemplate() { + SetupWizardLayout layout = new SetupWizardLayout(mContext); + assertDefaultTemplateInflated(layout); + } + + @Test + public void testSetHeaderText() { + SetupWizardLayout layout = new SetupWizardLayout(mContext); + TextView title = (TextView) layout.findViewById(R.id.suw_layout_title); + layout.setHeaderText("Abracadabra"); + assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText()); + } + + @Test + public void testAddView() { + SetupWizardLayout layout = new SetupWizardLayout(mContext); + TextView tv = new TextView(mContext); + tv.setId(R.id.test_view_id); + layout.addView(tv); + assertDefaultTemplateInflated(layout); + View view = layout.findViewById(R.id.test_view_id); + assertSame("The view added should be the same text view", tv, view); + } + + @Test + public void testInflateFromXml() { + LayoutInflater inflater = LayoutInflater.from(mContext); + SetupWizardLayout layout = (SetupWizardLayout) inflater.inflate(R.layout.test_layout, null); + assertDefaultTemplateInflated(layout); + View content = layout.findViewById(R.id.test_content); + assertTrue("@id/test_content should be a TextView", content instanceof TextView); + } + + @Test + public void testCustomTemplate() { + SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template); + View templateView = layout.findViewById(R.id.test_template_view); + assertNotNull("@id/test_template_view should exist in template", templateView); + + TextView tv = new TextView(mContext); + tv.setId(R.id.test_view_id); + layout.addView(tv); + + templateView = layout.findViewById(R.id.test_template_view); + assertNotNull("@id/test_template_view should exist in template", templateView); + View contentView = layout.findViewById(R.id.test_view_id); + assertSame("The view added should be the same text view", tv, contentView); + + // The following methods should be no-ops because the custom template doesn't contain the + // corresponding optional views. Just check that they don't throw exceptions. + layout.setHeaderText("Abracadabra"); + layout.setIllustration(new ColorDrawable(Color.MAGENTA)); + layout.setLayoutBackground(new ColorDrawable(Color.RED)); + } + + @Test + public void testGetNavigationBar() { + final SetupWizardLayout layout = new SetupWizardLayout(mContext); + final NavigationBar navigationBar = layout.getNavigationBar(); + assertEquals( + "Navigation bar should have ID = @id/suw_layout_navigation_bar", + R.id.suw_layout_navigation_bar, + navigationBar.getId()); + } + + @Test + public void testGetNavigationBarNull() { + // test_template does not have navigation bar so getNavigationBar() should return null. + final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template); + final NavigationBar navigationBar = layout.getNavigationBar(); + assertNull("getNavigationBar() in test_template should return null", navigationBar); + } + + @Test + public void testShowProgressBar() { + final SetupWizardLayout layout = new SetupWizardLayout(mContext); + layout.showProgressBar(); + assertTrue("Progress bar should be shown", layout.isProgressBarShown()); + final View progressBar = layout.findViewById(R.id.suw_layout_progress); + assertTrue( + "Progress bar view should be shown", + progressBar instanceof ProgressBar && progressBar.getVisibility() == View.VISIBLE); + } + + @Test + public void testHideProgressBar() { + final SetupWizardLayout layout = new SetupWizardLayout(mContext); + layout.showProgressBar(); + assertTrue("Progress bar should be shown", layout.isProgressBarShown()); + layout.hideProgressBar(); + assertFalse("Progress bar should be hidden", layout.isProgressBarShown()); + final View progressBar = layout.findViewById(R.id.suw_layout_progress); + assertTrue( + "Progress bar view should exist", + progressBar == null || progressBar.getVisibility() != View.VISIBLE); + } + + @Test + public void testShowProgressBarNotExist() { + // test_template does not have progress bar, so showNavigationBar() should do nothing. + final SetupWizardLayout layout = new SetupWizardLayout(mContext, R.layout.test_template); + layout.showProgressBar(); + assertFalse("Progress bar should not be shown", layout.isProgressBarShown()); + } + + @Test + public void testNonMaterialTheme() { + mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(), android.R.style.Theme); + new SetupWizardLayout(mContext); + // Inflating with a non-Material theme should not crash + } + + @Test + public void testOnRestoreFromInstanceState() { + final SetupWizardLayout layout = new SetupWizardLayout(mContext); + layout.setId(ID1234); + + SparseArray<Parcelable> container = new SparseArray<>(); + layout.saveHierarchyState(container); + + final SetupWizardLayout layout2 = new SetupWizardLayout(mContext); + layout2.setId(ID1234); + layout2.restoreHierarchyState(container); + + assertFalse("Progress bar should not be shown", layout2.isProgressBarShown()); + } + + @Test + public void testOnRestoreFromInstanceStateProgressBarShown() { + final SetupWizardLayout layout = new SetupWizardLayout(mContext); + layout.setId(ID1234); + + layout.setProgressBarShown(true); + + SparseArray<Parcelable> container = new SparseArray<>(); + layout.saveHierarchyState(container); + + final SetupWizardLayout layout2 = new SetupWizardLayout(mContext); + layout2.setId(ID1234); + layout2.restoreHierarchyState(container); + + assertTrue("Progress bar should be shown", layout2.isProgressBarShown()); + } + + @Test + public void testOnRestoreFromIncompatibleInstanceState() { + final SetupWizardLayout layout = new SetupWizardLayout(mContext); + layout.setId(ID1234); + + SparseArray<Parcelable> container = new SparseArray<>(); + container.put(1234, AbsSavedState.EMPTY_STATE); + layout.restoreHierarchyState(container); + + // SetupWizardLayout shouldn't crash with incompatible Parcelable + + assertFalse("Progress bar should not be shown", layout.isProgressBarShown()); + } + + @Test + public void testGetMixins() { + final SetupWizardLayout layout = new SetupWizardLayout(mContext); + assertNotNull("SetupWizardLayout should have header mixin", layout.getMixin(HeaderMixin.class)); + assertNotNull( + "SetupWizardLayout should have progress bar mixin", + layout.getMixin(ProgressBarMixin.class)); + assertNotNull( + "SetupWizardLayout should have navigation bar mixin", + layout.getMixin(NavigationBarMixin.class)); + } + + private void assertDefaultTemplateInflated(SetupWizardLayout layout) { + View decorView = layout.findViewById(R.id.suw_layout_decor); + View navbar = layout.findViewById(R.id.suw_layout_navigation_bar); + View title = layout.findViewById(R.id.suw_layout_title); + assertNotNull("@id/suw_layout_decor_view should not be null", decorView); + assertTrue( + "@id/suw_layout_navigation_bar should be an instance of NavigationBar", + navbar instanceof NavigationBar); + assertNotNull("@id/suw_layout_title should not be null", title); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/SetupWizardListLayoutTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/SetupWizardListLayoutTest.java index 5c34fe0..fc18a31 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/SetupWizardListLayoutTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/SetupWizardListLayoutTest.java @@ -25,20 +25,18 @@ import android.content.Context; import android.graphics.drawable.Drawable; import android.graphics.drawable.InsetDrawable; import android.os.Build; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.View; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; - +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.SetupWizardLayout; import com.android.setupwizardlib.SetupWizardListLayout; import com.android.setupwizardlib.view.NavigationBar; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -47,90 +45,93 @@ import org.junit.runner.RunWith; @SmallTest public class SetupWizardListLayoutTest { - private Context mContext; - - @Before - public void setUp() throws Exception { - mContext = new ContextThemeWrapper(InstrumentationRegistry.getContext(), - R.style.SuwThemeMaterial_Light); - } - - @Test - public void testDefaultTemplate() { - SetupWizardListLayout layout = new SetupWizardListLayout(mContext); - assertListTemplateInflated(layout); + private Context mContext; + + @Before + public void setUp() throws Exception { + mContext = + new ContextThemeWrapper( + InstrumentationRegistry.getContext(), R.style.SuwThemeMaterial_Light); + } + + @Test + public void testDefaultTemplate() { + SetupWizardListLayout layout = new SetupWizardListLayout(mContext); + assertListTemplateInflated(layout); + } + + @Test + public void testAddView() { + SetupWizardListLayout layout = new SetupWizardListLayout(mContext); + TextView tv = new TextView(mContext); + try { + layout.addView(tv); + fail("Adding view to ListLayout should throw"); + } catch (UnsupportedOperationException e) { + // Expected exception } - - @Test - public void testAddView() { - SetupWizardListLayout layout = new SetupWizardListLayout(mContext); - TextView tv = new TextView(mContext); - try { - layout.addView(tv); - fail("Adding view to ListLayout should throw"); - } catch (UnsupportedOperationException e) { - // Expected exception - } - } - - @Test - public void testInflateFromXml() { - LayoutInflater inflater = LayoutInflater.from(mContext); - SetupWizardListLayout layout = (SetupWizardListLayout) - inflater.inflate(R.layout.test_list_layout, null); - assertListTemplateInflated(layout); + } + + @Test + public void testInflateFromXml() { + LayoutInflater inflater = LayoutInflater.from(mContext); + SetupWizardListLayout layout = + (SetupWizardListLayout) inflater.inflate(R.layout.test_list_layout, null); + assertListTemplateInflated(layout); + } + + @Test + public void testShowProgressBar() { + final SetupWizardListLayout layout = new SetupWizardListLayout(mContext); + layout.showProgressBar(); + assertTrue("Progress bar should be shown", layout.isProgressBarShown()); + final View progressBar = layout.findViewById(R.id.suw_layout_progress); + assertTrue( + "Progress bar view should be shown", + progressBar instanceof ProgressBar && progressBar.getVisibility() == View.VISIBLE); + } + + @Test + public void testDividerInsetLegacy() { + SetupWizardListLayout layout = new SetupWizardListLayout(mContext); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); } + assertListTemplateInflated(layout); - @Test - public void testShowProgressBar() { - final SetupWizardListLayout layout = new SetupWizardListLayout(mContext); - layout.showProgressBar(); - assertTrue("Progress bar should be shown", layout.isProgressBarShown()); - final View progressBar = layout.findViewById(R.id.suw_layout_progress); - assertTrue("Progress bar view should be shown", - progressBar instanceof ProgressBar && progressBar.getVisibility() == View.VISIBLE); - } - - @Test - public void testDividerInsetLegacy() { - SetupWizardListLayout layout = new SetupWizardListLayout(mContext); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); - } - assertListTemplateInflated(layout); - - layout.setDividerInset(10); - assertEquals("Divider inset should be 10", 10, layout.getDividerInset()); + layout.setDividerInset(10); + assertEquals("Divider inset should be 10", 10, layout.getDividerInset()); - final Drawable divider = layout.getDivider(); - assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); - } - - @Test - public void testDividerInsets() { - SetupWizardListLayout layout = new SetupWizardListLayout(mContext); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); - } - assertListTemplateInflated(layout); - - layout.setDividerInsets(10, 15); - assertEquals("Divider inset start should be 10", 10, layout.getDividerInsetStart()); - assertEquals("Divider inset end should be 15", 15, layout.getDividerInsetEnd()); - - final Drawable divider = layout.getDivider(); - assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); - } + final Drawable divider = layout.getDivider(); + assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); + } - private void assertListTemplateInflated(SetupWizardLayout layout) { - View decorView = layout.findViewById(R.id.suw_layout_decor); - View navbar = layout.findViewById(R.id.suw_layout_navigation_bar); - View title = layout.findViewById(R.id.suw_layout_title); - View list = layout.findViewById(android.R.id.list); - assertNotNull("@id/suw_layout_decor_view should not be null", decorView); - assertTrue("@id/suw_layout_navigation_bar should be an instance of NavigationBar", - navbar instanceof NavigationBar); - assertNotNull("@id/suw_layout_title should not be null", title); - assertTrue("@android:id/list should be an instance of ListView", list instanceof ListView); + @Test + public void testDividerInsets() { + SetupWizardListLayout layout = new SetupWizardListLayout(mContext); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + layout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR); } + assertListTemplateInflated(layout); + + layout.setDividerInsets(10, 15); + assertEquals("Divider inset start should be 10", 10, layout.getDividerInsetStart()); + assertEquals("Divider inset end should be 15", 15, layout.getDividerInsetEnd()); + + final Drawable divider = layout.getDivider(); + assertTrue("Divider should be instance of InsetDrawable", divider instanceof InsetDrawable); + } + + private void assertListTemplateInflated(SetupWizardLayout layout) { + View decorView = layout.findViewById(R.id.suw_layout_decor); + View navbar = layout.findViewById(R.id.suw_layout_navigation_bar); + View title = layout.findViewById(R.id.suw_layout_title); + View list = layout.findViewById(android.R.id.list); + assertNotNull("@id/suw_layout_decor_view should not be null", decorView); + assertTrue( + "@id/suw_layout_navigation_bar should be an instance of NavigationBar", + navbar instanceof NavigationBar); + assertNotNull("@id/suw_layout_title should not be null", title); + assertTrue("@android:id/list should be an instance of ListView", list instanceof ListView); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/SimpleInflaterTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/SimpleInflaterTest.java index f4738ca..da39a7b 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/SimpleInflaterTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/SimpleInflaterTest.java @@ -20,15 +20,12 @@ import static org.junit.Assert.assertEquals; import android.content.Context; import android.content.res.Resources; +import androidx.annotation.NonNull; +import android.util.AttributeSet; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; -import android.util.AttributeSet; - -import androidx.annotation.NonNull; - import com.android.setupwizardlib.items.SimpleInflater; - import org.junit.Test; import org.junit.runner.RunWith; @@ -36,30 +33,30 @@ import org.junit.runner.RunWith; @RunWith(AndroidJUnit4.class) public class SimpleInflaterTest { - @Test - public void testInflateXml() { - final Context context = InstrumentationRegistry.getContext(); - TestInflater inflater = new TestInflater(context.getResources()); - final StringBuilder result = inflater.inflate(R.xml.simple_inflater_test); + @Test + public void testInflateXml() { + final Context context = InstrumentationRegistry.getContext(); + TestInflater inflater = new TestInflater(context.getResources()); + final StringBuilder result = inflater.inflate(R.xml.simple_inflater_test); - assertEquals("Parent[null] > Child[foobar]", result.toString()); - } + assertEquals("Parent[null] > Child[foobar]", result.toString()); + } - private static class TestInflater extends SimpleInflater<StringBuilder> { + private static class TestInflater extends SimpleInflater<StringBuilder> { - protected TestInflater(@NonNull Resources resources) { - super(resources); - } + protected TestInflater(@NonNull Resources resources) { + super(resources); + } - @Override - protected StringBuilder onCreateItem(String tagName, AttributeSet attrs) { - final String attribute = attrs.getAttributeValue(null, "myattribute"); - return new StringBuilder(tagName).append("[").append(attribute).append("]"); - } + @Override + protected StringBuilder onCreateItem(String tagName, AttributeSet attrs) { + final String attribute = attrs.getAttributeValue(null, "myattribute"); + return new StringBuilder(tagName).append("[").append(attribute).append("]"); + } - @Override - protected void onAddChildItem(StringBuilder parent, StringBuilder child) { - parent.append(" > ").append(child); - } + @Override + protected void onAddChildItem(StringBuilder parent, StringBuilder child) { + parent.append(" > ").append(child); } + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/SpanHelperTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/SpanHelperTest.java index 903cf5e..920d7ab 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/SpanHelperTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/SpanHelperTest.java @@ -19,13 +19,11 @@ package com.android.setupwizardlib.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; -import android.support.test.filters.SmallTest; -import android.support.test.runner.AndroidJUnit4; import android.text.Annotation; import android.text.SpannableStringBuilder; - +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; import com.android.setupwizardlib.span.SpanHelper; - import org.junit.Test; import org.junit.runner.RunWith; @@ -33,17 +31,17 @@ import org.junit.runner.RunWith; @SmallTest public class SpanHelperTest { - @Test - public void testReplaceSpan() { - SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); - Annotation oldSpan = new Annotation("key", "value"); - Annotation newSpan = new Annotation("newkey", "newvalue"); - ssb.setSpan(oldSpan, 2, 5, 0 /* flags */); + @Test + public void testReplaceSpan() { + SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); + Annotation oldSpan = new Annotation("key", "value"); + Annotation newSpan = new Annotation("newkey", "newvalue"); + ssb.setSpan(oldSpan, 2, 5, 0 /* flags */); - SpanHelper.replaceSpan(ssb, oldSpan, newSpan); + SpanHelper.replaceSpan(ssb, oldSpan, newSpan); - final Object[] spans = ssb.getSpans(0, ssb.length(), Object.class); - assertEquals("There should be one span in the builder", 1, spans.length); - assertSame("The span should be newSpan", newSpan, spans[0]); - } + final Object[] spans = ssb.getSpans(0, ssb.length(), Object.class); + assertEquals("There should be one span in the builder", 1, spans.length); + assertSame("The span should be newSpan", newSpan, spans[0]); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/StatusBarBackgroundLayoutTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/StatusBarBackgroundLayoutTest.java index 006e5c4..e0fd49b 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/StatusBarBackgroundLayoutTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/StatusBarBackgroundLayoutTest.java @@ -24,9 +24,7 @@ import android.graphics.drawable.ShapeDrawable; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; - import com.android.setupwizardlib.view.StatusBarBackgroundLayout; - import org.junit.Test; import org.junit.runner.RunWith; @@ -34,46 +32,48 @@ import org.junit.runner.RunWith; @SmallTest public class StatusBarBackgroundLayoutTest { - @Test - public void testSetStatusBarBackground() { - final StatusBarBackgroundLayout layout = new StatusBarBackgroundLayout( - InstrumentationRegistry.getContext()); - final ShapeDrawable drawable = new ShapeDrawable(); - layout.setStatusBarBackground(drawable); - assertSame("Status bar background drawable should be same as set", - drawable, layout.getStatusBarBackground()); - } + @Test + public void testSetStatusBarBackground() { + final StatusBarBackgroundLayout layout = + new StatusBarBackgroundLayout(InstrumentationRegistry.getContext()); + final ShapeDrawable drawable = new ShapeDrawable(); + layout.setStatusBarBackground(drawable); + assertSame( + "Status bar background drawable should be same as set", + drawable, + layout.getStatusBarBackground()); + } - @Test - public void testAttachedToWindow() { - // Attaching to window should request apply window inset - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { - final TestStatusBarBackgroundLayout layout = - new TestStatusBarBackgroundLayout(InstrumentationRegistry.getContext()); - layout.mRequestApplyInsets = false; - layout.onAttachedToWindow(); + @Test + public void testAttachedToWindow() { + // Attaching to window should request apply window inset + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { + final TestStatusBarBackgroundLayout layout = + new TestStatusBarBackgroundLayout(InstrumentationRegistry.getContext()); + layout.mRequestApplyInsets = false; + layout.onAttachedToWindow(); - assertTrue("Attaching to window should apply window inset", layout.mRequestApplyInsets); - } + assertTrue("Attaching to window should apply window inset", layout.mRequestApplyInsets); } + } - private static class TestStatusBarBackgroundLayout extends StatusBarBackgroundLayout { + private static class TestStatusBarBackgroundLayout extends StatusBarBackgroundLayout { - boolean mRequestApplyInsets = false; + boolean mRequestApplyInsets = false; - TestStatusBarBackgroundLayout(Context context) { - super(context); - } + TestStatusBarBackgroundLayout(Context context) { + super(context); + } - @Override - public void onAttachedToWindow() { - super.onAttachedToWindow(); - } + @Override + public void onAttachedToWindow() { + super.onAttachedToWindow(); + } - @Override - public void requestApplyInsets() { - super.requestApplyInsets(); - mRequestApplyInsets = true; - } + @Override + public void requestApplyInsets() { + super.requestApplyInsets(); + mRequestApplyInsets = true; } + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/SystemBarHelperTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/SystemBarHelperTest.java index 98c28f6..1b534e1 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/SystemBarHelperTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/SystemBarHelperTest.java @@ -17,7 +17,6 @@ package com.android.setupwizardlib.test; import static com.google.common.truth.Truth.assertThat; - import static org.junit.Assert.assertEquals; import android.annotation.SuppressLint; @@ -28,19 +27,17 @@ import android.os.Build.VERSION_CODES; import android.os.Handler; import android.os.HandlerThread; import android.os.SystemClock; +import android.view.ContextThemeWrapper; +import android.view.View; +import android.view.Window; +import android.view.WindowManager; import android.support.test.InstrumentationRegistry; import android.support.test.annotation.UiThreadTest; import android.support.test.filters.SmallTest; import android.support.test.rule.UiThreadTestRule; import android.support.test.runner.AndroidJUnit4; -import android.view.ContextThemeWrapper; -import android.view.View; -import android.view.Window; -import android.view.WindowManager; - import com.android.setupwizardlib.test.util.MockWindow; import com.android.setupwizardlib.util.SystemBarHelper; - import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -49,241 +46,240 @@ import org.junit.runner.RunWith; @SmallTest public class SystemBarHelperTest { - @Rule - public UiThreadTestRule mUiThreadTestRule = new UiThreadTestRule(); - - private static final int STATUS_BAR_DISABLE_BACK = 0x00400000; - - @SuppressLint("InlinedApi") - private static final int DEFAULT_IMMERSIVE_FLAGS = - View.SYSTEM_UI_FLAG_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY - | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; - - @SuppressLint("InlinedApi") - private static final int DIALOG_IMMERSIVE_FLAGS = - View.SYSTEM_UI_FLAG_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; - - @UiThreadTest - @Test - public void testAddVisibilityFlagView() { - final View view = createViewWithSystemUiVisibility(0x456); - SystemBarHelper.addVisibilityFlag(view, 0x1400); - if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { - // Check that result is 0x1456, because 0x1400 | 0x456 = 0x1456. - assertEquals("View visibility should be 0x1456", 0x1456, view.getSystemUiVisibility()); - } + @Rule public UiThreadTestRule mUiThreadTestRule = new UiThreadTestRule(); + + private static final int STATUS_BAR_DISABLE_BACK = 0x00400000; + + @SuppressLint("InlinedApi") + private static final int DEFAULT_IMMERSIVE_FLAGS = + View.SYSTEM_UI_FLAG_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; + + @SuppressLint("InlinedApi") + private static final int DIALOG_IMMERSIVE_FLAGS = + View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; + + @UiThreadTest + @Test + public void testAddVisibilityFlagView() { + final View view = createViewWithSystemUiVisibility(0x456); + SystemBarHelper.addVisibilityFlag(view, 0x1400); + if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { + // Check that result is 0x1456, because 0x1400 | 0x456 = 0x1456. + assertEquals("View visibility should be 0x1456", 0x1456, view.getSystemUiVisibility()); } - - @UiThreadTest - @Test - public void testRemoveVisibilityFlagView() { - final View view = createViewWithSystemUiVisibility(0x456); - SystemBarHelper.removeVisibilityFlag(view, 0x1400); - if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { - // Check that result is 0x56, because 0x456 & ~0x1400 = 0x56. - assertEquals("View visibility should be 0x56", 0x56, view.getSystemUiVisibility()); - } + } + + @UiThreadTest + @Test + public void testRemoveVisibilityFlagView() { + final View view = createViewWithSystemUiVisibility(0x456); + SystemBarHelper.removeVisibilityFlag(view, 0x1400); + if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { + // Check that result is 0x56, because 0x456 & ~0x1400 = 0x56. + assertEquals("View visibility should be 0x56", 0x56, view.getSystemUiVisibility()); } - - @UiThreadTest - @Test - public void testAddVisibilityFlagWindow() { - final Window window = createWindowWithSystemUiVisibility(0x456); - SystemBarHelper.addVisibilityFlag(window, 0x1400); - if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { - // Check that result is 0x1456 = 0x1400 | 0x456. - assertEquals("View visibility should be 0x1456", 0x1456, - window.getAttributes().systemUiVisibility); - } + } + + @UiThreadTest + @Test + public void testAddVisibilityFlagWindow() { + final Window window = createWindowWithSystemUiVisibility(0x456); + SystemBarHelper.addVisibilityFlag(window, 0x1400); + if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { + // Check that result is 0x1456 = 0x1400 | 0x456. + assertEquals( + "View visibility should be 0x1456", 0x1456, window.getAttributes().systemUiVisibility); } - - @UiThreadTest - @Test - public void testRemoveVisibilityFlagWindow() { - final Window window = createWindowWithSystemUiVisibility(0x456); - SystemBarHelper.removeVisibilityFlag(window, 0x1400); - if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { - // Check that result is 0x56 = 0x456 & ~0x1400. - assertEquals("View visibility should be 0x56", 0x56, - window.getAttributes().systemUiVisibility); - } + } + + @UiThreadTest + @Test + public void testRemoveVisibilityFlagWindow() { + final Window window = createWindowWithSystemUiVisibility(0x456); + SystemBarHelper.removeVisibilityFlag(window, 0x1400); + if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { + // Check that result is 0x56 = 0x456 & ~0x1400. + assertEquals( + "View visibility should be 0x56", 0x56, window.getAttributes().systemUiVisibility); } - - @UiThreadTest - @Test - public void testHideSystemBarsWindow() { - final Window window = createWindowWithSystemUiVisibility(0x456); - SystemBarHelper.hideSystemBars(window); - if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { - assertEquals("DEFAULT_IMMERSIVE_FLAGS should be added to window's systemUiVisibility", - DEFAULT_IMMERSIVE_FLAGS | 0x456, - window.getAttributes().systemUiVisibility); - assertEquals( - "DEFAULT_IMMERSIVE_FLAGS should be added to decorView's systemUiVisibility", - DEFAULT_IMMERSIVE_FLAGS | 0x456, - window.getDecorView().getSystemUiVisibility()); - assertEquals("Navigation bar should be transparent", window.getNavigationBarColor(), 0); - assertEquals("Status bar should be transparent", window.getStatusBarColor(), 0); - } + } + + @UiThreadTest + @Test + public void testHideSystemBarsWindow() { + final Window window = createWindowWithSystemUiVisibility(0x456); + SystemBarHelper.hideSystemBars(window); + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + assertEquals( + "DEFAULT_IMMERSIVE_FLAGS should be added to window's systemUiVisibility", + DEFAULT_IMMERSIVE_FLAGS | 0x456, + window.getAttributes().systemUiVisibility); + assertEquals( + "DEFAULT_IMMERSIVE_FLAGS should be added to decorView's systemUiVisibility", + DEFAULT_IMMERSIVE_FLAGS | 0x456, + window.getDecorView().getSystemUiVisibility()); + assertEquals("Navigation bar should be transparent", window.getNavigationBarColor(), 0); + assertEquals("Status bar should be transparent", window.getStatusBarColor(), 0); } - - @UiThreadTest - @Test - public void testShowSystemBarsWindow() { - final Window window = createWindowWithSystemUiVisibility(0x456); - Context context = new ContextThemeWrapper( - InstrumentationRegistry.getContext(), android.R.style.Theme); - SystemBarHelper.showSystemBars(window, context); - if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { - assertEquals( - "DEFAULT_IMMERSIVE_FLAGS should be removed from window's systemUiVisibility", - 0x456 & ~DEFAULT_IMMERSIVE_FLAGS, - window.getAttributes().systemUiVisibility); - assertEquals( - "DEFAULT_IMMERSIVE_FLAGS should be removed from decorView's systemUiVisibility", - 0x456 & ~DEFAULT_IMMERSIVE_FLAGS, - window.getDecorView().getSystemUiVisibility()); - assertEquals("Navigation bar should not be transparent", - window.getNavigationBarColor(), 0xff000000); - assertEquals("Status bar should not be transparent", - window.getStatusBarColor(), 0xff000000); - } + } + + @UiThreadTest + @Test + public void testShowSystemBarsWindow() { + final Window window = createWindowWithSystemUiVisibility(0x456); + Context context = + new ContextThemeWrapper(InstrumentationRegistry.getContext(), android.R.style.Theme); + SystemBarHelper.showSystemBars(window, context); + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + assertEquals( + "DEFAULT_IMMERSIVE_FLAGS should be removed from window's systemUiVisibility", + 0x456 & ~DEFAULT_IMMERSIVE_FLAGS, + window.getAttributes().systemUiVisibility); + assertEquals( + "DEFAULT_IMMERSIVE_FLAGS should be removed from decorView's systemUiVisibility", + 0x456 & ~DEFAULT_IMMERSIVE_FLAGS, + window.getDecorView().getSystemUiVisibility()); + assertEquals( + "Navigation bar should not be transparent", window.getNavigationBarColor(), 0xff000000); + assertEquals("Status bar should not be transparent", window.getStatusBarColor(), 0xff000000); } - - @UiThreadTest - @Test - public void testHideSystemBarsNoInfiniteLoop() throws InterruptedException { - final TestWindow window = new TestWindow(InstrumentationRegistry.getContext(), null); - final HandlerThread thread = new HandlerThread("SystemBarHelperTest"); - thread.start(); - final Handler handler = new Handler(thread.getLooper()); - handler.post(new Runnable() { - @Override - public void run() { - SystemBarHelper.hideSystemBars(window); - } + } + + @UiThreadTest + @Test + public void testHideSystemBarsNoInfiniteLoop() throws InterruptedException { + final TestWindow window = new TestWindow(InstrumentationRegistry.getContext(), null); + final HandlerThread thread = new HandlerThread("SystemBarHelperTest"); + thread.start(); + final Handler handler = new Handler(thread.getLooper()); + handler.post( + new Runnable() { + @Override + public void run() { + SystemBarHelper.hideSystemBars(window); + } }); - SystemClock.sleep(500); // Wait for the looper to drain all the messages - thread.quit(); - // Initial peek + 3 retries = 4 tries total - if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { - assertEquals("Peek decor view should give up after 4 tries", 4, - window.peekDecorViewCount); - } + SystemClock.sleep(500); // Wait for the looper to drain all the messages + thread.quit(); + // Initial peek + 3 retries = 4 tries total + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + assertEquals("Peek decor view should give up after 4 tries", 4, window.peekDecorViewCount); } - - @UiThreadTest - @Test - public void testHideSystemBarsDialog() { - final Dialog dialog = new Dialog(InstrumentationRegistry.getContext()); - if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { - final WindowManager.LayoutParams attrs = dialog.getWindow().getAttributes(); - attrs.systemUiVisibility = 0x456; - dialog.getWindow().setAttributes(attrs); - } - - SystemBarHelper.hideSystemBars(dialog); - if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { - assertEquals("DIALOG_IMMERSIVE_FLAGS should be added to window's systemUiVisibility", - DIALOG_IMMERSIVE_FLAGS | 0x456, - dialog.getWindow().getAttributes().systemUiVisibility); - } + } + + @UiThreadTest + @Test + public void testHideSystemBarsDialog() { + final Dialog dialog = new Dialog(InstrumentationRegistry.getContext()); + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + final WindowManager.LayoutParams attrs = dialog.getWindow().getAttributes(); + attrs.systemUiVisibility = 0x456; + dialog.getWindow().setAttributes(attrs); } - @UiThreadTest - @Test - public void testSetBackButtonVisibleTrue() { - final Window window = createWindowWithSystemUiVisibility(STATUS_BAR_DISABLE_BACK | 0x456); - SystemBarHelper.setBackButtonVisible(window, true); - if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { - assertThat(window.getAttributes().systemUiVisibility) - .named("window sysUiVisibility") - .isEqualTo(0x456); - assertThat(window.getDecorView().getSystemUiVisibility()) - .named("decor view sysUiVisibility") - .isEqualTo(0x456); - } + SystemBarHelper.hideSystemBars(dialog); + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + assertEquals( + "DIALOG_IMMERSIVE_FLAGS should be added to window's systemUiVisibility", + DIALOG_IMMERSIVE_FLAGS | 0x456, + dialog.getWindow().getAttributes().systemUiVisibility); } - - @UiThreadTest - @Test - public void testSetBackButtonVisibleFalse() { - final Window window = createWindowWithSystemUiVisibility(0x456); - SystemBarHelper.setBackButtonVisible(window, false); - if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { - assertThat(window.getAttributes().systemUiVisibility) - .named("window sysUiVisibility") - .isEqualTo(0x456 | STATUS_BAR_DISABLE_BACK); - assertThat(window.getDecorView().getSystemUiVisibility()) - .named("decor view sysUiVisibility") - .isEqualTo(0x456 | STATUS_BAR_DISABLE_BACK); - } + } + + @UiThreadTest + @Test + public void testSetBackButtonVisibleTrue() { + final Window window = createWindowWithSystemUiVisibility(STATUS_BAR_DISABLE_BACK | 0x456); + SystemBarHelper.setBackButtonVisible(window, true); + if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { + assertThat(window.getAttributes().systemUiVisibility) + .named("window sysUiVisibility") + .isEqualTo(0x456); + assertThat(window.getDecorView().getSystemUiVisibility()) + .named("decor view sysUiVisibility") + .isEqualTo(0x456); } - - private View createViewWithSystemUiVisibility(int vis) { - final View view = new View(InstrumentationRegistry.getContext()); - if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { - view.setSystemUiVisibility(vis); - } - return view; + } + + @UiThreadTest + @Test + public void testSetBackButtonVisibleFalse() { + final Window window = createWindowWithSystemUiVisibility(0x456); + SystemBarHelper.setBackButtonVisible(window, false); + if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { + assertThat(window.getAttributes().systemUiVisibility) + .named("window sysUiVisibility") + .isEqualTo(0x456 | STATUS_BAR_DISABLE_BACK); + assertThat(window.getDecorView().getSystemUiVisibility()) + .named("decor view sysUiVisibility") + .isEqualTo(0x456 | STATUS_BAR_DISABLE_BACK); } + } - private Window createWindowWithSystemUiVisibility(int vis) { - final Window window = new TestWindow(InstrumentationRegistry.getContext(), - createViewWithSystemUiVisibility(vis)); - if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { - WindowManager.LayoutParams attrs = window.getAttributes(); - attrs.systemUiVisibility = vis; - window.setAttributes(attrs); - } - return window; + private View createViewWithSystemUiVisibility(int vis) { + final View view = new View(InstrumentationRegistry.getContext()); + if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { + view.setSystemUiVisibility(vis); + } + return view; + } + + private Window createWindowWithSystemUiVisibility(int vis) { + final Window window = + new TestWindow(InstrumentationRegistry.getContext(), createViewWithSystemUiVisibility(vis)); + if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) { + WindowManager.LayoutParams attrs = window.getAttributes(); + attrs.systemUiVisibility = vis; + window.setAttributes(attrs); } + return window; + } - private static class TestWindow extends MockWindow { + private static class TestWindow extends MockWindow { - private View mDecorView; - public int peekDecorViewCount = 0; + private View mDecorView; + public int peekDecorViewCount = 0; - private int mNavigationBarColor = -1; - private int mStatusBarColor = -1; + private int mNavigationBarColor = -1; + private int mStatusBarColor = -1; - TestWindow(Context context, View decorView) { - super(context); - mDecorView = decorView; - } + TestWindow(Context context, View decorView) { + super(context); + mDecorView = decorView; + } - @Override - public View getDecorView() { - return mDecorView; - } + @Override + public View getDecorView() { + return mDecorView; + } - @Override - public View peekDecorView() { - peekDecorViewCount++; - return mDecorView; - } + @Override + public View peekDecorView() { + peekDecorViewCount++; + return mDecorView; + } - @Override - public void setNavigationBarColor(int i) { - mNavigationBarColor = i; - } + @Override + public void setNavigationBarColor(int i) { + mNavigationBarColor = i; + } - @Override - public int getNavigationBarColor() { - return mNavigationBarColor; - } + @Override + public int getNavigationBarColor() { + return mNavigationBarColor; + } - @Override - public void setStatusBarColor(int i) { - mStatusBarColor = i; - } + @Override + public void setStatusBarColor(int i) { + mStatusBarColor = i; + } - @Override - public int getStatusBarColor() { - return mStatusBarColor; - } + @Override + public int getStatusBarColor() { + return mStatusBarColor; } + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/util/DrawingTestHelper.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/util/DrawingTestHelper.java index 6910513..918d63a 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/util/DrawingTestHelper.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/util/DrawingTestHelper.java @@ -24,69 +24,69 @@ import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Bitmap; import android.graphics.Canvas; -import android.support.test.InstrumentationRegistry; +import androidx.annotation.StyleRes; import android.view.View; import android.view.View.MeasureSpec; - -import androidx.annotation.StyleRes; +import android.support.test.InstrumentationRegistry; public class DrawingTestHelper { - /** - * Creates an activity of which to inflate views and drawables for drawing tests. This method - * will return an instance of AppCompatActivity which allows testing of drawing behavior - * injected by support libraries (like drawable tinting) as well. - */ - public static Activity createCanvasActivity(@StyleRes int theme) - throws IllegalAccessException, InstantiationException { - final Context context = InstrumentationRegistry.getTargetContext(); - final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); + /** + * Creates an activity of which to inflate views and drawables for drawing tests. This method will + * return an instance of AppCompatActivity which allows testing of drawing behavior injected by + * support libraries (like drawable tinting) as well. + */ + public static Activity createCanvasActivity(@StyleRes int theme) + throws IllegalAccessException, InstantiationException { + final Context context = InstrumentationRegistry.getTargetContext(); + final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); - final Intent intent = new Intent(context, DrawingTestActivity.class); - final Activity activity = instrumentation.newActivity( - DrawingTestActivity.class, - context, - null, /* token */ - new Application(), - intent, - new ActivityInfo(), - "", /* title */ - null, /* parent */ - null, /* id */ - null /* lastNonConfigurationInstance */); - instrumentation.callActivityOnCreate(activity, null); - activity.setTheme(theme); - return activity; - } + final Intent intent = new Intent(context, DrawingTestActivity.class); + final Activity activity = + instrumentation.newActivity( + DrawingTestActivity.class, + context, + null, /* token */ + new Application(), + intent, + new ActivityInfo(), + "", /* title */ + null, /* parent */ + null, /* id */ + null /* lastNonConfigurationInstance */); + instrumentation.callActivityOnCreate(activity, null); + activity.setTheme(theme); + return activity; + } - private final int mWidth; - private final int mHeight; - private final Canvas mCanvas; - private final Bitmap mBitmap; + private final int mWidth; + private final int mHeight; + private final Canvas mCanvas; + private final Bitmap mBitmap; - public DrawingTestHelper(int width, int height) { - mWidth = width; - mHeight = height; + public DrawingTestHelper(int width, int height) { + mWidth = width; + mHeight = height; - mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - mCanvas = new Canvas(mBitmap); - } + mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + mCanvas = new Canvas(mBitmap); + } - public void drawView(View view) { - view.measure( - MeasureSpec.makeMeasureSpec(mWidth, MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(mHeight, MeasureSpec.EXACTLY)); - view.layout(0, 0, mWidth, mHeight); - view.draw(mCanvas); - } + public void drawView(View view) { + view.measure( + MeasureSpec.makeMeasureSpec(mWidth, MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(mHeight, MeasureSpec.EXACTLY)); + view.layout(0, 0, mWidth, mHeight); + view.draw(mCanvas); + } - public int[] getPixels() { - int[] out = new int[mWidth * mHeight]; - mBitmap.getPixels(out, 0, mWidth, 0, 0, mWidth, mHeight); - return out; - } + public int[] getPixels() { + int[] out = new int[mWidth * mHeight]; + mBitmap.getPixels(out, 0, mWidth, 0, 0, mWidth, mHeight); + return out; + } - public int getPixel(int x, int y) { - return mBitmap.getPixel(x, y); - } + public int getPixel(int x, int y) { + return mBitmap.getPixel(x, y); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/util/MockWindow.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/util/MockWindow.java index 7af20eb..1e096eb 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/util/MockWindow.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/util/MockWindow.java @@ -21,6 +21,7 @@ import android.content.res.Configuration; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; +import androidx.annotation.NonNull; import android.view.InputQueue; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -30,252 +31,250 @@ import android.view.View; import android.view.ViewGroup; import android.view.Window; -import androidx.annotation.NonNull; - public class MockWindow extends Window { - public MockWindow(Context context) { - super(context); - } - - @Override - public void takeSurface(SurfaceHolder.Callback2 callback2) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void takeInputQueue(InputQueue.Callback callback) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public boolean isFloating() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setContentView(int i) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setContentView(View view) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setContentView(View view, ViewGroup.LayoutParams layoutParams) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void addContentView(View view, ViewGroup.LayoutParams layoutParams) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public View getCurrentFocus() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @NonNull - @Override - public LayoutInflater getLayoutInflater() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setTitle(CharSequence charSequence) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setTitleColor(int i) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void openPanel(int i, KeyEvent keyEvent) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void closePanel(int i) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void togglePanel(int i, KeyEvent keyEvent) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void invalidatePanelMenu(int i) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public boolean performPanelShortcut(int i, int i1, KeyEvent keyEvent, int i2) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public boolean performPanelIdentifierAction(int i, int i1, int i2) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void closeAllPanels() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public boolean performContextMenuIdentifierAction(int i, int i1) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void onConfigurationChanged(Configuration configuration) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setBackgroundDrawable(Drawable drawable) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setFeatureDrawableResource(int i, int i1) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setFeatureDrawableUri(int i, Uri uri) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setFeatureDrawable(int i, Drawable drawable) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setFeatureDrawableAlpha(int i, int i1) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setFeatureInt(int i, int i1) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void takeKeyEvents(boolean b) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public boolean superDispatchKeyEvent(KeyEvent keyEvent) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public boolean superDispatchKeyShortcutEvent(KeyEvent keyEvent) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public boolean superDispatchTouchEvent(MotionEvent motionEvent) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public boolean superDispatchTrackballEvent(MotionEvent motionEvent) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public boolean superDispatchGenericMotionEvent(MotionEvent motionEvent) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public View getDecorView() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public View peekDecorView() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public Bundle saveHierarchyState() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void restoreHierarchyState(Bundle bundle) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - protected void onActive() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setChildDrawable(int i, Drawable drawable) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setChildInt(int i, int i1) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public boolean isShortcutKey(int i, KeyEvent keyEvent) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setVolumeControlStream(int i) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public int getVolumeControlStream() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public int getStatusBarColor() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setStatusBarColor(int i) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public int getNavigationBarColor() { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setNavigationBarColor(int i) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setDecorCaptionShade(int i) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } - - @Override - public void setResizingCaptionDrawable(Drawable drawable) { - throw new UnsupportedOperationException("Unexpected method call on mock"); - } + public MockWindow(Context context) { + super(context); + } + + @Override + public void takeSurface(SurfaceHolder.Callback2 callback2) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void takeInputQueue(InputQueue.Callback callback) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public boolean isFloating() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setContentView(int i) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setContentView(View view) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setContentView(View view, ViewGroup.LayoutParams layoutParams) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void addContentView(View view, ViewGroup.LayoutParams layoutParams) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public View getCurrentFocus() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @NonNull + @Override + public LayoutInflater getLayoutInflater() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setTitle(CharSequence charSequence) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setTitleColor(int i) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void openPanel(int i, KeyEvent keyEvent) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void closePanel(int i) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void togglePanel(int i, KeyEvent keyEvent) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void invalidatePanelMenu(int i) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public boolean performPanelShortcut(int i, int i1, KeyEvent keyEvent, int i2) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public boolean performPanelIdentifierAction(int i, int i1, int i2) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void closeAllPanels() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public boolean performContextMenuIdentifierAction(int i, int i1) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void onConfigurationChanged(Configuration configuration) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setBackgroundDrawable(Drawable drawable) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setFeatureDrawableResource(int i, int i1) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setFeatureDrawableUri(int i, Uri uri) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setFeatureDrawable(int i, Drawable drawable) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setFeatureDrawableAlpha(int i, int i1) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setFeatureInt(int i, int i1) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void takeKeyEvents(boolean b) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public boolean superDispatchKeyEvent(KeyEvent keyEvent) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public boolean superDispatchKeyShortcutEvent(KeyEvent keyEvent) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public boolean superDispatchTouchEvent(MotionEvent motionEvent) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public boolean superDispatchTrackballEvent(MotionEvent motionEvent) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public boolean superDispatchGenericMotionEvent(MotionEvent motionEvent) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public View getDecorView() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public View peekDecorView() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public Bundle saveHierarchyState() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void restoreHierarchyState(Bundle bundle) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + protected void onActive() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setChildDrawable(int i, Drawable drawable) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setChildInt(int i, int i1) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public boolean isShortcutKey(int i, KeyEvent keyEvent) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setVolumeControlStream(int i) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public int getVolumeControlStream() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public int getStatusBarColor() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setStatusBarColor(int i) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public int getNavigationBarColor() { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setNavigationBarColor(int i) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setDecorCaptionShade(int i) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } + + @Override + public void setResizingCaptionDrawable(Drawable drawable) { + throw new UnsupportedOperationException("Unexpected method call on mock"); + } } diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/util/FallbackThemeWrapperTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/util/FallbackThemeWrapperTest.java index d492765..99d997b 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/util/FallbackThemeWrapperTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/util/FallbackThemeWrapperTest.java @@ -20,13 +20,11 @@ import static org.junit.Assert.assertEquals; import android.content.Context; import android.content.res.TypedArray; +import android.view.ContextThemeWrapper; import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; -import android.view.ContextThemeWrapper; - import com.android.setupwizardlib.test.R; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,37 +33,35 @@ import org.junit.runner.RunWith; @SmallTest public class FallbackThemeWrapperTest { - private FallbackThemeWrapper mThemedContext; + private FallbackThemeWrapper mThemedContext; - @Before - public void setUp() { - Context baseContext = new ContextThemeWrapper( - InstrumentationRegistry.getContext(), - R.style.TestBaseTheme); - mThemedContext = new FallbackThemeWrapper(baseContext, R.style.TestFallbackTheme); - } + @Before + public void setUp() { + Context baseContext = + new ContextThemeWrapper(InstrumentationRegistry.getContext(), R.style.TestBaseTheme); + mThemedContext = new FallbackThemeWrapper(baseContext, R.style.TestFallbackTheme); + } - @Test - public void testThemeValueOnlyInBase() { - final TypedArray a = - mThemedContext.obtainStyledAttributes(new int[] {android.R.attr.background}); - assertEquals(0xffff0000, a.getColor(0, 0)); - a.recycle(); - } + @Test + public void testThemeValueOnlyInBase() { + final TypedArray a = + mThemedContext.obtainStyledAttributes(new int[] {android.R.attr.background}); + assertEquals(0xffff0000, a.getColor(0, 0)); + a.recycle(); + } - @Test - public void testThemeValueOnlyInFallback() { - final TypedArray a = - mThemedContext.obtainStyledAttributes(new int[] {android.R.attr.foreground}); - assertEquals(0xff0000ff, a.getColor(0, 0)); - a.recycle(); - } + @Test + public void testThemeValueOnlyInFallback() { + final TypedArray a = + mThemedContext.obtainStyledAttributes(new int[] {android.R.attr.foreground}); + assertEquals(0xff0000ff, a.getColor(0, 0)); + a.recycle(); + } - @Test - public void testThemeValueInBoth() { - final TypedArray a = - mThemedContext.obtainStyledAttributes(new int[] {android.R.attr.theme}); - assertEquals(R.style.TestBaseTheme, a.getResourceId(0, 0)); - a.recycle(); - } + @Test + public void testThemeValueInBoth() { + final TypedArray a = mThemedContext.obtainStyledAttributes(new int[] {android.R.attr.theme}); + assertEquals(R.style.TestBaseTheme, a.getResourceId(0, 0)); + a.recycle(); + } } |