This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[url_launcher] Add non-browser URL launch mode for Android #5953
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d14828d
Add non-browser URL launch mode for Android
Mosc 96d7b94
Use application context to access package manager
Mosc 6888d86
Add tests
Mosc 327480c
Bump version
Mosc 56a6d82
Fix formatting
Mosc 99c3c8e
Fix imports
Mosc cfd4412
Bump version
Mosc 2b91cc0
Simplify test
Mosc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...auncher_android/android/src/test/java/io/flutter/plugins/urllauncher/UrlLauncherTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.urllauncher; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.anyInt; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.ActivityInfo; | ||
import android.content.pm.PackageManager; | ||
import android.content.pm.ResolveInfo; | ||
import android.os.Build; | ||
import io.flutter.plugins.urllauncher.utils.IntentDataMatcher; | ||
import io.flutter.plugins.urllauncher.utils.TestUtils; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Matchers; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class UrlLauncherTest { | ||
private Context applicationContext; | ||
private UrlLauncher urlLauncher; | ||
|
||
private static final String LAUNCH_URL = "https://www.google.com"; | ||
|
||
@Before | ||
public void setUp() { | ||
applicationContext = mock(Context.class); | ||
Activity activity = mock(Activity.class); | ||
urlLauncher = new UrlLauncher(applicationContext, activity); | ||
} | ||
|
||
@Test | ||
public void launch_shouldNotQueryPackageManagerWhenUniversalLinksOnlyOnAndroidR() { | ||
updateSdkVersion(Build.VERSION_CODES.R); | ||
|
||
try { | ||
PackageManager mockPackageManager = mock(PackageManager.class); | ||
when(applicationContext.getPackageManager()).thenReturn(mockPackageManager); | ||
|
||
urlLauncher.launch(LAUNCH_URL, null, false, false, false, true); | ||
|
||
verify(mockPackageManager, never()).queryIntentActivities(any(Intent.class), anyInt()); | ||
} finally { | ||
updateSdkVersion(0); | ||
} | ||
} | ||
|
||
@Test | ||
public void launch_shouldReturnOkWhenUniversalLinksOnlyBelowAndroidRAndNonBrowserPresent() { | ||
updateSdkVersion(Build.VERSION_CODES.Q); | ||
|
||
try { | ||
PackageManager mockPackageManager = mock(PackageManager.class); | ||
when(applicationContext.getPackageManager()).thenReturn(mockPackageManager); | ||
ResolveInfo browserIntentActivity = stubResolveInfo("browser"); | ||
ResolveInfo nonBrowserIntentActivity = stubResolveInfo("nonBrowser"); | ||
when(mockPackageManager.queryIntentActivities(any(Intent.class), anyInt())) | ||
.thenReturn(Collections.singletonList(browserIntentActivity)); | ||
when(mockPackageManager.queryIntentActivities( | ||
Matchers.argThat(new IntentDataMatcher(LAUNCH_URL)), anyInt())) | ||
.thenReturn(Arrays.asList(nonBrowserIntentActivity, browserIntentActivity)); | ||
|
||
UrlLauncher.LaunchStatus launchStatus = | ||
urlLauncher.launch(LAUNCH_URL, null, false, false, false, true); | ||
|
||
verify(mockPackageManager, times(2)).queryIntentActivities(any(Intent.class), anyInt()); | ||
assertEquals(launchStatus, UrlLauncher.LaunchStatus.OK); | ||
} finally { | ||
updateSdkVersion(0); | ||
} | ||
} | ||
|
||
@Test | ||
public void | ||
launch_shouldReturnActivityNotFoundWhenUniversalLinksOnlyBelowAndroidRAndNonBrowserNotPresent() { | ||
updateSdkVersion(Build.VERSION_CODES.Q); | ||
|
||
try { | ||
PackageManager mockPackageManager = mock(PackageManager.class); | ||
when(applicationContext.getPackageManager()).thenReturn(mockPackageManager); | ||
ResolveInfo browserIntentActivity = stubResolveInfo("browser"); | ||
when(mockPackageManager.queryIntentActivities(any(Intent.class), anyInt())) | ||
.thenReturn(Collections.singletonList(browserIntentActivity)); | ||
|
||
UrlLauncher.LaunchStatus launchStatus = | ||
urlLauncher.launch(LAUNCH_URL, null, false, false, false, true); | ||
|
||
verify(mockPackageManager, times(2)).queryIntentActivities(any(Intent.class), anyInt()); | ||
assertEquals(launchStatus, UrlLauncher.LaunchStatus.ACTIVITY_NOT_FOUND); | ||
} finally { | ||
updateSdkVersion(0); | ||
} | ||
} | ||
|
||
private static ResolveInfo stubResolveInfo(String packageName) { | ||
ResolveInfo resolveInfo = new ResolveInfo(); | ||
resolveInfo.activityInfo = new ActivityInfo(); | ||
resolveInfo.activityInfo.packageName = packageName; | ||
return resolveInfo; | ||
} | ||
|
||
private static void updateSdkVersion(int version) { | ||
TestUtils.setFinalStatic(Build.VERSION.class, "SDK_INT", version); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...android/android/src/test/java/io/flutter/plugins/urllauncher/utils/IntentDataMatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.urllauncher.utils; | ||
|
||
import android.content.Intent; | ||
import org.mockito.ArgumentMatcher; | ||
|
||
public class IntentDataMatcher extends ArgumentMatcher<Intent> { | ||
public IntentDataMatcher(String dataString) { | ||
this.dataString = dataString; | ||
} | ||
|
||
private final String dataString; | ||
|
||
@Override | ||
public boolean matches(Object intent) { | ||
return ((Intent) intent).getDataString().equals(dataString); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...auncher_android/android/src/test/java/io/flutter/plugins/urllauncher/utils/TestUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.urllauncher.utils; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import org.junit.Assert; | ||
|
||
public class TestUtils { | ||
public static <T> void setFinalStatic(Class<T> classToModify, String fieldName, Object newValue) { | ||
try { | ||
Field field = classToModify.getField(fieldName); | ||
field.setAccessible(true); | ||
|
||
Field modifiersField = Field.class.getDeclaredField("modifiers"); | ||
modifiersField.setAccessible(true); | ||
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); | ||
|
||
field.set(null, newValue); | ||
} catch (Exception e) { | ||
Assert.fail("Unable to mock static field: " + fieldName); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change will need to be a separate PR, since you need to update the minimum Android implementation package version to say this.