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 Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Rectangle;
023: import java.awt.Toolkit;
024: import java.awt.datatransfer.Clipboard;
025: import java.awt.datatransfer.DataFlavor;
026: import java.awt.datatransfer.StringSelection;
027: import java.awt.datatransfer.UnsupportedFlavorException;
028: import java.beans.PropertyChangeEvent;
029: import java.beans.PropertyChangeListener;
030: import java.io.IOException;
031: import java.util.Vector;
032: import javax.accessibility.AccessibleContext;
033: import javax.accessibility.AccessibleRole;
034: import javax.accessibility.AccessibleState;
035: import javax.accessibility.AccessibleStateSet;
036: import javax.swing.Action;
037: import javax.swing.JFrame;
038: import javax.swing.JTextArea;
039: import javax.swing.JTextField;
040: import javax.swing.SwingTestCase;
041: import javax.swing.text.JTextComponentTest.JTextComp;
042: import javax.swing.text.JTextComponentTest.SimplePropertyChangeListener;
043:
044: public class JTextComponent_AccessibleJTextComponentTest extends
045: SwingTestCase {
046: JFrame jf;
047:
048: JTextComponent jtc;
049:
050: SimplePropertyChangeListener pChListener;
051:
052: JTextComponent.AccessibleJTextComponent accessible;
053:
054: boolean bWasException;
055:
056: String str;
057:
058: AccessiblePropertyChangeListener listener;
059:
060: AbstractDocument doc;
061:
062: JTextComp jtComp;
063:
064: AbstractDocument docXXX;
065:
066: JTextField jep;
067:
068: Rectangle rect;
069:
070: Object obj_old[] = new Object[] { null };
071:
072: Object obj_new[] = new Object[1];
073:
074: String s[] = new String[] { AccessibleContext.ACCESSIBLE_TEXT_PROPERTY };
075:
076: boolean was[] = new boolean[] { true };
077:
078: String text;
079:
080: class AccessiblePropertyChangeListener implements
081: PropertyChangeListener {
082: Vector<String> PropertyNames = new Vector<String>();
083:
084: Object oldValues[] = new Object[5];
085:
086: Object newValues[] = new Object[5];
087:
088: boolean wasPropertyChange[] = new boolean[5];
089:
090: int index = 0;
091:
092: public void InterestProperty(final String s) {
093: PropertyNames.add(s);
094: wasPropertyChange[index++] = false;
095: }
096:
097: public void propertyChange(final PropertyChangeEvent e) {
098: for (int i = 0; i < PropertyNames.size(); i++) {
099: if (e.getPropertyName() == PropertyNames.get(i)) {
100: oldValues[i] = e.getOldValue();
101: newValues[i] = e.getNewValue();
102: wasPropertyChange[i] = true;
103: }
104: }
105: }
106:
107: public void reset() {
108: for (int i = 0; i < index; i++) {
109: oldValues[i] = null;
110: newValues[i] = null;
111: wasPropertyChange[i] = false;
112: }
113: index = 0;
114: PropertyNames.clear();
115: }
116: }
117:
118: void isElement(final Object[] a, final Object b, final int count) {
119: assertNotNull(a);
120: boolean cond = false;
121: int k = 0;
122: for (int i = 0; i < a.length; i++) {
123: if (a[i] == b) {
124: cond = true;
125: k += 1;
126: }
127: }
128: assertTrue(cond);
129: assertEquals(count, k);
130: }
131:
132: void assertEqualsPropertyChange(
133: final AccessiblePropertyChangeListener listener,
134: final String s[], final Object oldValues[],
135: final Object newValues[], final boolean b[]) {
136: for (int i = 0; i < listener.PropertyNames.size(); i++) {
137: for (int j = 0; j < s.length; j++) {
138: if (s[j] == listener.PropertyNames.get(i)) {
139: assertEquals(b[j], listener.wasPropertyChange[i]);
140: if (b[j]) {
141: assertEquals(oldValues[j],
142: listener.oldValues[i]);
143: assertEquals(newValues[j],
144: listener.newValues[i]);
145: }
146: }
147: }
148: }
149: }
150:
151: void assertEqualsPropertyChangeEvent(final String name,
152: final Object oldValue, final Object newValue,
153: final PropertyChangeEvent e) {
154: assertEquals(name, e.getPropertyName());
155: assertEquals(oldValue, e.getOldValue());
156: assertEquals(newValue, e.getNewValue());
157: }
158:
159: public JTextComponent_AccessibleJTextComponentTest() {
160: setIgnoreNotImplemented(true);
161: }
162:
163: @Override
164: protected void setUp() throws Exception {
165: super .setUp();
166: jf = new JFrame();
167: bWasException = false;
168: str = null;
169: listener = new AccessiblePropertyChangeListener();
170: text = ("JTextComponent\n" + "\u05dc"
171: + "JTextComponent.AccessibleJTextComponent");
172: jtc = new JTextArea(text);
173: accessible = (JTextComponent.AccessibleJTextComponent) jtc
174: .getAccessibleContext();
175: accessible.addPropertyChangeListener(listener);
176: jf.getContentPane().add(jtc);
177: jf.setLocation(200, 300);
178: jf.setSize(200, 300);
179: jf.pack();
180: }
181:
182: @Override
183: protected void tearDown() throws Exception {
184: jf.dispose();
185: super .tearDown();
186: }
187:
188: public void testAccessibleJTextComponent() {
189: assertNotNull(accessible);
190: }
191:
192: private AttributeSet getAttributeSetByIndex(
193: final AbstractDocument d, final int offset) {
194: AttributeSet as = null;
195: Element elem = d.getDefaultRootElement();
196: while (elem.getElementCount() > 0) {
197: elem = elem.getElement(elem.getElementIndex(offset));
198: as = elem.getAttributes();
199: }
200: return as;
201: }
202:
203: public void testSetAttributesGetCharacterAttributes()
204: throws Exception {
205: AttributeSet samples[] = new AttributeSet[20];
206: AttributeSet as;
207: String name1 = "ATTRIBUTE_NAME1";
208: String value1 = "ATTRIBUTE_VALUE1";
209: String name2 = "ATTRIBUTE_NAME2";
210: String value2 = "ATTRIBUTE_VALUE2";
211: SimpleAttributeSet sas = new SimpleAttributeSet();
212: sas.addAttribute(name1, value1);
213: sas.addAttribute(name2, value2);
214: doc = (AbstractDocument) jtc.getDocument();
215: for (int i = 0; i < 20; i++) {
216: samples[i] = getAttributeSetByIndex(doc, i);
217: }
218: accessible.setAttributes(4, 6, sas);
219: for (int i = 4; i < 7; i++) {
220: as = getAttributeSetByIndex(doc, i);
221: assertFalse(as.containsAttribute(name1, value1));
222: assertFalse(as.containsAttribute(name2, value2));
223: }
224: for (int i = 0; i < 20; i++) {
225: as = samples[i];
226: assertEquals(as, accessible.getCharacterAttribute(i));
227: assertEquals(as, getAttributeSetByIndex(doc, i));
228: }
229: jtc.setDocument(new DefaultStyledDocument());
230: listener.reset();
231: listener
232: .InterestProperty(AccessibleContext.ACCESSIBLE_CARET_PROPERTY);
233: jtc.setText("JTEXT_COMPONENT\n JTEXTCOMPONENT");
234: obj_old[0] = new Integer(0);
235: obj_new[0] = new Integer(31);
236: assertEqualsPropertyChange(
237: listener,
238: new String[] { AccessibleContext.ACCESSIBLE_CARET_PROPERTY },
239: obj_old, obj_new, was);
240: accessible.setAttributes(5, 8, sas);
241: doc = (AbstractDocument) jtc.getDocument();
242: for (int i = 0; i < 20; i++) {
243: samples[i] = getAttributeSetByIndex(doc, i);
244: }
245: for (int i = 5; i < 8; i++) {
246: as = getAttributeSetByIndex(doc, i);
247: assertEquals(sas, as);
248: assertEquals(as, accessible.getCharacterAttribute(i));
249: }
250: for (int i = 0; i < 20; i++) {
251: if (i >= 5 && i < 7) {
252: continue;
253: }
254: as = samples[i];
255: assertEquals(as, accessible.getCharacterAttribute(i));
256: assertEquals(as, getAttributeSetByIndex(doc, i));
257: }
258: }
259:
260: public void testRemoveUpdate() throws Exception {
261: listener
262: .InterestProperty(AccessibleContext.ACCESSIBLE_TEXT_PROPERTY);
263: try {
264: jtc.getDocument().remove(0, 4);
265: } catch (BadLocationException e) {
266: }
267: obj_new[0] = new Integer(0);
268: assertEqualsPropertyChange(listener, s, obj_old, obj_new, was);
269: listener.reset();
270: try {
271: jtc.getDocument().remove(5, 3);
272: } catch (BadLocationException e) {
273: }
274: obj_new[0] = new Integer(0);
275: assertEqualsPropertyChange(listener, s, obj_old, obj_new, was);
276: }
277:
278: public void testInsertUpdate() throws Exception {
279: listener
280: .InterestProperty(AccessibleContext.ACCESSIBLE_TEXT_PROPERTY);
281: try {
282: jtc.getDocument().insertString(0, "TEST", null);
283: } catch (BadLocationException e) {
284: }
285: obj_new[0] = new Integer(0);
286: assertEqualsPropertyChange(
287: listener,
288: new String[] { AccessibleContext.ACCESSIBLE_TEXT_PROPERTY },
289: obj_old, obj_new, was);
290: listener.reset();
291: try {
292: jtc.getDocument().insertString(3, "TEST", null);
293: } catch (BadLocationException e) {
294: }
295: obj_new[0] = new Integer(3);
296: assertEqualsPropertyChange(
297: listener,
298: new String[] { AccessibleContext.ACCESSIBLE_TEXT_PROPERTY },
299: obj_old, obj_new, was);
300: }
301:
302: public void testChangedUpdate() throws Exception {
303: listener
304: .InterestProperty(AccessibleContext.ACCESSIBLE_TEXT_PROPERTY);
305: try {
306: ((AbstractDocument) jtc.getDocument()).replace(0, 3,
307: "TEST", null);
308: } catch (BadLocationException e) {
309: }
310: obj_new[0] = new Integer(0);
311: assertEqualsPropertyChange(listener, s, obj_old, obj_new, was);
312: listener.reset();
313: try {
314: ((AbstractDocument) jtc.getDocument()).replace(4, 5,
315: "TEST", null);
316: } catch (BadLocationException e) {
317: }
318: obj_new[0] = new Integer(4);
319: assertEqualsPropertyChange(listener, s, obj_old, obj_new, was);
320: }
321:
322: public void testCaretUpdate() throws Exception {
323: listener
324: .InterestProperty(AccessibleContext.ACCESSIBLE_CARET_PROPERTY);
325: listener
326: .InterestProperty(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY);
327: String s[] = new String[] {
328: AccessibleContext.ACCESSIBLE_CARET_PROPERTY,
329: AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY };
330: obj_old = new Object[] { null, null };
331: obj_new = new Object[2];
332: boolean was[] = new boolean[] { true, false };
333: jtc.setCaretPosition(4);
334: obj_new[0] = new Integer(4);
335: obj_old[0] = new Integer(0);
336: assertEqualsPropertyChange(listener, s, obj_old, obj_new, was);
337: listener.reset();
338: jtc.moveCaretPosition(7);
339: obj_old[0] = new Integer(4);
340: obj_new[0] = new Integer(7);
341: was[1] = true;
342: obj_new[1] = "tCo";
343: assertEqualsPropertyChange(listener, s, obj_old, obj_new, was);
344: listener.reset();
345: jtc.moveCaretPosition(6);
346: obj_old[0] = new Integer(7);
347: obj_new[0] = new Integer(6);
348: obj_new[1] = "tC";
349: assertEqualsPropertyChange(listener, s, obj_old, obj_new, was);
350: listener.reset();
351: jtc.moveCaretPosition(4);
352: obj_old[0] = new Integer(6);
353: obj_new[0] = new Integer(4);
354: was[1] = false;
355: assertEqualsPropertyChange(listener, s, obj_old, obj_new, was);
356: }
357:
358: public void testGetAccessibleText() {
359: assertEquals(accessible, accessible.getAccessibleText());
360: }
361:
362: private void checkState(final AccessibleStateSet set,
363: final AccessibleState s) {
364: assertTrue(set.contains(s));
365: set.remove(s);
366: }
367:
368: private void checkStateSet(final AccessibleStateSet ass) {
369: if (jtc.isEnabled()) {
370: checkState(ass, AccessibleState.ENABLED);
371: }
372: if (jtc.isFocusable()) {
373: checkState(ass, AccessibleState.FOCUSABLE);
374: }
375: if (jtc.isVisible()) {
376: checkState(ass, AccessibleState.VISIBLE);
377: }
378: if (jtc.isShowing()) {
379: checkState(ass, AccessibleState.SHOWING);
380: }
381: if (jtc.isFocusOwner()) {
382: checkState(ass, AccessibleState.FOCUSED);
383: }
384: if (jtc.isOpaque()) {
385: checkState(ass, AccessibleState.OPAQUE);
386: }
387: assertEquals(0, ass.toArray().length);
388: }
389:
390: public void testGetAccessibleStateSet() throws Exception {
391: AccessibleStateSet ass = accessible.getAccessibleStateSet();
392: assertTrue(ass.contains(AccessibleState.MULTI_LINE));
393: assertTrue(ass.contains(AccessibleState.EDITABLE));
394: ass.remove(AccessibleState.MULTI_LINE);
395: ass.remove(AccessibleState.EDITABLE);
396: checkStateSet(ass);
397: jtc.setEditable(false);
398: ass = accessible.getAccessibleStateSet();
399: assertTrue(ass.contains(AccessibleState.MULTI_LINE));
400: ass.remove(AccessibleState.MULTI_LINE);
401: checkStateSet(ass);
402: }
403:
404: public void testGetAccessibleRole() {
405: assertEquals(AccessibleRole.TEXT, accessible
406: .getAccessibleRole());
407: }
408:
409: public void testGetAccessibleEditableText() {
410: assertEquals(accessible, accessible.getAccessibleEditableText());
411: }
412:
413: public void testGetAccessibleAction() {
414: assertEquals(accessible, accessible.getAccessibleAction());
415: }
416:
417: public void testSetTextContents() throws Exception {
418: accessible.setTextContents("NEW Text");
419: assertEquals("NEW Text", jtc.getText());
420: }
421:
422: public void testInsertTextAtIndex() throws Exception {
423: jtc.setCaretPosition(5);
424: jtc.setDocument(new DefaultStyledDocument());
425: jtc.setText(text);
426: accessible.insertTextAtIndex(2, "SSS");
427: assertEquals(text.substring(0, 2) + "SSS"
428: + text.substring(2, text.length()), jtc.getText());
429: }
430:
431: public void testReplaceText() throws Exception {
432: accessible.replaceText(5, 8, "XXX");
433: assertEquals(text.replaceFirst("Com", "XXX"), jtc.getText());
434: try {
435: accessible.replaceText(8, 5, "ZZZ");
436: assertEquals(text.replaceFirst("Com", "XXXZZZ"), jtc
437: .getText());
438: } catch (IllegalArgumentException e) {
439: bWasException = true;
440: }
441: if (isHarmony()) {
442: assertTrue(bWasException);
443: }
444: }
445:
446: public void testGetTextRange() {
447: assertEquals("JTextCo", accessible.getTextRange(0, 7));
448: assertEquals("omponent\n" + "\u05dc" + "JTe", accessible
449: .getTextRange(6, 19));
450: }
451:
452: public void testGetAccessibleActionDescription() {
453: for (int i = 0; i < jtc.getActions().length; i++) {
454: assertEquals(jtc.getActions()[i].getValue(Action.NAME),
455: accessible.getAccessibleActionDescription(i));
456: }
457: }
458:
459: public void testGetSelectedText() throws Exception {
460: jtc.setCaretPosition(5);
461: assertNull(accessible.getSelectedText());
462: jtc.moveCaretPosition(8);
463: assertEquals("Com", accessible.getSelectedText());
464: }
465:
466: public void testSelectText() throws Exception {
467: accessible.selectText(5, 8);
468: assertEquals("Com", accessible.getSelectedText());
469: }
470:
471: public void testDelete() throws Exception {
472: accessible.delete(4, 6);
473: text = text.replaceFirst("tC", "");
474: assertEquals(text, jtc.getText());
475: try {
476: accessible.delete(6, 4);
477: text = text.replaceFirst("om", "");
478: assertEquals(text, jtc.getText());
479: } catch (IllegalArgumentException e) {
480: bWasException = true;
481: }
482: if (isHarmony()) {
483: assertTrue(bWasException);
484: }
485: bWasException = false;
486: try {
487: accessible.delete(-6, -4);
488: assertEquals(text, jtc.getText());
489: } catch (IllegalArgumentException e) {
490: bWasException = true;
491: }
492: if (isHarmony()) {
493: assertTrue(bWasException);
494: }
495: }
496:
497: String getClipboardString(final JTextComponent jtc) {
498: String content = null;
499: Toolkit toolkit = jtc.getToolkit();
500: Clipboard clipboard = toolkit.getSystemClipboard();
501: DataFlavor dataFlavor = DataFlavor.stringFlavor;
502: try {
503: content = (String) clipboard.getContents(null)
504: .getTransferData(dataFlavor);
505: } catch (UnsupportedFlavorException e) {
506: } catch (IOException e) {
507: }
508: return content;
509: }
510:
511: void setClipboardString(final JTextComponent jtc,
512: final String content) {
513: Toolkit toolkit = jtc.getToolkit();
514: Clipboard clipboard = toolkit.getSystemClipboard();
515: StringSelection dataFlavor = new StringSelection(content);
516: clipboard.setContents(dataFlavor, dataFlavor);
517: }
518:
519: public void testCut() throws Exception {
520: // TODO: uncomment when System clipboard is properly supported
521: // if (jtc.getToolkit().getSystemClipboard() == null)
522: // return;
523: // setClipboardString(jtc, "XXX");
524: // accessible.cut(2, 2);
525: // assertEquals("XXX", getClipboardString(jtc));
526: // assertEquals(2, jtc.getCaretPosition());
527: // assertEquals(text, jtc.getText());
528: //
529: // accessible.cut(5, 8);
530: // assertEquals(5, jtc.getCaretPosition());
531: // assertEquals("Com", getClipboardString(jtc));
532: // assertEquals(text.replaceFirst("Com", ""), jtc.getText());
533: //
534: // accessible.cut(9, 4);
535: // assertEquals(9, jtc.getCaretPosition());
536: // assertEquals(text.replaceFirst("Com", ""), jtc.getText());
537: }
538:
539: public void testDoAccessibleAction() {
540: assertTrue(accessible.doAccessibleAction(0));
541: assertTrue(accessible.doAccessibleAction(1));
542: assertTrue(accessible.doAccessibleAction(2));
543: }
544:
545: public void testPaste() throws Exception {
546: // TODO: uncomment when System clipboard is properly supported
547: // if (jtc.getToolkit().getSystemClipboard() == null)
548: // return;
549: // setClipboardString(jtc, "XXX");
550: // accessible.paste(5);
551: // assertEquals(text.substring(0, 5) + "XXX"
552: // + text.substring(5, text.length()), jtc.getText());
553: // assertEquals(8, jtc.getCaretPosition());
554: // setClipboardString(jtc, "");
555: // accessible.paste(10);
556: // assertEquals(10, jtc.getCaretPosition());
557: }
558:
559: public void testGetSelectionStartEnd() throws Exception {
560: jtc.setSelectionStart(5);
561: jtc.setSelectionEnd(8);
562: assertEquals(5, accessible.getSelectionStart());
563: assertEquals(8, jtc.getSelectionEnd());
564: }
565:
566: public void testGetCharCount() {
567: assertEquals(jtc.getDocument().getLength(), accessible
568: .getCharCount());
569: }
570:
571: public void testGetCaretPosition() throws Exception {
572: jtc.setCaretPosition(5);
573: assertEquals(5, accessible.getCaretPosition());
574: jtc.setCaretPosition(7);
575: assertEquals(7, accessible.getCaretPosition());
576: }
577:
578: public void testGetAccessibleActionCount() {
579: assertEquals(53, accessible.getAccessibleActionCount());
580: }
581: }
|