001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Michael Danilov
019: * @version $Revision$
020: */package java.awt.event;
021:
022: import java.awt.Button;
023: import java.text.AttributedCharacterIterator;
024: import java.util.Map;
025: import java.util.Set;
026: import junit.framework.TestCase;
027:
028: public class InputMethodEventTest extends TestCase {
029:
030: public final void testInputMethodEventComponentintTextHitInfoTextHitInfo() {
031: Button button = new Button();
032: InputMethodEvent event = new InputMethodEvent(button,
033: InputMethodEvent.CARET_POSITION_CHANGED, null, null);
034:
035: assertEquals(event.getSource(), button);
036: assertEquals(event.getID(),
037: InputMethodEvent.CARET_POSITION_CHANGED);
038: assertNull(event.getText());
039: assertEquals(event.getWhen(), 0);
040: assertNull(event.getCaret());
041: assertEquals(event.getCommittedCharacterCount(), 0);
042: assertNull(event.getVisiblePosition());
043: assertFalse(event.isConsumed());
044:
045: boolean wrongID = false;
046: try {
047: event = new InputMethodEvent(button,
048: InputMethodEvent.CARET_POSITION_CHANGED + 1024,
049: null, null);
050: } catch (IllegalArgumentException e) {
051: wrongID = true;
052: }
053: assertTrue(wrongID);
054: }
055:
056: public final void testInputMethodEventComponentintAttributedCharacterIteratorintTextHitInfoTextHitInfo() {
057: Button button = new Button();
058: InputMethodEvent event = new InputMethodEvent(button,
059: InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, text, 0,
060: null, null);
061:
062: assertEquals(event.getSource(), button);
063: assertEquals(event.getID(),
064: InputMethodEvent.INPUT_METHOD_TEXT_CHANGED);
065: assertEquals(event.getText(), text);
066: assertEquals(event.getWhen(), 0);
067: assertNull(event.getCaret());
068: assertEquals(event.getCommittedCharacterCount(), 0);
069: assertNull(event.getVisiblePosition());
070: assertFalse(event.isConsumed());
071:
072: boolean wrongID = false;
073: try {
074: event = new InputMethodEvent(button,
075: InputMethodEvent.INPUT_METHOD_TEXT_CHANGED + 1024,
076: text, 0, null, null);
077: } catch (IllegalArgumentException e) {
078: wrongID = true;
079: }
080: assertTrue(wrongID);
081:
082: boolean nullText = false;
083: try {
084: event = new InputMethodEvent(button,
085: InputMethodEvent.CARET_POSITION_CHANGED, text, 0,
086: null, null);
087: } catch (IllegalArgumentException e) {
088: nullText = true;
089: }
090: assertTrue(nullText);
091:
092: boolean wrongCount = false;
093: try {
094: event = new InputMethodEvent(button,
095: InputMethodEvent.CARET_POSITION_CHANGED, text, 10,
096: null, null);
097: } catch (IllegalArgumentException e) {
098: wrongCount = true;
099: }
100: assertTrue(wrongCount);
101: }
102:
103: public final void testInputMethodEventComponentintlongAttributedCharacterIteratorintTextHitInfoTextHitInfo() {
104: Button button = new Button();
105: InputMethodEvent event = new InputMethodEvent(button,
106: InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, 1000000000,
107: text, 0, null, null);
108:
109: assertEquals(event.getSource(), button);
110: assertEquals(event.getID(),
111: InputMethodEvent.INPUT_METHOD_TEXT_CHANGED);
112: assertEquals(event.getText(), text);
113: assertEquals(event.getWhen(), 1000000000);
114: assertNull(event.getCaret());
115: assertEquals(event.getCommittedCharacterCount(), 0);
116: assertNull(event.getVisiblePosition());
117: assertFalse(event.isConsumed());
118:
119: boolean wrongID = false;
120: try {
121: event = new InputMethodEvent(button,
122: InputMethodEvent.CARET_POSITION_CHANGED + 1024,
123: 1000000000, text, 0, null, null);
124: } catch (IllegalArgumentException e) {
125: wrongID = true;
126: }
127: assertTrue(wrongID);
128:
129: boolean nullText = false;
130: try {
131: event = new InputMethodEvent(button,
132: InputMethodEvent.CARET_POSITION_CHANGED,
133: 1000000000, text, 0, null, null);
134: } catch (IllegalArgumentException e) {
135: nullText = true;
136: }
137: assertTrue(nullText);
138:
139: boolean wrongCount = false;
140: try {
141: event = new InputMethodEvent(button,
142: InputMethodEvent.CARET_POSITION_CHANGED,
143: 1000000000, text, 10, null, null);
144: } catch (IllegalArgumentException e) {
145: wrongCount = true;
146: }
147: assertTrue(wrongCount);
148: }
149:
150: public final void testIsConsuming() {
151: Button button = new Button("Button");
152: InputMethodEvent event = new InputMethodEvent(button,
153: InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, 1000000000,
154: text, 0, null, null);
155:
156: assertFalse(event.isConsumed());
157: event.consume();
158: assertTrue(event.isConsumed());
159: }
160:
161: public final void testParamString() {
162: Button button = new Button("Button");
163: InputMethodEvent event = new InputMethodEvent(button,
164: InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, 1000000000,
165: text, 0, null, null);
166:
167: assertTrue(event
168: .paramString()
169: .indexOf(
170: "INPUT_METHOD_TEXT_CHANGED,text=java.awt.event.InputMethodEventTest") != -1);
171: assertTrue(event.paramString().indexOf(
172: ",commitedCharCount=0,caret=null,visiblePosition=null") != -1);
173: }
174:
175: static final AttributedCharacterIterator text = new AttributedCharacterIterator() {
176: public int getRunStart() {
177: return 0;
178: }
179:
180: public int getRunStart(Attribute arg0) {
181: return 0;
182: }
183:
184: public int getRunStart(Set<? extends Attribute> arg0) {
185: return 0;
186: }
187:
188: public int getRunLimit() {
189: return 0;
190: }
191:
192: public int getRunLimit(Attribute arg0) {
193: return 0;
194: }
195:
196: public int getRunLimit(Set<? extends Attribute> arg0) {
197: return 0;
198: }
199:
200: public Set<Attribute> getAllAttributeKeys() {
201: return null;
202: }
203:
204: public Object getAttribute(Attribute arg0) {
205: return null;
206: }
207:
208: public Map<Attribute, Object> getAttributes() {
209: return null;
210: }
211:
212: public char first() {
213: return 0;
214: }
215:
216: public char last() {
217: return 0;
218: }
219:
220: public char current() {
221: return 0;
222: }
223:
224: public char next() {
225: return 0;
226: }
227:
228: public char previous() {
229: return 0;
230: }
231:
232: public char setIndex(int arg0) {
233: return 0;
234: }
235:
236: public int getBeginIndex() {
237: return 0;
238: }
239:
240: public int getEndIndex() {
241: return 0;
242: }
243:
244: public int getIndex() {
245: return 0;
246: }
247:
248: @Override
249: public Object clone() {
250: return null;
251: }
252: };
253: }
|