diff options
Diffstat (limited to 'tests/src/com/android/launcher3/util/rule')
7 files changed, 164 insertions, 84 deletions
diff --git a/tests/src/com/android/launcher3/util/rule/FailureWatcher.java b/tests/src/com/android/launcher3/util/rule/FailureWatcher.java index 62d70ad1cd..10b428a111 100644 --- a/tests/src/com/android/launcher3/util/rule/FailureWatcher.java +++ b/tests/src/com/android/launcher3/util/rule/FailureWatcher.java @@ -59,8 +59,9 @@ public class FailureWatcher extends TestWatcher { throw new AssertionError( "Launcher received events not sent by the test. This may mean " + "that the touch screen of the lab device has sent false" - + " events. See the logcat for TaplEvents tag and look " - + "for events with deviceId != -1"); + + " events. See the logcat for " + + "TaplEvents|LauncherEvents|TaplTarget tag and look for " + + "events with deviceId != -1"); } } } diff --git a/tests/src/com/android/launcher3/util/rule/SetFlagsRuleExt.kt b/tests/src/com/android/launcher3/util/rule/SetFlagsRuleExt.kt new file mode 100644 index 0000000000..d6824561c1 --- /dev/null +++ b/tests/src/com/android/launcher3/util/rule/SetFlagsRuleExt.kt @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2008 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 android.platform.test.flag.junit.SetFlagsRule + +fun SetFlagsRule.setFlags(enabled: Boolean, vararg flagName: String) { + if (enabled) enableFlags(*flagName) else disableFlags(*flagName) +} diff --git a/tests/src/com/android/launcher3/util/rule/StaticMockitoRule.java b/tests/src/com/android/launcher3/util/rule/StaticMockitoRule.java new file mode 100644 index 0000000000..6b91474f02 --- /dev/null +++ b/tests/src/com/android/launcher3/util/rule/StaticMockitoRule.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2022 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 com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; + +import com.android.dx.mockito.inline.extended.StaticMockitoSession; +import com.android.dx.mockito.inline.extended.StaticMockitoSessionBuilder; + +import org.junit.rules.MethodRule; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.Statement; +import org.mockito.junit.MockitoRule; +import org.mockito.quality.Strictness; + +/** + * Similar to {@link MockitoRule}, but uses {@link StaticMockitoSession}, which allows mocking + * static methods. + */ +public class StaticMockitoRule implements MethodRule { + private Class<?>[] mClasses; + + public StaticMockitoRule(Class<?>... classes) { + mClasses = classes; + } + + @Override + public Statement apply(Statement base, FrameworkMethod method, Object target) { + return new Statement() { + public void evaluate() throws Throwable { + StaticMockitoSessionBuilder builder = + mockitoSession() + .name(target.getClass().getSimpleName() + "." + method.getName()) + .initMocks(target) + .strictness(Strictness.STRICT_STUBS); + + for (Class<?> clazz : mClasses) { + builder.mockStatic(clazz); + } + + StaticMockitoSession session = builder.startMocking(); + Throwable testFailure = evaluateSafely(base); + session.finishMocking(testFailure); + if (testFailure != null) { + throw testFailure; + } + } + + private Throwable evaluateSafely(Statement base) { + try { + base.evaluate(); + return null; + } catch (Throwable throwable) { + return throwable; + } + } + }; + } +} diff --git a/tests/src/com/android/launcher3/util/rule/TISBindRule.java b/tests/src/com/android/launcher3/util/rule/TISBindRule.java deleted file mode 100644 index 3ec4a29cdf..0000000000 --- a/tests/src/com/android/launcher3/util/rule/TISBindRule.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2023 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 android.app.UiAutomation; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; -import android.os.IBinder; -import android.util.Log; - -import androidx.annotation.NonNull; -import androidx.test.platform.app.InstrumentationRegistry; - -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -public class TISBindRule implements TestRule { - public static String TAG = "TISBindRule"; - public static String INTENT_FILTER = "android.intent.action.QUICKSTEP_SERVICE"; - public static String TIS_PERMISSIONS = "android.permission.STATUS_BAR_SERVICE"; - - private String getLauncherPackageName(Context context) { - return ComponentName.unflattenFromString(context.getString( - com.android.internal.R.string.config_recentsComponentName)).getPackageName(); - } - - private ServiceConnection createConnection() { - return new ServiceConnection() { - @Override - public void onServiceConnected(ComponentName componentName, IBinder iBinder) { - Log.d(TAG, "Connected to TouchInteractionService"); - } - - @Override - public void onServiceDisconnected(ComponentName componentName) { - Log.d(TAG, "Disconnected from TouchInteractionService"); - } - }; - } - - @NonNull - @Override - public Statement apply(@NonNull Statement base, @NonNull Description description) { - return new Statement() { - - @Override - public void evaluate() throws Throwable { - Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); - final ServiceConnection connection = createConnection(); - UiAutomation uiAutomation = - InstrumentationRegistry.getInstrumentation().getUiAutomation(); - uiAutomation.adoptShellPermissionIdentity(TIS_PERMISSIONS); - Intent launchIntent = new Intent(INTENT_FILTER); - launchIntent.setPackage(getLauncherPackageName(context)); - context.bindService(launchIntent, connection, Context.BIND_AUTO_CREATE); - uiAutomation.dropShellPermissionIdentity(); - try { - base.evaluate(); - } finally { - context.unbindService(connection); - } - } - }; - } -} diff --git a/tests/src/com/android/launcher3/util/rule/TestIsolationRule.java b/tests/src/com/android/launcher3/util/rule/TestIsolationRule.java new file mode 100644 index 0000000000..2b45902813 --- /dev/null +++ b/tests/src/com/android/launcher3/util/rule/TestIsolationRule.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2023 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 androidx.annotation.NonNull; +import androidx.test.InstrumentationRegistry; +import androidx.test.uiautomator.UiDevice; + +import com.android.launcher3.tapl.LauncherInstrumentation; +import com.android.launcher3.ui.AbstractLauncherUiTest; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * Isolates tests from some of the state created by the previous test. + */ +public class TestIsolationRule implements TestRule { + private final LauncherInstrumentation mLauncher; + private final boolean mRequireOneActiveActivity; + + public TestIsolationRule(LauncherInstrumentation launcher, boolean requireOneActiveActivity) { + mLauncher = launcher; + mRequireOneActiveActivity = requireOneActiveActivity; + } + + @NonNull + @Override + public Statement apply(@NonNull Statement base, @NonNull Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + base.evaluate(); + // Make sure that Launcher workspace looks correct. + + UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressHome(); + AbstractLauncherUiTest.checkDetectedLeaks(mLauncher, mRequireOneActiveActivity); + } + }; + } +} diff --git a/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java b/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java index 38de07192a..b51045fc09 100644 --- a/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java +++ b/tests/src/com/android/launcher3/util/rule/TestStabilityRule.java @@ -146,4 +146,8 @@ public class TestStabilityRule implements TestRule { return sRunFlavor; } + + public static boolean isPresubmit() { + return getRunFlavor() == PLATFORM_PRESUBMIT; + } } diff --git a/tests/src/com/android/launcher3/util/rule/ViewCaptureRule.kt b/tests/src/com/android/launcher3/util/rule/ViewCaptureRule.kt index ccbae4fb0c..e70ea18f7a 100644 --- a/tests/src/com/android/launcher3/util/rule/ViewCaptureRule.kt +++ b/tests/src/com/android/launcher3/util/rule/ViewCaptureRule.kt @@ -132,7 +132,9 @@ class ViewCaptureRule(var alreadyOpenActivitySupplier: Supplier<Activity?>) : Te for (i in 0 until viewCaptureData!!.windowDataCount) { frameCount += viewCaptureData!!.getWindowData(i).frameDataCount } - assertTrue("Empty ViewCapture data", frameCount > 0) + + val mayProduceNoFrames = description.getAnnotation(MayProduceNoFrames::class.java) != null + assertTrue("Empty ViewCapture data", mayProduceNoFrames || frameCount > 0) val anomalies: Map<String, String> = ViewCaptureAnalyzer.getAnomalies(viewCaptureData) if (!anomalies.isEmpty()) { @@ -159,4 +161,8 @@ class ViewCaptureRule(var alreadyOpenActivitySupplier: Supplier<Activity?>) : Te ) } } + + @Retention(AnnotationRetention.RUNTIME) + @Target(AnnotationTarget.FUNCTION) + annotation class MayProduceNoFrames } |