Skip to content

Commit

Permalink
Refactoring tests to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
kpmmmurphy committed May 31, 2018
1 parent 541d008 commit a2dc38c
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 150 deletions.
143 changes: 0 additions & 143 deletions alerter/src/androidTest/java/com/tapadoo/alerter/AlertTest.java

This file was deleted.

143 changes: 143 additions & 0 deletions alerter/src/androidTest/java/com/tapadoo/alerter/AlertTest.kt
Original file line number Diff line number Diff line change
@@ -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>(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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
}
}
23 changes: 22 additions & 1 deletion alerter/src/main/java/com/tapadoo/alerter/Alert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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?) {
Expand Down Expand Up @@ -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
*/
Expand Down
11 changes: 11 additions & 0 deletions alerter/src/main/java/com/tapadoo/alerter/Alerter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a2dc38c

Please sign in to comment.