OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.content.browser; |
| 6 |
| 7 import android.content.ClipData; |
| 8 import android.content.ClipboardManager; |
| 9 import android.content.Context; |
| 10 import android.test.suitebuilder.annotation.MediumTest; |
| 11 import android.text.Editable; |
| 12 import android.text.Selection; |
| 13 |
| 14 import org.chromium.base.test.util.Feature; |
| 15 import org.chromium.content.browser.test.util.Criteria; |
| 16 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 17 import org.chromium.content.browser.test.util.TouchUtils; |
| 18 import org.chromium.content_shell.ContentShellTestBase; |
| 19 |
| 20 public class InsertionHandleTest extends ContentShellTestBase { |
| 21 |
| 22 private static final String FILENAME = "content/insertion_handle/editable_lo
ng_text.html"; |
| 23 // Offset to compensate for the fact that the handle is below the text. |
| 24 private static final int VERTICAL_OFFSET = 10; |
| 25 // These positions should both be in the text area and not within |
| 26 // HANDLE_POSITON_TOLERANCE of each other in either coordinate. |
| 27 private static final int INITIAL_CLICK_X = 100; |
| 28 private static final int INITIAL_CLICK_Y = 100; |
| 29 private static final int DRAG_TO_X = 287; |
| 30 private static final int DRAG_TO_Y = 199; |
| 31 private static final int HANDLE_POSITION_TOLERANCE = 60; |
| 32 private static final String PASTE_TEXT = "**test text to paste**"; |
| 33 |
| 34 @MediumTest |
| 35 @Feature({"TextSelection", "TextInput", "Main"}) |
| 36 public void testDragInsertionHandle() throws Throwable { |
| 37 startActivityWithTestUrl(FILENAME); |
| 38 |
| 39 clickToShowInsertionHandle(); |
| 40 |
| 41 InsertionHandleController ihc = getContentViewCore().getInsertionHandleC
ontrollerForTest(); |
| 42 HandleView handle = ihc.getHandleViewForTest(); |
| 43 int initialX = handle.getPositionX(); |
| 44 int initialY = handle.getPositionY(); |
| 45 |
| 46 TouchUtils.dragCompleteView(getInstrumentation(), getContentView(), init
ialX, DRAG_TO_X, |
| 47 initialY + VERTICAL_OFFSET, DRAG_TO_Y, 5); |
| 48 |
| 49 assertTrue(waitForHandleNear(handle, DRAG_TO_X, DRAG_TO_Y)); |
| 50 |
| 51 // Unselecting should cause the handle to disappear. |
| 52 getImeAdapter().unselect(); |
| 53 assertTrue(waitForHandleShowingEquals(false)); |
| 54 } |
| 55 |
| 56 private static boolean isHandleNear(HandleView handle, int x, int y) { |
| 57 return (Math.abs(handle.getPositionX() - x) < HANDLE_POSITION_TOLERANCE)
&& |
| 58 (Math.abs(handle.getPositionY() - y) < HANDLE_POSITION_TOLERANCE
); |
| 59 } |
| 60 |
| 61 private static boolean waitForHandleNear(final HandleView handle, final int
x, |
| 62 final int y) throws Throwable { |
| 63 return CriteriaHelper.pollForCriteria(new Criteria() { |
| 64 @Override |
| 65 public boolean isSatisfied() { |
| 66 return isHandleNear(handle, x, y); |
| 67 } |
| 68 }); |
| 69 } |
| 70 |
| 71 private boolean waitForHandleShowingEquals(final boolean shouldBeShowing) |
| 72 throws Throwable { |
| 73 return CriteriaHelper.pollForCriteria(new Criteria() { |
| 74 @Override |
| 75 public boolean isSatisfied() { |
| 76 InsertionHandleController ihc = |
| 77 getContentViewCore().getInsertionHandleControllerForTest
(); |
| 78 boolean isShowing = ihc != null && ihc.isShowing(); |
| 79 return isShowing == shouldBeShowing; |
| 80 } |
| 81 }); |
| 82 } |
| 83 |
| 84 @MediumTest |
| 85 @Feature({"TextSelection", "TextInput"}) |
| 86 public void testPasteAtInsertionHandle() throws Throwable { |
| 87 startActivityWithTestUrl(FILENAME); |
| 88 |
| 89 clickToShowInsertionHandle(); |
| 90 |
| 91 assertTrue(waitForHasSelectionPosition()); |
| 92 |
| 93 // See below. Removed assignments to satisfy findbugs. |
| 94 // int offset = getSelectionStart(); |
| 95 // String initialText = getEditableText(); |
| 96 |
| 97 saveToClipboard(PASTE_TEXT); |
| 98 pasteOnMainSync(); |
| 99 |
| 100 // TODO(aurimas): The ImeAdapter is often not receiving the correct upda
te from the paste, |
| 101 // and so the editableText is never equal to the expectedText. Enable th
is check when fixed. |
| 102 // http://crbug.com/163966 |
| 103 // String expectedText = |
| 104 // initialText.substring(0, offset) + PASTE_TEXT + initialText.su
bstring(offset); |
| 105 // assertTrue(waitForEditableTextEquals(expectedText)); |
| 106 |
| 107 assertTrue(waitForHandleShowingEquals(false)); |
| 108 } |
| 109 |
| 110 private boolean waitForEditableTextEquals(final String expectedText) |
| 111 throws Throwable { |
| 112 return CriteriaHelper.pollForCriteria(new Criteria() { |
| 113 @Override |
| 114 public boolean isSatisfied() { |
| 115 return getEditableText().trim().equals(expectedText.trim()); |
| 116 } |
| 117 }); |
| 118 } |
| 119 |
| 120 private boolean waitForHasSelectionPosition() |
| 121 throws Throwable { |
| 122 return CriteriaHelper.pollForCriteria(new Criteria() { |
| 123 @Override |
| 124 public boolean isSatisfied() { |
| 125 int start = getSelectionStart(); |
| 126 int end = getSelectionEnd(); |
| 127 return start > 0 && start == end; |
| 128 } |
| 129 }); |
| 130 } |
| 131 |
| 132 private void pasteOnMainSync() { |
| 133 getInstrumentation().runOnMainSync(new Runnable() { |
| 134 @Override |
| 135 public void run() { |
| 136 getContentViewCore().getInsertionHandleControllerForTest().paste
(); |
| 137 } |
| 138 }); |
| 139 } |
| 140 |
| 141 @Override |
| 142 protected void tearDown() throws Exception { |
| 143 super.tearDown(); |
| 144 // No way to just clear clipboard, so setting to empty string instead. |
| 145 saveToClipboard(""); |
| 146 } |
| 147 |
| 148 private void clickToShowInsertionHandle() throws Throwable { |
| 149 TouchUtils.singleClickView(getInstrumentation(), getContentView(), INITI
AL_CLICK_X, |
| 150 INITIAL_CLICK_Y); |
| 151 TouchUtils.sleepForDoubleTapTimeout(getInstrumentation()); |
| 152 TouchUtils.singleClickView(getInstrumentation(), getContentView(), INITI
AL_CLICK_X, |
| 153 INITIAL_CLICK_Y); |
| 154 assertTrue(waitForHandleShowingEquals(true)); |
| 155 assertTrue(waitForZoomFinished()); |
| 156 } |
| 157 |
| 158 private boolean waitForZoomFinished() throws Throwable { |
| 159 // If the polling interval is too short, slowly zooming may be detected
as not zooming. |
| 160 final int POLLING_INTERVAL = 200; |
| 161 return CriteriaHelper.pollForCriteria(new Criteria() { |
| 162 int mPositionX = -1; |
| 163 int mPositionY = -1; |
| 164 @Override |
| 165 public boolean isSatisfied() { |
| 166 int lastPositionX = mPositionX; |
| 167 int lastPositionY = mPositionY; |
| 168 InsertionHandleController ihc = |
| 169 getContentViewCore().getInsertionHandleControllerForTest
(); |
| 170 HandleView handle = ihc.getHandleViewForTest(); |
| 171 mPositionX = handle.getPositionX(); |
| 172 mPositionY = handle.getPositionY(); |
| 173 return mPositionX == lastPositionX && mPositionY == lastPosition
Y; |
| 174 } |
| 175 }, CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL, POLLING_INTERVAL); |
| 176 } |
| 177 |
| 178 private int getSelectionStart() { |
| 179 return Selection.getSelectionStart(getEditable()); |
| 180 } |
| 181 |
| 182 private int getSelectionEnd() { |
| 183 return Selection.getSelectionEnd(getEditable()); |
| 184 } |
| 185 |
| 186 private Editable getEditable() { |
| 187 return getContentViewCore().getEditableForTest(); |
| 188 } |
| 189 |
| 190 private String getEditableText() { |
| 191 return getEditable().toString(); |
| 192 } |
| 193 |
| 194 private void saveToClipboard(String text) { |
| 195 ClipboardManager clipMgr = |
| 196 (ClipboardManager) getActivity().getSystemService(Context.CLIPBO
ARD_SERVICE); |
| 197 clipMgr.setPrimaryClip(ClipData.newPlainText(null, text)); |
| 198 } |
| 199 |
| 200 private ImeAdapter getImeAdapter() { |
| 201 return getContentViewCore().getImeAdapterForTest(); |
| 202 } |
| 203 } |
OLD | NEW |