1 19 20 package org.netbeans.core.windows.services; 21 22 import java.util.Arrays ; 23 import javax.swing.JButton ; 24 import javax.swing.JFrame ; 25 import javax.swing.JLabel ; 26 import org.openide.DialogDescriptor; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.util.HelpCtx; 29 30 34 public class NbPresenterTest extends NbTestCase { 35 36 public NbPresenterTest (String 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 JButton erase = new JButton ("Erase all my data"); 58 JButton rescue = new JButton ("Rescue"); 59 JButton cancel = new JButton ("Cancel"); 60 JButton [] options = new JButton [] {erase, rescue, cancel}; 61 DialogDescriptor dd = new DialogDescriptor (new JLabel ("Something interesting"), "My dialog", modal, 62 options, 64 rescue, 65 DialogDescriptor.RIGHT_ALIGN, 67 new HelpCtx (NbPresenterTest.class), null); 68 69 70 dd.setClosingOptions (new Object [0]); 71 72 NbPresenter presenter = new NbDialog (dd, (JFrame )null); 73 presenter.setVisible (true); 74 75 erase.doClick (); 76 assertEquals ("Erase was invoked.", erase.getText (), ((JButton )dd.getValue ()).getText ()); 77 erase.doClick (); 78 assertEquals ("Erase was invoked again on same dialog.", erase.getText (), ((JButton )dd.getValue ()).getText ()); 79 presenter.dispose (); 80 81 presenter = new NbDialog (dd, (JFrame )null); 82 presenter.setVisible (true); 83 84 erase.doClick (); 85 assertEquals ("Erase was invoked of reused dialog.", erase.getText (), ((JButton )dd.getValue ()).getText ()); 86 erase.doClick (); 87 assertEquals ("Erase was invoked again on reused dialog.", erase.getText (), ((JButton )dd.getValue ()).getText ()); 88 presenter.dispose (); 89 90 presenter = new NbDialog (dd, (JFrame )null); 91 presenter.setVisible (true); 92 93 rescue.doClick (); 94 assertEquals ("Rescue was invoked of reused dialog.", rescue.getText (), ((JButton )dd.getValue ()).getText ()); 95 rescue.doClick (); 96 assertEquals ("Rescue was invoked again on reused dialog.", rescue.getText (), ((JButton )dd.getValue ()).getText ()); 97 presenter.dispose (); 98 99 presenter = new NbDialog (dd, (JFrame )null); 100 presenter.setVisible (true); 101 102 cancel.doClick (); 103 assertEquals ("Cancel was invoked of reused dialog.", cancel.getText (), ((JButton )dd.getValue ()).getText ()); 104 cancel.doClick (); 105 assertEquals ("Cancel was invoked again on reused dialog.", cancel.getText (), ((JButton )dd.getValue ()).getText ()); 106 presenter.dispose (); 107 } 108 109 public void testNbPresenterComparator () { 110 JButton erase = new JButton ("Erase all my data"); 111 JButton rescue = new JButton ("Rescue"); 112 JButton cancel = new JButton ("Cancel"); 113 JButton [] options = new JButton [] {erase, rescue, cancel}; 114 DialogDescriptor dd = new DialogDescriptor (new JLabel ("Something interesting"), "My dialog", false, 115 options, 117 rescue, 118 DialogDescriptor.RIGHT_ALIGN, 120 null, null); 121 122 dd.setClosingOptions (null); 123 124 NbPresenter presenter = new NbDialog (dd, (JFrame )null); 125 assertEquals ("Dialog has Rescue option as default value.", rescue, dd.getDefaultValue ()); 126 JButton [] backup = (JButton [])options.clone (); 127 Arrays.sort (options, presenter); 129 JButton [] onceSorted = (JButton [])options.clone (); 131 Arrays.sort (options, presenter); 132 JButton [] twiceSorted = (JButton [])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 )null); 141 presenter.setVisible (true); 142 143 options = (JButton [])backup.clone (); 144 Arrays.sort (options, presenter); 146 JButton [] onceSorted2 = (JButton [])options.clone (); 147 Arrays.sort (options, presenter); 149 JButton [] twiceSorted2 = (JButton [])options.clone (); 150 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 [] array) { 158 JButton [] arr = (JButton []) 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 |