Skip to content

Commit 3551566

Browse files
authored
refactor user agent setting, set beta release version (#94)
1 parent 5cf6e0f commit 3551566

File tree

9 files changed

+106
-48
lines changed

9 files changed

+106
-48
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Changelog
22

3-
## 3.0.0 April 19, 2024
3+
## 3.0.0 May 10, 2024
44

5-
- Improves error handling. See [Error Handling Guidance](https://github.com/Shopify/checkout-sheet-kit-android#error-handling-guidance) for more information.
5+
- Improved error handling. Attempts to load checkout in a recovery WebView when certain errors are encountered. See [Error Handling](https://github.com/Shopify/checkout-sheet-kit-android#error-handling) for more information.
66

77
## 2.0.1 March 19, 2024
88

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def resolveEnvVarValue(name, defaultValue) {
1414
return rawValue ? rawValue : defaultValue
1515
}
1616

17-
def versionName = resolveEnvVarValue("CHECKOUT_SHEET_KIT_VERSION", "3.0.0")
17+
def versionName = resolveEnvVarValue("CHECKOUT_SHEET_KIT_VERSION", "3.0.0-beta.1")
1818

1919
ext {
2020
app_compat_version = '1.6.1'

lib/src/main/java/com/shopify/checkoutsheetkit/BaseWebView.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ internal abstract class BaseWebView(context: Context, attributeSet: AttributeSet
5252

5353
abstract fun getEventProcessor(): CheckoutWebViewEventProcessor
5454
abstract val recoverErrors: Boolean
55+
abstract val variant: String
56+
abstract val cspSchema: String
5557

5658
private fun configureWebView() {
5759
visibility = VISIBLE
@@ -79,6 +81,13 @@ internal abstract class BaseWebView(context: Context, attributeSet: AttributeSet
7981
}
8082
return super.onKeyDown(keyCode, event)
8183
}
84+
85+
internal fun userAgentSuffix(): String {
86+
val theme = ShopifyCheckoutSheetKit.configuration.colorScheme.id
87+
val version = ShopifyCheckoutSheetKit.version.split("-").first()
88+
return "ShopifyCheckoutSDK/${version} ($cspSchema;$theme;$variant)"
89+
}
90+
8291
open inner class BaseWebViewClient : WebViewClient() {
8392
init {
8493
if (BuildConfig.DEBUG) {

lib/src/main/java/com/shopify/checkoutsheetkit/CheckoutBridge.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ internal class CheckoutBridge(
137137
}
138138

139139
companion object {
140-
private const val SDK_VERSION_NUMBER: String = BuildConfig.SDK_VERSION
141-
private const val SCHEMA_VERSION_NUMBER: String = "8.1"
140+
const val SCHEMA_VERSION_NUMBER: String = "8.1"
142141

143142
private fun dispatchMessageTemplate(body: String) = """|
144143
|if (window.MobileCheckoutSdk && window.MobileCheckoutSdk.dispatchMessage) {
@@ -149,11 +148,6 @@ internal class CheckoutBridge(
149148
| }, {passive: true, once: true});
150149
|}
151150
|""".trimMargin()
152-
153-
fun userAgentSuffix(): String {
154-
val theme = ShopifyCheckoutSheetKit.configuration.colorScheme.id
155-
return "ShopifyCheckoutSDK/$SDK_VERSION_NUMBER ($SCHEMA_VERSION_NUMBER;$theme;standard)"
156-
}
157151
}
158152
}
159153

lib/src/main/java/com/shopify/checkoutsheetkit/CheckoutWebView.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n
4040
BaseWebView(context, attributeSet) {
4141

4242
override val recoverErrors = true
43+
override val variant = "standard"
44+
override val cspSchema = CheckoutBridge.SCHEMA_VERSION_NUMBER
4345

4446
private val checkoutBridge = CheckoutBridge(CheckoutWebViewEventProcessor(NoopEventProcessor()))
4547
private var loadComplete = false
@@ -64,7 +66,7 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n
6466
init {
6567
webViewClient = CheckoutWebViewClient()
6668
addJavascriptInterface(checkoutBridge, JAVASCRIPT_INTERFACE_NAME)
67-
settings.userAgentString = "${settings.userAgentString} ${CheckoutBridge.userAgentSuffix()}"
69+
settings.userAgentString = "${settings.userAgentString} ${userAgentSuffix()}"
6870
}
6971

7072
fun hasFinishedLoading() = loadComplete

lib/src/main/java/com/shopify/checkoutsheetkit/FallbackWebView.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ import com.shopify.checkoutsheetkit.lifecycleevents.emptyCompletedEvent
3131
internal class FallbackWebView(context: Context, attributeSet: AttributeSet? = null) :
3232
BaseWebView(context, attributeSet) {
3333

34+
override val recoverErrors = false
35+
override val variant = "standard_recovery"
36+
override val cspSchema = "noconnect"
37+
3438
init {
3539
webViewClient = FallbackWebViewClient()
36-
val theme = ShopifyCheckoutSheetKit.configuration.colorScheme.id
37-
val suffix = "ShopifyCheckoutSDK/${BuildConfig.SDK_VERSION} (noconnect;$theme;standard_recovery)"
38-
settings.userAgentString = "${settings.userAgentString} $suffix"
40+
settings.userAgentString = "${settings.userAgentString} ${userAgentSuffix()}"
3941
}
4042

41-
override val recoverErrors = false
42-
4343
private var checkoutEventProcessor = CheckoutWebViewEventProcessor(NoopEventProcessor())
4444

4545
fun setEventProcessor(processor: CheckoutWebViewEventProcessor) {

lib/src/test/java/com/shopify/checkoutsheetkit/CheckoutBridgeTest.kt

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,36 +93,6 @@ class CheckoutBridgeTest {
9393
verifyNoInteractions(mockEventProcessor)
9494
}
9595

96-
@Test
97-
fun `user agent suffix includes ShopifyCheckoutSDK and version number`() {
98-
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Dark()
99-
assertThat(CheckoutBridge.userAgentSuffix()).startsWith("ShopifyCheckoutSDK/${BuildConfig.SDK_VERSION} ")
100-
}
101-
102-
@Test
103-
fun `user agent suffix includes metadata for the schema version, theme, and variant - dark`() {
104-
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Dark()
105-
assertThat(CheckoutBridge.userAgentSuffix()).endsWith("(8.1;dark;standard)")
106-
}
107-
108-
@Test
109-
fun `user agent suffix includes metadata for the schema version, theme, and variant - light`() {
110-
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Light()
111-
assertThat(CheckoutBridge.userAgentSuffix()).endsWith("(8.1;light;standard)")
112-
}
113-
114-
@Test
115-
fun `user agent suffix includes metadata for the schema version, theme, and variant - web`() {
116-
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Web()
117-
assertThat(CheckoutBridge.userAgentSuffix()).endsWith("(8.1;web_default;standard)")
118-
}
119-
120-
@Test
121-
fun `user agent suffix includes metadata for the schema version, theme, and variant - automatic`() {
122-
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Automatic()
123-
assertThat(CheckoutBridge.userAgentSuffix()).endsWith("(8.1;automatic;standard)")
124-
}
125-
12696
@Test
12797
fun `sendMessage evaluates javascript on the provided WebView`() {
12898
val webView = mock<WebView>()

lib/src/test/java/com/shopify/checkoutsheetkit/CheckoutWebViewTest.kt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class CheckoutWebViewTest {
6161
assertThat(view.visibility).isEqualTo(VISIBLE)
6262
assertThat(view.settings.javaScriptEnabled).isTrue
6363
assertThat(view.settings.domStorageEnabled).isTrue
64-
assertThat(view.settings.userAgentString).contains("ShopifyCheckoutSDK")
6564
assertThat(view.layoutParams.height).isEqualTo(MATCH_PARENT)
6665
assertThat(view.layoutParams.width).isEqualTo(MATCH_PARENT)
6766
assertThat(view.id).isNotNull
@@ -71,6 +70,46 @@ class CheckoutWebViewTest {
7170
.isEqualTo(CheckoutBridge::class.java)
7271
}
7372

73+
@Test
74+
fun `user agent suffix includes ShopifyCheckoutSDK and version number`() {
75+
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Dark()
76+
val view = CheckoutWebView.cacheableCheckoutView(URL, activity)
77+
78+
assertThat(view.settings.userAgentString).contains("ShopifyCheckoutSDK/3.0.0 ")
79+
}
80+
81+
@Test
82+
fun `user agent suffix includes metadata for the schema version, theme, and variant - dark`() {
83+
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Dark()
84+
val view = CheckoutWebView.cacheableCheckoutView(URL, activity)
85+
86+
assertThat(view.settings.userAgentString).endsWith("(8.1;dark;standard)")
87+
}
88+
89+
@Test
90+
fun `user agent suffix includes metadata for the schema version, theme, and variant - light`() {
91+
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Light()
92+
val view = CheckoutWebView.cacheableCheckoutView(URL, activity)
93+
94+
assertThat(view.settings.userAgentString).endsWith("(8.1;light;standard)")
95+
}
96+
97+
@Test
98+
fun `user agent suffix includes metadata for the schema version, theme, and variant - web`() {
99+
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Web()
100+
val view = CheckoutWebView.cacheableCheckoutView(URL, activity)
101+
102+
assertThat(view.settings.userAgentString).endsWith("(8.1;web_default;standard)")
103+
}
104+
105+
@Test
106+
fun `user agent suffix includes metadata for the schema version, theme, and variant - automatic`() {
107+
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Automatic()
108+
val view = CheckoutWebView.cacheableCheckoutView(URL, activity)
109+
110+
assertThat(view.settings.userAgentString).endsWith("(8.1;automatic;standard)")
111+
}
112+
74113
@Test
75114
fun `sends prefetch header for preloads`() {
76115
withPreloadingEnabled {

lib/src/test/java/com/shopify/checkoutsheetkit/FallbackWebViewTest.kt

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class FallbackWebViewTest {
4646
assertThat(view.visibility).isEqualTo(VISIBLE)
4747
assertThat(view.settings.javaScriptEnabled).isTrue
4848
assertThat(view.settings.domStorageEnabled).isTrue
49-
assertThat(view.settings.userAgentString).contains("ShopifyCheckoutSDK/${BuildConfig.SDK_VERSION} (noconnect")
5049
assertThat(view.layoutParams.height).isEqualTo(MATCH_PARENT)
5150
assertThat(view.layoutParams.width).isEqualTo(MATCH_PARENT)
5251
assertThat(view.id).isNotNull
@@ -56,6 +55,51 @@ class FallbackWebViewTest {
5655
}
5756
}
5857

58+
@Test
59+
fun `user agent suffix includes ShopifyCheckoutSDK and version number`() {
60+
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Dark()
61+
Robolectric.buildActivity(ComponentActivity::class.java).use { activityController ->
62+
val view = FallbackWebView(activityController.get())
63+
assertThat(view.settings.userAgentString).contains("ShopifyCheckoutSDK/3.0.0 ")
64+
}
65+
}
66+
67+
@Test
68+
fun `user agent suffix includes metadata for the schema version, theme, and variant - dark`() {
69+
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Dark()
70+
Robolectric.buildActivity(ComponentActivity::class.java).use { activityController ->
71+
val view = FallbackWebView(activityController.get())
72+
assertThat(view.settings.userAgentString).endsWith("(noconnect;dark;standard_recovery)")
73+
}
74+
}
75+
76+
@Test
77+
fun `user agent suffix includes metadata for the schema version, theme, and variant - light`() {
78+
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Light()
79+
Robolectric.buildActivity(ComponentActivity::class.java).use { activityController ->
80+
val view = FallbackWebView(activityController.get())
81+
assertThat(view.settings.userAgentString).endsWith("(noconnect;light;standard_recovery)")
82+
}
83+
}
84+
85+
@Test
86+
fun `user agent suffix includes metadata for the schema version, theme, and variant - web`() {
87+
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Web()
88+
Robolectric.buildActivity(ComponentActivity::class.java).use { activityController ->
89+
val view = FallbackWebView(activityController.get())
90+
assertThat(view.settings.userAgentString).endsWith("(noconnect;web_default;standard_recovery)")
91+
}
92+
}
93+
94+
@Test
95+
fun `user agent suffix includes metadata for the schema version, theme, and variant - automatic`() {
96+
ShopifyCheckoutSheetKit.configuration.colorScheme = ColorScheme.Automatic()
97+
Robolectric.buildActivity(ComponentActivity::class.java).use { activityController ->
98+
val view = FallbackWebView(activityController.get())
99+
assertThat(view.settings.userAgentString).endsWith("(noconnect;automatic;standard_recovery)")
100+
}
101+
}
102+
59103
@Test
60104
fun `calls update progress when new progress is reported by WebChromeClient`() {
61105
Robolectric.buildActivity(ComponentActivity::class.java).use { activityController ->

0 commit comments

Comments
 (0)