Skip to content

Package: ECPCommonSWTBotTest$TestRunnable$2

ECPCommonSWTBotTest$TestRunnable$2

nameinstructionbranchcomplexitylinemethod
run()
M: 16 C: 62
79%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 7 C: 12
63%
M: 0 C: 1
100%
{...}
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%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 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: * Edgar Mueller - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.ui.editor.test;
15:
16: import org.eclipse.core.databinding.observable.Realm;
17: import org.eclipse.emf.ecore.EObject;
18: import org.eclipse.emf.ecp.ui.view.ECPRendererException;
19: import org.eclipse.emf.ecp.ui.view.swt.DefaultReferenceService;
20: import org.eclipse.emf.ecp.ui.view.swt.ECPSWTView;
21: import org.eclipse.emf.ecp.ui.view.swt.ECPSWTViewRenderer;
22: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
23: import org.eclipse.emf.ecp.view.spi.context.ViewModelContextFactory;
24: import org.eclipse.emf.ecp.view.spi.model.VView;
25: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
26: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
27: import org.eclipse.emf.ecp.view.test.common.spi.GCCollectable;
28: import org.eclipse.jface.databinding.swt.DisplayRealm;
29: import org.eclipse.jface.layout.GridLayoutFactory;
30: import org.eclipse.swt.SWT;
31: import org.eclipse.swt.layout.GridData;
32: import org.eclipse.swt.widgets.Composite;
33: import org.eclipse.swt.widgets.Display;
34: import org.eclipse.swt.widgets.Shell;
35: import org.eclipse.swtbot.swt.finder.SWTBotTestCase;
36: import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
37: import org.eclipse.swtbot.swt.finder.results.Result;
38: import org.eclipse.swtbot.swt.finder.results.VoidResult;
39: import org.junit.Before;
40: import org.junit.Test;
41:
42: /**
43: * Common base class for SWTBot tests.
44: *
45: * @author emueller
46: * @author jfaltermeier
47: *
48: */
49: public abstract class ECPCommonSWTBotTest extends SWTBotTestCase {
50:
51:         private static Shell shell;
52:         private Display display;
53:         private GCCollectable swtViewCollectable;
54:         private EObject domainObject;
55:
56:         @Override
57:         @Before
58:         public void setUp() {
59:                 display = Display.getDefault();
60:                 shell = UIThreadRunnable.syncExec(display, new Result<Shell>() {
61:                         @Override
62:                         public Shell run() {
63:                                 final Shell shell = new Shell(display);
64:                                 GridLayoutFactory.fillDefaults().applyTo(shell);
65:                                 return shell;
66:                         }
67:                 });
68:         }
69:
70:         @Test
71:         public void test() throws ECPRendererException,
72:                 InterruptedException {
73:                 Realm.runWithDefault(DisplayRealm.getRealm(display), new TestRunnable());
74:         }
75:
76:         public abstract EObject createDomainObject();
77:
78:         public abstract VView createView();
79:
80:         public abstract void logic();
81:
82:         public GCCollectable getSWTViewCollectable() {
83:                 return swtViewCollectable;
84:         }
85:
86:         public void unsetSWTViewCollectable() {
87:                 swtViewCollectable = null;
88:         }
89:
90:         public void unsetDomainObject() {
91:                 domainObject = null;
92:         }
93:
94:         /**
95:          * Can be overridden to add assertions at end of execution.
96:          */
97:         public void assertions(double memBefore, double memAfter) {
98:         }
99:
100:         public EObject getDomainObject() {
101:                 return domainObject;
102:         }
103:
104:         public void setDomainObject(EObject eObject) {
105:                 domainObject = eObject;
106:         }
107:
108:         private class TestRunnable implements Runnable {
109:
110:                 private double memBefore;
111:                 private double memAfter;
112:
113:                 @Override
114:                 public void run() {
115:                         try {
116:                                 UIThreadRunnable.syncExec(new Result<Void>() {
117:
118:                                         @Override
119:                                         public Void run() {
120:                                                 try {
121:                                                         final EObject domainObject = createDomainObject();
122:                                                         memBefore = usedMemory();
123:
124:                                                         final ViewModelContext viewContext = ViewModelContextFactory.INSTANCE
125:                                                                 .createViewModelContext(createView(), domainObject, new DefaultReferenceService());
126:                                                         final ECPSWTView swtView = ECPSWTViewRenderer.INSTANCE.render(shell, viewContext);
127:                                                         swtViewCollectable = new GCCollectable(swtView);
128:                                                         final Composite composite = (Composite) swtView.getSWTControl();
129:                                                         final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
130:                                                         composite.setLayoutData(gridData);
131:
132:                                                         shell.open();
133:                                                         shell.pack();
134:                                                         return null;
135:                                                 } catch (final NoRendererFoundException e) {
136:                                                         fail(e.getMessage());
137:                                                 } catch (final NoPropertyDescriptorFoundExeption e) {
138:                                                         fail(e.getMessage());
139:                                                 } catch (final ECPRendererException ex) {
140:                                                         fail(ex.getMessage());
141:                                                 }
142:                                                 return null;
143:                                         }
144:                                 });
145:                                 logic();
146:                         } finally {
147:                                 UIThreadRunnable.syncExec(new VoidResult() {
148:                                         @Override
149:                                         public void run() {
150:                                                 shell.close();
151:                                                 shell.dispose();
152:                                                 memAfter = usedMemory();
153:                                         }
154:                                 });
155:                                 assertions(memBefore, memAfter);
156:                         }
157:                 }
158:
159:         }
160:
161:         public double usedMemory() {
162:                 Runtime.getRuntime().gc();
163:                 return 0d + Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
164:         }
165:
166: }