Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c3b40f3525cd382b3e3115c9d37382bac39cb03f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*****************************************************************************
 * Copyright (c) 2013, 2016, 2019 CEA LIST, Christian W. Damus, and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 433371
 *  Christian W. Damus - bug 485220
 *  Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Bug 550321
 *
 *****************************************************************************/
package org.eclipse.papyrus.editor.integration.tests.tests;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.commands.DestroyElementPapyrusCommand;
import org.eclipse.papyrus.commands.ICreationCommand;
import org.eclipse.papyrus.commands.OpenDiagramCommand;
import org.eclipse.papyrus.editor.integration.tests.Activator;
import org.eclipse.papyrus.infra.architecture.commands.IModelCreationCommand;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.sasheditor.editor.IPage;
import org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.papyrus.infra.core.services.ExtensionServicesRegistry;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.DiResourceSet;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationUtils;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.junit.utils.EditorUtils;
import org.eclipse.papyrus.uml.diagram.clazz.CreateClassDiagramCommand;
import org.eclipse.papyrus.uml.diagram.clazz.UmlClassDiagramForMultiEditor;
import org.eclipse.papyrus.uml.diagram.common.commands.CreateUMLModelCommand;
import org.eclipse.papyrus.uml.diagram.sequence.UmlSequenceDiagramForMultiEditor;
import org.eclipse.papyrus.uml.diagram.timing.custom.UmlTimingDiagramForMultiEditor;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.papyrus.uml.tools.model.UmlUtils;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Model;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

/**
 * Tests the basic PageManager cases, integrated with the CoreMultiDiagramEditor
 * and a few UML Diagrams/Tables
 *
 * @author Camille Letavernier
 *
 */
public class PageManagerTests extends AbstractEditorIntegrationTest {

	public static final String SOURCE_PATH = "/model/basic/";

