11package org .medicmobile .webapp .mobile ;
22
3+ import static org .junit .Assert .assertEquals ;
34import static org .junit .Assert .assertFalse ;
45import static org .junit .Assert .assertTrue ;
56import static org .mockito .ArgumentMatchers .any ;
67import static org .mockito .Mockito .doNothing ;
78import static org .mockito .Mockito .mock ;
9+ import static org .mockito .Mockito .mockConstruction ;
810import static org .mockito .Mockito .times ;
911import static org .mockito .Mockito .verify ;
1012import static org .mockito .Mockito .when ;
1416import android .webkit .WebResourceRequest ;
1517import android .webkit .WebView ;
1618
19+ import androidx .browser .customtabs .CustomTabsIntent ;
20+
1721import org .junit .Before ;
1822import org .junit .Test ;
1923import org .junit .runner .RunWith ;
24+ import org .mockito .MockedConstruction ;
2025import org .robolectric .RobolectricTestRunner ;
2126
2227@ RunWith (RobolectricTestRunner .class )
2328public class UrlHandlerTest {
2429 private static final String APP_URL = "https://project-abc.medic.org" ;
30+ private EmbeddedBrowserActivity parentActivity ;
2531 private SettingsStore settingsStore ;
2632 private WebView webView ;
2733 private WebResourceRequest webResourceRequest ;
@@ -30,6 +36,7 @@ public class UrlHandlerTest {
3036
3137 @ Before
3238 public void setup () {
39+ parentActivity = mock (EmbeddedBrowserActivity .class );
3340 settingsStore = mock (SettingsStore .class );
3441 when (settingsStore .getAppUrl ()).thenReturn (APP_URL );
3542 webView = mock (WebView .class );
@@ -38,7 +45,7 @@ public void setup() {
3845 doNothing ().when (context ).startActivity (any ());
3946 webResourceRequest = mock (WebResourceRequest .class );
4047
41- handler = new UrlHandler (null , settingsStore );
48+ handler = new UrlHandler (parentActivity , settingsStore );
4249 }
4350
4451 @ Test
@@ -69,17 +76,26 @@ public void shouldOverrideUrlLoading_withExternalUrl() {
6976
7077 @ Test
7178 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 ());
79+ CustomTabsIntent intent = mock (CustomTabsIntent .class );
80+ Uri expectedUri = Uri .parse ("some-external-url.com?redirect_uri=" + Uri .encode (APP_URL + "/medic/login/oidc" ));
81+ try (MockedConstruction <CustomTabsIntent .Builder > mocked = mockConstruction (
82+ CustomTabsIntent .Builder .class ,
83+ (mock , context ) -> when (mock .build ()).thenReturn (intent )
84+ )) {
85+ doNothing ().when (intent ).launchUrl (any (), any ());
86+ when (webResourceRequest .getUrl ()).thenReturn (expectedUri );
87+
88+ boolean result = handler .shouldOverrideUrlLoading (webView , webResourceRequest );
89+
90+ assertTrue (result );
91+ verify (settingsStore , times (2 )).getAppUrl ();
92+ verify (webResourceRequest ).getUrl ();
93+ verify (webView , times (0 )).getContext ();
94+ verify (context , times (0 )).startActivity (any ());
95+ assertEquals (1 , mocked .constructed ().size ());
96+ verify (mocked .constructed ().get (0 ), times (1 )).build ();
97+ verify (intent , times (1 )).launchUrl (parentActivity , expectedUri );
98+ }
8399 }
84100
85101 @ Test
0 commit comments