KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > services > NbPresenterTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.services;
21
22 import java.util.Arrays JavaDoc;
23 import javax.swing.JButton JavaDoc;
24 import javax.swing.JFrame JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import org.openide.DialogDescriptor;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.util.HelpCtx;
29
30 /** Tests issue 56534.
31  *
32  * @author Jiri Rechtacek
33  */

34 public class NbPresenterTest extends NbTestCase {
35
36     public NbPresenterTest (String JavaDoc testName) {
37         super (testName);
38     }
39
40     protected boolean runInEQ () {
41         return true;
42     }
43
44     public void testDialogsOptionsOnDefaultSystem () {
45         System.setProperty ("xtest.looks_as_mac", "false");
46         doTestDialogsOptions ();
47     }
48     
49     public void testDialogsOptionsOnMac () {
50         System.setProperty ("xtest.looks_as_mac", "true");
51         doTestDialogsOptions ();
52     }
53     
54     private void doTestDialogsOptions () {
55         boolean modal = false;
56         //boolean modal = true;
57
JButton JavaDoc erase = new JButton JavaDoc ("Erase all my data");
58         JButton JavaDoc rescue = new JButton JavaDoc ("Rescue");
59         JButton JavaDoc cancel = new JButton JavaDoc ("Cancel");
60         JButton JavaDoc [] options = new JButton JavaDoc [] {erase, rescue, cancel};
61         DialogDescriptor dd = new DialogDescriptor (new JLabel JavaDoc ("Something interesting"), "My dialog", modal,
62                 // options
63
options,
64                 rescue,
65                 // align
66
DialogDescriptor.RIGHT_ALIGN,
67                 new HelpCtx (NbPresenterTest.class), null);
68         
69
70         dd.setClosingOptions (new Object JavaDoc[0]);
71                 
72         NbPresenter presenter = new NbDialog (dd, (JFrame JavaDoc)null);
73         presenter.setVisible (true);
74         
75         erase.doClick ();
76         assertEquals ("Erase was invoked.", erase.getText (), ((JButton JavaDoc)dd.getValue ()).getText ());
77         erase.doClick ();
78         assertEquals ("Erase was invoked again on same dialog.", erase.getText (), ((JButton JavaDoc)dd.getValue ()).getText ());
79         presenter.dispose ();
80
81         presenter = new NbDialog (dd, (JFrame JavaDoc)null);
82         presenter.setVisible (true);
83
84         erase.doClick ();
85         assertEquals ("Erase was invoked of reused dialog.", erase.getText (), ((JButton JavaDoc)dd.getValue ()).getText ());
86         erase.doClick ();
87         assertEquals ("Erase was invoked again on reused dialog.", erase.getText (), ((JButton JavaDoc)dd.getValue ()).getText ());
88         presenter.dispose ();
89
90         presenter = new NbDialog (dd, (JFrame JavaDoc)null);
91         presenter.setVisible (true);
92
93         rescue.doClick ();
94         assertEquals ("Rescue was invoked of reused dialog.", rescue.getText (), ((JButton JavaDoc)dd.getValue ()).getText ());
95         rescue.doClick ();
96         assertEquals ("Rescue was invoked again on reused dialog.", rescue.getText (), ((JButton JavaDoc)dd.getValue ()).getText ());
97         presenter.dispose ();
98         
99         presenter = new NbDialog (dd, (JFrame JavaDoc)null);
100         presenter.setVisible (true);
101
102         cancel.doClick ();
103         assertEquals ("Cancel was invoked of reused dialog.", cancel.getText (), ((JButton JavaDoc)dd.getValue ()).getText ());
104         cancel.doClick ();
105         assertEquals ("Cancel was invoked again on reused dialog.", cancel.getText (), ((JButton JavaDoc)dd.getValue ()).getText ());
106         presenter.dispose ();
107     }
108     
109     public void testNbPresenterComparator () {
110         JButton JavaDoc erase = new JButton JavaDoc ("Erase all my data");
111         JButton JavaDoc rescue = new JButton JavaDoc ("Rescue");
112         JButton JavaDoc cancel = new JButton JavaDoc ("Cancel");
113         JButton JavaDoc [] options = new JButton JavaDoc [] {erase, rescue, cancel};
114         DialogDescriptor dd = new DialogDescriptor (new JLabel JavaDoc ("Something interesting"), "My dialog", false,
115                 // options
116
options,
117                 rescue,
118                 // align
119
DialogDescriptor.RIGHT_ALIGN,
120                 null, null);
121                 
122         dd.setClosingOptions (null);
123                 
124         NbPresenter presenter = new NbDialog (dd, (JFrame JavaDoc)null);
125         assertEquals ("Dialog has Rescue option as default value.", rescue, dd.getDefaultValue ());
126         JButton JavaDoc [] backup = (JButton JavaDoc [])options.clone ();
127         //showButtonArray (backup);
128
Arrays.sort (options, presenter);
129         //showButtonArray (options);
130
JButton JavaDoc [] onceSorted = (JButton JavaDoc [])options.clone ();
131         Arrays.sort (options, presenter);
132         //showButtonArray (options);
133
JButton JavaDoc [] twiceSorted = (JButton JavaDoc [])options.clone ();
134         assertFalse ("Original options not same as sorted option.", Arrays.asList (backup).equals (Arrays.asList (onceSorted)));
135         assertEquals ("Sorting of options is invariable.", Arrays.asList (onceSorted), Arrays.asList (twiceSorted));
136         presenter.setVisible (true);
137         erase.doClick ();
138         assertEquals ("Dialog has been close by Erase option", erase, dd.getValue ());
139
140         presenter = new NbDialog (dd, (JFrame JavaDoc)null);
141         presenter.setVisible (true);
142
143         options = (JButton JavaDoc [])backup.clone ();
144         //showButtonArray (backup);
145
Arrays.sort (options, presenter);
146         JButton JavaDoc [] onceSorted2 = (JButton JavaDoc [])options.clone ();
147         //showButtonArray (onceSorted2);
148
Arrays.sort (options, presenter);
149         JButton JavaDoc [] twiceSorted2 = (JButton JavaDoc [])options.clone ();
150         //showButtonArray (twiceSorted2);
151
assertFalse ("Original options not same as sorted option on reused dialog.", Arrays.asList (backup).equals (Arrays.asList (onceSorted2)));
152         assertEquals ("Sorting of options is invariable also on reused dialog.", Arrays.asList (onceSorted2), Arrays.asList (twiceSorted2));
153         assertEquals ("The options are sorted same on both dialogs.", Arrays.asList (onceSorted), Arrays.asList (twiceSorted2));
154         
155     }
156     
157     private void showButtonArray (Object JavaDoc [] array) {
158         JButton JavaDoc [] arr = (JButton JavaDoc []) array;
159         System.out.print("do: ");
160         for (int i = 0; i < arr.length; i++) {
161             System.out.print(arr[i].getText() + ", ");
162         }
163         System.out.println(".");
164     }
165     
166 }
167
Popular Tags