diff --git a/sirius/plugins/org.polarsys.kitalpha.sirius.rotativeimage/META-INF/MANIFEST.MF b/sirius/plugins/org.polarsys.kitalpha.sirius.rotativeimage/META-INF/MANIFEST.MF index 7e8286bfc..f16513457 100644 --- a/sirius/plugins/org.polarsys.kitalpha.sirius.rotativeimage/META-INF/MANIFEST.MF +++ b/sirius/plugins/org.polarsys.kitalpha.sirius.rotativeimage/META-INF/MANIFEST.MF @@ -14,7 +14,10 @@ Require-Bundle: org.eclipse.ui, org.eclipse.sirius.common.ui, org.eclipse.sirius.diagram, org.eclipse.sirius.diagram.ui, - org.eclipse.osgi + org.eclipse.osgi, + org.apache.batik.dom;bundle-version="1.17.0", + org.apache.batik.bridge;bundle-version="1.17.0", + org.apache.batik.transcoder;bundle-version="1.17.0" Bundle-RequiredExecutionEnvironment: JavaSE-17 Eclipse-LazyStart: true Export-Package: org.polarsys.kitalpha.sirius.rotativeimage, diff --git a/sirius/plugins/org.polarsys.kitalpha.sirius.rotativeimage/src/org/polarsys/kitalpha/sirius/rotativeimage/internal/helpers/RotativeWorkspaceImageHelper.java b/sirius/plugins/org.polarsys.kitalpha.sirius.rotativeimage/src/org/polarsys/kitalpha/sirius/rotativeimage/internal/helpers/RotativeWorkspaceImageHelper.java index 76842407b..e991b4084 100644 --- a/sirius/plugins/org.polarsys.kitalpha.sirius.rotativeimage/src/org/polarsys/kitalpha/sirius/rotativeimage/internal/helpers/RotativeWorkspaceImageHelper.java +++ b/sirius/plugins/org.polarsys.kitalpha.sirius.rotativeimage/src/org/polarsys/kitalpha/sirius/rotativeimage/internal/helpers/RotativeWorkspaceImageHelper.java @@ -1,230 +1,230 @@ -/******************************************************************************* - * Copyright (c) 2019, 2021 THALES GLOBAL SERVICES. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Thales - initial API and implementation - *******************************************************************************/ -package org.polarsys.kitalpha.sirius.rotativeimage.internal.helpers; - -import java.io.File; -import java.util.HashMap; - -import org.eclipse.core.runtime.Path; -import org.eclipse.draw2d.PositionConstants; -import org.eclipse.draw2d.geometry.Rectangle; -import org.eclipse.jface.resource.ImageRegistry; -import org.eclipse.sirius.common.tools.api.resource.FileProvider; -import org.eclipse.sirius.diagram.ui.tools.api.figure.WorkspaceImageFigure; -import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.ImageData; -import org.polarsys.kitalpha.sirius.rotativeimage.Activator; - -/** - * An utility class to create rotated images and access them. It stores images in the ImageRegistry of this plugin - */ -public class RotativeWorkspaceImageHelper { - - private static HashMap IMAGE_PATHS_CACHE = new HashMap<>(); - - private RotativeWorkspaceImageHelper() { - throw new IllegalStateException("Utility class"); - } - - /** - * Return an image from the given path. - * - * @param path - * {@link org.eclipse.sirius.diagram.WorkspaceImage#getWorkspacePath Workspace Path} - * @param orientation - * one of PositionConstants.WEST, PositionConstants.EAST, PositionConstants.SOUTH, - * PositionConstants.NORTH - * @return - */ - public static Image getImage(String path, int orientation) { - String key = getKey(path, orientation); - - ImageRegistry registry = Activator.getDefault().getImageRegistry(); - Image image = registry.get(key); - if (image != null) { - return image; - } - - switch (orientation) { - case PositionConstants.SOUTH: - case PositionConstants.WEST: - case PositionConstants.EAST: - image = rotate(getImage(path, PositionConstants.NORTH), orientation); - break; - default: - image = WorkspaceImageFigure.getImageInstanceFromPath(path); - } - - if (image != null) { - registry.put(key, image); - } - return image; - } - - public static Rectangle rotateRectangle(Rectangle rect, int orientation) { - switch (orientation) { - case PositionConstants.WEST: - case PositionConstants.EAST: - return rect.transpose(); - default: - return rect; - } - } - - public static String get4ImagesDocumentKey(String path, int orientation) { - String result = IMAGE_PATHS_CACHE.get(path+orientation); - if (result == null) { - result = getImageURI(getOrientedPath(path, orientation)); - if (result == null) { - // Default to original file path - result = getImageURI(path); - } - IMAGE_PATHS_CACHE.put(path+orientation, result); - } - return result; - } - - /** - * Inserts {@code orientation} as "_top", "_bottom", "_left", "_right" between file name in {@code path} and file - * extension. - * - * @param path - * @param orientation - * one of PositionConstants.WEST, PositionConstants.EAST, PositionConstants.SOUTH, - * PositionConstants.NORTH - * @return {@code path} with inserted {@code orientation}. If {@code orientation} is not among expected values then - * {@code path} is kepts as is. - */ - public static String getOrientedPath(String path, int orientation) { - int extentionPosition = path.lastIndexOf("."); - String baseName = path.substring(0, extentionPosition); - String ext = path.substring(extentionPosition); - switch (orientation) { - case PositionConstants.NORTH: - baseName += "_top"; - break; - case PositionConstants.SOUTH: - baseName += "_bottom"; - break; - case PositionConstants.WEST: - baseName += "_left"; - break; - case PositionConstants.EAST: - baseName += "_right"; - break; - default: - // Do nothing to default to baseName - } - return baseName + ext; - } - - private static String getImageURI(String basepath) { - final File imageFile = FileProvider.getDefault().getFile(new Path(basepath)); - if (imageFile != null) { - return imageFile.toURI().toString(); - } - return null; - } - - public static void createImage(String mainPath, int orientation) { - String key = getKey(mainPath, orientation); - String orientedPath = getOrientedPath(mainPath, orientation); - ImageRegistry registry = Activator.getDefault().getImageRegistry(); - Image image = registry.get(key); - if (image == null) { - image = WorkspaceImageFigure.flyWeightImage(orientedPath); - registry.put(key, image); - } - } - - /** - * Rotate an image - * - * @param image - * the source - * @param orientation - * PositionConstants.WEST will rotate by 90 degrees on the left, PositionConstants.EAST will rotate by 90 - * degrees on the right, PositionConstants.SOUTH will rotate by 180 degrees. - * @return the rotated image - */ - public static Image rotate(Image image, int orientation) { - if (image == null) { - // We don't want to rotate a null image - return null; - } - ImageData srcData = image.getImageData(); - int bytesPerPixel = srcData.bytesPerLine / srcData.width; - byte[] newData = new byte[srcData.data.length]; - - boolean isAlpha = srcData.alphaData != null; - byte[] newAlphaData = null; - - int destBytesPerLine = 0; - ImageData imgData = null; - - switch (orientation) { - case PositionConstants.WEST: - case PositionConstants.EAST: - destBytesPerLine = srcData.height * bytesPerPixel; - imgData = new ImageData(srcData.height, srcData.width, srcData.depth, srcData.palette, destBytesPerLine, newData); - break; - default: - // Default behavior for NORTH and SOUTH - destBytesPerLine = srcData.width * bytesPerPixel; - imgData = new ImageData(srcData.width, srcData.height, srcData.depth, srcData.palette, destBytesPerLine, newData); - } - - if (isAlpha) { - newAlphaData = new byte[srcData.alphaData.length]; - imgData.alphaData = newAlphaData; - } - imgData.alpha = srcData.alpha; - - for (int srcY = 0; srcY < srcData.height; srcY++) { - for (int srcX = 0; srcX < srcData.width; srcX++) { - int destX; - int destY; - - switch (orientation) { - case PositionConstants.WEST: // left 90 degrees - destX = srcY; - destY = srcData.width - srcX - 1; - break; - case PositionConstants.EAST: // right 90 degrees - destX = srcData.height - srcY - 1; - destY = srcX; - break; - case PositionConstants.SOUTH: // 180 degrees - destX = srcData.width - srcX - 1; - destY = srcData.height - srcY - 1; - break; - default: - destX = srcX; - destY = srcY; - } - - imgData.setPixel(destX, destY, srcData.getPixel(srcX, srcY)); - if (isAlpha) { - imgData.setAlpha(destX, destY, srcData.getAlpha(srcX, srcY)); - } - } - } - - return new Image(image.getDevice(), imgData); - } - - private static String getKey(String path, int orientation) { - return path + orientation; - } - -} +/******************************************************************************* + * Copyright (c) 2019, 2021 THALES GLOBAL SERVICES. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Thales - initial API and implementation + *******************************************************************************/ +package org.polarsys.kitalpha.sirius.rotativeimage.internal.helpers; + +import java.io.File; +import java.util.HashMap; + +import org.eclipse.core.runtime.Path; +import org.eclipse.draw2d.PositionConstants; +import org.eclipse.draw2d.geometry.Rectangle; +import org.eclipse.jface.resource.ImageRegistry; +import org.eclipse.sirius.common.tools.api.resource.FileProvider; +import org.eclipse.sirius.diagram.ui.tools.api.figure.WorkspaceImageFigure; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.polarsys.kitalpha.sirius.rotativeimage.Activator; + +/** + * An utility class to create rotated images and access them. It stores images in the ImageRegistry of this plugin + */ +public class RotativeWorkspaceImageHelper { + + private static HashMap IMAGE_PATHS_CACHE = new HashMap<>(); + + private RotativeWorkspaceImageHelper() { + throw new IllegalStateException("Utility class"); + } + + /** + * Return an image from the given path. + * + * @param path + * {@link org.eclipse.sirius.diagram.WorkspaceImage#getWorkspacePath Workspace Path} + * @param orientation + * one of PositionConstants.WEST, PositionConstants.EAST, PositionConstants.SOUTH, + * PositionConstants.NORTH + * @return + */ + public static Image getImage(String path, int orientation) { + String key = getKey(path, orientation); + + ImageRegistry registry = Activator.getDefault().getImageRegistry(); + Image image = registry.get(key); + if (image != null) { + return image; + } + + switch (orientation) { + case PositionConstants.SOUTH: + case PositionConstants.WEST: + case PositionConstants.EAST: + image = rotate(getImage(path, PositionConstants.NORTH), orientation); + break; + default: + image = WorkspaceImageFigure.getImageInstanceFromPath(path); + } + + if (image != null) { + registry.put(key, image); + } + return image; + } + + public static Rectangle rotateRectangle(Rectangle rect, int orientation) { + switch (orientation) { + case PositionConstants.WEST: + case PositionConstants.EAST: + return rect.transpose(); + default: + return rect; + } + } + + public static String get4ImagesDocumentKey(String path, int orientation) { + String result = IMAGE_PATHS_CACHE.get(path+orientation); + if (result == null) { + result = getImageURI(getOrientedPath(path, orientation)); + if (result == null) { + // Default to original file path + result = getImageURI(path); + } + IMAGE_PATHS_CACHE.put(path+orientation, result); + } + return result; + } + + /** + * Inserts {@code orientation} as "_top", "_bottom", "_left", "_right" between file name in {@code path} and file + * extension. + * + * @param path + * @param orientation + * one of PositionConstants.WEST, PositionConstants.EAST, PositionConstants.SOUTH, + * PositionConstants.NORTH + * @return {@code path} with inserted {@code orientation}. If {@code orientation} is not among expected values then + * {@code path} is kepts as is. + */ + public static String getOrientedPath(String path, int orientation) { + int extentionPosition = path.lastIndexOf("."); + String baseName = path.substring(0, extentionPosition); + String ext = path.substring(extentionPosition); + switch (orientation) { + case PositionConstants.NORTH: + baseName += "_top"; + break; + case PositionConstants.SOUTH: + baseName += "_bottom"; + break; + case PositionConstants.WEST: + baseName += "_left"; + break; + case PositionConstants.EAST: + baseName += "_right"; + break; + default: + // Do nothing to default to baseName + } + return baseName + ext; + } + + private static String getImageURI(String basepath) { + final File imageFile = FileProvider.getDefault().getFile(new Path(basepath)); + if (imageFile != null) { + return imageFile.toURI().toString(); + } + return null; + } + + public static void createImage(String mainPath, int orientation) { + String key = getKey(mainPath, orientation); + String orientedPath = getOrientedPath(mainPath, orientation); + ImageRegistry registry = Activator.getDefault().getImageRegistry(); + Image image = registry.get(key); + if (image == null) { + image = WorkspaceImageFigure.flyWeightImage(orientedPath); + registry.put(key, image); + } + } + + /** + * Rotate an image + * + * @param image + * the source + * @param orientation + * PositionConstants.WEST will rotate by 90 degrees on the left, PositionConstants.EAST will rotate by 90 + * degrees on the right, PositionConstants.SOUTH will rotate by 180 degrees. + * @return the rotated image + */ + public static Image rotate(Image image, int orientation) { + if (image == null) { + // We don't want to rotate a null image + return null; + } + ImageData srcData = image.getImageData(); + int bytesPerPixel = srcData.bytesPerLine / srcData.width; + byte[] newData = new byte[srcData.data.length]; + + boolean isAlpha = srcData.alphaData != null; + byte[] newAlphaData = null; + + int destBytesPerLine = 0; + ImageData imgData = null; + + switch (orientation) { + case PositionConstants.WEST: + case PositionConstants.EAST: + destBytesPerLine = srcData.height * bytesPerPixel; + imgData = new ImageData(srcData.height, srcData.width, srcData.depth, srcData.palette, destBytesPerLine, newData); + break; + default: + // Default behavior for NORTH and SOUTH + destBytesPerLine = srcData.width * bytesPerPixel; + imgData = new ImageData(srcData.width, srcData.height, srcData.depth, srcData.palette, destBytesPerLine, newData); + } + + if (isAlpha) { + newAlphaData = new byte[srcData.alphaData.length]; + imgData.alphaData = newAlphaData; + } + imgData.alpha = srcData.alpha; + + for (int srcY = 0; srcY < srcData.height; srcY++) { + for (int srcX = 0; srcX < srcData.width; srcX++) { + int destX; + int destY; + + switch (orientation) { + case PositionConstants.WEST: // left 90 degrees + destX = srcY; + destY = srcData.width - srcX - 1; + break; + case PositionConstants.EAST: // right 90 degrees + destX = srcData.height - srcY - 1; + destY = srcX; + break; + case PositionConstants.SOUTH: // 180 degrees + destX = srcData.width - srcX - 1; + destY = srcData.height - srcY - 1; + break; + default: + destX = srcX; + destY = srcY; + } + + imgData.setPixel(destX, destY, srcData.getPixel(srcX, srcY)); + if (isAlpha) { + imgData.setAlpha(destX, destY, srcData.getAlpha(srcX, srcY)); + } + } + } + + return new Image(image.getDevice(), imgData); + } + + private static String getKey(String path, int orientation) { + return path + orientation; + } + +} diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/description/test.odesign b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/description/test.odesign index 12e41bf3b..c96235358 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/description/test.odesign +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/description/test.odesign @@ -1,125 +1,134 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon.svg b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon.svg index 01e175515..0c65e7810 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon.svg +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon.svg @@ -1,64 +1,64 @@ - - - - - - - - - - - + + + + + + + + + + + diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_bottom.svg b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_bottom.svg index b4f6365cf..226b0523a 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_bottom.svg +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_bottom.svg @@ -1,68 +1,68 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_left.svg b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_left.svg index e4a914076..924b2232b 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_left.svg +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_left.svg @@ -1,68 +1,68 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_right.svg b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_right.svg index 6ffc0dc8d..bbf145a44 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_right.svg +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_right.svg @@ -1,68 +1,68 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_top.svg b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_top.svg index 062c6d0ec..f7a2a561a 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_top.svg +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon_top.svg @@ -1,64 +1,64 @@ - - - - - - - - - - - + + + + + + + + + + + diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/rotationIcon.svg b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/rotationIcon.svg index 891e9b427..90ca804e1 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/rotationIcon.svg +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/rotationIcon.svg @@ -1,64 +1,64 @@ - - - - - - - - - - - + + + + + + + + + + + diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/models/Test1.aird b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/models/Test1.aird index 1ac4be92f..5fbccf828 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/models/Test1.aird +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/models/Test1.aird @@ -1,1220 +1,1220 @@ - - - - Test1.xmi - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - - - - - - - - - - - - - KEEP_LOCATION - KEEP_SIZE - KEEP_RATIO - - - - - - - - - - - - + + + + Test1.xmi + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/AbstractRotativeImageTest.java b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/AbstractRotativeImageTest.java index d5abe048a..5037467ad 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/AbstractRotativeImageTest.java +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/AbstractRotativeImageTest.java @@ -49,6 +49,7 @@ public void setup(){ resourceSet = new ResourceSetImpl(); resources = new ArrayList<>(); sessions = new ArrayList<>(); + org.eclipse.swt.graphics.Resource.setNonDisposeHandler(null); for (URI uri : resourcesToLoad()) { Resource resource = resourceSet.getResource(uri, true); if (resource == null) { diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/LoadSessionWithExtensionTest.java b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/LoadSessionWithExtensionTest.java index a2751481b..0851af1e0 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/LoadSessionWithExtensionTest.java +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/LoadSessionWithExtensionTest.java @@ -1,128 +1,132 @@ -/******************************************************************************* - * Copyright (c) 2021 Thales Global Services S.A.S. - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Thales Global Services S.A.S - initial API and implementation - *******************************************************************************/ -package org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases; - -import java.util.Collection; - -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.sirius.business.api.dialect.DialectManager; -import org.eclipse.sirius.business.api.session.Session; -import org.eclipse.sirius.business.api.session.SessionManager; -import org.eclipse.sirius.diagram.DDiagram; -import org.eclipse.sirius.tests.support.api.SiriusDiagramTestCase; -import org.eclipse.sirius.tests.support.api.TestsUtil; -import org.eclipse.sirius.ui.business.api.dialect.DialectEditor; -import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager; -import org.eclipse.sirius.ui.business.api.session.IEditingSession; -import org.eclipse.sirius.ui.business.api.session.SessionUIManager; -import org.eclipse.sirius.viewpoint.DRepresentationDescriptor; -import org.junit.Assert; - -/** - * Checks the loading of the RotativeImage extension point - * - * @author Arnaud Dieumegard - */ -public class LoadSessionWithExtensionTest extends SiriusDiagramTestCase { - - protected static final String PLATFORM_PLUGIN_PATH = "platform:/plugin/"; - protected static final String ROTATIVEIMAGE_TEST_PLUGIN_NAME = "/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests"; - - private static final String TEST_XMI_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.xmi"; - private static final String TEST_AIRD_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.aird"; - private static final String TEST_ODESIGN_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/description/test.odesign"; - - private static final String ID_REPRESENTATION_DESCRIPTOR = "_idds4E1NEeySgagIY4HK9g"; - private static final String ID_REPRESENTATION_DESCRIPTOR_PNG = "_h9U1ME4GEeyTxN2ah5Tp3g"; - private static final String ID_REPRESENTATION_DESCRIPTOR_FAULTY_ROTATIVE_PNG = "_4gkTAE6SEeyXs6TJVt2feA"; - private static final String ID_REPRESENTATION_DESCRIPTOR_FAULTY_ROTATIVE_SVG = "_BTwm8E6TEeyXs6TJVt2feA"; - - private static final String[] ALL_REPRESENTATION_ID = {ID_REPRESENTATION_DESCRIPTOR, ID_REPRESENTATION_DESCRIPTOR_PNG, ID_REPRESENTATION_DESCRIPTOR_FAULTY_ROTATIVE_PNG, ID_REPRESENTATION_DESCRIPTOR_FAULTY_ROTATIVE_SVG}; - - @Override - protected void setUp() throws Exception { - super.setUp(); - genericSetUp(TEST_XMI_PATH, TEST_ODESIGN_PATH, TEST_AIRD_PATH); - } - - /** - * Open selected diagrams in session. - */ - public void testDiagramsOpening() { - for (String repId : ALL_REPRESENTATION_ID) { - DRepresentationDescriptor desc = getRepresentationDescriptor(session, repId); - assertNotNull(desc); - - DDiagram ddiagram = (DDiagram) desc.getRepresentation(); - assertNotNull(ddiagram); - - DialectEditor editor = (DialectEditor) DialectUIManager.INSTANCE.openEditor(session, ddiagram, new NullProgressMonitor()); - TestsUtil.synchronizationWithUIThread(); - DialectUIManager.INSTANCE.refreshEditor(editor, new NullProgressMonitor()); - TestsUtil.synchronizationWithUIThread(); - } - } - - public DRepresentationDescriptor getRepresentationDescriptor(Session session, String id) { - Collection representationDescriptors = DialectManager.INSTANCE.getAllRepresentationDescriptors(session); - for (DRepresentationDescriptor representationDescriptor : representationDescriptors) { - - String descriptorFragment; - try { - descriptorFragment = representationDescriptor.getRepPath().getResourceURI().fragment(); - } catch (NullPointerException e) { - descriptorFragment = ""; - } - - String descriptorUid = representationDescriptor.getUid(); - - if (id.equals(descriptorFragment) || id.equals(descriptorUid)) { - return representationDescriptor; - } - } - return null; - } - - @Override - protected void tearDown() throws Exception { - doCleanup(); - super.tearDown(); - } - - private void doCleanup() { - final IEditingSession sessionUI = SessionUIManager.INSTANCE.getUISession(session); - if (sessionUI != null) { - SessionUIManager.INSTANCE.remove(sessionUI); - sessionUI.close(); - TestsUtil.emptyEventsFromUIThread(); - } - if (session != null) { - doRemoveSession(); - doCloseSession(); - session = null; - } - viewpoints.clear(); - } - - private void doCloseSession() { - session.close(new NullProgressMonitor()); - Assert.assertFalse("Can't close the session", session.isOpen()); - } - - private void doRemoveSession() { - SessionManager.INSTANCE.remove(session); - for (final Session session2 : SessionManager.INSTANCE.getSessions()) { - Assert.assertFalse("Remove failed", session2.equals(session)); - } - } - -} +/******************************************************************************* + * Copyright (c) 2021 Thales Global Services S.A.S. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Thales Global Services S.A.S - initial API and implementation + *******************************************************************************/ +package org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases; + +import java.util.Collection; + +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.sirius.business.api.dialect.DialectManager; +import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.business.api.session.SessionManager; +import org.eclipse.sirius.diagram.DDiagram; +import org.eclipse.sirius.tests.support.api.SiriusDiagramTestCase; +import org.eclipse.sirius.tests.support.api.TestsUtil; +import org.eclipse.sirius.ui.business.api.dialect.DialectEditor; +import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager; +import org.eclipse.sirius.ui.business.api.session.IEditingSession; +import org.eclipse.sirius.ui.business.api.session.SessionUIManager; +import org.eclipse.sirius.viewpoint.DRepresentationDescriptor; +import org.eclipse.swt.graphics.Resource; +import org.junit.Assert; + +/** + * Checks the loading of the RotativeImage extension point + * + * @author Arnaud Dieumegard + */ +public class LoadSessionWithExtensionTest extends SiriusDiagramTestCase { + + protected static final String PLATFORM_PLUGIN_PATH = "platform:/plugin/"; + protected static final String ROTATIVEIMAGE_TEST_PLUGIN_NAME = "/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests"; + + private static final String TEST_XMI_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.xmi"; + private static final String TEST_AIRD_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.aird"; + private static final String TEST_ODESIGN_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/description/test.odesign"; + + private static final String ID_REPRESENTATION_DESCRIPTOR = "_idds4E1NEeySgagIY4HK9g"; + private static final String ID_REPRESENTATION_DESCRIPTOR_PNG = "_h9U1ME4GEeyTxN2ah5Tp3g"; + private static final String ID_REPRESENTATION_DESCRIPTOR_FAULTY_ROTATIVE_PNG = "_4gkTAE6SEeyXs6TJVt2feA"; + private static final String ID_REPRESENTATION_DESCRIPTOR_FAULTY_ROTATIVE_SVG = "_BTwm8E6TEeyXs6TJVt2feA"; + + private static final String[] ALL_REPRESENTATION_ID = {ID_REPRESENTATION_DESCRIPTOR, ID_REPRESENTATION_DESCRIPTOR_PNG, ID_REPRESENTATION_DESCRIPTOR_FAULTY_ROTATIVE_PNG, ID_REPRESENTATION_DESCRIPTOR_FAULTY_ROTATIVE_SVG}; + + @Override + protected void setUp() throws Exception { + super.setUp(); + genericSetUp(TEST_XMI_PATH, TEST_ODESIGN_PATH, TEST_AIRD_PATH); + Resource.setNonDisposeHandler(null); + } + + /** + * Open selected diagrams in session. + */ + public void testDiagramsOpening() { + for (String repId : ALL_REPRESENTATION_ID) { + DRepresentationDescriptor desc = getRepresentationDescriptor(session, repId); + assertNotNull(desc); + + DDiagram ddiagram = (DDiagram) desc.getRepresentation(); + assertNotNull(ddiagram); + + DialectEditor editor = (DialectEditor) DialectUIManager.INSTANCE.openEditor(session, ddiagram, new NullProgressMonitor()); + TestsUtil.synchronizationWithUIThread(); + DialectUIManager.INSTANCE.refreshEditor(editor, new NullProgressMonitor()); + TestsUtil.synchronizationWithUIThread(); + DialectUIManager.INSTANCE.closeEditor(editor, false); + TestsUtil.synchronizationWithUIThread(); + } + } + + public DRepresentationDescriptor getRepresentationDescriptor(Session session, String id) { + Collection representationDescriptors = DialectManager.INSTANCE.getAllRepresentationDescriptors(session); + for (DRepresentationDescriptor representationDescriptor : representationDescriptors) { + + String descriptorFragment; + try { + descriptorFragment = representationDescriptor.getRepPath().getResourceURI().fragment(); + } catch (NullPointerException e) { + descriptorFragment = ""; + } + + String descriptorUid = representationDescriptor.getUid(); + + if (id.equals(descriptorFragment) || id.equals(descriptorUid)) { + return representationDescriptor; + } + } + return null; + } + + @Override + protected void tearDown() throws Exception { + doCleanup(); + super.tearDown(); + } + + private void doCleanup() { + final IEditingSession sessionUI = SessionUIManager.INSTANCE.getUISession(session); + if (sessionUI != null) { + SessionUIManager.INSTANCE.remove(sessionUI); + sessionUI.close(); + TestsUtil.emptyEventsFromUIThread(); + } + if (session != null) { + doRemoveSession(); + doCloseSession(); + session = null; + } + viewpoints.clear(); + } + + private void doCloseSession() { + session.close(new NullProgressMonitor()); + Assert.assertFalse("Can't close the session", session.isOpen()); + } + + private void doRemoveSession() { + SessionManager.INSTANCE.remove(session); + for (final Session session2 : SessionManager.INSTANCE.getSessions()) { + Assert.assertFalse("Remove failed", session2.equals(session)); + } + } + +} diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/RotativeImageDisplayTest.java b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/RotativeImageDisplayTest.java index afea4694a..a10ebff1f 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/RotativeImageDisplayTest.java +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/RotativeImageDisplayTest.java @@ -1,465 +1,479 @@ -/******************************************************************************* - * Copyright (c) 2021 Thales Global Services S.A.S. - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Thales Global Services S.A.S - initial API and implementation - *******************************************************************************/ -package org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases; - -import java.io.File; -import java.net.MalformedURLException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; - -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.draw2d.IFigure; -import org.eclipse.draw2d.PositionConstants; -import org.eclipse.emf.ecore.EClass; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.EStructuralFeature; -import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; -import org.eclipse.jface.resource.ImageDescriptor; -import org.eclipse.sirius.business.api.dialect.DialectManager; -import org.eclipse.sirius.business.api.session.Session; -import org.eclipse.sirius.business.api.session.SessionManager; -import org.eclipse.sirius.diagram.DDiagram; -import org.eclipse.sirius.diagram.DDiagramElement; -import org.eclipse.sirius.diagram.ui.internal.edit.parts.DNode2EditPart; -import org.eclipse.sirius.diagram.ui.internal.edit.parts.WorkspaceImageEditPart; -import org.eclipse.sirius.diagram.ui.internal.refresh.listeners.WorkspaceFileResourceChangeListener; -import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin; -import org.eclipse.sirius.diagram.ui.tools.api.image.DiagramImagesPath; -import org.eclipse.sirius.tests.support.api.SiriusDiagramTestCase; -import org.eclipse.sirius.tests.support.api.TestsUtil; -import org.eclipse.sirius.ui.business.api.dialect.DialectEditor; -import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager; -import org.eclipse.sirius.ui.business.api.session.IEditingSession; -import org.eclipse.sirius.ui.business.api.session.SessionUIManager; -import org.eclipse.sirius.viewpoint.DRepresentationDescriptor; -import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.ImageData; -import org.junit.Assert; -import org.polarsys.kitalpha.sirius.rotativeimage.figures.Rotative4ImagesSVGWorkspaceImageFigure; -import org.polarsys.kitalpha.sirius.rotativeimage.figures.RotativeSVGWorkspaceImageFigure; -import org.polarsys.kitalpha.sirius.rotativeimage.figures.RotativeWorkspaceImageFigure; - -/** - * Checks the displayed images in rotative ports. Compares displayed images with Image registers content. PNG images are - * stored in the static {@link org.polarsys.kitalpha.sirius.rotativeimage.Activator ImageRegistry} SVG images are stored - * in the static {@link org.eclipse.sirius.diagram.ui.tools.api.figure.SVGFigure.ImageCache} - * - * @author Arnaud Dieumegard - */ -public class RotativeImageDisplayTest extends SiriusDiagramTestCase { - - protected static final String PLATFORM_PLUGIN_PATH = "platform:/plugin/"; - - protected static final String ROTATIVEIMAGE_TEST_PLUGIN_NAME = "/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests"; - - private static final String TEST_XMI_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.xmi"; - - private static final String TEST_AIRD_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.aird"; - - private static final String TEST_ODESIGN_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/description/test.odesign"; - - private static final String ID_REPRESENTATION_DESCRIPTOR_SVG = "_idds4E1NEeySgagIY4HK9g"; - - private static final String ID_REPRESENTATION_DESCRIPTOR_PNG = "_h9U1ME4GEeyTxN2ah5Tp3g"; - - private static final String ID_REPRESENTATION_DESCRIPTOR_FAULTY_4IMAGES = "_xkHCoFKBEeytGcI8gHYSmQ"; - - private Map dDiagramElementToPositionConstantRotationSVG; - - private Map dDiagramElementToPositionConstantFourImagesSVG; - - private Map dDiagramElementToPositionConstantRotationPNG; - - private Map dDiagramElementToPositionConstantFourImagesPNG; - - private Map dDiagramElementsToFileExtensionForFauly4Images; - - private static final String SVG = ".svg"; - - private static final String PNG = ".png"; - - private static final String ROTATIONICON = "org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/rotationIcon"; - - private static final String ROTATIONICON_SVG = ROTATIONICON + SVG; - - private static final String ROTATIONICON_PNG = ROTATIONICON + PNG; - - private static final String FOURIMAGESICON_PREFIX = "/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon"; - - private static final String FOURIMAGESICON_ERROR = FOURIMAGESICON_PREFIX + "_error"; - - private static final String TOP = "top"; - - private static final String BOTTOM = "bottom"; - - private static final String RIGHT = "right"; - - private static final String LEFT = "left"; - - @Override - protected void setUp() throws Exception { - super.setUp(); - genericSetUp(TEST_XMI_PATH, TEST_ODESIGN_PATH, TEST_AIRD_PATH); - - // Set test data - dDiagramElementToPositionConstantFourImagesSVG = new HashMap<>(); - dDiagramElementToPositionConstantFourImagesSVG.put("_ieoKgE1NEeySgagIY4HK9g", PositionConstants.NORTH); - dDiagramElementToPositionConstantFourImagesSVG.put("_ie8TkE1NEeySgagIY4HK9g", PositionConstants.EAST); - dDiagramElementToPositionConstantFourImagesSVG.put("_ie-v0E1NEeySgagIY4HK9g", PositionConstants.WEST); - dDiagramElementToPositionConstantFourImagesSVG.put("_ifBMEE1NEeySgagIY4HK9g", PositionConstants.SOUTH); - - dDiagramElementToPositionConstantRotationSVG = new HashMap<>(); - dDiagramElementToPositionConstantRotationSVG.put("_ZRjK4U6VEey8JIaoK8ucAw", PositionConstants.NORTH); - dDiagramElementToPositionConstantRotationSVG.put("_ZRoqcU6VEey8JIaoK8ucAw", PositionConstants.EAST); - dDiagramElementToPositionConstantRotationSVG.put("_ZRrtwU6VEey8JIaoK8ucAw", PositionConstants.WEST); - dDiagramElementToPositionConstantRotationSVG.put("_ZRuxEk6VEey8JIaoK8ucAw", PositionConstants.SOUTH); - - dDiagramElementToPositionConstantFourImagesPNG = new HashMap<>(); - dDiagramElementToPositionConstantFourImagesPNG.put("_iB9dkU4GEeyTxN2ah5Tp3g", PositionConstants.NORTH); - dDiagramElementToPositionConstantFourImagesPNG.put("_iCBvAU4GEeyTxN2ah5Tp3g", PositionConstants.EAST); - dDiagramElementToPositionConstantFourImagesPNG.put("_iCC9IU4GEeyTxN2ah5Tp3g", PositionConstants.WEST); - dDiagramElementToPositionConstantFourImagesPNG.put("_iCDkMU4GEeyTxN2ah5Tp3g", PositionConstants.SOUTH); - - dDiagramElementToPositionConstantRotationPNG = new HashMap<>(); - dDiagramElementToPositionConstantRotationPNG.put("_bfZigE6VEey8JIaoK8ucAw", PositionConstants.NORTH); - dDiagramElementToPositionConstantRotationPNG.put("_bfb-wk6VEey8JIaoK8ucAw", PositionConstants.EAST); - dDiagramElementToPositionConstantRotationPNG.put("_bffCEU6VEey8JIaoK8ucAw", PositionConstants.WEST); - dDiagramElementToPositionConstantRotationPNG.put("_bfheU06VEey8JIaoK8ucAw", PositionConstants.SOUTH); - - dDiagramElementsToFileExtensionForFauly4Images = new HashMap<>(); - dDiagramElementsToFileExtensionForFauly4Images.put("_xkVsIFKBEeytGcI8gHYSmQ", PNG); - dDiagramElementsToFileExtensionForFauly4Images.put("_xkWTMlKBEeytGcI8gHYSmQ", PNG); - dDiagramElementsToFileExtensionForFauly4Images.put("_xkWTNlKBEeytGcI8gHYSmQ", PNG); - dDiagramElementsToFileExtensionForFauly4Images.put("_xkW6Q1KBEeytGcI8gHYSmQ", PNG); - dDiagramElementsToFileExtensionForFauly4Images.put("_xkW6R1KBEeytGcI8gHYSmQ", SVG); - dDiagramElementsToFileExtensionForFauly4Images.put("_xkXhUlKBEeytGcI8gHYSmQ", SVG); - dDiagramElementsToFileExtensionForFauly4Images.put("_xkXhVlKBEeytGcI8gHYSmQ", SVG); - dDiagramElementsToFileExtensionForFauly4Images.put("_xkYIYVKBEeytGcI8gHYSmQ", SVG); - } - - /** - * Ensure Rotative and 4Images WorkspaceImages reference expected SVG images - */ - @SuppressWarnings("restriction") - public void testSVGDiagramImages() { - String repId = ID_REPRESENTATION_DESCRIPTOR_SVG; - DDiagram ddiagram = getDDiagramFromId(repId); - - for (Entry entry : dDiagramElementToPositionConstantRotationSVG.entrySet()) { - String elementId = entry.getKey(); - int position = entry.getValue(); - - WorkspaceImageEditPart editPart = getImageEditPart(ddiagram, elementId); - assertNotNull(editPart); - RotativeSVGWorkspaceImageFigure figure = (RotativeSVGWorkspaceImageFigure) editPart.getContentPane(); - assertNotNull(figure); - - // Check for SVGImage DocumentKey - String figureDocumentKey = figure.getDocumentKey(); - assertTrue("Figure " + entry.getKey() + " should reference uri " + ROTATIONICON_SVG + position + " instead uri is " + figureDocumentKey, - figureDocumentKey.endsWith(ROTATIONICON_SVG + position)); - } - - for (Entry entry : dDiagramElementToPositionConstantFourImagesSVG.entrySet()) { - String elementId = entry.getKey(); - int position = entry.getValue(); - - WorkspaceImageEditPart editPart = getImageEditPart(ddiagram, elementId); - assertNotNull(editPart); - Rotative4ImagesSVGWorkspaceImageFigure figure = (Rotative4ImagesSVGWorkspaceImageFigure) editPart.getContentPane(); - assertNotNull(figure); - - // Check for SVGImage DocumentKey - String figureDocumentKey = figure.getDocumentKey(); - String postfix = buildExpectedImagePostfix(position, SVG); - assertTrue("Figure " + entry.getKey() + " should reference uri containing " + FOURIMAGESICON_PREFIX + postfix + " instead uri is " + figureDocumentKey, - figureDocumentKey.endsWith(FOURIMAGESICON_PREFIX + postfix)); - } - } - - /** - * Ensure Rotative and 4Images WorkspaceImages reference expected PNG images - */ - @SuppressWarnings("restriction") - public void testPNGDiagramImages() { - String repId = ID_REPRESENTATION_DESCRIPTOR_PNG; - DDiagram ddiagram = getDDiagramFromId(repId); - - // Ensure displayed images rely on the correct rotated image - for (Entry entry : dDiagramElementToPositionConstantRotationPNG.entrySet()) { - String elementId = entry.getKey(); - int position = entry.getValue(); - - // Get the EditPart and the Figure - WorkspaceImageEditPart editPart = getImageEditPart(ddiagram, elementId); - assertNotNull(editPart); - RotativeWorkspaceImageFigure figure = (RotativeWorkspaceImageFigure) editPart.getContentPane(); - assertNotNull(figure); - - Image nonRotatedImage = getImageFromPath(ROTATIONICON_PNG, PNG); - assertNotNull(nonRotatedImage); - compareImages(figure, nonRotatedImage, position); - } - - // Ensure displayed images rely on the correct 4Images - for (Entry entry : dDiagramElementToPositionConstantFourImagesPNG.entrySet()) { - String elementId = entry.getKey(); - int position = entry.getValue(); - - // Get the EditPart and the Figure - WorkspaceImageEditPart editPart = getImageEditPart(ddiagram, elementId); - assertNotNull(editPart); - RotativeWorkspaceImageFigure figure = (RotativeWorkspaceImageFigure) editPart.getContentPane(); - assertNotNull(figure); - - // Get expected image - Image expectedImage = getImageFromPath(FOURIMAGESICON_PREFIX + buildExpectedImagePostfix(position, PNG), PNG); - assertNotNull(expectedImage); - compareImages(figure, expectedImage, PositionConstants.NORTH); - } - } - - /** - * Ensure 4Images WorkspaceImages reference default WorkspaceImage (the one defined in the odesign) when no - * postfixed 4image exists (_top, ...) - */ - public void testFaultyRotativeDiagramImages() { - String repId = ID_REPRESENTATION_DESCRIPTOR_FAULTY_4IMAGES; - DDiagram ddiagram = getDDiagramFromId(repId); - - for (Entry entry : dDiagramElementsToFileExtensionForFauly4Images.entrySet()) { - String elementId = entry.getKey(); - String extension = entry.getValue(); - - IFigure figure = getFigure(ddiagram, elementId); - - if (figure instanceof RotativeWorkspaceImageFigure) { - // Get expected image - Image expectedImage = getImageNotFoundImage(); - assertNotNull(expectedImage); - compareImages((RotativeWorkspaceImageFigure) figure, expectedImage, PositionConstants.NORTH); - } else { - String figureDocumentKey = ((Rotative4ImagesSVGWorkspaceImageFigure) figure).getDocumentKey(); - assertTrue("Figure " + entry.getKey() + " should reference uri " + FOURIMAGESICON_ERROR + extension + " instead uri is " + figureDocumentKey, - figureDocumentKey.endsWith(FOURIMAGESICON_ERROR + extension)); - } - } - } - - private Image getImageFromPath(String path, String extension) { - if (extension.equals(SVG)) { - return DiagramUIPlugin.getPlugin().getBundledImage(path); - } else { - final File imageFile = WorkspaceFileResourceChangeListener.getInstance().getFileFromURI(path); - ImageDescriptor desc = null; - if (imageFile != null && WorkspaceFileResourceChangeListener.getInstance().getReadStatusOfFile(imageFile)) { - try { - desc = WorkspaceFileResourceChangeListener.getInstance().findImageDescriptor(imageFile); - } catch (MalformedURLException e) { - // do nothing - } - } - if (desc != null) { - return DiagramUIPlugin.getPlugin().getImage(desc); - } else { - return getImageNotFoundImage(); - } - } - } - - private Image getImageNotFoundImage() { - return DiagramUIPlugin.getPlugin().getImage(DiagramUIPlugin.Implementation.findImageWithDimensionDescriptor(DiagramImagesPath.IMAGE_NOT_FOUND)); - } - - private IFigure getFigure(DDiagram ddiagram, String elementId) { - WorkspaceImageEditPart editPart = getImageEditPart(ddiagram, elementId); - assertNotNull(editPart); - IFigure figure = editPart.getContentPane(); - assertNotNull(figure); - return figure; - } - - private DDiagram getDDiagramFromId(String repId) { - DRepresentationDescriptor desc = getRepresentationDescriptor(session, repId); - DDiagram ddiagram = (DDiagram) desc.getRepresentation(); - - DialectEditor editor = (DialectEditor) DialectUIManager.INSTANCE.openEditor(session, ddiagram, new NullProgressMonitor()); - TestsUtil.synchronizationWithUIThread(); - DialectUIManager.INSTANCE.refreshEditor(editor, new NullProgressMonitor()); - TestsUtil.synchronizationWithUIThread(); - return ddiagram; - } - - private String buildExpectedImagePostfix(int position, String extension) { - String postfix = "_"; - switch (position) { - case PositionConstants.NORTH: - postfix += TOP; - break; - case PositionConstants.EAST: - postfix += RIGHT; - break; - case PositionConstants.WEST: - postfix += LEFT; - break; - case PositionConstants.SOUTH: - postfix += BOTTOM; - break; - } - postfix += extension; - return postfix; - } - - /** - * Compare images through ImageData comparison. expectedImage may be rotated according to the rotation parameter - * whose values should be among: - *
    - *
  • PositionConstants.NORTH: No rotation
  • - *
  • PositionConstants.EAST: 90° rotation to the right
  • - *
  • PositionConstants.WEST: 90° rotation to the left
  • - *
  • PositionConstants.SOUTH: 180° rotation
  • - *
- * - * @param figure - * @param nonRotatedOriginalImage - * @param rotation - * The rotation to apply to expectedImage - */ - private void compareImages(RotativeWorkspaceImageFigure figure, Image nonRotatedOriginalImage, int rotation) { - ImageData expectedImageData = nonRotatedOriginalImage.getImageData(); - ImageData actualImageData = figure.getImage().getImageData(); - compareImageData(rotation, expectedImageData, actualImageData); - } - - private void compareImageData(int rotation, ImageData expectedImageData, ImageData actualImageData) { - for (int x = 0; x < expectedImageData.width; x++) { - for (int y = 0; y < expectedImageData.height; y++) { - int expectedX = x; - int expectedY = y; - - switch (rotation) { - case PositionConstants.WEST: // left 90 degrees - expectedX = y; - expectedY = actualImageData.width - x - 1; - break; - case PositionConstants.EAST: // right 90 degrees - expectedX = actualImageData.height - y - 1; - expectedY = x; - break; - case PositionConstants.SOUTH: // 180 degrees - expectedX = actualImageData.width - x - 1; - expectedY = actualImageData.height - y - 1; - break; - } - // The tested image pixel at position rotated matches the original non rotated image - assertEquals(expectedImageData.getPixel(x, y), actualImageData.getPixel(expectedX, expectedY)); - assertEquals(expectedImageData.getAlpha(x, y), actualImageData.getAlpha(expectedX, expectedY)); - } - } - } - - @SuppressWarnings({ "unchecked", "restriction" }) - private WorkspaceImageEditPart getImageEditPart(DDiagram ddiagram, String elementId) { - List diagramElementsFromLabel = getDiagramElementsFromUid(ddiagram, elementId, DDiagramElement.class); - for (DDiagramElement dDiagramElement : diagramElementsFromLabel) { - IGraphicalEditPart editPart = getEditPart(dDiagramElement); - if (editPart instanceof DNode2EditPart) { - List childrens = (List) editPart.getChildren().stream().filter(WorkspaceImageEditPart.class::isInstance).collect(Collectors.toList()); - assertNotNull(childrens); - assertEquals(1, childrens.size()); - return childrens.get(0); - } - - } - return null; - } - - private List getDiagramElementsFromUid(final DDiagram diagram, final String uid, final Class searchedClass) { - final List found = new ArrayList<>(); - final Iterator it = diagram.eAllContents(); - while (it.hasNext()) { - final EObject cur = it.next(); - if (searchedClass.isInstance(cur) && (uidFeature(cur.eClass()) != null) && uid.equals(getUidValue(cur))) { - found.add(searchedClass.cast(cur)); - } - } - return found; - } - - private String getUidValue(final EObject cur) { - return (String) cur.eGet(uidFeature(cur.eClass())); - } - - private EStructuralFeature uidFeature(final EClass class1) { - return class1.getEStructuralFeature("uid"); - } - - public DRepresentationDescriptor getRepresentationDescriptor(Session session, String id) { - Collection representationDescriptors = DialectManager.INSTANCE.getAllRepresentationDescriptors(session); - for (DRepresentationDescriptor representationDescriptor : representationDescriptors) { - - String descriptorFragment; - try { - descriptorFragment = representationDescriptor.getRepPath().getResourceURI().fragment(); - } catch (NullPointerException e) { - descriptorFragment = ""; - } - - String descriptorUid = representationDescriptor.getUid(); - - if (id.equals(descriptorFragment) || id.equals(descriptorUid)) { - return representationDescriptor; - } - } - return null; - } - - @Override - protected void tearDown() throws Exception { - doCleanup(); - super.tearDown(); - } - - private void doCleanup() { - final IEditingSession sessionUI = SessionUIManager.INSTANCE.getUISession(session); - if (sessionUI != null) { - SessionUIManager.INSTANCE.remove(sessionUI); - sessionUI.close(); - TestsUtil.emptyEventsFromUIThread(); - } - if (session != null) { - doRemoveSession(); - doCloseSession(); - session = null; - } - viewpoints.clear(); - } - - private void doCloseSession() { - session.close(new NullProgressMonitor()); - Assert.assertFalse("Can't close the session", session.isOpen()); - } - - private void doRemoveSession() { - SessionManager.INSTANCE.remove(session); - for (final Session session2 : SessionManager.INSTANCE.getSessions()) { - Assert.assertFalse("Remove failed", session2.equals(session)); - } - } - -} +/******************************************************************************* + * Copyright (c) 2021 Thales Global Services S.A.S. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Thales Global Services S.A.S - initial API and implementation + *******************************************************************************/ +package org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases; + +import java.io.File; +import java.net.MalformedURLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Map.Entry; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.draw2d.IFigure; +import org.eclipse.draw2d.PositionConstants; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.sirius.business.api.dialect.DialectManager; +import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.business.api.session.SessionManager; +import org.eclipse.sirius.diagram.DDiagram; +import org.eclipse.sirius.diagram.DDiagramElement; +import org.eclipse.sirius.diagram.ui.internal.edit.parts.DNode2EditPart; +import org.eclipse.sirius.diagram.ui.internal.edit.parts.WorkspaceImageEditPart; +import org.eclipse.sirius.diagram.ui.internal.refresh.listeners.WorkspaceFileResourceChangeListener; +import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin; +import org.eclipse.sirius.diagram.ui.tools.api.image.DiagramImagesPath; +import org.eclipse.sirius.tests.support.api.SiriusDiagramTestCase; +import org.eclipse.sirius.tests.support.api.TestsUtil; +import org.eclipse.sirius.ui.business.api.dialect.DialectEditor; +import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager; +import org.eclipse.sirius.ui.business.api.session.IEditingSession; +import org.eclipse.sirius.ui.business.api.session.SessionUIManager; +import org.eclipse.sirius.viewpoint.DRepresentationDescriptor; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.Resource; +import org.eclipse.ui.PlatformUI; +import org.junit.Assert; +import org.polarsys.kitalpha.sirius.rotativeimage.figures.Rotative4ImagesSVGWorkspaceImageFigure; +import org.polarsys.kitalpha.sirius.rotativeimage.figures.RotativeSVGWorkspaceImageFigure; +import org.polarsys.kitalpha.sirius.rotativeimage.figures.RotativeWorkspaceImageFigure; + +/** + * Checks the displayed images in rotative ports. Compares displayed images with Image registers content. PNG images are + * stored in the static {@link org.polarsys.kitalpha.sirius.rotativeimage.Activator ImageRegistry} SVG images are stored + * in the static {@link org.eclipse.sirius.diagram.ui.tools.api.figure.SVGFigure.ImageCache} + * + * @author Arnaud Dieumegard + */ +public class RotativeImageDisplayTest extends SiriusDiagramTestCase { + + protected static final String PLATFORM_PLUGIN_PATH = "platform:/plugin/"; + + protected static final String ROTATIVEIMAGE_TEST_PLUGIN_NAME = "/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests"; + + private static final String TEST_XMI_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.xmi"; + + private static final String TEST_AIRD_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.aird"; + + private static final String TEST_ODESIGN_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/description/test.odesign"; + + private static final String ID_REPRESENTATION_DESCRIPTOR_SVG = "_idds4E1NEeySgagIY4HK9g"; + + private static final String ID_REPRESENTATION_DESCRIPTOR_PNG = "_h9U1ME4GEeyTxN2ah5Tp3g"; + + private static final String ID_REPRESENTATION_DESCRIPTOR_FAULTY_4IMAGES = "_xkHCoFKBEeytGcI8gHYSmQ"; + + private Map dDiagramElementToPositionConstantRotationSVG; + + private Map dDiagramElementToPositionConstantFourImagesSVG; + + private Map dDiagramElementToPositionConstantRotationPNG; + + private Map dDiagramElementToPositionConstantFourImagesPNG; + + private Map dDiagramElementsToFileExtensionForFauly4Images; + + private static final String SVG = ".svg"; + + private static final String PNG = ".png"; + + private static final String ROTATIONICON = "org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/rotationIcon"; + + private static final String ROTATIONICON_SVG = ROTATIONICON + SVG; + + private static final String ROTATIONICON_PNG = ROTATIONICON + PNG; + + private static final String FOURIMAGESICON_PREFIX = "/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon"; + + private static final String FOURIMAGESICON_ERROR = FOURIMAGESICON_PREFIX + "_error"; + + private static final String TOP = "top"; + + private static final String BOTTOM = "bottom"; + + private static final String RIGHT = "right"; + + private static final String LEFT = "left"; + + @Override + protected void setUp() throws Exception { + super.setUp(); + Resource.setNonDisposeHandler(null); + genericSetUp(TEST_XMI_PATH, TEST_ODESIGN_PATH, TEST_AIRD_PATH); + + // Set test data + dDiagramElementToPositionConstantFourImagesSVG = new HashMap<>(); + dDiagramElementToPositionConstantFourImagesSVG.put("_ieoKgE1NEeySgagIY4HK9g", PositionConstants.NORTH); + dDiagramElementToPositionConstantFourImagesSVG.put("_ie8TkE1NEeySgagIY4HK9g", PositionConstants.EAST); + dDiagramElementToPositionConstantFourImagesSVG.put("_ie-v0E1NEeySgagIY4HK9g", PositionConstants.WEST); + dDiagramElementToPositionConstantFourImagesSVG.put("_ifBMEE1NEeySgagIY4HK9g", PositionConstants.SOUTH); + + dDiagramElementToPositionConstantRotationSVG = new HashMap<>(); + dDiagramElementToPositionConstantRotationSVG.put("_ZRjK4U6VEey8JIaoK8ucAw", PositionConstants.NORTH); + dDiagramElementToPositionConstantRotationSVG.put("_ZRoqcU6VEey8JIaoK8ucAw", PositionConstants.EAST); + dDiagramElementToPositionConstantRotationSVG.put("_ZRrtwU6VEey8JIaoK8ucAw", PositionConstants.WEST); + dDiagramElementToPositionConstantRotationSVG.put("_ZRuxEk6VEey8JIaoK8ucAw", PositionConstants.SOUTH); + + dDiagramElementToPositionConstantFourImagesPNG = new HashMap<>(); + dDiagramElementToPositionConstantFourImagesPNG.put("_iB9dkU4GEeyTxN2ah5Tp3g", PositionConstants.NORTH); + dDiagramElementToPositionConstantFourImagesPNG.put("_iCBvAU4GEeyTxN2ah5Tp3g", PositionConstants.EAST); + dDiagramElementToPositionConstantFourImagesPNG.put("_iCC9IU4GEeyTxN2ah5Tp3g", PositionConstants.WEST); + dDiagramElementToPositionConstantFourImagesPNG.put("_iCDkMU4GEeyTxN2ah5Tp3g", PositionConstants.SOUTH); + + dDiagramElementToPositionConstantRotationPNG = new HashMap<>(); + dDiagramElementToPositionConstantRotationPNG.put("_bfZigE6VEey8JIaoK8ucAw", PositionConstants.NORTH); + dDiagramElementToPositionConstantRotationPNG.put("_bfb-wk6VEey8JIaoK8ucAw", PositionConstants.EAST); + dDiagramElementToPositionConstantRotationPNG.put("_bffCEU6VEey8JIaoK8ucAw", PositionConstants.WEST); + dDiagramElementToPositionConstantRotationPNG.put("_bfheU06VEey8JIaoK8ucAw", PositionConstants.SOUTH); + + dDiagramElementsToFileExtensionForFauly4Images = new HashMap<>(); + dDiagramElementsToFileExtensionForFauly4Images.put("_xkVsIFKBEeytGcI8gHYSmQ", PNG); + dDiagramElementsToFileExtensionForFauly4Images.put("_xkWTMlKBEeytGcI8gHYSmQ", PNG); + dDiagramElementsToFileExtensionForFauly4Images.put("_xkWTNlKBEeytGcI8gHYSmQ", PNG); + dDiagramElementsToFileExtensionForFauly4Images.put("_xkW6Q1KBEeytGcI8gHYSmQ", PNG); + dDiagramElementsToFileExtensionForFauly4Images.put("_xkW6R1KBEeytGcI8gHYSmQ", SVG); + dDiagramElementsToFileExtensionForFauly4Images.put("_xkXhUlKBEeytGcI8gHYSmQ", SVG); + dDiagramElementsToFileExtensionForFauly4Images.put("_xkXhVlKBEeytGcI8gHYSmQ", SVG); + dDiagramElementsToFileExtensionForFauly4Images.put("_xkYIYVKBEeytGcI8gHYSmQ", SVG); + } + /** + * Ensure Rotative and 4Images WorkspaceImages reference expected SVG images + */ + @SuppressWarnings("restriction") + public void testSVGDiagramImages() { + String repId = ID_REPRESENTATION_DESCRIPTOR_SVG; + DDiagram ddiagram = getDDiagramFromId(repId); + DialectEditor editor = getEditor(ddiagram); + + for (Entry entry : dDiagramElementToPositionConstantRotationSVG.entrySet()) { + String elementId = entry.getKey(); + int position = entry.getValue(); + + WorkspaceImageEditPart editPart = getImageEditPart(ddiagram, elementId); + assertNotNull(editPart); + RotativeSVGWorkspaceImageFigure figure = (RotativeSVGWorkspaceImageFigure) editPart.getContentPane(); + assertNotNull(figure); + + // Check for SVGImage DocumentKey + String figureDocumentKey = figure.getDocumentKey(); + assertTrue("Figure " + entry.getKey() + " should reference uri " + ROTATIONICON_SVG + position + " instead uri is " + figureDocumentKey, + figureDocumentKey.endsWith(ROTATIONICON_SVG + position)); + } + + for (Entry entry : dDiagramElementToPositionConstantFourImagesSVG.entrySet()) { + String elementId = entry.getKey(); + int position = entry.getValue(); + + WorkspaceImageEditPart editPart = getImageEditPart(ddiagram, elementId); + assertNotNull(editPart); + Rotative4ImagesSVGWorkspaceImageFigure figure = (Rotative4ImagesSVGWorkspaceImageFigure) editPart.getContentPane(); + assertNotNull(figure); + + // Check for SVGImage DocumentKey + String figureDocumentKey = figure.getDocumentKey(); + String postfix = buildExpectedImagePostfix(position, SVG); + assertTrue("Figure " + entry.getKey() + " should reference uri containing " + FOURIMAGESICON_PREFIX + postfix + " instead uri is " + figureDocumentKey, + figureDocumentKey.endsWith(FOURIMAGESICON_PREFIX + postfix)); + } + } + + + /** + * Ensure Rotative and 4Images WorkspaceImages reference expected PNG images + */ + @SuppressWarnings("restriction") + public void testPNGDiagramImages() { + String repId = ID_REPRESENTATION_DESCRIPTOR_PNG; + DDiagram ddiagram = getDDiagramFromId(repId); + DialectEditor editor = getEditor(ddiagram); + + // Ensure displayed images rely on the correct rotated image + for (Entry entry : dDiagramElementToPositionConstantRotationPNG.entrySet()) { + String elementId = entry.getKey(); + int position = entry.getValue(); + + // Get the EditPart and the Figure + WorkspaceImageEditPart editPart = getImageEditPart(ddiagram, elementId); + assertNotNull(editPart); + RotativeWorkspaceImageFigure figure = (RotativeWorkspaceImageFigure) editPart.getContentPane(); + assertNotNull(figure); + + Image nonRotatedImage = getImageFromPath(ROTATIONICON_PNG, PNG); + assertNotNull(nonRotatedImage); + compareImages(figure, nonRotatedImage, position); + } + + // Ensure displayed images rely on the correct 4Images + for (Entry entry : dDiagramElementToPositionConstantFourImagesPNG.entrySet()) { + String elementId = entry.getKey(); + int position = entry.getValue(); + + // Get the EditPart and the Figure + WorkspaceImageEditPart editPart = getImageEditPart(ddiagram, elementId); + assertNotNull(editPart); + RotativeWorkspaceImageFigure figure = (RotativeWorkspaceImageFigure) editPart.getContentPane(); + assertNotNull(figure); + + // Get expected image + Image expectedImage = getImageFromPath(FOURIMAGESICON_PREFIX + buildExpectedImagePostfix(position, PNG), PNG); + assertNotNull(expectedImage); + compareImages(figure, expectedImage, PositionConstants.NORTH); + } + } + + /** + * Ensure 4Images WorkspaceImages reference default WorkspaceImage (the one defined in the odesign) when no + * postfixed 4image exists (_top, ...) + */ + public void testFaultyRotativeDiagramImages() { + String repId = ID_REPRESENTATION_DESCRIPTOR_FAULTY_4IMAGES; + DDiagram ddiagram = getDDiagramFromId(repId); + DialectEditor editor = getEditor(ddiagram); + for (Entry entry : dDiagramElementsToFileExtensionForFauly4Images.entrySet()) { + String elementId = entry.getKey(); + String extension = entry.getValue(); + + IFigure figure = getFigure(ddiagram, elementId); + + if (figure instanceof RotativeWorkspaceImageFigure) { + // Get expected image + Image expectedImage = getImageNotFoundImage(); + assertNotNull(expectedImage); + compareImages((RotativeWorkspaceImageFigure) figure, expectedImage, PositionConstants.NORTH); + } else { + String figureDocumentKey = ((Rotative4ImagesSVGWorkspaceImageFigure) figure).getDocumentKey(); + assertTrue("Figure " + entry.getKey() + " should reference uri " + FOURIMAGESICON_ERROR + extension + " instead uri is " + figureDocumentKey, + figureDocumentKey.endsWith(FOURIMAGESICON_ERROR + extension)); + } + } + } + + private DialectEditor getEditor(DDiagram ddiagram) { + DialectEditor editor = (DialectEditor) DialectUIManager.INSTANCE.openEditor(session, ddiagram, new NullProgressMonitor()); + TestsUtil.synchronizationWithUIThread(); + DialectUIManager.INSTANCE.refreshEditor(editor, new NullProgressMonitor()); + TestsUtil.synchronizationWithUIThread(); + return editor; + } + + private Image getImageFromPath(String path, String extension) { + if (extension.equals(SVG)) { + return DiagramUIPlugin.getPlugin().getBundledImage(path); + } else { + final File imageFile = WorkspaceFileResourceChangeListener.getInstance().getFileFromURI(path); + ImageDescriptor desc = null; + if (imageFile != null && WorkspaceFileResourceChangeListener.getInstance().getReadStatusOfFile(imageFile)) { + try { + desc = WorkspaceFileResourceChangeListener.getInstance().findImageDescriptor(imageFile); + } catch (MalformedURLException e) { + // do nothing + } + } + if (desc != null) { + return DiagramUIPlugin.getPlugin().getImage(desc); + } else { + return getImageNotFoundImage(); + } + } + } + + private Image getImageNotFoundImage() { + return DiagramUIPlugin.getPlugin().getImage(DiagramUIPlugin.Implementation.findImageWithDimensionDescriptor(DiagramImagesPath.IMAGE_NOT_FOUND)); + } + + private IFigure getFigure(DDiagram ddiagram, String elementId) { + WorkspaceImageEditPart editPart = getImageEditPart(ddiagram, elementId); + assertNotNull(editPart); + IFigure figure = editPart.getContentPane(); + assertNotNull(figure); + return figure; + } + + private DDiagram getDDiagramFromId(String repId) { + DRepresentationDescriptor desc = getRepresentationDescriptor(session, repId); + DDiagram ddiagram = (DDiagram) desc.getRepresentation(); + + return ddiagram; + } + + private String buildExpectedImagePostfix(int position, String extension) { + String postfix = "_"; + switch (position) { + case PositionConstants.NORTH: + postfix += TOP; + break; + case PositionConstants.EAST: + postfix += RIGHT; + break; + case PositionConstants.WEST: + postfix += LEFT; + break; + case PositionConstants.SOUTH: + postfix += BOTTOM; + break; + } + postfix += extension; + return postfix; + } + + /** + * Compare images through ImageData comparison. expectedImage may be rotated according to the rotation parameter + * whose values should be among: + *
    + *
  • PositionConstants.NORTH: No rotation
  • + *
  • PositionConstants.EAST: 90� rotation to the right
  • + *
  • PositionConstants.WEST: 90� rotation to the left
  • + *
  • PositionConstants.SOUTH: 180� rotation
  • + *
+ * + * @param figure + * @param nonRotatedOriginalImage + * @param rotation + * The rotation to apply to expectedImage + */ + private void compareImages(RotativeWorkspaceImageFigure figure, Image nonRotatedOriginalImage, int rotation) { + ImageData expectedImageData = nonRotatedOriginalImage.getImageData(); + ImageData actualImageData = figure.getImage().getImageData(); + compareImageData(rotation, expectedImageData, actualImageData); + } + + private void compareImageData(int rotation, ImageData expectedImageData, ImageData actualImageData) { + for (int x = 0; x < expectedImageData.width; x++) { + for (int y = 0; y < expectedImageData.height; y++) { + int expectedX = x; + int expectedY = y; + + switch (rotation) { + case PositionConstants.WEST: // left 90 degrees + expectedX = y; + expectedY = actualImageData.width - x - 1; + break; + case PositionConstants.EAST: // right 90 degrees + expectedX = actualImageData.height - y - 1; + expectedY = x; + break; + case PositionConstants.SOUTH: // 180 degrees + expectedX = actualImageData.width - x - 1; + expectedY = actualImageData.height - y - 1; + break; + } + // The tested image pixel at position rotated matches the original non rotated image + assertEquals(expectedImageData.getPixel(x, y), actualImageData.getPixel(expectedX, expectedY)); + assertEquals(expectedImageData.getAlpha(x, y), actualImageData.getAlpha(expectedX, expectedY)); + } + } + } + + @SuppressWarnings({ "unchecked", "restriction" }) + private WorkspaceImageEditPart getImageEditPart(DDiagram ddiagram, String elementId) { + List diagramElementsFromLabel = getDiagramElementsFromUid(ddiagram, elementId, DDiagramElement.class); + for (DDiagramElement dDiagramElement : diagramElementsFromLabel) { + IGraphicalEditPart editPart = getEditPart(dDiagramElement); + if (editPart instanceof DNode2EditPart) { + List childrens = (List) editPart.getChildren().stream().filter(WorkspaceImageEditPart.class::isInstance).collect(Collectors.toList()); + assertNotNull(childrens); + assertEquals(1, childrens.size()); + return childrens.get(0); + } + + } + return null; + } + + private List getDiagramElementsFromUid(final DDiagram diagram, final String uid, final Class searchedClass) { + final List found = new ArrayList<>(); + final Iterator it = diagram.eAllContents(); + while (it.hasNext()) { + final EObject cur = it.next(); + if (searchedClass.isInstance(cur) && (uidFeature(cur.eClass()) != null) && uid.equals(getUidValue(cur))) { + found.add(searchedClass.cast(cur)); + } + } + return found; + } + + private String getUidValue(final EObject cur) { + return (String) cur.eGet(uidFeature(cur.eClass())); + } + + private EStructuralFeature uidFeature(final EClass class1) { + return class1.getEStructuralFeature("uid"); + } + + public DRepresentationDescriptor getRepresentationDescriptor(Session session, String id) { + Collection representationDescriptors = DialectManager.INSTANCE.getAllRepresentationDescriptors(session); + for (DRepresentationDescriptor representationDescriptor : representationDescriptors) { + + String descriptorFragment; + try { + descriptorFragment = representationDescriptor.getRepPath().getResourceURI().fragment(); + } catch (NullPointerException e) { + descriptorFragment = ""; + } + + String descriptorUid = representationDescriptor.getUid(); + + if (id.equals(descriptorFragment) || id.equals(descriptorUid)) { + return representationDescriptor; + } + } + return null; + } + + @Override + protected void tearDown() throws Exception { + doCleanup(); + super.tearDown(); + } + + private void doCleanup() { + final IEditingSession sessionUI = SessionUIManager.INSTANCE.getUISession(session); + if (sessionUI != null) { + SessionUIManager.INSTANCE.remove(sessionUI); + sessionUI.close(); + TestsUtil.emptyEventsFromUIThread(); + } + if (session != null) { + doRemoveSession(); + doCloseSession(); + session = null; + } + viewpoints.clear(); + Collection editors = Stream.of(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()).map(x -> x.getEditor(false)).filter(Objects::nonNull).filter(DialectEditor.class::isInstance).map(DialectEditor.class::cast).collect(Collectors.toSet()); + editors.forEach(e -> DialectUIManager.INSTANCE.closeEditor(e, false)); + TestsUtil.emptyEventsFromUIThread(); + } + + private void doCloseSession() { + session.close(new NullProgressMonitor()); + Assert.assertFalse("Can't close the session", session.isOpen()); + } + + private void doRemoveSession() { + SessionManager.INSTANCE.remove(session); + for (final Session session2 : SessionManager.INSTANCE.getSessions()) { + Assert.assertFalse("Remove failed", session2.equals(session)); + } + } + +} diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/RotativeImageMovementTest.java b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/RotativeImageMovementTest.java index d99e943cc..d08743cfa 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/RotativeImageMovementTest.java +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testcases/RotativeImageMovementTest.java @@ -1,212 +1,244 @@ -/******************************************************************************* - * Copyright (c) 2021 Thales Global Services S.A.S. - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Thales Global Services S.A.S - initial API and implementation - *******************************************************************************/ -package org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.stream.Collectors; - -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.emf.ecore.EClass; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.EStructuralFeature; -import org.eclipse.gmf.runtime.diagram.ui.actions.ActionIds; -import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; -import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor; -import org.eclipse.gmf.runtime.diagram.ui.requests.ArrangeRequest; -import org.eclipse.sirius.business.api.dialect.DialectManager; -import org.eclipse.sirius.business.api.session.Session; -import org.eclipse.sirius.business.api.session.SessionManager; -import org.eclipse.sirius.common.tools.api.util.CommandStackUtil; -import org.eclipse.sirius.diagram.DDiagram; -import org.eclipse.sirius.diagram.DDiagramElement; -import org.eclipse.sirius.diagram.ui.internal.edit.parts.DNode2EditPart; -import org.eclipse.sirius.diagram.ui.internal.edit.parts.WorkspaceImageEditPart; -import org.eclipse.sirius.tests.support.api.SiriusDiagramTestCase; -import org.eclipse.sirius.tests.support.api.TestsUtil; -import org.eclipse.sirius.ui.business.api.dialect.DialectEditor; -import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager; -import org.eclipse.sirius.ui.business.api.session.IEditingSession; -import org.eclipse.sirius.ui.business.api.session.SessionUIManager; -import org.eclipse.sirius.viewpoint.DRepresentationDescriptor; -import org.junit.Assert; -import org.polarsys.kitalpha.sirius.rotativeimage.figures.Rotative4ImagesSVGWorkspaceImageFigure; - -/** - * Checks that displayed images in rotative bordered node move when node changes side of parent - * - * @author Arnaud Dieumegard - */ -public class RotativeImageMovementTest extends SiriusDiagramTestCase { - - protected static final String PLATFORM_PLUGIN_PATH = "platform:/plugin/"; - - protected static final String ROTATIVEIMAGE_TEST_PLUGIN_NAME = "/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests"; - - private static final String TEST_XMI_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.xmi"; - - private static final String TEST_AIRD_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.aird"; - - private static final String TEST_ODESIGN_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/description/test.odesign"; - - private static final String ID_REPRESENTATION_DESCRIPTOR_SVG = "_idds4E1NEeySgagIY4HK9g"; - - private static final String SVG = ".svg"; - - private static final String FOURIMAGESICON_PREFIX = "/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon"; - - private static final String BOTTOM = "bottom"; - - private static final String LEFT = "left"; - - private static final String BOTTOM_E1_BORDERNODE_ID = "_ifBMEE1NEeySgagIY4HK9g"; - - @Override - protected void setUp() throws Exception { - super.setUp(); - genericSetUp(TEST_XMI_PATH, TEST_ODESIGN_PATH, TEST_AIRD_PATH); - } - - /** - * Ensure Rotative and 4Images WorkspaceImages reference expected SVG images - */ - @SuppressWarnings("restriction") - public void testMoveImages() { - String repId = ID_REPRESENTATION_DESCRIPTOR_SVG; - DRepresentationDescriptor desc = getRepresentationDescriptor(session, repId); - DDiagram ddiagram = (DDiagram) desc.getRepresentation(); - - DialectEditor editor = (DialectEditor) DialectUIManager.INSTANCE.openEditor(session, ddiagram, new NullProgressMonitor()); - TestsUtil.synchronizationWithUIThread(); - DialectUIManager.INSTANCE.refreshEditor(editor, new NullProgressMonitor()); - TestsUtil.synchronizationWithUIThread(); - - List diagramElementsFromLabel = getDiagramElementsFromUid(ddiagram, BOTTOM_E1_BORDERNODE_ID, DDiagramElement.class); - for (DDiagramElement dDiagramElement : diagramElementsFromLabel) { - IGraphicalEditPart editPart = getEditPart(dDiagramElement); - if (editPart instanceof DNode2EditPart) { - List childrens = (List) editPart.getChildren().stream().filter(WorkspaceImageEditPart.class::isInstance).collect(Collectors.toList()); - assertNotNull(childrens); - assertEquals(1, childrens.size()); - - WorkspaceImageEditPart bottomEditPart = childrens.get(0); - assertNotNull(bottomEditPart); - Rotative4ImagesSVGWorkspaceImageFigure bottomFigure = (Rotative4ImagesSVGWorkspaceImageFigure) bottomEditPart.getContentPane(); - assertNotNull(bottomFigure); - - // Check for SVGImage DocumentKey - String figureDocumentKey = bottomFigure.getDocumentKey(); - assertTrue("Figure should reference uri " + FOURIMAGESICON_PREFIX + "_" + BOTTOM + SVG + " instead uri is " + figureDocumentKey, - figureDocumentKey.endsWith(FOURIMAGESICON_PREFIX + "_" + BOTTOM + SVG)); - - // Arrange all in diagram - performArrangeAll(ddiagram, (DiagramEditor) editor); - - // Check new position - figureDocumentKey = bottomFigure.getDocumentKey(); - assertTrue("Figure should reference uri " + FOURIMAGESICON_PREFIX + "_" + LEFT + SVG + " instead uri is " + figureDocumentKey, - figureDocumentKey.endsWith(FOURIMAGESICON_PREFIX + "_" + LEFT + SVG)); - - } - } - - } - - private void performArrangeAll(DDiagram ddiagram, DiagramEditor editor) { - final ArrangeRequest request = new ArrangeRequest(ActionIds.ACTION_ARRANGE_ALL); - request.setPartsToArrange(Arrays.asList(editor.getDiagramEditPart())); - editor.getDiagramEditPart().refresh(); - editor.getDiagramEditPart().performRequest(request); - CommandStackUtil.flushOperations(editor.getEditingDomain().getCommandStack()); - editor.getDiagramEditPart().getRoot().getViewer().flush(); - TestsUtil.synchronizationWithUIThread(); - } - - private List getDiagramElementsFromUid(final DDiagram diagram, final String uid, final Class searchedClass) { - final List found = new ArrayList<>(); - final Iterator it = diagram.eAllContents(); - while (it.hasNext()) { - final EObject cur = it.next(); - if (searchedClass.isInstance(cur) && uidFeature(cur.eClass()) != null) { - if (uid.equals(getUidValue(cur))) { - found.add(searchedClass.cast(cur)); - } - } - } - return found; - } - - private String getUidValue(final EObject cur) { - return (String) cur.eGet(uidFeature(cur.eClass())); - } - - private EStructuralFeature uidFeature(final EClass class1) { - return class1.getEStructuralFeature("uid"); - } - - public DRepresentationDescriptor getRepresentationDescriptor(Session session, String id) { - Collection representationDescriptors = DialectManager.INSTANCE.getAllRepresentationDescriptors(session); - for (DRepresentationDescriptor representationDescriptor : representationDescriptors) { - - String descriptorFragment; - try { - descriptorFragment = representationDescriptor.getRepPath().getResourceURI().fragment(); - } catch (NullPointerException e) { - descriptorFragment = ""; - } - - String descriptorUid = representationDescriptor.getUid(); - - if (id.equals(descriptorFragment) || id.equals(descriptorUid)) { - return representationDescriptor; - } - } - return null; - } - - @Override - protected void tearDown() throws Exception { - doCleanup(); - super.tearDown(); - } - - private void doCleanup() { - final IEditingSession sessionUI = SessionUIManager.INSTANCE.getUISession(session); - if (sessionUI != null) { - SessionUIManager.INSTANCE.remove(sessionUI); - sessionUI.close(); - TestsUtil.emptyEventsFromUIThread(); - } - if (session != null) { - doRemoveSession(); - doCloseSession(); - session = null; - } - viewpoints.clear(); - } - - private void doCloseSession() { - session.close(new NullProgressMonitor()); - Assert.assertFalse("Can't close the session", session.isOpen()); - } - - private void doRemoveSession() { - SessionManager.INSTANCE.remove(session); - for (final Session session2 : SessionManager.INSTANCE.getSessions()) { - Assert.assertFalse("Remove failed", session2.equals(session)); - } - } - -} +/******************************************************************************* + * Copyright (c) 2021 Thales Global Services S.A.S. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Thales Global Services S.A.S - initial API and implementation + *******************************************************************************/ +package org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.transaction.RecordingCommand; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.emf.transaction.util.TransactionUtil; +import org.eclipse.gmf.runtime.diagram.ui.actions.ActionIds; +import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; +import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor; +import org.eclipse.gmf.runtime.diagram.ui.requests.ArrangeRequest; +import org.eclipse.sirius.business.api.dialect.DialectManager; +import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.business.api.session.SessionManager; +import org.eclipse.sirius.common.tools.api.util.CommandStackUtil; +import org.eclipse.sirius.diagram.DDiagram; +import org.eclipse.sirius.diagram.DDiagramElement; +import org.eclipse.sirius.diagram.ui.internal.edit.parts.DNode2EditPart; +import org.eclipse.sirius.diagram.ui.internal.edit.parts.WorkspaceImageEditPart; +import org.eclipse.sirius.tests.support.api.SiriusDiagramTestCase; +import org.eclipse.sirius.tests.support.api.TestsUtil; +import org.eclipse.sirius.ui.business.api.dialect.DialectEditor; +import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager; +import org.eclipse.sirius.ui.business.api.session.IEditingSession; +import org.eclipse.sirius.ui.business.api.session.SessionUIManager; +import org.eclipse.sirius.viewpoint.DRepresentationDescriptor; +import org.eclipse.swt.graphics.Resource; +import org.eclipse.ui.PlatformUI; +import org.junit.Assert; +import org.polarsys.kitalpha.sirius.rotativeimage.figures.Rotative4ImagesSVGWorkspaceImageFigure; + +/** + * Checks that displayed images in rotative bordered node move when node changes side of parent + * + * @author Arnaud Dieumegard + */ +public class RotativeImageMovementTest extends SiriusDiagramTestCase { + + protected static final String PLATFORM_PLUGIN_PATH = "platform:/plugin/"; + + protected static final String ROTATIVEIMAGE_TEST_PLUGIN_NAME = "/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests"; + + private static final String TEST_XMI_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.xmi"; + + private static final String TEST_AIRD_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/models/Test1.aird"; + + private static final String TEST_ODESIGN_PATH = ROTATIVEIMAGE_TEST_PLUGIN_NAME + "/description/test.odesign"; + + private static final String ID_REPRESENTATION_DESCRIPTOR_SVG = "_idds4E1NEeySgagIY4HK9g"; + + private static final String SVG = ".svg"; + + private static final String FOURIMAGESICON_PREFIX = "/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/icons/4imagesIcon"; + + private static final String BOTTOM = "bottom"; + + private static final String LEFT = "left"; + + private static final String BOTTOM_E1_BORDERNODE_ID = "_ifBMEE1NEeySgagIY4HK9g"; + + @Override + protected void setUp() throws Exception { + super.setUp(); + Resource.setNonDisposeHandler(null); + genericSetUp(TEST_XMI_PATH, TEST_ODESIGN_PATH, TEST_AIRD_PATH); + } + + /** + * Ensure Rotative and 4Images WorkspaceImages reference expected SVG images + */ + @SuppressWarnings("restriction") + public void testMoveImages() { + String repId = ID_REPRESENTATION_DESCRIPTOR_SVG; + DRepresentationDescriptor desc = getRepresentationDescriptor(session, repId); + DDiagram ddiagram = (DDiagram) desc.getRepresentation(); + + DialectEditor editor = (DialectEditor) DialectUIManager.INSTANCE.openEditor(session, ddiagram, new NullProgressMonitor()); + TestsUtil.synchronizationWithUIThread(); + DialectUIManager.INSTANCE.refreshEditor(editor, new NullProgressMonitor()); + TestsUtil.synchronizationWithUIThread(); + + List diagramElementsFromLabel = getDiagramElementsFromUid(ddiagram, BOTTOM_E1_BORDERNODE_ID, DDiagramElement.class); + assertTrue(diagramElementsFromLabel.size() == 1); + DDiagramElement port = diagramElementsFromLabel.iterator().next(); + + IGraphicalEditPart editPart = getEditPart(port); + if (editPart instanceof DNode2EditPart) { + + // Check for SVGImage DocumentKey + Rotative4ImagesSVGWorkspaceImageFigure bottomFigure = getFigure(editPart); + String figureDocumentKey = bottomFigure.getDocumentKey(); + assertTrue("Figure should reference uri " + FOURIMAGESICON_PREFIX + "_" + BOTTOM + SVG + " instead uri is " + + figureDocumentKey, figureDocumentKey.endsWith(FOURIMAGESICON_PREFIX + "_" + BOTTOM + SVG)); + + // We change the name of a port. After an Arrange all, it will move to west according to conditional style. + setName(port, "westSideOnly"); + performArrangeAll(ddiagram, ((DiagramEditor) editor)); + TestsUtil.synchronizationWithUIThread(); + + // Check new position + bottomFigure = getFigure(editPart); + figureDocumentKey = bottomFigure.getDocumentKey(); + assertTrue("Figure should reference uri " + FOURIMAGESICON_PREFIX + "_" + LEFT + SVG + " instead uri is " + + figureDocumentKey, figureDocumentKey.endsWith(FOURIMAGESICON_PREFIX + "_" + LEFT + SVG)); + } + + } + + private Rotative4ImagesSVGWorkspaceImageFigure getFigure(IGraphicalEditPart editPart) { + List childrens = (List) editPart.getChildren().stream() + .filter(WorkspaceImageEditPart.class::isInstance).collect(Collectors.toList()); + assertNotNull(childrens); + assertEquals(1, childrens.size()); + + WorkspaceImageEditPart bottomEditPart = childrens.get(0); + assertNotNull(bottomEditPart); + Rotative4ImagesSVGWorkspaceImageFigure bottomFigure = (Rotative4ImagesSVGWorkspaceImageFigure) bottomEditPart + .getContentPane(); + assertNotNull(bottomFigure); + return bottomFigure; + + } + + private void setName(EObject target, String name) { + TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(target); + domain.getCommandStack().execute(new RecordingCommand(domain) { + @Override + protected void doExecute() { + ((DDiagramElement)target).getTarget().eSet(((DDiagramElement)target).getTarget().eClass().getEStructuralFeature("name"), name); + } + }); + } + + private void performArrangeAll(DDiagram ddiagram, DiagramEditor editor) { + final ArrangeRequest request = new ArrangeRequest(ActionIds.ACTION_ARRANGE_ALL); + request.setPartsToArrange(Arrays.asList(editor.getDiagramEditPart())); + editor.getDiagramEditPart().refresh(); + editor.getDiagramEditPart().performRequest(request); + CommandStackUtil.flushOperations(editor.getEditingDomain().getCommandStack()); + editor.getDiagramEditPart().getRoot().getViewer().flush(); + TestsUtil.synchronizationWithUIThread(); + } + + private List getDiagramElementsFromUid(final DDiagram diagram, final String uid, final Class searchedClass) { + final List found = new ArrayList<>(); + final Iterator it = diagram.eAllContents(); + while (it.hasNext()) { + final EObject cur = it.next(); + if (searchedClass.isInstance(cur) && uidFeature(cur.eClass()) != null) { + if (uid.equals(getUidValue(cur))) { + found.add(searchedClass.cast(cur)); + } + } + } + return found; + } + + private String getUidValue(final EObject cur) { + return (String) cur.eGet(uidFeature(cur.eClass())); + } + + private EStructuralFeature uidFeature(final EClass class1) { + return class1.getEStructuralFeature("uid"); + } + + public DRepresentationDescriptor getRepresentationDescriptor(Session session, String id) { + Collection representationDescriptors = DialectManager.INSTANCE.getAllRepresentationDescriptors(session); + for (DRepresentationDescriptor representationDescriptor : representationDescriptors) { + + String descriptorFragment; + try { + descriptorFragment = representationDescriptor.getRepPath().getResourceURI().fragment(); + } catch (NullPointerException e) { + descriptorFragment = ""; + } + + String descriptorUid = representationDescriptor.getUid(); + + if (id.equals(descriptorFragment) || id.equals(descriptorUid)) { + return representationDescriptor; + } + } + return null; + } + + @Override + protected void tearDown() throws Exception { + doCleanup(); + super.tearDown(); + Collection editors = Stream.of(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()).map(x -> x.getEditor(false)).filter(Objects::nonNull).filter(DialectEditor.class::isInstance).map(DialectEditor.class::cast).collect(Collectors.toSet()); + editors.forEach(e -> DialectUIManager.INSTANCE.closeEditor(e, false)); + TestsUtil.emptyEventsFromUIThread(); + } + + private void doCleanup() { + final IEditingSession sessionUI = SessionUIManager.INSTANCE.getUISession(session); + if (sessionUI != null) { + SessionUIManager.INSTANCE.remove(sessionUI); + sessionUI.close(); + TestsUtil.emptyEventsFromUIThread(); + } + if (session != null) { + doRemoveSession(); + doCloseSession(); + session = null; + } + viewpoints.clear(); + } + + private void doCloseSession() { + session.close(new NullProgressMonitor()); + Assert.assertFalse("Can't close the session", session.isOpen()); + } + + private void doRemoveSession() { + SessionManager.INSTANCE.remove(session); + for (final Session session2 : SessionManager.INSTANCE.getSessions()) { + Assert.assertFalse("Remove failed", session2.equals(session)); + } + } + +} diff --git a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testsuite/RotativeImageTestSuite.java b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testsuite/RotativeImageTestSuite.java index 8609fd6e4..40db09dbf 100644 --- a/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testsuite/RotativeImageTestSuite.java +++ b/sirius/tests/plugins/org.polarsys.kitalpha.sirius.rotativeimage.junit.tests/src/org/polarsys/kitalpha/sirius/rotativeimage/junit/tests/testsuite/RotativeImageTestSuite.java @@ -1,30 +1,30 @@ -/******************************************************************************* - * Copyright (c) 2021 Thales Global Services S.A.S. - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Thales Global Services S.A.S - initial API and implementation - *******************************************************************************/ - -package org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testsuite; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; -import org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases.ExtensionPointTest; -import org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases.LoadSessionWithExtensionTest; -import org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases.RotativeImageDisplayTest; -import org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases.RotativeImageMovementTest; - -/** - * @author Arnaud Dieumegard - */ -@RunWith(Suite.class) -@SuiteClasses({ ExtensionPointTest.class, LoadSessionWithExtensionTest.class, RotativeImageDisplayTest.class, RotativeImageMovementTest.class }) -public class RotativeImageTestSuite { - -} +/******************************************************************************* + * Copyright (c) 2021 Thales Global Services S.A.S. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Thales Global Services S.A.S - initial API and implementation + *******************************************************************************/ + +package org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testsuite; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; +import org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases.ExtensionPointTest; +import org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases.LoadSessionWithExtensionTest; +import org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases.RotativeImageDisplayTest; +import org.polarsys.kitalpha.sirius.rotativeimage.junit.tests.testcases.RotativeImageMovementTest; + +/** + * @author Arnaud Dieumegard + */ +@RunWith(Suite.class) +@SuiteClasses({ RotativeImageDisplayTest.class, RotativeImageMovementTest.class, ExtensionPointTest.class, LoadSessionWithExtensionTest.class }) +public class RotativeImageTestSuite { + +}