|
| 1 | +package org.medicmobile.webapp.mobile; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.Mockito.doNothing; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.times; |
| 9 | +import static org.mockito.Mockito.verify; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import android.content.Context; |
| 13 | +import android.net.Uri; |
| 14 | +import android.webkit.WebResourceRequest; |
| 15 | +import android.webkit.WebView; |
| 16 | + |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Test; |
| 19 | +import org.junit.runner.RunWith; |
| 20 | +import org.robolectric.RobolectricTestRunner; |
| 21 | + |
| 22 | +@RunWith(RobolectricTestRunner.class) |
| 23 | +public class UrlHandlerTest { |
| 24 | + private static final String APP_URL = "https://project-abc.medic.org"; |
| 25 | + private SettingsStore settingsStore; |
| 26 | + private WebView webView; |
| 27 | + private WebResourceRequest webResourceRequest; |
| 28 | + private Context context; |
| 29 | + private UrlHandler handler; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setup() { |
| 33 | + settingsStore = mock(SettingsStore.class); |
| 34 | + when(settingsStore.getAppUrl()).thenReturn(APP_URL); |
| 35 | + webView = mock(WebView.class); |
| 36 | + context = mock(Context.class); |
| 37 | + when(webView.getContext()).thenReturn(context); |
| 38 | + doNothing().when(context).startActivity(any()); |
| 39 | + webResourceRequest = mock(WebResourceRequest.class); |
| 40 | + |
| 41 | + handler = new UrlHandler(null, settingsStore); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void shouldOverrideUrlLoading_withAppUrlSubPath() { |
| 46 | + when(webResourceRequest.getUrl()).thenReturn(Uri.parse(APP_URL + "/some-sub-path")); |
| 47 | + |
| 48 | + boolean result = handler.shouldOverrideUrlLoading(webView, webResourceRequest); |
| 49 | + |
| 50 | + assertFalse(result); |
| 51 | + verify(settingsStore, times(2)).getAppUrl(); |
| 52 | + verify(webResourceRequest).getUrl(); |
| 53 | + verify(webView, times(0)).getContext(); |
| 54 | + verify(context, times(0)).startActivity(any()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void shouldOverrideUrlLoading_withExternalUrl() { |
| 59 | + when(webResourceRequest.getUrl()).thenReturn(Uri.parse("some-external-url.com")); |
| 60 | + |
| 61 | + boolean result = handler.shouldOverrideUrlLoading(webView, webResourceRequest); |
| 62 | + |
| 63 | + assertTrue(result); |
| 64 | + verify(settingsStore, times(2)).getAppUrl(); |
| 65 | + verify(webResourceRequest).getUrl(); |
| 66 | + verify(webView).getContext(); |
| 67 | + verify(context).startActivity(any()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void shouldOverrideUrlLoading_withExternalOidcProviderUrl() { |
| 72 | + when(webResourceRequest.getUrl()).thenReturn( |
| 73 | + Uri.parse("some-external-url.com?redirect_uri=" + Uri.encode(APP_URL + "/medic/login/oidc")) |
| 74 | + ); |
| 75 | + |
| 76 | + boolean result = handler.shouldOverrideUrlLoading(webView, webResourceRequest); |
| 77 | + |
| 78 | + assertFalse(result); |
| 79 | + verify(settingsStore, times(2)).getAppUrl(); |
| 80 | + verify(webResourceRequest).getUrl(); |
| 81 | + verify(webView, times(0)).getContext(); |
| 82 | + verify(context, times(0)).startActivity(any()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void shouldOverrideUrlLoading_withNullUrl() { |
| 87 | + when(webResourceRequest.getUrl()).thenReturn(null); |
| 88 | + |
| 89 | + boolean result = handler.shouldOverrideUrlLoading(webView, webResourceRequest); |
| 90 | + |
| 91 | + assertTrue(result); |
| 92 | + verify(settingsStore, times(2)).getAppUrl(); |
| 93 | + verify(webResourceRequest).getUrl(); |
| 94 | + verify(webView).getContext(); |
| 95 | + verify(context).startActivity(any()); |
| 96 | + } |
| 97 | +} |
0 commit comments