|
| 1 | +package com.iterable.iterableapi; |
| 2 | + |
| 3 | +import org.junit.After; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.mockito.ArgumentCaptor; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertEquals; |
| 9 | +import static org.mockito.ArgumentMatchers.eq; |
| 10 | +import static org.mockito.Mockito.spy; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | + |
| 13 | +public class IterableWebViewTest extends BaseTest { |
| 14 | + |
| 15 | + private IterableWebView webView; |
| 16 | + private IterableWebView webViewSpy; |
| 17 | + |
| 18 | + @Before |
| 19 | + public void setUp() { |
| 20 | + IterableTestUtils.createIterableApiNew(); |
| 21 | + webView = new IterableWebView(getContext()); |
| 22 | + webViewSpy = spy(webView); |
| 23 | + } |
| 24 | + |
| 25 | + @After |
| 26 | + public void tearDown() { |
| 27 | + IterableTestUtils.resetIterableApi(); |
| 28 | + } |
| 29 | + |
| 30 | + // ===== Base URL Configuration Tests ===== |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testGetWebViewBaseUrl_DefaultConfiguration() { |
| 34 | + // Test: When webViewBaseUrl is not configured, should return empty string |
| 35 | + String baseUrl = IterableUtil.getWebViewBaseUrl(); |
| 36 | + assertEquals("Default webViewBaseUrl should be empty string", "", baseUrl); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testGetWebViewBaseUrl_CustomConfiguration() { |
| 41 | + // Test: When webViewBaseUrl is configured, should return the configured value |
| 42 | + String customBaseUrl = "https://app.iterable.com"; |
| 43 | + |
| 44 | + IterableConfig config = new IterableConfig.Builder() |
| 45 | + .setWebViewBaseUrl(customBaseUrl) |
| 46 | + .build(); |
| 47 | + |
| 48 | + IterableApi.initialize(getContext(), "test-api-key", config); |
| 49 | + |
| 50 | + String baseUrl = IterableUtil.getWebViewBaseUrl(); |
| 51 | + assertEquals("Custom webViewBaseUrl should be returned", customBaseUrl, baseUrl); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testGetWebViewBaseUrl_EUConfiguration() { |
| 56 | + // Test: EU region configuration |
| 57 | + String euBaseUrl = "https://app.eu.iterable.com"; |
| 58 | + |
| 59 | + IterableConfig config = new IterableConfig.Builder() |
| 60 | + .setWebViewBaseUrl(euBaseUrl) |
| 61 | + .build(); |
| 62 | + |
| 63 | + IterableApi.initialize(getContext(), "test-api-key", config); |
| 64 | + |
| 65 | + String baseUrl = IterableUtil.getWebViewBaseUrl(); |
| 66 | + assertEquals("EU webViewBaseUrl should be returned", euBaseUrl, baseUrl); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testGetWebViewBaseUrl_NullConfiguration() { |
| 71 | + // Test: When webViewBaseUrl is explicitly set to null, should return empty string |
| 72 | + IterableConfig config = new IterableConfig.Builder() |
| 73 | + .setWebViewBaseUrl(null) |
| 74 | + .build(); |
| 75 | + |
| 76 | + IterableApi.initialize(getContext(), "test-api-key", config); |
| 77 | + |
| 78 | + String baseUrl = IterableUtil.getWebViewBaseUrl(); |
| 79 | + assertEquals("Null webViewBaseUrl should return empty string", "", baseUrl); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testGetWebViewBaseUrl_EmptyStringConfiguration() { |
| 84 | + // Test: When webViewBaseUrl is explicitly set to empty string, should return empty string |
| 85 | + IterableConfig config = new IterableConfig.Builder() |
| 86 | + .setWebViewBaseUrl("") |
| 87 | + .build(); |
| 88 | + |
| 89 | + IterableApi.initialize(getContext(), "test-api-key", config); |
| 90 | + |
| 91 | + String baseUrl = IterableUtil.getWebViewBaseUrl(); |
| 92 | + assertEquals("Empty webViewBaseUrl should return empty string", "", baseUrl); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testGetWebViewBaseUrl_ExceptionHandling() { |
| 97 | + // Test: Exception handling when SDK is not initialized properly |
| 98 | + IterableTestUtils.resetIterableApi(); |
| 99 | + |
| 100 | + // This should not throw an exception and should return empty string |
| 101 | + String baseUrl = IterableUtil.getWebViewBaseUrl(); |
| 102 | + assertEquals("Exception case should return empty string", "", baseUrl); |
| 103 | + } |
| 104 | + |
| 105 | + // ===== WebView Integration Tests ===== |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testCreateWithHtml_DefaultConfiguration_UsesEmptyBaseUrl() { |
| 109 | + // Test: WebView uses empty string as base URL when not configured (about:blank origin) |
| 110 | + MockHTMLNotificationCallbacks mockCallbacks = new MockHTMLNotificationCallbacks(); |
| 111 | + String testHtml = "<html><body>Test Content</body></html>"; |
| 112 | + |
| 113 | + webViewSpy.createWithHtml(mockCallbacks, testHtml); |
| 114 | + |
| 115 | + // Verify loadDataWithBaseURL was called with empty string (default behavior) |
| 116 | + ArgumentCaptor<String> baseUrlCaptor = ArgumentCaptor.forClass(String.class); |
| 117 | + verify(webViewSpy).loadDataWithBaseURL( |
| 118 | + baseUrlCaptor.capture(), |
| 119 | + eq(testHtml), |
| 120 | + eq(IterableWebView.MIME_TYPE), |
| 121 | + eq(IterableWebView.ENCODING), |
| 122 | + eq("") |
| 123 | + ); |
| 124 | + |
| 125 | + assertEquals("Default base URL should be empty string (about:blank origin)", "", baseUrlCaptor.getValue()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testCreateWithHtml_CustomConfiguration_UsesConfiguredBaseUrl() { |
| 130 | + // Test: WebView uses configured base URL to enable CORS for external resources |
| 131 | + String customBaseUrl = "https://app.iterable.com"; |
| 132 | + |
| 133 | + IterableConfig config = new IterableConfig.Builder() |
| 134 | + .setWebViewBaseUrl(customBaseUrl) |
| 135 | + .build(); |
| 136 | + |
| 137 | + IterableApi.initialize(getContext(), "test-api-key", config); |
| 138 | + |
| 139 | + MockHTMLNotificationCallbacks mockCallbacks = new MockHTMLNotificationCallbacks(); |
| 140 | + String testHtml = "<html><head><link href='https://webfonts.wolt.com/index.css' rel='stylesheet'></head><body>Custom Fonts</body></html>"; |
| 141 | + |
| 142 | + webViewSpy.createWithHtml(mockCallbacks, testHtml); |
| 143 | + |
| 144 | + // Verify loadDataWithBaseURL was called with custom base URL |
| 145 | + ArgumentCaptor<String> baseUrlCaptor = ArgumentCaptor.forClass(String.class); |
| 146 | + verify(webViewSpy).loadDataWithBaseURL( |
| 147 | + baseUrlCaptor.capture(), |
| 148 | + eq(testHtml), |
| 149 | + eq(IterableWebView.MIME_TYPE), |
| 150 | + eq(IterableWebView.ENCODING), |
| 151 | + eq("") |
| 152 | + ); |
| 153 | + |
| 154 | + assertEquals("Custom base URL should enable CORS for external resources", customBaseUrl, baseUrlCaptor.getValue()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testCreateWithHtml_EUConfiguration_EnablesCORSForWoltFonts() { |
| 159 | + // Test: WebView uses EU base URL for CORS compliance with Wolt's self-hosted fonts |
| 160 | + String euBaseUrl = "https://app.eu.iterable.com"; |
| 161 | + |
| 162 | + IterableConfig config = new IterableConfig.Builder() |
| 163 | + .setWebViewBaseUrl(euBaseUrl) |
| 164 | + .build(); |
| 165 | + |
| 166 | + IterableApi.initialize(getContext(), "test-api-key", config); |
| 167 | + |
| 168 | + MockHTMLNotificationCallbacks mockCallbacks = new MockHTMLNotificationCallbacks(); |
| 169 | + String woltHtml = "<html><head><link href='https://webfonts.wolt.com/index.css' rel='stylesheet'></head><body>Wolt Content with Custom Fonts</body></html>"; |
| 170 | + |
| 171 | + webViewSpy.createWithHtml(mockCallbacks, woltHtml); |
| 172 | + |
| 173 | + // Verify loadDataWithBaseURL was called with EU base URL |
| 174 | + ArgumentCaptor<String> baseUrlCaptor = ArgumentCaptor.forClass(String.class); |
| 175 | + verify(webViewSpy).loadDataWithBaseURL( |
| 176 | + baseUrlCaptor.capture(), |
| 177 | + eq(woltHtml), |
| 178 | + eq(IterableWebView.MIME_TYPE), |
| 179 | + eq(IterableWebView.ENCODING), |
| 180 | + eq("") |
| 181 | + ); |
| 182 | + |
| 183 | + assertEquals("EU base URL should enable CORS for Wolt's custom fonts", euBaseUrl, baseUrlCaptor.getValue()); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void testCreateWithHtml_CustomDomain_EnablesCORSForAnyDomain() { |
| 188 | + // Test: Custom domain configuration works for any customer domain |
| 189 | + String customDomain = "https://custom.example.com"; |
| 190 | + |
| 191 | + IterableConfig config = new IterableConfig.Builder() |
| 192 | + .setWebViewBaseUrl(customDomain) |
| 193 | + .build(); |
| 194 | + |
| 195 | + IterableApi.initialize(getContext(), "test-api-key", config); |
| 196 | + |
| 197 | + MockHTMLNotificationCallbacks mockCallbacks = new MockHTMLNotificationCallbacks(); |
| 198 | + String testHtml = "<html><body>Custom Domain Content</body></html>"; |
| 199 | + |
| 200 | + webViewSpy.createWithHtml(mockCallbacks, testHtml); |
| 201 | + |
| 202 | + // Verify loadDataWithBaseURL was called with custom domain |
| 203 | + ArgumentCaptor<String> baseUrlCaptor = ArgumentCaptor.forClass(String.class); |
| 204 | + verify(webViewSpy).loadDataWithBaseURL( |
| 205 | + baseUrlCaptor.capture(), |
| 206 | + eq(testHtml), |
| 207 | + eq(IterableWebView.MIME_TYPE), |
| 208 | + eq(IterableWebView.ENCODING), |
| 209 | + eq("") |
| 210 | + ); |
| 211 | + |
| 212 | + assertEquals("Custom domain should be used for CORS", customDomain, baseUrlCaptor.getValue()); |
| 213 | + } |
| 214 | + |
| 215 | + // Mock implementation for testing |
| 216 | + private static class MockHTMLNotificationCallbacks implements IterableWebView.HTMLNotificationCallbacks { |
| 217 | + @Override |
| 218 | + public void onUrlClicked(String url) { |
| 219 | + // Mock implementation |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public void setLoaded(boolean loaded) { |
| 224 | + // Mock implementation |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public void runResizeScript() { |
| 229 | + // Mock implementation |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments