001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.explorer.view;
043:
044: import java.awt.datatransfer.DataFlavor;
045: import java.awt.datatransfer.StringSelection;
046: import java.awt.datatransfer.Transferable;
047: import java.io.IOException;
048: import org.netbeans.junit.MockServices;
049: import org.netbeans.junit.NbTestCase;
050: import org.openide.nodes.AbstractNode;
051: import org.openide.nodes.Children;
052: import org.openide.nodes.NodeTransfer;
053: import org.openide.util.datatransfer.ExClipboard;
054: import org.openide.util.datatransfer.ExTransferable;
055: import org.openide.util.datatransfer.MultiTransferObject;
056:
057: /**
058: *
059: * @author Jaroslav Tulach
060: */
061: public class DragDropUtilitiesTest extends NbTestCase {
062:
063: public DragDropUtilitiesTest(String testName) {
064: super (testName);
065: }
066:
067: protected void setUp() throws Exception {
068: MockServices.setServices(new Class[] { MyClipboard.class });
069: last = null;
070: }
071:
072: public void testGetNodeTransferableForSingleNodeCopy()
073: throws Exception {
074: N node = new N();
075:
076: Transferable t = DragDropUtilities.getNodeTransferable(node,
077: NodeTransfer.DND_COPY);
078:
079: assertEquals("One call to copy", 1, node.copy);
080: assertEquals("Also one call to drag which delegates to copy",
081: 1, node.drag);
082: assertEquals("No call to cut", 0, node.cut);
083: assertNotNull("Call to convertor", last);
084: assertTrue("StringSelection got to ExClipboard convertor",
085: last instanceof StringSelection);
086: }
087:
088: public void testGetNodeTransferableForSingleNodeCut()
089: throws Exception {
090: N node = new N();
091:
092: Transferable t = DragDropUtilities.getNodeTransferable(node,
093: NodeTransfer.DND_MOVE);
094:
095: assertEquals("One call to cut", 1, node.cut);
096: assertEquals("No call to drag", 0, node.drag);
097: assertEquals("No call to copy", 0, node.copy);
098: assertNotNull("Call to convertor", last);
099: assertTrue("StringSelection got to ExClipboard convertor",
100: last instanceof StringSelection);
101: }
102:
103: public void testMultiTransferableForCopy() throws Exception {
104: N node = new N();
105: N n2 = new N();
106: N[] arr = { node, n2 };
107:
108: Transferable t = DragDropUtilities.getNodeTransferable(arr,
109: NodeTransfer.DND_COPY);
110:
111: assertEquals("One call to copy", 1, node.copy);
112: assertEquals("One call to copy on n2", 1, n2.copy);
113: assertEquals("Also one call to drag which delegates to copy",
114: 1, node.drag);
115: assertEquals(
116: "Also one call to drag which delegates to copy on n2",
117: 1, n2.drag);
118: assertEquals("No call to cut", 0, node.cut);
119: assertEquals("No call to cut", 0, n2.cut);
120:
121: assertNotNull("Call to convertor", last);
122: assertTrue("multi flavor supported", last
123: .isDataFlavorSupported(ExTransferable.multiFlavor));
124: Object obj = last.getTransferData(ExTransferable.multiFlavor);
125: if (!(obj instanceof MultiTransferObject)) {
126: fail("It should be MultiTransferObject: " + obj);
127: }
128: MultiTransferObject m = (MultiTransferObject) obj;
129:
130: assertEquals("Two in multi", 2, m.getCount());
131: assertTrue("Is string", m.getTransferData(0,
132: DataFlavor.stringFlavor) instanceof String);
133: assertTrue("Is string2", m.getTransferData(1,
134: DataFlavor.stringFlavor) instanceof String);
135: }
136:
137: public void testMultiTransferableForCut() throws Exception {
138: N node = new N();
139: N n2 = new N();
140: N[] arr = { node, n2 };
141:
142: Transferable t = DragDropUtilities.getNodeTransferable(arr,
143: NodeTransfer.DND_MOVE);
144:
145: assertEquals("One call to cut ", 1, node.cut);
146: assertEquals("One call to cut on n2", 1, n2.cut);
147: assertEquals("No to drag", 0, node.drag);
148: assertEquals("No to drag on n2", 0, n2.drag);
149: assertEquals("No call to copy", 0, node.copy);
150: assertEquals("No call to copy on n2", 0, n2.copy);
151:
152: assertNotNull("Call to convertor", last);
153: assertTrue("multi flavor supported", last
154: .isDataFlavorSupported(ExTransferable.multiFlavor));
155: Object obj = last.getTransferData(ExTransferable.multiFlavor);
156: if (!(obj instanceof MultiTransferObject)) {
157: fail("It should be MultiTransferObject: " + obj);
158: }
159: MultiTransferObject m = (MultiTransferObject) obj;
160:
161: assertEquals("Two in multi", 2, m.getCount());
162: assertTrue("Is string", m.getTransferData(0,
163: DataFlavor.stringFlavor) instanceof String);
164: assertTrue("Is string2", m.getTransferData(1,
165: DataFlavor.stringFlavor) instanceof String);
166: }
167:
168: private static class N extends AbstractNode {
169: public int copy;
170: public int cut;
171: public int drag;
172: public Transferable ret = new StringSelection("A text");
173:
174: public N() {
175: super (Children.LEAF);
176: }
177:
178: public Transferable clipboardCut() throws IOException {
179: cut++;
180: return ret;
181: }
182:
183: public Transferable clipboardCopy() throws IOException {
184: copy++;
185: return ret;
186: }
187:
188: public Transferable drag() throws IOException {
189: drag++;
190: return super .drag();
191: }
192: }
193:
194: public static Transferable last;
195:
196: public static final class MyClipboard extends ExClipboard {
197:
198: public MyClipboard() {
199: super ("Empty");
200: }
201:
202: public ExClipboard.Convertor[] getConvertors() {
203: return new ExClipboard.Convertor[] { new ExClipboard.Convertor() {
204: public Transferable convert(Transferable t) {
205: last = t;
206: return t;
207: }
208: } };
209: }
210: }
211: }
|