Launchable.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2018 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. package com.android.launcher3.tapl;
  17. import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
  18. import android.graphics.Point;
  19. import androidx.test.uiautomator.By;
  20. import androidx.test.uiautomator.BySelector;
  21. import androidx.test.uiautomator.UiObject2;
  22. import androidx.test.uiautomator.Until;
  23. /**
  24. * Ancestor for AppIcon and AppMenuItem.
  25. */
  26. abstract class Launchable {
  27. protected final LauncherInstrumentation mLauncher;
  28. protected final UiObject2 mObject;
  29. Launchable(LauncherInstrumentation launcher, UiObject2 object) {
  30. mObject = object;
  31. mLauncher = launcher;
  32. }
  33. UiObject2 getObject() {
  34. return mObject;
  35. }
  36. /**
  37. * Clicks the object to launch its app.
  38. */
  39. public Background launch(String expectedPackageName) {
  40. try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) {
  41. return launch(By.pkg(expectedPackageName));
  42. }
  43. }
  44. protected abstract void expectActivityStartEvents();
  45. protected abstract String launchableType();
  46. private Background launch(BySelector selector) {
  47. try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
  48. "want to launch an app from " + launchableType())) {
  49. LauncherInstrumentation.log("Launchable.launch before click "
  50. + mObject.getVisibleCenter() + " in " + mLauncher.getVisibleBounds(mObject));
  51. final String label = mObject.getText();
  52. mLauncher.executeAndWaitForEvent(
  53. () -> {
  54. mLauncher.clickLauncherObject(mObject);
  55. expectActivityStartEvents();
  56. },
  57. event -> event.getEventType() == TYPE_WINDOW_STATE_CHANGED,
  58. () -> "Launching an app didn't open a new window: " + label,
  59. "clicking " + launchableType());
  60. try (LauncherInstrumentation.Closable c1 = mLauncher.addContextLayer("clicked")) {
  61. mLauncher.assertTrue(
  62. "App didn't start: " + label + " (" + selector + ")",
  63. TestHelpers.wait(Until.hasObject(selector),
  64. LauncherInstrumentation.WAIT_TIME_MS));
  65. return new Background(mLauncher);
  66. }
  67. }
  68. }
  69. /**
  70. * Drags an object to the center of homescreen.
  71. *
  72. * @param startsActivity whether it's expected to start an activity.
  73. * @param isWidgetShortcut whether we drag a widget shortcut
  74. */
  75. public void dragToWorkspace(boolean startsActivity, boolean isWidgetShortcut) {
  76. try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) {
  77. final Point launchableCenter = getObject().getVisibleCenter();
  78. final Point displaySize = mLauncher.getRealDisplaySize();
  79. final int width = displaySize.x / 2;
  80. Workspace.dragIconToWorkspace(
  81. mLauncher,
  82. this,
  83. new Point(
  84. launchableCenter.x >= width
  85. ? launchableCenter.x - width / 2
  86. : launchableCenter.x + width / 2,
  87. displaySize.y / 2),
  88. getLongPressIndicator(),
  89. startsActivity,
  90. isWidgetShortcut,
  91. () -> addExpectedEventsForLongClick());
  92. }
  93. }
  94. protected abstract void addExpectedEventsForLongClick();
  95. protected abstract String getLongPressIndicator();
  96. }