Skip to content

Commit

Permalink
Merge pull request #203 from droibit/feature/enhance_testability
Browse files Browse the repository at this point in the history
Improve unit testability of CustomTabsLauncher by moving logic to IntentFactory
  • Loading branch information
droibit committed Jul 26, 2024
2 parents 10e4073 + 172914e commit db1fcab
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,7 @@ public void launch(
}

try {
final @Nullable CustomTabsIntentOptions customTabsOptions;
if (options == null) {
customTabsOptions = null;
} else {
customTabsOptions = new CustomTabsIntentOptions.Builder()
.setOptions(options)
.build();
}
final CustomTabsIntentOptions customTabsOptions = intentFactory.createCustomTabsIntentOptions(options);
final Intent externalBrowserIntent = intentFactory.createExternalBrowserIntent(customTabsOptions);
if (externalBrowserIntent != null) {
externalBrowserIntent.setData(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,14 @@ void applyBrowserConfiguration(
}
return dest;
}

public @Nullable CustomTabsIntentOptions createCustomTabsIntentOptions(@Nullable Map<String, Object> options) {
if (options == null) {
return null;
} else {
return new CustomTabsIntentOptions.Builder()
.setOptions(options)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
Expand All @@ -25,6 +26,7 @@
import com.github.droibit.flutter.plugins.customtabs.Messages.FlutterError;
import com.github.droibit.flutter.plugins.customtabs.core.IntentFactory;
import com.github.droibit.flutter.plugins.customtabs.core.NativeAppLauncher;
import com.github.droibit.flutter.plugins.customtabs.core.options.CustomTabsIntentOptions;

import org.junit.Rule;
import org.junit.Test;
Expand All @@ -46,7 +48,7 @@ public class CustomTabsLauncherTest {
public MockitoRule mockitoRule = MockitoJUnit.rule();

@Mock
private IntentFactory customTabsFactory;
private IntentFactory intentFactory;

@Mock
private NativeAppLauncher nativeAppLauncher;
Expand Down Expand Up @@ -85,17 +87,18 @@ public void launchNativeAppSuccess() {
fail(e.getMessage());
}

verify(customTabsFactory, never()).createExternalBrowserIntent(any());
verify(customTabsFactory, never()).createCustomTabsIntent(any(), any());
verify(intentFactory, never()).createExternalBrowserIntent(any());
verify(intentFactory, never()).createCustomTabsIntent(any(), any());
}

@Test
public void launchExternalBrowserSuccess() {
final Activity activity = mock(Activity.class);
launcher.setActivity(activity);

when(intentFactory.createCustomTabsIntentOptions(any())).thenReturn(null);
final Intent externalBrowserIntent = new Intent();
when(customTabsFactory.createExternalBrowserIntent(any())).thenReturn(externalBrowserIntent);
when(intentFactory.createExternalBrowserIntent(any())).thenReturn(externalBrowserIntent);

try {
final String expUrl = "https://example.com";
Expand All @@ -106,20 +109,21 @@ public void launchExternalBrowserSuccess() {

final Intent actualIntent = intentCaptor.getValue();
assertThat(actualIntent).hasData(Uri.parse(expUrl));

verify(intentFactory, never()).createCustomTabsIntent(any(), any());
} catch (Exception e) {
fail(e.getMessage());
}

verify(customTabsFactory, never()).createCustomTabsIntent(any(), any());
}

@Test
public void launchExternalBrowserFailure() {
final Activity activity = mock(Activity.class);
launcher.setActivity(activity);

when(intentFactory.createCustomTabsIntentOptions(any())).thenReturn(null);
final Intent externalBrowserIntent = new Intent();
when(customTabsFactory.createExternalBrowserIntent(any())).thenReturn(externalBrowserIntent);
when(intentFactory.createExternalBrowserIntent(any())).thenReturn(externalBrowserIntent);

final ActivityNotFoundException anf = mock(ActivityNotFoundException.class);
doThrow(anf).when(activity).startActivity(any());
Expand All @@ -133,6 +137,8 @@ public void launchExternalBrowserFailure() {
final FlutterError actualError = ((FlutterError) e);
assertThat(actualError.code).isEqualTo(CustomTabsLauncher.CODE_LAUNCH_ERROR);
}

verify(intentFactory, never()).createCustomTabsIntent(any(), any());
}


Expand All @@ -141,12 +147,14 @@ public void launchPartialCustomTabsSuccess() {
final Activity activity = mock(Activity.class);
launcher.setActivity(activity);

when(customTabsFactory.createExternalBrowserIntent(any())).thenReturn(null);
final CustomTabsIntentOptions intentOptions = mock(CustomTabsIntentOptions.class);
when(intentFactory.createCustomTabsIntentOptions(any())).thenReturn(intentOptions);
when(intentFactory.createExternalBrowserIntent(any())).thenReturn(null);

final CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
.setInitialActivityHeightPx(100)
.build();
when(customTabsFactory.createCustomTabsIntent(any(), any())).thenReturn(customTabsIntent);
when(intentFactory.createCustomTabsIntent(any(), any())).thenReturn(customTabsIntent);

try {
final String expUrl = "https://example.com";
Expand All @@ -159,6 +167,8 @@ public void launchPartialCustomTabsSuccess() {
final Intent actualIntent = intentCaptor.getValue();
assertThat(actualIntent).hasData(Uri.parse(expUrl));
assertThat(actualIntent).isSameInstanceAs(customTabsIntent.intent);

verify(intentFactory).createCustomTabsIntent(any(), same(intentOptions));
} catch (Exception e) {
fail(e.getMessage());
}
Expand All @@ -169,10 +179,12 @@ public void launchCustomTabsSuccess() {
final Activity activity = mock(Activity.class);
launcher.setActivity(activity);

when(customTabsFactory.createExternalBrowserIntent(any())).thenReturn(null);
final CustomTabsIntentOptions intentOptions = mock(CustomTabsIntentOptions.class);
when(intentFactory.createCustomTabsIntentOptions(any())).thenReturn(intentOptions);
when(intentFactory.createExternalBrowserIntent(any())).thenReturn(null);

final CustomTabsIntent customTabsIntent = spy(new CustomTabsIntent.Builder().build());
when(customTabsFactory.createCustomTabsIntent(any(), any())).thenReturn(customTabsIntent);
when(intentFactory.createCustomTabsIntent(any(), any())).thenReturn(customTabsIntent);

try {
final String expUrl = "https://example.com";
Expand All @@ -184,6 +196,8 @@ public void launchCustomTabsSuccess() {

final Uri actualUrl = urlCaptor.getValue();
assertThat(actualUrl).isEqualTo(Uri.parse(expUrl));

verify(intentFactory).createCustomTabsIntent(any(), same(intentOptions));
} catch (Exception e) {
fail(e.getMessage());
}
Expand All @@ -194,10 +208,12 @@ public void launchCustomTabsFailure() {
final Activity activity = mock(Activity.class);
launcher.setActivity(activity);

when(customTabsFactory.createExternalBrowserIntent(any())).thenReturn(null);
final CustomTabsIntentOptions intentOptions = mock(CustomTabsIntentOptions.class);
when(intentFactory.createCustomTabsIntentOptions(any())).thenReturn(intentOptions);
when(intentFactory.createExternalBrowserIntent(any())).thenReturn(null);

final CustomTabsIntent customTabsIntent = spy(new CustomTabsIntent.Builder().build());
when(customTabsFactory.createCustomTabsIntent(any(), any())).thenReturn(customTabsIntent);
when(intentFactory.createCustomTabsIntent(any(), any())).thenReturn(customTabsIntent);

final ActivityNotFoundException anf = mock(ActivityNotFoundException.class);
doThrow(anf).when(customTabsIntent).launchUrl(any(), any());
Expand All @@ -212,5 +228,7 @@ public void launchCustomTabsFailure() {
final FlutterError actualError = ((FlutterError) e);
assertThat(actualError.code).isEqualTo(CustomTabsLauncher.CODE_LAUNCH_ERROR);
}

verify(intentFactory).createCustomTabsIntent(any(), same(intentOptions));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;

Expand Down Expand Up @@ -74,6 +75,7 @@
import org.robolectric.annotation.Config;

import java.util.AbstractMap.SimpleEntry;
import java.util.Collections;
import java.util.List;

@RunWith(AndroidJUnit4.class)
Expand Down Expand Up @@ -589,4 +591,16 @@ public void applyBrowserConfiguration_prefersDefaultBrowser() {
);
}
}

@Test
public void createCustomTabsIntentOptions_nullOptions() {
final CustomTabsIntentOptions options = intentFactory.createCustomTabsIntentOptions(null);
assertThat(options).isNull();
}

@Test
public void createCustomTabsIntentOptions_notNullOptions() {
final CustomTabsIntentOptions options = intentFactory.createCustomTabsIntentOptions(emptyMap());
assertThat(options).isNotNull();
}
}

0 comments on commit db1fcab

Please sign in to comment.