Skip to content

Commit 5baaba0

Browse files
committed
fix tests
1 parent 7f266eb commit 5baaba0

File tree

4 files changed

+45
-155
lines changed

4 files changed

+45
-155
lines changed

common/src/main/java/com/microsoft/identity/common/internal/ui/AndroidAuthorizationStrategyFactory.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
@SuppressWarnings(WarningType.rawtype_warning)
4848
@Builder
4949
@Accessors(prefix = "m")
50-
public class AndroidAuthorizationStrategyFactory implements IAuthorizationStrategyFactory<IAuthorizationStrategy>{
50+
public class AndroidAuthorizationStrategyFactory implements IAuthorizationStrategyFactory<IAuthorizationStrategy> {
5151
private static final String TAG = AndroidAuthorizationStrategyFactory.class.getSimpleName();
5252

5353
private final Context mContext;
@@ -58,9 +58,8 @@ public class AndroidAuthorizationStrategyFactory implements IAuthorizationStrate
5858
* Get the authorization strategy.
5959
*
6060
* @param authorizationAgent The authorization agent provided by the caller.
61-
* @param browser The browser to use for authorization.
62-
* @param isBrowserRequest True if the request is from browser.
63-
*
61+
* @param browser The browser to use for authorization.
62+
* @param isBrowserRequest True if the request is from browser.
6463
* @return The authorization strategy.
6564
*/
6665
@Override
@@ -72,7 +71,7 @@ public IAuthorizationStrategy getAuthorizationStrategy(
7271
final String methodTag = TAG + ":getAuthorizationStrategy";
7372

7473
// Use embedded webView if no browser available or authorization agent is webView
75-
if (authorizationAgent== AuthorizationAgent.WEBVIEW || browser == null) {
74+
if (authorizationAgent == AuthorizationAgent.WEBVIEW || browser == null) {
7675
Logger.info(methodTag, "Use webView for authorization.");
7776
return getGenericAuthorizationStrategy(browser);
7877
}
@@ -85,7 +84,7 @@ public IAuthorizationStrategy getAuthorizationStrategy(
8584
* Get current task browser authorization strategy or default browser authorization strategy.
8685
* If the authorization is in current task, use current task browser authorization strategy.
8786
*
88-
* @param browser The browser to use for authorization.
87+
* @param browser The browser to use for authorization.
8988
* @param isBrokerRequest True if the request is from broker.
9089
* @return The browser authorization strategy.
9190
*/

common/src/test/java/com/microsoft/identity/common/internal/ui/browser/BrowserSelectorTest.java

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
// THE SOFTWARE.
2323
package com.microsoft.identity.common.internal.ui.browser;
2424

25+
import static org.junit.Assert.assertEquals;
2526
import static org.junit.Assert.assertNotNull;
27+
import static org.junit.Assert.assertNull;
2628
import static org.mockito.Mockito.mock;
2729
import static org.mockito.Mockito.when;
2830
import static org.robolectric.Shadows.shadowOf;
@@ -41,8 +43,6 @@
4143
import androidx.test.core.app.ApplicationProvider;
4244

4345
import com.microsoft.identity.common.java.browser.Browser;
44-
import com.microsoft.identity.common.java.exception.ClientException;
45-
import com.microsoft.identity.common.java.exception.ErrorStrings;
4646
import com.microsoft.identity.common.java.ui.BrowserDescriptor;
4747

4848
import org.junit.Assert;
@@ -113,12 +113,8 @@ public void testSelect_noMatchingBrowser() {
113113
setBrowserList(CHROME, FIREFOX);
114114

115115
final List<BrowserDescriptor> browserSafelist = new ArrayList<>();
116-
try {
117-
new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, null);
118-
} catch (final ClientException exception) {
119-
assertNotNull(exception);
120-
assert (exception.getErrorCode().equalsIgnoreCase(ErrorStrings.NO_AVAILABLE_BROWSER_FOUND));
121-
}
116+
final Browser browser = new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, null);
117+
assertNull(browser);
122118
}
123119

124120
@Test
@@ -134,12 +130,9 @@ public void testSelect_versionNotSupported() {
134130
null)
135131
);
136132

137-
try {
138-
new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, null);
139-
} catch (final ClientException exception) {
140-
assertNotNull(exception);
141-
assert (exception.getErrorCode().equalsIgnoreCase(ErrorStrings.NO_AVAILABLE_BROWSER_FOUND));
142-
}
133+
134+
final Browser browser = new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, null);
135+
assertNull(browser);
143136
}
144137

