summaryrefslogtreecommitdiff
path: root/libraries/app-helpers/core/src/android/platform/helpers/AbstractStandardAppHelper.java
blob: 3310915415d37cf90db0e1d6b691e0a0327b0953 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * 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 android.platform.helpers;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Instrumentation;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.os.RemoteException;
import android.os.SystemClock;
import android.platform.helpers.exceptions.AccountException;
import android.platform.helpers.exceptions.TestHelperException;
import android.platform.helpers.exceptions.UnknownUiException;
import android.platform.helpers.watchers.AppIsNotRespondingWatcher;
import android.support.test.launcherhelper.ILauncherStrategy;
import android.support.test.launcherhelper.LauncherStrategyFactory;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiWatcher;
import android.support.test.uiautomator.Until;
import androidx.test.InstrumentationRegistry;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public abstract class AbstractStandardAppHelper implements IAppHelper {
    private static final String LOG_TAG = AbstractStandardAppHelper.class.getSimpleName();
    private static final String SCREENSHOT_DIR = "apphelper-screenshots";
    private static final String FAVOR_CMD = "favor-shell-commands";
    private static final String USE_HOME_CMD = "press-home-to-exit";
    private static final String APP_IDLE_OPTION = "app-idle_ms";
    private static final String LAUNCH_TIMEOUT_OPTION = "app-launch-timeout_ms";
    private static final String ERROR_NOT_FOUND =
        "Element %s %s is not found in the application %s";

    private static final long EXIT_WAIT_TIMEOUT = TimeUnit.SECONDS.toMillis(5);

    private static File sScreenshotDirectory;

    public UiDevice mDevice;
    public Instrumentation mInstrumentation;
    public ILauncherStrategy mLauncherStrategy;
    private final KeyCharacterMap mKeyCharacterMap =
            KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
    private final boolean mFavorShellCommands;
    private final boolean mPressHomeToExit;
    private final long mAppIdle;
    private final long mLaunchTimeout;

    public AbstractStandardAppHelper(Instrumentation instr) {
        mInstrumentation = instr;
        mDevice = UiDevice.getInstance(instr);
        mFavorShellCommands =
                Boolean.valueOf(
                        InstrumentationRegistry.getArguments().getString(FAVOR_CMD, "false"));
        mPressHomeToExit =
                Boolean.valueOf(
                        InstrumentationRegistry.getArguments().getString(USE_HOME_CMD, "false"));
        mAppIdle =
                Long.valueOf(
                        InstrumentationRegistry.getArguments()
                                .getString(
                                        APP_IDLE_OPTION,
                                        String.valueOf(TimeUnit.SECONDS.toMillis(0))));
        //TODO(b/127356533): Choose a sensible default for app launch timeout after b/125356281.
        mLaunchTimeout =
                Long.valueOf(
                        InstrumentationRegistry.getArguments()
                                .getString(
                                        LAUNCH_TIMEOUT_OPTION,
                                        String.valueOf(TimeUnit.SECONDS.toMillis(30))));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void open() {
        // Turn on the screen if necessary.
        try {
            if (!mDevice.isScreenOn()) {
                mDevice.wakeUp();
            }
        } catch (RemoteException e) {
            throw new TestHelperException("Could not unlock the device.", e);
        }
        // Unlock the screen if necessary.
        if (mDevice.hasObject(By.res("com.android.systemui", "keyguard_bottom_area"))) {
            mDevice.pressMenu();
            mDevice.waitForIdle();
        }
        // Launch the application as normal.
        String pkg = getPackage();
        long launchInitiationTimeMs = System.currentTimeMillis();

        registerDialogWatchers();
        if (mFavorShellCommands) {
            String output = null;
            try {
                Log.i(LOG_TAG, String.format("Sending command to launch: %s", pkg));
                mInstrumentation.getContext().startActivity(getOpenAppIntent());
            } catch (ActivityNotFoundException e) {
                removeDialogWatchers();
                throw new TestHelperException(String.format("Failed to find package: %s", pkg), e);
            }
        } else {
            // Launch using the UI and launcher strategy.
            String id = getLauncherName();
            if (!mDevice.hasObject(By.pkg(pkg).depth(0))) {
                getLauncherStrategy().launch(id, pkg);
                Log.i(LOG_TAG, "Launched package: id=" + id + ", pkg=" + pkg);
            }
        }

        // Ensure the package is in the foreground for success.
        if (!mDevice.wait(Until.hasObject(By.pkg(pkg).depth(0)), mLaunchTimeout)) {
            removeDialogWatchers();
            throw new IllegalStateException(
                    String.format(
                            "Did not find package, %s, in foreground after %d ms.",
                            pkg, System.currentTimeMillis() - launchInitiationTimeMs));
        }
        removeDialogWatchers();
        // Idle for specified time after app launch
        idleApp();
    }

    private void idleApp() {
        if (mAppIdle != 0) {
            Log.v(LOG_TAG, String.format("Idle app for %d ms", mAppIdle));
            SystemClock.sleep(mAppIdle);
        }
    }

    /**
     * Returns the {@code Intent} used by {@code open()} to launch an {@code Activity}. The default
     * implementation launches the default {@code Activity} of the package. Override this method to
     * launch a different {@code Activity}.
     */
    public Intent getOpenAppIntent() {
        Intent intent =
                mInstrumentation
                        .getContext()
                        .getPackageManager()
                        .getLaunchIntentForPackage(getPackage());
        if (intent == null) {
            throw new IllegalStateException(
                    String.format("Failed to get intent of package: %s", getPackage()));
        }
        return intent;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void exit() {
        Log.i(LOG_TAG, "Exiting the current application.");
        if (mPressHomeToExit) {
            mDevice.pressHome();
            mDevice.waitForIdle();
        } else {
            int maxBacks = 4;
            while (!mDevice.hasObject(getLauncherStrategy().getWorkspaceSelector())
                    && maxBacks > 0) {
                mDevice.pressBack();
                mDevice.waitForIdle();
                maxBacks--;
            }

            if (maxBacks == 0) {
                mDevice.pressHome();
            }
        }
        if (!mDevice.wait(
                Until.hasObject(mLauncherStrategy.getWorkspaceSelector()), EXIT_WAIT_TIMEOUT)) {
            throw new IllegalStateException("Failed to exit the app to launcher.");
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getVersion() throws NameNotFoundException {
        String pkg = getPackage();

        if (null == pkg || pkg.isEmpty()) {
            throw new TestHelperException("Cannot find version of empty package");
        }
        PackageManager pm = mInstrumentation.getContext().getPackageManager();
        PackageInfo pInfo = pm.getPackageInfo(pkg, 0);
        String version = pInfo.versionName;
        if (null == version || version.isEmpty()) {
            throw new TestHelperException(
                    String.format("Version isn't found for package, %s", pkg));
        }

        return version;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isAppInForeground() {
        return mDevice.hasObject(By.pkg(getPackage()).depth(0));
    }

    protected int getOrientation() {
        return mInstrumentation.getContext().getResources().getConfiguration().orientation;
    }

    protected void requiresGoogleAccount() {
        if (!hasRegisteredGoogleAccount()) {
            throw new AccountException("This method requires a Google account be registered.");
        }
    }

    protected void requiresNoGoogleAccount() {
        if (hasRegisteredGoogleAccount()) {
            throw new AccountException("This method requires no Google account be registered.");
        }
    }

    protected boolean hasRegisteredGoogleAccount() {
        Context context = mInstrumentation.getContext();
        Account[] accounts = AccountManager.get(context).getAccounts();
        for (int i = 0; i < accounts.length; ++i) {
            if (accounts[i].type.equals("com.google")) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean captureScreenshot(String name) throws IOException {
        File scrOut = File.createTempFile(name, ".png", getScreenshotDirectory());
        File uixOut = File.createTempFile(name, ".uix", getScreenshotDirectory());
        mDevice.dumpWindowHierarchy(uixOut);
        return mDevice.takeScreenshot(scrOut);
    }

    private static File getScreenshotDirectory() {
        if (sScreenshotDirectory == null) {
            File storage = Environment.getExternalStorageDirectory();
            sScreenshotDirectory = new File(storage, SCREENSHOT_DIR);
            if (!sScreenshotDirectory.exists()) {
                if (!sScreenshotDirectory.mkdirs()) {
                    throw new TestHelperException("Failed to create a screenshot directory.");
                }
            }
        }
        return sScreenshotDirectory;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean sendTextEvents(String text, long delay) {
      Log.v(LOG_TAG, String.format("Sending text events for %s", text));
      KeyEvent[] events = mKeyCharacterMap.getEvents(text.toCharArray());
      for (KeyEvent event : events) {
        if (KeyEvent.ACTION_DOWN == event.getAction()) {
          if (!mDevice.pressKeyCode(event.getKeyCode(), event.getMetaState())) {
            return false;
          }
          SystemClock.sleep(delay);
        }
      }
      return true;
    }

    protected UiObject2 findElementById(String id) {
      UiObject2 element = mDevice.findObject(By.res(getPackage(), id));
      if (element != null) {
        return element;
      } else {
        throw new UnknownUiException(String.format(ERROR_NOT_FOUND, "with id", id, getPackage()));
      }
    }

    protected UiObject2 findElementByText(String text) {
      UiObject2 element = mDevice.findObject(By.text(text));
      if (element != null) {
        return element;
      } else {
        throw new UnknownUiException(
            String.format(ERROR_NOT_FOUND, "with text", text, getPackage()));
      }
    }

    protected UiObject2 findElementByDescription(String description) {
      UiObject2 element = mDevice.findObject(By.desc(description));
      if (element != null) {
        return element;
      } else {
        throw new UnknownUiException(
            String.format(ERROR_NOT_FOUND, "with description", description, getPackage()));
      }
    }

    protected void clickOn(UiObject2 element) {
      if (element != null) {
        element.click();
      } else {
        throw new UnknownUiException(String.format(ERROR_NOT_FOUND, "", "", getPackage()));
      }
    }

    protected void waitAndClickById(String packageStr, String id, long timeout) {
      clickOn(mDevice.wait(Until.findObject(By.res(packageStr, id)), timeout));
    }

    protected void waitAndClickByText(String text, long timeout) {
      clickOn(mDevice.wait(Until.findObject(By.text(text)), timeout));
    }

    protected void waitAndClickByDescription(String description, long timeout) {
      clickOn(mDevice.wait(Until.findObject(By.desc(description)), timeout));
    }


    protected void checkElementWithIdExists(String packageStr, String id, long timeout) {
      if (!mDevice.wait(Until.hasObject(By.res(packageStr, id)), timeout)) {
        throw new UnknownUiException(String.format(ERROR_NOT_FOUND, "with id", id, getPackage()));
        }
    }

    protected void checkElementWithTextExists(String text, long timeout) {
      if (!mDevice.wait(Until.hasObject(By.text(text)), timeout)) {
        throw new UnknownUiException(
            String.format(ERROR_NOT_FOUND, "with text", text, getPackage()));
        }
    }

    protected void checkElementWithDescriptionExists(String description, long timeout) {
      if (!mDevice.wait(Until.hasObject(By.desc(description)), timeout)) {
        throw new UnknownUiException(
            String.format(ERROR_NOT_FOUND, "with description", description, getPackage()));
      }
    }

    protected void checkIfElementChecked(UiObject2 element) {
      if (!element.isChecked()) {
        throw new UnknownUiException("Element " + element + " is not checked");
      }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void registerWatcher(String name, UiWatcher watcher) {
        mDevice.registerWatcher(name, watcher);
    }

    /**
    * {@inheritDoc}
    */
    @Override
    public void removeWatcher(String name) {
      mDevice.removeWatcher(name);
    }

    private void registerDialogWatchers() {
        registerWatcher(
                AppIsNotRespondingWatcher.class.getSimpleName(),
                new AppIsNotRespondingWatcher(InstrumentationRegistry.getInstrumentation()));
    }

    private void removeDialogWatchers() {
        removeWatcher(AppIsNotRespondingWatcher.class.getSimpleName());
    }

    private ILauncherStrategy getLauncherStrategy() {
        if (mLauncherStrategy == null) {
            mLauncherStrategy = LauncherStrategyFactory.getInstance(mDevice).getLauncherStrategy();
        }
        return mLauncherStrategy;
    }
}