summaryrefslogtreecommitdiff
path: root/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/AppManager.java
blob: 7ed95892510df31f5b0f8ff3be2fae3b2246f564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright (c) 2016 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.devtools.systemimage.uitest.utils;

import com.android.devtools.systemimage.uitest.common.Res;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Environment;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiScrollable;
import android.support.test.uiautomator.UiSelector;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class AppManager {

    private AppManager() {
        throw new AssertionError();
    }

    private static boolean isExternalStorageAvailable() {
        String extStorageState = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
            return true;
        }
        return false;
    }

    /**
     * Installs a testing app. The app must be in main/assets.
     * <p>
     * Since all APK installation and uninstallation under tests will be handled in boot strap,
     * this method will be deprecated.
     * @param instrumentation see {@link android.test.InstrumentationTestCase#getInstrumentation()
     *                        getInstrumentation}
     * @param apkName         the APK file full name in main/assets
     * @throws IOException               if File IO fails.
     * @throws UiObjectNotFoundException if it fails to find a UI widget.
     */
    @Deprecated
    public static void deprecateInstallApp(Instrumentation instrumentation, String apkName)
            throws IOException, UiObjectNotFoundException {
        Context context = instrumentation.getTargetContext();
        AssetManager assetManager = context.getAssets();
        InputStream in = assetManager.open(apkName);
        File apkFile = new File(context.getExternalFilesDir(null), apkName);
        OutputStream out = new FileOutputStream(apkFile);
        copyFile(in, out);
        in.close();
        out.close();

        // Install app via Intent and UiAutomator
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        UiDevice device = UiDevice.getInstance(instrumentation);
        UiObject installButton =
                device.findObject(new UiSelector().textMatches("(INSTALL|Install)"));
        installButton.clickAndWaitForNewWindow();
        while (installButton.exists()) {
            installButton.clickAndWaitForNewWindow();
        }
        device.findObject(new UiSelector().textMatches("(DONE|Done)")).click();
    }

    @Deprecated
    private static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }

    /**
     * Uninstalls a testing app.
     * <p>
     * Since all APK installation and uninstallation under tests will be handled in boot strap,
     * this method will be deprecated.
     * @param instrumentation see {@link android.test.InstrumentationTestCase#getInstrumentation()
     *                        getInstrumentation}
     * @param appName         the app name
     * @param pkgName         the app's package name which is used when the app (e.g., service) has
     *                        no name.
     * @throws IOException               if File IO fails.
     * @throws UiObjectNotFoundException if it fails to find a UI object.
     */
    @Deprecated
    public static void deprecateUninstallApp(Instrumentation instrumentation, String appName, String pkgName)
            throws IOException, UiObjectNotFoundException {
        UiDevice device = UiDevice.getInstance(instrumentation);

        // Find and click "appName" or "pkgName" in Settings/Apps
        openAppList(instrumentation);
        UiScrollable appList =
                new UiScrollable(new UiSelector().resourceIdMatches(Res.APPS_LIST_CONTAINER_RES));
        appList.setAsVerticalList();
        String searchedName;
        if (appName != null) {
            searchedName = appName;
        } else {
            if (pkgName == null) {
                throw new AssertionError("Neither appName nor pkgName is non-null.");
            }
            searchedName = pkgName;
        }
        UiObject app =
                appList.getChildByText(
                        new UiSelector().className("android.widget.TextView"),
                        searchedName
                );
        app.clickAndWaitForNewWindow();

        // Uninstall
        device.findObject(new UiSelector().text("Uninstall")).clickAndWaitForNewWindow();
        device.findObject(new UiSelector().text("OK")).clickAndWaitForNewWindow();
    }

    /**
     * Checks if an app is installed.
     *
     * @param instrumentation see {@link android.test.InstrumentationTestCase#getInstrumentation()
     *                        getInstrumentation}
     * @param appName         the app name
     * @param pkgName         the app's package name which is used when the app (e.g., service) has
     *                        no name.
     * @return {@code true} if the app is installed, or {@code false} otherwise.
     */
    public static boolean isAppInstalled(
            Instrumentation instrumentation, String appName, String pkgName)
            throws UiObjectNotFoundException {
        UiDevice device = UiDevice.getInstance(instrumentation);

        // Looking for "appName" or "pkgName" in Settings/Apps
        openAppList(instrumentation);
        UiScrollable appList =
                new UiScrollable(new UiSelector().resourceIdMatches(Res.APPS_LIST_CONTAINER_RES));
        appList.setAsVerticalList();
        String searchedName;
        if (appName != null) {
            searchedName = appName;
        } else {
            if (pkgName == null) {
                throw new AssertionError("Neither appName nor pkgName is non-null.");
            }
            searchedName = pkgName;
        }
        try {
            appList.getChildByText(
                    new UiSelector().className("android.widget.TextView"),
                    searchedName
            );
            return true;
        } catch (UiObjectNotFoundException e) {
            return false;
        }
    }

    /**
     * Clicks "Settings" and "Apps" options to launch the "Apps" page.
     *
     * @param instrumentation see {@link android.test.InstrumentationTestCase#getInstrumentation()
     *                        getInstrumentation}
     *
     * @throws UiObjectNotFoundException if it fails to find a UI widget.
     */
    public static void openAppList(Instrumentation instrumentation)
            throws UiObjectNotFoundException {
        SettingsUtil.openItem(instrumentation, "Apps");
    }

    /**
     * Show all the system apps.
     * For API >= 23, clicks "Show system" text option from the "More options" drop down,
     * for APIs <= 22 scrolls to the last tab to select the "All" option.
     *
     * @param instrumentation see {@link android.test.InstrumentationTestCase#getInstrumentation()
     *                        getInstrumentation}
     * @throws UiObjectNotFoundException if it fails to find a UI widget.
     */
    public static void openSystemAppList(Instrumentation instrumentation)
            throws UiObjectNotFoundException {
        UiDevice device = UiDevice.getInstance(instrumentation);

        // Launch the "Apps" page.
        openAppList(instrumentation);

        if (SystemUtil.getApiLevel() > 22) {
            // From "More options" drop down click "Show system"
            device.pressMenu();
            device.findObject(new UiSelector().textContains("Show system")).click();
        } else {
            // Scroll to the last tab for all the system apps for APIs <= 22
            UiScrollable itemList =
                    new UiScrollable(new UiSelector().resourceId(Res.APPS_TAB_CONTAINER_RES));
            itemList.setAsHorizontalList();
            itemList.getChildByText(new UiSelector().className("android.widget.TextView"),
                    "All").clickAndWaitForNewWindow();
        }
    }
}