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: package org.openide;
042:
043: import org.netbeans.junit.*;
044: import junit.framework.Test;
045: import junit.framework.TestSuite;
046: import org.netbeans.junit.NbTestCase;
047: import org.netbeans.junit.NbTestSuite;
048:
049: import java.awt.Component;
050: import java.util.*;
051: import javax.swing.*;
052: import javax.swing.JLabel;
053: import javax.swing.event.ChangeListener;
054: import org.netbeans.junit.NbTestCase;
055: import org.openide.util.*;
056: import org.openide.util.HelpCtx;
057:
058: /** Testing the order of calling the WizardDescriptor support methods and
059: * the methods in implemented wizard's interfaces.
060: * @see issue 46587, issue 46589
061: * @author Jiri Rechtacek, Jesse Glick
062: *
063: */
064: public class WizardDescriptorOrderTest extends NbTestCase {
065:
066: public WizardDescriptorOrderTest(String name) {
067: super (name);
068: }
069:
070: public static void main(String[] args) {
071: junit.textui.TestRunner.run(new NbTestSuite(
072: WizardDescTest.class));
073: System.exit(0);
074: }
075:
076: WizardDescriptor wd;
077: TestPanel[] panels;
078:
079: // help fields to control the order of calls to panel
080: int[] readSettingsCalls;
081: int[] storeSettingsCalls;
082:
083: // help variables
084: boolean checkStoreBeforeNext;
085: boolean checkStoreOldeBeforeReadNew;
086: boolean checkPCH2FinishOption;
087:
088: protected final void setUp() {
089: panels = new TestPanel[] { new TestPanel(0), new TestPanel(1) };
090: readSettingsCalls = new int[] { 0, 0 };
091: storeSettingsCalls = new int[] { 0, 0 };
092: wd = new WizardDescriptor(
093: new TestIterator(panels[0], panels[1]));
094: wd.addPropertyChangeListener(new Listener());
095: java.awt.Dialog d = DialogDisplayer.getDefault().createDialog(
096: wd);
097: //d.show();
098:
099: checkStoreBeforeNext = false;
100: checkStoreOldeBeforeReadNew = false;
101: checkPCH2FinishOption = false;
102: }
103:
104: public void testReadSettingsOnFirstPanel() throws Exception {
105: log("Wizard has been initialized.");
106: assertTrue(
107: "WD.P.readSettings on the first panel has been called.",
108: readSettingsCalls[0] > 0);
109: }
110:
111: public void testOrderNextPanelAndStoreSettings() throws Exception {
112: checkStoreBeforeNext = true;
113: log("Do click Next button.");
114: wd.doNextClick();
115: }
116:
117: public void testStoreOldBeforeReadNew() throws Exception {
118: checkStoreOldeBeforeReadNew = true;
119: log("Do click Next button.");
120: wd.doNextClick();
121: }
122:
123: public void testStoreLastBeforePCH2Finish() throws Exception {
124: log("Do click Next button.");
125: wd.doNextClick();
126: checkPCH2FinishOption = true;
127: log("Do click Finish button.");
128: wd.doFinishClick();
129: }
130:
131: public class Listener implements java.beans.PropertyChangeListener {
132:
133: public void propertyChange(
134: java.beans.PropertyChangeEvent propertyChangeEvent) {
135: if (WizardDescriptor.PROP_VALUE.equals(propertyChangeEvent
136: .getPropertyName())) {
137: log("propertyChange [time: "
138: + System.currentTimeMillis()
139: + "] with PROP_VALUE: "
140: + handleValue(wd.getValue()));
141:
142: if (checkPCH2FinishOption
143: && WizardDescriptor.FINISH_OPTION
144: .equals(propertyChangeEvent
145: .getNewValue())) {
146: assertTrue(
147: "WD.P.storeSettings on the last panel has been called before propertyChangeEvent to FINISH_OPTION was fired.",
148: storeSettingsCalls[1] > 0);
149: }
150: }
151: }
152:
153: }
154:
155: private final class TestPanel implements WizardDescriptor.Panel {
156:
157: private final int x;
158:
159: public TestPanel(int x) {
160: this .x = x;
161: }
162:
163: public void readSettings(Object settings) {
164: readSettingsCalls[x]++;
165: log("readSettings of panel: " + x + " [time: "
166: + System.currentTimeMillis()
167: + "] with PROP_VALUE: "
168: + handleValue(wd.getValue()));
169: }
170:
171: public void addChangeListener(ChangeListener l) {
172: }
173:
174: public void storeSettings(Object settings) {
175: if (checkStoreOldeBeforeReadNew && (x > 0)) {
176: assertTrue(
177: "WD.P.storeSettings on the previous panel has been called before WD.P.readSettings on next panel.",
178: readSettingsCalls[x - 1] > 0);
179: }
180: storeSettingsCalls[x]++;
181: log("storeSettings of panel: " + x + " [time: "
182: + System.currentTimeMillis()
183: + "] with PROP_VALUE: "
184: + handleValue(wd.getValue()));
185: }
186:
187: public void removeChangeListener(ChangeListener l) {
188: }
189:
190: public boolean isValid() {
191: return true;
192: }
193:
194: public HelpCtx getHelp() {
195: return null;
196: }
197:
198: public Component getComponent() {
199: return new JLabel("panel #" + x);
200: }
201: }
202:
203: private final class TestIterator implements
204: WizardDescriptor.Iterator {
205: private final TestPanel panel1, panel2;
206: private int which = 0;
207:
208: public TestIterator(TestPanel panel1, TestPanel panel2) {
209: this .panel1 = panel1;
210: this .panel2 = panel2;
211: }
212:
213: public boolean hasNext() {
214: return which == 0;
215: }
216:
217: public WizardDescriptor.Panel current() {
218: log("current: " + name());
219: TestPanel currentPanel = which == 0 ? panel1 : panel2;
220: return currentPanel;
221: }
222:
223: public void nextPanel() {
224: if (checkStoreBeforeNext) {
225: assertTrue(
226: "WD.P.storeSettings on the previous panel has been called before WD.I.nextPanel.",
227: storeSettingsCalls[which] > 0);
228: }
229: which++;
230: }
231:
232: public void removeChangeListener(ChangeListener l) {
233: }
234:
235: public boolean hasPrevious() {
236: return which > 0;
237: }
238:
239: public void previousPanel() {
240: which--;
241: ;
242: }
243:
244: public String name() {
245: return which == 0 ? "First Panel" : "Second Panel";
246: }
247:
248: public void addChangeListener(ChangeListener l) {
249: }
250: }
251:
252: public String handleValue(Object val) {
253: if (val == null)
254: return "NULL";
255: if (val instanceof String)
256: return (String) val;
257: if (WizardDescriptor.FINISH_OPTION.equals(val))
258: return "FINISH_OPTION";
259: if (WizardDescriptor.CANCEL_OPTION.equals(val))
260: return "CANCEL_OPTION";
261: if (WizardDescriptor.CLOSED_OPTION.equals(val))
262: return "CLOSED_OPTION";
263: if (val instanceof JButton) {
264: JButton butt = (JButton) val;
265: ResourceBundle b = NbBundle.getBundle("org.openide.Bundle"); // NOI18N
266: if (b.getString("CTL_NEXT").equals(butt.getText()))
267: return "NEXT_OPTION";
268: if (b.getString("CTL_PREVIOUS").equals(butt.getText()))
269: return "NEXT_PREVIOUS";
270: if (b.getString("CTL_FINISH").equals(butt.getText()))
271: return "FINISH_OPTION";
272: if (b.getString("CTL_CANCEL").equals(butt.getText()))
273: return "CANCEL_OPTION";
274: }
275: return "UNKNOWN OPTION: " + val;
276: }
277:
278: }
|