|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright 2023-present, Shopify Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | +package com.shopify.checkoutsheetkit |
| 24 | + |
| 25 | +import androidx.activity.ComponentActivity |
| 26 | +import org.assertj.core.api.Assertions.assertThat |
| 27 | +import org.junit.After |
| 28 | +import org.junit.Before |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.runner.RunWith |
| 31 | +import org.robolectric.Robolectric |
| 32 | +import org.robolectric.RobolectricTestRunner |
| 33 | +import org.robolectric.Shadows.shadowOf |
| 34 | +import org.robolectric.shadows.ShadowLooper |
| 35 | + |
| 36 | +@RunWith(RobolectricTestRunner::class) |
| 37 | +class ShopifyCheckoutSheetKitTest { |
| 38 | + |
| 39 | + @Before |
| 40 | + fun setUp() { |
| 41 | + ShopifyCheckoutSheetKit.configure { |
| 42 | + it.preloading = Preloading(enabled = false) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @After |
| 47 | + fun tearDown() { |
| 48 | + CheckoutWebView.cacheEntry = null |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `preload is a noop if preloading is not enabled`() { |
| 53 | + Robolectric.buildActivity(ComponentActivity::class.java).use { activityController -> |
| 54 | + val url = "https://shopify.dev" |
| 55 | + ShopifyCheckoutSheetKit.preload(url, activityController.get()) |
| 56 | + ShadowLooper.shadowMainLooper().runToEndOfTasks() |
| 57 | + |
| 58 | + assertThat(CheckoutWebView.cacheEntry).isNull() |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `preload caches a WebView and loads the URL if cache is currently empty`() { |
| 64 | + Robolectric.buildActivity(ComponentActivity::class.java).use { activityController -> |
| 65 | + withPreloadingEnabled { |
| 66 | + val url = "https://shopify.dev" |
| 67 | + ShopifyCheckoutSheetKit.preload(url, activityController.get()) |
| 68 | + ShadowLooper.shadowMainLooper().runToEndOfTasks() |
| 69 | + |
| 70 | + assertThat(CheckoutWebView.cacheEntry).isNotNull() |
| 71 | + val entry = CheckoutWebView.cacheEntry!! |
| 72 | + |
| 73 | + assertThat(entry.isStale).isFalse() |
| 74 | + assertThat(entry.view).isInstanceOf(CheckoutWebView::class.java) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `preload caches a new WebView, loads the URL, and destroys old view if cache is not empty`() { |
| 81 | + Robolectric.buildActivity(ComponentActivity::class.java).use { activityController -> |
| 82 | + withPreloadingEnabled { |
| 83 | + val url = "https://shopify.dev" |
| 84 | + ShopifyCheckoutSheetKit.preload(url, activityController.get()) |
| 85 | + ShadowLooper.shadowMainLooper().runToEndOfTasks() |
| 86 | + val originalEntry = CheckoutWebView.cacheEntry |
| 87 | + |
| 88 | + ShopifyCheckoutSheetKit.preload(url, activityController.get()) |
| 89 | + ShadowLooper.shadowMainLooper().runToEndOfTasks() |
| 90 | + |
| 91 | + assertThat(CheckoutWebView.cacheEntry).isNotNull() |
| 92 | + val entry = CheckoutWebView.cacheEntry!! |
| 93 | + |
| 94 | + assertThat(entry.isStale).isFalse() |
| 95 | + assertThat(entry.view).isInstanceOf(CheckoutWebView::class.java) |
| 96 | + assertThat(entry.view).isNotEqualTo(originalEntry) |
| 97 | + assertThat(shadowOf(originalEntry?.view).wasDestroyCalled()).isTrue() |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun `preload while presented loads url in the existing view`() { |
| 104 | + Robolectric.buildActivity(ComponentActivity::class.java).use { activityController -> |
| 105 | + withPreloadingEnabled { |
| 106 | + val activity = activityController.get() |
| 107 | + |
| 108 | + // first preload caches the view |
| 109 | + ShopifyCheckoutSheetKit.preload("https://one.com", activityController.get()) |
| 110 | + ShadowLooper.shadowMainLooper().runToEndOfTasks() |
| 111 | + |
| 112 | + val originalEntry = CheckoutWebView.cacheEntry |
| 113 | + assertThat(originalEntry!!.key).isEqualTo("https://one.com") |
| 114 | + assertThat(originalEntry.isStale).isFalse() |
| 115 | + |
| 116 | + // present loads the cached view |
| 117 | + ShopifyCheckoutSheetKit.present("https://one.com", activity, noopDefaultCheckoutEventProcessor(activity)) |
| 118 | + ShadowLooper.shadowMainLooper().runToEndOfTasks() |
| 119 | + |
| 120 | + val secondEntry = CheckoutWebView.cacheEntry |
| 121 | + assertThat(secondEntry!!.key).isEqualTo("https://one.com") |
| 122 | + assertThat(secondEntry.isStale).isFalse() |
| 123 | + |
| 124 | + // preload after present loads URL in the cached view |
| 125 | + ShopifyCheckoutSheetKit.preload("https://one.com", activityController.get()) |
| 126 | + ShadowLooper.shadowMainLooper().runToEndOfTasks() |
| 127 | + |
| 128 | + val thirdEntry = CheckoutWebView.cacheEntry |
| 129 | + assertThat(thirdEntry!!.key).isEqualTo("https://one.com") |
| 130 | + assertThat(thirdEntry.isStale).isFalse() |
| 131 | + |
| 132 | + assertThat(shadowOf(thirdEntry.view).lastLoadedUrl).isEqualTo("https://one.com") |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + fun `preload after presented loads the url and marks cache stale if url doesn't match cache key`() { |
| 139 | + Robolectric.buildActivity(ComponentActivity::class.java).use { activityController -> |
| 140 | + withPreloadingEnabled { |
| 141 | + val activity = activityController.get() |
| 142 | + |
| 143 | + // first preload caches the view |
| 144 | + ShopifyCheckoutSheetKit.preload("https://one.com", activityController.get()) |
| 145 | + ShadowLooper.shadowMainLooper().runToEndOfTasks() |
| 146 | + |
| 147 | + val originalEntry = CheckoutWebView.cacheEntry |
| 148 | + assertThat(originalEntry?.key).isEqualTo("https://one.com") |
| 149 | + assertThat(originalEntry?.isStale).isFalse() |
| 150 | + |
| 151 | + // present loads the cached view |
| 152 | + ShopifyCheckoutSheetKit.present("https://one.com", activity, noopDefaultCheckoutEventProcessor(activity)) |
| 153 | + ShadowLooper.shadowMainLooper().runToEndOfTasks() |
| 154 | + |
| 155 | + val secondEntry = CheckoutWebView.cacheEntry |
| 156 | + assertThat(secondEntry?.key).isEqualTo("https://one.com") |
| 157 | + assertThat(secondEntry?.isStale).isFalse() |
| 158 | + |
| 159 | + // preload after present loads URL in the cached view |
| 160 | + ShopifyCheckoutSheetKit.preload("https://two.com", activityController.get()) |
| 161 | + ShadowLooper.shadowMainLooper().runToEndOfTasks() |
| 162 | + |
| 163 | + // as the URL no longer matches the cache key, mark the cache entry as stale |
| 164 | + val thirdEntry = CheckoutWebView.cacheEntry |
| 165 | + assertThat(thirdEntry?.key).isEqualTo("https://one.com") |
| 166 | + assertThat(thirdEntry?.isStale).isTrue() |
| 167 | + |
| 168 | + assertThat(shadowOf(thirdEntry?.view).lastLoadedUrl).isEqualTo("https://two.com") |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments