1 19 20 package org.netbeans.modules.navigator; 21 22 import java.util.List ; 23 import javax.swing.JComboBox ; 24 import javax.swing.JComponent ; 25 import javax.swing.JLabel ; 26 import org.netbeans.junit.NbTest; 27 import org.netbeans.junit.NbTestCase; 28 import org.netbeans.junit.NbTestSuite; 29 import org.netbeans.spi.navigator.NavigatorHandler; 30 import org.netbeans.spi.navigator.NavigatorLookupHint; 31 import org.netbeans.spi.navigator.NavigatorPanel; 32 import org.openide.util.ContextGlobalProvider; 33 import org.openide.util.Lookup; 34 import org.openide.util.lookup.AbstractLookup; 35 import org.openide.util.lookup.InstanceContent; 36 import org.openide.util.lookup.Lookups; 37 38 39 43 public class NavigatorTCTest extends NbTestCase { 44 45 46 public NavigatorTCTest() { 47 super(""); 48 } 49 50 public NavigatorTCTest(String testName) { 51 super(testName); 52 } 53 54 public static void main(java.lang.String [] args) { 55 junit.textui.TestRunner.run(suite()); 56 } 57 58 public static NbTest suite() { 59 NbTestSuite suite = new NbTestSuite(NavigatorTCTest.class); 60 return suite; 61 } 62 63 64 public void testCorrectCallsOfNavigatorPanelMethods () throws Exception { 65 System.out.println("Testing correct calls of NavigatorPanel methods..."); 66 InstanceContent ic = getInstanceContent(); 67 68 TestLookupHint ostravskiHint = new TestLookupHint("ostravski/gyzd"); 69 ic.add(ostravskiHint); 71 72 NavigatorTC navTC = NavigatorTC.getInstance(); 73 navTC.componentOpened(); 74 75 NavigatorPanel selPanel = navTC.getSelectedPanel(); 76 77 assertNotNull("Selected panel is null", selPanel); 78 assertTrue("Panel class not expected", selPanel instanceof OstravskiGyzdProvider); 79 OstravskiGyzdProvider ostravak = (OstravskiGyzdProvider)selPanel; 80 assertEquals("panelActivated calls count invalid: " + ostravak.getPanelActivatedCallsCount(), 81 1, ostravak.getPanelActivatedCallsCount()); 82 assertEquals(0, ostravak.getPanelDeactivatedCallsCount()); 83 84 TestLookupHint prazskyHint = new TestLookupHint("prazsky/pepik"); 85 ic.add(prazskyHint); 86 ic.remove(ostravskiHint); 87 88 waitForChange(); 91 92 selPanel = navTC.getSelectedPanel(); 93 assertNotNull(selPanel); 94 assertTrue(selPanel instanceof PrazskyPepikProvider); 95 PrazskyPepikProvider prazak = (PrazskyPepikProvider)selPanel; 96 97 assertEquals(1, ostravak.getPanelDeactivatedCallsCount()); 98 assertTrue(ostravak.wasGetCompBetween()); 99 assertFalse(ostravak.wasActCalledOnActive()); 100 assertFalse(ostravak.wasDeactCalledOnInactive()); 101 102 assertEquals(1, prazak.getPanelActivatedCallsCount()); 103 assertEquals(0, prazak.getPanelDeactivatedCallsCount()); 104 105 ic.remove(prazskyHint); 106 ic.add(ostravskiHint); 107 waitForChange(); 110 111 selPanel = navTC.getSelectedPanel(); 112 assertNotNull("Selected panel is null", selPanel); 113 114 assertEquals(1, prazak.getPanelDeactivatedCallsCount()); 115 assertTrue(prazak.wasGetCompBetween()); 116 assertFalse(prazak.wasActCalledOnActive()); 117 assertFalse(prazak.wasDeactCalledOnInactive()); 118 119 navTC.componentClosed(); 120 121 selPanel = navTC.getSelectedPanel(); 122 assertNull("Selected panel should be null", selPanel); 123 assertNull("Set of panels should be null", navTC.getPanels()); 124 125 ic.remove(ostravskiHint); 127 } 128 129 public void testBugfix80155_NotEmptyOnProperties () throws Exception { 130 System.out.println("Testing bugfix 80155, keeping content on Properties window and similar..."); 131 InstanceContent ic = getInstanceContent(); 132 133 TestLookupHint ostravskiHint = new TestLookupHint("ostravski/gyzd"); 134 ic.add(ostravskiHint); 135 136 NavigatorTC navTC = NavigatorTC.getInstance(); 137 navTC.componentOpened(); 138 139 NavigatorPanel selPanel = navTC.getSelectedPanel(); 140 141 assertNotNull("Selected panel is null", selPanel); 142 143 ic.remove(ostravskiHint); 144 145 waitForChange(); 148 149 selPanel = navTC.getSelectedPanel(); 152 assertNotNull("Selected panel is null", selPanel); 153 assertTrue("Panel class not expected", selPanel instanceof OstravskiGyzdProvider); 154 } 155 156 public void testBugfix93123_RefreshCombo () throws Exception { 157 System.out.println("Testing bugfix 93123, correct refreshing of combo box with providers list..."); 158 159 InstanceContent ic = getInstanceContent(); 160 161 TestLookupHint ostravskiHint = new TestLookupHint("ostravski/gyzd"); 162 ic.add(ostravskiHint); 163 TestLookupHint prazskyHint = new TestLookupHint("prazsky/pepik"); 164 ic.add(prazskyHint); 165 166 NavigatorTC navTC = NavigatorTC.getInstance(); 167 navTC.componentOpened(); 168 169 List <NavigatorPanel> panels = navTC.getPanels(); 170 171 assertTrue("Expected 2 provider panels, but got " + panels.size(), panels.size() == 2); 172 173 NavigatorHandler.activatePanel(panels.get(1)); 174 175 NavigatorPanel selPanel = navTC.getSelectedPanel(); 176 int selIdx = panels.indexOf(selPanel); 177 178 assertTrue("Expected selected provider #2, but got #1", selIdx == 1); 179 180 TestLookupHint prazskyHint2 = new TestLookupHint("prazsky/pepik"); 181 ic.add(prazskyHint2); 182 183 waitForChange(); 186 187 panels = navTC.getPanels(); 188 assertTrue("Expected 3 provider panels, but got " + panels.size(), panels.size() == 3); 189 190 JComboBox combo = navTC.getPanelSelector(); 191 assertTrue("Expected 3 combo items, but got " + combo.getItemCount(), combo.getItemCount() == 3); 192 193 assertTrue("Expected the same selection", selPanel.equals(navTC.getSelectedPanel())); 194 195 selIdx = panels.indexOf(selPanel); 196 assertTrue("Expected the same selection in combo, sel panel index: " 197 + selIdx + ", sel in combo index: " + 198 combo.getSelectedIndex(), selIdx == combo.getSelectedIndex()); 199 200 } 201 202 206 private static InstanceContent getInstanceContent () throws Exception { 207 if (instanceContent == null) { 208 instanceContent = new InstanceContent(); 209 GlobalLookup4TestImpl nodesLkp = new GlobalLookup4TestImpl(instanceContent); 210 UnitTestUtils.prepareTest(new String [] { 211 "/org/netbeans/modules/navigator/resources/testCorrectCallsOfNavigatorPanelMethodsLayer.xml" }, 212 Lookups.singleton(nodesLkp) 213 ); 214 } 215 return instanceContent; 216 } 217 218 private void waitForChange () { 219 synchronized (this) { 220 try { 221 wait(NavigatorController.COALESCE_TIME + 500); 222 } catch (InterruptedException exc) { 223 System.out.println("waiting interrupted..."); 224 } 225 } 226 } 227 228 229 232 private static abstract class CorrectCallsProvider implements NavigatorPanel { 233 234 private int panelActCalls = 0; 235 private int panelDeactCalls = 0; 236 237 private boolean wasGetCompBetween = true; 238 239 private boolean wasActCalledOnActive = false; 240 private boolean wasDeactCalledOnInactive = false; 241 242 private boolean activated = false; 243 244 public JComponent getComponent () { 245 if (!activated) { 246 wasGetCompBetween = false; 247 } 248 return null; 249 } 250 251 public void panelActivated (Lookup context) { 252 if (activated) { 253 wasActCalledOnActive = true; 254 } 255 panelActCalls++; 256 activated = true; 257 } 258 259 public void panelDeactivated () { 260 if (!activated) { 261 wasDeactCalledOnInactive = true; 262 } 263 panelDeactCalls++; 264 activated = false; 265 } 266 267 public Lookup getLookup () { 268 return null; 269 } 270 271 public int getPanelActivatedCallsCount () { 272 return panelActCalls; 273 } 274 275 public int getPanelDeactivatedCallsCount () { 276 return panelDeactCalls; 277 } 278 279 public boolean wasGetCompBetween () { 280 return wasGetCompBetween; 281 } 282 283 public boolean wasActCalledOnActive () { 284 return wasActCalledOnActive; 285 } 286 287 public boolean wasDeactCalledOnInactive () { 288 return wasDeactCalledOnInactive; 289 } 290 291 } 292 293 public static final class OstravskiGyzdProvider extends CorrectCallsProvider { 294 295 public String getDisplayName () { 296 return "Ostravski Gyzd"; 297 } 298 299 public String getDisplayHint () { 300 return null; 301 } 302 303 public JComponent getComponent () { 304 super.getComponent(); 306 return new JLabel (getDisplayName()); 307 } 308 309 } 310 311 public static final class PrazskyPepikProvider extends CorrectCallsProvider { 312 313 public String getDisplayName () { 314 return "Prazsky Pepik"; 315 } 316 317 public String getDisplayHint () { 318 return null; 319 } 320 321 public JComponent getComponent () { 322 super.getComponent(); 324 return new JLabel (getDisplayName()); 325 } 326 327 } 328 329 332 private static final class TestLookupHint implements NavigatorLookupHint { 333 334 private final String contentType; 335 336 public TestLookupHint (String contentType) { 337 this.contentType = contentType; 338 } 339 340 public String getContentType () { 341 return contentType; 342 } 343 344 } 345 346 347 private static final class GlobalLookup4TestImpl extends AbstractLookup implements ContextGlobalProvider { 348 349 public GlobalLookup4TestImpl (AbstractLookup.Content content) { 350 super(content); 351 } 352 353 public Lookup createGlobalContext() { 354 return this; 355 } 356 357 364 } 365 private static InstanceContent instanceContent; 366 367 } 368 | Popular Tags |