Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resurrect BrowserSelectorTest #1339

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
// THE SOFTWARE.
package com.microsoft.identity.common;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
Expand All @@ -31,35 +30,34 @@
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.pm.Signature;
import android.content.pm.SigningInfo;
import android.net.Uri;
import android.os.Build;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.core.app.ApplicationProvider;

import com.microsoft.identity.common.exception.ClientException;
import com.microsoft.identity.common.exception.ErrorStrings;
import com.microsoft.identity.common.internal.broker.PackageHelper;
import com.microsoft.identity.common.internal.ui.browser.Browser;
import com.microsoft.identity.common.internal.ui.browser.BrowserDescriptor;
import com.microsoft.identity.common.internal.ui.browser.BrowserSelector;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowPackageManager;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

@RunWith(AndroidJUnit4.class)
@RunWith(RobolectricTestRunner.class)
public class BrowserSelectorTest {
private static final String SCHEME_HTTP = "http";
private static final String SCHEME_HTTPS = "https";
Expand Down Expand Up @@ -96,38 +94,25 @@ public class BrowserSelectorTest {
.addSignature("DolphinSignature")
.build();

@Mock
Context mContext;
@Mock
PackageManager mPackageManager;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
}

//Currently package manager call returns an empty list... failing this test. Needs investigation.
//Ignored while updating to latest Mockito version
@Ignore
@Test
public void testSelect_getAllBrowser() throws NameNotFoundException {
setBrowserList(CHROME, FIREFOX);
when(mContext.getPackageManager().resolveActivity(BROWSER_INTENT, 0))
.thenReturn(CHROME.mResolveInfo);
List<Browser> allBrowsers = BrowserSelector.getAllBrowsers(mContext);

List<Browser> allBrowsers = BrowserSelector.getAllBrowsers(ApplicationProvider.getApplicationContext());
assert (allBrowsers.get(0).getPackageName().equals(CHROME.mPackageName));
assert (allBrowsers.get(1).getPackageName().equals(FIREFOX.mPackageName));
}

@Test
public void testSelect_noMatchingBrowser() throws NameNotFoundException {
setBrowserList(CHROME, FIREFOX);
when(mContext.getPackageManager().resolveActivity(BROWSER_INTENT, 0))
.thenReturn(CHROME.mResolveInfo);

final List<BrowserDescriptor> browserSafelist = new ArrayList<>();
try {
BrowserSelector.select(mContext, browserSafelist);
BrowserSelector.select(ApplicationProvider.getApplicationContext(), browserSafelist);
} catch (final ClientException exception) {
assertNotNull(exception);
assert (exception.getErrorCode().equalsIgnoreCase(ErrorStrings.NO_AVAILABLE_BROWSER_FOUND));
Expand All @@ -137,8 +122,7 @@ public void testSelect_noMatchingBrowser() throws NameNotFoundException {
@Test
public void testSelect_versionNotSupportedBrowser() throws NameNotFoundException {
setBrowserList(CHROME, FIREFOX);
when(mContext.getPackageManager().resolveActivity(BROWSER_INTENT, 0))
.thenReturn(CHROME.mResolveInfo);

List<BrowserDescriptor> browserSafelist = new ArrayList<>();
browserSafelist.add(
new BrowserDescriptor(
Expand All @@ -149,7 +133,7 @@ public void testSelect_versionNotSupportedBrowser() throws NameNotFoundException
);

try {
BrowserSelector.select(mContext, browserSafelist);
BrowserSelector.select(ApplicationProvider.getApplicationContext(), browserSafelist);
} catch (final ClientException exception) {
assertNotNull(exception);
assert (exception.getErrorCode().equalsIgnoreCase(ErrorStrings.NO_AVAILABLE_BROWSER_FOUND));
Expand All @@ -164,25 +148,13 @@ private void setBrowserList(TestBrowser... browsers) throws NameNotFoundExceptio
return;
}

final List<ResolveInfo> resolveInfos = new ArrayList<>();
final PackageManager packageManager = ApplicationProvider.getApplicationContext().getPackageManager();
final ShadowPackageManager shadowPackageManager = shadowOf(packageManager);

for (TestBrowser browser : browsers) {
when(mPackageManager.getPackageInfo(
eq(browser.mPackageInfo.packageName),
eq(PackageHelper.getPackageManagerSignaturesFlag())))
.thenReturn(browser.mPackageInfo);
resolveInfos.add(browser.mResolveInfo);
}

int queryFlag = PackageManager.GET_RESOLVED_FILTER;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
queryFlag |= PackageManager.MATCH_DEFAULT_ONLY;
shadowPackageManager.installPackage(browser.mPackageInfo);
shadowPackageManager.addResolveInfoForIntent(BROWSER_INTENT, browser.mResolveInfo);
}

when(mPackageManager.queryIntentActivities(
BROWSER_INTENT,
queryFlag))
.thenReturn(resolveInfos);
}

private static class TestBrowser {
Expand Down Expand Up @@ -253,14 +225,27 @@ public TestBrowserBuilder setVersion(String version) {
return this;
}

private PackageInfo addSignatures(final PackageInfo packageInfo, final Signature[] signatures) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
packageInfo.signatures = signatures;
return packageInfo;
}

final SigningInfo signingInfo = mock(SigningInfo.class);
when(signingInfo.hasMultipleSigners()).thenReturn(false);
when(signingInfo.getSigningCertificateHistory()).thenReturn(signatures);
packageInfo.signingInfo = signingInfo;
rpdome marked this conversation as resolved.
Show resolved Hide resolved
return packageInfo;
}

public TestBrowser build() {
Signature[] signatures = new Signature[mSignatures.size()];

for (int i = 0; i < mSignatures.size(); i++) {
signatures[i] = new Signature(mSignatures.get(i));
}

final PackageInfo pi = Util.addSignatures(new PackageInfo(), signatures);
rpdome marked this conversation as resolved.
Show resolved Hide resolved
final PackageInfo pi = addSignatures(new PackageInfo(), signatures);
pi.packageName = mPackageName;
pi.versionName = mVersion;

Expand Down