01: package abbot.editor;
02:
03: import javax.swing.*;
04: import java.awt.*;
05: import java.io.*;
06:
07: import junit.extensions.abbot.*;
08: import junit.extensions.abbot.Timer;
09:
10: public class HierarchyWriterTest extends ComponentTestFixture {
11:
12: public void xtestWriteHierarchy() throws IOException {
13: Panel panel = new Panel();
14: Panel p1 = new Panel();
15: Panel p2 = new Panel();
16: panel.add(p1);
17: panel.add(p2);
18: p1.add(new Button(getName()));
19: Frame f = new Frame(getName());
20: MenuBar mb = new MenuBar();
21: Menu menu = new Menu("File");
22: menu.add(new MenuItem("Open"));
23: mb.add(menu);
24: f.setMenuBar(mb);
25: f.add(panel);
26: // FIXME do regexp matches instead
27: String expected = "<awtHierarchy>\r\n"
28: + " <component class=\"java.awt.Frame\" >\r\n"
29: + " <component class=\"java.awt.Panel\" index=\"0\" >\r\n"
30: + " <component class=\"java.awt.Panel\" index=\"0\" >\r\n"
31: + " <component class=\"java.awt.Button\" index=\"0\" />\r\n"
32: + " </component>\r\n"
33: + " <component class=\"java.awt.Panel\" index=\"1\" />\r\n"
34: + " </component>\r\n" + " </component>\r\n"
35: + "</awtHierarchy>\r\n";
36: StringWriter writer = new StringWriter();
37: HierarchyWriter hw = new HierarchyWriter(getHierarchy());
38: hw.writeHierarchy(writer);
39: assertEquals("Wrong hierarchy output", expected, writer
40: .toString());
41: }
42:
43: /** Should be able to export the complete hierarchy in minimal time. */
44: public void testExportHierarchyPerformance() throws Exception {
45: JPanel pane = new JPanel();
46: int count = fill(pane, 8, 2) + 2;
47: showFrame(pane);
48: StringWriter writer = new StringWriter();
49: HierarchyWriter hw = new HierarchyWriter(getHierarchy());
50: Timer timer = new Timer();
51: hw.writeHierarchy(writer);
52: int MAX_TIME = 5000;
53: assertTrue("Too long to save hierarchy: " + timer.elapsed()
54: + "ms for " + count + " references (max total time "
55: + MAX_TIME + "ms)", timer.elapsed() < MAX_TIME);
56: }
57:
58: private int fill(JPanel pane, int level, int nsubs) {
59: int count = 0;
60: if (level > 0) {
61: for (int i = 0; i < nsubs; i++) {
62: JPanel sub = new JPanel();
63: count += fill(sub, level - 1, nsubs);
64: pane.add(sub);
65: }
66: } else {
67: pane.add(new JLabel(getName()));
68: ++count;
69: }
70: return count;
71: }
72:
73: public HierarchyWriterTest(String name) {
74: super (name);
75: }
76:
77: public static void main(String[] args) {
78: TestHelper.runTests(args, HierarchyWriterTest.class);
79: }
80: }
|