	@Test
	public void testModelWithDiagramCreation() throws Exception {
		final ModelSet modelSet = new DiResourceSet();
		IProject emptyModelCreationProject = ResourcesPlugin.getWorkspace().getRoot().getProject("diagramModelCreation");
		emptyModelCreationProject.create(new NullProgressMonitor());
		emptyModelCreationProject.open(new NullProgressMonitor());

		final IFile emptyModelDi = emptyModelCreationProject.getFile("diagram_model_creation.di");

		modelSet.createsModels(emptyModelDi);

		IFile emptyModelNotation = emptyModelCreationProject.getFile("diagram_model_creation.notation");
		IFile emptyModelUml = emptyModelCreationProject.getFile("diagram_model_creation.uml");

		ServicesRegistry registry = new ExtensionServicesRegistry(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID);
		try {
			registry.add(ModelSet.class, Integer.MAX_VALUE, modelSet);
			registry.startRegistry();
		} catch (ServiceException ex) {
			// Ignore
		}

		TransactionalEditingDomain ted = modelSet.getTransactionalEditingDomain();
		ted.getCommandStack().execute(new RecordingCommand(ted) {
			@Override
			protected void doExecute() {
				IModelCreationCommand creationCommand = new CreateUMLModelCommand();
				creationCommand.createModel(modelSet);
			}
		});

		// Create the root UML Model
		UmlModel umlModel = (UmlModel) modelSet.getModel(UmlModel.MODEL_ID);
		Model model = (Model) umlModel.lookupRoot();

		// Creates and opens a Papyrus Class Diagram
		ICreationCommand diagramCreationCommand = new CreateClassDiagramCommand();
		diagramCreationCommand.createDiagram(modelSet, model, "TestDiagram1");

		modelSet.save(new NullProgressMonitor());

		Assert.assertTrue("The DI file has not been created", emptyModelDi.exists());
		Assert.assertTrue("The Notation file has not been created", emptyModelNotation.exists());
		Assert.assertTrue("The UML file has not been created", emptyModelUml.exists());

		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				try {
					editor = EditorUtils.openPapyrusEditor(emptyModelDi);
				} catch (Exception ex) {
					Activator.log.error(ex);
					Assert.fail(ex.getMessage());
				}
			}
		});

		Assert.assertNotNull("Cannot open the Papyrus editor", editor);
		Assert.assertTrue("The active editor should be a Papyrus Class Diagram editor", editor.getActiveEditor() instanceof UmlClassDiagramForMultiEditor);

		try {
			registry.disposeRegistry();
		} catch (ServiceException ex) {
			// Ignore
		}
	}

	@Test
	public void testEmptyModelCreation() throws Exception {
		final IFile emptyModelDi = createEmptyModel();
		Assert.assertNotNull(emptyModelDi);

		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				try {
					editor = EditorUtils.openPapyrusEditor(emptyModelDi);
				} catch (Exception ex) {
					Activator.log.error(ex);
					Assert.fail(ex.getMessage());
				}
			}
		});

		Assert.assertNotNull("Cannot open the editor", editor);
		openAndCheckCreatedModel(emptyModelDi);
	}

	private IFile createEmptyModel() throws Exception {
		final ModelSet modelSet = new DiResourceSet();
		IProject emptyModelCreationProject = ResourcesPlugin.getWorkspace().getRoot().getProject("emptyModelCreation");
		emptyModelCreationProject.create(new NullProgressMonitor());
		emptyModelCreationProject.open(new NullProgressMonitor());

		final IFile emptyModelDi = emptyModelCreationProject.getFile("empty_model_creation.di");

		modelSet.createsModels(emptyModelDi);

		// create the UML model
		TransactionalEditingDomain ted = modelSet.getTransactionalEditingDomain();
		ted.getCommandStack().execute(new RecordingCommand(ted) {
			@Override
			protected void doExecute() {
				IModelCreationCommand creationCommand = new CreateUMLModelCommand();
				creationCommand.createModel(modelSet);
			}
		});


		modelSet.save(new NullProgressMonitor());

		IFile emptyModelNotation = emptyModelCreationProject.getFile("empty_model_creation.notation");
		IFile emptyModelUml = emptyModelCreationProject.getFile("empty_model_creation.uml");

		Assert.assertTrue("The DI file has not been created", emptyModelDi.exists());
		Assert.assertTrue("The Notation file has not been created", emptyModelNotation.exists());
		Assert.assertTrue("The UML file has not been created", emptyModelUml.exists());
		return emptyModelDi;
	}

	private void openAndCheckCreatedModel(IFile emptyModelDi) throws Exception {
		IPageManager pageManager = editor.getServicesRegistry().getService(IPageManager.class);
		Assert.assertNotNull("Cannot retrieve the PageManager", pageManager);
		Assert.assertTrue(pageManager.allPages().isEmpty());

		ModelSet modelSet = editor.getServicesRegistry().getService(ModelSet.class);
		Assert.assertNotNull("Cannot retrieve the ModelSet", modelSet);

		UmlModel umlModel = (UmlModel) modelSet.getModel(UmlModel.MODEL_ID);
		Assert.assertNotNull("Cannot retrieve the UML Model", umlModel);

		EObject rootEObject = umlModel.lookupRoot();
		Assert.assertTrue("The rootElement is not a model", rootEObject instanceof Model);
	}

	@Test
	public void testOpenEmptyModel() throws Exception {
		initModel("openEmptyModel", "empty_model", getBundle());
		IPageManager pageManager = editor.getServicesRegistry().getService(IPageManager.class);
		Assert.assertEquals(0, pageManager.allPages().size());
	}

	@Test
	public void testAvailableAndActivePage() throws Exception {
		initModel("availableAndActivePage", "two_diagrams_model", getBundle());
		IPageManager pageManager = editor.getServicesRegistry().getService(IPageManager.class);
		Assert.assertEquals(2, pageManager.allPages().size());

		Assert.assertTrue(editor.getActiveEditor() instanceof UmlClassDiagramForMultiEditor); // TODO: The PageManager doesn't provide the getActivePage() method

		Resource notationResource = NotationUtils.getNotationModel(editor.getServicesRegistry().getService(ModelSet.class)).getResource();

		Diagram classDiagram = (Diagram) notationResource.getContents().get(0);
		Diagram timingDiagram = (Diagram) notationResource.getContents().get(1);

		TransactionalEditingDomain editingDomain = editor.getServicesRegistry().getService(TransactionalEditingDomain.class);
		editingDomain.getCommandStack().execute(new GMFtoEMFCommandWrapper(new OpenDiagramCommand(editingDomain, timingDiagram)));
		Assert.assertTrue(editor.getActiveEditor() instanceof UmlTimingDiagramForMultiEditor);


		// Check type requested by selectPage
		ISashWindowsContainer container = editor.getServicesRegistry().getService(ISashWindowsContainer.class);
		IPage selectedPage = container.getActiveSashWindowsPage();

		// FIXME: Does not work yet. See Bug 401107: [IPageManager] selectPage does not work
		// TODO: There should be a specific test for selectPage, with a basic and a complex model
		pageManager.selectPage(classDiagram);
		Assert.assertTrue("Couldn't change the page selection", editor.getActiveEditor() instanceof UmlClassDiagramForMultiEditor);

		pageManager.selectPage(timingDiagram);
		Assert.assertTrue("Couldn't change the page selection", editor.getActiveEditor() instanceof UmlTimingDiagramForMultiEditor);
	}

	/**
	 * @author vincent lorenzo
	 * @throws Exception
	 */
	@Test
	@Ignore("new papyrus table are not yet in the main build")
	public void testAvailableAndActivePageWithNatTable() throws Exception {
		// initModel("availableAndActivePage2", "two_tables_model");
		// IPageManager pageManager = editor.getServicesRegistry().getService(IPageManager.class);
		// Assert.assertEquals(2, pageManager.allPages().size());
		// IEditorPart tableEditor = editor.getActiveEditor();
		// Assert.assertTrue(tableEditor instanceof NatTableEditor); //TODO: The PageManager doesn't provide the getActivePage() method
		// INattableModelManager manager = (INattableModelManager)tableEditor.getAdapter(INattableModelManager.class);
		// Assert.assertNotNull(manager);
		// Assert.assertEquals("PapyrusGenericTable", manager.getTable().getTableConfiguration().getType());
		//
		// Resource notationResource = NotationUtils.getNotationModel(editor.getServicesRegistry().getService(ModelSet.class)).getResource();
		//
		// Table genericTable = (Table)notationResource.getContents().get(0);
		// Table tableOfViews = (Table)notationResource.getContents().get(1);
		//
		// TransactionalEditingDomain editingDomain = editor.getServicesRegistry().getService(TransactionalEditingDomain.class);
		// editingDomain.getCommandStack().execute(new GMFtoEMFCommandWrapper(new OpenDiagramCommand(editingDomain, tableOfViews)));
		// tableEditor = editor.getActiveEditor();
		// Assert.assertTrue(tableEditor instanceof NatTableEditor);
		// manager = (INattableModelManager)tableEditor.getAdapter(INattableModelManager.class);
		// Assert.assertNotNull(manager);
		// Assert.assertEquals("PapyrusViewsTable", manager.getTable().getTableConfiguration().getType());
		//
		// //FIXME: Does not work yet. See Bug 401107: [IPageManager] selectPage does not work
		// //TODO: There should be a specific test for selectPage, with a basic and a complex model
		// pageManager.selectPage(genericTable);
		// tableEditor = editor.getActiveEditor();
		// Assert.assertTrue(tableEditor instanceof NatTableEditor); //TODO: The PageManager doesn't provide the getActivePage() method
		// manager = (INattableModelManager)tableEditor.getAdapter(INattableModelManager.class);
		// Assert.assertNotNull(manager);
		// Assert.assertEquals("Couldn't change the page selection", "PapyrusGenericTable", manager.getTable().getTableConfiguration().getType());
		//
		//
		// pageManager.selectPage(tableOfViews);
		// tableEditor = editor.getActiveEditor();
		// Assert.assertTrue(tableEditor instanceof NatTableEditor); //TODO: The PageManager doesn't provide the getActivePage() method
		// manager = (INattableModelManager)tableEditor.getAdapter(INattableModelManager.class);
		// Assert.assertNotNull(manager);
		// Assert.assertEquals("Couldn't change the page selection", "PapyrusViewsTable", manager.getTable().getTableConfiguration().getType());
	}

	@Test
	public void testDiagramCreation() throws Exception {
		initModel("diagramCreation", "empty_model", getBundle());
		ICreationCommand diagramCreationCommand = new CreateClassDiagramCommand();
		ModelSet modelSet = editor.getServicesRegistry().getService(ModelSet.class);

		TransactionalEditingDomain editingDomain = modelSet.getTransactionalEditingDomain();

		CompositeCommand createAndOpenDiagram = new CompositeCommand("Create diagram");
		ICommand creationCommand = diagramCreationCommand.getCreateDiagramCommand(modelSet, null, "DiagramCreationTestDiagram");
		createAndOpenDiagram.add(creationCommand);
		createAndOpenDiagram.add(new OpenDiagramCommand(editingDomain, creationCommand));

		testPageCreation(createAndOpenDiagram, UmlClassDiagramForMultiEditor.class);
	}

	@Test
	public void testDiagramDeletion() throws Exception {
		initModel("diagramDeletion", "simple_class_model", getBundle());
		ModelSet modelSet = getModelSet();
		final Diagram diagram = (Diagram) NotationUtils.getNotationModel(modelSet).getResource().getContents().get(0);
		testPageDeletion(diagram, UmlClassDiagramForMultiEditor.class);
	}

	@Test
	public void testDiagramIndirectDeletion_bug433371() throws Exception {
		initModel("bug433371", "delete_sash_page", getBundle());
		ModelSet modelSet = getModelSet();
		final Diagram diagram = (Diagram) NotationUtils.getNotationModel(modelSet).getResource().getContents().get(1);

		IPageManager pageManager = editor.getServicesRegistry().getService(IPageManager.class);
		pageManager.selectPage(diagram); // Make sure the one we want deleted is active

		testPageDeletion(diagram.getElement(), diagram, UmlSequenceDiagramForMultiEditor.class);
	}

	/**
	 * @author vincent lorenzo
	 * @throws Exception
	 */
	@Test
	@Ignore("new papyrus table are not yet in the main build")
	public void testNatTableDeletion() throws Exception {
		// initModel("tableDeletion", "simple_uml_generic_table_model");
		// ModelSet modelSet = getModelSet();
		// final Table diagram = (Table)NotationUtils.getNotationModel(modelSet).getResource().getContents().get(0);
		// testPageDeletion(diagram, NatTableEditor.class);
	}

	// Does not work. The table creation produces two executable commands (Create Table Editor and Identity)
	// Only Identity (which does nothing) is undoable. Undoing the table creation does nothing
	// This is an EMF Facet Table bug, which tries to delete columns for an empty table as soon as the table is created
	@Test
	public void testTableCreation() throws Exception {
		initModel("tableCreation", "empty_model", getBundle());

		final ServicesRegistry registry = editor.getServicesRegistry();
		ModelSet modelSet = registry.getService(ModelSet.class);
		TransactionalEditingDomain editingDomain = modelSet.getTransactionalEditingDomain();

		final EObject tableContext = UmlUtils.getUmlModel(modelSet).getResource().getContents().get(0);


	}


	private void testPageCreation(ICommand creationCommand, Class<?> expectedEditorClass) throws Exception {
		IPageManager pageManager = editor.getServicesRegistry().getService(IPageManager.class);

		// Check initial state
		Assert.assertTrue(pageManager.allPages().isEmpty());
		Assert.assertNull(editor.getActiveEditor());

		EditingDomain editingDomain = editor.getServicesRegistry().getService(EditingDomain.class);

		editingDomain.getCommandStack().execute(new GMFtoEMFCommandWrapper(creationCommand));

		for (int i = 0; i < 3; i++) { // Undo/Redo 3 times
			Assert.assertEquals("The page has not been correctly created", 1, pageManager.allPages().size());
			Assert.assertTrue("The editor has not been correctly opened", expectedEditorClass.isInstance(editor.getActiveEditor()));

			editingDomain.getCommandStack().undo();

			Assert.assertNull("The editor should be closed", editor.getActiveEditor());
			Assert.assertTrue("The page has not been correctly deleted", pageManager.allPages().isEmpty());

			editingDomain.getCommandStack().redo();
		}
	}

	private void testPageDeletion(final EObject page, Class<?> expectedEditorClass) throws Exception {
		final IPageManager pageManager = editor.getServicesRegistry().getService(IPageManager.class);

		// Check initial state
		Assert.assertFalse(pageManager.allPages().isEmpty());
		int initialPagesSize = pageManager.allPages().size();

		Assert.assertTrue(expectedEditorClass.isInstance(editor.getActiveEditor()));

		TransactionalEditingDomain editingDomain = editor.getServicesRegistry().getService(TransactionalEditingDomain.class);

		pageManager.closeAllOpenedPages(page);
		final Command destroyCommand = new GMFtoEMFCommandWrapper(new DestroyElementPapyrusCommand(new DestroyElementRequest(page, false)));

		editingDomain.getCommandStack().execute(destroyCommand);

		for (int i = 0; i < 3; i++) { // Undo/Redo 3 times
			Assert.assertNull("The editor should be closed", editor.getActiveEditor());
			Assert.assertEquals("The page has not been correctly deleted", initialPagesSize - 1, pageManager.allPages().size());

			editingDomain.getCommandStack().undo();
			pageManager.openPage(page);

			Assert.assertEquals("The has not been correctly created", initialPagesSize, pageManager.allPages().size());
			Assert.assertTrue("The editor has not been correctly opened", expectedEditorClass.isInstance(editor.getActiveEditor()));

			editingDomain.getCommandStack().redo();
			pageManager.closeAllOpenedPages(page);
		}
	}

	private void testPageDeletion(final EObject elementToDelete, final Object dependentPage, Class<?> expectedEditorClass) throws Exception {
		final IPageManager pageManager = editor.getServicesRegistry().getService(IPageManager.class);

		// Check initial state
		assertThat("Page not open", pageManager.allPages(), hasItem(dependentPage));

		assertThat("Wrong kind of page", editor.getActiveEditor(), instanceOf(expectedEditorClass));

		TransactionalEditingDomain editingDomain = editor.getServicesRegistry().getService(TransactionalEditingDomain.class);

		Element clazz = getRootUMLModel().getOwnedType("Class1");
		Behavior toDelete = ((BehavioredClassifier) clazz).getOwnedBehaviors().get(0);
		IElementEditService edit = ElementEditServiceUtils.getCommandProvider(clazz);
		ICommand command = edit.getEditCommand(new DestroyElementRequest(toDelete, false));

		editingDomain.getCommandStack().execute(GMFtoEMFCommandWrapper.wrap(command));

		for (int i = 0; i < 3; i++) { // Undo/Redo 3 times
			assertThat("The editor page should be closed", pageManager.allPages(), not(hasItem(dependentPage)));

			editingDomain.getCommandStack().undo();

			assertThat("The page has not been correctly restored", pageManager.allPages(), hasItem(dependentPage));

			editingDomain.getCommandStack().redo();
		}
	}

	@Test
	public void testContainedDiagramDeletion() throws Exception {
		initModel("deleteContainedDiagrams", "delete_contained_diagram", getBundle());
		ModelSet modelSet = getModelSet();
		IPageManager pageManager = getPageManager();
		TransactionalEditingDomain editingDomain = getTransactionalEditingDomain();

		final int initialSize = pageManager.allPages().size();
		Model model = (Model) UmlUtils.getUmlModel(modelSet).getResource().getContents().get(0);

		IElementEditService provider;

		Element firstRootElement = model.getOwnedElements().get(0);
		Element secondRootElement = model.getOwnedElements().get(1);

		{
			DestroyElementRequest destroyFirstElementRequest = new DestroyElementRequest(firstRootElement, false);
			provider = ElementEditServiceUtils.getCommandProvider(firstRootElement);
			ICommand destroyFirstElementCommand = provider.getEditCommand(destroyFirstElementRequest);

			editingDomain.getCommandStack().execute(new GMFtoEMFCommandWrapper(destroyFirstElementCommand));
			Assert.assertEquals("The page should have been destroyed", initialSize - 1, pageManager.allPages().size());
			// TODO: Test the opened pages too
		}

		{
			DestroyElementRequest destroySecondElementRequest = new DestroyElementRequest(secondRootElement, false);
			provider = ElementEditServiceUtils.getCommandProvider(secondRootElement);
			ICommand destroySecondElementCommand = provider.getEditCommand(destroySecondElementRequest);

			editingDomain.getCommandStack().execute(new GMFtoEMFCommandWrapper(destroySecondElementCommand));
			Assert.assertTrue("All remaining pages should have been destroyed", pageManager.allPages().isEmpty());
		}

		editingDomain.getCommandStack().undo();

		Assert.assertEquals("The deleted pages should have been restored", initialSize - 1, pageManager.allPages().size());

		editingDomain.getCommandStack().undo();

		Assert.assertEquals("All pages should have been restored", initialSize, pageManager.allPages().size());

		editingDomain.getCommandStack().redo();
		editingDomain.getCommandStack().redo();

		Assert.assertTrue("All remaining pages should have been destroyed", pageManager.allPages().isEmpty());
	}

	/**
	 * @author vincent lorenzo
	 * @throws Exception
	 */
	@Test
	public void testContainedNatTableDeletion() throws Exception {
		initModel("deleteContainedNatTable", "delete_contained_table", getBundle());
		ModelSet modelSet = getModelSet();
		IPageManager pageManager = getPageManager();
		TransactionalEditingDomain editingDomain = getTransactionalEditingDomain();

		final int initialSize = pageManager.allPages().size();
		Model model = (Model) UmlUtils.getUmlModel(modelSet).lookupRoot();

		IElementEditService provider;

		Element firstRootElement = model.getOwnedElements().get(0);
		Element secondRootElement = model.getOwnedElements().get(1);

		{
			DestroyElementRequest destroyFirstElementRequest = new DestroyElementRequest(firstRootElement, false);
			provider = ElementEditServiceUtils.getCommandProvider(firstRootElement);
			ICommand destroyFirstElementCommand = provider.getEditCommand(destroyFirstElementRequest);

			editingDomain.getCommandStack().execute(new GMFtoEMFCommandWrapper(destroyFirstElementCommand));
			Assert.assertEquals("The page should have been destroyed", initialSize - 1, pageManager.allPages().size());
			// TODO: Test the opened pages too
		}

		{
			DestroyElementRequest destroySecondElementRequest = new DestroyElementRequest(secondRootElement, false);
			provider = ElementEditServiceUtils.getCommandProvider(secondRootElement);
			ICommand destroySecondElementCommand = provider.getEditCommand(destroySecondElementRequest);

			editingDomain.getCommandStack().execute(new GMFtoEMFCommandWrapper(destroySecondElementCommand));
			Assert.assertTrue("All remaining pages should have been destroyed", pageManager.allPages().isEmpty());
		}

		editingDomain.getCommandStack().undo();

		Assert.assertEquals("The deleted pages should have been restored", initialSize - 1, pageManager.allPages().size());

		editingDomain.getCommandStack().undo();

		Assert.assertEquals("All pages should have been restored", initialSize, pageManager.allPages().size());

		editingDomain.getCommandStack().redo();
		editingDomain.getCommandStack().redo();

		Assert.assertTrue("All remaining pages should have been destroyed", pageManager.allPages().isEmpty());
	}

	@Ignore("Unsupported yet")
	@Test
	public void testContainedTableDeletion() throws Exception {
		throw new UnsupportedOperationException("Not implemented yet");
	}

	@Test
	public void testMultipageEditor() throws Exception {
		initModel("multipageEditor", "complex_multipage_model", getBundle());
	}

	@Test
	public void testOpenModel() throws Exception {
		initModel("openModel", "complex_multipage_model", getBundle());
	}

	@Override
	protected String getSourcePath() {
		return SOURCE_PATH;
	}

}

Back to the top