Skip to content

Package: ViewEditorPart_PTest

ViewEditorPart_PTest

nameinstructionbranchcomplexitylinemethod
ViewEditorPart_PTest()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
assertErrorMessage(ErrorEditorPart, String)
M: 22 C: 47
68%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 3 C: 12
80%
M: 0 C: 1
100%
closeAllEditors()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
contextMenu_delete_resetSelectionToRoot()
M: 1 C: 53
98%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 12
100%
M: 0 C: 1
100%
copyPaste_Detail_TextControl()
M: 9 C: 128
93%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 3 C: 36
92%
M: 0 C: 1
100%
copyPaste_Tree()
M: 0 C: 69
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 19
100%
M: 0 C: 1
100%
disposeClipboard()
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
focus(Control)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getActivePage()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getEditingDomain()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getEditor(IEditorInput)
M: 34 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getEditorParentComposite(ViewEditorPart)
M: 12 C: 17
59%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 5
63%
M: 0 C: 1
100%
keystroke(Keyboard, Control, int, char)
M: 5 C: 10
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 4
67%
M: 0 C: 1
100%
open(IEditorInput, Class)
M: 4 C: 13
76%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 4
80%
M: 0 C: 1
100%
open(String, Class)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
openError(String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
openView_NoRootEClass()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
openView_PackageNotRegistered()
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
openView_PackageRegisteredButDoesNotContainRootEClass()
M: 0 C: 33
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
safeGetEditorInput(IEditorReference)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
selectionTrackingAndUndo()
M: 0 C: 138
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 32
100%
M: 0 C: 1
100%
shortcuts_delete_resetSelectionToRoot()
M: 7 C: 61
90%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 2 C: 13
87%
M: 0 C: 1
100%
testResourceURI(String)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * lucas - initial API and implementation
13: * Christian W. Damus - bugs 543376, 545460
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.ide.editor.view;
16:
17: import static org.hamcrest.CoreMatchers.instanceOf;
18: import static org.hamcrest.CoreMatchers.is;
19: import static org.hamcrest.CoreMatchers.notNullValue;
20: import static org.hamcrest.CoreMatchers.nullValue;
21: import static org.hamcrest.MatcherAssert.assertThat;
22: import static org.junit.Assert.assertEquals;
23: import static org.junit.Assert.assertNotNull;
24: import static org.junit.Assert.assertTrue;
25: import static org.junit.Assert.fail;
26: import static org.junit.Assume.assumeThat;
27:
28: import java.lang.reflect.Field;
29: import java.text.MessageFormat;
30: import java.util.Iterator;
31:
32: import org.eclipse.core.runtime.IStatus;
33: import org.eclipse.core.runtime.Platform;
34: import org.eclipse.emf.common.ui.URIEditorInput;
35: import org.eclipse.emf.common.util.URI;
36: import org.eclipse.emf.ecp.ide.editor.view.messages.Messages;
37: import org.eclipse.emf.ecp.view.spi.model.VControl;
38: import org.eclipse.emf.ecp.view.spi.model.VView;
39: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
40: import org.eclipse.emf.edit.domain.EditingDomain;
41: import org.eclipse.emf.edit.domain.IEditingDomainProvider;
42: import org.eclipse.jface.action.IAction;
43: import org.eclipse.jface.bindings.keys.ParseException;
44: import org.eclipse.jface.viewers.ISelection;
45: import org.eclipse.jface.viewers.ISelectionProvider;
46: import org.eclipse.jface.viewers.IStructuredSelection;
47: import org.eclipse.jface.viewers.StructuredSelection;
48: import org.eclipse.swt.SWT;
49: import org.eclipse.swt.dnd.Clipboard;
50: import org.eclipse.swt.dnd.TextTransfer;
51: import org.eclipse.swt.widgets.Composite;
52: import org.eclipse.swt.widgets.Control;
53: import org.eclipse.swt.widgets.Display;
54: import org.eclipse.swt.widgets.Text;
55: import org.eclipse.swt.widgets.Tree;
56: import org.eclipse.swt.widgets.TreeItem;
57: import org.eclipse.swtbot.swt.finder.SWTBot;
58: import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
59: import org.eclipse.swtbot.swt.finder.keyboard.Keyboard;
60: import org.eclipse.swtbot.swt.finder.keyboard.KeyboardFactory;
61: import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
62: import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
63: import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
64: import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
65: import org.eclipse.ui.IEditorInput;
66: import org.eclipse.ui.IEditorPart;
67: import org.eclipse.ui.IEditorReference;
68: import org.eclipse.ui.IWorkbenchPage;
69: import org.eclipse.ui.PartInitException;
70: import org.eclipse.ui.PlatformUI;
71: import org.eclipse.ui.actions.ActionFactory;
72: import org.eclipse.ui.internal.ErrorEditorPart;
73: import org.eclipse.ui.part.EditorActionBarContributor;
74: import org.junit.After;
75: import org.junit.Test;
76:
77: /**
78: * Tests for ViewEditorPart.
79: *
80: * @author Lucas Koehler
81: *
82: */
83: @SuppressWarnings("restriction")
84: public class ViewEditorPart_PTest {
85:
86:         private static final String USER_VIEW_MODEL = "View-WellFormed.view";
87:
88:         private Clipboard clipboard;
89:
90:         @Test
91:         public void openView_NoRootEClass() {
92:                 final String expectedErrorMessage = Messages.ViewEditorPart_invalidVView_noRootEClass;
93:
94:                 final ErrorEditorPart error = openError("View-Without-RootEClass.view");
95:                 assertErrorMessage(error, expectedErrorMessage);
96:         }
97:
98:         @Test
99:         public void openView_PackageNotRegistered() {
100:                 final String expectedErrorMessage = MessageFormat.format(
101:                         Messages.ViewEditorPart_invalidVView_rootEClassPackageNotResolved,
102:                         new Object[] { "User", "http://this/is/not/registered" });
103:
104:                 final ErrorEditorPart error = openError("View-PackageNotExisting.view");
105:                 assertErrorMessage(error, expectedErrorMessage);
106:         }
107:
108:         @Test
109:         public void openView_PackageRegisteredButDoesNotContainRootEClass() {
110:                 final String expectedErrorMessage = MessageFormat.format(Messages.ViewEditorPart_ViewCannotBeDisplayed,
111:                         MessageFormat.format(Messages.ViewEditorPart_invalidVView_rootEClassNotInPackage,
112:                                 new Object[] { "NonExistingClass", "task", "http://eclipse/org/emf/ecp/makeithappen/model/task" }));
113:
114:                 final ErrorEditorPart error = openError("View-RootEClassNotExisting.view");
115:                 assertErrorMessage(error, expectedErrorMessage);
116:         }
117:
118:         @Test
119:         public void getEditingDomain() {
120:                 final IEditorPart editor = open(USER_VIEW_MODEL, IEditorPart.class);
121:                 assertThat("Editor does not provide an editing domain", editor, instanceOf(IEditingDomainProvider.class));
122:                 final IEditingDomainProvider provider = (IEditingDomainProvider) editor;
123:                 assertThat("No editing domain provided", provider.getEditingDomain(), notNullValue());
124:         }
125:
126:         @Test
127:         public void selectionTrackingAndUndo() {
128:                 final IEditorPart editor = open(USER_VIEW_MODEL, IEditorPart.class);
129:                 assumeThat("Editor does not provide an editing domain", editor, instanceOf(IEditingDomainProvider.class));
130:                 final IEditingDomainProvider provider = (IEditingDomainProvider) editor;
131:                 assumeThat("No editing domain provided", provider.getEditingDomain(), notNullValue());
132:
133:                 final ISelectionProvider selectionProvider = editor.getSite().getSelectionProvider();
134:                 assertThat("No selection provider for the editor site", selectionProvider, notNullValue());
135:
136:                 final EditorActionBarContributor contributor = (EditorActionBarContributor) editor.getEditorSite()
137:                         .getActionBarContributor();
138:                 final IAction delete = contributor.getActionBars().getGlobalActionHandler(ActionFactory.DELETE.getId());
139:                 final IAction undo = contributor.getActionBars().getGlobalActionHandler(ActionFactory.UNDO.getId());
140:
141:                 // Find a control to select
142:                 VControl control = null;
143:•                for (final Iterator<?> iter = provider.getEditingDomain().getResourceSet().getAllContents(); control == null
144:•                        && iter.hasNext();) {
145:                         final Object next = iter.next();
146:•                        if (next instanceof VControl) {
147:                                 control = (VControl) next;
148:                         }
149:                 }
150:
151:                 assumeThat("Could not find a control in the view model", control, notNullValue());
152:
153:                 selectionProvider.setSelection(new StructuredSelection(control));
154:
155:                 final ISelection selection = selectionProvider.getSelection();
156:                 assertThat(selection, instanceOf(IStructuredSelection.class));
157:                 assertThat("Control not selected", ((IStructuredSelection) selection).getFirstElement(), is(control));
158:
159:                 // Action enablement updates are deferred
160:                 SWTTestUtil.waitForUIThread();
161:
162:                 // Delete the selected control
163:                 assertThat("Delete command not enabled", delete.isEnabled(), is(true));
164:                 delete.run();
165:
166:                 assertThat("Control not deleted", control.eContainer(), nullValue());
167:                 assertThat("Editor not dirty", editor.isDirty(), is(true));
168:
169:                 // Action enablement updates are deferred
170:                 SWTTestUtil.waitForUIThread();
171:
172:                 // Undo the delete
173:                 assertThat("Undo command not enabled", undo.isEnabled(), is(true));
174:                 undo.run();
175:
176:                 assertThat("Deletion not undone", control.eContainer(), notNullValue());
177:                 assertThat("Editor still dirty", editor.isDirty(), is(false));
178:         }
179:
180:         @Test
181:         public void copyPaste_Tree() throws WidgetNotFoundException, ParseException, InterruptedException {
182:                 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
183:                 final Keyboard keyboard = KeyboardFactory.getAWTKeyboard();
184:
185:                 final ViewEditorPart editor = open(USER_VIEW_MODEL, ViewEditorPart.class);
186:                 final Composite parent = getEditorParentComposite(editor);
187:                 final Tree tree = SWTTestUtil.findControl(parent, 0, Tree.class);
188:                 final TreeItem viewItem = tree.getItem(0);
189:                 final TreeItem controlItem0 = viewItem.getItem(0);
190:                 SWTTestUtil.selectTreeItem(controlItem0); // select the VControl
191:
192:                 // Manually force focus to increase test stability because sometimes selecting a tree item programmatically
193:                 // doesn't set focus or at least not fast enough
194:                 focus(tree);
195:                 keystroke(keyboard, tree, SWT.MOD1, 'c');
196:
197:                 SWTTestUtil.selectTreeItem(viewItem);
198:                 SWTTestUtil.waitForUIThread();
199:
200:                 focus(tree);
201:                 keystroke(keyboard, tree, SWT.MOD1, 'v');
202:                 tree.update();
203:                 SWTTestUtil.waitForUIThread();
204:
205:                 // At the beginning of the test the view had one child control. After copy and pasting, it should have 2
206:                 assertEquals("Number of VControls in the VView object", 2, editor.getView().getChildren().size());
207:                 assertEquals("Number of VControls in the tree", 2, viewItem.getItemCount());
208:         }
209:
210:         @Test
211:         public void copyPaste_Detail_TextControl() {
212:                 SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
213:                 final Keyboard keyboard = KeyboardFactory.getAWTKeyboard();
214:
215:                 final ViewEditorPart editor = open(USER_VIEW_MODEL, ViewEditorPart.class);
216:                 clipboard = new Clipboard(Display.getCurrent());
217:                 clipboard.clearContents();
218:                 final Composite parent = getEditorParentComposite(editor);
219:                 final Tree tree = SWTTestUtil.findControl(parent, 0, Tree.class);
220:                 final TreeItem viewItem = tree.getItem(0);
221:                 final TreeItem controlItem0 = viewItem.getItem(0);
222:                 focus(tree);
223:                 SWTTestUtil.selectTreeItem(controlItem0); // select the VControl
224:                 SWTTestUtil.waitForUIThread();
225:
226:                 // Get the text field of a VControl's name
227:                 final Text text = SWTTestUtil.findControl(parent, 0, Text.class);
228:                 final String string = "TEST";
229:                 text.setText(string);
230:                 text.update();
231:                 SWTTestUtil.waitForUIThread();
232:
233:                 final SWTBot bot = new SWTBot(parent);
234:                 final SWTBotText botText = bot.text(string);
235:                 botText.selectAll();
236:                 SWTTestUtil.waitForUIThread();
237:                 focus(text);
238:                 // Press multiple times to increase test stability
239:•                for (int i = 0; i < 10; i++) {
240:                         keystroke(keyboard, text, SWT.MOD1, 'c');
241:                 }
242:                 text.setSelection(0);
243:
244:•                if (Platform.getWS().equals(Platform.WS_COCOA)) {
245:                         // Can only test that we did not copy in the tree because the native keyboard
246:                         // copy/paste support in the Cocoa text widget cannot be automated via SWT
247:                         final EditingDomain domain = editor.getEditingDomain();
248:                         assertThat("Copied in the tree", domain.getClipboard(), nullValue());
249:                         return;
250:                 }
251:
252:                 // Check that copy to clip board worked
253:                 final Object contents = clipboard.getContents(TextTransfer.getInstance());
254:                 assertNotNull(contents);
255:                 assertEquals(string, contents);
256:
257:                 focus(text);
258:                 // Press multiple times to increase test stability
259:•                for (int i = 0; i < 10; i++) {
260:                         keystroke(keyboard, text, SWT.MOD1, 'v');
261:                 }
262:                 text.update();
263:                 SWTTestUtil.waitForUIThread();
264:
265:                 assertTrue(text.getText().contains(string + string));
266:         }
267:
268:         @Test
269:         public void shortcuts_delete_resetSelectionToRoot() {
270:                 final ViewEditorPart editor = open(USER_VIEW_MODEL, ViewEditorPart.class);
271:                 final Composite parent = getEditorParentComposite(editor);
272:                 final SWTBot bot = new SWTBot(parent);
273:                 final SWTBotTree tree = bot.tree();
274:                 SWTTestUtil.selectTreeItem(tree.widget.getItem(0).getItem(0));
275:                 SWTTestUtil.waitForUIThread();
276:
277:•                if (Platform.getWS().equals(Platform.WS_COCOA)) {
278:                         SWTTestUtil.pressAndReleaseKey(tree.widget, SWT.DEL);
279:                 } else {
280:                         tree.pressShortcut(Keystrokes.DELETE);
281:                 }
282:                 SWTTestUtil.waitForUIThread();
283:
284:                 final TreeItem[] selection = tree.widget.getSelection();
285:                 assertTrue("Root node was not selected automatically!",
286:•                        selection.length == 1 && selection[0].getData() instanceof VView);
287:         }
288:
289:         @Test
290:         public void contextMenu_delete_resetSelectionToRoot() {
291:                 final ViewEditorPart editor = open(USER_VIEW_MODEL, ViewEditorPart.class);
292:                 final Composite parent = getEditorParentComposite(editor);
293:                 final SWTBot bot = new SWTBot(parent);
294:                 final SWTBotTree tree = bot.tree();
295:                 SWTTestUtil.selectTreeItem(tree.widget.getItem(0).getItem(0));
296:                 SWTTestUtil.waitForUIThread();
297:
298:                 tree.contextMenu("Delete").click();
299:                 SWTTestUtil.waitForUIThread();
300:
301:                 final TreeItem[] selection = tree.widget.getSelection();
302:                 assertTrue("Root node was not selected automatically!",
303:•                        selection.length == 1 && selection[0].getData() instanceof VView);
304:         }
305:
306:         @After
307:         public void disposeClipboard() {
308:•                if (clipboard != null) {
309:                         clipboard.dispose();
310:                 }
311:         }
312:
313:         @After
314:         public void closeAllEditors() {
315:                 getActivePage().closeAllEditors(false);
316:         }
317:
318:         // Test Infrastructure to open editors
319:
320:         <P extends IEditorPart> P open(String resourcePath, Class<P> expectedType) {
321:                 final URI resourceURI = testResourceURI(resourcePath);
322:                 final URIEditorInput input = new URIEditorInput(resourceURI);
323:
324:                 return open(input, expectedType);
325:         }
326:
327:         <P extends IEditorPart> P open(IEditorInput input, Class<P> expectedType) {
328:                 IEditorPart result = null;
329:
330:                 try {
331:                         result = getActivePage().openEditor(input, "org.eclipse.emf.ecp.ide.editor.view.editor");
332:                 } catch (final PartInitException e) {
333:                         // It's okay if we are expecting it
334:                         result = getEditor(input);
335:                         // assertThat("Unexpected open failure", result, instanceOf(expectedType));
336:                 }
337:
338:                 // assertThat("Unexpected editor type", result, instanceOf(expectedType));
339:                 return expectedType.cast(result);
340:         }
341:
342:         static URI testResourceURI(String resourcePath) {
343:                 return URI.createURI(
344:                         String.format("platform:/plugin/org.eclipse.emf.ecp.ide.editor.view.test/resources/%s", resourcePath),
345:                         true);
346:         }
347:
348:         static IWorkbenchPage getActivePage() {
349:                 return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
350:         }
351:
352:         static IEditorPart getEditor(IEditorInput input) {
353:                 final IEditorReference[] editorReferences = getActivePage().getEditorReferences();
354:•                for (final IEditorReference editorReference : editorReferences) {
355:•                        if (input.equals(safeGetEditorInput(editorReference))) {
356:                                 final IEditorPart editor = editorReference.getEditor(false);
357:•                                if (editor != null) {
358:                                         return editor;
359:                                 }
360:                         }
361:                 }
362:                 return null;
363:         }
364:
365:         static IEditorInput safeGetEditorInput(IEditorReference reference) {
366:                 try {
367:                         return reference.getEditorInput();
368:                 } catch (final PartInitException e) {
369:                         fail("Could not get editor input from editor reference: " + e.getMessage());
370:                         return null; // Unreachable
371:                 }
372:         }
373:
374:         ErrorEditorPart openError(String resourcePath) {
375:                 return open(resourcePath, ErrorEditorPart.class);
376:         }
377:
378:         void assertErrorMessage(ErrorEditorPart editor, String substring) {
379:                 Field statusField = null;
380:                 final Field[] declaredFields = editor.getClass().getDeclaredFields();
381:•                for (final Field field : declaredFields) {
382:•                        if (IStatus.class.isAssignableFrom(field.getType())) {
383:                                 statusField = field;
384:                                 break;
385:                         }
386:                 }
387:                 assertNotNull("Cannot find status field in error editor", statusField);
388:                 statusField.setAccessible(true);
389:
390:                 try {
391:                         final IStatus status = (IStatus) statusField.get(editor);
392:                         assertTrue(status.getMessage().contains(substring));
393:                 } catch (final IllegalAccessException e) {
394:                         fail("Cannot access status field of error editor: " + e.getMessage());
395:                 } catch (final IllegalArgumentException e) {
396:                         fail("Cannot access status field of error editor: " + e.getMessage());
397:                 }
398:         }
399:
400:         Composite getEditorParentComposite(ViewEditorPart editor) {
401:                 Field parentField;
402:                 try {
403:                         parentField = ViewEditorPart.class.getDeclaredField("parent");
404:                         parentField.setAccessible(true);
405:                         final Object parentFieldValue = parentField.get(editor);
406:                         assertTrue(parentFieldValue instanceof Composite);
407:                         return (Composite) parentFieldValue;
408:                 } catch (NoSuchFieldException | SecurityException | IllegalAccessException ex) {
409:                         fail("Could not get parent composite of the view editor: " + ex.getMessage());
410:                         return null; // Never happens
411:                 }
412:         }
413:
414:         void keystroke(Keyboard keyboard, Control control, int modifier, char key) {
415:•                if (Platform.getWS().equals(Platform.WS_COCOA)) {
416:                         SWTTestUtil.pressAndReleaseKey(control, modifier, key);
417:                 } else {
418:                         keyboard.pressShortcut(modifier, key);
419:                 }
420:                 SWTTestUtil.waitForUIThread();
421:         }
422:
423:         void focus(Control control) {
424:                 control.setFocus();
425:                 SWTTestUtil.waitForUIThread();
426:         }
427:
428: }