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 android.app.Instrumentation;
20 import android.content.pm.PackageManager;
21 import android.os.ParcelFileDescriptor;
22 import android.platform.test.annotations.AppModeFull;
23 import android.test.InstrumentationTestCase;
24 
25 import com.android.compatibility.common.util.BackupUtils;
26 import com.android.compatibility.common.util.LogcatInspector;
27 
28 import java.io.InputStream;
29 
30 /**
31  * Base class for backup instrumentation tests.
32  *
33  * Ensures that backup is enabled and local transport selected, and provides some utility methods.
34  */
35 @AppModeFull
36 public class BaseBackupCtsTest extends InstrumentationTestCase {
37     private static final String APP_LOG_TAG = "BackupCTSApp";
38 
39     private boolean mIsBackupSupported;
40     private LogcatInspector mLogcatInspector =
41             new LogcatInspector() {
42                 @Override
43                 protected InputStream executeShellCommand(String command) {
44                     return executeInstrumentationShellCommand(getInstrumentation(), command);
45                 }
46             };
47     private BackupUtils mBackupUtils =
48             new BackupUtils() {
49                 @Override
50                 protected InputStream executeShellCommand(String command) {
51                     return executeInstrumentationShellCommand(getInstrumentation(), command);
52                 }
53             };
54 
55     @Override
setUp()56     protected void setUp() throws Exception {
57         super.setUp();
58         PackageManager packageManager = getInstrumentation().getContext().getPackageManager();
59         mIsBackupSupported =
60                 packageManager != null
61                         && packageManager.hasSystemFeature(PackageManager.FEATURE_BACKUP);
62 
63         if (mIsBackupSupported) {
64             assertTrue("Backup not enabled", mBackupUtils.isBackupEnabled());
65             assertTrue("LocalTransport not selected", mBackupUtils.isLocalTransportSelected());
66             getBackupUtils()
67                     .executeShellCommandSync("setprop log.tag." + APP_LOG_TAG +" VERBOSE");
68         }
69     }
70 
getBackupUtils()71     protected BackupUtils getBackupUtils() {
72         return mBackupUtils;
73     }
74 
isBackupSupported()75     protected boolean isBackupSupported() {
76         return mIsBackupSupported;
77     }
78 
79     /** See {@link LogcatInspector#mark(String)}. */
markLogcat()80     protected String markLogcat() throws Exception {
81         return mLogcatInspector.mark(APP_LOG_TAG);
82     }
83 
84     /** See {@link LogcatInspector#assertLogcatContainsInOrder(String, int, String...)}. */
waitForLogcat(int maxTimeoutInSeconds, String... logcatStrings)85     protected void waitForLogcat(int maxTimeoutInSeconds, String... logcatStrings)
86             throws Exception {
87         mLogcatInspector.assertLogcatContainsInOrder(
88                 APP_LOG_TAG + ":* *:S", maxTimeoutInSeconds, logcatStrings);
89     }
90 
createTestFileOfSize(String packageName, int size)91     protected void createTestFileOfSize(String packageName, int size) throws Exception {
92         getBackupUtils().executeShellCommandSync(
93                 "am start -a android.intent.action.MAIN "
94                         + "-c android.intent.category.LAUNCHER "
95                         + "-n "
96                         + packageName
97                         + "/android.backup.app.MainActivity "
98                         + "-e file_size " + size);
99         waitForLogcat(30, "File created!");
100     }
101 
executeInstrumentationShellCommand( Instrumentation instrumentation, String command)102     private static InputStream executeInstrumentationShellCommand(
103             Instrumentation instrumentation, String command) {
104         final ParcelFileDescriptor pfd =
105                 instrumentation.getUiAutomation().executeShellCommand(command);
106         return new ParcelFileDescriptor.AutoCloseInputStream(pfd);
107     }
108 }
109