From 7431c59e218960d40b952e5813ec6f3760e164bb Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Sun, 13 Dec 2015 22:03:48 -0500 Subject: [PATCH] tmf: FilterColorEditorTest on Mac On Mac, RGB values that are captured with ImageHelper are affected by monitor color profiles. To account for this, we can draw the expected color in a simple shell and use that color as expected value instead. Also, switching to the Filter mode in the table is not instantaneous, we need to wait until it's done before capturing the next image. Change-Id: I930570027ba53f3dbdcecb5925a8e67c24c239b2 Signed-off-by: Marc-Andre Laperle Reviewed-on: https://git.eclipse.org/r/62573 Reviewed-by: Hudson CI Reviewed-by: Matthew Khouzam Tested-by: Matthew Khouzam --- .../ui/swtbot/tests/shared/ImageHelper.java | 68 +++++++++++++++++++ .../viewers/events/FilterColorEditorTest.java | 20 ++++-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ImageHelper.java b/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ImageHelper.java index 33763d0484..102625f415 100644 --- a/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ImageHelper.java +++ b/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/ImageHelper.java @@ -20,11 +20,19 @@ import java.util.List; import javax.imageio.ImageIO; +import org.eclipse.swt.events.PaintEvent; +import org.eclipse.swt.events.PaintListener; +import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swtbot.swt.finder.SWTBot; import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; import org.eclipse.swtbot.swt.finder.results.Result; +import org.eclipse.swtbot.swt.finder.utils.SWTUtils; +import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; @@ -213,4 +221,64 @@ public final class ImageHelper { private static RGB getRgbFromRGBPixel(int pixel) { return new RGB(((pixel >> 16) & 0xff), ((pixel >> 8) & 0xff), ((pixel) & 0xff)); } + + /** + * On Mac, RGB values that are captured with ImageHelper are affected by + * monitor color profiles. To account for this, we can draw the expected + * color in a simple shell and use that color as expected value instead. + * + * @param original + * original color to adjust + * @return adjusted color + */ + public static RGB adjustExpectedColor(RGB original) { + if (!SWTUtils.isMac()) { + return original; + } + + /* Create shell with desired color as background */ + boolean painted[] = new boolean[1]; + final Shell shell = UIThreadRunnable.syncExec(new Result() { + @Override + public Shell run() { + Shell s = new Shell(Display.getDefault()); + s.setSize(100, 100); + Color color = new Color(Display.getDefault(), original); + s.setBackground(color); + s.addPaintListener(new PaintListener() { + @Override + public void paintControl(PaintEvent e) { + painted[0] = true; + } + }); + s.open(); + return s; + } + }); + + /* Make sure the shell has been painted before getting the color */ + new SWTBot().waitUntil(new DefaultCondition() { + + @Override + public boolean test() throws Exception { + return painted[0]; + } + + @Override + public String getFailureMessage() { + return "Shell was not painted"; + } + }); + + /* Get the color */ + return UIThreadRunnable.syncExec(new Result() { + @Override + public RGB run() { + shell.update(); + RGB rgb = ImageHelper.grabImage(shell.getBounds()).getPixel(50, 50); + shell.close(); + return rgb; + } + }); + } } diff --git a/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/FilterColorEditorTest.java b/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/FilterColorEditorTest.java index e57e998a46..08eb62c902 100644 --- a/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/FilterColorEditorTest.java +++ b/tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/FilterColorEditorTest.java @@ -42,6 +42,7 @@ import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin; +import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers; import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ImageHelper; import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; import org.eclipse.ui.PlatformUI; @@ -78,9 +79,11 @@ public class FilterColorEditorTest { private static final Logger fLogger = Logger.getRootLogger(); private SWTBotTable fTableBot; private static final int ROW = 8; + /** Expected color values */ private RGB fForeground; private RGB fBackground; - private RGB fHighlight; + private static RGB fHighlight; + private static RGB EXPECTED_GREEN; /** * Test Class setup @@ -117,6 +120,10 @@ public class FilterColorEditorTest { /* Finish waiting for eclipse to load */ SWTBotUtils.waitForJobs(); + + ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry(); + fHighlight = ImageHelper.adjustExpectedColor(colorRegistry.get(HIGHLIGHT_COLOR_DEFINITION_ID).getRGB()); + EXPECTED_GREEN = ImageHelper.adjustExpectedColor(GREEN); } /** @@ -141,8 +148,6 @@ public class FilterColorEditorTest { fTableBot = editorBot.bot().table(); fBackground = fTableBot.backgroundColor().getRGB(); fForeground = fTableBot.foregroundColor().getRGB(); - ColorRegistry colorRegistry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry(); - fHighlight = colorRegistry.get(HIGHLIGHT_COLOR_DEFINITION_ID).getRGB(); SWTBotUtils.maximizeTable(fTableBot); } @@ -248,6 +253,9 @@ public class FilterColorEditorTest { ImageHelper after = ImageHelper.grabImage(cellBounds); // toggle filter mode fTableBot.click(0, 0); + fBot.waitUntil(ConditionHelpers.isTableCellFilled(fTableBot, "", 0, 1)); + //TODO: We need a better way to make sure that the table is done updating + SWTBotUtils.delay(2000); ImageHelper afterFilter = ImageHelper.grabImage(cellBounds); List beforeLine = before.getPixelRow(2); @@ -295,18 +303,18 @@ public class FilterColorEditorTest { assertTrue(colorBefore.contains(fBackground)); assertTrue(colorBefore.contains(fForeground)); assertFalse(colorBefore.contains(fHighlight)); - assertFalse(colorBefore.contains(GREEN)); + assertFalse(colorBefore.contains(EXPECTED_GREEN)); assertTrue(colorAfter.contains(fBackground)); assertTrue(colorAfter.contains(fForeground)); assertFalse(colorAfter.contains(fHighlight)); - assertTrue(colorAfter.contains(GREEN)); + assertTrue(colorAfter.contains(EXPECTED_GREEN)); /* * Check that some background became green. */ assertTrue(colorAfter.count(fBackground) < colorBefore.count(fBackground)); - assertTrue(colorAfter.count(GREEN) > colorBefore.count(GREEN)); + assertTrue(colorAfter.count(EXPECTED_GREEN) > colorBefore.count(EXPECTED_GREEN)); // reset the highlight color preference colorRegistry.put(HIGHLIGHT_COLOR_DEFINITION_ID, fHighlight); -- 2.34.1