[CTS] Move RestoreSessionTest to CTS
Moving to CTS because:
1. It is testing some public APIs and in order for that coverage to be recognized it needs to be in CTS.
2. We'll generally try to put tests in CTS unless there's a reason to do otherwise.
Only adding this to CTS for Q since the original GTS test also only ran for Q+
Bug: 133733353
Test: atest RestoreSessionHostSideTest
Change-Id: Idee3d50d0490ab0592976e56ee7611e535ec2007
diff --git a/hostsidetests/backup/OWNERS b/hostsidetests/backup/OWNERS
index 3637e32..c28c4d8 100644
--- a/hostsidetests/backup/OWNERS
+++ b/hostsidetests/backup/OWNERS
@@ -1,3 +1,4 @@
+# Bug component: 41666
# Use this reviewer by default.
br-framework-team+reviews@google.com
diff --git a/hostsidetests/backup/RestoreSessionTest/Android.bp b/hostsidetests/backup/RestoreSessionTest/Android.bp
new file mode 100644
index 0000000..29f6e59
--- /dev/null
+++ b/hostsidetests/backup/RestoreSessionTest/Android.bp
@@ -0,0 +1,27 @@
+// 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.
+
+java_library {
+ name: "CtsRestoreSessionApp",
+ defaults: ["cts_defaults"],
+ static_libs: [
+ "androidx.test.rules",
+ "platform-test-annotations",
+ "truth-prebuilt",
+ ],
+ srcs: [
+ "src/**/*.java",
+ ],
+ sdk_version: "system_current",
+}
diff --git a/hostsidetests/backup/RestoreSessionTest/AndroidManifest.xml b/hostsidetests/backup/RestoreSessionTest/AndroidManifest.xml
new file mode 100644
index 0000000..130e3aa
--- /dev/null
+++ b/hostsidetests/backup/RestoreSessionTest/AndroidManifest.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2018 Google Inc.
+ *
+ * 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 Licensea
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="android.cts.backup.restoresessionapp">
+
+ <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="26"/>
+
+ <application
+ android:label="RestoreSessionApp"
+ android:allowBackup="true">
+
+ <uses-library android:name="android.test.runner" />
+
+ </application>
+
+ <instrumentation
+ android:name="androidx.test.runner.AndroidJUnitRunner"
+ android:targetPackage="android.cts.backup.restoresessionapp" />
+</manifest>
diff --git a/hostsidetests/backup/RestoreSessionTest/src/android/cts/backup/restoresessionapp/BaseRestoreSessionAppTest.java b/hostsidetests/backup/RestoreSessionTest/src/android/cts/backup/restoresessionapp/BaseRestoreSessionAppTest.java
new file mode 100644
index 0000000..85b05d3
--- /dev/null
+++ b/hostsidetests/backup/RestoreSessionTest/src/android/cts/backup/restoresessionapp/BaseRestoreSessionAppTest.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2019 Google Inc.
+ *
+ * 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.cts.backup.restoresessionapp;
+
+import static androidx.test.InstrumentationRegistry.getTargetContext;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+public class BaseRestoreSessionAppTest {
+ private static final String SHARED_PREFERENCES_FILE = "restore_session_app_prefs";
+
+ private SharedPreferences mPreferences;
+
+ @Before
+ public void setUp() {
+ mPreferences =
+ getTargetContext()
+ .getSharedPreferences(SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
+ }
+
+ protected void clearSharedPrefs() {
+ mPreferences.edit().clear().commit();
+ }
+
+ protected void checkSharedPrefsDontExist(String prefKey) {
+ assertThat(mPreferences.getInt(prefKey, 0)).isEqualTo(0);
+ }
+
+ protected void saveValuesToSharedPrefs(String prefKey, int prefValue) {
+ mPreferences.edit().putInt(prefKey, prefValue).commit();
+ }
+
+ protected void checkSharedPrefsExist(String prefKey, int prefValue) {
+ assertThat(mPreferences.getInt(prefKey, 0)).isEqualTo(prefValue);
+ }
+}
diff --git a/hostsidetests/backup/RestoreSessionTest/src/android/cts/backup/restoresessionapp/RestoreSessionTest.java b/hostsidetests/backup/RestoreSessionTest/src/android/cts/backup/restoresessionapp/RestoreSessionTest.java
new file mode 100644
index 0000000..06b9ae0
--- /dev/null
+++ b/hostsidetests/backup/RestoreSessionTest/src/android/cts/backup/restoresessionapp/RestoreSessionTest.java
@@ -0,0 +1,237 @@
+/*
+ * Copyright (C) 2019 Google Inc.
+ *
+ * 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.cts.backup.restoresessionapp;
+
+import static androidx.test.InstrumentationRegistry.getTargetContext;
+import static androidx.test.InstrumentationRegistry.getInstrumentation;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+
+import static org.junit.Assert.assertNotEquals;
+
+import android.app.Instrumentation;
+import android.app.UiAutomation;
+import android.app.backup.BackupManager;
+import android.app.backup.BackupManagerMonitor;
+import android.app.backup.RestoreObserver;
+import android.app.backup.RestoreSession;
+import android.app.backup.RestoreSet;
+import android.content.Context;
+import android.os.Bundle;
+
+import android.platform.test.annotations.AppModeFull;
+import androidx.test.runner.AndroidJUnit4;
+
+// import com.android.compatibility.common.util.SystemUtil;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Device side routines to be invoked by the host side RestoreSessionHostSideTest. These are not
+ * designed to be called in any other way, as they rely on state set up by the host side test.
+ */
+@RunWith(AndroidJUnit4.class)
+@AppModeFull
+public class RestoreSessionTest {
+ private static final String PACKAGE_1 = "android.cts.backup.restoresessionapp1";
+ private static final String PACKAGE_2 = "android.cts.backup.restoresessionapp2";
+ private static final String PACKAGE_3 = "android.cts.backup.restoresessionapp3";
+
+ private static final int RESTORE_TIMEOUT_SECONDS = 10;
+
+ private BackupManager mBackupManager;
+ private Set<String> mRestorePackages;
+ private Set<String> mNonRestorePackages;
+ private CountDownLatch mRestoreObserverLatch;
+ private RestoreSession mRestoreSession;
+ private UiAutomation mUiAutomation;
+ private long mRestoreToken;
+
+ private final RestoreObserver mRestoreObserver =
+ new RestoreObserver() {
+ @Override
+ public void restoreSetsAvailable(RestoreSet[] result) {
+ super.restoreSetsAvailable(result);
+
+ long token = 0L;
+
+ for (RestoreSet restoreSet : result) {
+ long restoreToken = restoreSet.token;
+ if (doesRestoreSetContainAllPackages(restoreToken, mRestorePackages)
+ && doesRestoreSetContainAllPackages(
+ restoreToken, mNonRestorePackages)) {
+ token = restoreSet.token;
+ break;
+ }
+ }
+
+ mRestoreToken = token;
+
+ mRestoreObserverLatch.countDown();
+ }
+
+ @Override
+ public void restoreStarting(int numPackages) {
+ super.restoreStarting(numPackages);
+
+ assertEquals(
+ "Wrong number of packages in the restore set",
+ mRestorePackages.size(),
+ numPackages);
+ mRestoreObserverLatch.countDown();
+ }
+
+ @Override
+ public void onUpdate(int nowBeingRestored, String currentPackage) {
+ super.onUpdate(nowBeingRestored, currentPackage);
+
+ assertTrue(
+ "Restoring package that is not in mRestorePackages",
+ mRestorePackages.contains(currentPackage));
+ mRestoreObserverLatch.countDown();
+ }
+
+ @Override
+ public void restoreFinished(int error) {
+ super.restoreFinished(error);
+
+ assertEquals(
+ "Restore finished with error: " + error, BackupManager.SUCCESS, error);
+ mRestoreSession.endRestoreSession();
+ mRestoreObserverLatch.countDown();
+ }
+ };
+
+ @Before
+ public void setUp() throws InterruptedException {
+ Context context = getTargetContext();
+ mBackupManager = new BackupManager(context);
+
+ mRestorePackages = new HashSet<>();
+ mRestorePackages.add(PACKAGE_1);
+ mRestorePackages.add(PACKAGE_2);
+
+ mNonRestorePackages = new HashSet<>();
+ mNonRestorePackages.add(PACKAGE_3);
+
+ mRestoreToken = 0L;
+
+ mUiAutomation = getInstrumentation().getUiAutomation();
+ mUiAutomation.adoptShellPermissionIdentity();
+
+ loadAvailableRestoreSets();
+ }
+
+ @After
+ public void tearDown() {
+ mUiAutomation.dropShellPermissionIdentity();
+ }
+
+ /**
+ * Restore packages added to mRestorePackages and verify only those packages are restored. Use
+ * {@link RestoreSession#restorePackages(long, RestoreObserver, Set)}
+ */
+ @Test
+ public void testRestorePackages() throws InterruptedException {
+ testRestorePackagesInternal(false);
+ }
+
+ /**
+ * Restore packages added to mRestorePackages and verify only those packages are restored. Use
+ * {@link RestoreSession#restorePackages(long, RestoreObserver, Set, BackupManagerMonitor)}
+ */
+ @Test
+ public void testRestorePackagesWithMonitorParam() throws InterruptedException {
+ testRestorePackagesInternal(true);
+ }
+
+ private void testRestorePackagesInternal(boolean useMonitorParam) throws InterruptedException {
+ // Wait for the callbacks from RestoreObserver: one for each package from
+ // mRestorePackages plus restoreStarting and restoreFinished.
+ mRestoreObserverLatch = new CountDownLatch(mRestorePackages.size() + 2);
+ CountDownLatch backupMonitorLatch = null;
+ if (useMonitorParam) {
+ // Wait for the callbacks from BackupManagerMonitor: one for each package.
+ backupMonitorLatch = new CountDownLatch(mRestorePackages.size());
+ mRestoreSession.restorePackages(
+ mRestoreToken,
+ mRestoreObserver,
+ mRestorePackages,
+ new TestBackupMonitor(backupMonitorLatch));
+ } else {
+ mRestoreSession.restorePackages(mRestoreToken, mRestoreObserver, mRestorePackages);
+ }
+
+ awaitResultAndAssertSuccess(mRestoreObserverLatch);
+ if (backupMonitorLatch != null) {
+ awaitResultAndAssertSuccess(backupMonitorLatch);
+ }
+ }
+
+ private void loadAvailableRestoreSets() throws InterruptedException {
+ // Wait for getAvailableRestoreSets to finish and the callback to be fired.
+ mRestoreObserverLatch = new CountDownLatch(1);
+ mRestoreSession = mBackupManager.beginRestoreSession();
+ assertEquals(
+ BackupManager.SUCCESS, mRestoreSession.getAvailableRestoreSets(mRestoreObserver));
+ awaitResultAndAssertSuccess(mRestoreObserverLatch);
+
+ assertNotEquals("Restore set not found", 0L, mRestoreToken);
+ }
+
+ private boolean doesRestoreSetContainAllPackages(long restoreToken, Set<String> packages) {
+ for (String restorePackage : packages) {
+ if (mBackupManager.getAvailableRestoreToken(restorePackage) != restoreToken) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private void awaitResultAndAssertSuccess(CountDownLatch latch) throws InterruptedException {
+ boolean waitResult = latch.await(RESTORE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ assertTrue("Restore timed out", waitResult);
+ }
+
+ private static class TestBackupMonitor extends BackupManagerMonitor {
+ private final CountDownLatch mLatch;
+
+ TestBackupMonitor(CountDownLatch latch) {
+ mLatch = latch;
+ }
+
+ @Override
+ public void onEvent(Bundle event) {
+ super.onEvent(event);
+
+ int eventType = event.getInt(BackupManagerMonitor.EXTRA_LOG_EVENT_ID);
+ assertEquals(
+ "Unexpected event from BackupManagerMonitor: " + eventType,
+ BackupManagerMonitor.LOG_EVENT_ID_VERSIONS_MATCH,
+ eventType);
+ mLatch.countDown();
+ }
+ }
+}
diff --git a/hostsidetests/backup/restoresessionapp1/Android.bp b/hostsidetests/backup/restoresessionapp1/Android.bp
new file mode 100644
index 0000000..2eb5d0e
--- /dev/null
+++ b/hostsidetests/backup/restoresessionapp1/Android.bp
@@ -0,0 +1,32 @@
+// 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.
+
+android_test_helper_app {
+ name: "CtsRestoreSessionApp1",
+ defaults: ["cts_defaults"],
+ static_libs: [
+ "androidx.test.rules",
+ "truth-prebuilt",
+ "CtsRestoreSessionApp",
+ ],
+ srcs: [
+ "src/**/*.java"
+ ],
+ // tag this module as a cts test artifact
+ test_suites: [
+ "cts",
+ "vts",
+ ],
+ sdk_version: "system_current",
+}
diff --git a/hostsidetests/backup/restoresessionapp1/AndroidManifest.xml b/hostsidetests/backup/restoresessionapp1/AndroidManifest.xml
new file mode 100644
index 0000000..ae6b205
--- /dev/null
+++ b/hostsidetests/backup/restoresessionapp1/AndroidManifest.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2018 Google Inc.
+ *
+ * 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 Licensea
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="android.cts.backup.restoresessionapp1">
+
+ <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="26"/>
+
+ <application
+ android:label="RestoreSessionApp"
+ android:allowBackup="true">
+
+ <uses-library android:name="android.test.runner" />
+
+ </application>
+
+ <instrumentation
+ android:name="androidx.test.runner.AndroidJUnitRunner"
+ android:targetPackage="android.cts.backup.restoresessionapp1" />
+</manifest>
diff --git a/hostsidetests/backup/restoresessionapp1/src/android/cts/backup/restoresessionapp1/RestoreSessionAppTest.java b/hostsidetests/backup/restoresessionapp1/src/android/cts/backup/restoresessionapp1/RestoreSessionAppTest.java
new file mode 100644
index 0000000..f82d058
--- /dev/null
+++ b/hostsidetests/backup/restoresessionapp1/src/android/cts/backup/restoresessionapp1/RestoreSessionAppTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2019 Google Inc.
+ *
+ * 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.cts.backup.restoresessionapp1;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import android.cts.backup.restoresessionapp.BaseRestoreSessionAppTest;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Device side routines to be invoked by the host side RestoreSessionHostSideTest. These are not
+ * designed to be called in any other way, as they rely on state set up by the host side test.
+ */
+@RunWith(AndroidJUnit4.class)
+public class RestoreSessionAppTest extends BaseRestoreSessionAppTest {
+ private static final String SHARED_PREFERENCES_KEY = "test_key_1";
+ private static final int SHARED_PREFERENCES_VALUE = 123;
+
+ @Test
+ public void testClearSharedPrefs() {
+ clearSharedPrefs();
+ }
+
+ @Test
+ public void testCheckSharedPrefsDontExist() {
+ checkSharedPrefsDontExist(SHARED_PREFERENCES_KEY);
+ }
+
+ @Test
+ public void testSaveValuesToSharedPrefs() {
+ saveValuesToSharedPrefs(SHARED_PREFERENCES_KEY, SHARED_PREFERENCES_VALUE);
+ }
+
+ @Test
+ public void testCheckSharedPrefsExist() {
+ checkSharedPrefsExist(SHARED_PREFERENCES_KEY, SHARED_PREFERENCES_VALUE);
+ }
+}
diff --git a/hostsidetests/backup/restoresessionapp2/Android.bp b/hostsidetests/backup/restoresessionapp2/Android.bp
new file mode 100644
index 0000000..e389054
--- /dev/null
+++ b/hostsidetests/backup/restoresessionapp2/Android.bp
@@ -0,0 +1,32 @@
+// 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.
+
+android_test_helper_app {
+ name: "CtsRestoreSessionApp2",
+ defaults: ["cts_defaults"],
+ static_libs: [
+ "androidx.test.rules",
+ "truth-prebuilt",
+ "CtsRestoreSessionApp",
+ ],
+ srcs: [
+ "src/**/*.java"
+ ],
+ // tag this module as a cts test artifact
+ test_suites: [
+ "cts",
+ "vts",
+ ],
+ sdk_version: "system_current",
+}
diff --git a/hostsidetests/backup/restoresessionapp2/AndroidManifest.xml b/hostsidetests/backup/restoresessionapp2/AndroidManifest.xml
new file mode 100644
index 0000000..757c801
--- /dev/null
+++ b/hostsidetests/backup/restoresessionapp2/AndroidManifest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2018 Google Inc.
+ *
+ * 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 Licensea
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="android.cts.backup.restoresessionapp2">
+
+ <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="26"/>
+
+ <application
+ android:label="RestoreSessionApp"
+ android:allowBackup="true"/>
+
+ <instrumentation
+ android:name="androidx.test.runner.AndroidJUnitRunner"
+ android:targetPackage="android.cts.backup.restoresessionapp2" />
+</manifest>
diff --git a/hostsidetests/backup/restoresessionapp2/src/android/cts/backup/restoresessionapp2/RestoreSessionAppTest.java b/hostsidetests/backup/restoresessionapp2/src/android/cts/backup/restoresessionapp2/RestoreSessionAppTest.java
new file mode 100644
index 0000000..458103b
--- /dev/null
+++ b/hostsidetests/backup/restoresessionapp2/src/android/cts/backup/restoresessionapp2/RestoreSessionAppTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2019 Google Inc.
+ *
+ * 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.cts.backup.restoresessionapp2;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import android.cts.backup.restoresessionapp.BaseRestoreSessionAppTest;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Device side routines to be invoked by the host side RestoreSessionHostSideTest. These are not
+ * designed to be called in any other way, as they rely on state set up by the host side test.
+ */
+@RunWith(AndroidJUnit4.class)
+public class RestoreSessionAppTest extends BaseRestoreSessionAppTest {
+ private static final String SHARED_PREFERENCES_KEY = "test_key_2";
+ private static final int SHARED_PREFERENCES_VALUE = 124;
+
+ @Test
+ public void testClearSharedPrefs() {
+ clearSharedPrefs();
+ }
+
+ @Test
+ public void testCheckSharedPrefsDontExist() {
+ checkSharedPrefsDontExist(SHARED_PREFERENCES_KEY);
+ }
+
+ @Test
+ public void testSaveValuesToSharedPrefs() {
+ saveValuesToSharedPrefs(SHARED_PREFERENCES_KEY, SHARED_PREFERENCES_VALUE);
+ }
+
+ @Test
+ public void testCheckSharedPrefsExist() {
+ checkSharedPrefsExist(SHARED_PREFERENCES_KEY, SHARED_PREFERENCES_VALUE);
+ }
+}
diff --git a/hostsidetests/backup/restoresessionapp3/Android.bp b/hostsidetests/backup/restoresessionapp3/Android.bp
new file mode 100644
index 0000000..1451823
--- /dev/null
+++ b/hostsidetests/backup/restoresessionapp3/Android.bp
@@ -0,0 +1,32 @@
+// 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.
+
+android_test_helper_app {
+ name: "CtsRestoreSessionApp3",
+ defaults: ["cts_defaults"],
+ static_libs: [
+ "androidx.test.rules",
+ "truth-prebuilt",
+ "CtsRestoreSessionApp",
+ ],
+ srcs: [
+ "src/**/*.java"
+ ],
+ // tag this module as a cts test artifact
+ test_suites: [
+ "cts",
+ "vts",
+ ],
+ sdk_version: "system_current",
+}
diff --git a/hostsidetests/backup/restoresessionapp3/AndroidManifest.xml b/hostsidetests/backup/restoresessionapp3/AndroidManifest.xml
new file mode 100644
index 0000000..027cf68
--- /dev/null
+++ b/hostsidetests/backup/restoresessionapp3/AndroidManifest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2018 Google Inc.
+ *
+ * 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 Licensea
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="android.cts.backup.restoresessionapp3">
+
+ <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="26"/>
+
+ <application
+ android:label="RestoreSessionApp"
+ android:allowBackup="true"/>
+
+ <instrumentation
+ android:name="androidx.test.runner.AndroidJUnitRunner"
+ android:targetPackage="android.cts.backup.restoresessionapp3" />
+</manifest>
diff --git a/hostsidetests/backup/restoresessionapp3/src/android/cts/backup/restoresessionapp3/RestoreSessionAppTest.java b/hostsidetests/backup/restoresessionapp3/src/android/cts/backup/restoresessionapp3/RestoreSessionAppTest.java
new file mode 100644
index 0000000..b034e1a
--- /dev/null
+++ b/hostsidetests/backup/restoresessionapp3/src/android/cts/backup/restoresessionapp3/RestoreSessionAppTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2019 Google Inc.
+ *
+ * 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.cts.backup.restoresessionapp3;
+
+import androidx.test.runner.AndroidJUnit4;
+
+import android.cts.backup.restoresessionapp.BaseRestoreSessionAppTest;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Device side routines to be invoked by the host side RestoreSessionHostSideTest. These are not
+ * designed to be called in any other way, as they rely on state set up by the host side test.
+ */
+@RunWith(AndroidJUnit4.class)
+public class RestoreSessionAppTest extends BaseRestoreSessionAppTest {
+ private static final String SHARED_PREFERENCES_KEY = "test_key_3";
+ private static final int SHARED_PREFERENCES_VALUE = 125;
+
+ @Test
+ public void testClearSharedPrefs() {
+ clearSharedPrefs();
+ }
+
+ @Test
+ public void testCheckSharedPrefsDontExist() {
+ checkSharedPrefsDontExist(SHARED_PREFERENCES_KEY);
+ }
+
+ @Test
+ public void testSaveValuesToSharedPrefs() {
+ saveValuesToSharedPrefs(SHARED_PREFERENCES_KEY, SHARED_PREFERENCES_VALUE);
+ }
+
+ @Test
+ public void testCheckSharedPrefsExist() {
+ checkSharedPrefsExist(SHARED_PREFERENCES_KEY, SHARED_PREFERENCES_VALUE);
+ }
+}
diff --git a/hostsidetests/backup/src/android/cts/backup/RestoreSessionHostSideTest.java b/hostsidetests/backup/src/android/cts/backup/RestoreSessionHostSideTest.java
new file mode 100644
index 0000000..9c5d890
--- /dev/null
+++ b/hostsidetests/backup/src/android/cts/backup/RestoreSessionHostSideTest.java
@@ -0,0 +1,167 @@
+/*
+ * 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 android.cts.backup;
+
+import static org.junit.Assert.assertTrue;
+
+import android.platform.test.annotations.AppModeFull;
+
+import com.android.compatibility.common.util.BackupUtils;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+
+import java.io.IOException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.Optional;
+
+/**
+ * Tests for system APIs in {@link RestoreSession}
+ *
+ * <p>These tests use the local transport.
+ */
+@RunWith(DeviceJUnit4ClassRunner.class)
+@AppModeFull
+public class RestoreSessionHostSideTest extends BaseBackupHostSideTest {
+ private static final int USER_SYSTEM = 0;
+ private static final String MAIN_TEST_APP_PKG = "android.cts.backup.restoresessionapp";
+ private static final String DEVICE_MAIN_TEST_CLASS_NAME =
+ MAIN_TEST_APP_PKG + ".RestoreSessionTest";
+ private static final String MAIN_TEST_APK = "CtsRestoreSessionApp.apk";
+
+ private static final String TEST_APP_PKG_PREFIX = "android.cts.backup.restoresessionapp";
+ private static final String TEST_APP_APK_PREFIX = "CtsRestoreSessionApp";
+ private static final int TEST_APPS_COUNT = 3;
+
+ private Optional<String> mOldTransport = Optional.empty();
+ private BackupUtils mBackupUtils;
+
+ /** Switch to local transport. */
+ @Before
+ public void setUp() throws Exception {
+ mBackupUtils = getBackupUtils();
+ mOldTransport = Optional.of(setBackupTransport(mBackupUtils.getLocalTransportName()));
+ installPackage(MAIN_TEST_APK);
+ }
+
+ /** Restore transport settings to original values. */
+ @After
+ public void tearDown() throws Exception {
+ if (mOldTransport.isPresent()) {
+ setBackupTransport(mOldTransport.get());
+ mOldTransport = Optional.empty();
+
+ uninstallPackage(MAIN_TEST_APK);
+ }
+ }
+
+ /** Test {@link RestoreSession#restorePackages(long, RestoreObserver, Set)} */
+ @Test
+ public void testRestorePackages() throws Exception {
+ testRestorePackagesInternal("testRestorePackages");
+ }
+
+ /**
+ * Test {@link RestoreSession#restorePackages(long, RestoreObserver, Set, BackupManagerMonitor)}
+ */
+ @Test
+ public void testRestorePackagesWithMonitorParam() throws Exception {
+ testRestorePackagesInternal("testRestorePackagesWithMonitorParam");
+ }
+
+ /**
+ *
+ *
+ * <ol>
+ * <li>Install 3 test packages on the device
+ * <li>Write dummy values to shared preferences for each package
+ * <li>Backup each package (adb shell bmgr backupnow)
+ * <li>Clear shared preferences for each package
+ * <li>Run restore for 2 of the packages and verify that only they were restored
+ * <li>Verify that shared preferences for the 2 packages are restored correctly
+ * </ol>
+ */
+ private void testRestorePackagesInternal(String deviceTestName) throws Exception {
+ installPackage(getApkNameForTestApp(1));
+ installPackage(getApkNameForTestApp(2));
+ installPackage(getApkNameForTestApp(3));
+ //
+ // Write dummy value to shared preferences for all test packages.
+ checkRestoreSessionDeviceTestForAllApps("testSaveValuesToSharedPrefs");
+ checkRestoreSessionDeviceTestForAllApps("testCheckSharedPrefsExist");
+
+ // Backup all test packages.
+ mBackupUtils.backupNowAndAssertSuccess(getPackageNameForTestApp(1));
+ mBackupUtils.backupNowAndAssertSuccess(getPackageNameForTestApp(2));
+ mBackupUtils.backupNowAndAssertSuccess(getPackageNameForTestApp(3));
+
+ // Clear shared preferences for all test packages.
+ checkRestoreSessionDeviceTestForAllApps("testClearSharedPrefs");
+ checkRestoreSessionDeviceTestForAllApps("testCheckSharedPrefsDontExist");
+
+ runRestoreSessionDeviceTestAndAssertSuccess(
+ MAIN_TEST_APP_PKG, DEVICE_MAIN_TEST_CLASS_NAME, deviceTestName);
+
+ // Check that shared prefs are only restored (and restored correctly) for the first 2
+ // packages.
+ checkRestoreSessionDeviceTest(1, "testCheckSharedPrefsExist");
+ checkRestoreSessionDeviceTest(2, "testCheckSharedPrefsExist");
+ checkRestoreSessionDeviceTest(3, "testCheckSharedPrefsDontExist");
+
+ uninstallPackage(getPackageNameForTestApp(1));
+ uninstallPackage(getPackageNameForTestApp(2));
+ uninstallPackage(getPackageNameForTestApp(3));
+ }
+
+ /** Run the given device test for all test apps. */
+ private void checkRestoreSessionDeviceTestForAllApps(String testName)
+ throws DeviceNotAvailableException {
+ for (int appNumber = 1; appNumber <= TEST_APPS_COUNT; appNumber++) {
+ checkRestoreSessionDeviceTest(appNumber, testName);
+ }
+ }
+
+ /** Run device test with the given test name and test app number. */
+ private void checkRestoreSessionDeviceTest(int testAppNumber, String testName)
+ throws DeviceNotAvailableException {
+ String packageName = getPackageNameForTestApp(testAppNumber);
+ runRestoreSessionDeviceTestAndAssertSuccess(
+ packageName, packageName + ".RestoreSessionAppTest", testName);
+ }
+
+ private void runRestoreSessionDeviceTestAndAssertSuccess(
+ String packageName, String fullClassName, String testName)
+ throws DeviceNotAvailableException {
+ boolean result = runDeviceTests(packageName, fullClassName, testName);
+ assertTrue("Device test failed: " + testName, result);
+ }
+
+ private String getPackageNameForTestApp(int appNumber) {
+ return TEST_APP_PKG_PREFIX + appNumber;
+ }
+
+ private String getApkNameForTestApp(int appNumber) {
+ return TEST_APP_APK_PREFIX + appNumber + ".apk";
+ }
+
+ private String setBackupTransport(String transport) throws IOException {
+ return mBackupUtils.setBackupTransportForUser(transport, USER_SYSTEM);
+ }
+}