-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for default checkout event processor
- Loading branch information
Showing
3 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.shopify.checkoutkit | ||
|
||
import android.util.Log | ||
|
||
/** | ||
* Wrap Log class static methods to allow testing | ||
*/ | ||
public class LogWrapper { | ||
public fun w(tag: String, msg: String) { | ||
Log.w(tag, msg) | ||
} | ||
|
||
public fun e(tag: String, msg: String) { | ||
Log.e(tag, msg) | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
lib/src/test/java/com/shopify/checkoutkit/DefaultCheckoutEventProcessorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.shopify.checkoutkit | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import androidx.activity.ComponentActivity | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.verify | ||
import org.robolectric.Robolectric | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.Shadows.shadowOf | ||
import org.robolectric.shadows.ShadowActivity | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class DefaultCheckoutEventProcessorTest { | ||
|
||
private lateinit var activity: ComponentActivity | ||
private lateinit var shadowActivity: ShadowActivity | ||
|
||
@Before | ||
fun setUp() { | ||
activity = Robolectric.buildActivity(ComponentActivity::class.java).get() | ||
shadowActivity = shadowOf(activity) | ||
} | ||
|
||
@Test | ||
fun `onCheckoutLinkClicked with http scheme launches action view intent with uri as data`() { | ||
val processor = processor(activity) | ||
val uri = Uri.parse("https://shopify.com") | ||
|
||
processor.onCheckoutLinkClicked(uri) | ||
|
||
val intent = shadowActivity.peekNextStartedActivityForResult().intent | ||
assertThat(intent.data).isEqualTo(uri) | ||
intent.type = Intent.ACTION_VIEW | ||
} | ||
|
||
@Test | ||
fun `onCheckoutLinkClicked with mailto scheme launches email intent with to address`() { | ||
val processor = processor(activity) | ||
val uri = Uri.parse("mailto:test.user@shopify.com") | ||
|
||
processor.onCheckoutLinkClicked(uri) | ||
|
||
val intent = shadowActivity.peekNextStartedActivityForResult().intent | ||
assertThat(intent.getStringArrayExtra(Intent.EXTRA_EMAIL)).isEqualTo(arrayOf("test.user@shopify.com")) | ||
intent.type = "vnd.android.cursor.item/email" | ||
} | ||
|
||
@Test | ||
fun `onCheckoutLinkClicked with tel scheme launches action dial intent with phone number`() { | ||
val processor = processor(activity) | ||
val uri = Uri.parse("tel:0123456789") | ||
|
||
processor.onCheckoutLinkClicked(uri) | ||
|
||
val intent = shadowActivity.peekNextStartedActivityForResult().intent | ||
assertThat(intent.data).isEqualTo(uri) | ||
intent.type = Intent.ACTION_DIAL | ||
} | ||
|
||
@Test | ||
fun `onCheckoutLinkedClick with unhandled scheme logs warning`() { | ||
val log = mock<LogWrapper>() | ||
val processor = object: DefaultCheckoutEventProcessor(activity, log) { | ||
override fun onCheckoutCompleted() {/* not implemented */} | ||
override fun onCheckoutFailed(error: CheckoutException) {/* not implemented */} | ||
override fun onCheckoutCanceled() {/* not implemented */} | ||
} | ||
|
||
val uri = Uri.parse("ftp:lsklsm") | ||
|
||
processor.onCheckoutLinkClicked(uri) | ||
|
||
assertThat(shadowActivity.peekNextStartedActivityForResult()).isNull() | ||
verify(log).w("DefaultCheckoutEventProcessor", "Unrecognized scheme for link clicked in checkout 'ftp:lsklsm'") | ||
} | ||
|
||
private fun processor(activity: ComponentActivity): DefaultCheckoutEventProcessor { | ||
return object: DefaultCheckoutEventProcessor(activity) { | ||
override fun onCheckoutCompleted() {/* not implemented */} | ||
override fun onCheckoutFailed(error: CheckoutException) {/* not implemented */} | ||
override fun onCheckoutCanceled() {/* not implemented */} | ||
} | ||
} | ||
} |