|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
TestDragNDrop.java | - | 94.4% | 80% | 91.3% |
|
1 |
package dnd;
|
|
2 |
|
|
3 |
import javax.swing.JFrame;
|
|
4 |
import javax.swing.JLabel;
|
|
5 |
|
|
6 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
7 |
import junit.extensions.jfcunit.TestHelper;
|
|
8 |
import junit.extensions.jfcunit.RobotTestHelper;
|
|
9 |
import junit.extensions.jfcunit.eventdata.MouseEventData;
|
|
10 |
|
|
11 |
import junit.textui.TestRunner;
|
|
12 |
import junit.extensions.jfcunit.finder.FrameFinder;
|
|
13 |
import junit.extensions.jfcunit.finder.Finder;
|
|
14 |
import junit.extensions.jfcunit.finder.NamedComponentFinder;
|
|
15 |
|
|
16 |
/**
|
|
17 |
* A class written as an example of how to use the drag and drop
|
|
18 |
* support of the jfcUnit framework.
|
|
19 |
*
|
|
20 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
21 |
*/
|
|
22 |
public class TestDragNDrop |
|
23 |
extends JFCTestCase {
|
|
24 |
/**
|
|
25 |
* An instance of TestHelper to perform this example.
|
|
26 |
*/
|
|
27 |
private transient TestHelper m_helper; |
|
28 |
|
|
29 |
/**
|
|
30 |
* A common frame inside which the example's components are shown.
|
|
31 |
*/
|
|
32 |
private transient JFrame m_frame; |
|
33 |
|
|
34 |
/**
|
|
35 |
* Default JUnit constructor accepting a String.
|
|
36 |
*
|
|
37 |
* @param name Name of the test method to run
|
|
38 |
* (Called by the JUnit framework while building a suite of tests)
|
|
39 |
*/
|
|
40 | 1 |
public TestDragNDrop(final String name) {
|
41 | 1 |
super(name);
|
42 |
} |
|
43 |
|
|
44 |
/**
|
|
45 |
* Setup the test case.
|
|
46 |
*
|
|
47 |
* @exception Exception An instance of java.lang.Exception can be thrown
|
|
48 |
*/
|
|
49 | 1 |
public void setUp() throws Exception { |
50 |
|
|
51 | 1 |
m_helper = new RobotTestHelper();
|
52 | 1 |
DnDTest.main(new String[] {});
|
53 |
// Flush the AWT Event queue to insure that
|
|
54 |
// the app has fully opened.
|
|
55 | 1 |
flushAWT(); |
56 |
|
|
57 |
// Now lets get the Frame
|
|
58 | 1 |
Finder finder = new FrameFinder("Drag and Drop test"); |
59 | 1 |
m_frame = (JFrame) finder.find(); |
60 |
|
|
61 |
// In some applications, that use the defualt close operation,
|
|
62 |
// the app could be altered with the following to prevent the
|
|
63 |
// exit when the application window is closed.
|
|
64 |
// frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
65 |
} |
|
66 |
|
|
67 |
/**
|
|
68 |
* Teardown the test case and its resources.
|
|
69 |
*
|
|
70 |
* @exception Exception An instance of java.lang.Exception can be thrown
|
|
71 |
*/
|
|
72 | 1 |
public void tearDown() throws Exception { |
73 |
// Note: cleanUp may end up causing the VM to exit depending
|
|
74 |
// upon the implementation of the application. The DnD test
|
|
75 |
// application is left open since it is coded with its own
|
|
76 |
// window listener which calls Exit.
|
|
77 |
//
|
|
78 | 1 |
m_helper.cleanUp(this);
|
79 |
} |
|
80 |
|
|
81 |
/**
|
|
82 |
* This method will drag a label from one location and drop it on another inside the
|
|
83 |
* DnDTest class.
|
|
84 |
*/
|
|
85 | 1 |
public void testDragAndDrop() { |
86 |
|
|
87 | 1 |
setAssertExit(true);
|
88 |
//JFCEventManager.setDebug(true);
|
|
89 |
//JFCEventManager.setDebugType(JFCEventManager.DEBUG_INPUT);
|
|
90 |
//JFCEventManager.getEventManager().setRecording(true);
|
|
91 |
|
|
92 |
// Validate that the application exists.
|
|
93 | 1 |
assertNotNull("Could not find frame:", m_frame);
|
94 |
|
|
95 |
// now "find" the components by the names
|
|
96 | 1 |
JLabel label1 = (JLabel) new NamedComponentFinder(JLabel.class, |
97 |
"Drag Label 2").find(m_frame, 0);
|
|
98 | 1 |
assertNotNull("Could not find the JLabel: Drag Label 2", label1);
|
99 |
|
|
100 | 1 |
JLabel label2 = (JLabel) new NamedComponentFinder(JLabel.class, |
101 |
"Drop Label 2").find(m_frame, 0);
|
|
102 | 1 |
assertNotNull("Could not find the JLabel: Drop Label 2", label2);
|
103 |
|
|
104 |
// Save the text for later comparison.
|
|
105 | 1 |
String text = label1.getText(); |
106 |
|
|
107 |
// Enter the drag operation.
|
|
108 | 1 |
m_helper.enterDragAndLeave(new MouseEventData(this, label1), |
109 |
new MouseEventData(this, label2), 100); |
|
110 |
|
|
111 |
// Validate the results of the drag.
|
|
112 | 1 |
assertEquals("Label 1 text is incorrect", "", label1.getText()); |
113 | 1 |
assertEquals("Label 2 text is incorrect", text, label2.getText());
|
114 |
} |
|
115 |
|
|
116 |
/**
|
|
117 |
* Main method to run this class from the command line.
|
|
118 |
*
|
|
119 |
* @param args Command line arguments.
|
|
120 |
*/
|
|
121 | 0 |
public static void main(final String[] args) { |
122 |
/**
|
|
123 |
* @todo This should be moved into a setUp() method once we have jfcUnit support
|
|
124 |
* for TestSetup - since we need only one instance of SwingSet to run
|
|
125 |
*/
|
|
126 | 0 |
TestRunner.run(TestDragNDrop.class);
|
127 |
} |
|
128 |
} |
|
129 |
|
|