145138
@Test
@@ -154,15 +147,11 @@ public void testSelect_customTabsNotSupported() {
154147
null,
155148
null)
156149
);
157-
158-
try {
159-
new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, null);
160-
} catch (final ClientException exception) {
161-
assertNotNull(exception);
162-
assert (exception.getErrorCode().equalsIgnoreCase(ErrorStrings.NO_AVAILABLE_BROWSER_FOUND));
163-
}
164-
150+
final Browser browser = new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, null);
151+
assertNotNull(browser);
152+
assertEquals(browser.getPackageName(), FIREFOX_NO_CUSTOM_TAB.mPackageName);
165153
}
154+
166155
@Test
167156
public void testSelect_preferredBrowserSelected() {
168157
setBrowserList(CHROME, DOLPHIN, FIREFOX);
@@ -189,15 +178,10 @@ public void testSelect_preferredBrowserSelected() {
189178
"10",
190179
null)
191180
);
181+
final Browser browser = new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, preferredBrowser);
182+
Assert.assertEquals(preferredBrowser.getPackageName(), browser.getPackageName());
183+
Assert.assertEquals(preferredBrowser.getSignatureHashes(), browser.getSignatureHashes());
192184

193-
194-
try {
195-
final Browser browser = new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, preferredBrowser);
196-
Assert.assertEquals(preferredBrowser.getPackageName(), browser.getPackageName());
197-
Assert.assertEquals(preferredBrowser.getSignatureHashes(), browser.getSignatureHashes());
198-
} catch (final ClientException exception) {
199-
Assert.fail();
200-
}
201185
}
202186

203187
@Test
@@ -227,14 +211,9 @@ public void testSelect_preferredBrowserSelected_preferredBrowserNotInstalled() {
227211
null)
228212
);
229213

230-
231-
try {
232-
// It should return the first installed browser.
233-
final Browser browser = new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, preferredBrowser);
234-
Assert.assertEquals(CHROME.mPackageName, browser.getPackageName());
235-
} catch (final ClientException exception) {
236-
Assert.fail();
237-
}
214+
// It should return the first installed browser.
215+
final Browser browser = new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, preferredBrowser);
216+
Assert.assertEquals(CHROME.mPackageName, browser.getPackageName());
238217
}
239218

240219
@Test
@@ -262,16 +241,10 @@ public void testSelect_preferredBrowserSelected_preferredBrowserNotInSafeList()
262241
"10",
263242
null)
264243
);
265-
266-
267-
try {
268-
// The safe list shouldn't matter, given that we've already specified all info in preferredBrowser's BrowserDescriptor.
269-
final Browser browser = new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, preferredBrowser);
270-
Assert.assertEquals(preferredBrowser.getPackageName(), browser.getPackageName());
271-
Assert.assertEquals(preferredBrowser.getSignatureHashes(), browser.getSignatureHashes());
272-
} catch (final ClientException exception) {
273-
Assert.fail();
274-
}
244+
// The safe list shouldn't matter, given that we've already specified all info in preferredBrowser's BrowserDescriptor.
245+
final Browser browser = new BrowserSelector(ApplicationProvider.getApplicationContext()).select(browserSafelist, preferredBrowser);
246+
Assert.assertEquals(preferredBrowser.getPackageName(), browser.getPackageName());
247+
Assert.assertEquals(preferredBrowser.getSignatureHashes(), browser.getSignatureHashes());
275248
}
276249

