summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJW Wang <wangchun@google.com>2020-07-01 12:47:16 +0800
committerAnton Hansson <hansson@google.com>2020-11-19 13:21:04 +0000
commit10b6122aa1f819fab267780613de32b7a1ca72cc (patch)
tree32d20cd64a18f40ffd87679b7d39180a96b163ae
parent0a97fe3faaf1a7bc69030ac60e55b865ebd20d27 (diff)
downloadbase-10b6122aa1f819fab267780613de32b7a1ca72cc.tar.gz
Add AbandonSessionsRule
This is a partial cherry-pick of the change I3ad6317eaee3ffb4c6351c630460dd8f1d27e644 Test: sdkextensions_e2e_tests Change-Id: Ica17fdf745cedfc54567666b8231f89ee7f20a7b Merged-In: I3ad6317eaee3ffb4c6351c630460dd8f1d27e644
-rw-r--r--tests/RollbackTest/lib/src/com/android/tests/rollback/host/AbandonSessionsRule.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/RollbackTest/lib/src/com/android/tests/rollback/host/AbandonSessionsRule.java b/tests/RollbackTest/lib/src/com/android/tests/rollback/host/AbandonSessionsRule.java
new file mode 100644
index 000000000000..b08621314ee0
--- /dev/null
+++ b/tests/RollbackTest/lib/src/com/android/tests/rollback/host/AbandonSessionsRule.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2020 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.tests.rollback.host;
+
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
+
+import org.junit.rules.ExternalResource;
+
+public class AbandonSessionsRule extends ExternalResource {
+ private final BaseHostJUnit4Test mHost;
+
+ public AbandonSessionsRule(BaseHostJUnit4Test host) {
+ mHost = host;
+ }
+
+ @Override
+ protected void before() throws Throwable {
+ abandonSessions(mHost.getDevice());
+ }
+
+ @Override
+ protected void after() {
+ try {
+ abandonSessions(mHost.getDevice());
+ } catch (Exception ignore) {
+ }
+ }
+
+ /**
+ * Abandons all sessions to prevent interference in our tests.
+ */
+ private static void abandonSessions(ITestDevice device) throws Exception {
+ // No point in abandoning applied or failed sessions. We care about ready sessions only.
+ String cmdListReadySessions =
+ "pm list staged-sessions --only-sessionid --only-parent --only-ready";
+ String output = device.executeShellCommand(cmdListReadySessions);
+ if (output.trim().isEmpty()) {
+ // No sessions to abandon
+ return;
+ }
+ // Ensure we have sufficient privilege to abandon sessions from other apps
+ device.enableAdbRoot();
+ device.executeShellCommand("for i in $(" + cmdListReadySessions
+ + "); do pm install-abandon $i; done");
+ device.disableAdbRoot();
+ }
+}