Fixes MatchFlagTests for non-browser devices

This change modifies the MatchFlagTests to pass on non-browser devices
by branching the verification based on whether devices exist

Fixes: 158095408
Test: atest MatchFlagTests
Change-Id: Id0b57da76e2c069ab53ee9370b58b76b663f00f4
diff --git a/tests/tests/match_flags/Android.bp b/tests/tests/match_flags/Android.bp
index 900f3e9..b29d3e0 100644
--- a/tests/tests/match_flags/Android.bp
+++ b/tests/tests/match_flags/Android.bp
@@ -28,5 +28,5 @@
     ],
 
     srcs: ["src/**/*.java"],
-    sdk_version: "test_current",
+    sdk_version: "system_current",
 }
diff --git a/tests/tests/match_flags/AndroidManifest.xml b/tests/tests/match_flags/AndroidManifest.xml
index 1b6cb42..2878bd5 100644
--- a/tests/tests/match_flags/AndroidManifest.xml
+++ b/tests/tests/match_flags/AndroidManifest.xml
@@ -18,6 +18,8 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="android.matchflags.cts">
 
+    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
+
     <application>
       <uses-library android:name="android.test.runner" />
     </application>
diff --git a/tests/tests/match_flags/src/android/matchflags/cts/MatchFlagTests.java b/tests/tests/match_flags/src/android/matchflags/cts/MatchFlagTests.java
index ab6712a..74024df 100644
--- a/tests/tests/match_flags/src/android/matchflags/cts/MatchFlagTests.java
+++ b/tests/tests/match_flags/src/android/matchflags/cts/MatchFlagTests.java
@@ -16,43 +16,21 @@
 
 package android.matchflags.cts;
 
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
 
 import android.content.ActivityNotFoundException;
-import android.content.ComponentName;
-import android.content.Context;
 import android.content.Intent;
-import android.content.pm.PackageInfo;
-import android.content.pm.PackageManager;
 import android.net.Uri;
-import android.os.Bundle;
-import android.os.ConditionVariable;
-import android.os.Handler;
-import android.os.HandlerThread;
-import android.os.RemoteCallback;
 
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 import androidx.test.platform.app.InstrumentationRegistry;
 
-import com.android.compatibility.common.util.SystemUtil;
-
-import org.hamcrest.core.IsNull;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TestName;
 import org.junit.runner.RunWith;
 
-import java.util.Objects;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-import java.util.concurrent.atomic.AtomicReference;
-
 @RunWith(AndroidJUnit4.class)
 public class MatchFlagTests {
 
@@ -74,7 +52,16 @@
                 .addCategory(Intent.CATEGORY_BROWSABLE)
                 .setData(Uri.parse(ONLY_BROWSER_URI));
 
-        startActivity(onlyBrowserIntent);
+        if (isBrowserPresent()) {
+            startActivity(onlyBrowserIntent);
+        } else {
+            try {
+                startActivity(onlyBrowserIntent);
+                fail("Device without browser should not launch browser only intent");
+            } catch (ActivityNotFoundException e) {
+                // hooray
+            }
+        }
 
         Intent noBrowserWithBrowserOnlyIntent = new Intent(onlyBrowserIntent)
                 .addFlags(Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER);
@@ -123,16 +110,30 @@
                 .addFlags(Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER)
                 .addFlags(Intent.FLAG_ACTIVITY_REQUIRE_DEFAULT);
 
-        // with non-browser, we'd expect the resolver
-        // with require default, we'll get activity not found
-        try {
+        if (isBrowserPresent()) {
+            // with non-browser, we'd expect the resolver
+            // with require default, we'll get activity not found
+            try {
+                startActivity(uniqueUriIntentNoBrowserRequireDefault);
+                fail("Should fail to launch when started with non-browser and require default");
+            } catch (ActivityNotFoundException e) {
+                // hooray!
+            }
+        } else {
+            // with non-browser, but no browser present, we'd get a single result
+            // with require default, we'll resolve to that single result
             startActivity(uniqueUriIntentNoBrowserRequireDefault);
-            fail("Should fail to launch when started with non-browser and require default");
-        } catch (ActivityNotFoundException e) {
-            // hooray!
         }
     }
 
+    private static boolean isBrowserPresent() {
+        return InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager()
+                .queryIntentActivities(new Intent(Intent.ACTION_VIEW).addCategory(
+                        Intent.CATEGORY_BROWSABLE).setData(Uri.parse(ONLY_BROWSER_URI)),
+                        0 /* flags */)
+                .stream().anyMatch(resolveInfo -> resolveInfo.handleAllWebDataURI);
+    }
+
     private static void startActivity(Intent onlyBrowserIntent) {
         InstrumentationRegistry.getInstrumentation().getContext().startActivity(
                 onlyBrowserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));