summaryrefslogtreecommitdiff
path: root/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/PlayStoreUtil.java
blob: 91eb4f62dcf215f11859ccdf05e1685f5d9435fd (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
/*
 * 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 android.app.Instrumentation;
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 com.android.devtools.systemimage.uitest.common.Res;
import com.android.devtools.systemimage.uitest.watchers.PlayStoreConfirmationWatcher;

import java.util.concurrent.TimeUnit;


/**
 * Static utility methods pertaining to the Google Play Store.
 */
public class PlayStoreUtil {

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

    /**
     * Checks if Play Store has been installed.
     * Returns true if Play Store has been installed, false if not.
     */
    public static boolean isPlayStoreInstalled(Instrumentation instrumentation) throws Exception {
        final UiDevice device = UiDevice.getInstance(instrumentation);
        final UiScrollable scrollable = new UiScrollable(new UiSelector().scrollable(true));
        final String playStore = "Play Store";

        device.pressHome();
        device.findObject(new UiSelector().description("Apps")).clickAndWaitForNewWindow();

        boolean isInstalled = new Wait().until(new Wait.ExpectedCondition() {
            @Override
            public boolean isTrue() throws UiObjectNotFoundException {

                scrollable.scrollIntoView(new UiSelector().text(playStore));
                return scrollable.getChild(new UiSelector().text(playStore)).exists();
            }
        });
        return isInstalled;
    }

    /**
     * Backtracks to the opening Play Store screen.
     */
    public static void resetPlayStore(Instrumentation instrumentation) {
        final UiDevice device = UiDevice.getInstance(instrumentation);

        for (int i = 0; i < 5; i++) {
            device.pressBack();
        }
    }

    /**
     * Launches Play Store and then searches for an application.
     */
    public static void searchGooglePlay(Instrumentation instrumentation, String appName) throws Exception {
        final UiDevice device = UiDevice.getInstance(instrumentation);
        final String playStore = "Play Store";
        final String application = appName;

        device.findObject(new UiSelector().text(playStore)).clickAndWaitForNewWindow();
        resetPlayStore(instrumentation);
        device.pressHome();
        device.findObject(new UiSelector().text(playStore)).clickAndWaitForNewWindow();

        boolean idleTextFieldExists = new Wait().until(new Wait.ExpectedCondition() {
            @Override
            public boolean isTrue() throws UiObjectNotFoundException {
                return device.findObject(
                        new UiSelector().resourceId(Res.GOOGLE_PLAY_IDLE_RES)).exists();
            }
        });

        if (idleTextFieldExists) {
            device.findObject(
                    new UiSelector().resourceId(Res.GOOGLE_PLAY_IDLE_RES)).click();
        }

        boolean inputTextFieldExists = new Wait().until(new Wait.ExpectedCondition() {
            @Override
            public boolean isTrue() throws UiObjectNotFoundException {
                return device.findObject(
                        new UiSelector().resourceId(Res.GOOGLE_PLAY_INPUT_RES)).exists();
            }
        });

        if (inputTextFieldExists) {
            UiObject inputTextField = device.findObject(
                    new UiSelector().resourceId(Res.GOOGLE_PLAY_INPUT_RES));
            inputTextField.clearTextField();
            inputTextField.setText(application);
            device.pressEnter();
        }
    }

    /**
     * Selects an application listed in the Play Store.
     */
    public static void selectFromGooglePlay(Instrumentation instrumentation, String appDescription) throws Exception {
        final UiDevice device = UiDevice.getInstance(instrumentation);
        final String playStore = "Play Store";
        final String application = appDescription;

        boolean isListed = new Wait().until(new Wait.ExpectedCondition() {
            @Override
            public boolean isTrue() throws UiObjectNotFoundException {
                return device.findObject(new UiSelector()
                        .description(application)).exists();
            }
        });

        if (isListed) {
            device.findObject(new UiSelector()
                    .description(application)).clickAndWaitForNewWindow();
        }
    }

    /**
     * Attempts to install an application from Google Play Store, if it is not already installed.
     * Returns true if the application has been installed, false if not.
     */
    public static boolean installApplication(Instrumentation instrumentation) throws Exception {
        final UiDevice device = UiDevice.getInstance(instrumentation);

        boolean isInstallable = new Wait().until(new Wait.ExpectedCondition() {
            @Override
            public boolean isTrue() throws UiObjectNotFoundException {
                return device.findObject(new UiSelector()
                        .text("INSTALL")).exists();
            }
        });

        if (!isInstallable) {
            return new Wait().until(new Wait.ExpectedCondition() {
                @Override
                public boolean isTrue() throws UiObjectNotFoundException {
                    return device.findObject(new UiSelector()
                            .text("OPEN")).exists();
                }
            });
        }

        device.findObject(new UiSelector().text("INSTALL")).clickAndWaitForNewWindow();
        new PlayStoreConfirmationWatcher(device).checkForCondition();

        UiObject openButton = device.findObject(new UiSelector().text("OPEN"));
        boolean isAppInstalled = openButton.waitForExists(TimeUnit.SECONDS.toMillis(60));

        return isAppInstalled;
    }

    /**
     * Attempts to uninstall an application from Google Play Store, if it is already installed.
     * Returns true if the application has been uninstalled, false if not.
     */
    public static boolean uninstallApplication(Instrumentation instrumentation) throws Exception {
        final UiDevice device = UiDevice.getInstance(instrumentation);

        boolean isUninstallable = new Wait().until(new Wait.ExpectedCondition() {
            @Override
            public boolean isTrue() throws UiObjectNotFoundException {
                return device.findObject(new UiSelector()
                        .text("UNINSTALL")).exists();
            }
        });

        if (!isUninstallable) {
            return new Wait().until(new Wait.ExpectedCondition() {
                @Override
                public boolean isTrue() throws UiObjectNotFoundException {
                    return device.findObject(new UiSelector()
                            .text("INSTALL")).exists();
                }
            });
        }

        device.findObject(new UiSelector().text("UNINSTALL")).clickAndWaitForNewWindow();
        device.findObject(new UiSelector().text("OK")).clickAndWaitForNewWindow();

        UiObject installButton = device.findObject(new UiSelector().text("INSTALL"));
        boolean isAppUninstalled = installButton.waitForExists(TimeUnit.SECONDS.toMillis(60));

        return isAppUninstalled;
    }
}