OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.test.util; |
| 6 |
| 7 import android.graphics.Matrix; |
| 8 import android.graphics.RectF; |
| 9 import android.text.SpannedString; |
| 10 |
| 11 import org.chromium.base.VisibleForTesting; |
| 12 import org.chromium.content.browser.input.CursorAnchorInfoWrapper; |
| 13 |
| 14 import java.util.ArrayList; |
| 15 import java.util.Arrays; |
| 16 |
| 17 /** |
| 18 * Mock implementation of {@link CursorAnchorInfoWrapper}. |
| 19 */ |
| 20 @VisibleForTesting |
| 21 public class TestCursorAnchorInfoWrapper implements CursorAnchorInfoWrapper { |
| 22 |
| 23 /** |
| 24 * Mock implementation of |
| 25 * {@link org.chromium.content.browser.input.CursorAnchorInfoWrapper.Builder
}. |
| 26 */ |
| 27 public static final class Builder implements CursorAnchorInfoWrapper.Builder
{ |
| 28 private int mSelectionStart = -1; |
| 29 private int mSelectionEnd = -1; |
| 30 private int mComposingTextStart = -1; |
| 31 private CharSequence mComposingText = null; |
| 32 private float mInsertionMarkerHorizontal = Float.NaN; |
| 33 private float mInsertionMarkerTop = Float.NaN; |
| 34 private float mInsertionMarkerBaseline = Float.NaN; |
| 35 private float mInsertionMarkerBottom = Float.NaN; |
| 36 private int mInsertionMarkerFlags = 0; |
| 37 private final ArrayList<Integer> mCharacterBoundsIndexList = new ArrayLi
st<>(); |
| 38 private final ArrayList<Integer> mCharacterBoundsFlagsList = new ArrayLi
st<>(); |
| 39 private final ArrayList<RectF> mCharacterBoundsRectList = new ArrayList<
>(); |
| 40 private final Matrix mMatrix = new Matrix(); |
| 41 |
| 42 @Override |
| 43 public Builder setSelectionRange(int newStart, int newEnd) { |
| 44 mSelectionStart = newStart; |
| 45 mSelectionEnd = newEnd; |
| 46 return this; |
| 47 } |
| 48 |
| 49 @Override |
| 50 public Builder setComposingText(int composingTextStart, CharSequence com
posingText) { |
| 51 mComposingTextStart = composingTextStart; |
| 52 mComposingText = composingText == null ? null : new SpannedString(co
mposingText); |
| 53 return this; |
| 54 } |
| 55 |
| 56 @Override |
| 57 public Builder setInsertionMarkerLocation(float horizontalPosition, |
| 58 float lineTop, float lineBaseline, float lineBottom, int flags)
{ |
| 59 mInsertionMarkerHorizontal = horizontalPosition; |
| 60 mInsertionMarkerTop = lineTop; |
| 61 mInsertionMarkerBaseline = lineBaseline; |
| 62 mInsertionMarkerBottom = lineBottom; |
| 63 mInsertionMarkerFlags = flags; |
| 64 return this; |
| 65 } |
| 66 |
| 67 @Override |
| 68 public Builder addCharacterBounds(int index, float left, float top, floa
t right, |
| 69 float bottom, int flags) { |
| 70 if (mCharacterBoundsIndexList.size() > 0 |
| 71 && (mCharacterBoundsIndexList.get(mCharacterBoundsIndexList.
size() - 1) |
| 72 >= index)) { |
| 73 throw new IllegalArgumentException("key must be greater than all
existing keys."); |
| 74 } |
| 75 mCharacterBoundsIndexList.add(index); |
| 76 mCharacterBoundsFlagsList.add(flags); |
| 77 mCharacterBoundsRectList.add(new RectF(left, top, right, bottom)); |
| 78 return this; |
| 79 } |
| 80 |
| 81 @Override |
| 82 public Builder setMatrix(Matrix matrix) { |
| 83 mMatrix.set(matrix != null ? matrix : new Matrix()); |
| 84 return this; |
| 85 } |
| 86 |
| 87 @Override |
| 88 public CursorAnchorInfoWrapper build() { |
| 89 return new TestCursorAnchorInfoWrapper(this); |
| 90 } |
| 91 |
| 92 @Override |
| 93 public void reset() { |
| 94 mSelectionStart = -1; |
| 95 mSelectionEnd = -1; |
| 96 mComposingTextStart = -1; |
| 97 mComposingText = null; |
| 98 mInsertionMarkerFlags = 0; |
| 99 mInsertionMarkerHorizontal = Float.NaN; |
| 100 mInsertionMarkerTop = Float.NaN; |
| 101 mInsertionMarkerBaseline = Float.NaN; |
| 102 mInsertionMarkerBottom = Float.NaN; |
| 103 mMatrix.set(new Matrix()); |
| 104 mCharacterBoundsIndexList.clear(); |
| 105 mCharacterBoundsFlagsList.clear(); |
| 106 mCharacterBoundsRectList.clear(); |
| 107 } |
| 108 } |
| 109 |
| 110 private final int mSelectionStart; |
| 111 private final int mSelectionEnd; |
| 112 private final int mComposingTextStart; |
| 113 private final CharSequence mComposingText; |
| 114 private final int mInsertionMarkerFlags; |
| 115 private final float mInsertionMarkerHorizontal; |
| 116 private final float mInsertionMarkerTop; |
| 117 private final float mInsertionMarkerBaseline; |
| 118 private final float mInsertionMarkerBottom; |
| 119 private final int[] mCharacterBoundsIndexArray; |
| 120 private final int[] mCharacterBoundsFlagsArray; |
| 121 private final RectF[] mCharacterBoundsRectArray; |
| 122 private final Matrix mMatrix; |
| 123 |
| 124 private TestCursorAnchorInfoWrapper(Builder builder) { |
| 125 mSelectionStart = builder.mSelectionStart; |
| 126 mSelectionEnd = builder.mSelectionEnd; |
| 127 mComposingTextStart = builder.mComposingTextStart; |
| 128 mComposingText = builder.mComposingText; |
| 129 mInsertionMarkerFlags = builder.mInsertionMarkerFlags; |
| 130 mInsertionMarkerHorizontal = builder.mInsertionMarkerHorizontal; |
| 131 mInsertionMarkerTop = builder.mInsertionMarkerTop; |
| 132 mInsertionMarkerBaseline = builder.mInsertionMarkerBaseline; |
| 133 mInsertionMarkerBottom = builder.mInsertionMarkerBottom; |
| 134 mCharacterBoundsIndexArray = new int[builder.mCharacterBoundsIndexList.s
ize()]; |
| 135 mCharacterBoundsFlagsArray = new int[mCharacterBoundsIndexArray.length]; |
| 136 mCharacterBoundsRectArray = new RectF[mCharacterBoundsIndexArray.length]
; |
| 137 for (int i = 0; i < mCharacterBoundsIndexArray.length; ++i) { |
| 138 mCharacterBoundsIndexArray[i] = builder.mCharacterBoundsIndexList.ge
t(i); |
| 139 mCharacterBoundsFlagsArray[i] = builder.mCharacterBoundsFlagsList.ge
t(i); |
| 140 mCharacterBoundsRectArray[i] = new RectF(builder.mCharacterBoundsRec
tList.get(i)); |
| 141 } |
| 142 mMatrix = new Matrix(builder.mMatrix); |
| 143 } |
| 144 |
| 145 @Override |
| 146 public int getSelectionStart() { |
| 147 return mSelectionStart; |
| 148 } |
| 149 |
| 150 @Override |
| 151 public int getSelectionEnd() { |
| 152 return mSelectionEnd; |
| 153 } |
| 154 |
| 155 @Override |
| 156 public int getComposingTextStart() { |
| 157 return mComposingTextStart; |
| 158 } |
| 159 |
| 160 @Override |
| 161 public CharSequence getComposingText() { |
| 162 return mComposingText; |
| 163 } |
| 164 |
| 165 @Override |
| 166 public int getInsertionMarkerFlags() { |
| 167 return mInsertionMarkerFlags; |
| 168 } |
| 169 |
| 170 @Override |
| 171 public float getInsertionMarkerHorizontal() { |
| 172 return mInsertionMarkerHorizontal; |
| 173 } |
| 174 |
| 175 @Override |
| 176 public float getInsertionMarkerTop() { |
| 177 return mInsertionMarkerTop; |
| 178 } |
| 179 |
| 180 @Override |
| 181 public float getInsertionMarkerBaseline() { |
| 182 return mInsertionMarkerBaseline; |
| 183 } |
| 184 |
| 185 @Override |
| 186 public float getInsertionMarkerBottom() { |
| 187 return mInsertionMarkerBottom; |
| 188 } |
| 189 |
| 190 @Override |
| 191 public RectF getCharacterBounds(int index) { |
| 192 int keyIndex = Arrays.binarySearch(mCharacterBoundsIndexArray, index); |
| 193 if (keyIndex < 0) { |
| 194 return null; |
| 195 } |
| 196 return new RectF(mCharacterBoundsRectArray[keyIndex]); |
| 197 } |
| 198 |
| 199 @Override |
| 200 public int getCharacterBoundsFlags(int index) { |
| 201 int keyIndex = Arrays.binarySearch(mCharacterBoundsIndexArray, index); |
| 202 if (keyIndex < 0) { |
| 203 return 0; |
| 204 } |
| 205 return mCharacterBoundsFlagsArray[keyIndex]; |
| 206 } |
| 207 |
| 208 @Override |
| 209 public Matrix getMatrix() { |
| 210 return new Matrix(mMatrix); |
| 211 } |
| 212 |
| 213 @Override |
| 214 public Object unwrap() { |
| 215 return null; |
| 216 } |
| 217 } |
| 218 |
OLD | NEW |