summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java
blob: d7f41bf0415d30a211758b1ac35ade3ba50da69c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * 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("^("
                    + "(?<local>(BuildFromAndroidStudio|"
                    + "([0-9]+|[A-Z])-eng\\.[a-z]+\\.[0-9]+\\.[0-9]+))|"
                    + "(?<presubmit>([0-9]+|[A-Z])-P[0-9]+)|"
                    + "(?<postsubmit>([0-9]+|[A-Z])-[0-9]+)|"
                    + "(?<platform>[0-9]+|[A-Z])"
                    + ")$");
    private static final Pattern PLATFORM_BUILD =
            Pattern.compile("^("
                    + "(?<commandLine>eng\\.[a-z]+\\.[0-9]+\\.[0-9]+)|"
                    + "(?<presubmit>P[0-9]+)|"
                    + "(?<postsubmit>[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 {
                    getRunFlavor();

                    base.evaluate();
                }
            };
        } else {
            return base;
        }
    }

    private static void getRunFlavor() throws Exception {
        final String launcherVersion = getInstrumentation().
                getContext().
                getPackageManager().
                getPackageInfo(
                        UiDevice.getInstance(getInstrumentation()).
                                getLauncherPackageName(),
                        0).
                versionName;

        final Matcher launcherBuildMatcher = LAUNCHER_BUILD.matcher(launcherVersion);

        if (!launcherBuildMatcher.find()) {
            Log.e(TAG, "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");
        }

        Log.d(TAG, "Launcher: " + launcherVersion + ", platform: " + platformVersion);

        if (launcherBuildMatcher.group("local") != null && (
                platformBuildMatcher.group("commandLine") != null ||
                        platformBuildMatcher.group("postsubmit") != null)) {
            Log.d(TAG, "LOCAL RUN");
        } else if (launcherBuildMatcher.group("presubmit") != null
                && platformBuildMatcher.group("postsubmit") != null) {
            Log.d(TAG, "UNBUNDLED PRESUBMIT");
        } else if (launcherBuildMatcher.group("postsubmit") != null
                && platformBuildMatcher.group("postsubmit") != null) {
            Log.d(TAG, "UNBUNDLED POSTSUBMIT");
        } else if (launcherBuildMatcher.group("platform") != null
                && platformBuildMatcher.group("presubmit") != null) {
            Log.d(TAG, "PLATFORM PRESUBMIT");
        } else if (launcherBuildMatcher.group("platform") != null
                && platformBuildMatcher.group("postsubmit") != null) {
            Log.d(TAG, "PLATFORM POSTSUBMIT");
        } else {
            Log.e(TAG, "ERROR3");
        }
    }
}