OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser.test.util; | 5 package org.chromium.content.browser.test.util; |
6 | 6 |
7 import android.os.IBinder; | 7 import android.os.IBinder; |
8 import android.os.ResultReceiver; | 8 import android.os.ResultReceiver; |
| 9 import android.util.Pair; |
9 import android.view.View; | 10 import android.view.View; |
10 import android.view.inputmethod.EditorInfo; | 11 import android.view.inputmethod.EditorInfo; |
11 import android.view.inputmethod.InputConnection; | 12 import android.view.inputmethod.InputConnection; |
12 | 13 |
13 import org.chromium.base.Log; | 14 import org.chromium.base.Log; |
14 import org.chromium.content.browser.ContentViewCore; | 15 import org.chromium.content.browser.ContentViewCore; |
15 import org.chromium.content.browser.input.InputMethodManagerWrapper; | 16 import org.chromium.content.browser.input.InputMethodManagerWrapper; |
| 17 import org.chromium.content.browser.input.Range; |
| 18 |
| 19 import java.util.ArrayList; |
| 20 import java.util.List; |
16 | 21 |
17 /** | 22 /** |
18 * Overrides InputMethodManagerWrapper for testing purposes. | 23 * Overrides InputMethodManagerWrapper for testing purposes. |
19 */ | 24 */ |
20 public class TestInputMethodManagerWrapper extends InputMethodManagerWrapper { | 25 public class TestInputMethodManagerWrapper extends InputMethodManagerWrapper { |
21 private static final String TAG = "cr_Ime"; | 26 private static final String TAG = "cr_Ime"; |
22 | 27 |
23 /** | |
24 * A simple class to set start and end in int type. | |
25 */ | |
26 public static class Range { | |
27 private int mStart; | |
28 private int mEnd; | |
29 | |
30 public Range(int start, int end) { | |
31 mStart = start; | |
32 mEnd = end; | |
33 } | |
34 | |
35 public void set(int start, int end) { | |
36 mStart = start; | |
37 mEnd = end; | |
38 } | |
39 | |
40 public int start() { | |
41 return mStart; | |
42 } | |
43 | |
44 public int end() { | |
45 return mEnd; | |
46 } | |
47 | |
48 @Override | |
49 public boolean equals(Object o) { | |
50 if (!(o instanceof Range)) return false; | |
51 if (o == this) return true; | |
52 Range r = (Range) o; | |
53 return mStart == r.mStart && mEnd == r.mEnd; | |
54 } | |
55 | |
56 @Override | |
57 public int hashCode() { | |
58 final int prime = 31; | |
59 return prime * mStart + mEnd; | |
60 } | |
61 | |
62 @Override | |
63 public String toString() { | |
64 return "[ " + mStart + ", " + mEnd + " ]"; | |
65 } | |
66 } | |
67 | |
68 private final ContentViewCore mContentViewCore; | 28 private final ContentViewCore mContentViewCore; |
69 private InputConnection mInputConnection; | 29 private InputConnection mInputConnection; |
70 private int mRestartInputCounter; | 30 private int mRestartInputCounter; |
71 private int mShowSoftInputCounter; | 31 private int mShowSoftInputCounter; |
72 private int mHideSoftInputCounter; | 32 private int mHideSoftInputCounter; |
73 private int mUpdateSelectionCounter; | 33 private final Range mSelection = new Range(-1, -1); |
74 private EditorInfo mEditorInfo; | |
75 private final Range mSelection = new Range(0, 0); | |
76 private final Range mComposition = new Range(-1, -1); | 34 private final Range mComposition = new Range(-1, -1); |
77 private boolean mIsShowWithoutHideOutstanding; | 35 private boolean mIsShowWithoutHideOutstanding; |
| 36 private final List<Pair<Range, Range>> mUpdateSelectionList; |
78 | 37 |
79 public TestInputMethodManagerWrapper(ContentViewCore contentViewCore) { | 38 public TestInputMethodManagerWrapper(ContentViewCore contentViewCore) { |
80 super(null); | 39 super(null); |
81 Log.d(TAG, "TestInputMethodManagerWrapper constructor"); | 40 Log.d(TAG, "TestInputMethodManagerWrapper constructor"); |
82 mContentViewCore = contentViewCore; | 41 mContentViewCore = contentViewCore; |
| 42 mUpdateSelectionList = new ArrayList<>(); |
83 } | 43 } |
84 | 44 |
85 @Override | 45 @Override |
86 public void restartInput(View view) { | 46 public void restartInput(View view) { |
87 mRestartInputCounter++; | 47 mRestartInputCounter++; |
88 Log.d(TAG, "restartInput: count [%d]", mRestartInputCounter); | 48 Log.d(TAG, "restartInput: count [%d]", mRestartInputCounter); |
89 mEditorInfo = new EditorInfo(); | 49 EditorInfo editorInfo = new EditorInfo(); |
90 mInputConnection = mContentViewCore.onCreateInputConnection(mEditorInfo)
; | 50 mInputConnection = mContentViewCore.onCreateInputConnection(editorInfo); |
91 } | 51 } |
92 | 52 |
93 @Override | 53 @Override |
94 public void showSoftInput(View view, int flags, ResultReceiver resultReceive
r) { | 54 public void showSoftInput(View view, int flags, ResultReceiver resultReceive
r) { |
95 mIsShowWithoutHideOutstanding = true; | 55 mIsShowWithoutHideOutstanding = true; |
96 mShowSoftInputCounter++; | 56 mShowSoftInputCounter++; |
97 Log.d(TAG, "showSoftInput: count [%d]", mShowSoftInputCounter); | 57 Log.d(TAG, "showSoftInput: count [%d]", mShowSoftInputCounter); |
98 if (mInputConnection != null) return; | 58 if (mInputConnection != null) return; |
99 mEditorInfo = new EditorInfo(); | 59 EditorInfo editorInfo = new EditorInfo(); |
100 mInputConnection = mContentViewCore.onCreateInputConnection(mEditorInfo)
; | 60 mInputConnection = mContentViewCore.onCreateInputConnection(editorInfo); |
101 } | 61 } |
102 | 62 |
103 @Override | 63 @Override |
104 public boolean isActive(View view) { | 64 public boolean isActive(View view) { |
105 boolean result = mInputConnection != null; | 65 boolean result = mInputConnection != null; |
106 Log.d(TAG, "isActive: returns [%b]", result); | 66 Log.d(TAG, "isActive: returns [%b]", result); |
107 return result; | 67 return result; |
108 } | 68 } |
109 | 69 |
110 @Override | 70 @Override |
111 public boolean hideSoftInputFromWindow(IBinder windowToken, int flags, | 71 public boolean hideSoftInputFromWindow(IBinder windowToken, int flags, |
112 ResultReceiver resultReceiver) { | 72 ResultReceiver resultReceiver) { |
113 mIsShowWithoutHideOutstanding = false; | 73 mIsShowWithoutHideOutstanding = false; |
114 mHideSoftInputCounter++; | 74 mHideSoftInputCounter++; |
115 Log.d(TAG, "hideSoftInputFromWindow: count [%d]", mHideSoftInputCounter)
; | 75 Log.d(TAG, "hideSoftInputFromWindow: count [%d]", mHideSoftInputCounter)
; |
116 boolean retVal = mInputConnection == null; | 76 boolean retVal = mInputConnection == null; |
117 mInputConnection = null; | 77 mInputConnection = null; |
118 return retVal; | 78 return retVal; |
119 } | 79 } |
120 | 80 |
121 @Override | 81 @Override |
122 public void updateSelection(View view, int selStart, int selEnd, | 82 public void updateSelection(View view, int selStart, int selEnd, |
123 int candidatesStart, int candidatesEnd) { | 83 int candidatesStart, int candidatesEnd) { |
124 Log.d(TAG, "updateSelection"); | 84 Log.d(TAG, "updateSelection: [%d %d] [%d %d]", selStart, selEnd, candida
tesStart, |
125 mUpdateSelectionCounter++; | 85 candidatesEnd); |
| 86 Pair<Range, Range> newUpdateSelection = |
| 87 new Pair<>(new Range(selStart, selEnd), new Range(candidatesStar
t, candidatesEnd)); |
| 88 if (!mUpdateSelectionList.isEmpty()) { |
| 89 Pair<Range, Range> lastUpdateSelection = |
| 90 mUpdateSelectionList.get(mUpdateSelectionList.size() - 1); |
| 91 if (lastUpdateSelection.equals(newUpdateSelection)) return; |
| 92 } |
| 93 mUpdateSelectionList.add(new Pair<Range, Range>( |
| 94 new Range(selStart, selEnd), new Range(candidatesStart, candidat
esEnd))); |
126 mSelection.set(selStart, selEnd); | 95 mSelection.set(selStart, selEnd); |
127 mComposition.set(candidatesStart, candidatesEnd); | 96 mComposition.set(candidatesStart, candidatesEnd); |
128 } | 97 } |
129 | 98 |
| 99 @Override |
| 100 public void notifyUserAction() {} |
| 101 |
| 102 public final List<Pair<Range, Range>> getUpdateSelectionList() { |
| 103 return mUpdateSelectionList; |
| 104 } |
| 105 |
130 public int getRestartInputCounter() { | 106 public int getRestartInputCounter() { |
131 return mRestartInputCounter; | 107 return mRestartInputCounter; |
132 } | 108 } |
133 | 109 |
134 public int getShowSoftInputCounter() { | 110 public int getShowSoftInputCounter() { |
135 Log.d(TAG, "getShowSoftInputCounter: %d", mShowSoftInputCounter); | 111 Log.d(TAG, "getShowSoftInputCounter: %d", mShowSoftInputCounter); |
136 return mShowSoftInputCounter; | 112 return mShowSoftInputCounter; |
137 } | 113 } |
138 | 114 |
139 public int getHideSoftInputCounter() { | 115 public int getHideSoftInputCounter() { |
140 return mHideSoftInputCounter; | 116 return mHideSoftInputCounter; |
141 } | 117 } |
142 | 118 |
143 public int getUpdateSelectionCounter() { | 119 public void reset() { |
144 return mUpdateSelectionCounter; | 120 Log.d(TAG, "reset"); |
145 } | |
146 | |
147 public void resetCounters() { | |
148 Log.d(TAG, "resetCounters"); | |
149 mRestartInputCounter = 0; | 121 mRestartInputCounter = 0; |
150 mShowSoftInputCounter = 0; | 122 mShowSoftInputCounter = 0; |
151 mHideSoftInputCounter = 0; | 123 mHideSoftInputCounter = 0; |
152 mUpdateSelectionCounter = 0; | 124 mUpdateSelectionList.clear(); |
153 } | |
154 | |
155 public EditorInfo getEditorInfo() { | |
156 return mEditorInfo; | |
157 } | 125 } |
158 | 126 |
159 public InputConnection getInputConnection() { | 127 public InputConnection getInputConnection() { |
160 return mInputConnection; | 128 return mInputConnection; |
161 } | 129 } |
162 | 130 |
163 public Range getSelection() { | 131 public Range getSelection() { |
164 return mSelection; | 132 return mSelection; |
165 } | 133 } |
166 | 134 |
167 public Range getComposition() { | 135 public Range getComposition() { |
168 return mComposition; | 136 return mComposition; |
169 } | 137 } |
170 | 138 |
171 public boolean isShowWithoutHideOutstanding() { | 139 public boolean isShowWithoutHideOutstanding() { |
172 return mIsShowWithoutHideOutstanding; | 140 return mIsShowWithoutHideOutstanding; |
173 } | 141 } |
174 } | 142 } |
OLD | NEW |