diff --git a/alerter/src/androidTest/java/com/tapadoo/alerter/AlertTest.java b/alerter/src/androidTest/java/com/tapadoo/alerter/AlertTest.java deleted file mode 100644 index 7e31182..0000000 --- a/alerter/src/androidTest/java/com/tapadoo/alerter/AlertTest.java +++ /dev/null @@ -1,143 +0,0 @@ -package com.tapadoo.alerter; - -import android.annotation.SuppressLint; -import android.graphics.drawable.ColorDrawable; -import android.os.Build; -import android.support.test.filters.LargeTest; -import android.support.test.rule.ActivityTestRule; -import android.support.test.runner.AndroidJUnit4; -import android.support.v4.content.ContextCompat; -import android.view.View; - -import com.tapadoo.android.R; - -import junit.framework.Assert; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -/** - * Alert Test Case Class - * - * @author Kevin Murphy, Tapadoo - * @since 04/10/2016 - **/ -@RunWith(AndroidJUnit4.class) -@LargeTest -public class AlertTest { - - //Rule which sets the Activity to be used - @Rule - public final ActivityTestRule activityRule = new ActivityTestRule<>(MockActivity.class); - - /** - * Test Strings - */ - private static final String HELLO = "Hello"; - private static final String HI = "Hi"; - private static final String ALERTER = "Alerter"; - - @SuppressLint("StaticFieldLeak") - private static Alert alert; - - @Before // Called before each test - public void setUp() throws Exception { - alert = new Alert(activityRule.getActivity()); - } - - @Test - public void testConstruction() { - final Alert alert = new Alert(activityRule.getActivity()); - Assert.assertNotNull(alert); - } - - @Test - public void testLayoutElements() { - final Alert alert = new Alert(activityRule.getActivity()); - - //Ensure all elements are present - Assert.assertNotNull(alert.getAlertBackground()); - Assert.assertNotNull(alert.getTitle()); - Assert.assertNotNull(alert.getText()); - Assert.assertNotNull(alert.getIcon()); - } - - @Test - public void testTitleString() { - //Strings - alert.setTitle(HELLO); - Assert.assertTrue(alert.getTitle().getVisibility() == View.VISIBLE); - - Assert.assertNotNull(alert.getTitle().getText()); - Assert.assertEquals(HELLO, alert.getTitle().getText()); - Assert.assertNotSame(HI, alert.getTitle().getText()); - } - - @Test - public void testTitleStringRes() { - //String Resources - alert.setTitle(R.string.lib_name); - Assert.assertTrue(alert.getTitle().getVisibility() == View.VISIBLE); - - Assert.assertNotNull(alert.getTitle().getText()); - Assert.assertEquals(ALERTER, alert.getTitle().getText()); - Assert.assertNotSame(HI, alert.getTitle().getText()); - } - - @Test - public void testTextString() { - //Strings - alert.setText(HELLO); - Assert.assertTrue(alert.getText().getVisibility() == View.VISIBLE); - - Assert.assertNotNull(alert.getText().getText()); - Assert.assertEquals(HELLO, alert.getText().getText()); - Assert.assertNotSame(HI, alert.getText().getText()); - } - - @Test - public void testTextStringRes() { - //Strings Resources - alert.setText(R.string.lib_name); - Assert.assertTrue(alert.getText().getVisibility() == View.VISIBLE); - - Assert.assertNotNull(alert.getText().getText()); - Assert.assertEquals(ALERTER, alert.getText().getText()); - Assert.assertNotSame(HI, alert.getText().getText()); - } - - @Test - public void testBackgroundColour() { - alert.setAlertBackgroundColor(ContextCompat.getColor(activityRule.getActivity(), android.R.color.darker_gray)); - - Assert.assertNotNull(alert.getAlertBackground().getBackground()); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { - Assert.assertEquals(((ColorDrawable) alert.getAlertBackground().getBackground()).getColor(), ContextCompat.getColor(activityRule.getActivity(), android.R.color.darker_gray)); - } - } - - @Test - public void testIcon() { - //Compare same Drawables - alert.setIcon(android.R.drawable.sym_def_app_icon); - Assert.assertNotNull(alert.getIcon().getDrawable()); - } - - @Test - public void testOnClickListener() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { - return; - } - - //Check default onClickListener - Assert.assertTrue(alert.getAlertBackground().hasOnClickListeners()); - - //Check nullifying - alert.setOnClickListener(null); - Assert.assertFalse(alert.getAlertBackground().hasOnClickListeners()); - } - -} \ No newline at end of file diff --git a/alerter/src/androidTest/java/com/tapadoo/alerter/AlertTest.kt b/alerter/src/androidTest/java/com/tapadoo/alerter/AlertTest.kt new file mode 100644 index 0000000..60a51a3 --- /dev/null +++ b/alerter/src/androidTest/java/com/tapadoo/alerter/AlertTest.kt @@ -0,0 +1,143 @@ +package com.tapadoo.alerter + +import android.annotation.SuppressLint +import android.graphics.drawable.ColorDrawable +import android.os.Build +import android.support.test.filters.LargeTest +import android.support.test.rule.ActivityTestRule +import android.support.test.runner.AndroidJUnit4 +import android.support.v4.content.ContextCompat +import android.view.View +import junit.framework.Assert +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Alert Test Case Class + * + * @author Kevin Murphy, Tapadoo + * @since 04/10/2016 + */ +@RunWith(AndroidJUnit4::class) +@LargeTest +class AlertTest { + + //Rule which sets the Activity to be used + @Rule + internal val activityRule = ActivityTestRule(MockActivity::class.java) + + @Before // Called before each test + @Throws(Exception::class) + fun setUp() { + alert = Alert(activityRule.activity) + } + + @Test + fun testConstruction() { + val alert = Alert(activityRule.activity) + Assert.assertNotNull(alert) + } + + @Test + fun testLayoutElements() { + val alert = Alert(activityRule.activity) + + //Ensure all elements are present + Assert.assertNotNull(alert.alertBackground) + Assert.assertNotNull(alert.title) + Assert.assertNotNull(alert.text) + Assert.assertNotNull(alert.icon) + } + + @Test + fun testTitleString() { + //Strings + alert!!.setTitle(HELLO) + Assert.assertTrue(alert!!.title?.visibility === View.VISIBLE) + + Assert.assertNotNull(alert!!.title?.text) + Assert.assertEquals(HELLO, alert!!.title?.text) + Assert.assertNotSame(HI, alert!!.title?.text) + } + + @Test + fun testTitleStringRes() { + //String Resources + alert!!.setTitle(R.string.lib_name) + Assert.assertTrue(alert!!.title?.visibility === View.VISIBLE) + + Assert.assertNotNull(alert!!.title?.text) + Assert.assertEquals(ALERTER, alert!!.title?.text) + Assert.assertNotSame(HI, alert!!.title?.text) + } + + @Test + fun testTextString() { + //Strings + alert!!.setText(HELLO) + Assert.assertTrue(alert!!.text?.visibility === View.VISIBLE) + + Assert.assertNotNull(alert!!.text?.text) + Assert.assertEquals(HELLO, alert!!.text?.text) + Assert.assertNotSame(HI, alert!!.text?.text) + } + + @Test + fun testTextStringRes() { + //Strings Resources + alert!!.setText(R.string.lib_name) + Assert.assertTrue(alert!!.text?.visibility === View.VISIBLE) + + Assert.assertNotNull(alert!!.text?.text) + Assert.assertEquals(ALERTER, alert!!.text?.text) + Assert.assertNotSame(HI, alert!!.text?.text) + } + + @Test + fun testBackgroundColour() { + alert!!.setAlertBackgroundColor(ContextCompat.getColor(activityRule.activity, android.R.color.darker_gray)) + + Assert.assertNotNull(alert!!.alertBackground?.background) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { + Assert.assertEquals((alert!!.alertBackground?.background as ColorDrawable).color, ContextCompat.getColor(activityRule.activity, android.R.color.darker_gray)) + } + } + + @Test + fun testIcon() { + //Compare same Drawables + alert!!.setIcon(android.R.drawable.sym_def_app_icon) + Assert.assertNotNull(alert!!.icon?.drawable) + } + + @Test + fun testOnClickListener() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { + return + } + + //Check default onClickListener + Assert.assertTrue(alert!!.alertBackground?.hasOnClickListeners() ?: false) + + //Check nullifying + alert!!.setOnClickListener(null) + Assert.assertFalse(alert!!.alertBackground?.hasOnClickListeners() ?: false) + } + + companion object { + + /** + * Test Strings + */ + private val HELLO = "Hello" + private val HI = "Hi" + private val ALERTER = "Alerter" + + @SuppressLint("StaticFieldLeak") + private var alert: Alert? = null + } + +} \ No newline at end of file diff --git a/alerter/src/androidTest/java/com/tapadoo/alerter/AlerterTest.kt b/alerter/src/androidTest/java/com/tapadoo/alerter/AlerterTest.kt index df46bed..e22f0e0 100644 --- a/alerter/src/androidTest/java/com/tapadoo/alerter/AlerterTest.kt +++ b/alerter/src/androidTest/java/com/tapadoo/alerter/AlerterTest.kt @@ -10,8 +10,6 @@ import android.util.Log import android.view.View import android.view.ViewGroup -import com.tapadoo.android.R - import org.junit.Assert import org.junit.Rule import org.junit.Test @@ -29,7 +27,7 @@ class AlerterTest { //Rule which sets the Activity to be used @Rule - val activityRule = ActivityTestRule(MockActivity::class.java) + internal val activityRule = ActivityTestRule(MockActivity::class.java) @Test fun testConstruction() { @@ -126,9 +124,8 @@ class AlerterTest { //Test setting listener val alert3 = Alerter.create(activityRule.activity).setOnClickListener(View.OnClickListener { //Ignore - }) - .show() + }).show() - Assert.assertTrue(alert3!!.getAlertBackground().hasOnClickListeners()) + Assert.assertTrue(alert3!!.alertBackground?.hasOnClickListeners() ?: false) } } diff --git a/alerter/src/main/java/com/tapadoo/alerter/Alert.kt b/alerter/src/main/java/com/tapadoo/alerter/Alert.kt index 9a6f3dd..df30d92 100644 --- a/alerter/src/main/java/com/tapadoo/alerter/Alert.kt +++ b/alerter/src/main/java/com/tapadoo/alerter/Alert.kt @@ -55,6 +55,8 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n private var runningAnimation: Runnable? = null + private var dismissable = true + /** * Flag to ensure we only set the margins once */ @@ -138,7 +140,9 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n } override fun onClick(v: View) { - hide() + if (dismissable) { + hide() + } } override fun setOnClickListener(listener: View.OnClickListener?) { @@ -464,6 +468,23 @@ class Alert @JvmOverloads constructor(context: Context, attrs: AttributeSet? = n icon!!.visibility = if (showIcon) View.VISIBLE else View.GONE } + /** + * Set if the alerter is dismissable or not + * + * @param dismissible True if alert can be dismissed + */ + fun setDismissable(dismissable: Boolean) { + this.dismissable = dismissable + } + + /** + * Get if the alert is dismissable + * @return + */ + fun isDismissable(): Boolean { + return dismissable + } + /** * Set whether to enable swipe to dismiss or not */ diff --git a/alerter/src/main/java/com/tapadoo/alerter/Alerter.kt b/alerter/src/main/java/com/tapadoo/alerter/Alerter.kt index 8ff51f4..8e3c951 100644 --- a/alerter/src/main/java/com/tapadoo/alerter/Alerter.kt +++ b/alerter/src/main/java/com/tapadoo/alerter/Alerter.kt @@ -463,6 +463,17 @@ class Alerter private constructor() { return this } + /** + * Set if the Alert is dismissable or not + * + * @param dismissable true if it can be dismissed + * @return + */ + fun setDismissable(dismissable: Boolean): Alerter { + alert?.setDismissable(dismissable) + + return this + } /** * Creates a weak reference to the calling Activity