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:
042: package org.netbeans.core.multiview;
043:
044: import java.awt.event.ActionEvent;
045:
046: import javax.swing.AbstractAction;
047:
048: import javax.swing.Action;
049: import javax.swing.ActionMap;
050: import javax.swing.JComponent;
051:
052: import junit.framework.Test;
053: import junit.framework.TestSuite;
054: import junit.textui.TestRunner;
055: import org.netbeans.core.api.multiview.MultiViewHandler;
056: import org.netbeans.core.api.multiview.MultiViews;
057: import org.netbeans.core.spi.multiview.MultiViewDescription;
058: import org.netbeans.core.spi.multiview.MultiViewElement;
059:
060: import org.netbeans.core.spi.multiview.MultiViewFactory;
061: import org.netbeans.junit.NbTestCase;
062: import org.netbeans.junit.NbTestSuite;
063:
064: import org.openide.util.Lookup;
065: import org.openide.util.LookupEvent;
066:
067: import org.openide.util.LookupListener;
068: import org.openide.util.Utilities;
069: import org.openide.util.lookup.Lookups;
070:
071: import org.openide.windows.TopComponent;
072:
073: /**
074: *
075: * @author Milos Kleint
076: */
077: public class MultiViewActionMapTest extends NbTestCase {
078:
079: /** Creates a new instance of SFSTest */
080: public MultiViewActionMapTest(String name) {
081: super (name);
082: }
083:
084: /**
085: * @param args the command line arguments
086: */
087: public static void main(java.lang.String[] args) {
088: TestRunner.run(suite());
089: }
090:
091: public static Test suite() {
092: TestSuite suite = new NbTestSuite(MultiViewActionMapTest.class);
093:
094: return suite;
095: }
096:
097: protected boolean runInEQ() {
098: return true;
099: }
100:
101: public void testElementIsTopComponent() throws Exception {
102: MVElemTopComponent elem1 = new MVElemTopComponent();
103: MVElemTopComponent elem2 = new MVElemTopComponent();
104: MVElemTopComponent elem3 = new MVElemTopComponent();
105: doTestActionMap(elem1, elem2, elem3);
106: }
107:
108: public void testElementIsNotTC() throws Exception {
109: MVElem elem1 = new MVElem();
110: MVElem elem2 = new MVElem();
111: MVElem elem3 = new MVElem();
112: doTestActionMap(elem1, elem2, elem3);
113: }
114:
115: private void doTestActionMap(MultiViewElement elem1,
116: MultiViewElement elem2, MultiViewElement elem3) {
117: MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
118: MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
119: MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
120:
121: MultiViewDescription[] descs = new MultiViewDescription[] {
122: desc1, desc2, desc3 };
123: TopComponent tc = MultiViewFactory
124: .createMultiView(descs, desc1);
125: // WARNING: as anything else the first element's action map is set only after the tc is opened..
126: tc.open();
127:
128: Action act = new TestAction("MultiViewAction");
129: // add action to the MVTC map
130: tc.getActionMap().put("testkey", act);
131: ActionMap obj = (ActionMap) tc.getLookup().lookup(
132: ActionMap.class);
133: assertNotNull(obj);
134: assertEquals(obj.getClass(),
135: MultiViewTopComponentLookup.LookupProxyActionMap.class);
136: Action res = (Action) obj.get("testkey");
137: assertNotNull(res);
138: assertEquals("MultiViewAction", res.getValue(Action.NAME));
139: // remove action from the MVTC map
140: tc.getActionMap().remove("testkey");
141: obj = (ActionMap) tc.getLookup().lookup(ActionMap.class);
142: res = (Action) obj.get("testkey");
143: assertNull(res);
144:
145: // make sure the action in MVTC has higher priority..
146: JComponent elemtc = elem1.getVisualRepresentation();
147: Action innerAct = new TestAction("InnerAction");
148: elemtc.getActionMap().put("testkey", innerAct);
149: assertNotNull(elemtc.getActionMap().get("testkey"));
150: obj = (ActionMap) tc.getLookup().lookup(ActionMap.class);
151: // check if anything there in elemen'ts actionmap
152: assertNotNull(obj);
153: res = (Action) obj.get("testkey");
154: assertNotNull(res);
155: // put actin to the mvtc actionmap as well..
156: tc.getActionMap().put("testkey", act);
157: assertNotNull(obj);
158: res = (Action) obj.get("testkey");
159: assertNotNull(res);
160: assertEquals("MultiViewAction", res.getValue(Action.NAME));
161: //remove from mvtc's map..
162: tc.getActionMap().remove("testkey");
163: res = (Action) obj.get("testkey");
164: assertNotNull(res);
165: assertEquals("InnerAction", res.getValue(Action.NAME));
166: // now switch to the other element...
167: MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
168: // test related hack, easy establishing a connection from Desc->perspective
169: Accessor.DEFAULT.createPerspective(desc2);
170: handler.requestVisible(Accessor.DEFAULT
171: .createPerspective(desc2));
172: obj = (ActionMap) tc.getLookup().lookup(ActionMap.class);
173: res = (Action) obj.get("testkey");
174: assertNull(res); // is not defined in element2
175: }
176:
177: public void testActionMapChanges() throws Exception {
178: MVElemTopComponent elem1 = new MVElemTopComponent();
179: MVElemTopComponent elem2 = new MVElemTopComponent();
180: MVElem elem3 = new MVElem();
181: MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
182: MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
183: MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
184:
185: MultiViewDescription[] descs = new MultiViewDescription[] {
186: desc1, desc2, desc3 };
187: TopComponent tc = MultiViewFactory
188: .createMultiView(descs, desc1);
189: // WARNING: as anything else the first element's action map is set only after the tc is opened..
190: Lookup.Result result = tc.getLookup().lookup(
191: new Lookup.Template(ActionMap.class));
192: LookListener list = new LookListener();
193: list.resetCount();
194: result.addLookupListener(list);
195: result.allItems();
196:
197: tc.open();
198: assertEquals(1, list.getCount());
199:
200: MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
201: // test related hack, easy establishing a connection from Desc->perspective
202: Accessor.DEFAULT.createPerspective(desc2);
203: handler.requestVisible(Accessor.DEFAULT
204: .createPerspective(desc2));
205: assertEquals(2, list.getCount());
206:
207: Accessor.DEFAULT.createPerspective(desc3);
208: handler.requestVisible(Accessor.DEFAULT
209: .createPerspective(desc3));
210: assertEquals(3, list.getCount());
211: }
212:
213: public void testSimplifiedActionMapChanges81117() {
214: MultiViewTopComponentLookup.InitialProxyLookup lookup = new MultiViewTopComponentLookup.InitialProxyLookup(
215: new ActionMap());
216: Lookup.Result res = lookup.lookup(new Lookup.Template(
217: ActionMap.class));
218: LookListener list = new LookListener();
219: list.resetCount();
220: res.addLookupListener(list);
221: assertEquals(1, res.allInstances().size());
222: assertEquals(0, list.getCount());
223: lookup.refreshLookup();
224: assertEquals(1, list.getCount());
225: assertEquals(1, res.allInstances().size());
226:
227: MultiViewTopComponentLookup lookup2 = new MultiViewTopComponentLookup(
228: new ActionMap());
229: res = lookup2.lookup(new Lookup.Template(ActionMap.class));
230: list = new LookListener();
231: list.resetCount();
232: res.addLookupListener(list);
233: assertEquals(1, res.allInstances().size());
234: assertEquals(0, list.getCount());
235: lookup2.setElementLookup(Lookups
236: .fixed(new Object[] { new Object() }));
237: assertEquals(1, list.getCount());
238: assertEquals(1, res.allInstances().size());
239:
240: }
241:
242: public void testActionMapChangesForElementsWithComponentShowingInit()
243: throws Exception {
244: Action act1 = new TestAction("MultiViewAction1");
245: Action act2 = new TestAction("MultiViewAction2");
246: MVElemTopComponent elem1 = new ComponentShowingElement(
247: "testAction", act1);
248: MVElemTopComponent elem2 = new ComponentShowingElement(
249: "testAction", act2);
250: MVElem elem3 = new MVElem();
251: MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
252: MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
253: MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
254:
255: MultiViewDescription[] descs = new MultiViewDescription[] {
256: desc1, desc2, desc3 };
257: TopComponent tc = MultiViewFactory
258: .createMultiView(descs, desc1);
259: Lookup.Result result = tc.getLookup().lookup(
260: new Lookup.Template(ActionMap.class));
261: LookListener2 list = new LookListener2();
262: result.addLookupListener(list);
263: result.allInstances().size();
264: list.setCorrectValues("testAction", act1);
265: // WARNING: as anything else the first element's action map is set only after the tc is opened..
266: tc.open();
267: assertEquals(1, list.getCount());
268: MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
269: // test related hack, easy establishing a connection from Desc->perspective
270: Accessor.DEFAULT.createPerspective(desc2);
271: list.setCorrectValues("testAction", act2);
272: handler.requestVisible(Accessor.DEFAULT
273: .createPerspective(desc2));
274: assertEquals(2, list.getCount());
275: Accessor.DEFAULT.createPerspective(desc3);
276: list.setCorrectValues("testAction", null);
277: handler.requestVisible(Accessor.DEFAULT
278: .createPerspective(desc3));
279: assertEquals(3, list.getCount());
280: }
281:
282: public class ComponentShowingElement extends MVElemTopComponent {
283: private String key;
284: private Action action;
285:
286: public ComponentShowingElement(String actionkey, Action value) {
287: action = value;
288: key = actionkey;
289: }
290:
291: public void componentShowing() {
292: super .componentShowing();
293: getActionMap().put(key, action);
294: }
295:
296: }
297:
298: private class LookListener2 implements LookupListener {
299: private String key;
300: private Action action;
301: int count = 0;
302: private ActionMap lastMap;
303:
304: public void setCorrectValues(String keyValue, Action actionValue) {
305: action = actionValue;
306: key = keyValue;
307: }
308:
309: public int getCount() {
310: return count;
311: }
312:
313: public void resultChanged(LookupEvent ev) {
314: Lookup.Result res = (Lookup.Result) ev.getSource();
315: assertEquals(1, res.allInstances().size());
316: ActionMap map = (ActionMap) res.allInstances().iterator()
317: .next();
318: if (lastMap != null) {
319: // because of CallbackSystemAction.GlobalManager
320: assertNotSame(map, lastMap);
321: }
322: lastMap = map;
323: Action act = map.get(key);
324: assertEquals(action, act);
325: count++;
326: }
327: }
328:
329: // //
330: // // Set of tests for ActionMap and context.. copied from CallbackSystemActionTest
331: // //
332: //
333: // public void testLookupOfStateInActionMap () throws Exception {
334: //
335: // class MyAction extends javax.swing.AbstractAction
336: // implements org.openide.util.actions.ActionPerformer {
337: // int actionPerformed;
338: // int performAction;
339: //
340: // public void actionPerformed (java.awt.event.ActionEvent ev) {
341: // actionPerformed++;
342: // }
343: //
344: // public void performAction (SystemAction a) {
345: // performAction++;
346: // }
347: // }
348: // MyAction action = new MyAction ();
349: //
350: // ActionMap map = new ActionMap ();
351: // CallbackSystemAction system = (CallbackSystemAction)SystemAction.get(SurviveFocusChgCallbackAction.class);
352: // system.setActionPerformer (null);
353: // map.put (system.getActionMapKey(), action);
354: //
355: // javax.swing.Action clone;
356: // clone = system.createContextAwareInstance(org.openide.util.Lookup.EMPTY);
357: //
358: // assertTrue ("Action should not be enabled if no callback provided", !clone.isEnabled());
359: //
360: // system.setActionPerformer (action);
361: // assertTrue ("Is enabled, because it has a performer", clone.isEnabled());
362: // system.setActionPerformer (null);
363: // assertTrue ("Is disabled, because the performer has been unregistered", !clone.isEnabled ());
364: //
365: // //
366: // // test with actionmap
367: // //
368: // action.setEnabled (false);
369: //
370: // org.openide.util.Lookup context = org.openide.util.lookup.Lookups.singleton(map);
371: // clone = system.createContextAwareInstance(context);
372: //
373: // CntListener listener = new CntListener ();
374: // clone.addPropertyChangeListener (listener);
375: //
376: // assertTrue ("Not enabled now", !clone.isEnabled ());
377: // action.setEnabled (true);
378: // assertTrue ("Clone is enabled because the action in ActionMap is", clone.isEnabled ());
379: // listener.assertCnt ("One change expected", 1);
380: //
381: // system.setActionPerformer (action);
382: // clone.actionPerformed(new java.awt.event.ActionEvent (this, 0, ""));
383: // assertEquals ("MyAction.actionPerformed invoked", 1, action.actionPerformed);
384: // assertEquals ("MyAction.performAction is not invoked", 0, action.performAction);
385: //
386: //
387: // action.setEnabled (false);
388: // assertTrue ("Clone is disabled because the action in ActionMap is", !clone.isEnabled ());
389: // listener.assertCnt ("Another change expected", 1);
390: //
391: // clone.actionPerformed(new java.awt.event.ActionEvent (this, 0, ""));
392: // assertEquals ("MyAction.actionPerformed invoked again", 2, action.actionPerformed);
393: // assertEquals ("MyAction.performAction is not invoked, remains 0", 0, action.performAction);
394: //
395: // }
396: //
397: // private static final class CntListener extends Object
398: // implements java.beans.PropertyChangeListener {
399: // private int cnt;
400: //
401: // public void propertyChange(java.beans.PropertyChangeEvent evt) {
402: // cnt++;
403: // }
404: //
405: // public void assertCnt (String msg, int count) {
406: // assertEquals (msg, count, this.cnt);
407: // this.cnt = 0;
408: // }
409: // } // end of CntListener
410:
411: public void testActionsGlobalContext() throws Exception {
412: Lookup look = Utilities.actionsGlobalContext();
413: MVElemTopComponent elem1 = new MVElemTopComponent();
414: MVElemTopComponent elem2 = new MVElemTopComponent();
415: MVElemTopComponent elem3 = new MVElemTopComponent();
416: MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
417: MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
418: MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
419: MultiViewDescription[] descs = new MultiViewDescription[] {
420: desc1, desc2, desc3 };
421: TopComponent tc = MultiViewFactory
422: .createMultiView(descs, desc1);
423: // WARNING: as anything else the first element's action map is set only after the tc is opened..
424: tc.open();
425: tc.requestActive();
426:
427: ActionMap map = (ActionMap) look.lookup(ActionMap.class);
428: assertNotNull("is null", map);
429: assertEquals("is wrong class=" + map.getClass(),
430: map.getClass(),
431: MultiViewTopComponentLookup.LookupProxyActionMap.class);
432: Action res = map.get("testkey");
433: assertNull(res);
434: Action act = new TestAction("MultiViewAction");
435: // add action to the MVTC map
436: elem1.getVisualRepresentation().getActionMap().put("testkey",
437: act);
438: res = map.get("testkey");
439: assertNotNull(res);
440:
441: // test switching to a different component..
442: TopComponent tc2 = new TopComponent();
443: tc2.open();
444: tc2.requestActive();
445: map = (ActionMap) look.lookup(ActionMap.class);
446: res = map.get("testkey");
447: assertNull(res);
448:
449: // switch back and test a different element..
450: tc.requestActive();
451: map = (ActionMap) look.lookup(ActionMap.class);
452: MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
453: // test related hack, easy establishing a connection from Desc->perspective
454: Accessor.DEFAULT.createPerspective(desc2);
455: handler.requestVisible(Accessor.DEFAULT
456: .createPerspective(desc2));
457: res = map.get("testkey");
458: assertNull(res);
459: // now switch back to the original element and see if the action is stil there..
460: Accessor.DEFAULT.createPerspective(desc1);
461: handler.requestVisible(Accessor.DEFAULT
462: .createPerspective(desc1));
463: res = map.get("testkey");
464: assertNotNull(res);
465:
466: }
467:
468: private class TestAction extends AbstractAction {
469: public TestAction(String name) {
470: super (name);
471: }
472:
473: public void actionPerformed(ActionEvent event) {
474:
475: }
476:
477: }
478:
479: private class LookListener implements LookupListener {
480: int count = 0;
481:
482: public void resetCount() {
483: count = 0;
484: }
485:
486: public int getCount() {
487: return count;
488: }
489:
490: public void resultChanged(LookupEvent ev) {
491: count++;
492: }
493: }
494:
495: }
|