diff options
Diffstat (limited to 'library/test')
50 files changed, 4948 insertions, 4702 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(); + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/GlifLayoutTest.java b/library/test/robotest/src/com/android/setupwizardlib/GlifLayoutTest.java index 2a4a8c0..d77838f 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/GlifLayoutTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/GlifLayoutTest.java @@ -16,14 +16,8 @@ package com.android.setupwizardlib; -import static org.hamcrest.Matchers.instanceOf; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static org.robolectric.RuntimeEnvironment.application; import android.content.Context; @@ -34,328 +28,337 @@ import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; +import androidx.annotation.IdRes; import android.view.ContextThemeWrapper; import android.view.View; import android.widget.ProgressBar; import android.widget.ScrollView; import android.widget.TextView; - -import androidx.annotation.IdRes; - -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; import com.android.setupwizardlib.template.ColoredHeaderMixin; import com.android.setupwizardlib.template.HeaderMixin; import com.android.setupwizardlib.template.IconMixin; import com.android.setupwizardlib.template.ProgressBarMixin; import com.android.setupwizardlib.view.StatusBarBackgroundLayout; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -@RunWith(SuwLibRobolectricTestRunner.class) -@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) public class GlifLayoutTest { - private Context mContext; - - @Before - public void setUp() throws Exception { - mContext = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light); - } - - @Test - public void testDefaultTemplate() { - GlifLayout layout = new GlifLayout(mContext); - assertDefaultTemplateInflated(layout); - } - - @Test - public void testSetHeaderText() { - GlifLayout layout = new GlifLayout(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() { - @IdRes int testViewId = 123456; - GlifLayout layout = new GlifLayout(mContext); - TextView tv = new TextView(mContext); - tv.setId(testViewId); - layout.addView(tv); - assertDefaultTemplateInflated(layout); - View view = layout.findViewById(testViewId); - assertSame("The view added should be the same text view", tv, view); - } - - @Test - public void testGetScrollView() { - GlifLayout layout = new GlifLayout(mContext); - assertNotNull("Get scroll view should not be null with default template", - layout.getScrollView()); - } - - @Test - public void testSetPrimaryColor() { - GlifLayout layout = new GlifLayout(mContext); - layout.setProgressBarShown(true); - layout.setPrimaryColor(ColorStateList.valueOf(Color.RED)); - assertEquals("Primary color should be red", - ColorStateList.valueOf(Color.RED), layout.getPrimaryColor()); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress); - assertEquals("Progress bar should be tinted red", - ColorStateList.valueOf(Color.RED), progressBar.getIndeterminateTintList()); - assertEquals("Determinate progress bar should also be tinted red", - ColorStateList.valueOf(Color.RED), progressBar.getProgressBackgroundTintList()); - } - } - - @Config(qualifiers = "sw600dp") - @Test - public void testSetPrimaryColorTablet() { - GlifLayout layout = new GlifLayout(mContext); - layout.setProgressBarShown(true); - layout.setPrimaryColor(ColorStateList.valueOf(Color.RED)); - assertEquals("Primary color should be red", - ColorStateList.valueOf(Color.RED), layout.getPrimaryColor()); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress); - assertEquals("Progress bar should be tinted red", - ColorStateList.valueOf(Color.RED), progressBar.getIndeterminateTintList()); - assertEquals("Determinate progress bar should also be tinted red", - ColorStateList.valueOf(Color.RED), progressBar.getProgressBackgroundTintList()); - } - - assertEquals(Color.RED, ((GlifPatternDrawable) getTabletBackground(layout)).getColor()); - } - - @Test - public void testSetBackgroundBaseColor() { - GlifLayout layout = new GlifLayout(mContext); - layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE)); - layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED)); - - assertEquals(Color.RED, ((GlifPatternDrawable) getPhoneBackground(layout)).getColor()); - assertEquals(Color.RED, layout.getBackgroundBaseColor().getDefaultColor()); - } - - @Config(qualifiers = "sw600dp") - @Test - public void testSetBackgroundBaseColorTablet() { - GlifLayout layout = new GlifLayout(mContext); - layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE)); - layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED)); - - assertEquals(Color.RED, ((GlifPatternDrawable) getTabletBackground(layout)).getColor()); - assertEquals(Color.RED, layout.getBackgroundBaseColor().getDefaultColor()); - } - - @Test - public void testSetBackgroundPatternedTrue() { - GlifLayout layout = new GlifLayout(mContext); - layout.setBackgroundPatterned(true); - - assertThat(getPhoneBackground(layout), instanceOf(GlifPatternDrawable.class)); - assertTrue("Background should be patterned", layout.isBackgroundPatterned()); - } - - @Test - public void testSetBackgroundPatternedFalse() { - GlifLayout layout = new GlifLayout(mContext); - layout.setBackgroundPatterned(false); - - assertThat(getPhoneBackground(layout), instanceOf(ColorDrawable.class)); - assertFalse("Background should not be patterned", layout.isBackgroundPatterned()); - } - - @Config(qualifiers = "sw600dp") - @Test - public void testSetBackgroundPatternedTrueTablet() { - GlifLayout layout = new GlifLayout(mContext); - layout.setBackgroundPatterned(true); - - assertThat(getTabletBackground(layout), instanceOf(GlifPatternDrawable.class)); - assertTrue("Background should be patterned", layout.isBackgroundPatterned()); - } - - @Config(qualifiers = "sw600dp") - @Test - public void testSetBackgroundPatternedFalseTablet() { - GlifLayout layout = new GlifLayout(mContext); - layout.setBackgroundPatterned(false); - - assertThat(getTabletBackground(layout), instanceOf(ColorDrawable.class)); - assertFalse("Background should not be patterned", layout.isBackgroundPatterned()); - } - - @Test - public void testNonGlifTheme() { - mContext = new ContextThemeWrapper(application, android.R.style.Theme); - new GlifLayout(mContext); - // Inflating with a non-GLIF theme should not crash - } - - @Test - public void testPeekProgressBarNull() { - GlifLayout layout = new GlifLayout(mContext); - assertNull("PeekProgressBar should return null initially", layout.peekProgressBar()); - } - - @Test - public void testPeekProgressBar() { - GlifLayout layout = new GlifLayout(mContext); - layout.setProgressBarShown(true); - assertNotNull("Peek progress bar should return the bar after setProgressBarShown(true)", - layout.peekProgressBar()); - } - - @Test - public void testMixins() { - GlifLayout layout = new GlifLayout(mContext); - final HeaderMixin header = layout.getMixin(HeaderMixin.class); - assertTrue("Header should be instance of ColoredHeaderMixin. " - + "Found " + header.getClass() + " instead.", header instanceof ColoredHeaderMixin); - - assertNotNull("GlifLayout should have icon mixin", layout.getMixin(IconMixin.class)); - assertNotNull("GlifLayout should have progress bar mixin", - layout.getMixin(ProgressBarMixin.class)); - } - - @Test - public void testInflateFooter() { - GlifLayout layout = new GlifLayout(mContext); - - final View view = layout.inflateFooter(android.R.layout.simple_list_item_1); - assertEquals(android.R.id.text1, view.getId()); - assertNotNull(layout.findViewById(android.R.id.text1)); - } - - @Config(qualifiers = "sw600dp") - @Test - public void testInflateFooterTablet() { - testInflateFooter(); - } - - @Test - public void testInflateFooterBlankTemplate() { - GlifLayout layout = new GlifLayout(mContext, R.layout.suw_glif_blank_template); - - final View view = layout.inflateFooter(android.R.layout.simple_list_item_1); - assertEquals(android.R.id.text1, view.getId()); - assertNotNull(layout.findViewById(android.R.id.text1)); - } - - @Config(qualifiers = "sw600dp") - @Test - public void testInflateFooterBlankTemplateTablet() { - testInflateFooterBlankTemplate(); - } - - @Test - public void testFooterXml() { - GlifLayout layout = new GlifLayout( - mContext, - Robolectric.buildAttributeSet() - .addAttribute(R.attr.suwFooter, "@android:layout/simple_list_item_1") - .build()); - - assertNotNull(layout.findViewById(android.R.id.text1)); - } - - @Test - public void inflateStickyHeader_shouldAddViewToLayout() { - GlifLayout layout = new GlifLayout(mContext); - - final View view = layout.inflateStickyHeader(android.R.layout.simple_list_item_1); - assertEquals(android.R.id.text1, view.getId()); - assertNotNull(layout.findViewById(android.R.id.text1)); - } - - @Config(qualifiers = "sw600dp") - @Test - public void inflateStickyHeader_whenOnTablets_shouldAddViewToLayout() { - inflateStickyHeader_shouldAddViewToLayout(); - } - - @Test - public void inflateStickyHeader_whenInXml_shouldAddViewToLayout() { - GlifLayout layout = new GlifLayout( - mContext, - Robolectric.buildAttributeSet() - .addAttribute(R.attr.suwStickyHeader, "@android:layout/simple_list_item_1") - .build()); - - assertNotNull(layout.findViewById(android.R.id.text1)); - } - - @Test - public void inflateStickyHeader_whenOnBlankTemplate_shouldAddViewToLayout() { - GlifLayout layout = new GlifLayout(mContext, R.layout.suw_glif_blank_template); - - final View view = layout.inflateStickyHeader(android.R.layout.simple_list_item_1); - assertEquals(android.R.id.text1, view.getId()); - assertNotNull(layout.findViewById(android.R.id.text1)); - } - - @Config(qualifiers = "sw600dp") - @Test - public void inflateStickyHeader_whenOnBlankTemplateTablet_shouldAddViewToLayout() { - inflateStickyHeader_whenOnBlankTemplate_shouldAddViewToLayout(); - } - - @Config(minSdk = Config.OLDEST_SDK, maxSdk = Config.NEWEST_SDK) - @Test - public void createFromXml_shouldSetLayoutFullscreen_whenLayoutFullscreenIsNotSet() { - GlifLayout layout = new GlifLayout( - mContext, - Robolectric.buildAttributeSet() - .build()); - if (VERSION.SDK_INT >= VERSION_CODES.M) { - assertEquals( - View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, - layout.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); - } - } - - @Test - public void createFromXml_shouldNotSetLayoutFullscreen_whenLayoutFullscreenIsFalse() { - GlifLayout layout = new GlifLayout( - mContext, - Robolectric.buildAttributeSet() - .addAttribute(R.attr.suwLayoutFullscreen, "false") - .build()); - - assertEquals( - 0, - layout.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); - } - - private Drawable getPhoneBackground(GlifLayout layout) { - final StatusBarBackgroundLayout patternBg = - (StatusBarBackgroundLayout) layout.findManagedViewById(R.id.suw_pattern_bg); - return patternBg.getStatusBarBackground(); - } - - private Drawable getTabletBackground(GlifLayout layout) { - final View patternBg = layout.findManagedViewById(R.id.suw_pattern_bg); - return patternBg.getBackground(); - } - - 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 scrollView = layout.findViewById(R.id.suw_scroll_view); - assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView); - } + private Context context; + + @Before + public void setUpContext() { + context = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light); + } + + @Test + public void testDefaultTemplate() { + GlifLayout layout = new GlifLayout(context); + assertDefaultTemplateInflated(layout); + } + + @Test + public void testSetHeaderText() { + GlifLayout layout = new GlifLayout(context); + TextView title = layout.findViewById(R.id.suw_layout_title); + layout.setHeaderText("Abracadabra"); + assertWithMessage("Header text should be \"Abracadabra\"") + .that(title.getText().toString()) + .isEqualTo("Abracadabra"); + } + + @Test + public void testAddView() { + @IdRes int testViewId = 123456; + GlifLayout layout = new GlifLayout(context); + TextView tv = new TextView(context); + tv.setId(testViewId); + layout.addView(tv); + assertDefaultTemplateInflated(layout); + View view = layout.findViewById(testViewId); + assertThat(view).named("Text view added").isSameAs(tv); + } + + @Test + public void testGetScrollView() { + GlifLayout layout = new GlifLayout(context); + assertWithMessage("Get scroll view should not be null with default template") + .that(layout.getScrollView()) + .isNotNull(); + } + + @Test + public void testSetPrimaryColor() { + GlifLayout layout = new GlifLayout(context); + layout.setProgressBarShown(true); + layout.setPrimaryColor(ColorStateList.valueOf(Color.RED)); + assertWithMessage("Primary color should be red") + .that(layout.getPrimaryColor()) + .isEqualTo(ColorStateList.valueOf(Color.RED)); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + ProgressBar progressBar = layout.findViewById(R.id.suw_layout_progress); + assertThat(progressBar.getIndeterminateTintList()) + .named("indeterminate progress bar tint") + .isEqualTo(ColorStateList.valueOf(Color.RED)); + assertThat(progressBar.getProgressBackgroundTintList()) + .named("determinate progress bar tint") + .isEqualTo(ColorStateList.valueOf(Color.RED)); + } + } + + @Config(qualifiers = "sw600dp") + @Test + public void testSetPrimaryColorTablet() { + GlifLayout layout = new GlifLayout(context); + layout.setProgressBarShown(true); + layout.setPrimaryColor(ColorStateList.valueOf(Color.RED)); + assertWithMessage("Primary color should be red") + .that(layout.getPrimaryColor()) + .isEqualTo(ColorStateList.valueOf(Color.RED)); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + ProgressBar progressBar = layout.findViewById(R.id.suw_layout_progress); + assertWithMessage("Progress bar should be tinted red") + .that(progressBar.getIndeterminateTintList()) + .isEqualTo(ColorStateList.valueOf(Color.RED)); + assertWithMessage("Determinate progress bar should also be tinted red") + .that(progressBar.getProgressBackgroundTintList()) + .isEqualTo(ColorStateList.valueOf(Color.RED)); + } + + assertThat(((GlifPatternDrawable) getTabletBackground(layout)).getColor()).isEqualTo(Color.RED); + } + + @Test + public void testSetBackgroundBaseColor() { + GlifLayout layout = new GlifLayout(context); + layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE)); + layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED)); + + assertThat(((GlifPatternDrawable) getPhoneBackground(layout)).getColor()).isEqualTo(Color.RED); + assertThat(layout.getBackgroundBaseColor().getDefaultColor()).isEqualTo(Color.RED); + } + + @Config(qualifiers = "sw600dp") + @Test + public void testSetBackgroundBaseColorTablet() { + GlifLayout layout = new GlifLayout(context); + layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE)); + layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED)); + + assertThat(((GlifPatternDrawable) getTabletBackground(layout)).getColor()).isEqualTo(Color.RED); + assertThat(layout.getBackgroundBaseColor().getDefaultColor()).isEqualTo(Color.RED); + } + + @Test + public void testSetBackgroundPatternedTrue() { + GlifLayout layout = new GlifLayout(context); + layout.setBackgroundPatterned(true); + + assertThat(getPhoneBackground(layout)).isInstanceOf(GlifPatternDrawable.class); + assertThat(layout.isBackgroundPatterned()).named("background is patterned").isTrue(); + } + + @Test + public void testSetBackgroundPatternedFalse() { + GlifLayout layout = new GlifLayout(context); + layout.setBackgroundPatterned(false); + + assertThat(getPhoneBackground(layout)).isInstanceOf(ColorDrawable.class); + assertThat(layout.isBackgroundPatterned()).named("background is patterned").isFalse(); + } + + @Config(qualifiers = "sw600dp") + @Test + public void testSetBackgroundPatternedTrueTablet() { + GlifLayout layout = new GlifLayout(context); + layout.setBackgroundPatterned(true); + + assertThat(getTabletBackground(layout)).isInstanceOf(GlifPatternDrawable.class); + assertThat(layout.isBackgroundPatterned()).named("background is patterned").isTrue(); + } + + @Config(qualifiers = "sw600dp") + @Test + public void testSetBackgroundPatternedFalseTablet() { + GlifLayout layout = new GlifLayout(context); + layout.setBackgroundPatterned(false); + + assertThat(getTabletBackground(layout)).isInstanceOf(ColorDrawable.class); + assertThat(layout.isBackgroundPatterned()).named("background is patterned").isFalse(); + } + + @Test + public void testNonGlifTheme() { + context = new ContextThemeWrapper(application, android.R.style.Theme); + new GlifLayout(context); + // Inflating with a non-GLIF theme should not crash + } + + @Test + public void testPeekProgressBarNull() { + GlifLayout layout = new GlifLayout(context); + assertWithMessage("PeekProgressBar should return null initially") + .that(layout.peekProgressBar()) + .isNull(); + } + + @Test + public void testPeekProgressBar() { + GlifLayout layout = new GlifLayout(context); + layout.setProgressBarShown(true); + assertWithMessage("Peek progress bar should return the bar after setProgressBarShown(true)") + .that(layout.peekProgressBar()) + .isNotNull(); + } + + @Test + public void testMixins() { + GlifLayout layout = new GlifLayout(context); + final HeaderMixin header = layout.getMixin(HeaderMixin.class); + assertThat(header).named("header").isInstanceOf(ColoredHeaderMixin.class); + + assertWithMessage("GlifLayout should have icon mixin") + .that(layout.getMixin(IconMixin.class)) + .isNotNull(); + assertWithMessage("GlifLayout should have progress bar mixin") + .that(layout.getMixin(ProgressBarMixin.class)) + .isNotNull(); + } + + @Test + public void testInflateFooter() { + GlifLayout layout = new GlifLayout(context); + + final View view = layout.inflateFooter(android.R.layout.simple_list_item_1); + assertThat(view.getId()).isEqualTo(android.R.id.text1); + assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull(); + } + + @Config(qualifiers = "sw600dp") + @Test + public void testInflateFooterTablet() { + testInflateFooter(); + } + + @Test + public void testInflateFooterBlankTemplate() { + GlifLayout layout = new GlifLayout(context, R.layout.suw_glif_blank_template); + + final View view = layout.inflateFooter(android.R.layout.simple_list_item_1); + assertThat(view.getId()).isEqualTo(android.R.id.text1); + assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull(); + } + + @Config(qualifiers = "sw600dp") + @Test + public void testInflateFooterBlankTemplateTablet() { + testInflateFooterBlankTemplate(); + } + + @Test + public void testFooterXml() { + GlifLayout layout = + new GlifLayout( + context, + Robolectric.buildAttributeSet() + .addAttribute(R.attr.suwFooter, "@android:layout/simple_list_item_1") + .build()); + + assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull(); + } + + @Test + public void inflateStickyHeader_shouldAddViewToLayout() { + GlifLayout layout = new GlifLayout(context); + + final View view = layout.inflateStickyHeader(android.R.layout.simple_list_item_1); + assertThat(view.getId()).isEqualTo(android.R.id.text1); + assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull(); + } + + @Config(qualifiers = "sw600dp") + @Test + public void inflateStickyHeader_whenOnTablets_shouldAddViewToLayout() { + inflateStickyHeader_shouldAddViewToLayout(); + } + + @Test + public void inflateStickyHeader_whenInXml_shouldAddViewToLayout() { + GlifLayout layout = + new GlifLayout( + context, + Robolectric.buildAttributeSet() + .addAttribute(R.attr.suwStickyHeader, "@android:layout/simple_list_item_1") + .build()); + + assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull(); + } + + @Test + public void inflateStickyHeader_whenOnBlankTemplate_shouldAddViewToLayout() { + GlifLayout layout = new GlifLayout(context, R.layout.suw_glif_blank_template); + + final View view = layout.inflateStickyHeader(android.R.layout.simple_list_item_1); + assertThat(view.getId()).isEqualTo(android.R.id.text1); + assertThat((View) layout.findViewById(android.R.id.text1)).isNotNull(); + } + + @Config(qualifiers = "sw600dp") + @Test + public void inflateStickyHeader_whenOnBlankTemplateTablet_shouldAddViewToLayout() { + inflateStickyHeader_whenOnBlankTemplate_shouldAddViewToLayout(); + } + + @Config(minSdk = Config.OLDEST_SDK, maxSdk = Config.NEWEST_SDK) + @Test + public void createFromXml_shouldSetLayoutFullscreen_whenLayoutFullscreenIsNotSet() { + GlifLayout layout = new GlifLayout(context, Robolectric.buildAttributeSet().build()); + if (VERSION.SDK_INT >= VERSION_CODES.M) { + assertThat(layout.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) + .isEqualTo(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); + } + } + + @Test + public void createFromXml_shouldNotSetLayoutFullscreen_whenLayoutFullscreenIsFalse() { + GlifLayout layout = + new GlifLayout( + context, + Robolectric.buildAttributeSet() + .addAttribute(R.attr.suwLayoutFullscreen, "false") + .build()); + + assertThat(layout.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN).isEqualTo(0); + } + + private Drawable getPhoneBackground(GlifLayout layout) { + final StatusBarBackgroundLayout patternBg = layout.findManagedViewById(R.id.suw_pattern_bg); + return patternBg.getStatusBarBackground(); + } + + private Drawable getTabletBackground(GlifLayout layout) { + final View patternBg = layout.findManagedViewById(R.id.suw_pattern_bg); + return patternBg.getBackground(); + } + + private void assertDefaultTemplateInflated(GlifLayout layout) { + View title = layout.findViewById(R.id.suw_layout_title); + assertWithMessage("@id/suw_layout_title should not be null").that(title).isNotNull(); + + View icon = layout.findViewById(R.id.suw_layout_icon); + assertWithMessage("@id/suw_layout_icon should not be null").that(icon).isNotNull(); + + View scrollView = layout.findViewById(R.id.suw_scroll_view); + assertWithMessage("@id/suw_scroll_view should be a ScrollView") + .that(scrollView instanceof ScrollView) + .isTrue(); + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/gesture/ConsecutiveTapsGestureDetectorTest.java b/library/test/robotest/src/com/android/setupwizardlib/gesture/ConsecutiveTapsGestureDetectorTest.java index aa2cce3..ba3e2c5 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/gesture/ConsecutiveTapsGestureDetectorTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/gesture/ConsecutiveTapsGestureDetectorTest.java @@ -23,96 +23,93 @@ import static org.robolectric.RuntimeEnvironment.application; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; - -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) -@RunWith(SuwLibRobolectricTestRunner.class) +@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) +@RunWith(RobolectricTestRunner.class) public class ConsecutiveTapsGestureDetectorTest { - @Mock - private ConsecutiveTapsGestureDetector.OnConsecutiveTapsListener mListener; + @Mock private ConsecutiveTapsGestureDetector.OnConsecutiveTapsListener listener; - private ConsecutiveTapsGestureDetector mDetector; - private int mSlop; - private int mTapTimeout; + private ConsecutiveTapsGestureDetector detector; + private int slop; + private int tapTimeout; - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); - View view = new View(application); - view.measure(500, 500); - view.layout(0, 0, 500, 500); - mDetector = new ConsecutiveTapsGestureDetector(mListener, view); + View view = new View(application); + view.measure(500, 500); + view.layout(0, 0, 500, 500); + detector = new ConsecutiveTapsGestureDetector(listener, view); - mSlop = ViewConfiguration.get(application).getScaledDoubleTapSlop(); - mTapTimeout = ViewConfiguration.getDoubleTapTimeout(); - } + slop = ViewConfiguration.get(application).getScaledDoubleTapSlop(); + tapTimeout = ViewConfiguration.getDoubleTapTimeout(); + } - @Test - public void onTouchEvent_shouldTriggerCallbackOnFourTaps() { - InOrder inOrder = inOrder(mListener); + @Test + public void onTouchEvent_shouldTriggerCallbackOnFourTaps() { + InOrder inOrder = inOrder(listener); - tap(0, 25f, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(1)); + tap(0, 25f, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(1)); - tap(100, 25f, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(2)); + tap(100, 25f, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(2)); - tap(200, 25f, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(3)); + tap(200, 25f, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(3)); - tap(300, 25f, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(4)); - } + tap(300, 25f, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(4)); + } - @Test - public void onTouchEvent_tapOnDifferentLocation_shouldResetCounter() { - InOrder inOrder = inOrder(mListener); + @Test + public void onTouchEvent_tapOnDifferentLocation_shouldResetCounter() { + InOrder inOrder = inOrder(listener); - tap(0, 25f, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(1)); + tap(0, 25f, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(1)); - tap(100, 25f, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(2)); + tap(100, 25f, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(2)); - tap(200, 25f + mSlop * 2, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(1)); + tap(200, 25f + slop * 2, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(1)); - tap(300, 25f + mSlop * 2, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(2)); - } + tap(300, 25f + slop * 2, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(2)); + } - @Test - public void onTouchEvent_tapAfterTimeout_shouldResetCounter() { - InOrder inOrder = inOrder(mListener); + @Test + public void onTouchEvent_tapAfterTimeout_shouldResetCounter() { + InOrder inOrder = inOrder(listener); - tap(0, 25f, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(1)); + tap(0, 25f, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(1)); - tap(100, 25f, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(2)); + tap(100, 25f, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(2)); - tap(200 + mTapTimeout, 25f, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(1)); + tap(200 + tapTimeout, 25f, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(1)); - tap(300 + mTapTimeout, 25f, 25f); - inOrder.verify(mListener).onConsecutiveTaps(eq(2)); - } + tap(300 + tapTimeout, 25f, 25f); + inOrder.verify(listener).onConsecutiveTaps(eq(2)); + } - private void tap(int timeMillis, float x, float y) { - mDetector.onTouchEvent( - MotionEvent.obtain(timeMillis, timeMillis, MotionEvent.ACTION_DOWN, x, y, 0)); - mDetector.onTouchEvent( - MotionEvent.obtain(timeMillis, timeMillis + 10, MotionEvent.ACTION_UP, x, y, 0)); - } + private void tap(int timeMillis, float x, float y) { + detector.onTouchEvent( + MotionEvent.obtain(timeMillis, timeMillis, MotionEvent.ACTION_DOWN, x, y, 0)); + detector.onTouchEvent( + MotionEvent.obtain(timeMillis, timeMillis + 10, MotionEvent.ACTION_UP, x, y, 0)); + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/items/ButtonItemTest.java b/library/test/robotest/src/com/android/setupwizardlib/items/ButtonItemTest.java index 40e5da8..b51e875 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/items/ButtonItemTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/items/ButtonItemTest.java @@ -16,12 +16,8 @@ package com.android.setupwizardlib.items; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Matchers.same; @@ -31,152 +27,157 @@ import static org.mockito.Mockito.verify; import static org.robolectric.RuntimeEnvironment.application; import android.content.Context; -import android.text.TextUtils; import android.view.ContextThemeWrapper; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.FrameLayout; import android.widget.LinearLayout; - import com.android.setupwizardlib.R; import com.android.setupwizardlib.items.ButtonItem.OnClickListener; -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -@RunWith(SuwLibRobolectricTestRunner.class) -@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) public class ButtonItemTest { - private ViewGroup mParent; - private Context mContext; - - @Before - public void setUp() { - mContext = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light); - mParent = new LinearLayout(mContext); - } - - @Test - public void testDefaultItem() { - ButtonItem item = new ButtonItem(); - - assertTrue("ButtonItem should be enabled by default", item.isEnabled()); - assertEquals("ButtonItem should return count = 0", 0, item.getCount()); - assertEquals("ButtonItem should return layout resource = 0", 0, item.getLayoutResource()); - assertEquals("Default theme should be @style/SuwButtonItem", R.style.SuwButtonItem, - item.getTheme()); - assertNull("Default text should be null", item.getText()); - } - - @Test - public void testOnBindView() { - ButtonItem item = new ButtonItem(); - - try { - item.onBindView(new View(mContext)); - fail("Calling onBindView on ButtonItem should throw UnsupportedOperationException"); - } catch (UnsupportedOperationException e) { - // pass - } - } - - @Test - public void testCreateButton() { - TestButtonItem item = new TestButtonItem(); - final Button button = item.createButton(mParent); - - assertTrue("Default button should be enabled", button.isEnabled()); - assertTrue("Default button text should be empty", TextUtils.isEmpty(button.getText())); - } - - @Test - public void testButtonItemSetsItsId() { - TestButtonItem item = new TestButtonItem(); - final int id = 12345; - item.setId(id); - - assertEquals("Button's id should be set", item.createButton(mParent).getId(), id); - } - - @Test - public void testCreateButtonTwice() { - TestButtonItem item = new TestButtonItem(); - final Button button = item.createButton(mParent); - - FrameLayout frameLayout = new FrameLayout(mContext); - frameLayout.addView(button); - - final Button button2 = item.createButton(mParent); - assertSame("createButton should be reused", button, button2); - assertNull("Should be removed from parent after createButton", button2.getParent()); + private ViewGroup parent; + private Context context; + + @Before + public void setUp() { + context = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light); + parent = new LinearLayout(context); + } + + @Test + public void testDefaultItem() { + ButtonItem item = new ButtonItem(); + + assertThat(item.isEnabled()).named("enabled").isTrue(); + assertThat(item.getCount()).named("count").isEqualTo(0); + assertThat(item.getLayoutResource()).named("layout resource").isEqualTo(0); + assertThat(item.getTheme()).named("theme").isEqualTo(R.style.SuwButtonItem); + assertThat(item.getText()).named("text").isNull(); + } + + @Test + public void testOnBindView() { + ButtonItem item = new ButtonItem(); + + try { + item.onBindView(new View(context)); + fail("Calling onBindView on ButtonItem should throw UnsupportedOperationException"); + } catch (UnsupportedOperationException e) { + // pass } - - @Test - public void testSetEnabledTrue() { - TestButtonItem item = new TestButtonItem(); - item.setEnabled(true); - - final Button button = item.createButton(mParent); - assertTrue("ButtonItem should be enabled", item.isEnabled()); - assertTrue("Button should be enabled", button.isEnabled()); - } - - @Test - public void testSetEnabledFalse() { - TestButtonItem item = new TestButtonItem(); - item.setEnabled(false); - - final Button button = item.createButton(mParent); - assertFalse("ButtonItem should be disabled", item.isEnabled()); - assertFalse("Button should be disabled", button.isEnabled()); - } - - @Test - public void testSetText() { - TestButtonItem item = new TestButtonItem(); - item.setText("lorem ipsum"); - - final Button button = item.createButton(mParent); - assertEquals("ButtonItem text should be \"lorem ipsum\"", "lorem ipsum", item.getText()); - assertEquals("Button text should be \"lorem ipsum\"", "lorem ipsum", button.getText()); - } - - @Test - public void testSetTheme() { - TestButtonItem item = new TestButtonItem(); - item.setTheme(R.style.SuwButtonItem_Colored); - - final Button button = item.createButton(mParent); - assertEquals("ButtonItem theme should be SuwButtonItem.Colored", - R.style.SuwButtonItem_Colored, item.getTheme()); - assertNotNull(button.getContext().getTheme()); - } - - @Test - public void testOnClickListener() { - TestButtonItem item = new TestButtonItem(); - final OnClickListener listener = mock(OnClickListener.class); - item.setOnClickListener(listener); - - verify(listener, never()).onClick(any(ButtonItem.class)); - - final Button button = item.createButton(mParent); - button.performClick(); - - verify(listener).onClick(same(item)); - } - - private static class TestButtonItem extends ButtonItem { - - @Override - public Button createButton(ViewGroup parent) { - // Make this method public for testing - return super.createButton(parent); - } + } + + @Test + public void testCreateButton() { + TestButtonItem item = new TestButtonItem(); + final Button button = item.createButton(parent); + + assertThat(button.isEnabled()).named("enabled").isTrue(); + assertThat(button.getText().toString()).isEmpty(); + } + + @Test + public void testButtonItemSetsItsId() { + TestButtonItem item = new TestButtonItem(); + final int id = 12345; + item.setId(id); + + assertWithMessage("Button's id should be set") + .that(item.createButton(parent).getId()) + .isEqualTo(id); + } + + @Test + public void testCreateButtonTwice() { + TestButtonItem item = new TestButtonItem(); + final Button button = item.createButton(parent); + + FrameLayout frameLayout = new FrameLayout(context); + frameLayout.addView(button); + + final Button button2 = item.createButton(parent); + assertWithMessage("createButton should be reused").that(button2).isSameAs(button); + assertWithMessage("Should be removed from parent after createButton") + .that(button2.getParent()) + .isNull(); + } + + @Test + public void testSetEnabledTrue() { + TestButtonItem item = new TestButtonItem(); + item.setEnabled(true); + + final Button button = item.createButton(parent); + assertWithMessage("ButtonItem should be enabled").that(item.isEnabled()).isTrue(); + assertWithMessage("Button should be enabled").that(button.isEnabled()).isTrue(); + } + + @Test + public void testSetEnabledFalse() { + TestButtonItem item = new TestButtonItem(); + item.setEnabled(false); + + final Button button = item.createButton(parent); + assertWithMessage("ButtonItem should be disabled").that(item.isEnabled()).isFalse(); + assertWithMessage("Button should be disabled").that(button.isEnabled()).isFalse(); + } + + @Test + public void testSetText() { + TestButtonItem item = new TestButtonItem(); + item.setText("lorem ipsum"); + + final Button button = item.createButton(parent); + assertWithMessage("ButtonItem text should be \"lorem ipsum\"") + .that(item.getText().toString()) + .isEqualTo("lorem ipsum"); + assertWithMessage("Button text should be \"lorem ipsum\"") + .that(button.getText().toString()) + .isEqualTo("lorem ipsum"); + } + + @Test + public void testSetTheme() { + TestButtonItem item = new TestButtonItem(); + item.setTheme(R.style.SuwButtonItem_Colored); + + final Button button = item.createButton(parent); + assertWithMessage("ButtonItem theme should be SuwButtonItem.Colored") + .that(item.getTheme()) + .isEqualTo(R.style.SuwButtonItem_Colored); + assertThat(button.getContext().getTheme()).isNotNull(); + } + + @Test + public void testOnClickListener() { + TestButtonItem item = new TestButtonItem(); + final OnClickListener listener = mock(OnClickListener.class); + item.setOnClickListener(listener); + + verify(listener, never()).onClick(any(ButtonItem.class)); + + final Button button = item.createButton(parent); + button.performClick(); + + verify(listener).onClick(same(item)); + } + + private static class TestButtonItem extends ButtonItem { + + @Override + public Button createButton(ViewGroup parent) { + // Make this method public for testing + return super.createButton(parent); } + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/items/ItemGroupTest.java b/library/test/robotest/src/com/android/setupwizardlib/items/ItemGroupTest.java index ecaec71..3cbc576 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/items/ItemGroupTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/items/ItemGroupTest.java @@ -16,289 +16,308 @@ package com.android.setupwizardlib.items; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; +import static com.google.common.truth.Truth.assertWithMessage; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -@RunWith(SuwLibRobolectricTestRunner.class) -@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) public class ItemGroupTest { - private static final Item CHILD_1 = new EqualsItem("Child 1"); - private static final Item CHILD_2 = new EqualsItem("Child 2"); - private static final Item CHILD_3 = new EqualsItem("Child 3"); - private static final Item CHILD_4 = new EqualsItem("Child 4"); - - private ItemGroup mItemGroup; - - @Mock - private ItemHierarchy.Observer mObserver; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - mItemGroup = new ItemGroup(); - mItemGroup.registerObserver(mObserver); - } - - @Test - public void testGroup() { - mItemGroup.addChild(CHILD_1); - mItemGroup.addChild(CHILD_2); - - assertSame("Item at position 0 should be child1", CHILD_1, mItemGroup.getItemAt(0)); - assertSame("Item at position 1 should be child2", CHILD_2, mItemGroup.getItemAt(1)); - assertEquals("Should have 2 children", 2, mItemGroup.getCount()); - - final InOrder inOrder = inOrder(mObserver); - inOrder.verify(mObserver).onItemRangeInserted(eq(mItemGroup), eq(0), eq(1)); - inOrder.verify(mObserver).onItemRangeInserted(eq(mItemGroup), eq(1), eq(1)); - } - - @Test - public void testRemoveChild() { - mItemGroup.addChild(CHILD_1); - mItemGroup.addChild(CHILD_2); - mItemGroup.addChild(CHILD_3); - - mItemGroup.removeChild(CHILD_2); - - assertSame("Item at position 0 should be child1", CHILD_1, mItemGroup.getItemAt(0)); - assertSame("Item at position 1 should be child3", CHILD_3, mItemGroup.getItemAt(1)); - assertEquals("Should have 2 children", 2, mItemGroup.getCount()); - - verify(mObserver).onItemRangeRemoved(eq(mItemGroup), eq(1), eq(1)); - } - - @Test - public void testClear() { - mItemGroup.addChild(CHILD_1); - mItemGroup.addChild(CHILD_2); - - mItemGroup.clear(); - - assertEquals("Should have 0 child", 0, mItemGroup.getCount()); - - verify(mObserver).onItemRangeRemoved(eq(mItemGroup), eq(0), eq(2)); - } - - @Test - public void testNestedGroup() { - ItemGroup parentGroup = new ItemGroup(); - ItemGroup childGroup = new ItemGroup(); - parentGroup.registerObserver(mObserver); - - parentGroup.addChild(CHILD_1); - childGroup.addChild(CHILD_2); - childGroup.addChild(CHILD_3); - parentGroup.addChild(childGroup); - parentGroup.addChild(CHILD_4); - - assertSame("Position 0 should be child 1", CHILD_1, parentGroup.getItemAt(0)); - assertSame("Position 1 should be child 2", CHILD_2, parentGroup.getItemAt(1)); - assertSame("Position 2 should be child 3", CHILD_3, parentGroup.getItemAt(2)); - assertSame("Position 3 should be child 4", CHILD_4, parentGroup.getItemAt(3)); - - final InOrder inOrder = inOrder(mObserver); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(0), eq(1)); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(1), eq(2)); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(3), eq(1)); - verifyNoMoreInteractions(mObserver); - } - - @Test - public void testNestedGroupClearNotification() { - ItemGroup parentGroup = new ItemGroup(); - ItemGroup childGroup = new ItemGroup(); - parentGroup.registerObserver(mObserver); - - parentGroup.addChild(CHILD_1); - childGroup.addChild(CHILD_2); - childGroup.addChild(CHILD_3); - parentGroup.addChild(childGroup); - parentGroup.addChild(CHILD_4); - - childGroup.clear(); - - final InOrder inOrder = inOrder(mObserver); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(0), eq(1)); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(1), eq(2)); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(3), eq(1)); - verify(mObserver).onItemRangeRemoved(eq(parentGroup), eq(1), eq(2)); - verifyNoMoreInteractions(mObserver); - } - - @Test - public void testNestedGroupRemoveNotification() { - ItemGroup parentGroup = new ItemGroup(); - ItemGroup childGroup = new ItemGroup(); - parentGroup.registerObserver(mObserver); - - parentGroup.addChild(CHILD_1); - childGroup.addChild(CHILD_2); - childGroup.addChild(CHILD_3); - parentGroup.addChild(childGroup); - parentGroup.addChild(CHILD_4); - - childGroup.removeChild(CHILD_3); - childGroup.removeChild(CHILD_2); - - final InOrder inOrder = inOrder(mObserver); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(0), eq(1)); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(1), eq(2)); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(3), eq(1)); - inOrder.verify(mObserver).onItemRangeRemoved(eq(parentGroup), eq(2), eq(1)); - inOrder.verify(mObserver).onItemRangeRemoved(eq(parentGroup), eq(1), eq(1)); - verifyNoMoreInteractions(mObserver); + private static final Item CHILD_1 = new EqualsItem("Child 1"); + private static final Item CHILD_2 = new EqualsItem("Child 2"); + private static final Item CHILD_3 = new EqualsItem("Child 3"); + private static final Item CHILD_4 = new EqualsItem("Child 4"); + + private ItemGroup itemGroup; + + @Mock private ItemHierarchy.Observer observer; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + itemGroup = new ItemGroup(); + itemGroup.registerObserver(observer); + } + + @Test + public void testGroup() { + itemGroup.addChild(CHILD_1); + itemGroup.addChild(CHILD_2); + + assertWithMessage("Item at position 0 should be child1") + .that(itemGroup.getItemAt(0)) + .isSameAs(CHILD_1); + assertWithMessage("Item at position 1 should be child2") + .that(itemGroup.getItemAt(1)) + .isSameAs(CHILD_2); + assertWithMessage("Should have 2 children").that(itemGroup.getCount()).isEqualTo(2); + + final InOrder inOrder = inOrder(observer); + inOrder.verify(observer).onItemRangeInserted(eq(itemGroup), eq(0), eq(1)); + inOrder.verify(observer).onItemRangeInserted(eq(itemGroup), eq(1), eq(1)); + } + + @Test + public void testRemoveChild() { + itemGroup.addChild(CHILD_1); + itemGroup.addChild(CHILD_2); + itemGroup.addChild(CHILD_3); + + itemGroup.removeChild(CHILD_2); + + assertWithMessage("Item at position 0 should be child1") + .that(itemGroup.getItemAt(0)) + .isSameAs(CHILD_1); + assertWithMessage("Item at position 1 should be child3") + .that(itemGroup.getItemAt(1)) + .isSameAs(CHILD_3); + assertWithMessage("Should have 2 children").that(itemGroup.getCount()).isEqualTo(2); + + verify(observer).onItemRangeRemoved(eq(itemGroup), eq(1), eq(1)); + } + + @Test + public void testClear() { + itemGroup.addChild(CHILD_1); + itemGroup.addChild(CHILD_2); + + itemGroup.clear(); + + assertWithMessage("Should have 0 child").that(itemGroup.getCount()).isEqualTo(0); + + verify(observer).onItemRangeRemoved(eq(itemGroup), eq(0), eq(2)); + } + + @Test + public void testNestedGroup() { + ItemGroup parentGroup = new ItemGroup(); + ItemGroup childGroup = new ItemGroup(); + parentGroup.registerObserver(observer); + + parentGroup.addChild(CHILD_1); + childGroup.addChild(CHILD_2); + childGroup.addChild(CHILD_3); + parentGroup.addChild(childGroup); + parentGroup.addChild(CHILD_4); + + assertWithMessage("Position 0 should be child 1") + .that(parentGroup.getItemAt(0)) + .isSameAs(CHILD_1); + assertWithMessage("Position 1 should be child 2") + .that(parentGroup.getItemAt(1)) + .isSameAs(CHILD_2); + assertWithMessage("Position 2 should be child 3") + .that(parentGroup.getItemAt(2)) + .isSameAs(CHILD_3); + assertWithMessage("Position 3 should be child 4") + .that(parentGroup.getItemAt(3)) + .isSameAs(CHILD_4); + + final InOrder inOrder = inOrder(observer); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(0), eq(1)); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(1), eq(2)); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(3), eq(1)); + verifyNoMoreInteractions(observer); + } + + @Test + public void testNestedGroupClearNotification() { + ItemGroup parentGroup = new ItemGroup(); + ItemGroup childGroup = new ItemGroup(); + parentGroup.registerObserver(observer); + + parentGroup.addChild(CHILD_1); + childGroup.addChild(CHILD_2); + childGroup.addChild(CHILD_3); + parentGroup.addChild(childGroup); + parentGroup.addChild(CHILD_4); + + childGroup.clear(); + + final InOrder inOrder = inOrder(observer); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(0), eq(1)); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(1), eq(2)); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(3), eq(1)); + verify(observer).onItemRangeRemoved(eq(parentGroup), eq(1), eq(2)); + verifyNoMoreInteractions(observer); + } + + @Test + public void testNestedGroupRemoveNotification() { + ItemGroup parentGroup = new ItemGroup(); + ItemGroup childGroup = new ItemGroup(); + parentGroup.registerObserver(observer); + + parentGroup.addChild(CHILD_1); + childGroup.addChild(CHILD_2); + childGroup.addChild(CHILD_3); + parentGroup.addChild(childGroup); + parentGroup.addChild(CHILD_4); + + childGroup.removeChild(CHILD_3); + childGroup.removeChild(CHILD_2); + + final InOrder inOrder = inOrder(observer); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(0), eq(1)); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(1), eq(2)); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(3), eq(1)); + inOrder.verify(observer).onItemRangeRemoved(eq(parentGroup), eq(2), eq(1)); + inOrder.verify(observer).onItemRangeRemoved(eq(parentGroup), eq(1), eq(1)); + verifyNoMoreInteractions(observer); + } + + @Test + public void testNestedGroupClear() { + ItemGroup parentGroup = new ItemGroup(); + ItemGroup childGroup = new ItemGroup(); + parentGroup.registerObserver(observer); + + parentGroup.addChild(CHILD_1); + childGroup.addChild(CHILD_2); + childGroup.addChild(CHILD_3); + parentGroup.addChild(childGroup); + + childGroup.clear(); + + final InOrder inOrder = inOrder(observer); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(0), eq(1)); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(1), eq(2)); + inOrder.verify(observer).onItemRangeRemoved(eq(parentGroup), eq(1), eq(2)); + verifyNoMoreInteractions(observer); + } + + @Test + public void testNestedGroupRemoveLastChild() { + ItemGroup parentGroup = new ItemGroup(); + ItemGroup childGroup1 = new ItemGroup(); + ItemGroup childGroup2 = new ItemGroup(); + parentGroup.registerObserver(observer); + + childGroup1.addChild(CHILD_1); + childGroup1.addChild(CHILD_2); + parentGroup.addChild(childGroup1); + childGroup2.addChild(CHILD_3); + childGroup2.addChild(CHILD_4); + parentGroup.addChild(childGroup2); + + childGroup2.removeChild(CHILD_4); + childGroup2.removeChild(CHILD_3); + + final InOrder inOrder = inOrder(observer); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(0), eq(2)); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(2), eq(2)); + inOrder.verify(observer).onItemRangeRemoved(eq(parentGroup), eq(3), eq(1)); + inOrder.verify(observer).onItemRangeRemoved(eq(parentGroup), eq(2), eq(1)); + verifyNoMoreInteractions(observer); + } + + @Test + public void testNestedGroupClearOnlyChild() { + ItemGroup parentGroup = new ItemGroup(); + ItemGroup childGroup = new ItemGroup(); + parentGroup.registerObserver(observer); + + childGroup.addChild(CHILD_1); + childGroup.addChild(CHILD_2); + parentGroup.addChild(childGroup); + + childGroup.clear(); + + final InOrder inOrder = inOrder(observer); + inOrder.verify(observer).onItemRangeInserted(eq(parentGroup), eq(0), eq(2)); + inOrder.verify(observer).onItemRangeRemoved(eq(parentGroup), eq(0), eq(2)); + verifyNoMoreInteractions(observer); + } + + @Test + public void testNotifyChange() { + itemGroup.addChild(CHILD_1); + itemGroup.addChild(CHILD_2); + + CHILD_2.setTitle("Child 2 modified"); + + verify(observer).onItemRangeChanged(eq(itemGroup), eq(1), eq(1)); + } + + @Test + public void testEmptyChildGroup() { + ItemGroup parentGroup = new ItemGroup(); + ItemGroup childGroup = new ItemGroup(); + + parentGroup.addChild(CHILD_1); + parentGroup.addChild(childGroup); + parentGroup.addChild(CHILD_2); + + assertWithMessage("Position 0 should be child 1") + .that(parentGroup.getItemAt(0)) + .isSameAs(CHILD_1); + assertWithMessage("Position 1 should be child 2") + .that(parentGroup.getItemAt(1)) + .isSameAs(CHILD_2); + } + + @Test + public void testFindItemById() { + CHILD_1.setId(12345); + CHILD_2.setId(23456); + + itemGroup.addChild(CHILD_1); + itemGroup.addChild(CHILD_2); + + assertWithMessage("Find item 23456 should return child 2") + .that(itemGroup.findItemById(23456)) + .isSameAs(CHILD_2); + } + + @Test + public void testFindItemByIdNotFound() { + CHILD_1.setId(12345); + CHILD_2.setId(23456); + + itemGroup.addChild(CHILD_1); + itemGroup.addChild(CHILD_2); + + assertWithMessage("ID not found should return null") + .that(itemGroup.findItemById(56789)) + .isNull(); + } + + /** + * This class will always return true on {@link #equals(Object)}. Used to ensure that ItemGroup is + * using identity rather than equals(). Be sure to use assertSame rather than assertEquals when + * comparing items of this class. + */ + private static class EqualsItem extends Item { + + EqualsItem(String name) { + setTitle(name); } - @Test - public void testNestedGroupClear() { - ItemGroup parentGroup = new ItemGroup(); - ItemGroup childGroup = new ItemGroup(); - parentGroup.registerObserver(mObserver); - - parentGroup.addChild(CHILD_1); - childGroup.addChild(CHILD_2); - childGroup.addChild(CHILD_3); - parentGroup.addChild(childGroup); - - childGroup.clear(); - - final InOrder inOrder = inOrder(mObserver); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(0), eq(1)); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(1), eq(2)); - inOrder.verify(mObserver).onItemRangeRemoved(eq(parentGroup), eq(1), eq(2)); - verifyNoMoreInteractions(mObserver); - } - - @Test - public void testNestedGroupRemoveLastChild() { - ItemGroup parentGroup = new ItemGroup(); - ItemGroup childGroup1 = new ItemGroup(); - ItemGroup childGroup2 = new ItemGroup(); - parentGroup.registerObserver(mObserver); - - childGroup1.addChild(CHILD_1); - childGroup1.addChild(CHILD_2); - parentGroup.addChild(childGroup1); - childGroup2.addChild(CHILD_3); - childGroup2.addChild(CHILD_4); - parentGroup.addChild(childGroup2); - - childGroup2.removeChild(CHILD_4); - childGroup2.removeChild(CHILD_3); - - final InOrder inOrder = inOrder(mObserver); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(0), eq(2)); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(2), eq(2)); - inOrder.verify(mObserver).onItemRangeRemoved(eq(parentGroup), eq(3), eq(1)); - inOrder.verify(mObserver).onItemRangeRemoved(eq(parentGroup), eq(2), eq(1)); - verifyNoMoreInteractions(mObserver); - } - - @Test - public void testNestedGroupClearOnlyChild() { - ItemGroup parentGroup = new ItemGroup(); - ItemGroup childGroup = new ItemGroup(); - parentGroup.registerObserver(mObserver); - - childGroup.addChild(CHILD_1); - childGroup.addChild(CHILD_2); - parentGroup.addChild(childGroup); - - childGroup.clear(); - - final InOrder inOrder = inOrder(mObserver); - inOrder.verify(mObserver).onItemRangeInserted(eq(parentGroup), eq(0), eq(2)); - inOrder.verify(mObserver).onItemRangeRemoved(eq(parentGroup), eq(0), eq(2)); - verifyNoMoreInteractions(mObserver); - } - - @Test - public void testNotifyChange() { - mItemGroup.addChild(CHILD_1); - mItemGroup.addChild(CHILD_2); - - CHILD_2.setTitle("Child 2 modified"); - - verify(mObserver).onItemRangeChanged(eq(mItemGroup), eq(1), eq(1)); - } - - @Test - public void testEmptyChildGroup() { - ItemGroup parentGroup = new ItemGroup(); - ItemGroup childGroup = new ItemGroup(); - - parentGroup.addChild(CHILD_1); - parentGroup.addChild(childGroup); - parentGroup.addChild(CHILD_2); - - assertSame("Position 0 should be child 1", CHILD_1, parentGroup.getItemAt(0)); - assertSame("Position 1 should be child 2", CHILD_2, parentGroup.getItemAt(1)); + @Override + public int hashCode() { + return 1; } - @Test - public void testFindItemById() { - CHILD_1.setId(12345); - CHILD_2.setId(23456); - - mItemGroup.addChild(CHILD_1); - mItemGroup.addChild(CHILD_2); - - assertSame("Find item 23456 should return child 2", - CHILD_2, mItemGroup.findItemById(23456)); - } - - @Test - public void testFindItemByIdNotFound() { - CHILD_1.setId(12345); - CHILD_2.setId(23456); - - mItemGroup.addChild(CHILD_1); - mItemGroup.addChild(CHILD_2); - - assertNull("ID not found should return null", mItemGroup.findItemById(56789)); + @Override + public boolean equals(Object obj) { + return obj instanceof Item; } - /** - * This class will always return true on {@link #equals(Object)}. Used to ensure that ItemGroup - * is using identity rather than equals(). Be sure to use assertSame rather than assertEquals - * when comparing items of this class. - */ - private static class EqualsItem extends Item { - - EqualsItem(String name) { - setTitle(name); - } - - @Override - public int hashCode() { - return 1; - } - - @Override - public boolean equals(Object obj) { - return obj instanceof Item; - } - - @Override - public String toString() { - return "EqualsItem{title=" + getTitle() + "}"; - } + @Override + public String toString() { + return "EqualsItem{title=" + getTitle() + "}"; } + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/robolectric/ExternalResources.java b/library/test/robotest/src/com/android/setupwizardlib/robolectric/ExternalResources.java new file mode 100644 index 0000000..06ef508 --- /dev/null +++ b/library/test/robotest/src/com/android/setupwizardlib/robolectric/ExternalResources.java @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.setupwizardlib.robolectric; + +import static org.robolectric.RuntimeEnvironment.application; +import static org.robolectric.Shadows.shadowOf; + +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager.NameNotFoundException; +import android.content.res.AssetManager; +import android.content.res.Configuration; +import androidx.annotation.AnyRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import android.util.DisplayMetrics; +import java.util.HashMap; +import java.util.Map; +import org.robolectric.res.ResName; +import org.robolectric.res.ResType; +import org.robolectric.res.TypedResource; +import org.robolectric.shadows.ShadowPackageManager; + +/** + * Utility class to inject resources for an "external" application in Robolectric tests. This can be + * used with {@link org.robolectric.shadows.ShadowPackageManager#resources} to simulate loading + * resources from another package. + */ +public final class ExternalResources { + + public static Resources injectExternalResources(String packageName) { + return injectExternalResources(createPackageInfo(packageName)); + } + + public static Resources injectExternalResources(PackageInfo packageInfo) { + try { + application.getPackageManager().getPackageInfo(packageInfo.packageName, 0); + } catch (NameNotFoundException e) { + // Add the package if it does not exist + shadowOf(application.getPackageManager()).addPackage(packageInfo); + } + Resources resources = Resources.forPackageName(packageInfo.packageName); + ShadowPackageManager.resources.put(packageInfo.packageName, resources); + return resources; + } + + /** + * Constructed resources for testing, representing resources external to the current package under + * test. + */ + public static class Resources extends android.content.res.Resources { + + private final String packageName; + + public static Resources forPackageName(String packageName) { + android.content.res.Resources res = application.getResources(); + return new Resources( + packageName, res.getAssets(), res.getDisplayMetrics(), res.getConfiguration()); + } + + private Resources( + String packageName, AssetManager assets, DisplayMetrics metrics, Configuration config) { + super(assets, metrics, config); + this.packageName = packageName; + } + + @Override + public int getIdentifier(String name, String defType, String defPackage) { + Integer resourceId = resourceIds.get(ResName.qualifyResName(name, defPackage, defType)); + if (resourceId == null) { + return 0; + } + return resourceId; + } + + @Override + public int getInteger(int id) { + return (int) get(id, ResType.INTEGER); + } + + public void putInteger(String name, int value) { + put( + ResName.qualifyResName(name, packageName, "integer"), + new TypedResource<>(value, ResType.INTEGER, null)); + } + + @Override + public int getColor(int id) { + return (int) get(id, ResType.COLOR); + } + + @Override + public int getColor(int id, @Nullable Theme theme) { + return (int) get(id, ResType.COLOR); + } + + public void putColor(String name, int value) { + put( + ResName.qualifyResName(name, packageName, "color"), + new TypedResource<>(value, ResType.COLOR, null)); + } + + @NonNull + @Override + public CharSequence getText(int id) { + return (CharSequence) get(id, ResType.CHAR_SEQUENCE); + } + + @NonNull + @Override + public String getString(int id) { + return get(id, ResType.CHAR_SEQUENCE).toString(); + } + + public void putText(String name, CharSequence value) { + put( + ResName.qualifyResName(name, packageName, "string"), + new TypedResource<>(value, ResType.CHAR_SEQUENCE, null)); + } + + private final Map<Integer, TypedResource<?>> overrideResources = new HashMap<>(); + private final Map<ResName, Integer> resourceIds = new HashMap<>(); + private int nextId = 1; + + private <T> void put(ResName resName, TypedResource<T> value) { + int id = nextId++; + overrideResources.put(id, value); + resourceIds.put(resName, id); + } + + private Object get(@AnyRes int id, ResType type) { + TypedResource<?> override = overrideResources.get(id); + if (override != null && override.getResType() == type) { + return override.getData(); + } + throw new NotFoundException(); + } + } + + private static PackageInfo createPackageInfo(String packageName) { + PackageInfo packageInfo = new PackageInfo(); + packageInfo.packageName = packageName; + return packageInfo; + } + + private ExternalResources() {} +} diff --git a/library/test/robotest/src/com/android/setupwizardlib/robolectric/SuwLibRobolectricTestRunner.java b/library/test/robotest/src/com/android/setupwizardlib/robolectric/SuwLibRobolectricTestRunner.java deleted file mode 100644 index 61baa23..0000000 --- a/library/test/robotest/src/com/android/setupwizardlib/robolectric/SuwLibRobolectricTestRunner.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.setupwizardlib.robolectric; - -import org.junit.runner.notification.RunNotifier; -import org.junit.runners.model.FrameworkMethod; -import org.junit.runners.model.InitializationError; -import org.robolectric.RobolectricTestRunner; - -public class SuwLibRobolectricTestRunner extends RobolectricTestRunner { - - public SuwLibRobolectricTestRunner(Class<?> testClass) throws InitializationError { - super(testClass); - } - - @Override - protected void runChild(FrameworkMethod method, RunNotifier notifier) { - System.out.println("===== Running " + method + " ====="); - super.runChild(method, notifier); - } -} diff --git a/library/test/robotest/src/com/android/setupwizardlib/shadow/ShadowLog.java b/library/test/robotest/src/com/android/setupwizardlib/shadow/ShadowLog.java deleted file mode 100644 index f1d37c8..0000000 --- a/library/test/robotest/src/com/android/setupwizardlib/shadow/ShadowLog.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.setupwizardlib.shadow; - -import android.util.Log; - -import org.robolectric.annotation.Implementation; -import org.robolectric.annotation.Implements; - -@Implements(Log.class) -public class ShadowLog extends org.robolectric.shadows.ShadowLog { - - public static boolean sWtfIsFatal = true; - - public static class TerribleFailure extends RuntimeException { - - public TerribleFailure(String msg, Throwable cause) { - super(msg, cause); - } - } - - @Implementation - public static void wtf(String tag, String msg) { - org.robolectric.shadows.ShadowLog.wtf(tag, msg); - if (sWtfIsFatal) { - throw new TerribleFailure(msg, null); - } - } - - @Implementation - public static void wtf(String tag, String msg, Throwable throwable) { - org.robolectric.shadows.ShadowLog.wtf(tag, msg, throwable); - if (sWtfIsFatal) { - throw new TerribleFailure(msg, throwable); - } - } -} diff --git a/library/test/robotest/src/com/android/setupwizardlib/span/LinkSpanTest.java b/library/test/robotest/src/com/android/setupwizardlib/span/LinkSpanTest.java index 3aafa7d..3bc9fd1 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/span/LinkSpanTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/span/LinkSpanTest.java @@ -17,8 +17,7 @@ package com.android.setupwizardlib.span; import static com.google.common.truth.Truth.assertThat; - -import static org.junit.Assert.assertSame; +import static com.google.common.truth.Truth.assertWithMessage; import static org.robolectric.RuntimeEnvironment.application; import android.content.Context; @@ -27,82 +26,83 @@ import android.text.Selection; import android.text.SpannableStringBuilder; import android.text.method.LinkMovementMethod; import android.widget.TextView; - -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; - import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; -@RunWith(SuwLibRobolectricTestRunner.class) +@RunWith(RobolectricTestRunner.class) public class LinkSpanTest { - @Test - public void onClick_shouldCallListenerOnContext() { - final TestContext context = new TestContext(application); - final TextView textView = new TextView(context); - final LinkSpan linkSpan = new LinkSpan("test_id"); - - linkSpan.onClick(textView); - - assertSame("Clicked LinkSpan should be passed to setup", linkSpan, context.clickedSpan); - } - - @Test - public void onClick_contextDoesNotImplementOnClickListener_shouldBeNoOp() { - final TextView textView = new TextView(application); - final LinkSpan linkSpan = new LinkSpan("test_id"); - - linkSpan.onClick(textView); - - // This would be no-op, because the context doesn't implement LinkSpan.OnClickListener. - // Just check that no uncaught exception here. + @Test + public void onClick_shouldCallListenerOnContext() { + final TestContext context = new TestContext(application); + final TextView textView = new TextView(context); + final LinkSpan linkSpan = new LinkSpan("test_id"); + + linkSpan.onClick(textView); + + assertWithMessage("Clicked LinkSpan should be passed to setup") + .that(context.clickedSpan) + .isSameAs(linkSpan); + } + + @Test + public void onClick_contextDoesNotImplementOnClickListener_shouldBeNoOp() { + final TextView textView = new TextView(application); + final LinkSpan linkSpan = new LinkSpan("test_id"); + + linkSpan.onClick(textView); + + // This would be no-op, because the context doesn't implement LinkSpan.OnClickListener. + // Just check that no uncaught exception here. + } + + @Test + public void onClick_contextWrapsOnClickListener_shouldCallWrappedListener() { + final TestContext context = new TestContext(application); + final Context wrapperContext = new ContextWrapper(context); + final TextView textView = new TextView(wrapperContext); + final LinkSpan linkSpan = new LinkSpan("test_id"); + + linkSpan.onClick(textView); + assertWithMessage("Clicked LinkSpan should be passed to setup") + .that(context.clickedSpan) + .isSameAs(linkSpan); + } + + @Test + public void onClick_shouldClearSelection() { + final TestContext context = new TestContext(application); + final TextView textView = new TextView(context); + textView.setMovementMethod(LinkMovementMethod.getInstance()); + textView.setFocusable(true); + textView.setFocusableInTouchMode(true); + final LinkSpan linkSpan = new LinkSpan("test_id"); + + SpannableStringBuilder text = new SpannableStringBuilder("Lorem ipsum dolor sit"); + textView.setText(text); + text.setSpan(linkSpan, /* start= */ 0, /* end= */ 5, /* flags= */ 0); + // Simulate the touch effect set by TextView when touched. + Selection.setSelection(text, /* start= */ 0, /* stop= */ 5); + + linkSpan.onClick(textView); + + assertThat(Selection.getSelectionStart(textView.getText())).isEqualTo(0); + assertThat(Selection.getSelectionEnd(textView.getText())).isEqualTo(0); + } + + @SuppressWarnings("deprecation") + private static class TestContext extends ContextWrapper implements LinkSpan.OnClickListener { + + public LinkSpan clickedSpan = null; + + TestContext(Context base) { + super(base); } - @Test - public void onClick_contextWrapsOnClickListener_shouldCallWrappedListener() { - final TestContext context = new TestContext(application); - final Context wrapperContext = new ContextWrapper(context); - final TextView textView = new TextView(wrapperContext); - final LinkSpan linkSpan = new LinkSpan("test_id"); - - - linkSpan.onClick(textView); - assertSame("Clicked LinkSpan should be passed to setup", linkSpan, context.clickedSpan); - } - - @Test - public void onClick_shouldClearSelection() { - final TestContext context = new TestContext(application); - final TextView textView = new TextView(context); - textView.setMovementMethod(LinkMovementMethod.getInstance()); - textView.setFocusable(true); - textView.setFocusableInTouchMode(true); - final LinkSpan linkSpan = new LinkSpan("test_id"); - - SpannableStringBuilder text = new SpannableStringBuilder("Lorem ipsum dolor sit"); - textView.setText(text); - text.setSpan(linkSpan, /* start= */ 0, /* end= */ 5, /* flags= */ 0); - // Simulate the touch effect set by TextView when touched. - Selection.setSelection(text, /* start= */ 0, /* end= */ 5); - - linkSpan.onClick(textView); - - assertThat(Selection.getSelectionStart(textView.getText())).isEqualTo(0); - assertThat(Selection.getSelectionEnd(textView.getText())).isEqualTo(0); - } - - @SuppressWarnings("deprecation") - private static class TestContext extends ContextWrapper implements LinkSpan.OnClickListener { - - public LinkSpan clickedSpan = null; - - TestContext(Context base) { - super(base); - } - - @Override - public void onClick(LinkSpan span) { - clickedSpan = span; - } + @Override + public void onClick(LinkSpan span) { + clickedSpan = span; } + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/template/ListViewScrollHandlingDelegateTest.java b/library/test/robotest/src/com/android/setupwizardlib/template/ListViewScrollHandlingDelegateTest.java index ec3622d..9b99a68 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/template/ListViewScrollHandlingDelegateTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/template/ListViewScrollHandlingDelegateTest.java @@ -16,105 +16,93 @@ package com.android.setupwizardlib.template; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; +import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.verify; import static org.robolectric.RuntimeEnvironment.application; import android.content.Context; import android.view.View; import android.view.ViewGroup; -import android.widget.AbsListView.OnScrollListener; import android.widget.BaseAdapter; import android.widget.ListView; - -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.Shadows; import org.robolectric.annotation.Config; -@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) -@RunWith(SuwLibRobolectricTestRunner.class) +@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) +@RunWith(RobolectricTestRunner.class) public class ListViewScrollHandlingDelegateTest { - @Mock - private RequireScrollMixin mRequireScrollMixin; + @Mock private RequireScrollMixin requireScrollMixin; - private ListView mListView; - private ListViewScrollHandlingDelegate mDelegate; - private ArgumentCaptor<OnScrollListener> mListenerCaptor; + private ListView listView; + private ListViewScrollHandlingDelegate delegate; - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); - mListView = spy(new TestListView(application)); - mDelegate = new ListViewScrollHandlingDelegate(mRequireScrollMixin, mListView); + listView = new TestListView(application); + delegate = new ListViewScrollHandlingDelegate(requireScrollMixin, listView); - mListenerCaptor = ArgumentCaptor.forClass(OnScrollListener.class); - doNothing().when(mListView).setOnScrollListener(mListenerCaptor.capture()); + listView.layout(0, 0, 50, 50); + } - mListView.layout(0, 0, 50, 50); - } + @Test + public void testRequireScroll() throws Throwable { + delegate.startListening(); - @Test - public void testRequireScroll() throws Throwable { - mDelegate.startListening(); + verify(requireScrollMixin).notifyScrollabilityChange(true); + } - verify(mRequireScrollMixin).notifyScrollabilityChange(true); - } + @Test + public void testScrolledToBottom() throws Throwable { + delegate.startListening(); - @Test - public void testScrolledToBottom() throws Throwable { - mDelegate.startListening(); + verify(requireScrollMixin).notifyScrollabilityChange(true); - verify(mRequireScrollMixin).notifyScrollabilityChange(true); + Shadows.shadowOf(listView).getOnScrollListener().onScroll(listView, 2, 20, 20); - doReturn(20).when(mListView).getLastVisiblePosition(); - mListenerCaptor.getValue().onScroll(mListView, 2, 20, 20); + verify(requireScrollMixin).notifyScrollabilityChange(false); + } - verify(mRequireScrollMixin).notifyScrollabilityChange(false); - } + @Test + public void testPageScrollDown() throws Throwable { + delegate.pageScrollDown(); + assertThat(Shadows.shadowOf(listView).getLastSmoothScrollByDistance()).isEqualTo(50); + } - @Test - public void testPageScrollDown() throws Throwable { - mDelegate.pageScrollDown(); - verify(mListView).smoothScrollBy(eq(50), anyInt()); - } + private static class TestListView extends ListView { + + TestListView(Context context) { + super(context); + setAdapter( + new BaseAdapter() { + @Override + public int getCount() { + return 20; + } + + @Override + public Object getItem(int position) { + return null; + } + + @Override + public long getItemId(int position) { + return position; + } - private static class TestListView extends ListView { - - TestListView(Context context) { - super(context); - setAdapter(new BaseAdapter() { - @Override - public int getCount() { - return 20; - } - - @Override - public Object getItem(int position) { - return null; - } - - @Override - public long getItemId(int position) { - return position; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - return new View(parent.getContext()); - } - }); - } + @Override + public View getView(int position, View convertView, ViewGroup parent) { + return new View(parent.getContext()); + } + }); } + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/template/RequireScrollMixinTest.java b/library/test/robotest/src/com/android/setupwizardlib/template/RequireScrollMixinTest.java index c641449..fe45d5f 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/template/RequireScrollMixinTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/template/RequireScrollMixinTest.java @@ -16,13 +16,10 @@ package com.android.setupwizardlib.template; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -33,135 +30,138 @@ import android.annotation.SuppressLint; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; - +import com.android.setupwizardlib.GlifLayout; import com.android.setupwizardlib.TemplateLayout; -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; import com.android.setupwizardlib.template.RequireScrollMixin.OnRequireScrollStateChangedListener; import com.android.setupwizardlib.template.RequireScrollMixin.ScrollHandlingDelegate; import com.android.setupwizardlib.view.NavigationBar; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) -@RunWith(SuwLibRobolectricTestRunner.class) +@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) +@RunWith(RobolectricTestRunner.class) public class RequireScrollMixinTest { - @Mock - private TemplateLayout mTemplateLayout; - - @Mock - private ScrollHandlingDelegate mDelegate; - - private RequireScrollMixin mRequireScrollMixin; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - - doReturn(application).when(mTemplateLayout).getContext(); - mRequireScrollMixin = new RequireScrollMixin(mTemplateLayout); - mRequireScrollMixin.setScrollHandlingDelegate(mDelegate); - } - - @Test - public void testRequireScroll() { - mRequireScrollMixin.requireScroll(); - - verify(mDelegate).startListening(); - } - - @Test - public void testScrollStateChangedListener() { - OnRequireScrollStateChangedListener listener = - mock(OnRequireScrollStateChangedListener.class); - mRequireScrollMixin.setOnRequireScrollStateChangedListener(listener); - assertFalse("Scrolling should not be required initially", - mRequireScrollMixin.isScrollingRequired()); - - mRequireScrollMixin.notifyScrollabilityChange(true); - verify(listener).onRequireScrollStateChanged(true); - assertTrue("Scrolling should be required when there is more content below the fold", - mRequireScrollMixin.isScrollingRequired()); - - mRequireScrollMixin.notifyScrollabilityChange(false); - verify(listener).onRequireScrollStateChanged(false); - assertFalse("Scrolling should not be required after scrolling to bottom", - mRequireScrollMixin.isScrollingRequired()); - - // Once the user has scrolled to the bottom, they should not be forced to scroll down again - mRequireScrollMixin.notifyScrollabilityChange(true); - verifyNoMoreInteractions(listener); - - assertFalse("Scrolling should not be required after scrolling to bottom once", - mRequireScrollMixin.isScrollingRequired()); - - assertSame(listener, mRequireScrollMixin.getOnRequireScrollStateChangedListener()); - } - - @Test - public void testCreateOnClickListener() { - OnClickListener wrappedListener = mock(OnClickListener.class); - final OnClickListener onClickListener = - mRequireScrollMixin.createOnClickListener(wrappedListener); - - mRequireScrollMixin.notifyScrollabilityChange(true); - onClickListener.onClick(null); - - verify(wrappedListener, never()).onClick(any(View.class)); - verify(mDelegate).pageScrollDown(); - - mRequireScrollMixin.notifyScrollabilityChange(false); - onClickListener.onClick(null); - - verify(wrappedListener).onClick(any(View.class)); - } - - @Test - public void testRequireScrollWithNavigationBar() { - final NavigationBar navigationBar = new NavigationBar(application); - mRequireScrollMixin.requireScrollWithNavigationBar(navigationBar); - - mRequireScrollMixin.notifyScrollabilityChange(true); - assertEquals("More button should be visible", - View.VISIBLE, navigationBar.getMoreButton().getVisibility()); - assertEquals("Next button should be hidden", - View.GONE, navigationBar.getNextButton().getVisibility()); - - navigationBar.getMoreButton().performClick(); - verify(mDelegate).pageScrollDown(); - - mRequireScrollMixin.notifyScrollabilityChange(false); - assertEquals("More button should be hidden", - View.GONE, navigationBar.getMoreButton().getVisibility()); - assertEquals("Next button should be visible", - View.VISIBLE, navigationBar.getNextButton().getVisibility()); - } - - @SuppressLint("SetTextI18n") // It's OK for testing - @Test - public void testRequireScrollWithButton() { - final Button button = new Button(application); - button.setText("OriginalLabel"); - OnClickListener wrappedListener = mock(OnClickListener.class); - mRequireScrollMixin.requireScrollWithButton( - button, "TestMoreLabel", wrappedListener); - - assertEquals("Button label should be kept initially", "OriginalLabel", button.getText()); - - mRequireScrollMixin.notifyScrollabilityChange(true); - assertEquals("TestMoreLabel", button.getText()); - button.performClick(); - verify(wrappedListener, never()).onClick(eq(button)); - verify(mDelegate).pageScrollDown(); - - mRequireScrollMixin.notifyScrollabilityChange(false); - assertEquals("OriginalLabel", button.getText()); - button.performClick(); - verify(wrappedListener).onClick(eq(button)); - } + @Mock private ScrollHandlingDelegate delegate; + + private RequireScrollMixin requireScrollMixin; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + TemplateLayout templateLayout = new GlifLayout(application); + requireScrollMixin = new RequireScrollMixin(templateLayout); + requireScrollMixin.setScrollHandlingDelegate(delegate); + } + + @Test + public void testRequireScroll() { + requireScrollMixin.requireScroll(); + + verify(delegate).startListening(); + } + + @Test + public void testScrollStateChangedListener() { + OnRequireScrollStateChangedListener listener = mock(OnRequireScrollStateChangedListener.class); + requireScrollMixin.setOnRequireScrollStateChangedListener(listener); + assertWithMessage("Scrolling should not be required initially") + .that(requireScrollMixin.isScrollingRequired()) + .isFalse(); + + requireScrollMixin.notifyScrollabilityChange(true); + verify(listener).onRequireScrollStateChanged(true); + assertWithMessage("Scrolling should be required when there is more content below the fold") + .that(requireScrollMixin.isScrollingRequired()) + .isTrue(); + + requireScrollMixin.notifyScrollabilityChange(false); + verify(listener).onRequireScrollStateChanged(false); + assertWithMessage("Scrolling should not be required after scrolling to bottom") + .that(requireScrollMixin.isScrollingRequired()) + .isFalse(); + + // Once the user has scrolled to the bottom, they should not be forced to scroll down again + requireScrollMixin.notifyScrollabilityChange(true); + verifyNoMoreInteractions(listener); + + assertWithMessage("Scrolling should not be required after scrolling to bottom once") + .that(requireScrollMixin.isScrollingRequired()) + .isFalse(); + + assertThat(requireScrollMixin.getOnRequireScrollStateChangedListener()).isSameAs(listener); + } + + @Test + public void testCreateOnClickListener() { + OnClickListener wrappedListener = mock(OnClickListener.class); + final OnClickListener onClickListener = + requireScrollMixin.createOnClickListener(wrappedListener); + + requireScrollMixin.notifyScrollabilityChange(true); + onClickListener.onClick(null); + + verify(wrappedListener, never()).onClick(any(View.class)); + verify(delegate).pageScrollDown(); + + requireScrollMixin.notifyScrollabilityChange(false); + onClickListener.onClick(null); + + verify(wrappedListener).onClick(any(View.class)); + } + + @Test + public void testRequireScrollWithNavigationBar() { + final NavigationBar navigationBar = new NavigationBar(application); + requireScrollMixin.requireScrollWithNavigationBar(navigationBar); + + requireScrollMixin.notifyScrollabilityChange(true); + assertWithMessage("More button should be visible") + .that(navigationBar.getMoreButton().getVisibility()) + .isEqualTo(View.VISIBLE); + assertWithMessage("Next button should be hidden") + .that(navigationBar.getNextButton().getVisibility()) + .isEqualTo(View.GONE); + + navigationBar.getMoreButton().performClick(); + verify(delegate).pageScrollDown(); + + requireScrollMixin.notifyScrollabilityChange(false); + assertWithMessage("More button should be hidden") + .that(navigationBar.getMoreButton().getVisibility()) + .isEqualTo(View.GONE); + assertWithMessage("Next button should be visible") + .that(navigationBar.getNextButton().getVisibility()) + .isEqualTo(View.VISIBLE); + } + + @SuppressLint("SetTextI18n") // It's OK for testing + @Test + public void testRequireScrollWithButton() { + final Button button = new Button(application); + button.setText("OriginalLabel"); + OnClickListener wrappedListener = mock(OnClickListener.class); + requireScrollMixin.requireScrollWithButton(button, "TestMoreLabel", wrappedListener); + + assertWithMessage("Button label should be kept initially") + .that(button.getText().toString()) + .isEqualTo("OriginalLabel"); + + requireScrollMixin.notifyScrollabilityChange(true); + assertThat(button.getText().toString()).isEqualTo("TestMoreLabel"); + button.performClick(); + verify(wrappedListener, never()).onClick(eq(button)); + verify(delegate).pageScrollDown(); + + requireScrollMixin.notifyScrollabilityChange(false); + assertThat(button.getText().toString()).isEqualTo("OriginalLabel"); + button.performClick(); + verify(wrappedListener).onClick(eq(button)); + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/template/ScrollViewScrollHandlingDelegateTest.java b/library/test/robotest/src/com/android/setupwizardlib/template/ScrollViewScrollHandlingDelegateTest.java index 429445c..b07e2a3 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/template/ScrollViewScrollHandlingDelegateTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/template/ScrollViewScrollHandlingDelegateTest.java @@ -16,72 +16,65 @@ package com.android.setupwizardlib.template; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.spy; +import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.verify; import static org.robolectric.RuntimeEnvironment.application; -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; +import android.view.View; import com.android.setupwizardlib.view.BottomScrollView; -import com.android.setupwizardlib.view.BottomScrollView.BottomScrollListener; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) -@RunWith(SuwLibRobolectricTestRunner.class) +@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) +@RunWith(RobolectricTestRunner.class) public class ScrollViewScrollHandlingDelegateTest { - @Mock - private RequireScrollMixin mRequireScrollMixin; - - private BottomScrollView mScrollView; - private ScrollViewScrollHandlingDelegate mDelegate; - private ArgumentCaptor<BottomScrollListener> mListenerCaptor; + @Mock private RequireScrollMixin requireScrollMixin; - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + private BottomScrollView scrollView; + private ScrollViewScrollHandlingDelegate delegate; - mScrollView = spy(new BottomScrollView(application)); - mDelegate = new ScrollViewScrollHandlingDelegate(mRequireScrollMixin, mScrollView); + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); - mListenerCaptor = ArgumentCaptor.forClass(BottomScrollListener.class); - doNothing().when(mScrollView).setBottomScrollListener(mListenerCaptor.capture()); + scrollView = new BottomScrollView(application); + View childView = new View(application); + scrollView.addView(childView); + delegate = new ScrollViewScrollHandlingDelegate(requireScrollMixin, scrollView); - mScrollView.layout(0, 0, 50, 50); - } + scrollView.layout(0, 0, 500, 500); + childView.layout(0, 0, 1000, 1000); + } - @Test - public void testRequireScroll() throws Throwable { - mDelegate.startListening(); + @Test + public void testRequireScroll() throws Throwable { + delegate.startListening(); - mListenerCaptor.getValue().onRequiresScroll(); - verify(mRequireScrollMixin).notifyScrollabilityChange(true); - } + scrollView.getBottomScrollListener().onRequiresScroll(); + verify(requireScrollMixin).notifyScrollabilityChange(true); + } - @Test - public void testScrolledToBottom() throws Throwable { - mDelegate.startListening(); + @Test + public void testScrolledToBottom() throws Throwable { + delegate.startListening(); - mListenerCaptor.getValue().onRequiresScroll(); - verify(mRequireScrollMixin).notifyScrollabilityChange(true); + scrollView.getBottomScrollListener().onRequiresScroll(); + verify(requireScrollMixin).notifyScrollabilityChange(true); - mListenerCaptor.getValue().onScrolledToBottom(); + scrollView.getBottomScrollListener().onScrolledToBottom(); - verify(mRequireScrollMixin).notifyScrollabilityChange(false); - } + verify(requireScrollMixin).notifyScrollabilityChange(false); + } - @Test - public void testPageScrollDown() throws Throwable { - mDelegate.pageScrollDown(); - verify(mScrollView).smoothScrollBy(anyInt(), eq(50)); - } + @Test + public void testPageScrollDown() throws Throwable { + delegate.pageScrollDown(); + assertThat(scrollView.getScrollY()).isEqualTo(500); + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/util/GlifDimensionTest.java b/library/test/robotest/src/com/android/setupwizardlib/util/GlifDimensionTest.java index c10c122..a0c688c 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/util/GlifDimensionTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/util/GlifDimensionTest.java @@ -16,7 +16,7 @@ package com.android.setupwizardlib.util; -import static org.junit.Assert.assertEquals; +import static com.google.common.truth.Truth.assertWithMessage; import static org.robolectric.RuntimeEnvironment.application; import android.content.Context; @@ -25,90 +25,88 @@ import android.content.res.TypedArray; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.ContextThemeWrapper; - import com.android.setupwizardlib.R; -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -@RunWith(SuwLibRobolectricTestRunner.class) +@RunWith(RobolectricTestRunner.class) @Config(sdk = Config.ALL_SDKS) public class GlifDimensionTest { - private Context mContext; - - @Before - public void setUp() { - mContext = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light); - } - - @Test - public void testDividerInsetPhone() { - assertDividerInset(); - } - - @Config(qualifiers = "sw600dp") - @Test - public void testDividerInsetSw600dp() { - assertDividerInset(); - } - - private void assertDividerInset() { - final Resources res = mContext.getResources(); - - final TypedArray a = mContext.obtainStyledAttributes(new int[]{R.attr.suwMarginSides}); - final int marginSides = a.getDimensionPixelSize(0, 0); - a.recycle(); - - assertEquals( - "Dimensions should satisfy constraint: " - + "?attr/suwMarginSides = suw_items_glif_text_divider_inset", - marginSides, - res.getDimensionPixelSize(R.dimen.suw_items_glif_text_divider_inset)); - - assertEquals( - "Dimensions should satisfy constraint: ?attr/suwMarginSides + " - + "suw_items_icon_container_width = suw_items_glif_icon_divider_inset", - marginSides + res.getDimensionPixelSize(R.dimen.suw_items_icon_container_width), - res.getDimensionPixelSize(R.dimen.suw_items_glif_icon_divider_inset)); - } - - @Test - public void testButtonMargin() { - assertButtonMargin(); - } - - @Config(qualifiers = "sw600dp") - @Test - public void testButtonMarginSw600dp() { - assertButtonMargin(); - } - - private void assertButtonMargin() { - final Resources res = mContext.getResources(); - - final TypedArray a = mContext.obtainStyledAttributes(new int[]{R.attr.suwMarginSides}); - final int marginSides = a.getDimensionPixelSize(0, 0); - a.recycle(); - - assertEquals( - "Dimensions should satisfy constraint: ?attr/suwMarginSides - " - + "4dp (internal padding of button) = suw_glif_button_margin_end", - marginSides - dp2Px(4), - res.getDimensionPixelSize(R.dimen.suw_glif_button_margin_end)); - - assertEquals( - "Dimensions should satisfy constraint: ?attr/suwMarginSides - " - + "suw_glif_button_padding = suw_glif_button_margin_start", - marginSides - res.getDimensionPixelSize(R.dimen.suw_glif_button_padding), - res.getDimensionPixelSize(R.dimen.suw_glif_button_margin_start)); - } - - private int dp2Px(float dp) { - DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); - return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics); - } + private Context context; + + @Before + public void setUp() { + context = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light); + } + + @Test + public void testDividerInsetPhone() { + assertDividerInset(); + } + + @Config(qualifiers = "sw600dp") + @Test + public void testDividerInsetSw600dp() { + assertDividerInset(); + } + + private void assertDividerInset() { + final Resources res = context.getResources(); + + final TypedArray a = context.obtainStyledAttributes(new int[] {R.attr.suwMarginSides}); + final int marginSides = a.getDimensionPixelSize(0, 0); + a.recycle(); + + assertWithMessage( + "Dimensions should satisfy constraint: " + + "?attr/suwMarginSides = suw_items_glif_text_divider_inset") + .that(res.getDimensionPixelSize(R.dimen.suw_items_glif_text_divider_inset)) + .isEqualTo(marginSides); + + assertWithMessage( + "Dimensions should satisfy constraint: ?attr/suwMarginSides + " + + "suw_items_icon_container_width = suw_items_glif_icon_divider_inset") + .that(res.getDimensionPixelSize(R.dimen.suw_items_glif_icon_divider_inset)) + .isEqualTo(marginSides + res.getDimensionPixelSize(R.dimen.suw_items_icon_container_width)); + } + + @Test + public void testButtonMargin() { + assertButtonMargin(); + } + + @Config(qualifiers = "sw600dp") + @Test + public void testButtonMarginSw600dp() { + assertButtonMargin(); + } + + private void assertButtonMargin() { + final Resources res = context.getResources(); + + final TypedArray a = context.obtainStyledAttributes(new int[] {R.attr.suwMarginSides}); + final int marginSides = a.getDimensionPixelSize(0, 0); + a.recycle(); + + assertWithMessage( + "Dimensions should satisfy constraint: ?attr/suwMarginSides - " + + "4dp (internal padding of button) = suw_glif_button_margin_end") + .that(res.getDimensionPixelSize(R.dimen.suw_glif_button_margin_end)) + .isEqualTo(marginSides - dp2Px(4)); + + assertWithMessage( + "Dimensions should satisfy constraint: ?attr/suwMarginSides - " + + "suw_glif_button_padding = suw_glif_button_margin_start") + .that(res.getDimensionPixelSize(R.dimen.suw_glif_button_margin_start)) + .isEqualTo(marginSides - res.getDimensionPixelSize(R.dimen.suw_glif_button_padding)); + } + + private int dp2Px(float dp) { + DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics); + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/util/GlifStyleTest.java b/library/test/robotest/src/com/android/setupwizardlib/util/GlifStyleTest.java index d8e318d..61b84bb 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/util/GlifStyleTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/util/GlifStyleTest.java @@ -17,9 +17,6 @@ package com.android.setupwizardlib.util; import static com.google.common.truth.Truth.assertThat; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import static org.robolectric.RuntimeEnvironment.application; import android.annotation.TargetApi; @@ -28,71 +25,82 @@ import android.content.Context; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import android.os.Bundle; +import androidx.annotation.Nullable; +import android.util.AttributeSet; import android.view.ContextThemeWrapper; +import android.view.View; import android.widget.Button; import android.widget.ProgressBar; - -import androidx.annotation.Nullable; - import com.android.setupwizardlib.R; -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; - -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; +import org.robolectric.util.ReflectionHelpers; +import org.robolectric.util.ReflectionHelpers.ClassParameter; -@RunWith(SuwLibRobolectricTestRunner.class) +@RunWith(RobolectricTestRunner.class) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) public class GlifStyleTest { - private Context mContext; - - @Before - public void setUp() { - mContext = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light); + @Test + public void testSuwGlifButtonTertiary() { + Button button = + createButton( + new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light), + Robolectric.buildAttributeSet() + .setStyleAttribute("@style/SuwGlifButton.Tertiary") + .build()); + assertThat(button.getBackground()).named("background").isNotNull(); + assertThat(button.getTransformationMethod()).named("transformation method").isNull(); + if (VERSION.SDK_INT < VERSION_CODES.M) { + // Robolectric resolved the wrong theme attribute on versions >= M + // https://github.com/robolectric/robolectric/issues/2940 + assertThat(Integer.toHexString(button.getTextColors().getDefaultColor())) + .isEqualTo("ff4285f4"); } - - @Test - public void testSuwGlifButtonTertiary() { - Button button = new Button( - mContext, - Robolectric.buildAttributeSet() - .setStyleAttribute("@style/SuwGlifButton.Tertiary") - .build()); - assertThat(button.getBackground()).named("background").isNotNull(); - assertThat(button.getTransformationMethod()).named("transformation method").isNull(); - if (VERSION.SDK_INT < VERSION_CODES.M) { - // Robolectric resolved the wrong theme attribute on versions >= M - // https://github.com/robolectric/robolectric/issues/2940 - assertEquals("ff4285f4", Integer.toHexString(button.getTextColors().getDefaultColor())); - } + } + + @TargetApi(VERSION_CODES.LOLLIPOP) + @Config(sdk = Config.NEWEST_SDK) + @Test + public void glifThemeLight_statusBarColorShouldBeTransparent() { + GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); + assertThat(activity.getWindow().getStatusBarColor()).isEqualTo(0x00000000); + } + + @Test + public void glifLoadingScreen_shouldHaveProgressBar() { + GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); + activity.setContentView(R.layout.suw_glif_loading_screen); + + assertThat((View) activity.findViewById(R.id.suw_large_progress_bar)) + .isInstanceOf(ProgressBar.class); + } + + private Button createButton(Context context, AttributeSet attrs) { + Class<? extends Button> buttonClass; + try { + // Use AppCompatButton in builds that have them (i.e. gingerbreadCompat) + // noinspection unchecked + buttonClass = + (Class<? extends Button>) Class.forName("androidx.appcompat.widget.AppCompatButton"); + } catch (ClassNotFoundException e) { + buttonClass = Button.class; } - - @TargetApi(VERSION_CODES.LOLLIPOP) - @Config(sdk = Config.NEWEST_SDK) - @Test - public void glifThemeLight_statusBarColorShouldBeTransparent() { - GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); - assertEquals(0x00000000, activity.getWindow().getStatusBarColor()); - } - - @Test - public void glifLoadingScreen_shouldHaveProgressBar() { - GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); - activity.setContentView(R.layout.suw_glif_loading_screen); - - assertTrue("Progress bar should exist", - activity.findViewById(R.id.suw_large_progress_bar) instanceof ProgressBar); - } - - private static class GlifThemeActivity extends Activity { - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - setTheme(R.style.SuwThemeGlif_Light); - super.onCreate(savedInstanceState); - } + return ReflectionHelpers.callConstructor( + buttonClass, + ClassParameter.from(Context.class, context), + ClassParameter.from(AttributeSet.class, attrs)); + } + + private static class GlifThemeActivity extends Activity { + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + setTheme(R.style.SuwThemeGlif_Light); + super.onCreate(savedInstanceState); } + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/util/GlifV3StyleTest.java b/library/test/robotest/src/com/android/setupwizardlib/util/GlifV3StyleTest.java index 44b8886..13e29f6 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/util/GlifV3StyleTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/util/GlifV3StyleTest.java @@ -18,68 +18,59 @@ package com.android.setupwizardlib.util; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - import android.app.Activity; import android.graphics.Color; import android.graphics.Typeface; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import android.os.Bundle; +import androidx.annotation.Nullable; import android.view.View; import android.widget.Button; - -import androidx.annotation.Nullable; - import com.android.setupwizardlib.R; -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; - import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -@RunWith(SuwLibRobolectricTestRunner.class) +@RunWith(RobolectricTestRunner.class) @Config(minSdk = Config.OLDEST_SDK, maxSdk = Config.NEWEST_SDK) public class GlifV3StyleTest { - @Test - public void activityWithGlifV3Theme_shouldUseLightNavBarOnV27OrAbove() { - GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); - if (VERSION.SDK_INT >= VERSION_CODES.O_MR1) { - assertEquals( - activity.getWindow().getNavigationBarColor(), - Color.WHITE); - int vis = activity.getWindow().getDecorView().getSystemUiVisibility(); - assertTrue((vis & View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR) != 0); - } else if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { - assertEquals( - activity.getWindow().getNavigationBarColor(), - Color.BLACK); - } - // Nav bar color is not customizable pre-L + @Test + public void activityWithGlifV3Theme_shouldUseLightNavBarOnV27OrAbove() { + GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); + if (VERSION.SDK_INT >= VERSION_CODES.O_MR1) { + assertThat(activity.getWindow().getNavigationBarColor()).isEqualTo(Color.WHITE); + int vis = activity.getWindow().getDecorView().getSystemUiVisibility(); + assertThat((vis & View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR) != 0).isTrue(); + } else if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + assertThat(activity.getWindow().getNavigationBarColor()).isEqualTo(Color.BLACK); } + // Nav bar color is not customizable pre-L + } - @Test - public void buttonWithGlifV3_shouldBeGoogleSans() { - GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); - Button button = new Button( - activity, - Robolectric.buildAttributeSet() - .setStyleAttribute("@style/SuwGlifButton.Primary") - .build()); - assertThat(button.getTypeface()).isEqualTo(Typeface.create("google-sans", 0)); - // Button should not be all caps - assertThat(button.getTransformationMethod()).isNull(); - } + @Test + public void buttonWithGlifV3_shouldBeGoogleSans() { + GlifThemeActivity activity = Robolectric.setupActivity(GlifThemeActivity.class); + Button button = + new Button( + activity, + Robolectric.buildAttributeSet() + .setStyleAttribute("@style/SuwGlifButton.Primary") + .build()); + assertThat(button.getTypeface()).isEqualTo(Typeface.create("google-sans", 0)); + // Button should not be all caps + assertThat(button.getTransformationMethod()).isNull(); + } - private static class GlifThemeActivity extends Activity { + private static class GlifThemeActivity extends Activity { - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - setTheme(R.style.SuwThemeGlifV3_Light); - super.onCreate(savedInstanceState); - } + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + setTheme(R.style.SuwThemeGlifV3_Light); + super.onCreate(savedInstanceState); } + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/util/PartnerTest.java b/library/test/robotest/src/com/android/setupwizardlib/util/PartnerTest.java index 2285cd5..f301e43 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/util/PartnerTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/util/PartnerTest.java @@ -17,211 +17,164 @@ package com.android.setupwizardlib.util; import static com.google.common.truth.Truth.assertThat; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; import static org.robolectric.RuntimeEnvironment.application; +import static org.robolectric.Shadows.shadowOf; -import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; -import android.content.res.Resources; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; - import com.android.setupwizardlib.R; -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; +import com.android.setupwizardlib.robolectric.ExternalResources; +import com.android.setupwizardlib.robolectric.ExternalResources.Resources; import com.android.setupwizardlib.util.Partner.ResourceEntry; -import com.android.setupwizardlib.util.PartnerTest.ShadowApplicationPackageManager; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.Shadows; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -import org.robolectric.annotation.Implementation; -import org.robolectric.annotation.Implements; -import org.robolectric.shadows.ShadowResources; -import java.util.Arrays; -import java.util.Collections; - -@RunWith(SuwLibRobolectricTestRunner.class) -@Config( - sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }, - shadows = ShadowApplicationPackageManager.class) +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) public class PartnerTest { - private static final String ACTION_PARTNER_CUSTOMIZATION = - "com.android.setupwizard.action.PARTNER_CUSTOMIZATION"; - - private Context mContext; - private Resources mPartnerResources; - - private ShadowApplicationPackageManager mPackageManager; - - @Before - public void setUp() throws Exception { - Partner.resetForTesting(); - - mContext = spy(application); - mPartnerResources = spy(ShadowResources.getSystem()); - - mPackageManager = - (ShadowApplicationPackageManager) Shadows.shadowOf(application.getPackageManager()); - mPackageManager.partnerResources = mPartnerResources; - } - - @Test - public void testLoadPartner() { - mPackageManager.addResolveInfoForIntent( - new Intent(ACTION_PARTNER_CUSTOMIZATION), - Arrays.asList( - createResolveInfo("foo.bar", false, true), - createResolveInfo("test.partner.package", true, true)) - ); - - Partner partner = Partner.get(mContext); - assertNotNull("Partner should not be null", partner); - } - - @Test - public void testLoadNoPartner() { - Partner partner = Partner.get(mContext); - assertNull("Partner should be null", partner); - } - - @Test - public void testLoadNonSystemPartner() { - mPackageManager.addResolveInfoForIntent( - new Intent(ACTION_PARTNER_CUSTOMIZATION), - Arrays.asList( - createResolveInfo("foo.bar", false, true), - createResolveInfo("test.partner.package", false, true)) - ); - - Partner partner = Partner.get(mContext); - assertNull("Partner should be null", partner); - } - - @Test - public void testLoadPartnerValue() { - doReturn(0x7f010000).when(mPartnerResources) - .getIdentifier(eq("suwTransitionDuration"), eq("integer"), anyString()); - doReturn(5000).when(mPartnerResources).getInteger(eq(0x7f010000)); - - mPackageManager.addResolveInfoForIntent( - new Intent(ACTION_PARTNER_CUSTOMIZATION), - Arrays.asList( - createResolveInfo("foo.bar", false, true), - createResolveInfo("test.partner.package", true, true)) - ); - - ResourceEntry entry = Partner.getResourceEntry(mContext, R.integer.suwTransitionDuration); - int partnerValue = entry.resources.getInteger(entry.id); - assertEquals("Partner value should be overlaid to 5000", 5000, partnerValue); - assertTrue("Partner value should come from overlay", entry.isOverlay); - } - - @Test - public void getColor_shouldReturnPartnerValueIfPresent() { - final int expectedPartnerColor = 1111; - doReturn(12345).when(mPartnerResources) - .getIdentifier(eq("suw_color_accent_dark"), eq("color"), anyString()); - doReturn(expectedPartnerColor).when(mPartnerResources).getColor(eq(12345)); - mPackageManager.addResolveInfoForIntent( - new Intent(ACTION_PARTNER_CUSTOMIZATION), - Arrays.asList(createResolveInfo("test.partner.package", true, true))); - final int foundColor = Partner.getColor(mContext, R.color.suw_color_accent_dark); - assertEquals("Partner color should be overlayed to: " + expectedPartnerColor, - expectedPartnerColor, foundColor); - } - - @Test - public void getText_shouldReturnPartnerValueIfPresent() { - final CharSequence expectedPartnerText = "partner"; - doReturn(12345).when(mPartnerResources) - .getIdentifier(eq("suw_next_button_label"), eq("string"), anyString()); - doReturn(expectedPartnerText).when(mPartnerResources).getText(eq(12345)); - mPackageManager.addResolveInfoForIntent( - new Intent(ACTION_PARTNER_CUSTOMIZATION), - Collections.singletonList(createResolveInfo("test.partner.package", true, true))); - final CharSequence partnerText = Partner.getText(mContext, R.string.suw_next_button_label); - assertThat(partnerText).isEqualTo(expectedPartnerText); + private static final String ACTION_PARTNER_CUSTOMIZATION = + "com.android.setupwizard.action.PARTNER_CUSTOMIZATION"; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + Partner.resetForTesting(); + } + + @Test + public void get_withPartnerPackage_shouldReturnNonNull() { + new PartnerPackageBuilder("foo.bar") + .setIsSystem(false) + .setDirectBootAware(true) + .injectResources(); + new PartnerPackageBuilder("test.partner.package").setDirectBootAware(true).injectResources(); + + Partner partner = Partner.get(application); + assertThat(partner).isNotNull(); + assertThat(partner.getPackageName()).isEqualTo("test.partner.package"); + } + + @Test + public void get_noPartnerPackage_shouldReturnNull() { + Partner partner = Partner.get(application); + assertThat(partner).isNull(); + } + + @Test + public void get_nonSystemPartnerPackage_shouldIgnoreAndReturnNull() { + new PartnerPackageBuilder("foo.bar") + .setIsSystem(false) + .setDirectBootAware(true) + .injectResources(); + new PartnerPackageBuilder("test.partner.package") + .setIsSystem(false) + .setDirectBootAware(true) + .injectResources(); + + Partner partner = Partner.get(application); + assertThat(partner).isNull(); + } + + @Test + public void getResourceEntry_hasOverlay_shouldReturnOverlayValue() { + new PartnerPackageBuilder("test.partner.package") + .injectResources() + .putInteger("suwTransitionDuration", 5000); + + ResourceEntry entry = Partner.getResourceEntry(application, R.integer.suwTransitionDuration); + int partnerValue = entry.resources.getInteger(entry.id); + assertThat(partnerValue).named("partner value").isEqualTo(5000); + assertThat(entry.isOverlay).isTrue(); + } + + @Test + public void getColor_partnerValuePresent_shouldReturnPartnerValue() { + new PartnerPackageBuilder("test.partner.package") + .injectResources() + .putColor("suw_color_accent_dark", 0xffff00ff); + + final int color = Partner.getColor(application, R.color.suw_color_accent_dark); + assertThat(color).isEqualTo(0xffff00ff); + } + + @Test + public void getText_partnerValuePresent_shouldReturnPartnerValue() { + new PartnerPackageBuilder("test.partner.package") + .injectResources() + .putText("suw_next_button_label", "partner"); + + final CharSequence partnerText = Partner.getText(application, R.string.suw_next_button_label); + assertThat(partnerText.toString()).isEqualTo("partner"); + } + + @Test + public void getResourceEntry_partnerValueNotPresent_shouldReturnDefault() { + new PartnerPackageBuilder("test.partner.package").injectResources(); + + ResourceEntry entry = Partner.getResourceEntry(application, R.color.suw_color_accent_dark); + int partnerValue = entry.resources.getColor(entry.id); + assertThat(partnerValue).isEqualTo(0xff448aff); + assertThat(entry.isOverlay).isFalse(); + } + + @Test + public void getResourceEntry_directBootUnawareNoValueDefined_shouldReturnDefaultValue() { + new PartnerPackageBuilder("test.partner.package").injectResources(); + + ResourceEntry entry = Partner.getResourceEntry(application, R.color.suw_color_accent_dark); + int partnerValue = entry.resources.getColor(entry.id); + assertThat(partnerValue).isEqualTo(0xff448aff); + assertThat(entry.isOverlay).isFalse(); + } + + private static class PartnerPackageBuilder { + private final String packageName; + private final ResolveInfo resolveInfo; + + PartnerPackageBuilder(String packageName) { + this.packageName = packageName; + + resolveInfo = new ResolveInfo(); + resolveInfo.resolvePackageName = packageName; + ActivityInfo activityInfo = new ActivityInfo(); + ApplicationInfo appInfo = new ApplicationInfo(); + appInfo.flags = ApplicationInfo.FLAG_SYSTEM; + appInfo.packageName = packageName; + activityInfo.applicationInfo = appInfo; + activityInfo.packageName = packageName; + activityInfo.name = packageName; + resolveInfo.activityInfo = activityInfo; } - @Test - public void testLoadDefaultValue() { - mPackageManager.addResolveInfoForIntent( - new Intent(ACTION_PARTNER_CUSTOMIZATION), - Arrays.asList( - createResolveInfo("foo.bar", false, true), - createResolveInfo("test.partner.package", true, true)) - ); - - ResourceEntry entry = Partner.getResourceEntry(mContext, R.color.suw_color_accent_dark); - int partnerValue = entry.resources.getColor(entry.id); - assertEquals("Partner value should default to 0xff448aff", 0xff448aff, partnerValue); - assertFalse("Partner value should come from fallback", entry.isOverlay); - } - - @Test - public void testNotDirectBootAware() { - mPackageManager.addResolveInfoForIntent( - new Intent(ACTION_PARTNER_CUSTOMIZATION), - Collections.singletonList(createResolveInfo("test.partner.package", true, false))); - - ResourceEntry entry = Partner.getResourceEntry(mContext, R.color.suw_color_accent_dark); - int partnerValue = entry.resources.getColor(entry.id); - assertEquals("Partner value should default to 0xff448aff", 0xff448aff, partnerValue); - assertFalse("Partner value should come from fallback", entry.isOverlay); + PartnerPackageBuilder setIsSystem(boolean isSystem) { + if (isSystem) { + resolveInfo.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; + } else { + resolveInfo.activityInfo.applicationInfo.flags &= ~ApplicationInfo.FLAG_SYSTEM; + } + return this; } - private ResolveInfo createResolveInfo( - String packageName, - boolean isSystem, - boolean directBootAware) { - ResolveInfo info = new ResolveInfo(); - info.resolvePackageName = packageName; - ActivityInfo activityInfo = new ActivityInfo(); - ApplicationInfo appInfo = new ApplicationInfo(); - appInfo.flags = isSystem ? ApplicationInfo.FLAG_SYSTEM : 0; - appInfo.packageName = packageName; - activityInfo.applicationInfo = appInfo; - activityInfo.packageName = packageName; - activityInfo.name = packageName; - if (VERSION.SDK_INT >= VERSION_CODES.N) { - activityInfo.directBootAware = directBootAware; - } - info.activityInfo = activityInfo; - return info; + PartnerPackageBuilder setDirectBootAware(boolean directBootAware) { + if (VERSION.SDK_INT >= VERSION_CODES.N) { + resolveInfo.activityInfo.directBootAware = directBootAware; + } + return this; } - @Implements(className = "android.app.ApplicationPackageManager") - public static class ShadowApplicationPackageManager extends - org.robolectric.shadows.ShadowApplicationPackageManager { - - public Resources partnerResources; - - @Implementation - @Override - public Resources getResourcesForApplication(ApplicationInfo app) - throws NameNotFoundException { - if (app != null && "test.partner.package".equals(app.packageName)) { - return partnerResources; - } else { - return super.getResourcesForApplication(app); - } - } + Resources injectResources() { + shadowOf(application.getPackageManager()) + .addResolveInfoForIntent(new Intent(ACTION_PARTNER_CUSTOMIZATION), resolveInfo); + return ExternalResources.injectExternalResources(packageName); } + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/util/ThemeResolverTest.java b/library/test/robotest/src/com/android/setupwizardlib/util/ThemeResolverTest.java new file mode 100644 index 0000000..6489961 --- /dev/null +++ b/library/test/robotest/src/com/android/setupwizardlib/util/ThemeResolverTest.java @@ -0,0 +1,216 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.setupwizardlib.util; + +import static com.google.common.truth.Truth.assertThat; +import static org.robolectric.Shadows.shadowOf; + +import android.app.Activity; +import android.content.Intent; +import androidx.annotation.StyleRes; +import com.android.setupwizardlib.R; +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +@RunWith(RobolectricTestRunner.class) +@Config(sdk = Config.NEWEST_SDK) +public class ThemeResolverTest { + + @After + public void resetDefaultThemeResolver() { + ThemeResolver.setDefault(null); + } + + @Test + public void resolve_nonDayNight_shouldReturnCorrespondingTheme() { + @StyleRes int defaultTheme = 12345; + ThemeResolver themeResolver = + new ThemeResolver.Builder().setDefaultTheme(defaultTheme).setUseDayNight(false).build(); + assertThat(themeResolver.resolve("material")).isEqualTo(R.style.SuwThemeMaterial); + assertThat(themeResolver.resolve("material_light")).isEqualTo(R.style.SuwThemeMaterial_Light); + assertThat(themeResolver.resolve("glif")).isEqualTo(R.style.SuwThemeGlif); + assertThat(themeResolver.resolve("glif_light")).isEqualTo(R.style.SuwThemeGlif_Light); + assertThat(themeResolver.resolve("glif_v2")).isEqualTo(R.style.SuwThemeGlifV2); + assertThat(themeResolver.resolve("glif_v2_light")).isEqualTo(R.style.SuwThemeGlifV2_Light); + assertThat(themeResolver.resolve("glif_v3")).isEqualTo(R.style.SuwThemeGlifV3); + assertThat(themeResolver.resolve("glif_v3_light")).isEqualTo(R.style.SuwThemeGlifV3_Light); + assertThat(themeResolver.resolve("unknown_theme")).isEqualTo(defaultTheme); + } + + @Test + public void resolve_dayNight_shouldReturnDayNightTheme() { + @StyleRes int defaultTheme = 12345; + ThemeResolver themeResolver = new ThemeResolver.Builder().setDefaultTheme(defaultTheme).build(); + assertThat(themeResolver.resolve("material")).isEqualTo(R.style.SuwThemeMaterial_DayNight); + assertThat(themeResolver.resolve("material_light")) + .isEqualTo(R.style.SuwThemeMaterial_DayNight); + assertThat(themeResolver.resolve("glif")).isEqualTo(R.style.SuwThemeGlif_DayNight); + assertThat(themeResolver.resolve("glif_light")).isEqualTo(R.style.SuwThemeGlif_DayNight); + assertThat(themeResolver.resolve("glif_v2")).isEqualTo(R.style.SuwThemeGlifV2_DayNight); + assertThat(themeResolver.resolve("glif_v2_light")).isEqualTo(R.style.SuwThemeGlifV2_DayNight); + assertThat(themeResolver.resolve("glif_v3")).isEqualTo(R.style.SuwThemeGlifV3_DayNight); + assertThat(themeResolver.resolve("glif_v3_light")).isEqualTo(R.style.SuwThemeGlifV3_DayNight); + assertThat(themeResolver.resolve("unknown_theme")).isEqualTo(defaultTheme); + } + + @Test + public void resolve_newerThanOldestSupportedTheme_shouldReturnSpecifiedTheme() { + ThemeResolver themeResolver = + new ThemeResolver.Builder() + .setOldestSupportedTheme(WizardManagerHelper.THEME_GLIF_V2) + .build(); + assertThat(themeResolver.resolve("glif_v2")).isEqualTo(R.style.SuwThemeGlifV2_DayNight); + assertThat(themeResolver.resolve("glif_v2_light")).isEqualTo(R.style.SuwThemeGlifV2_DayNight); + assertThat(themeResolver.resolve("glif_v3")).isEqualTo(R.style.SuwThemeGlifV3_DayNight); + assertThat(themeResolver.resolve("glif_v3_light")).isEqualTo(R.style.SuwThemeGlifV3_DayNight); + } + + @Test + public void resolve_olderThanOldestSupportedTheme_shouldReturnDefault() { + @StyleRes int defaultTheme = 12345; + ThemeResolver themeResolver = + new ThemeResolver.Builder() + .setDefaultTheme(defaultTheme) + .setOldestSupportedTheme(WizardManagerHelper.THEME_GLIF_V2) + .build(); + assertThat(themeResolver.resolve("material")).isEqualTo(defaultTheme); + assertThat(themeResolver.resolve("material_light")).isEqualTo(defaultTheme); + assertThat(themeResolver.resolve("glif")).isEqualTo(defaultTheme); + assertThat(themeResolver.resolve("glif_light")).isEqualTo(defaultTheme); + } + + @Test + public void resolve_intentTheme_shouldReturnCorrespondingTheme() { + @StyleRes int defaultTheme = 12345; + ThemeResolver themeResolver = + new ThemeResolver.Builder().setDefaultTheme(defaultTheme).setUseDayNight(false).build(); + assertThat( + themeResolver.resolve( + new Intent().putExtra(WizardManagerHelper.EXTRA_THEME, "material"))) + .isEqualTo(R.style.SuwThemeMaterial); + assertThat( + themeResolver.resolve( + new Intent().putExtra(WizardManagerHelper.EXTRA_THEME, "material_light"))) + .isEqualTo(R.style.SuwThemeMaterial_Light); + assertThat( + themeResolver.resolve(new Intent().putExtra(WizardManagerHelper.EXTRA_THEME, "glif"))) + .isEqualTo(R.style.SuwThemeGlif); + assertThat( + themeResolver.resolve( + new Intent().putExtra(WizardManagerHelper.EXTRA_THEME, "glif_light"))) + .isEqualTo(R.style.SuwThemeGlif_Light); + assertThat( + themeResolver.resolve( + new Intent().putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v2"))) + .isEqualTo(R.style.SuwThemeGlifV2); + assertThat( + themeResolver.resolve( + new Intent().putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v2_light"))) + .isEqualTo(R.style.SuwThemeGlifV2_Light); + assertThat( + themeResolver.resolve( + new Intent().putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v3"))) + .isEqualTo(R.style.SuwThemeGlifV3); + assertThat( + themeResolver.resolve( + new Intent().putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v3_light"))) + .isEqualTo(R.style.SuwThemeGlifV3_Light); + assertThat( + themeResolver.resolve( + new Intent().putExtra(WizardManagerHelper.EXTRA_THEME, "unknown_theme"))) + .isEqualTo(defaultTheme); + } + + @Test + public void resolve_suwIntent_shouldForceNonDayNightTheme() { + @StyleRes int defaultTheme = 12345; + ThemeResolver themeResolver = + new ThemeResolver.Builder().setDefaultTheme(defaultTheme).setUseDayNight(true).build(); + Intent originalIntent = new Intent().putExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, true); + assertThat( + themeResolver.resolve( + new Intent(originalIntent).putExtra(WizardManagerHelper.EXTRA_THEME, "material"))) + .isEqualTo(R.style.SuwThemeMaterial); + assertThat( + themeResolver.resolve( + new Intent(originalIntent) + .putExtra(WizardManagerHelper.EXTRA_THEME, "material_light"))) + .isEqualTo(R.style.SuwThemeMaterial_Light); + assertThat( + themeResolver.resolve( + new Intent(originalIntent).putExtra(WizardManagerHelper.EXTRA_THEME, "glif"))) + .isEqualTo(R.style.SuwThemeGlif); + assertThat( + themeResolver.resolve( + new Intent(originalIntent).putExtra(WizardManagerHelper.EXTRA_THEME, "glif_light"))) + .isEqualTo(R.style.SuwThemeGlif_Light); + assertThat( + themeResolver.resolve( + new Intent(originalIntent).putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v2"))) + .isEqualTo(R.style.SuwThemeGlifV2); + assertThat( + themeResolver.resolve( + new Intent(originalIntent) + .putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v2_light"))) + .isEqualTo(R.style.SuwThemeGlifV2_Light); + assertThat( + themeResolver.resolve( + new Intent(originalIntent).putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v3"))) + .isEqualTo(R.style.SuwThemeGlifV3); + assertThat( + themeResolver.resolve( + new Intent(originalIntent) + .putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v3_light"))) + .isEqualTo(R.style.SuwThemeGlifV3_Light); + assertThat( + themeResolver.resolve( + new Intent(originalIntent) + .putExtra(WizardManagerHelper.EXTRA_THEME, "unknown_theme"))) + .isEqualTo(defaultTheme); + } + + @Test + public void applyTheme_glifV3_shouldSetActivityThemeToGlifV3() { + @StyleRes int defaultTheme = 12345; + ThemeResolver themeResolver = + new ThemeResolver.Builder().setUseDayNight(false).setDefaultTheme(defaultTheme).build(); + + Activity activity = + Robolectric.buildActivity( + Activity.class, + new Intent() + .putExtra(WizardManagerHelper.EXTRA_THEME, WizardManagerHelper.THEME_GLIF_V3)) + .setup() + .get(); + + themeResolver.applyTheme(activity); + + assertThat(shadowOf(activity).callGetThemeResId()).isEqualTo(R.style.SuwThemeGlifV3); + } + + @Test + public void setDefault_shouldSetDefaultResolver() { + ThemeResolver themeResolver = new ThemeResolver.Builder().setUseDayNight(false).build(); + + ThemeResolver.setDefault(themeResolver); + assertThat(ThemeResolver.getDefault()).isSameAs(themeResolver); + } +} diff --git a/library/test/robotest/src/com/android/setupwizardlib/util/WizardManagerHelperTest.java b/library/test/robotest/src/com/android/setupwizardlib/util/WizardManagerHelperTest.java index 616ccdd..e302cab 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/util/WizardManagerHelperTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/util/WizardManagerHelperTest.java @@ -16,10 +16,10 @@ package com.android.setupwizardlib.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static org.robolectric.RuntimeEnvironment.application; +import static org.robolectric.Shadows.shadowOf; import android.annotation.TargetApi; import android.app.Activity; @@ -29,299 +29,343 @@ import android.os.Bundle; import android.provider.Settings; import android.provider.Settings.Global; import android.provider.Settings.Secure; - import androidx.annotation.StyleRes; - import com.android.setupwizardlib.R; -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; -@RunWith(SuwLibRobolectricTestRunner.class) +@RunWith(RobolectricTestRunner.class) @Config(sdk = Config.NEWEST_SDK) public class WizardManagerHelperTest { - @Test - public void testGetNextIntent() { - final Intent intent = new Intent("test.intent.ACTION"); - intent.putExtra("scriptUri", "android-resource://test-script"); - intent.putExtra("actionId", "test_action_id"); - intent.putExtra("theme", "test_theme"); - intent.putExtra("ignoreExtra", "poof"); // extra is ignored because it's not known - - final Intent data = new Intent(); - data.putExtra("extraData", "shazam"); - - final Intent nextIntent = - WizardManagerHelper.getNextIntent(intent, Activity.RESULT_OK, data); - assertEquals("Next intent action should be NEXT", "com.android.wizard.NEXT", - nextIntent.getAction()); - assertEquals("Script URI should be the same as original intent", - "android-resource://test-script", nextIntent.getStringExtra("scriptUri")); - assertEquals("Action ID should be the same as original intent", "test_action_id", - nextIntent.getStringExtra("actionId")); - assertEquals("Theme extra should be the same as original intent", "test_theme", - nextIntent.getStringExtra("theme")); - assertFalse("ignoreExtra should not be in nextIntent", nextIntent.hasExtra("ignoreExtra")); - assertEquals("Result code extra should be RESULT_OK", Activity.RESULT_OK, - nextIntent.getIntExtra("com.android.setupwizard.ResultCode", 0)); - assertEquals("Extra data should surface as extra in nextIntent", "shazam", - nextIntent.getStringExtra("extraData")); - } - - @Test - public void testIsSetupWizardTrue() { - final Intent intent = new Intent(); - intent.putExtra("firstRun", true); - assertTrue("Is setup wizard should be true", - WizardManagerHelper.isSetupWizardIntent(intent)); - } - - @Test - public void testIsDeferredSetupTrue() { - final Intent intent = new Intent(); - intent.putExtra("deferredSetup", true); - assertTrue("Is deferred setup wizard should be true", - WizardManagerHelper.isDeferredSetupWizard(intent)); - } - - @Test - public void testIsPreDeferredSetupTrue() { - final Intent intent = new Intent(); - intent.putExtra("preDeferredSetup", true); - assertTrue("Is pre-deferred setup wizard should be true", - WizardManagerHelper.isPreDeferredSetupWizard(intent)); - } - - @Test - public void testIsSetupWizardFalse() { - final Intent intent = new Intent(); - intent.putExtra("firstRun", false); - assertFalse("Is setup wizard should be true", - WizardManagerHelper.isSetupWizardIntent(intent)); - } - - @Test - public void isLightTheme_shouldReturnTrue_whenThemeIsLight() { - List<String> lightThemes = Arrays.asList( - "holo_light", - "material_light", - "glif_light", - "glif_v2_light", - "glif_v3_light" - ); - ArrayList<String> unexpectedIntentThemes = new ArrayList<>(); - ArrayList<String> unexpectedStringThemes = new ArrayList<>(); - for (final String theme : lightThemes) { - Intent intent = new Intent(); - intent.putExtra(WizardManagerHelper.EXTRA_THEME, theme); - if (!WizardManagerHelper.isLightTheme(intent, false)) { - unexpectedIntentThemes.add(theme); - } - if (!WizardManagerHelper.isLightTheme(theme, false)) { - unexpectedStringThemes.add(theme); - } - } - assertTrue("Intent themes " + unexpectedIntentThemes + " should be light", - unexpectedIntentThemes.isEmpty()); - assertTrue("String themes " + unexpectedStringThemes + " should be light", - unexpectedStringThemes.isEmpty()); - } - - @Test - public void isLightTheme_shouldReturnFalse_whenThemeIsNotLight() { - List<String> lightThemes = Arrays.asList( - "holo", - "material", - "glif", - "glif_v2", - "glif_v3" - ); - ArrayList<String> unexpectedIntentThemes = new ArrayList<>(); - ArrayList<String> unexpectedStringThemes = new ArrayList<>(); - for (final String theme : lightThemes) { - Intent intent = new Intent(); - intent.putExtra(WizardManagerHelper.EXTRA_THEME, theme); - if (WizardManagerHelper.isLightTheme(intent, true)) { - unexpectedIntentThemes.add(theme); - } - if (WizardManagerHelper.isLightTheme(theme, true)) { - unexpectedStringThemes.add(theme); - } - } - assertTrue("Intent themes " + unexpectedIntentThemes + " should not be light", - unexpectedIntentThemes.isEmpty()); - assertTrue("String themes " + unexpectedStringThemes + " should not be light", - unexpectedStringThemes.isEmpty()); - } - - @Test - public void testIsLightThemeDefault() { - final Intent intent = new Intent(); - intent.putExtra("theme", "abracadabra"); - assertTrue("isLightTheme should return default value true", - WizardManagerHelper.isLightTheme(intent, true)); - assertFalse("isLightTheme should return default value false", - WizardManagerHelper.isLightTheme(intent, false)); - } - - @Test - public void testIsLightThemeUnspecified() { - final Intent intent = new Intent(); - assertTrue("isLightTheme should return default value true", - WizardManagerHelper.isLightTheme(intent, true)); - assertFalse("isLightTheme should return default value false", - WizardManagerHelper.isLightTheme(intent, false)); - } - - @Test - public void testGetThemeResGlifV3Light() { - assertEquals(R.style.SuwThemeGlifV3_Light, - WizardManagerHelper.getThemeRes("glif_v3_light", 0)); - } - - @Test - public void testGetThemeResGlifV3() { - assertEquals(R.style.SuwThemeGlifV3, - WizardManagerHelper.getThemeRes("glif_v3", 0)); + @Test + public void testGetNextIntent() { + final Intent intent = new Intent("test.intent.ACTION"); + intent.putExtra("scriptUri", "android-resource://test-script"); + intent.putExtra("actionId", "test_action_id"); + intent.putExtra("theme", "test_theme"); + intent.putExtra("ignoreExtra", "poof"); // extra is ignored because it's not known + + final Intent data = new Intent(); + data.putExtra("extraData", "shazam"); + + final Intent nextIntent = WizardManagerHelper.getNextIntent(intent, Activity.RESULT_OK, data); + assertWithMessage("Next intent action should be NEXT") + .that(nextIntent.getAction()) + .isEqualTo("com.android.wizard.NEXT"); + assertWithMessage("Script URI should be the same as original intent") + .that(nextIntent.getStringExtra("scriptUri")) + .isEqualTo("android-resource://test-script"); + assertWithMessage("Action ID should be the same as original intent") + .that(nextIntent.getStringExtra("actionId")) + .isEqualTo("test_action_id"); + assertWithMessage("Theme extra should be the same as original intent") + .that(nextIntent.getStringExtra("theme")) + .isEqualTo("test_theme"); + assertWithMessage("ignoreExtra should not be in nextIntent") + .that(nextIntent.hasExtra("ignoreExtra")) + .isFalse(); + assertWithMessage("Result code extra should be RESULT_OK") + .that(nextIntent.getIntExtra("com.android.setupwizard.ResultCode", 0)) + .isEqualTo(Activity.RESULT_OK); + assertWithMessage("Extra data should surface as extra in nextIntent") + .that(nextIntent.getStringExtra("extraData")) + .isEqualTo("shazam"); + } + + @Test + public void testIsSetupWizardTrue() { + final Intent intent = new Intent(); + intent.putExtra("firstRun", true); + assertWithMessage("Is setup wizard should be true") + .that(WizardManagerHelper.isSetupWizardIntent(intent)) + .isTrue(); + } + + @Test + public void testIsDeferredSetupTrue() { + final Intent intent = new Intent(); + intent.putExtra("deferredSetup", true); + assertWithMessage("Is deferred setup wizard should be true") + .that(WizardManagerHelper.isDeferredSetupWizard(intent)) + .isTrue(); + } + + @Test + public void testIsPreDeferredSetupTrue() { + final Intent intent = new Intent(); + intent.putExtra("preDeferredSetup", true); + assertWithMessage("Is pre-deferred setup wizard should be true") + .that(WizardManagerHelper.isPreDeferredSetupWizard(intent)) + .isTrue(); + } + + @Test + public void testIsSetupWizardFalse() { + final Intent intent = new Intent(); + intent.putExtra("firstRun", false); + assertWithMessage("Is setup wizard should be true") + .that(WizardManagerHelper.isSetupWizardIntent(intent)) + .isFalse(); + } + + @Test + public void isLightTheme_shouldReturnTrue_whenThemeIsLight() { + List<String> lightThemes = + Arrays.asList( + "holo_light", "material_light", "glif_light", "glif_v2_light", "glif_v3_light"); + ArrayList<String> unexpectedIntentThemes = new ArrayList<>(); + ArrayList<String> unexpectedStringThemes = new ArrayList<>(); + for (final String theme : lightThemes) { + Intent intent = new Intent(); + intent.putExtra(WizardManagerHelper.EXTRA_THEME, theme); + if (!WizardManagerHelper.isLightTheme(intent, false)) { + unexpectedIntentThemes.add(theme); + } + if (!WizardManagerHelper.isLightTheme(theme, false)) { + unexpectedStringThemes.add(theme); + } } - - @Test - public void testGetThemeResGlifV2Light() { - assertEquals(R.style.SuwThemeGlifV2_Light, - WizardManagerHelper.getThemeRes("glif_v2_light", 0)); - } - - @Test - public void testGetThemeResGlifV2() { - assertEquals(R.style.SuwThemeGlifV2, - WizardManagerHelper.getThemeRes("glif_v2", 0)); - } - - @Test - public void testGetThemeResGlifLight() { - assertEquals(R.style.SuwThemeGlif_Light, - WizardManagerHelper.getThemeRes("glif_light", 0)); - } - - @Test - public void testGetThemeResGlif() { - assertEquals(R.style.SuwThemeGlif, - WizardManagerHelper.getThemeRes("glif", 0)); - } - - @Test - public void testGetThemeResMaterialLight() { - assertEquals(R.style.SuwThemeMaterial_Light, - WizardManagerHelper.getThemeRes("material_light", 0)); - } - - @Test - public void testGetThemeResMaterial() { - assertEquals(R.style.SuwThemeMaterial, - WizardManagerHelper.getThemeRes("material", 0)); - } - - @Test - public void testGetThemeResDefault() { - @StyleRes int def = 123; - assertEquals(def, WizardManagerHelper.getThemeRes("abracadabra", def)); - } - - @Test - public void testGetThemeResNull() { - @StyleRes int def = 123; - assertEquals(def, WizardManagerHelper.getThemeRes((String) null, def)); - } - - @Test - public void testGetThemeResFromIntent() { - Intent intent = new Intent(); - intent.putExtra(WizardManagerHelper.EXTRA_THEME, "material"); - assertEquals(R.style.SuwThemeMaterial, WizardManagerHelper.getThemeRes(intent, 0)); - } - - @Test - public void testCopyWizardManagerIntent() { - Bundle wizardBundle = new Bundle(); - wizardBundle.putString("foo", "bar"); - Intent originalIntent = new Intent() - .putExtra(WizardManagerHelper.EXTRA_THEME, "test_theme") - .putExtra(WizardManagerHelper.EXTRA_WIZARD_BUNDLE, wizardBundle) - .putExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, true) - .putExtra(WizardManagerHelper.EXTRA_IS_DEFERRED_SETUP, true) - .putExtra(WizardManagerHelper.EXTRA_IS_PRE_DEFERRED_SETUP, true) - // Script URI and Action ID are kept for backwards compatibility - .putExtra(WizardManagerHelper.EXTRA_SCRIPT_URI, "test_script_uri") - .putExtra(WizardManagerHelper.EXTRA_ACTION_ID, "test_action_id"); - - Intent intent = new Intent("test.intent.action"); - WizardManagerHelper.copyWizardManagerExtras(originalIntent, intent); - - assertEquals("Intent action should be kept", "test.intent.action", intent.getAction()); - assertEquals("EXTRA_THEME should be copied", - "test_theme", intent.getStringExtra(WizardManagerHelper.EXTRA_THEME)); - Bundle copiedWizardBundle = - intent.getParcelableExtra(WizardManagerHelper.EXTRA_WIZARD_BUNDLE); - assertEquals("Wizard bundle should be copied", "bar", copiedWizardBundle.getString("foo")); - - assertTrue("EXTRA_IS_FIRST_RUN should be copied", - intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, false)); - assertTrue("EXTRA_IS_DEFERRED_SETUP should be copied", - intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_DEFERRED_SETUP, false)); - assertTrue("EXTRA_IS_PRE_DEFERRED_SETUP should be copied", - intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_PRE_DEFERRED_SETUP, false)); - - // Script URI and Action ID are replaced by Wizard Bundle in M, but are kept for backwards - // compatibility - assertEquals("EXTRA_SCRIPT_URI should be copied", - "test_script_uri", intent.getStringExtra(WizardManagerHelper.EXTRA_SCRIPT_URI)); - assertEquals("EXTRA_ACTION_ID should be copied", - "test_action_id", intent.getStringExtra(WizardManagerHelper.EXTRA_ACTION_ID)); - } - - @TargetApi(VERSION_CODES.JELLY_BEAN_MR1) - @Test - public void testIsUserSetupComplete() { - Settings.Secure.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 1); - Settings.Secure.putInt(application.getContentResolver(), "user_setup_complete", 1); - assertTrue(WizardManagerHelper.isUserSetupComplete(application)); - - Settings.Secure.putInt(application.getContentResolver(), "user_setup_complete", 0); - assertFalse(WizardManagerHelper.isUserSetupComplete(application)); - } - - @Test - @Config(sdk = VERSION_CODES.JELLY_BEAN) - public void testIsUserSetupCompleteCompat() { - Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 1); - assertTrue(WizardManagerHelper.isUserSetupComplete(application)); - - Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 0); - assertFalse(WizardManagerHelper.isUserSetupComplete(application)); - } - - @TargetApi(VERSION_CODES.JELLY_BEAN_MR1) - @Test - public void testIsDeviceProvisioned() { - Settings.Secure.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 1); - assertTrue(WizardManagerHelper.isDeviceProvisioned(application)); - Settings.Secure.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 0); - assertFalse(WizardManagerHelper.isDeviceProvisioned(application)); - } - - @Test - @Config(sdk = VERSION_CODES.JELLY_BEAN) - public void testIsDeviceProvisionedCompat() { - Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 1); - assertTrue(WizardManagerHelper.isDeviceProvisioned(application)); - Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 0); - assertFalse(WizardManagerHelper.isDeviceProvisioned(application)); + assertWithMessage("Intent themes " + unexpectedIntentThemes + " should be light") + .that(unexpectedIntentThemes.isEmpty()) + .isTrue(); + assertWithMessage("String themes " + unexpectedStringThemes + " should be light") + .that(unexpectedStringThemes.isEmpty()) + .isTrue(); + } + + @Test + public void isLightTheme_shouldReturnFalse_whenThemeIsNotLight() { + List<String> lightThemes = Arrays.asList("holo", "material", "glif", "glif_v2", "glif_v3"); + ArrayList<String> unexpectedIntentThemes = new ArrayList<>(); + ArrayList<String> unexpectedStringThemes = new ArrayList<>(); + for (final String theme : lightThemes) { + Intent intent = new Intent(); + intent.putExtra(WizardManagerHelper.EXTRA_THEME, theme); + if (WizardManagerHelper.isLightTheme(intent, true)) { + unexpectedIntentThemes.add(theme); + } + if (WizardManagerHelper.isLightTheme(theme, true)) { + unexpectedStringThemes.add(theme); + } } + assertWithMessage("Intent themes " + unexpectedIntentThemes + " should not be light") + .that(unexpectedIntentThemes.isEmpty()) + .isTrue(); + assertWithMessage("String themes " + unexpectedStringThemes + " should not be light") + .that(unexpectedStringThemes.isEmpty()) + .isTrue(); + } + + @Test + public void getThemeRes_whenOldestSupportedThemeTakeEffect_shouldReturnDefault() { + Intent intent = new Intent(); + intent.putExtra(WizardManagerHelper.EXTRA_THEME, "material"); + assertThat(WizardManagerHelper.getThemeRes(intent, 0, WizardManagerHelper.THEME_GLIF_V2)) + .isEqualTo(0); + } + + @Test + public void getThemeRes_whenOldestSupportedThemeNotTakeEffect_shouldReturnCurrent() { + Intent intent = new Intent(); + intent.putExtra(WizardManagerHelper.EXTRA_THEME, "glif_v3"); + assertThat(WizardManagerHelper.getThemeRes(intent, 0, WizardManagerHelper.THEME_GLIF_V2)) + .isEqualTo(R.style.SuwThemeGlifV3); + } + + @Test + public void testIsLightThemeDefault() { + final Intent intent = new Intent(); + intent.putExtra("theme", "abracadabra"); + assertWithMessage("isLightTheme should return default value true") + .that(WizardManagerHelper.isLightTheme(intent, true)) + .isTrue(); + assertWithMessage("isLightTheme should return default value false") + .that(WizardManagerHelper.isLightTheme(intent, false)) + .isFalse(); + } + + @Test + public void testIsLightThemeUnspecified() { + final Intent intent = new Intent(); + assertWithMessage("isLightTheme should return default value true") + .that(WizardManagerHelper.isLightTheme(intent, true)) + .isTrue(); + assertWithMessage("isLightTheme should return default value false") + .that(WizardManagerHelper.isLightTheme(intent, false)) + .isFalse(); + } + + @Test + public void testGetThemeResGlifV3Light() { + assertThat(WizardManagerHelper.getThemeRes("glif_v3_light", 0)) + .isEqualTo(R.style.SuwThemeGlifV3_Light); + } + + @Test + public void testGetThemeResGlifV3() { + assertThat(WizardManagerHelper.getThemeRes("glif_v3", 0)).isEqualTo(R.style.SuwThemeGlifV3); + } + + @Test + public void testGetThemeResGlifV2Light() { + assertThat(WizardManagerHelper.getThemeRes("glif_v2_light", 0)) + .isEqualTo(R.style.SuwThemeGlifV2_Light); + } + + @Test + public void testGetThemeResGlifV2() { + assertThat(WizardManagerHelper.getThemeRes("glif_v2", 0)).isEqualTo(R.style.SuwThemeGlifV2); + } + + @Test + public void testGetThemeResGlifLight() { + assertThat(WizardManagerHelper.getThemeRes("glif_light", 0)) + .isEqualTo(R.style.SuwThemeGlif_Light); + } + + @Test + public void testGetThemeResGlif() { + assertThat(WizardManagerHelper.getThemeRes("glif", 0)).isEqualTo(R.style.SuwThemeGlif); + } + + @Test + public void testGetThemeResMaterialLight() { + assertThat(WizardManagerHelper.getThemeRes("material_light", 0)) + .isEqualTo(R.style.SuwThemeMaterial_Light); + } + + @Test + public void testGetThemeResMaterial() { + assertThat(WizardManagerHelper.getThemeRes("material", 0)).isEqualTo(R.style.SuwThemeMaterial); + } + + @Test + public void testGetThemeResDefault() { + @StyleRes int def = 123; + assertThat(WizardManagerHelper.getThemeRes("abracadabra", def)).isEqualTo(def); + } + + @Test + public void testGetThemeResNull() { + @StyleRes int def = 123; + assertThat(WizardManagerHelper.getThemeRes((String) null, def)).isEqualTo(def); + } + + @Test + public void testGetThemeResFromIntent() { + Intent intent = new Intent(); + intent.putExtra(WizardManagerHelper.EXTRA_THEME, "material"); + assertThat(WizardManagerHelper.getThemeRes(intent, 0)).isEqualTo(R.style.SuwThemeMaterial); + } + + @Test + public void testCopyWizardManagerIntent() { + Bundle wizardBundle = new Bundle(); + wizardBundle.putString("foo", "bar"); + Intent originalIntent = + new Intent() + .putExtra(WizardManagerHelper.EXTRA_THEME, "test_theme") + .putExtra(WizardManagerHelper.EXTRA_WIZARD_BUNDLE, wizardBundle) + .putExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, true) + .putExtra(WizardManagerHelper.EXTRA_IS_DEFERRED_SETUP, true) + .putExtra(WizardManagerHelper.EXTRA_IS_PRE_DEFERRED_SETUP, true) + // Script URI and Action ID are kept for backwards compatibility + .putExtra(WizardManagerHelper.EXTRA_SCRIPT_URI, "test_script_uri") + .putExtra(WizardManagerHelper.EXTRA_ACTION_ID, "test_action_id"); + + Intent intent = new Intent("test.intent.action"); + WizardManagerHelper.copyWizardManagerExtras(originalIntent, intent); + + assertWithMessage("Intent action should be kept") + .that(intent.getAction()) + .isEqualTo("test.intent.action"); + assertWithMessage("EXTRA_THEME should be copied") + .that(intent.getStringExtra(WizardManagerHelper.EXTRA_THEME)) + .isEqualTo("test_theme"); + Bundle copiedWizardBundle = intent.getParcelableExtra(WizardManagerHelper.EXTRA_WIZARD_BUNDLE); + assertWithMessage("Wizard bundle should be copied") + .that(copiedWizardBundle.getString("foo")) + .isEqualTo("bar"); + + assertWithMessage("EXTRA_IS_FIRST_RUN should be copied") + .that(intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, false)) + .isTrue(); + assertWithMessage("EXTRA_IS_DEFERRED_SETUP should be copied") + .that(intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_DEFERRED_SETUP, false)) + .isTrue(); + assertWithMessage("EXTRA_IS_PRE_DEFERRED_SETUP should be copied") + .that(intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_PRE_DEFERRED_SETUP, false)) + .isTrue(); + + // Script URI and Action ID are replaced by Wizard Bundle in M, but are kept for backwards + // compatibility + assertWithMessage("EXTRA_SCRIPT_URI should be copied") + .that(intent.getStringExtra(WizardManagerHelper.EXTRA_SCRIPT_URI)) + .isEqualTo("test_script_uri"); + assertWithMessage("EXTRA_ACTION_ID should be copied") + .that(intent.getStringExtra(WizardManagerHelper.EXTRA_ACTION_ID)) + .isEqualTo("test_action_id"); + } + + @TargetApi(VERSION_CODES.JELLY_BEAN_MR1) + @Test + public void testIsUserSetupComplete() { + Settings.Global.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 1); + Settings.Secure.putInt(application.getContentResolver(), "user_setup_complete", 1); + assertThat(WizardManagerHelper.isUserSetupComplete(application)).isTrue(); + + Settings.Secure.putInt(application.getContentResolver(), "user_setup_complete", 0); + assertThat(WizardManagerHelper.isUserSetupComplete(application)).isFalse(); + } + + @Test + @Config(sdk = VERSION_CODES.JELLY_BEAN) + public void testIsUserSetupCompleteCompat() { + Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 1); + assertThat(WizardManagerHelper.isUserSetupComplete(application)).isTrue(); + + Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 0); + assertThat(WizardManagerHelper.isUserSetupComplete(application)).isFalse(); + } + + @TargetApi(VERSION_CODES.JELLY_BEAN_MR1) + @Test + public void testIsDeviceProvisioned() { + Settings.Global.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 1); + assertThat(WizardManagerHelper.isDeviceProvisioned(application)).isTrue(); + Settings.Global.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 0); + assertThat(WizardManagerHelper.isDeviceProvisioned(application)).isFalse(); + } + + @Test + @Config(sdk = VERSION_CODES.JELLY_BEAN) + public void testIsDeviceProvisionedCompat() { + Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 1); + assertThat(WizardManagerHelper.isDeviceProvisioned(application)).isTrue(); + Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 0); + assertThat(WizardManagerHelper.isDeviceProvisioned(application)).isFalse(); + } + + @Test + public void applyTheme_glifDayNight_shouldApplyThemeToActivity() { + Activity activity = + Robolectric.buildActivity( + Activity.class, + new Intent() + .putExtra( + WizardManagerHelper.EXTRA_THEME, WizardManagerHelper.THEME_GLIF_LIGHT)) + .setup() + .get(); + + WizardManagerHelper.applyTheme(activity); + + assertThat(shadowOf(activity).callGetThemeResId()).isEqualTo(R.style.SuwThemeGlif_DayNight); + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/view/FillContentLayoutTest.java b/library/test/robotest/src/com/android/setupwizardlib/view/FillContentLayoutTest.java index ae4f3d1..001f1da 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/view/FillContentLayoutTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/view/FillContentLayoutTest.java @@ -16,72 +16,73 @@ package com.android.setupwizardlib.view; -import static org.junit.Assert.assertEquals; +import static com.google.common.truth.Truth.assertThat; import static org.robolectric.RuntimeEnvironment.application; import android.view.View; import android.view.View.MeasureSpec; - -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; - import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -@RunWith(SuwLibRobolectricTestRunner.class) +@RunWith(RobolectricTestRunner.class) @Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) public class FillContentLayoutTest { - @Test - public void testMeasureMinSize() { - FillContentLayout layout = new FillContentLayout( - application, - Robolectric.buildAttributeSet() - .addAttribute(android.R.attr.minWidth, "123dp") - .addAttribute(android.R.attr.minHeight, "123dp") - .build()); - layout.measure( - MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), - MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); + @Test + public void testMeasureMinSize() { + FillContentLayout layout = + new FillContentLayout( + application, + Robolectric.buildAttributeSet() + .addAttribute(android.R.attr.minWidth, "123dp") + .addAttribute(android.R.attr.minHeight, "123dp") + .build()); + layout.measure( + MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), + MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); - assertEquals(123, layout.getMeasuredWidth()); - assertEquals(123, layout.getMeasuredHeight()); - } + assertThat(layout.getMeasuredWidth()).isEqualTo(123); + assertThat(layout.getMeasuredHeight()).isEqualTo(123); + } - @Test - public void testMeasureChildIsSmallerThanMaxSize() { - View child = new View(application); - FillContentLayout layout = new FillContentLayout( - application, - Robolectric.buildAttributeSet() - .addAttribute(android.R.attr.maxWidth, "123dp") - .addAttribute(android.R.attr.maxHeight, "123dp") - .build()); - layout.addView(child); - layout.measure( - MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY)); + @Test + public void testMeasureChildIsSmallerThanMaxSize() { + View child = new View(application); + FillContentLayout layout = + new FillContentLayout( + application, + Robolectric.buildAttributeSet() + .addAttribute(android.R.attr.maxWidth, "123dp") + .addAttribute(android.R.attr.maxHeight, "123dp") + .build()); + layout.addView(child); + layout.measure( + MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(300, MeasureSpec.EXACTLY)); - assertEquals(123, child.getMeasuredWidth()); - assertEquals(123, child.getMeasuredHeight()); - } + assertThat(child.getMeasuredWidth()).isEqualTo(123); + assertThat(child.getMeasuredHeight()).isEqualTo(123); + } - @Test - public void testMeasureChildIsSmallerThanParent() { - View child = new View(application); - FillContentLayout layout = new FillContentLayout( - application, - Robolectric.buildAttributeSet() - .addAttribute(android.R.attr.maxWidth, "123dp") - .addAttribute(android.R.attr.maxHeight, "123dp") - .build()); - layout.addView(child); - layout.measure( - MeasureSpec.makeMeasureSpec(88, MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(88, MeasureSpec.EXACTLY)); + @Test + public void testMeasureChildIsSmallerThanParent() { + View child = new View(application); + FillContentLayout layout = + new FillContentLayout( + application, + Robolectric.buildAttributeSet() + .addAttribute(android.R.attr.maxWidth, "123dp") + .addAttribute(android.R.attr.maxHeight, "123dp") + .build()); + layout.addView(child); + layout.measure( + MeasureSpec.makeMeasureSpec(88, MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(88, MeasureSpec.EXACTLY)); - assertEquals(88, child.getMeasuredWidth()); - assertEquals(88, child.getMeasuredHeight()); - } + assertThat(child.getMeasuredWidth()).isEqualTo(88); + assertThat(child.getMeasuredHeight()).isEqualTo(88); + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/view/IllustrationVideoViewTest.java b/library/test/robotest/src/com/android/setupwizardlib/view/IllustrationVideoViewTest.java index 0e0e99c..e980563 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/view/IllustrationVideoViewTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/view/IllustrationVideoViewTest.java @@ -17,204 +17,223 @@ package com.android.setupwizardlib.view; import static com.google.common.truth.Truth.assertThat; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.verify; import static org.robolectric.RuntimeEnvironment.application; -import android.annotation.TargetApi; -import android.content.Context; +import android.app.Activity; import android.graphics.SurfaceTexture; -import android.media.MediaPlayer; -import android.os.Build.VERSION_CODES; -import android.view.Surface; -import android.view.View; - +import android.net.Uri; import androidx.annotation.RawRes; - +import android.view.View; import com.android.setupwizardlib.R; -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; -import com.android.setupwizardlib.shadow.ShadowLog; -import com.android.setupwizardlib.shadow.ShadowLog.TerribleFailure; -import com.android.setupwizardlib.view.IllustrationVideoViewTest.ShadowMockMediaPlayer; -import com.android.setupwizardlib.view.IllustrationVideoViewTest.ShadowSurface; - -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.Shadows; import org.robolectric.annotation.Config; -import org.robolectric.annotation.Implementation; -import org.robolectric.annotation.Implements; -import org.robolectric.annotation.RealObject; -import org.robolectric.shadow.api.Shadow; import org.robolectric.shadows.ShadowMediaPlayer; +import org.robolectric.shadows.ShadowMediaPlayer.InvalidStateBehavior; +import org.robolectric.shadows.ShadowMediaPlayer.MediaInfo; +import org.robolectric.shadows.util.DataSource; import org.robolectric.util.ReflectionHelpers; +import org.robolectric.util.ReflectionHelpers.ClassParameter; -@RunWith(SuwLibRobolectricTestRunner.class) -@Config( - sdk = Config.NEWEST_SDK, - shadows = { - ShadowLog.class, - ShadowMockMediaPlayer.class, - ShadowSurface.class - }) +@RunWith(RobolectricTestRunner.class) +@Config(sdk = Config.NEWEST_SDK) public class IllustrationVideoViewTest { - @Mock - private SurfaceTexture mSurfaceTexture; - - private IllustrationVideoView mView; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - } - - @After - public void tearDown() { - ShadowMockMediaPlayer.reset(); - } - - @Test - public void nullMediaPlayer_shouldThrowWtf() { - ShadowMockMediaPlayer.sMediaPlayer = null; - try { - createDefaultView(); - fail("WTF should be thrown for null media player"); - } catch (TerribleFailure e) { - // pass - } - } - - @Test - public void onVisibilityChanged_notVisible_shouldRelease() { - createDefaultView(); - mView.onWindowVisibilityChanged(View.GONE); - - verify(ShadowMockMediaPlayer.sMediaPlayer).release(); - assertThat(mView.mSurface).isNull(); - assertThat(mView.mMediaPlayer).isNull(); - } - - @Test - public void onVisibilityChanged_visible_shouldPlay() { - createDefaultView(); - - mView.onWindowVisibilityChanged(View.GONE); - assertThat(mView.mSurface).isNull(); - assertThat(mView.mMediaPlayer).isNull(); - - mView.onWindowVisibilityChanged(View.VISIBLE); - - assertThat(mView.mSurface).isNotNull(); - assertThat(mView.mMediaPlayer).isNotNull(); - } - - @Test - public void testPausedWhenWindowFocusLost() { - createDefaultView(); - mView.start(); - - assertNotNull(mView.mMediaPlayer); - assertNotNull(mView.mSurface); - - mView.onWindowFocusChanged(false); - verify(ShadowMockMediaPlayer.getMock()).pause(); - } - - @Test - public void testStartedWhenWindowFocusRegained() { - testPausedWhenWindowFocusLost(); - - // Clear verifications for calls in the other test - reset(ShadowMockMediaPlayer.getMock()); - - mView.onWindowFocusChanged(true); - verify(ShadowMockMediaPlayer.getMock()).start(); - } - - @Test - public void testSurfaceReleasedWhenTextureDestroyed() { - createDefaultView(); - mView.start(); - - assertNotNull(mView.mMediaPlayer); - assertNotNull(mView.mSurface); - - mView.onSurfaceTextureDestroyed(mSurfaceTexture); - verify(ShadowMockMediaPlayer.getMock()).release(); - } - - @Test - public void testXmlSetVideoResId() { - createDefaultView(); - assertEquals(android.R.color.white, ShadowMockMediaPlayer.sResId); - } - - @Test - public void testSetVideoResId() { - createDefaultView(); - - @RawRes int black = android.R.color.black; - mView.setVideoResource(black); - - assertEquals(android.R.color.black, ShadowMockMediaPlayer.sResId); - } - - private void createDefaultView() { - mView = new IllustrationVideoView( - application, - Robolectric.buildAttributeSet() - // Any resource attribute should work, since the media player is mocked - .addAttribute(R.attr.suwVideo, "@android:color/white") - .build()); - mView.setSurfaceTexture(mock(SurfaceTexture.class)); - mView.onSurfaceTextureAvailable(mSurfaceTexture, 500, 500); - } - - @Implements(MediaPlayer.class) - public static class ShadowMockMediaPlayer extends ShadowMediaPlayer { - - private static MediaPlayer sMediaPlayer = mock(MediaPlayer.class); - private static int sResId; - - public static void reset() { - sMediaPlayer = mock(MediaPlayer.class); - sResId = 0; - } - - @Implementation - public static MediaPlayer create(Context context, int resId) { - sResId = resId; - return sMediaPlayer; - } - - public static MediaPlayer getMock() { - return sMediaPlayer; - } - } - - @Implements(Surface.class) - @TargetApi(VERSION_CODES.HONEYCOMB) - public static class ShadowSurface extends org.robolectric.shadows.ShadowSurface { - - @RealObject - private Surface mRealSurface; - - public void __constructor__(SurfaceTexture surfaceTexture) { - // Call the constructor on the real object, so that critical fields such as mLock is - // initialized properly. - Shadow.invokeConstructor(Surface.class, mRealSurface, - ReflectionHelpers.ClassParameter.from(SurfaceTexture.class, surfaceTexture)); - super.__constructor__(surfaceTexture); - } - } + @Mock private SurfaceTexture surfaceTexture; + + private IllustrationVideoView view; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + ShadowMediaPlayer.addMediaInfo( + DataSource.toDataSource( + "android.resource://" + application.getPackageName() + "/" + android.R.color.white), + new ShadowMediaPlayer.MediaInfo(100, 10)); + ShadowMediaPlayer.addMediaInfo( + DataSource.toDataSource( + "android.resource://" + application.getPackageName() + "/" + android.R.color.black), + new ShadowMediaPlayer.MediaInfo(100, 10)); + } + + @Test + public void testPausedWhenWindowFocusLost() { + createDefaultView(); + Robolectric.flushForegroundThreadScheduler(); + view.start(); + + assertThat(view.mMediaPlayer).isNotNull(); + assertThat(view.surface).isNotNull(); + + view.onWindowFocusChanged(false); + assertThat(getShadowMediaPlayer().getState()).isEqualTo(ShadowMediaPlayer.State.PAUSED); + } + + @Test + public void testStartedWhenWindowFocusRegained() { + testPausedWhenWindowFocusLost(); + Robolectric.flushForegroundThreadScheduler(); + + view.onWindowFocusChanged(true); + assertThat(getShadowMediaPlayer().getState()).isEqualTo(ShadowMediaPlayer.State.STARTED); + } + + @Test + public void testSurfaceReleasedWhenTextureDestroyed() { + createDefaultView(); + view.start(); + + assertThat(view.mMediaPlayer).isNotNull(); + assertThat(view.surface).isNotNull(); + + // MediaPlayer is set to null after destroy. Retrieve it first before we call destroy. + ShadowMediaPlayer shadowMediaPlayer = getShadowMediaPlayer(); + view.onSurfaceTextureDestroyed(surfaceTexture); + assertThat(shadowMediaPlayer.getState()).isEqualTo(ShadowMediaPlayer.State.END); + } + + @Test + public void testXmlSetVideoResId() { + createDefaultView(); + assertThat(getShadowMediaPlayer().getSourceUri().toString()) + .isEqualTo("android.resource://com.android.setupwizardlib/" + android.R.color.white); + } + + @Test + public void testSetVideoResId() { + createDefaultView(); + + @RawRes int black = android.R.color.black; + view.setVideoResource(black); + + assertThat(getShadowMediaPlayer().getSourceUri().toString()) + .isEqualTo("android.resource://com.android.setupwizardlib/" + android.R.color.black); + } + + @Test + public void prepareVideo_shouldSetAspectRatio() { + createDefaultView(); + + ReflectionHelpers.setField(getShadowMediaPlayer(), "videoWidth", 720); + ReflectionHelpers.setField(getShadowMediaPlayer(), "videoHeight", 1280); + + Robolectric.flushForegroundThreadScheduler(); + view.start(); + + view.measure( + View.MeasureSpec.makeMeasureSpec(720, View.MeasureSpec.EXACTLY), + View.MeasureSpec.makeMeasureSpec(720, View.MeasureSpec.EXACTLY)); + + final float aspectRatio = (float) view.getMeasuredHeight() / view.getMeasuredWidth(); + assertThat(aspectRatio).isWithin(0.001f).of(1280f / 720f); + } + + @Test + public void prepareVideo_zeroHeight_shouldSetAspectRatioToZero() { + createDefaultView(); + + ReflectionHelpers.setField(getShadowMediaPlayer(), "videoWidth", 720); + ReflectionHelpers.setField(getShadowMediaPlayer(), "videoHeight", 0); + + Robolectric.flushForegroundThreadScheduler(); + view.start(); + + final float aspectRatio = (float) view.getHeight() / view.getWidth(); + assertThat(aspectRatio).isEqualTo(0.0f); + } + + @Test + public void setVideoResId_resetDiffVideoResFromDiffPackage_videoResShouldBeSet() { + // VideoRes default set as android.R.color.white with + // default package(com.android.setupwizardlib) + createDefaultView(); + + // reset different videoRes from different package + String newPackageName = "com.android.fakepackage"; + @RawRes int black = android.R.color.black; + addMediaInfo(black, newPackageName); + view.setVideoResource(black, newPackageName); + + // should be reset to black with the new package + assertThat(getShadowMediaPlayer().getSourceUri().toString()) + .isEqualTo("android.resource://" + newPackageName + "/" + android.R.color.black); + } + + @Test + public void setVideoResId_resetDiffVideoResFromSamePackage_videoResShouldBeSet() { + // VideoRes default set as android.R.color.white with + // default package(com.android.setupwizardlib) + createDefaultView(); + + // reset different videoRes from the same package(default package) + String defaultPackageName = "com.android.setupwizardlib"; + @RawRes int black = android.R.color.black; + addMediaInfo(black, defaultPackageName); + view.setVideoResource(black, defaultPackageName); + + // should be reset to black with the same package(default package) + assertThat(getShadowMediaPlayer().getSourceUri().toString()) + .isEqualTo("android.resource://" + defaultPackageName + "/" + android.R.color.black); + } + + @Test + public void setVideoResId_resetSameVideoResFromDifferentPackage_videoResShouldBeSet() { + // VideoRes default set as android.R.color.white with + // default package(com.android.setupwizardlib) + createDefaultView(); + + // reset same videoRes from different package + @RawRes int white = android.R.color.white; + String newPackageName = "com.android.fakepackage"; + addMediaInfo(white, newPackageName); + view.setVideoResource(white, newPackageName); + + // should be white with the new package + assertThat(getShadowMediaPlayer().getSourceUri().toString()) + .isEqualTo("android.resource://" + newPackageName + "/" + android.R.color.white); + } + + private ShadowMediaPlayer getShadowMediaPlayer() { + return Shadows.shadowOf(view.mMediaPlayer); + } + + private void createDefaultView() { + view = + new IllustrationVideoView( + application, + Robolectric.buildAttributeSet() + // Any resource attribute should work, since the data source is fake + .addAttribute(R.attr.suwVideo, "@android:color/white") + .build()); + + Activity activity = Robolectric.setupActivity(Activity.class); + activity.setContentView(view); + setWindowVisible(); + + view.setSurfaceTexture(mock(SurfaceTexture.class)); + view.onSurfaceTextureAvailable(surfaceTexture, 500, 500); + getShadowMediaPlayer().setInvalidStateBehavior(InvalidStateBehavior.EMULATE); + } + + private void setWindowVisible() { + Object viewRootImpl = ReflectionHelpers.callInstanceMethod(view, "getViewRootImpl"); + ReflectionHelpers.callInstanceMethod( + viewRootImpl, "handleAppVisibility", ClassParameter.from(boolean.class, true)); + assertThat(view.isAttachedToWindow()).isTrue(); + assertThat(view.getWindowVisibility()).isEqualTo(View.VISIBLE); + } + + private void addMediaInfo(@RawRes int res, String packageName) { + ShadowMediaPlayer.addMediaInfo( + DataSource.toDataSource( + application, Uri.parse("android.resource://" + packageName + "/" + res), null), + new MediaInfo(5000, 1)); + } } diff --git a/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java b/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java index f77de68..477c42a 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/view/RichTextViewTest.java @@ -17,15 +17,10 @@ package com.android.setupwizardlib.view; import static com.google.common.truth.Truth.assertThat; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static com.google.common.truth.Truth.assertWithMessage; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.robolectric.RuntimeEnvironment.application; @@ -39,201 +34,196 @@ import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.TextAppearanceSpan; import android.view.MotionEvent; - -import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner; import com.android.setupwizardlib.span.LinkSpan; import com.android.setupwizardlib.span.LinkSpan.OnLinkClickListener; import com.android.setupwizardlib.view.TouchableMovementMethod.TouchableLinkMovementMethod; - import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -import java.util.Arrays; - -@RunWith(SuwLibRobolectricTestRunner.class) -@Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK }) +@RunWith(RobolectricTestRunner.class) +@Config(sdk = {Config.OLDEST_SDK, Config.NEWEST_SDK}) public class RichTextViewTest { - @Test - public void testLinkAnnotation() { - Annotation link = new Annotation("link", "foobar"); - SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); - ssb.setSpan(link, 1, 2, 0 /* flags */); - - RichTextView textView = new RichTextView(application); - textView.setText(ssb); - - final CharSequence text = textView.getText(); - assertTrue("Text should be spanned", text instanceof Spanned); - - assertThat(textView.getMovementMethod()).isInstanceOf(TouchableLinkMovementMethod.class); - - Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class); - assertEquals("Annotation should be removed " + Arrays.toString(spans), 0, spans.length); - - spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class); - assertEquals("There should be one span " + Arrays.toString(spans), 1, spans.length); - assertTrue("The span should be a LinkSpan", spans[0] instanceof LinkSpan); - assertEquals("The LinkSpan should have id \"foobar\"", - "foobar", ((LinkSpan) spans[0]).getId()); - } - - @Test - public void testOnLinkClickListener() { - Annotation link = new Annotation("link", "foobar"); - SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); - ssb.setSpan(link, 1, 2, 0 /* flags */); + @Test + public void testLinkAnnotation() { + Annotation link = new Annotation("link", "foobar"); + SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); + ssb.setSpan(link, 1, 2, 0 /* flags */); + + RichTextView textView = new RichTextView(application); + textView.setText(ssb); + + final CharSequence text = textView.getText(); + assertThat(text).isInstanceOf(Spanned.class); + + assertThat(textView.getMovementMethod()).isInstanceOf(TouchableLinkMovementMethod.class); - RichTextView textView = new RichTextView(application); - textView.setText(ssb); + Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class); + assertThat(spans).isEmpty(); - OnLinkClickListener listener = mock(OnLinkClickListener.class); - textView.setOnLinkClickListener(listener); + spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class); + assertThat(spans).hasLength(1); + assertThat(spans[0]).isInstanceOf(LinkSpan.class); + assertWithMessage("The LinkSpan should have id \"foobar\"") + .that(((LinkSpan) spans[0]).getId()) + .isEqualTo("foobar"); + } + + @Test + public void testOnLinkClickListener() { + Annotation link = new Annotation("link", "foobar"); + SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); + ssb.setSpan(link, 1, 2, 0 /* flags */); + + RichTextView textView = new RichTextView(application); + textView.setText(ssb); - assertSame(listener, textView.getOnLinkClickListener()); - - CharSequence text = textView.getText(); - LinkSpan[] spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class); - spans[0].onClick(textView); - - verify(listener).onLinkClick(eq(spans[0])); + OnLinkClickListener listener = mock(OnLinkClickListener.class); + textView.setOnLinkClickListener(listener); + + assertThat(textView.getOnLinkClickListener()).isSameAs(listener); + + CharSequence text = textView.getText(); + LinkSpan[] spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class); + spans[0].onClick(textView); + + verify(listener).onLinkClick(eq(spans[0])); + } + + @Test + public void testLegacyContextOnClickListener() { + // Click listener implemented by context should still be invoked for compatibility. + Annotation link = new Annotation("link", "foobar"); + SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); + ssb.setSpan(link, 1, 2, 0 /* flags */); + + TestContext context = new TestContext(application); + context.delegate = mock(LinkSpan.OnClickListener.class); + RichTextView textView = new RichTextView(context); + textView.setText(ssb); + + CharSequence text = textView.getText(); + LinkSpan[] spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class); + spans[0].onClick(textView); + + verify(context.delegate).onClick(eq(spans[0])); + } + + @Test + public void onTouchEvent_clickOnLinks_shouldReturnTrue() { + Annotation link = new Annotation("link", "foobar"); + SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); + ssb.setSpan(link, 0, 2, 0 /* flags */); + + RichTextView textView = new RichTextView(application); + textView.setText(ssb); + + TouchableLinkMovementMethod mockMovementMethod = mock(TouchableLinkMovementMethod.class); + textView.setMovementMethod(mockMovementMethod); + + MotionEvent motionEvent = MotionEvent.obtain(123, 22, MotionEvent.ACTION_DOWN, 0, 0, 0); + doReturn(motionEvent).when(mockMovementMethod).getLastTouchEvent(); + doReturn(true).when(mockMovementMethod).isLastTouchEventHandled(); + assertThat(textView.onTouchEvent(motionEvent)).isTrue(); + } + + @Test + public void onTouchEvent_clickOutsideLinks_shouldReturnFalse() { + Annotation link = new Annotation("link", "foobar"); + SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); + ssb.setSpan(link, 0, 2, 0 /* flags */); + + RichTextView textView = new RichTextView(application); + textView.setText(ssb); + + TouchableLinkMovementMethod mockMovementMethod = mock(TouchableLinkMovementMethod.class); + textView.setMovementMethod(mockMovementMethod); + + MotionEvent motionEvent = MotionEvent.obtain(123, 22, MotionEvent.ACTION_DOWN, 0, 0, 0); + doReturn(motionEvent).when(mockMovementMethod).getLastTouchEvent(); + doReturn(false).when(mockMovementMethod).isLastTouchEventHandled(); + assertThat(textView.onTouchEvent(motionEvent)).isFalse(); + } + + @Test + public void testTextStyle() { + Annotation link = new Annotation("textAppearance", "foobar"); + SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); + ssb.setSpan(link, 1, 2, 0 /* flags */); + + RichTextView textView = new RichTextView(application); + textView.setText(ssb); + + final CharSequence text = textView.getText(); + assertThat(text).isInstanceOf(Spanned.class); + + Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class); + assertThat(spans).isEmpty(); + + spans = ((Spanned) text).getSpans(0, text.length(), TextAppearanceSpan.class); + assertThat(spans).hasLength(1); + assertThat(spans[0]).isInstanceOf(TextAppearanceSpan.class); + } + + @Test + public void testTextContainingLinksAreFocusable() { + Annotation testLink = new Annotation("link", "value"); + SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("Linked"); + spannableStringBuilder.setSpan(testLink, 0, 3, 0); + + RichTextView view = new RichTextView(application); + view.setText(spannableStringBuilder); + + assertThat(view.isFocusable()).named("view focusable").isTrue(); + } + + @SuppressLint("SetTextI18n") // It's OK. This is just a test. + @Test + public void testTextContainingNoLinksAreNotFocusable() { + RichTextView textView = new RichTextView(application); + textView.setText("Thou shall not be focusable!"); + + assertThat(textView.isFocusable()).named("view focusable").isFalse(); + } + + // Based on the text contents of the text view, the "focusable" property of the element + // should also be automatically changed. + @SuppressLint("SetTextI18n") // It's OK. This is just a test. + @Test + public void testRichTextViewFocusChangesWithTextChange() { + RichTextView textView = new RichTextView(application); + textView.setText("Thou shall not be focusable!"); + + assertThat(textView.isFocusable()).isFalse(); + assertThat(textView.isFocusableInTouchMode()).isFalse(); + + SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("I am focusable"); + spannableStringBuilder.setSpan(new Annotation("link", "focus:on_me"), 0, 1, 0); + textView.setText(spannableStringBuilder); + assertThat(textView.isFocusable()).isTrue(); + if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) { + assertThat(textView.isFocusableInTouchMode()).isTrue(); + assertThat(textView.getRevealOnFocusHint()).isFalse(); + } else { + assertThat(textView.isFocusableInTouchMode()).isFalse(); } + } - @Test - public void testLegacyContextOnClickListener() { - // Click listener implemented by context should still be invoked for compatibility. - Annotation link = new Annotation("link", "foobar"); - SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); - ssb.setSpan(link, 1, 2, 0 /* flags */); - - TestContext context = spy(new TestContext(application)); - RichTextView textView = new RichTextView(context); - textView.setText(ssb); + public static class TestContext extends ContextWrapper implements LinkSpan.OnClickListener { - CharSequence text = textView.getText(); - LinkSpan[] spans = ((Spanned) text).getSpans(0, text.length(), LinkSpan.class); - spans[0].onClick(textView); + LinkSpan.OnClickListener delegate; - verify(context).onClick(eq(spans[0])); + public TestContext(Context base) { + super(base); } - @Test - public void onTouchEvent_clickOnLinks_shouldReturnTrue() { - Annotation link = new Annotation("link", "foobar"); - SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); - ssb.setSpan(link, 0, 2, 0 /* flags */); - - RichTextView textView = new RichTextView(application); - textView.setText(ssb); - - TouchableLinkMovementMethod mockMovementMethod = mock(TouchableLinkMovementMethod.class); - textView.setMovementMethod(mockMovementMethod); - - MotionEvent motionEvent = - MotionEvent.obtain(123, 22, MotionEvent.ACTION_DOWN, 0, 0, 0); - doReturn(motionEvent).when(mockMovementMethod).getLastTouchEvent(); - doReturn(true).when(mockMovementMethod).isLastTouchEventHandled(); - assertThat(textView.onTouchEvent(motionEvent)).isTrue(); - } - - @Test - public void onTouchEvent_clickOutsideLinks_shouldReturnFalse() { - Annotation link = new Annotation("link", "foobar"); - SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); - ssb.setSpan(link, 0, 2, 0 /* flags */); - - RichTextView textView = new RichTextView(application); - textView.setText(ssb); - - TouchableLinkMovementMethod mockMovementMethod = mock(TouchableLinkMovementMethod.class); - textView.setMovementMethod(mockMovementMethod); - - MotionEvent motionEvent = - MotionEvent.obtain(123, 22, MotionEvent.ACTION_DOWN, 0, 0, 0); - doReturn(motionEvent).when(mockMovementMethod).getLastTouchEvent(); - doReturn(false).when(mockMovementMethod).isLastTouchEventHandled(); - assertThat(textView.onTouchEvent(motionEvent)).isFalse(); - } - - @Test - public void testTextStyle() { - Annotation link = new Annotation("textAppearance", "foobar"); - SpannableStringBuilder ssb = new SpannableStringBuilder("Hello world"); - ssb.setSpan(link, 1, 2, 0 /* flags */); - - RichTextView textView = new RichTextView(application); - textView.setText(ssb); - - final CharSequence text = textView.getText(); - assertTrue("Text should be spanned", text instanceof Spanned); - - Object[] spans = ((Spanned) text).getSpans(0, text.length(), Annotation.class); - assertEquals("Annotation should be removed " + Arrays.toString(spans), 0, spans.length); - - spans = ((Spanned) text).getSpans(0, text.length(), TextAppearanceSpan.class); - assertEquals("There should be one span " + Arrays.toString(spans), 1, spans.length); - assertTrue("The span should be a TextAppearanceSpan", - spans[0] instanceof TextAppearanceSpan); - } - - @Test - public void testTextContainingLinksAreFocusable() { - Annotation testLink = new Annotation("link", "value"); - SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("Linked"); - spannableStringBuilder.setSpan(testLink, 0, 3, 0); - - RichTextView view = new RichTextView(application); - view.setText(spannableStringBuilder); - - assertTrue("TextView should be focusable since it contains spans", view.isFocusable()); - } - - - @SuppressLint("SetTextI18n") // It's OK. This is just a test. - @Test - public void testTextContainingNoLinksAreNotFocusable() { - RichTextView textView = new RichTextView(application); - textView.setText("Thou shall not be focusable!"); - - assertFalse("TextView should not be focusable since it does not contain any span", - textView.isFocusable()); - } - - - // Based on the text contents of the text view, the "focusable" property of the element - // should also be automatically changed. - @SuppressLint("SetTextI18n") // It's OK. This is just a test. - @Test - public void testRichTextViewFocusChangesWithTextChange() { - RichTextView textView = new RichTextView(application); - textView.setText("Thou shall not be focusable!"); - - assertFalse(textView.isFocusable()); - assertFalse(textView.isFocusableInTouchMode()); - - SpannableStringBuilder spannableStringBuilder = - new SpannableStringBuilder("I am focusable"); - spannableStringBuilder.setSpan(new Annotation("link", "focus:on_me"), 0, 1, 0); - textView.setText(spannableStringBuilder); - assertTrue(textView.isFocusable()); - if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) { - assertTrue(textView.isFocusableInTouchMode()); - assertFalse(textView.getRevealOnFocusHint()); - } else { - assertFalse(textView.isFocusableInTouchMode()); - } - } - - public static class TestContext extends ContextWrapper implements LinkSpan.OnClickListener { - - public TestContext(Context base) { - super(base); - } - - @Override - public void onClick(LinkSpan span) { - // Ignore. Can be verified using Mockito - } + @Override + public void onClick(LinkSpan span) { + if (delegate != null) { + delegate.onClick(span); + } } + } } |