diff options
author | Tony <tonyswickham@gmail.com> | 2015-10-18 15:01:32 -0700 |
---|---|---|
committer | Tony <twickham@google.com> | 2015-12-04 12:06:13 -0800 |
commit | 2f04430fa4636519072b128dff9ddec9e879da6b (patch) | |
tree | 3fcf84bed3cae5a6c84176524f13908cb2d379a0 /tests/src | |
parent | bcec69525d8c4c64258f930608ab4e437368c058 (diff) | |
download | android_packages_apps_Trebuchet-2f04430fa4636519072b128dff9ddec9e879da6b.tar.gz android_packages_apps_Trebuchet-2f04430fa4636519072b128dff9ddec9e879da6b.tar.bz2 android_packages_apps_Trebuchet-2f04430fa4636519072b128dff9ddec9e879da6b.zip |
Added an instrumentation test case for quickly adding a widget.
This would have caught the race condition bugs that caused the "specified child
already has a parent" IllegalStateException (bug 23896857).
Change-Id: I3b408d21113237b0c89a0a7161f504596212bd58
Diffstat (limited to 'tests/src')
-rw-r--r-- | tests/src/com/android/launcher3/QuickAddWidgetTest.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/src/com/android/launcher3/QuickAddWidgetTest.java b/tests/src/com/android/launcher3/QuickAddWidgetTest.java new file mode 100644 index 000000000..8c563f380 --- /dev/null +++ b/tests/src/com/android/launcher3/QuickAddWidgetTest.java @@ -0,0 +1,81 @@ +package com.android.launcher3; + +import android.content.Intent; +import android.graphics.Point; +import android.os.SystemClock; +import android.support.test.uiautomator.By; +import android.support.test.uiautomator.Direction; +import android.support.test.uiautomator.UiDevice; +import android.support.test.uiautomator.UiObject2; +import android.support.test.uiautomator.Until; +import android.test.InstrumentationTestCase; +import android.view.MotionEvent; +import android.view.ViewConfiguration; + +import java.util.List; + +/** + * Add an arbitrary widget from the widget picker very quickly to test potential race conditions. + */ +public class QuickAddWidgetTest extends InstrumentationTestCase { + private UiDevice mDevice; + private String mTargetPackage; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + mDevice = UiDevice.getInstance(getInstrumentation()); + + // Set Launcher3 as home. + mTargetPackage = getInstrumentation().getTargetContext().getPackageName(); + Intent homeIntent = new Intent(Intent.ACTION_MAIN) + .addCategory(Intent.CATEGORY_HOME) + .setPackage(mTargetPackage) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + getInstrumentation().getContext().startActivity(homeIntent); + mDevice.wait(Until.hasObject(By.pkg(mTargetPackage).depth(0)), 3000); + } + + public void testAddWidgetQuickly() throws Exception { + mDevice.pressMenu(); // Enter overview mode. + mDevice.wait(Until.findObject(By.text("Widgets")), 3000).click(); + UiObject2 calendarWidget = getWidgetByName("Calendar"); + Point center = calendarWidget.getVisibleCenter(); + // Touch widget just long enough to pick it up (longPressTimeout), then let go immediately. + getInstrumentation().sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), + SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, center.x, center.y, 0)); + Thread.sleep(ViewConfiguration.getLongPressTimeout()); + getInstrumentation().sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), + SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, center.x, center.y, 0)); + + assertTrue("Drag was never started", isOnHomescreen()); + } + + private UiObject2 getWidgetByName(String name) { + UiObject2 widgetsList = mDevice.wait(Until.findObject(By.res(mTargetPackage, + "widgets_list_view")), 3000); + do { + UiObject2 widget = getVisibleWidgetByName(name); + if (widget != null) { + return widget; + } + } while (widgetsList.scroll(Direction.DOWN, 1f)); + return getVisibleWidgetByName(name); + } + + private UiObject2 getVisibleWidgetByName(String name) { + List<UiObject2> visibleWidgets = mDevice.wait(Until.findObjects(By.clazz( + "android.widget.LinearLayout")), 3000); + for (UiObject2 widget : visibleWidgets) { + if (widget.hasObject(By.text(name))) { + return widget; + } + } + return null; + } + + private boolean isOnHomescreen() { + return mDevice.wait(Until.hasObject(By.res(mTargetPackage, "hotseat")), 3000); + } +} |