summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAneesh Kher <kaneesh@google.com>2023-06-20 19:00:30 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2023-06-20 19:00:30 +0000
commitfe139fa023d6094b18dca20e815ad09765d2e3a7 (patch)
tree4bb3776de766c2f7cc49f0490e42bcfd481d28ff
parent8d9037066fe6d4a8b3d5020992b1f0ff72f61332 (diff)
parentc2c31c5aac3b9f9f0796775db2c03ab177f9cc3b (diff)
downloadplatform_testing-fe139fa023d6094b18dca20e815ad09765d2e3a7.tar.gz
Add hot and cold start up specific JUnit rules am: c2c31c5aac
Original change: https://googleplex-android-review.googlesource.com/c/platform/platform_testing/+/23732868 Change-Id: Ie7ce770774226f76ba76626fd0564020de0e4f5b Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--tests/automotive/health/rules/src/android/platform/scenario/ColdAppStartupRunRule.java41
-rw-r--r--tests/automotive/health/rules/src/android/platform/scenario/HotAppStartupRunRule.java76
-rw-r--r--tests/automotive/health/rules/src/android/platform/scenario/SleepAtTestStartRule.java41
3 files changed, 158 insertions, 0 deletions
diff --git a/tests/automotive/health/rules/src/android/platform/scenario/ColdAppStartupRunRule.java b/tests/automotive/health/rules/src/android/platform/scenario/ColdAppStartupRunRule.java
new file mode 100644
index 000000000..5f227f012
--- /dev/null
+++ b/tests/automotive/health/rules/src/android/platform/scenario/ColdAppStartupRunRule.java
@@ -0,0 +1,41 @@
+/*
+ * 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 android.platform.test.scenario;
+
+import android.platform.helpers.IAppHelper;
+import android.platform.test.rule.DropCachesRule;
+import android.platform.test.rule.KillAppsRule;
+
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+public class ColdAppStartupRunRule<T extends IAppHelper> implements TestRule {
+ private final RuleChain mRuleChain;
+
+ public ColdAppStartupRunRule(T appHelper) {
+ mRuleChain =
+ RuleChain.outerRule(new KillAppsRule(appHelper.getPackage()))
+ .around(new DropCachesRule())
+ .around(new SleepAtTestStartRule(3000))
+ .around(new SleepAtTestFinishRule(3000));
+ }
+
+ public Statement apply(final Statement base, final Description description) {
+ return mRuleChain.apply(base, description);
+ }
+}
diff --git a/tests/automotive/health/rules/src/android/platform/scenario/HotAppStartupRunRule.java b/tests/automotive/health/rules/src/android/platform/scenario/HotAppStartupRunRule.java
new file mode 100644
index 000000000..273a3b28c
--- /dev/null
+++ b/tests/automotive/health/rules/src/android/platform/scenario/HotAppStartupRunRule.java
@@ -0,0 +1,76 @@
+/*
+ * 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 android.platform.test.scenario;
+
+import android.platform.helpers.IAppHelper;
+import android.platform.test.rule.TestWatcher;
+import android.util.Log;
+
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+public class HotAppStartupRunRule<T extends IAppHelper> implements TestRule {
+ private final RuleChain mRuleChain;
+
+ public HotAppStartupRunRule(T appHelper) {
+ mRuleChain =
+ RuleChain.outerRule(new SwitchOutAppRule())
+ .around(new SleepAtTestStartRule(2000))
+ .around(new SleepAtTestFinishRule(3000));
+ }
+
+ public Statement apply(final Statement base, final Description description) {
+ return mRuleChain.apply(base, description);
+ }
+
+ // Custom rule to move away from app under test
+ private static class SwitchOutAppRule extends TestWatcher {
+ private static final String GO_HOME_PARAM_NAME = "go-home";
+ private static final String GO_HOME_DEFAULT = "true";
+ private static final String LAUNCHER_PARAM_NAME = "app-package";
+ private static final String CARLAUNCHER_PACKAGE = "com.android.car.carlauncher";
+ private static final String APP_ACTIVITY_PARAM_NAME = "app-activity";
+ private static final String APP_GRID_ACTIVITY =
+ "com.android.car.carlauncher.AppGridActivity";
+ private static final String LOG_TAG = SwitchOutAppRule.class.getSimpleName();
+
+ private boolean mGoHome;
+ private String mAppPackage;
+ private String mAppActivity;
+
+ @Override
+ protected void starting(Description description) {
+ mGoHome =
+ Boolean.parseBoolean(
+ getArguments().getString(GO_HOME_PARAM_NAME, GO_HOME_DEFAULT));
+ mAppPackage = getArguments().getString(LAUNCHER_PARAM_NAME, CARLAUNCHER_PACKAGE);
+ mAppActivity = getArguments().getString(APP_ACTIVITY_PARAM_NAME, APP_GRID_ACTIVITY);
+
+ // Default behavior is to press home
+ if (mGoHome) {
+ Log.v(LOG_TAG, "Pressing home");
+ getUiDevice().pressHome();
+ } else {
+ Log.i(LOG_TAG, String.format("Starting %s/%s", mAppPackage, mAppActivity));
+ String openAppGridCommand =
+ String.format("am start -n %s/%s", mAppPackage, mAppActivity);
+ executeShellCommand(openAppGridCommand);
+ }
+ }
+ }
+}
diff --git a/tests/automotive/health/rules/src/android/platform/scenario/SleepAtTestStartRule.java b/tests/automotive/health/rules/src/android/platform/scenario/SleepAtTestStartRule.java
new file mode 100644
index 000000000..8d8f8c540
--- /dev/null
+++ b/tests/automotive/health/rules/src/android/platform/scenario/SleepAtTestStartRule.java
@@ -0,0 +1,41 @@
+/*
+ * 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 android.platform.test.scenario;
+
+import android.os.SystemClock;
+import android.platform.test.rule.TestWatcher;
+import android.util.Log;
+
+import org.junit.runner.Description;
+
+/** This rule will sleep for a given amount of time at the start of each test method. */
+public class SleepAtTestStartRule extends TestWatcher {
+ private static final String LOG_TAG = SleepAtTestStartRule.class.getSimpleName();
+
+ private final long mMillis;
+
+ public SleepAtTestStartRule(long millis) {
+ mMillis = millis;
+ }
+
+ @Override
+ protected void starting(Description description) {
+ Log.v(LOG_TAG, String.format("Sleeping for %d ms", mMillis));
+ SystemClock.sleep(mMillis);
+ Log.v(LOG_TAG, String.format("Done sleeping for %d ms", mMillis));
+ }
+}