• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package android.backup.cts;
18 
19 import static junit.framework.Assert.assertTrue;
20 
21 import android.app.Instrumentation;
22 import android.content.pm.PackageManager;
23 import android.os.ParcelFileDescriptor;
24 
25 import androidx.test.platform.app.InstrumentationRegistry;
26 
27 import com.android.compatibility.common.util.BackupUtils;
28 import com.android.compatibility.common.util.LogcatInspector;
29 
30 import java.io.InputStream;
31 
32 /**
33  * Base class for backup instrumentation tests.
34  *
35  * Ensures that backup is enabled and local transport selected, and provides some utility methods.
36  */
37 public class BaseBackupCtsTest {
38     private static final String APP_LOG_TAG = "BackupCTSApp";
39 
40     public final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
41     private boolean mIsBackupSupported;
42     private LogcatInspector mLogcatInspector =
43             new LogcatInspector() {
44                 @Override
45                 protected InputStream executeShellCommand(String command) {
46                     return executeInstrumentationShellCommand(mInstrumentation, command);
47                 }
48             };
49     private BackupUtils mBackupUtils =
50             new BackupUtils() {
51                 @Override
52                 protected InputStream executeShellCommand(String command) {
53                     return executeInstrumentationShellCommand(mInstrumentation, command);
54                 }
55             };
56 
57     /** Inheritors need to call this on their @Before methods. */
setUp()58     public void setUp() throws Exception {
59         PackageManager packageManager = mInstrumentation.getContext().getPackageManager();
60         mIsBackupSupported =
61                 packageManager != null
62                         && packageManager.hasSystemFeature(PackageManager.FEATURE_BACKUP);
63 
64         if (mIsBackupSupported) {
65             assertTrue("Backup not enabled", mBackupUtils.isBackupEnabled());
66             assertTrue("LocalTransport not selected", mBackupUtils.isLocalTransportSelected());
67             getBackupUtils()
68                     .executeShellCommandSync("setprop log.tag." + APP_LOG_TAG + " VERBOSE");
69         }
70     }
71 
getBackupUtils()72     protected BackupUtils getBackupUtils() {
73         return mBackupUtils;
74     }
75 
isBackupSupported()76     protected boolean isBackupSupported() {
77         return mIsBackupSupported;
78     }
79 
80     /** See {@link LogcatInspector#mark(String)}. */
markLogcat()81     protected String markLogcat() throws Exception {
82         return mLogcatInspector.mark(APP_LOG_TAG);
83     }
84 
85     /** See {@link LogcatInspector#assertLogcatContainsInOrder(String, int, String...)}. */
waitForLogcat(int maxTimeoutInSeconds, String... logcatStrings)86     protected void waitForLogcat(int maxTimeoutInSeconds, String... logcatStrings)
87             throws Exception {
88         mLogcatInspector.assertLogcatContainsInOrder(
89                 APP_LOG_TAG + ":* *:S", maxTimeoutInSeconds, logcatStrings);
90     }
91 
createTestFileOfSize(String packageName, int size)92     protected void createTestFileOfSize(String packageName, int size) throws Exception {
93         getBackupUtils().executeShellCommandSync(
94                 "am start -a android.intent.action.MAIN "
95                         + "-c android.intent.category.LAUNCHER "
96                         + "-n "
97                         + packageName
98                         + "/android.backup.app.MainActivity "
99                         + "-e file_size " + size);
100         waitForLogcat(30, "File created!");
101     }
102 
setLocalTransportParameters(String parameters)103     protected void setLocalTransportParameters(String parameters) throws Exception {
104         getBackupUtils().executeShellCommandSync(
105                 "settings put secure backup_local_transport_parameters " + parameters);
106     }
107 
clearBackupDataInLocalTransport(String packageName)108     protected void clearBackupDataInLocalTransport(String packageName) throws Exception {
109         getBackupUtils().executeShellCommandSync(
110                 String.format("bmgr wipe %s %s", "com.android.localtransport/.LocalTransport",
111                         packageName));
112     }
113 
forceStopPackage(String packageName)114     protected void forceStopPackage(String packageName) throws Exception {
115         getBackupUtils().executeShellCommandSync(String.format("am force-stop %s", packageName));
116     }
117 
executeInstrumentationShellCommand( Instrumentation instrumentation, String command)118     private static InputStream executeInstrumentationShellCommand(
119             Instrumentation instrumentation, String command) {
120         final ParcelFileDescriptor pfd =
121                 instrumentation.getUiAutomation().executeShellCommand(command);
122         return new ParcelFileDescriptor.AutoCloseInputStream(pfd);
123     }
124 }
125