Skip to content

Package: EcoreEditorFullRender_PTest

EcoreEditorFullRender_PTest

nameinstructionbranchcomplexitylinemethod
EcoreEditorFullRender_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%
closeAllEditors()
M: 0 C: 6
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%
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%
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%
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%
shortcuts_detail_noDelete()
M: 0 C: 83
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
shortcuts_tree_delete()
M: 0 C: 75
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 18
100%
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 Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.spi.editor;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertTrue;
18: import static org.junit.Assert.fail;
19:
20: import org.eclipse.emf.common.ui.URIEditorInput;
21: import org.eclipse.emf.common.util.URI;
22: import org.eclipse.emf.ecore.EPackage;
23: import org.eclipse.emf.ecore.resource.Resource;
24: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
25: import org.eclipse.emfforms.internal.editor.ecore.EcoreEditor;
26: import org.eclipse.swt.SWT;
27: import org.eclipse.swt.widgets.Composite;
28: import org.eclipse.swt.widgets.Tree;
29: import org.eclipse.swt.widgets.TreeItem;
30: import org.eclipse.swtbot.swt.finder.SWTBot;
31: import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
32: import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
33: import org.eclipse.ui.IEditorInput;
34: import org.eclipse.ui.IEditorPart;
35: import org.eclipse.ui.IEditorReference;
36: import org.eclipse.ui.IWorkbenchPage;
37: import org.eclipse.ui.PartInitException;
38: import org.eclipse.ui.PlatformUI;
39: import org.junit.After;
40: import org.junit.Test;
41: import org.junit.runner.RunWith;
42:
43: /**
44: * Tests for the {@link EcoreEditor}.
45: * This plugin is a fragment of the <b>org.eclipse.emfforms.editor</b> bundle, containing the {@link GenericEditor}
46: * class, although we are testing content from the <b>org.eclipse.emfforms.editor.ecore</b> bundle. The reason for
47: * this is that we need to call protected methods from the {@link GenericEditor}, which would not be possible otherwise.
48: * <p>
49: * In contrast to {@link EcoreEditor_PTest}, these tests fully render the editor without mocking parts of it or the IDE
50: * environment.
51: *
52: * @author Lucas Koehler
53: *
54: */
55: @SuppressWarnings("restriction")
56: @RunWith(SWTBotJunit4ClassRunner.class)
57: public class EcoreEditorFullRender_PTest {
58:
59:         @After
60:         public void closeAllEditors() {
61:                 assertTrue(getActivePage().closeAllEditors(false));
62:                 SWTTestUtil.waitForUIThread();
63:         }
64:
65:         @Test
66:         public void shortcuts_tree_delete() {
67:                 final EcoreEditor editor = open("shortcut_delete.ecore", EcoreEditor.class); //$NON-NLS-1$
68:                 SWTTestUtil.waitForUIThread();
69:                 final Composite parent = editor.getRootView();
70:                 final Tree tree = SWTTestUtil.findControl(parent, 0, Tree.class);
71:                 final TreeItem packageItem = tree.getItem(0).getItem(0);
72:                 final TreeItem toDeleteItem = packageItem.getItem(0);
73:                 final SWTBot bot = new SWTBot(parent);
74:
75:                 SWTTestUtil.selectTreeItem(toDeleteItem);
76:                 SWTTestUtil.waitForUIThread();
77:                 bot.tree().pressShortcut(Keystrokes.DELETE);
78:                 SWTTestUtil.waitForUIThread();
79:                 tree.update();
80:                 SWTTestUtil.waitForUIThread();
81:
82:                 assertEquals("EPackage tree node children after delete", 0, packageItem.getItemCount()); //$NON-NLS-1$
83:                 final Resource ecoreResource = editor.getResourceSet().getResources().get(0);
84:                 final EPackage ePackage = (EPackage) ecoreResource.getContents().get(0);
85:                 assertEquals("EPackage children after delete", 0, ePackage.getEClassifiers().size()); //$NON-NLS-1$
86:         }
87:
88:         /** Expectation: When the focus is on the detail view, no tree node must be deleted if the DEL key is pressed. */
89:         @Test
90:         public void shortcuts_detail_noDelete() {
91:                 final EcoreEditor editor = open("shortcut_delete.ecore", EcoreEditor.class); //$NON-NLS-1$
92:                 SWTTestUtil.waitForUIThread();
93:                 final Composite parent = editor.getRootView();
94:                 final Tree tree = SWTTestUtil.findControl(parent, 0, Tree.class);
95:                 final TreeItem packageItem = tree.getItem(0).getItem(0);
96:                 final TreeItem toNotDeleteItem = packageItem.getItem(0);
97:                 final SWTBot bot = new SWTBot(parent);
98:
99:                 tree.setFocus();
100:                 SWTTestUtil.selectTreeItem(toNotDeleteItem);
101:                 SWTTestUtil.waitForUIThread();
102:                 // Make sure the detail is re-rendered
103:                 SWTTestUtil.pressAndReleaseKey(tree, SWT.LF);
104:                 parent.update();
105:                 SWTTestUtil.waitForUIThread();
106:
107:                 bot.checkBox().pressShortcut(Keystrokes.DELETE);
108:                 tree.update();
109:                 SWTTestUtil.waitForUIThread();
110:
111:                 assertEquals("EPackage tree node children after pressing DEL in the detail", 1, packageItem.getItemCount()); //$NON-NLS-1$
112:                 final Resource ecoreResource = editor.getResourceSet().getResources().get(0);
113:                 final EPackage ePackage = (EPackage) ecoreResource.getContents().get(0);
114:                 assertEquals("EPackage children after pressing DEL in the detail", 1, ePackage.getEClassifiers().size()); //$NON-NLS-1$
115:         }
116:
117:         // Test Infrastructure to open editors
118:
119:         <P extends IEditorPart> P open(String resourcePath, Class<P> expectedType) {
120:                 final URI resourceURI = testResourceURI(resourcePath);
121:                 final URIEditorInput input = new URIEditorInput(resourceURI);
122:
123:                 return open(input, expectedType);
124:         }
125:
126:         <P extends IEditorPart> P open(IEditorInput input, Class<P> expectedType) {
127:                 IEditorPart result = null;
128:
129:                 try {
130:                         result = getActivePage().openEditor(input, "EcoreEditor"); //$NON-NLS-1$
131:                 } catch (final PartInitException e) {
132:                         // It's okay if we are expecting it
133:                         result = getEditor(input);
134:                 }
135:
136:                 return expectedType.cast(result);
137:         }
138:
139:         static URI testResourceURI(String resourcePath) {
140:                 return URI.createURI(
141:                         String.format("platform:/fragment/org.eclipse.emfforms.editor.ecore.test/data/%s", resourcePath), //$NON-NLS-1$
142:                         true);
143:         }
144:
145:         static IWorkbenchPage getActivePage() {
146:                 return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
147:         }
148:
149:         static IEditorPart getEditor(IEditorInput input) {
150:                 final IEditorReference[] editorReferences = getActivePage().getEditorReferences();
151:•                for (final IEditorReference editorReference : editorReferences) {
152:•                        if (input.equals(safeGetEditorInput(editorReference))) {
153:                                 final IEditorPart editor = editorReference.getEditor(false);
154:•                                if (editor != null) {
155:                                         return editor;
156:                                 }
157:                         }
158:                 }
159:                 return null;
160:         }
161:
162:         static IEditorInput safeGetEditorInput(IEditorReference reference) {
163:                 try {
164:                         return reference.getEditorInput();
165:                 } catch (final PartInitException e) {
166:                         fail("Could not get editor input from editor reference: " + e.getMessage()); //$NON-NLS-1$
167:                         return null; // Unreachable
168:                 }
169:         }
170: }