Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: content/public/android/javatests/src/org/chromium/content/browser/input/SelectionHandleUnitTest.java

Issue 24449007: [Android] Allow text handles to observe position of "parent" view (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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.input;
6
7 import android.content.Context;
8 import android.test.AndroidTestCase;
9 import android.test.suitebuilder.annotation.SmallTest;
10 import android.view.View;
11
12 import java.util.HashSet;
13
14 import org.chromium.base.test.util.Feature;
15 import org.chromium.content.browser.PositionObserverInterface;
16
17 /**
18 * Test suite for Selection handles.
19 */
20 public class SelectionHandleUnitTest extends AndroidTestCase {
21 public class FakeHandleViewDelegate implements HandleView.Delegate {
22 public Context getContext() {
23 return SelectionHandleUnitTest.this.getContext();
24 }
25
26 public boolean isPositionVisible(int x, int y) {
27 return true;
28 }
29
30 public void dismissContainer() {
31 }
32
33 public boolean isContainerShowing() {
34 return true;
35 }
36
37 public void showContainerAtPosition(View view, int x, int y) {
38 mContainerPositionX = x;
39 mContainerPositionY = y;
40 }
41
42 public void updateContainerPosition(int x, int y, int width, int height) {
43 mContainerPositionX = x;
44 mContainerPositionY = y;
45 }
46
47 public int getPositionX() {
48 return mContainerPositionX;
Ted C 2013/10/10 18:16:32 indent is off
49 }
50
51 public int getPositionY() {
52 return mContainerPositionY;
Ted C 2013/10/10 18:16:32 here too
53 }
54
55 private int mContainerPositionX;
Ted C 2013/10/10 18:16:32 member variables at the top
56 private int mContainerPositionY;
57 }
58
59 public class FakePositionObserver implements PositionObserverInterface {
60 FakePositionObserver() {
61 mListeners = new HashSet<Listener>();
62 }
63
64 public int getPositionX() {
65 return mX;
66 }
67
68 public int getPositionY() {
69 return mY;
70 }
71
72 public void addListener(Listener listener) {
73 mListeners.add(listener);
74 }
75
76 public void removeListener(Listener listener) {
77 mListeners.remove(listener);
78 }
79
80 public void moveTo(int x, int y) {
81 mX = x;
82 mY = y;
83 for (Listener l : mListeners) {
84 l.onPositionChanged(x, y);
85 }
86 }
87
88 private int mX;
Ted C 2013/10/10 18:16:32 same
89 private int mY;
90 private HashSet<Listener> mListeners;
91 }
92
93 private interface SelectBetweenCoordinatesCallback {
94 public void Run(int x1, int y1, int x2, int y2);
95 }
96
97 @Override
98 public void setUp() throws Exception {
99 super.setUp();
100 mFakePositionObserver = new FakePositionObserver();
101 mStartHandleDelegate = new FakeHandleViewDelegate();
102 mEndHandleDelegate = new FakeHandleViewDelegate();
103 mSelectBetweenCoordinatesCallback = null;
104 mController = new SelectionHandleController(
105 mStartHandleDelegate, mEndHandleDelegate, mFakePositionObserver) {
106 @Override
107 public void selectBetweenCoordinates(int x1, int y1, int x2, int y2) {
108 if (mSelectBetweenCoordinatesCallback != null) {
109 mSelectBetweenCoordinatesCallback.Run(x1, y1, x2, y2);
110 }
111 }
112 };
113 }
114
115 /**
116 * Verifies that selection handles are shown when selection changes if allow AutomaticShowing
117 * has been called and that they are not shown if hideAndDisallowAutomaticSh owing was called.
118 *
119 * Also verifies that they are shown at the expected position.
120 */
121 @SmallTest
122 @Feature({ "TextSelection" })
123 public void testShowSelectionHandles() throws Throwable {
124 mController.hideAndDisallowAutomaticShowing();
125 assertFalse(mController.isShowing());
126
127 int startHandleX = 10;
128 int startHandleY = 20;
129 int endHandleX = 30;
130 int endHandleY = 40;
131
132 mController.onSelectionChanged(1, 1);
133 assertFalse(mController.isShowing());
134
135 mController.allowAutomaticShowing();
136 mController.onSelectionChanged(1, 1);
137 assertTrue(mController.isShowing());
138
139 mController.setStartHandlePosition(startHandleX, startHandleY);
140 mController.setEndHandlePosition(endHandleX, endHandleY);
141
142 assertEquals(startHandleX, getStartHandlePositionX());
143 assertEquals(startHandleY, getStartHandlePositionY());
144 assertEquals(endHandleX, getEndHandlePositionX());
145 assertEquals(endHandleY, getEndHandlePositionY());
146 }
147
148 /**
149 * Verifies that the selection handle positions are updated as expected when the observed
150 * position (i.e. of the containing view) is changed.
151 */
152 @SmallTest
153 @Feature({ "TextSelection" })
154 public void testMoveSelectionHandleContainer() throws Throwable {
155 mController.allowAutomaticShowing();
156
157 int startHandleX = 10;
158 int startHandleY = 20;
159 int endHandleX = 30;
160 int endHandleY = 40;
161
162 mController.onSelectionChanged(1, 1);
163 mController.setStartHandlePosition(startHandleX, startHandleY);
164 mController.setEndHandlePosition(endHandleX, endHandleY);
165
166 assertTrue(mController.isShowing());
167
168 int containerPositionX = 0;
169 int containerPositionY = 0;
170
171 int previousStartHandleDelegatePositionX = mStartHandleDelegate.getPosit ionX();
172 int previousStartHandleDelegatePositionY = mStartHandleDelegate.getPosit ionY();
173 int previousEndHandleDelegatePositionX = mEndHandleDelegate.getPositionX ();
174 int previousEndHandleDelegatePositionY = mEndHandleDelegate.getPositionY ();
175
176 assertEquals(startHandleX, getStartHandlePositionX());
177 assertEquals(startHandleY, getStartHandlePositionY());
178 assertEquals(endHandleX, getEndHandlePositionX());
179 assertEquals(endHandleY, getEndHandlePositionY());
180
181 containerPositionX = 20;
182 containerPositionY = 30;
183
184 mFakePositionObserver.moveTo(containerPositionX, containerPositionY);
185
186 assertEquals(startHandleX + containerPositionX, getStartHandlePositionX( ));
187 assertEquals(startHandleY + containerPositionY, getStartHandlePositionY( ));
188 assertEquals(endHandleX + containerPositionX, getEndHandlePositionX());
189 assertEquals(endHandleY + containerPositionY, getEndHandlePositionY());
190
191
192 // Check that the handle has updated the position of the container in th e delegate by the
193 // movement in the position observer.
194 assertEquals(previousStartHandleDelegatePositionX + containerPositionX,
195 mStartHandleDelegate.getPositionX());
Ted C 2013/10/10 18:16:32 +4 indent on all of these
196 assertEquals(previousStartHandleDelegatePositionY + containerPositionY,
197 mStartHandleDelegate.getPositionY());
198 assertEquals(previousEndHandleDelegatePositionX + containerPositionX,
199 mEndHandleDelegate.getPositionX());
200 assertEquals(previousEndHandleDelegatePositionY + containerPositionY,
201 mEndHandleDelegate.getPositionY());
202 }
203
204 private int getStartHandlePositionX() {
205 return getStartHandle().getRootViewRelativePositionX();
Ted C 2013/10/10 18:16:32 the next 4 methods need an extra +2 indent on thei
206 }
207
208 private int getStartHandlePositionY() {
209 return getStartHandle().getRootViewRelativePositionY();
210 }
211
212 private int getEndHandlePositionX() {
213 return getEndHandle().getRootViewRelativePositionX();
214 }
215
216 private int getEndHandlePositionY() {
217 return getEndHandle().getRootViewRelativePositionY();
218 }
219
220 private HandleView getStartHandle() {
221 return mController.getStartHandleViewForTest();
222 }
223
224 private HandleView getEndHandle() {
225 return mController.getEndHandleViewForTest();
226 }
227
228 private SelectBetweenCoordinatesCallback mSelectBetweenCoordinatesCallback;
229 private FakePositionObserver mFakePositionObserver;
Ted C 2013/10/10 18:16:32 to the top here as well
230 private FakeHandleViewDelegate mStartHandleDelegate;
231 private FakeHandleViewDelegate mEndHandleDelegate;
232 private SelectionHandleController mController;
233
234 }
235
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698