2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.editor.mimelookup.impl;
44
import java.io.IOException;
45
import java.util.List;
46
import junit.framework.*;
47
import org.netbeans.api.editor.mimelookup.MimeLookup;
48
import org.netbeans.api.editor.mimelookup.MimePath;
49
import org.netbeans.junit.NbTestCase;
50
import org.openide.actions.CutAction;
51
import org.openide.actions.FindAction;
52
import org.openide.actions.RenameAction;
53
import org.openide.actions.ReplaceAction;
54
import org.openide.util.Lookup;
55
import org.openide.util.Lookup.Template;
56
import org.openide.util.LookupEvent;
57
import org.openide.util.LookupListener;
59
/** Testing functionality of dynamic change over inherited folders
61
* @author Martin Roskanin
63
public class MimeLookupPopupItemsChangeTest extends NbTestCase {
65
private static final int WAIT_TIME = 5000;
66
private String fsstruct [];
68
public MimeLookupPopupItemsChangeTest(String testName) {
72
protected @Override void setUp() throws Exception {
74
fsstruct = new String [] {
75
"Editors/Popup/org-openide-actions-CutAction.instance", //NOI18N
76
"Editors/Popup/org-openide-actions-CopyAction.instance", //NOI18N
77
"Editors/Popup/org-openide-actions-PasteAction.instance", //NOI18N
78
"Editors/text/html/Popup/org-openide-actions-DeleteAction.instance", //NOI18N
79
"Editors/text/html/Popup/org-openide-actions-RenameAction.instance", //NOI18N
80
"Editors/text/xml/text/html/Popup/org-openide-actions-PrintAction.instance", //NOI18N
81
"Editors/text/x-java/text/xml/text/html/Popup/org-openide-actions-NewAction.instance", //NOI18N
84
EditorTestLookup.setLookup(fsstruct, getWorkDir(), new Object[] {},
85
getClass().getClassLoader());
89
/** Testing Base level popup items lookup and sorting */
90
public void testDynamicChangeInPopupFolders() throws IOException{
91
final int resultChangedCount[] = new int[1];
92
resultChangedCount[0] = 0;
94
MimePath mp = MimePath.parse("text/x-java/text/xml/text/html");
95
Lookup lookup = MimeLookup.getLookup(mp);
96
Lookup.Result result = lookup.lookup(new Template(PopupActions.class));
97
result.allInstances(); // remove this line if issue #60010 is fixed
98
LookupListener listener = new LookupListener(){
99
public void resultChanged(LookupEvent ev){
100
resultChangedCount[0]++;
103
result.addLookupListener(listener);
104
PopupActions actions = (PopupActions) lookup.lookup(PopupActions.class);
105
assertTrue("PopupActions should be found", actions != null);
106
List popupActions = actions.getPopupActions();
107
int size = popupActions.size();
108
assertTrue("Number of PopupActions found:"+size+" and should be:"+fsstruct.length, size == fsstruct.length);
110
//delete RenameAction
111
TestUtilities.deleteFile(getWorkDir(),
112
"Editors/text/html/Popup/org-openide-actions-RenameAction.instance");
113
checkPopupItemPresence(lookup, RenameAction.class, false);
115
// check firing the change
116
assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of 1"),resultChangedCount[0] == 1);
117
resultChangedCount[0] = 0;
119
//delete base CutAction
120
TestUtilities.deleteFile(getWorkDir(),
121
"Editors/Popup/org-openide-actions-CutAction.instance");
123
checkPopupItemPresence(lookup, CutAction.class, false);
125
// check firing the change
126
assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of 1"),resultChangedCount[0] == 1);
127
resultChangedCount[0] = 0;
129
//simulate module installation, new action will be added
130
TestUtilities.createFile(getWorkDir(),
131
"Editors/Popup/org-openide-actions-FindAction.instance"); //NOI18N
133
checkPopupItemPresence(lookup, FindAction.class, true);
135
// check firing the change
136
assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of 1"),resultChangedCount[0] == 1);
137
resultChangedCount[0] = 0;
139
//simulate module installation, new action will be added
140
TestUtilities.createFile(getWorkDir(),
141
"Editors/text/x-java/text/xml/text/html/Popup/org-openide-actions-ReplaceAction.instance"); //NOI18N
143
checkPopupItemPresence(lookup, ReplaceAction.class, true);
145
//ReplaceAction was created in the uppermost folder
146
// let's try it is missing in the lower lookup
147
mp = MimePath.get(MimePath.get("text/x-java"), "text/xml");
148
lookup = MimeLookup.getLookup(mp);
149
checkPopupItemPresence(lookup, ReplaceAction.class, false);
150
checkPopupItemPresence(lookup, FindAction.class, true);
152
// lookup for ReplaceAction in the folder that doesn't exist
153
lookup = MimeLookup.getLookup("text/html"); //NOI18N
154
checkPopupItemPresence(lookup, ReplaceAction.class, false);
155
// create folder with ReplaceAction
156
TestUtilities.createFile(getWorkDir(),
157
"Editors/text/html/Popup/org-openide-actions-ReplaceAction.instance"); //NOI18N
159
checkPopupItemPresence(lookup, ReplaceAction.class, true);
163
private void checkPopupItemPresence(final Lookup lookup, final Class checkedClazz, final boolean shouldBePresent){
164
TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){
165
public Object getValue(){
166
PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class);
168
return Boolean.FALSE;
170
boolean bool = false;
171
List items = pa.getPopupActions();
172
for (int i=0; i<items.size(); i++){
173
Object obj = items.get(i);
174
if (checkedClazz == obj.getClass()){
179
if (!shouldBePresent){
182
return Boolean.valueOf(bool);
185
PopupActions pa = (PopupActions)lookup.lookup(PopupActions.class);
186
assertTrue("PopupActions should be found", pa != null);
187
boolean bool = false;
188
List items = pa.getPopupActions();
189
for (int i=0; i<items.size(); i++){
190
Object obj = items.get(i);
191
if (checkedClazz == obj.getClass()){
196
if (shouldBePresent){
197
assertTrue("Class: "+checkedClazz+" should be present in lookup", bool);
199
assertTrue("Class: "+checkedClazz+" should not be present in lookup", !bool);