Implementing test stability attribute

Change-Id: I5d78a19039ab2bcdb217886fdaca29e3df412780
diff --git a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
index e7a5de0..5e87612 100644
--- a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
+++ b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
@@ -24,8 +24,6 @@
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
-import android.util.Log;
-
 import androidx.test.filters.LargeTest;
 import androidx.test.runner.AndroidJUnit4;
 
@@ -38,7 +36,6 @@
 import com.android.launcher3.tapl.AppIconMenuItem;
 import com.android.launcher3.tapl.Widgets;
 import com.android.launcher3.tapl.Workspace;
-import com.android.launcher3.util.rule.TestStabilityRule.Stability;
 import com.android.launcher3.views.OptionsPopupView;
 import com.android.launcher3.widget.WidgetsFullSheet;
 import com.android.launcher3.widget.WidgetsRecyclerView;
@@ -362,10 +359,4 @@
     public static String getAppPackageName() {
         return getInstrumentation().getContext().getPackageName();
     }
-
-    @Test
-    @Stability
-    public void testTestStabilityAttribute() {
-        Log.d("TestStabilityRule", "Hello world!");
-    }
 }
diff --git a/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java b/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java
index d7f41bf..69bf01d 100644
--- a/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java
+++ b/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java
@@ -17,6 +17,7 @@
 
 import static androidx.test.InstrumentationRegistry.getInstrumentation;
 
+import android.content.pm.PackageManager;
 import android.os.Build;
 import android.util.Log;
 
@@ -50,20 +51,33 @@
                     + "(?<postsubmit>[0-9]+)"
                     + ")$");
 
+    public static final int LOCAL = 0x1;
+    public static final int UNBUNDLED_PRESUBMIT = 0x2;
+    public static final int UNBUNDLED_POSTSUBMIT = 0x4;
+    public static final int PLATFORM_PRESUBMIT = 0x8;
+    public static final int PLATFORM_POSTSUBMIT = 0x10;
+
+    private static final int RUN_FLAFOR = getRunFlavor();
+
     @Retention(RetentionPolicy.RUNTIME)
     @Target(ElementType.METHOD)
     public @interface Stability {
+        int flavors();
     }
 
     @Override
     public Statement apply(Statement base, Description description) {
-        if (description.getAnnotation(Stability.class) != null) {
+        final Stability stability = description.getAnnotation(Stability.class);
+        if (stability != null) {
             return new Statement() {
                 @Override
                 public void evaluate() throws Throwable {
-                    getRunFlavor();
-
-                    base.evaluate();
+                    if ((stability.flavors() & RUN_FLAFOR) != 0) {
+                        Log.d(TAG, "Running " + description.getDisplayName());
+                        base.evaluate();
+                    } else {
+                        Log.d(TAG, "Skipping " + description.getDisplayName());
+                    }
                 }
             };
         } else {
@@ -71,49 +85,63 @@
         }
     }
 
-    private static void getRunFlavor() throws Exception {
-        final String launcherVersion = getInstrumentation().
-                getContext().
-                getPackageManager().
-                getPackageInfo(
-                        UiDevice.getInstance(getInstrumentation()).
-                                getLauncherPackageName(),
-                        0).
-                versionName;
+    private static int getRunFlavor() {
+        final String launcherVersion;
+        try {
+            launcherVersion = getInstrumentation().
+                    getContext().
+                    getPackageManager().
+                    getPackageInfo(
+                            UiDevice.getInstance(getInstrumentation()).
+                                    getLauncherPackageName(),
+                            0).
+                    versionName;
+        } catch (PackageManager.NameNotFoundException e) {
+            throw new RuntimeException(e);
+        }
 
         final Matcher launcherBuildMatcher = LAUNCHER_BUILD.matcher(launcherVersion);
 
         if (!launcherBuildMatcher.find()) {
-            Log.e(TAG, "Match not found");
+            throw new AssertionError("Launcher build match not found");
         }
 
         final String platformVersion = Build.VERSION.INCREMENTAL;
         final Matcher platformBuildMatcher = PLATFORM_BUILD.matcher(platformVersion);
 
         if (!platformBuildMatcher.find()) {
-            Log.e(TAG, "Match not found");
+            throw new AssertionError("Platform build match not found");
         }
 
         Log.d(TAG, "Launcher: " + launcherVersion + ", platform: " + platformVersion);
 
+        final int runFlavor;
+
         if (launcherBuildMatcher.group("local") != null && (
                 platformBuildMatcher.group("commandLine") != null ||
                         platformBuildMatcher.group("postsubmit") != null)) {
             Log.d(TAG, "LOCAL RUN");
+            runFlavor = LOCAL;
         } else if (launcherBuildMatcher.group("presubmit") != null
                 && platformBuildMatcher.group("postsubmit") != null) {
             Log.d(TAG, "UNBUNDLED PRESUBMIT");
+            runFlavor = UNBUNDLED_PRESUBMIT;
         } else if (launcherBuildMatcher.group("postsubmit") != null
                 && platformBuildMatcher.group("postsubmit") != null) {
             Log.d(TAG, "UNBUNDLED POSTSUBMIT");
+            runFlavor = UNBUNDLED_POSTSUBMIT;
         } else if (launcherBuildMatcher.group("platform") != null
                 && platformBuildMatcher.group("presubmit") != null) {
             Log.d(TAG, "PLATFORM PRESUBMIT");
+            runFlavor = PLATFORM_PRESUBMIT;
         } else if (launcherBuildMatcher.group("platform") != null
                 && platformBuildMatcher.group("postsubmit") != null) {
             Log.d(TAG, "PLATFORM POSTSUBMIT");
+            runFlavor = PLATFORM_POSTSUBMIT;
         } else {
-            Log.e(TAG, "ERROR3");
+            throw new AssertionError("Unrecognized run flavor");
         }
+
+        return runFlavor;
     }
 }