277250
/**

common/src/test/java/com/microsoft/identity/common/internal/util/AndroidAuthorizationStrategyFactoryTest.kt

Lines changed: 19 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -24,150 +24,73 @@
2424
package com.microsoft.identity.common.internal.util
2525

2626
import android.app.Activity
27-
import android.content.Intent
28-
import android.content.IntentFilter
29-
import android.content.pm.ActivityInfo
30-
import android.content.pm.ApplicationInfo
31-
import android.content.pm.PackageInfo
32-
import android.content.pm.ResolveInfo
33-
import android.content.pm.Signature
34-
import android.net.Uri
3527
import androidx.fragment.app.Fragment
3628
import com.microsoft.identity.common.internal.ui.AndroidAuthorizationStrategyFactory
3729
import com.microsoft.identity.common.internal.ui.browser.DefaultBrowserAuthorizationStrategy
3830
import com.microsoft.identity.common.internal.ui.webview.EmbeddedWebViewAuthorizationStrategy
39-
import com.microsoft.identity.common.java.commands.parameters.InteractiveTokenCommandParameters
31+
import com.microsoft.identity.common.java.browser.Browser
4032
import com.microsoft.identity.common.java.providers.oauth2.IAuthorizationStrategy
4133
import com.microsoft.identity.common.java.ui.AuthorizationAgent
42-
import com.microsoft.identity.common.java.ui.BrowserDescriptor
4334
import org.junit.Test
4435
import org.junit.runner.RunWith
4536
import org.mockito.Mockito.mock
46-
import org.mockito.Mockito.`when`
4737
import org.robolectric.RobolectricTestRunner
48-
import org.robolectric.Shadows
49-
import org.robolectric.annotation.Config
5038

5139
@RunWith(RobolectricTestRunner::class)
52-
// We need this to add the PackageManager.MATCH_DEFAULT_ONLY flag on BrowserSelector.getBrowsers
53-
// and to avoid a NPE on PackageHelper.getSignatures.
54-
@Config(sdk = [27])
5540
class AndroidAuthorizationStrategyFactoryTest {
5641

5742
companion object {
58-
private const val PACKAGE_NAME = "com.android.chrome"
59-
private const val BROWSER_NAME = "Chrome"
60-
private const val SIGNATURE_HASH_HEX = "3082010A0282010100C3B3A700D1E020302020034A7B8888"
61-
private const val SIGNATURE_HASH_BASE_64 =
62-
"Lu2NRuBdl7odm7sAKREJMShwDFWO7piPO_K69PxPWghQSaboLhOI2fvJt-Q17dW9NTgyPhOopWS6Cxgi9wrTew=="
63-
private const val VERSION_LOWER_BOUND = "0"
64-
private const val VERSION_UPPER_BOUND = "1"
65-
private val fakeBrowser = BrowserDescriptor(
66-
PACKAGE_NAME,
67-
SIGNATURE_HASH_BASE_64,
68-
VERSION_LOWER_BOUND,
69-
VERSION_UPPER_BOUND
70-
)
43+
private val browser = mock(Browser::class.java)
7144
}
7245

7346
@Test
74-
fun `test getAuthorizationStrategy with authorization agent WEBVIEW, empty browser safe list`() {
47+
fun `test getAuthorizationStrategy with authorization agent WEBVIEW`() {
7548
val strategy = getAuthorizationStrategy(
7649
authorizationAgent = AuthorizationAgent.WEBVIEW,
77-
browserSafeList = emptyList()
50+
browser = browser,
7851
)
7952
assert(strategy is EmbeddedWebViewAuthorizationStrategy)
8053
}
8154

8255
@Test
83-
fun `test getAuthorizationStrategy with authorization agent BROWSER, empty browser safe list`() {
56+
fun `test getAuthorizationStrategy with authorization agent WEBVIEW, browser null`() {
8457
val strategy = getAuthorizationStrategy(
85-
authorizationAgent = AuthorizationAgent.BROWSER,
86-
browserSafeList = emptyList()
58+
authorizationAgent = AuthorizationAgent.WEBVIEW,
59+
browser = null,
8760
)
8861
assert(strategy is EmbeddedWebViewAuthorizationStrategy)
8962
}
9063

9164
@Test
92-
fun `test getAuthorizationStrategy with authorization agent WEBVIEW, fakeBrowser in safe list`() {
65+
fun `test getAuthorizationStrategy with authorization agent BROWSER`() {
9366
val strategy = getAuthorizationStrategy(
94-
authorizationAgent = AuthorizationAgent.WEBVIEW,
95-
browserSafeList = listOf(fakeBrowser)
67+
authorizationAgent = AuthorizationAgent.BROWSER,
68+
browser = browser,
9669
)
97-
assert(strategy is EmbeddedWebViewAuthorizationStrategy)
70+
assert(strategy is DefaultBrowserAuthorizationStrategy)
9871
}
9972

73+
10074
@Test
101-
fun `test getAuthorizationStrategy with authorization agent BROWSER, fakeBrowser in safe list`() {
75+
fun `test getAuthorizationStrategy with authorization agent BROWSER, browser null`() {
10276
val strategy = getAuthorizationStrategy(
10377
authorizationAgent = AuthorizationAgent.BROWSER,
104-
browserSafeList = listOf(fakeBrowser)
78+
browser = null,
10579
)
106-
assert(strategy is DefaultBrowserAuthorizationStrategy)
80+
assert(strategy is EmbeddedWebViewAuthorizationStrategy)
10781
}
10882

10983
private fun getAuthorizationStrategy(
110-
browserSafeList: List<BrowserDescriptor>,
111-
authorizationAgent: AuthorizationAgent
84+
browser: Browser?,
85+
authorizationAgent: AuthorizationAgent,
11286
): IAuthorizationStrategy<*, *> {
113-
114-
val context = org.robolectric.RuntimeEnvironment.getApplication()
115-
val shadowPackageManager = Shadows.shadowOf(context.packageManager)
116-
117-
// Define a mock browser package
118-
val browserPackageInfo = PackageInfo().apply {
119-
packageName = PACKAGE_NAME
120-
versionName = "1.0"
121-
// Add signatures
122-
signatures = arrayOf(
123-
Signature(SIGNATURE_HASH_HEX)
124-
)
125-
applicationInfo = ApplicationInfo().apply {
126-
this.packageName = PACKAGE_NAME
127-
this.name = BROWSER_NAME
128-
}
129-
}
130-
131-
// Add the browser package to the shadow PackageManager
132-
shadowPackageManager.installPackage(browserPackageInfo)
133-
134-
// Create a mock ResolveInfo for browser activities
135-
val browserResolveInfo = ResolveInfo().apply {
136-
activityInfo = ActivityInfo().apply {
137-
packageName = PACKAGE_NAME
138-
name = BROWSER_NAME
139-
}
140-
isDefault = true
141-
142-
// Add a filter for handling specific intents (e.g., http/https URLs)
143-
filter = IntentFilter().apply {
144-
addAction(Intent.ACTION_VIEW) // Action to view content
145-
addCategory(Intent.CATEGORY_BROWSABLE) // Default category
146-
addDataScheme("http") // Filter for http URLs
147-
addDataScheme("https") // Filter for https URLs
148-
}
149-
}
150-
151-
// Add the browser to handle VIEW intents with http/https schemes
152-
shadowPackageManager.addResolveInfoForIntent(
153-
Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")),
154-
browserResolveInfo
155-
)
156-
15787
// Construct the factory
15888
val strategyFactory = AndroidAuthorizationStrategyFactory.builder()
159-
.context(context)
89+
.context(org.robolectric.RuntimeEnvironment.getApplication())
16090
.activity(mock(Activity::class.java))
16191
.fragment(mock(Fragment::class.java))
16292
.build()
16393

164-
// Mock the parameters
165-
val params: InteractiveTokenCommandParameters =
166-
mock(InteractiveTokenCommandParameters::class.java)
167-
`when`(params.browserSafeList).thenReturn(browserSafeList)
168-
`when`(params.preferredBrowser).thenReturn(null)
169-
`when`(params.authorizationAgent).thenReturn(authorizationAgent)
170-
171-
return strategyFactory.getAuthorizationStrategy(params)
94+
return strategyFactory.getAuthorizationStrategy(authorizationAgent, browser, true)
17295
}
17396
}

common4j/src/main/com/microsoft/identity/common/java/exception/ErrorStrings.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,6 @@ private ErrorStrings() {
310310
*/
311311
public static final String AUTHORIZATION_INTENT_IS_NULL = "Authorization intent is null.";
312312

313-
/**
314-
* No available browser installed on the device.
315-
*/
316-
public static final String NO_AVAILABLE_BROWSER_FOUND = "No available browser installed on the device.";
317-
318313
/**
319314
* Refresh token request failed.
320315
*/

0 commit comments

Comments
 (0)