1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.launcher3.util.rule; 17 18 import static androidx.test.InstrumentationRegistry.getInstrumentation; 19 20 import android.os.Build; 21 import android.util.Log; 22 23 import androidx.test.uiautomator.UiDevice; 24 25 import org.junit.rules.TestRule; 26 import org.junit.runner.Description; 27 import org.junit.runners.model.Statement; 28 29 import java.lang.annotation.ElementType; 30 import java.lang.annotation.Retention; 31 import java.lang.annotation.RetentionPolicy; 32 import java.lang.annotation.Target; 33 import java.util.regex.Matcher; 34 import java.util.regex.Pattern; 35 36 public class TestStabilityRule implements TestRule { 37 private static final String TAG = "TestStabilityRule"; 38 private static final Pattern LAUNCHER_BUILD = 39 Pattern.compile("^(" 40 + "(?<local>(BuildFromAndroidStudio|" 41 + "([0-9]+|[A-Z])-eng\\.[a-z]+\\.[0-9]+\\.[0-9]+))|" 42 + "(?<presubmit>([0-9]+|[A-Z])-P[0-9]+)|" 43 + "(?<postsubmit>([0-9]+|[A-Z])-[0-9]+)|" 44 + "(?<platform>[0-9]+|[A-Z])" 45 + ")$"); 46 private static final Pattern PLATFORM_BUILD = 47 Pattern.compile("^(" 48 + "(?<commandLine>eng\\.[a-z]+\\.[0-9]+\\.[0-9]+)|" 49 + "(?<presubmit>P[0-9]+)|" 50 + "(?<postsubmit>[0-9]+)" 51 + ")$"); 52 53 @Retention(RetentionPolicy.RUNTIME) 54 @Target(ElementType.METHOD) 55 public @interface Stability { 56 } 57 58 @Override apply(Statement base, Description description)59 public Statement apply(Statement base, Description description) { 60 if (description.getAnnotation(Stability.class) != null) { 61 return new Statement() { 62 @Override 63 public void evaluate() throws Throwable { 64 getRunFlavor(); 65 66 base.evaluate(); 67 } 68 }; 69 } else { 70 return base; 71 } 72 } 73 74 private static void getRunFlavor() throws Exception { 75 final String launcherVersion = getInstrumentation(). 76 getContext(). 77 getPackageManager(). 78 getPackageInfo( 79 UiDevice.getInstance(getInstrumentation()). 80 getLauncherPackageName(), 81 0). 82 versionName; 83 84 final Matcher launcherBuildMatcher = LAUNCHER_BUILD.matcher(launcherVersion); 85 86 if (!launcherBuildMatcher.find()) { 87 Log.e(TAG, "Match not found"); 88 } 89 90 final String platformVersion = Build.VERSION.INCREMENTAL; 91 final Matcher platformBuildMatcher = PLATFORM_BUILD.matcher(platformVersion); 92 93 if (!platformBuildMatcher.find()) { 94 Log.e(TAG, "Match not found"); 95 } 96 97 Log.d(TAG, "Launcher: " + launcherVersion + ", platform: " + platformVersion); 98 99 if (launcherBuildMatcher.group("local") != null && ( 100 platformBuildMatcher.group("commandLine") != null || 101 platformBuildMatcher.group("postsubmit") != null)) { 102 Log.d(TAG, "LOCAL RUN"); 103 } else if (launcherBuildMatcher.group("presubmit") != null 104 && platformBuildMatcher.group("postsubmit") != null) { 105 Log.d(TAG, "UNBUNDLED PRESUBMIT"); 106 } else if (launcherBuildMatcher.group("postsubmit") != null 107 && platformBuildMatcher.group("postsubmit") != null) { 108 Log.d(TAG, "UNBUNDLED POSTSUBMIT"); 109 } else if (launcherBuildMatcher.group("platform") != null 110 && platformBuildMatcher.group("presubmit") != null) { 111 Log.d(TAG, "PLATFORM PRESUBMIT"); 112 } else if (launcherBuildMatcher.group("platform") != null 113 && platformBuildMatcher.group("postsubmit") != null) { 114 Log.d(TAG, "PLATFORM POSTSUBMIT"); 115 } else { 116 Log.e(TAG, "ERROR3"); 117 } 118 } 119 } 120