diff options
author | Aneesh Kher <kaneesh@google.com> | 2023-06-20 20:04:07 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-06-20 20:04:07 +0000 |
commit | dd50cb1288c99c9867b190062a68b623f5d3729f (patch) | |
tree | 5c6108ffc144f69b492b225fca28f4065b0099fc | |
parent | a958d04b6ba4434565c537381cc9a8e336748561 (diff) | |
parent | fe139fa023d6094b18dca20e815ad09765d2e3a7 (diff) | |
download | platform_testing-dd50cb1288c99c9867b190062a68b623f5d3729f.tar.gz |
Add hot and cold start up specific JUnit rules am: c2c31c5aac am: fe139fa023
Original change: https://googleplex-android-review.googlesource.com/c/platform/platform_testing/+/23732868
Change-Id: I60073b9441cb7d9756c1dc6578d51bbda44ebea9
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
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)); + } +} |