/* * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.android.launcher3.util.rule; import static androidx.test.InstrumentationRegistry.getInstrumentation; import android.os.Build; import android.util.Log; import androidx.test.uiautomator.UiDevice; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestStabilityRule implements TestRule { private static final String TAG = "TestStabilityRule"; private static final Pattern LAUNCHER_BUILD = Pattern.compile("^(" + "(?BuildFromAndroidStudio)|" + "(?[0-9]+-eng\\.[a-z]+\\.[0-9]+\\.[0-9]+)|" + "(?[0-9]+-P[0-9]+)|" + "(?[0-9]+-[0-9]+|" + "(?[0-9]+))" + ")$"); private static final Pattern PLATFORM_BUILD = Pattern.compile("^(" + "(?eng\\.[a-z]+\\.[0-9]+\\.[0-9]+)|" + "(?P[0-9]+)|" + "(?[0-9]+)" + ")$"); @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Stability { } @Override public Statement apply(Statement base, Description description) { if (description.getAnnotation(Stability.class) != null) { return new Statement() { @Override public void evaluate() throws Throwable { final String launcherVersion = getInstrumentation(). getContext(). getPackageManager(). getPackageInfo( UiDevice.getInstance(getInstrumentation()). getLauncherPackageName(), 0). versionName; final Matcher launcherBuildMatcher = LAUNCHER_BUILD.matcher(launcherVersion); boolean launcherLocalBuild = false; boolean launcherUnbundledPresubmit = false; boolean launcherUnbundledPostsubmit = false; boolean launcherPlatform = false; if (!launcherBuildMatcher.find()) { Log.e(TAG, "Match not found"); } else if (launcherBuildMatcher.group("androidStudio") != null || launcherBuildMatcher.group("commandLine") != null) { launcherLocalBuild = true; } else if (launcherBuildMatcher.group("presubmit") != null) { launcherUnbundledPresubmit = true; } else if (launcherBuildMatcher.group("postsubmit") != null) { launcherUnbundledPostsubmit = true; } else if (launcherBuildMatcher.group("platform") != null) { launcherPlatform = true; } else { Log.e(TAG, "ERROR1"); } boolean platformLocalBuild = false; boolean platformPresubmit = false; boolean platformPostsubmit = false; final String platformVersion = Build.VERSION.INCREMENTAL; final Matcher platformBuildMatcher = PLATFORM_BUILD.matcher(platformVersion); if (!platformBuildMatcher.find()) { Log.e(TAG, "Match not found"); } else if (platformBuildMatcher.group("commandLine") != null) { platformLocalBuild = true; } else if (platformBuildMatcher.group("presubmit") != null) { platformPresubmit = true; } else if (platformBuildMatcher.group("postsubmit") != null) { platformPostsubmit = true; } else { Log.e(TAG, "ERROR2"); } Log.d(TAG, "Launcher: " + launcherVersion + ", platform: " + platformVersion); if (launcherLocalBuild && (platformLocalBuild || platformPostsubmit)) { Log.d(TAG, "LOCAL RUN"); } else if (launcherUnbundledPresubmit && platformPostsubmit) { Log.d(TAG, "UNBUNDLED PRESUBMIT"); } else if (launcherUnbundledPostsubmit && platformPostsubmit) { Log.d(TAG, "UNBUNDLED POSTSUBMIT"); } else if (launcherPlatform && platformPresubmit) { Log.d(TAG, "PLATFORM PRESUBMIT"); } else if (launcherPlatform && platformPostsubmit) { Log.d(TAG, "PLATFORM POSTSUBMIT"); } else { Log.e(TAG, "ERROR3"); } base.evaluate(); } }; } else { return base; } } }