diff options
Diffstat (limited to 'org.eclipse.tips.tests/src/org')
15 files changed, 1605 insertions, 0 deletions
diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/core/JsonTestProvider.java b/org.eclipse.tips.tests/src/org/eclipse/tips/core/JsonTestProvider.java new file mode 100644 index 000000000..8c5829a26 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/core/JsonTestProvider.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.core; + +import java.net.MalformedURLException; + +import org.eclipse.tips.json.JsonTipProvider; + +public class JsonTestProvider extends JsonTipProvider { + + public JsonTestProvider() throws MalformedURLException { + setJsonUrl("https://raw.githubusercontent.com/wimjongman/jsontips/master/photon/m3tips.json"); + } + + @Override + public String getID() { + return getClass().getName(); + } +} diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/core/TestTip.java b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TestTip.java new file mode 100644 index 000000000..8c0b50b84 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TestTip.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.core; + +import java.util.Date; + +import org.eclipse.tips.ui.internal.util.DateUtil; + +public class TestTip extends Tip implements IHtmlTip { + + private String fSubject; + private String fHTML; + + public TestTip(String providerId, String html, String subject) { + super(providerId); + fHTML = html; + fSubject = subject; + } + + @Override + public Date getCreationDate() { + return DateUtil.getDateFromYYMMDD("31/12/1964"); + } + + @Override + public String getHTML() { + return fHTML; + } + + @Override + public String getSubject() { + return fSubject; + } + + @Override + public TipImage getImage() { + return null; + } +} diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/core/TestTipManager.java b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TestTipManager.java new file mode 100644 index 000000000..59ab3f1d4 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TestTipManager.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.core; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; + +public class TestTipManager extends TipManager { + + private boolean fShouldRun = true; + private List<Integer> fReadList = new ArrayList<>(); + + @Override + public TipManager setRunAtStartup(boolean shouldRun) { + fShouldRun = shouldRun; + return this; + } + + @Override + public boolean isRunAtStartup() { + return fShouldRun; + } + + @Override + public ITipManager register(TipProvider provider) { + super.register(provider); + load(provider); + return this; + } + + private void load(TipProvider provider) { + provider.loadNewTips(new NullProgressMonitor()); + } + + @Override + public boolean isRead(Tip tip) { + return fReadList.contains(tip.hashCode()); + } + + @Override + public TipManager setAsRead(Tip tip) { + fReadList.remove((Integer) tip.hashCode()); + fReadList.add(tip.hashCode()); + return this; + } + + @Override + public ITipManager log(IStatus status) { + System.out.println(status.toString()); + return this; + } + + @Override + public TipManager open(boolean startUp) { + return this; + } + + @Override + public int getPriority(TipProvider provider) { + return 20; + } +}
\ No newline at end of file diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/core/TestTipProvider.java b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TestTipProvider.java new file mode 100644 index 000000000..74423bf17 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TestTipProvider.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.core; + +import java.util.Collections; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.tips.ui.internal.util.ImageUtil; +import org.eclipse.tips.ui.internal.util.ResourceManager; + +public class TestTipProvider extends TipProvider { + + private static TipImage image; + + @Override + public String getDescription() { + return "Test Tip Provider"; + } + + @Override + public String getID() { + return getClass().getName(); + } + + @Override + public TipImage getImage() { + if (image == null) { + Image pluginImage = ResourceManager.getPluginImage("org.eclipse.tips.examples", "icons/48/c++.png"); + String base64 = ImageUtil.decodeFromImage(pluginImage, SWT.IMAGE_PNG); + image = new TipImage(base64); + } + return image; + } + + @Override + public IStatus loadNewTips(IProgressMonitor monitor) { + setTips(Collections.emptyList()); + return Status.OK_STATUS; + } + + @Override + public void dispose() { + + } +}
\ No newline at end of file diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageBas64Test.java b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageBas64Test.java new file mode 100644 index 000000000..a1e575f70 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageBas64Test.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.core; + +import static org.junit.Assert.assertTrue; + +import org.eclipse.core.runtime.AssertionFailedException; +import org.junit.Test; + +public class TipImageBas64Test { + + private static final String BASE64 = "data:image/png;base64,thequickbrownfox"; + private static final String BASE64WRONG = "date:image/png;base64,thequickbrownfox"; + private static final String BASE64WRONG2 = "data:image/plip ;base64,thequickbrownfox"; + + private TipImage getTipImage() { + return new TipImage(BASE64); + } + + @Test(expected = AssertionFailedException.class) + public void testAssertHeight() { + new TipImage(BASE64).setAspectRatio(1000, 0, false); + } + + @Test(expected = AssertionFailedException.class) + public void testAssertWidth() { + new TipImage(BASE64).setAspectRatio(0, 100, false); + } + + @Test + public void testSetExtension() { + // assertTrue(getTipImage().getIMGAttributes(19, 10).contains("png")); + } + + @Test + public void testSetExtension2() { + // assertTrue(getTipImage().setExtension("bmp").getBase64Image().contains("bmp")); + // assertTrue(getTipImage().getIMGAttributes(19, 10).contains("png")); + } + + @Test + public void testGetIMGAttributes() { + String result = getTipImage().setAspectRatio(1.5).getIMGAttributes(740, 370).trim(); + assertTrue(result, result.equalsIgnoreCase("width=\"555\" height=\"370\"")); + } + + @Test + public void testGetBase64() { + assertTrue(getTipImage().getBase64Image().equals(BASE64)); + } + + @Test + public void testSetAspectRatioDouble() { + String result = getTipImage().setAspectRatio(1.5).getIMGAttributes(740, 370).trim(); + assertTrue(result, result.equalsIgnoreCase("width=\"555\" height=\"370\"")); + } + + @Test + public void testSetAspectRatioIntIntFalse() { + String result = getTipImage().setAspectRatio(200, 50, false).getIMGAttributes(100, 100).trim(); + assertTrue(result, result.equalsIgnoreCase("width=\"100\" height=\"25\"")); + } + + @Test + public void testSetAspectRatioIntIntTrue() { + String result = getTipImage().setAspectRatio(400, 300, true).getIMGAttributes(740, 370).trim(); + assertTrue(result, result.equalsIgnoreCase("width=\"400\" height=\"300\"")); + } + + @Test + public void testSetMaxHeight() { + String imgAttributes = new TipImage(BASE64).setAspectRatio(2).setMaxHeight(300).getIMGAttributes(200, 200); + assertTrue(imgAttributes, imgAttributes.trim().equalsIgnoreCase("width=\"200\" height=\"100\"")); + } + + @Test + public void testSetMaxWidth() { + String imgAttributes = new TipImage(BASE64).setAspectRatio(1.6).setMaxWidth(200).getIMGAttributes(400, 300); + assertTrue(imgAttributes, imgAttributes.trim().equalsIgnoreCase("width=\"200\" height=\"125\"")); + } + + public void testTipImage() { + new TipImage(BASE64); + } + + @Test + public void testTipImage2() { + getTipImage(); + } + + @Test(expected = RuntimeException.class) + public void testTipImage3() { + new TipImage(BASE64WRONG); + } + + public void testTipImage4() { + TipImage tipImage = new TipImage(BASE64WRONG2); + assertTrue(tipImage.getIMGAttributes(1, 1).contains("plip")); + } +} diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageURLTest.java b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageURLTest.java new file mode 100644 index 000000000..c182d09fe --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipImageURLTest.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.core; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import org.eclipse.core.runtime.AssertionFailedException; +import org.junit.Test; + +public class TipImageURLTest { + + private static final String URL = "http://remainsoftware.com/img.png"; + + @Test(expected = Exception.class) + public void testTipImage() throws IOException { + new TipImage((URL) null); + } + + @Test(expected = MalformedURLException.class) + public void testTipImage3() throws IOException { + new TipImage(new URL("0gl kjfslkfjsl dkfjsldkfjl")); + } + + @Test + public void testTipImage2() throws IOException { + getTipImage(); + } + + private TipImage getTipImage() throws IOException { + return new TipImage(new URL(URL)); + } + + @Test + public void testSetMaxHeight() throws IOException { + String imgAttributes = new TipImage(new URL(URL)).setAspectRatio(2).setMaxHeight(300).getIMGAttributes(200, + 200); + assertTrue(imgAttributes, imgAttributes.trim().equalsIgnoreCase("width=\"200\" height=\"100\"")); + } + + @Test + public void testSetMaxWidth() throws IOException { + String imgAttributes = new TipImage(new URL(URL)).setAspectRatio(1.6).setMaxWidth(200).getIMGAttributes(400, + 300); + assertTrue(imgAttributes, imgAttributes.trim().equalsIgnoreCase("width=\"200\" height=\"125\"")); + + } + + @Test(expected = AssertionFailedException.class) + public void testAssertWidth() throws IOException { + new TipImage(new URL(URL)).setAspectRatio(0, 100, false); + } + + @Test(expected = AssertionFailedException.class) + public void testAssertHeight() throws IOException { + new TipImage(new URL(URL)).setAspectRatio(1000, 0, false); + } + + @Test + public void testSetAspectRatioIntIntFalse() throws IOException { + String result = getTipImage().setAspectRatio(200, 50, false).getIMGAttributes(100, 100).trim(); + assertTrue(result, result.equalsIgnoreCase("width=\"100\" height=\"25\"")); + } + + @Test + public void testSetAspectRatioIntIntTrue() throws IOException { + String result = getTipImage().setAspectRatio(400, 300, true).getIMGAttributes(740, 370).trim(); + assertTrue(result, result.equalsIgnoreCase("width=\"400\" height=\"300\"")); + } + + @Test + public void testSetAspectRatioDouble() throws IOException { + String result = getTipImage().setAspectRatio(1.5).getIMGAttributes(740, 370).trim(); + assertTrue(result, result.equalsIgnoreCase("width=\"555\" height=\"370\"")); + } + + @Test + public void testGetIMGAttributes() throws IOException { + String result = getTipImage().setAspectRatio(1.5).getIMGAttributes(740, 370).trim(); + assertTrue(result, result.equalsIgnoreCase("width=\"555\" height=\"370\"")); + } + + @Test + public void testSetExtension() throws IOException { + // assertTrue(getTipImage().getIMGAttributes(19, 10).contains("png")); + } + + @Test + public void testSetExtension2() throws IOException { + // assertTrue(getTipImage().setExtension("bmp").getBase64Image().contains("bmp")); + // assertTrue(getTipImage().getIMGAttributes(19, 10).contains("png")); + } +} diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipManagerTest.java b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipManagerTest.java new file mode 100644 index 000000000..f9d5c563e --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipManagerTest.java @@ -0,0 +1,179 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.core; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.tips.core.internal.LogUtil; +import org.junit.Before; +import org.junit.Test; + +public class TipManagerTest { + + private TestTipManager fManager; + private TestTipProvider fProvider1; + private TestTipProvider fProvider2; + + @Before + public void testTipManager() { + fManager = new TestTipManager(); + fProvider1 = new TestTipProvider(); + fProvider2 = new TestTipProvider(); + } + + @Test + public void testGetProvider() { + fManager.register(fProvider1); + TipProvider provider = fManager.getProvider(fProvider1.getID()); + assertTrue(provider == fProvider1); + } + + @Test + public void testRegister() { + fManager.register(fProvider1); + TipProvider provider = fManager.getProvider(fProvider1.getID()); + assertTrue(provider == fProvider1); + } + + /** + * Only one provider with the same id can be registered. + */ + @Test + public void testGetProviders() { + fManager.register(fProvider1); + fManager.register(fProvider2); + fManager.register(fProvider2); + assertTrue(fManager.getProviders().size() + "", fManager.getProviders().size() == 1); + } + + @Test + public void testIsRunAtStartup() { + assertTrue(fManager.isRunAtStartup()); + fManager.setRunAtStartup(false); + assertTrue(!fManager.isRunAtStartup()); + } + + @Test + public void testSetRunAtStartup() { + assertTrue(fManager.isRunAtStartup()); + fManager.setRunAtStartup(false); + assertTrue(!fManager.isRunAtStartup()); + } + + @Test + public void testLoad() { + ArrayList<String> test = new ArrayList<>(); + TestTipProvider testTipProvider = new TestTipProvider() { + @Override + public IStatus loadNewTips(IProgressMonitor monitor) { + test.add("fff"); + return Status.OK_STATUS; + } + }; + fManager.register(testTipProvider); + assertTrue(!test.isEmpty()); + } + + @Test + public void testLoad2() { + ArrayList<String> test = new ArrayList<>(); + TestTipProvider testTipProvider = new TestTipProvider() { + @Override + public IStatus loadNewTips(IProgressMonitor monitor) { + test.add("fff"); + return Status.OK_STATUS; + } + }; + fManager.register(testTipProvider); + assertTrue(!test.isEmpty()); + } + + @Test + public void testOpen() { + ArrayList<String> test = new ArrayList<>(); + TestTipManager m = new TestTipManager() { + @Override + public TipManager open(boolean startUp) { + if (startUp) { + test.add("1"); + } else { + test.add("1"); + test.add("2"); + } + return this; + } + }; + m.open(true); + assertTrue(test.size() == 1); + test.clear(); + m.open(false); + assertTrue(test.size() == 2); + } + + @Test + public void testDispose() { + fManager.dispose(); + } + + @Test + public void testDispose2() { + fManager.register(fProvider1); + fManager.dispose(); + } + + @Test + public void testLogClassOfQException() { + Exception e = new Exception("FFF"); + fManager.log(LogUtil.error(getClass(), e)); + } + + @Test + public void testIsRead() { + fManager.register(fProvider1); + fProvider1.setTips(Arrays.asList(new TestTip(fProvider1.getID(),"<b>bold</b>", "Tip 1"), + new TestTip(fProvider1.getID(),"<b>bold2</b>", "Tip 2"))); + fManager.setAsRead(fProvider1.getCurrentTip()); + assertTrue(fManager.isRead(fProvider1.getCurrentTip())); + } + + @Test + public void testSetRead() { + fManager.register(fProvider1); + fProvider1.setTips(Arrays.asList(new TestTip(fProvider1.getID(),"<b>bold</b>", "Tip 1"), + new TestTip(fProvider1.getID(),"<b>bold2</b>", "Tip 2"))); + fManager.setAsRead(fProvider1.getCurrentTip()); + assertTrue(fManager.isRead(fProvider1.getCurrentTip())); + } + + @Test + public void testSetServeUnread() { + fManager.register(fProvider1); + fProvider1.setTips(Arrays.asList(new TestTip(fProvider1.getID(),"<b>bold</b>", "Tip 1"), + new TestTip(fProvider1.getID(),"<b>bold2</b>", "Tip 2"))); + fManager.setAsRead(fProvider1.getCurrentTip()); + assertTrue(fProvider1.getTips(true).size() + "", fProvider1.getTips(true).size() == 1); + fManager.setServeReadTips(true); + assertTrue(fProvider1.getTips(true).size() == 2); + } + + @Test + public void testIsServeUnread() { + assertTrue(fManager.mustServeReadTips() == false); + fManager.setServeReadTips(true); + assertTrue(fManager.mustServeReadTips()); + } +}
\ No newline at end of file diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipProviderTest.java b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipProviderTest.java new file mode 100644 index 000000000..f56013e64 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipProviderTest.java @@ -0,0 +1,191 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.core; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Status; +import org.junit.Before; +import org.junit.Test; + +public class TipProviderTest { + + private TestTipManager fManager; + private TestTipProvider fProvider; + + @Before + public void testTipProvider() { + fManager = new TestTipManager(); + fProvider = (TestTipProvider) new TestTipProvider().setManager(fManager); + } + + @Test + public void testDispose() { + fProvider.dispose(); + } + + @Test + public void testGetDescription() { + assertTrue(fProvider.getDescription() != null); + } + + @Test + public void testGetID() { + assertTrue(fProvider.getID() != null); + assertTrue(fProvider.getID().equals(fProvider.getClass().getName())); + } + + + @Test + public void testGetImage() { + assertTrue(fProvider.getImage() != null); + } + + @Test + public void testGetTips() { + assertTrue(fProvider.getTips(false).size() == 0); + createTestDate(); + fManager.setAsRead(fProvider.getNextTip()); + assertTrue(fProvider.getTips(false).size() == 2); + assertTrue(fProvider.getTips(false).size() == 2); + List<Tip> tips = fProvider.getTips(true); + assertTrue(fProvider.getTips(true).size() == 1); + ((TipManager) fProvider.getManager()).setServeReadTips(true); + assertTrue(fProvider.getTips(false).size() == 2); + } + + private void createTestDate() { + fProvider.setTips(Arrays.asList(new TestTip(fProvider.getID(),"<b>bold</b>", "Tip 1"), + new TestTip(fProvider.getID(),"<b>bold2</b>", "Tip 2"))); + } + + @Test + public void testGetCurrentTip() { + assertTrue(fProvider.getNextTip().equals(fProvider.getCurrentTip())); + } + + @Test + public void testGetCurrentTip2() { + assertTrue(fProvider.getCurrentTip().equals(fProvider.getPreviousTip())); + } + + @Test + public void testGetNextTip() { + createTestDate(); + fManager.setAsRead(fProvider.getNextTip()); + assertTrue(fProvider.getNextTip().equals(fProvider.getCurrentTip())); + Tip nextTip = fProvider.getNextTip(); + fManager.setAsRead(nextTip); + assertTrue(fManager.isRead(nextTip)); + Tip nextTip2 = fProvider.getNextTip(); + fManager.setAsRead(nextTip2); + assertTrue(fManager.isRead(nextTip2)); + assertTrue(fProvider.getNextTip().getClass().getSimpleName().equals("FinalTip")); + ((TipManager) fProvider.getManager()).setServeReadTips(true); + assertFalse(fProvider.getNextTip().getClass().getSimpleName().equals("FinalTip")); + } + + @Test + public void testGetPreviousTip() { + assertTrue(fProvider.getPreviousTip().equals(fProvider.getCurrentTip())); + assertTrue(fProvider.getPreviousTip().equals(fProvider.getCurrentTip())); + } + + @Test + public void testGetPreviousTip2() { + assertTrue(!fProvider.getPreviousTip().equals(null)); + assertTrue(fProvider.getNextTip().getClass().getSimpleName().equals("FinalTip")); + } + + @Test + public void testGetPreviousTip3() { + ((TipManager) fProvider.getManager()).setServeReadTips(true); + assertTrue(fProvider.getPreviousTip().equals(fProvider.getCurrentTip())); + } + + @Test + public void testGetPreviousTip4() { + createTestDate(); + assertTrue(fProvider.getPreviousTip() != null); + assertTrue(fProvider.getPreviousTip() != null); + assertTrue(fProvider.getPreviousTip() != null); + } + + @Test + public void testGetTipManager() { + assertTrue(fProvider.getManager().equals(fManager)); + } + + @Test + public void testIsReady() { + TestTipProvider p = (TestTipProvider) new TestTipProvider().setManager(fManager); + assertTrue(!p.isReady()); + p.setTips(Collections.emptyList()); + assertTrue(p.isReady()); + } + + @Test + public void testLoad() { + TestTipProvider p = (TestTipProvider) new TestTipProvider().setManager(fManager); + assertTrue(!p.isReady()); + p.loadNewTips(new NullProgressMonitor()); + assertTrue(p.isReady()); + } + + @Test + public void testSetManager() { + TestTipProvider p = new TestTipProvider(); + assertTrue(p.getManager() == null); + p.setManager(fManager); + assertTrue(p.getManager() != null); + } + + @Test + public void testSetTips() { + TestTipProvider p = new TestTipProvider() { + @Override + public IStatus loadNewTips(IProgressMonitor pMonitor) { + assertTrue(getTips(false).size() == 0); + assertTrue(setTips(Arrays.asList(new Tip[] { new TestTip(getID(),"DDD", "XXX") })).getTips(false) + .size() == 1); + return Status.OK_STATUS; + } + }; + assertTrue(p.getTips(false).size() == 0); + fManager.register(p); + assertTrue(p.getTips(false).size() == 1); + } + + @Test + public void testAddTips() { + TestTipProvider p = new TestTipProvider() { + @Override + public IStatus loadNewTips(IProgressMonitor pMonitor) { + assertTrue(getTips(false).size() == 0); + assertTrue(setTips(Arrays.asList(new Tip[] { new TestTip(getID(),"DDD", "XXX") })).getTips(false) + .size() == 1); + assertTrue(addTips(Arrays.asList(new Tip[] { new TestTip(getID(),"DDD", "XXX") })).getTips(false) + .size() == 2); + return Status.OK_STATUS; + } + }; + fManager.register(p); + + } +} diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipTest.java b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipTest.java new file mode 100644 index 000000000..81e4a09e9 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/core/TipTest.java @@ -0,0 +1,153 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.core; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class TipTest { + + private static final String HTML = "<head></head>"; + private static final String SUBJECT_TIP = "Tip Subject"; + private TestTipManager fManager; + private TestTipProvider fProvider; + private TestTip fTip; + + @Before + public void setup() { + fManager = new TestTipManager(); + fProvider = (TestTipProvider) new TestTipProvider().setManager(fManager); + createTestDate(); + fTip = new TestTip(fProvider.getID(), HTML, SUBJECT_TIP) { + @Override + public List<TipAction> getActions() { + ArrayList<TipAction> actions = new ArrayList<>(); + ArrayList<String> result = new ArrayList<>(); + Runnable runner = () -> result.add("entry"); + + actions.add(new TipAction("text", "tooltip", runner, null)); + return actions; + } + }; + } + + @Test + public void testHashCode() { + assertTrue(fProvider.getNextTip().hashCode() != 0); + } + + @Test + public void testHashCode2() { + TestTip testTip = new TestTip(fProvider.getID(), HTML, SUBJECT_TIP); + TestTip testTip2 = new TestTip(fProvider.getID(), HTML, SUBJECT_TIP); + assertTrue(testTip.hashCode() == testTip2.hashCode()); + } + + @Test + public void testTip() { + new TestTip(fProvider.getID(),HTML, SUBJECT_TIP); + } + + @Test + public void testGetAction() { + assertTrue(fTip.getActions().size() > 0); + } + + @Test + public void testGetCreationDate() { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(fTip.getCreationDate()); + assertTrue(calendar.get(Calendar.MONTH) == 11); + assertTrue(calendar.get(Calendar.YEAR) == 1964); + assertTrue(calendar.get(Calendar.DAY_OF_MONTH) == 31); + } + + @Test + public void testGetHTML() { + assertTrue(fTip.getHTML() != null); + } + + @Test + public void testGetImage() { + assertTrue(fTip.getImage() == null); + } + + @Test + public void testGetSubject() { + assertTrue(fTip.getSubject() != null); + assertTrue(fTip.getSubject().equals(SUBJECT_TIP)); + } + + @Test + public void testEqualsObject() { + TestTip testTip = new TestTip(fProvider.getID(),HTML, SUBJECT_TIP); + TestTip testTipx = new TestTip(fProvider.getID(),HTML, SUBJECT_TIP); + assertTrue(!testTip.equals("hello")); + assertTrue(!testTip.equals(null)); + assertTrue(testTip.equals(testTip)); + assertTrue(!testTip.equals(fTip)); + assertTrue(testTip.equals(testTipx)); + + TestTipProvider testTipProvider = new TestTipProvider() { + @Override + public String getID() { + return "sss"; + } + }; + TestTip testTip2 = new TestTip(fProvider.getID(),HTML, SUBJECT_TIP + "DDD"); + assertTrue(!testTip.equals(testTip2)); + assertTrue(!testTip.equals(testTip2)); + + TestTip testTip3 = new TestTip(fProvider.getID(),HTML, SUBJECT_TIP + "DDD"); + assertTrue(!testTip.equals(testTip3)); + assertTrue(!testTip3.equals(testTip)); + + TestTipProvider testTipProvider2 = new TestTipProvider() { + @Override + public String getID() { + return null; + } + }; + + TestTip testTip4 = new TestTip(fProvider.getID(), HTML, SUBJECT_TIP + "DDD"); + assertTrue(!testTip.equals(testTip4)); + assertTrue(!testTip4.equals(testTip)); + + TestTip testTip5 = new TestTip(fProvider.getID(),HTML, SUBJECT_TIP + "DDDWW"); + assertTrue(!testTip.equals(testTip5)); + assertTrue(!testTip5.equals(testTip)); + + TestTip testTip6 = new TestTip(fProvider.getID(),HTML, null); + assertTrue(!testTip.equals(testTip6)); + assertTrue(!testTip6.equals(testTip)); + + } + + @Test + public void testIsRead() { + assertTrue(!fManager.isRead(fTip)); + fManager.setAsRead(fTip); + fManager.setAsRead(fTip); + assertTrue(fManager.isRead(fTip)); + } + + private void createTestDate() { + fProvider.setTips(Arrays.asList(new TestTip(fProvider.getID(),"<b>bold</b>", "Tip 1"), + new TestTip(fProvider.getID(),"<b>bold2</b>", "Tip 2"))); + } +}
\ No newline at end of file diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/Sleak.java b/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/Sleak.java new file mode 100644 index 000000000..88d1856d8 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/Sleak.java @@ -0,0 +1,306 @@ +package org.eclipse.tips.manual.tests; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +/* + * Copyright (c) 2000, 2018 IBM Corp. All rights reserved. + * This file is made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + */ +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Cursor; +import org.eclipse.swt.graphics.DeviceData; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.graphics.Region; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Canvas; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.List; +import org.eclipse.swt.widgets.MessageBox; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + +public class Sleak { + Display display; + Shell shell; + List list; + Canvas canvas; + Button start, stop, check; + Text text; + Label label; + + Object[] oldObjects = new Object[0]; + Error[] oldErrors = new Error[0]; + Object[] objects = new Object[0]; + Error[] errors = new Error[0]; + + public void open() { + display = Display.getCurrent(); + shell = new Shell(display); + shell.setText("S-Leak"); + list = new List(shell, SWT.BORDER | SWT.V_SCROLL); + list.addListener(SWT.Selection, event -> refreshObject()); + text = new Text(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); + canvas = new Canvas(shell, SWT.BORDER); + canvas.addListener(SWT.Paint, event -> paintCanvas(event)); + check = new Button(shell, SWT.CHECK); + check.setText("Stack"); + check.addListener(SWT.Selection, e -> toggleStackTrace()); + start = new Button(shell, SWT.PUSH); + start.setText("Snap"); + start.addListener(SWT.Selection, event -> refreshAll()); + stop = new Button(shell, SWT.PUSH); + stop.setText("Diff"); + stop.addListener(SWT.Selection, event -> refreshDifference()); + label = new Label(shell, SWT.BORDER); + label.setText("0 object(s)"); + shell.addListener(SWT.Resize, e -> layout()); + check.setSelection(false); + text.setVisible(false); + Point size = shell.getSize(); + shell.setSize(size.x / 2, size.y / 2); + shell.open(); + refreshAll(); + } + + void refreshLabel() { + int colors = 0, cursors = 0, fonts = 0, gcs = 0, images = 0; + for (Object object : objects) { + if (object instanceof Color) { + colors++; + } + if (object instanceof Cursor) { + cursors++; + } + if (object instanceof Font) { + fonts++; + } + if (object instanceof GC) { + gcs++; + } + if (object instanceof Image) { + images++; + } + } + String string = ""; + if (colors != 0) { + string += colors + " Color(s)\n"; + } + if (cursors != 0) { + string += cursors + " Cursor(s)\n"; + } + if (fonts != 0) { + string += fonts + " Font(s)\n"; + } + if (gcs != 0) { + string += gcs + " GC(s)\n"; + } + if (images != 0) { + string += images + " Image(s)\n"; + } + /* Currently regions are not counted. */ + // if (regions != 0) string += regions + " Region(s)\n"; + if (string.length() != 0) { + string = string.substring(0, string.length() - 1); + } + label.setText(string); + } + + void refreshDifference() { + DeviceData info = display.getDeviceData(); + if (!info.tracking) { + MessageBox dialog = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK); + dialog.setText(shell.getText()); + dialog.setMessage("Warning: Device is not tracking resource allocation"); + dialog.open(); + } + Object[] newObjects = info.objects; + Error[] newErrors = info.errors; + Object[] diffObjects = new Object[newObjects.length]; + Error[] diffErrors = new Error[newErrors.length]; + int count = 0; + for (int i = 0; i < newObjects.length; i++) { + int index = 0; + while (index < oldObjects.length) { + if (newObjects[i] == oldObjects[index]) { + break; + } + index++; + } + if (index == oldObjects.length) { + diffObjects[count] = newObjects[i]; + diffErrors[count] = newErrors[i]; + count++; + } + } + objects = new Object[count]; + errors = new Error[count]; + System.arraycopy(diffObjects, 0, objects, 0, count); + System.arraycopy(diffErrors, 0, errors, 0, count); + list.removeAll(); + text.setText(""); + canvas.redraw(); + for (Object object : objects) { + list.add(objectName(object)); + } + refreshLabel(); + layout(); + } + + String objectName(Object object) { + String string = object.toString(); + int index = string.lastIndexOf('.'); + if (index == -1) { + return string; + } + return string.substring(index + 1, string.length()); + } + + void toggleStackTrace() { + refreshObject(); + layout(); + } + + void paintCanvas(Event event) { + canvas.setCursor(null); + int index = list.getSelectionIndex(); + if (index == -1) { + return; + } + GC gc = event.gc; + Object object = objects[index]; + if (object instanceof Color) { + if (((Color) object).isDisposed()) { + return; + } + gc.setBackground((Color) object); + gc.fillRectangle(canvas.getClientArea()); + return; + } + if (object instanceof Cursor) { + if (((Cursor) object).isDisposed()) { + return; + } + canvas.setCursor((Cursor) object); + return; + } + if (object instanceof Font) { + if (((Font) object).isDisposed()) { + return; + } + gc.setFont((Font) object); + FontData[] array = gc.getFont().getFontData(); + String string = ""; + String lf = text.getLineDelimiter(); + for (FontData data : array) { + String style = "NORMAL"; + int bits = data.getStyle(); + if (bits != 0) { + if ((bits & SWT.BOLD) != 0) { + style = "BOLD "; + } + if ((bits & SWT.ITALIC) != 0) { + style += "ITALIC"; + } + } + string += data.getName() + " " + data.getHeight() + " " + style + lf; + } + gc.drawString(string, 0, 0); + return; + } + // NOTHING TO DRAW FOR GC + // if (object instanceof GC) { + // return; + // } + if (object instanceof Image) { + if (((Image) object).isDisposed()) { + return; + } + gc.drawImage((Image) object, 0, 0); + return; + } + if (object instanceof Region) { + if (((Region) object).isDisposed()) { + return; + } + String string = ((Region) object).getBounds().toString(); + gc.drawString(string, 0, 0); + return; + } + } + + void refreshObject() { + int index = list.getSelectionIndex(); + if (index == -1) { + return; + } + if (check.getSelection()) { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + PrintStream s = new PrintStream(stream); + errors[index].printStackTrace(s); + text.setText(stream.toString()); + text.setVisible(true); + canvas.setVisible(false); + } else { + canvas.setVisible(true); + text.setVisible(false); + canvas.redraw(); + } + } + + void refreshAll() { + oldObjects = new Object[0]; + oldErrors = new Error[0]; + refreshDifference(); + oldObjects = objects; + oldErrors = errors; + } + + void layout() { + Rectangle rect = shell.getClientArea(); + int width = 0; + String[] items = list.getItems(); + GC gc = new GC(list); + for (int i = 0; i < objects.length; i++) { + width = Math.max(width, gc.stringExtent(items[i]).x); + } + gc.dispose(); + Point size1 = start.computeSize(SWT.DEFAULT, SWT.DEFAULT); + Point size2 = stop.computeSize(SWT.DEFAULT, SWT.DEFAULT); + Point size3 = check.computeSize(SWT.DEFAULT, SWT.DEFAULT); + Point size4 = label.computeSize(SWT.DEFAULT, SWT.DEFAULT); + width = Math.max(size1.x, Math.max(size2.x, Math.max(size3.x, width))); + width = Math.max(64, Math.max(size4.x, list.computeSize(width, SWT.DEFAULT).x)); + start.setBounds(0, 0, width, size1.y); + stop.setBounds(0, size1.y, width, size2.y); + check.setBounds(0, size1.y + size2.y, width, size3.y); + label.setBounds(0, rect.height - size4.y, width, size4.y); + int height = size1.y + size2.y + size3.y; + list.setBounds(0, height, width, rect.height - height - size4.y); + text.setBounds(width, 0, rect.width - width, rect.height); + canvas.setBounds(width, 0, rect.width - width, rect.height); + } + + public static void main(String[] args) { + Display display = new Display(); + Sleak sleak = new Sleak(); + sleak.open(); + while (!sleak.shell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + display.dispose(); + } + +} diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/SleakTipManager.java b/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/SleakTipManager.java new file mode 100644 index 000000000..150ba8021 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/SleakTipManager.java @@ -0,0 +1,132 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.manual.tests; + +import java.net.MalformedURLException; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.swt.graphics.DeviceData; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.tips.core.ITipManager; +import org.eclipse.tips.core.JsonTestProvider; +import org.eclipse.tips.core.Tip; +import org.eclipse.tips.core.TipManager; +import org.eclipse.tips.core.TipProvider; +import org.eclipse.tips.ui.internal.TipDialog; +import org.eclipse.tips.ui.internal.util.ResourceManager; + +/** + * Class to manage the tip providers and start the tip of the day UI. + */ +public class SleakTipManager extends TipManager { + + private static SleakTipManager instance = new SleakTipManager(); + + public static void main(String[] args) throws MalformedURLException { + instance.register(new JsonTestProvider()); + if (!instance.getProviders().isEmpty()) { + instance.open(true); + } + } + + /** + * @return the tip manager instance. + */ + public static SleakTipManager getInstance() { + return instance; + } + + private SleakTipManager() { + } + + /** + * For resource leak detection rename this method to open and run the IDE. Won't + * work on Linux because GTK cannot handle multiple displays. + */ + @Override + public TipManager open(boolean pStart) { + + Thread t = new Thread(() -> { + + DeviceData data = new DeviceData(); + data.tracking = true; + Display display = new Display(data); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + new Sleak().open(); + TipDialog tipDialog = new TipDialog(shell, SleakTipManager.this, TipDialog.DEFAULT_STYLE); + tipDialog.addDisposeListener(pE -> dispose()); + tipDialog.open(); + shell.pack(); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + display.dispose(); + }); + + t.start(); + return this; + + } + + @Override + public ITipManager register(TipProvider provider) { + super.register(provider); + load(provider); + return this; + } + + private void load(TipProvider pProvider) { + pProvider.loadNewTips(new NullProgressMonitor()); + } + + @Override + public boolean isRead(Tip pTip) { + return false; + } + + @Override + public TipManager setAsRead(Tip pTip) { + return this; + } + + protected synchronized SleakTipManager setNewTips(boolean pNewTips) { + return this; + } + + @Override + public void dispose() { + ResourceManager.dispose(); + super.dispose(); + } + + @Override + public TipManager setRunAtStartup(boolean pShouldRun) { + return this; + } + + @Override + public ITipManager log(IStatus pStatus) { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getPriority(TipProvider pProvider) { + return 0; + } +}
\ No newline at end of file diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/XML.java b/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/XML.java new file mode 100644 index 000000000..888bbe11d --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/XML.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.manual.tests; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; + +public class XML { + + public static void main(String[] args) + throws ParserConfigurationException, TransformerException, SAXException, IOException { + String pExpression = "<with variable=\"activeWorkbenchWindow.activePerspective\"><equals value=\"org.eclipse.jdt.ui.JavaPerspective\"></equals></with>"; + pExpression = "<enablement>" + pExpression + "</enablement>"; + pExpression = "<?xml version=\"1.0\"?>" + pExpression; + System.out.println(pExpression); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(pExpression.getBytes())); + Element element2 = (Element) doc.getElementsByTagName("enablement").item(0); + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(element2); + StreamResult result = new StreamResult(System.out); + transformer.transform(source, result); + } +} diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/package-info.java b/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/package-info.java new file mode 100644 index 000000000..9e3779299 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/manual/tests/package-info.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +/** + * + */ +/** + * @author jongw + * + */ +package org.eclipse.tips.manual.tests;
\ No newline at end of file diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/tests/Good.java b/org.eclipse.tips.tests/src/org/eclipse/tips/tests/Good.java new file mode 100644 index 000000000..7232dd419 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/tests/Good.java @@ -0,0 +1,23 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.tests; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class Good { + + @Test + public void test() { + assertTrue(true); + } +} diff --git a/org.eclipse.tips.tests/src/org/eclipse/tips/util/ImageUtilTest.java b/org.eclipse.tips.tests/src/org/eclipse/tips/util/ImageUtilTest.java new file mode 100644 index 000000000..d98699d89 --- /dev/null +++ b/org.eclipse.tips.tests/src/org/eclipse/tips/util/ImageUtilTest.java @@ -0,0 +1,142 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.util; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.tips.ui.internal.util.ImageUtil; +import org.junit.Test; + +public class ImageUtilTest { + + private static String fImageBase64 = "" // + + "iVBORw0KGgoAAAANSUhEUgAAACIAAAAkCAYAAADsHujfAAAAAXNSR0IArs4c6QAAAARnQU1BAACx" + + "jwv8YQUAAAAJcEhZcwAAJOgAACToAYJjBRwAAAEcSURBVFhH7dZBCsIwEIXh3MkbuPJubtwI0kOIK" + + "IJuddseo+eIPORBDZM0MyPaRQr/IiRtP9osEoa+j0uoQdKykPVtiLunPKdtcx/i9iHPMRECRDi+82K" + + "A4LNKGBHCG5kVM0UgjKV1SITgxdMHIC1Gg0DZPeLBaBEoC0EWjAWBihCkwVgRaBaCajAeBKqCoBLGi0" + + "DVECRhVtfPsQWBVBAkYZgVgdQQlPsy0traTJB0TzDuGUtqSA7BrBgVJEVgLP0mC6YaIiE49w1MFaSEYF" + + "7MLKQGwTyYIkSDYFZMFmJBMAtGhHgQTIsRIdObLQiWYnAWltYhEYJDrhfBiCkhUHGz/rIGSWuQtAZJC+M" + + "4xn93Ol9iiAu49oduKZAuvgC5R8NSTiN3qAAAAABJRU5ErkJggg=="; + + private static String fImageBase64HTML = "data:image/png;base64," // + + "iVBORw0KGgoAAAANSUhEUgAAACIAAAAkCAYAAADsHujfAAAAAXNSR0IArs4c6QAAAARnQU1BAACx" + + "jwv8YQUAAAAJcEhZcwAAJOgAACToAYJjBRwAAAEcSURBVFhH7dZBCsIwEIXh3MkbuPJubtwI0kOIK" + + "IJuddseo+eIPORBDZM0MyPaRQr/IiRtP9osEoa+j0uoQdKykPVtiLunPKdtcx/i9iHPMRECRDi+82K" + + "A4LNKGBHCG5kVM0UgjKV1SITgxdMHIC1Gg0DZPeLBaBEoC0EWjAWBihCkwVgRaBaCajAeBKqCoBLGi0" + + "DVECRhVtfPsQWBVBAkYZgVgdQQlPsy0traTJB0TzDuGUtqSA7BrBgVJEVgLP0mC6YaIiE49w1MFaSEYF" + + "7MLKQGwTyYIkSDYFZMFmJBMAtGhHgQTIsRIdObLQiWYnAWltYhEYJDrhfBiCkhUHGz/rIGSWuQtAZJC+M" + + "4xn93Ol9iiAu49oduKZAuvgC5R8NSTiN3qAAAAABJRU5ErkJggg=="; + + @Test + public void decodeImageToBase64Test() throws IOException { + Image image = new Image(null, ImageUtil.decodeToImage(fImageBase64)); + String base64Image = ImageUtil.decodeFromImage(image, SWT.IMAGE_PNG); + image.dispose(); + image = new Image(null, ImageUtil.decodeToImage(base64Image)); + String base64Image2 = ImageUtil.decodeFromImage(image, SWT.IMAGE_PNG); + assertTrue(base64Image, base64Image.equals(base64Image2)); + image.dispose(); + + Image image2 = new Image(null, ImageUtil.decodeToImage(fImageBase64HTML)); + String base64Image3 = ImageUtil.decodeFromImage(image2, SWT.IMAGE_PNG); + image2.dispose(); + image2 = new Image(null, ImageUtil.decodeToImage(base64Image3)); + String base64Image4 = ImageUtil.decodeFromImage(image2, SWT.IMAGE_PNG); + assertTrue(base64Image3, base64Image3.equals(base64Image4)); + image2.dispose(); + } + + @Test + public void testGetWidth() { + assertTrue(ImageUtil.getWidth(1, 1000, 100) == 100); + assertTrue(ImageUtil.getWidth(1, 100, 1000) == 100); + assertTrue(ImageUtil.getWidth(1, 100, 99) == 99); + assertTrue(ImageUtil.getWidth(1, 100, 98) == 98); + assertTrue(ImageUtil.getWidth(1, 100, 100) == 100); + assertTrue(ImageUtil.getWidth(1, 99, 100) == 99); + assertTrue(ImageUtil.getWidth(1, 77, 77) == 77); + assertTrue(ImageUtil.getWidth(1, 101, 100) == 100); + assertTrue(ImageUtil.getWidth(1, 149, 100) == 100); + assertTrue(ImageUtil.getWidth(1, 200, 300) == 200); + assertTrue(ImageUtil.getWidth(1, 11, 10) == 10); + + assertTrue(ImageUtil.getWidth(0.5, 1000, 100) == 50); + assertTrue(ImageUtil.getWidth(0.5, 100, 1000) == 100); + assertTrue(ImageUtil.getWidth(0.5, 100, 99) == 49); + assertTrue(ImageUtil.getWidth(0.5, 100, 98) == 49); + assertTrue(ImageUtil.getWidth(0.5, 100, 100) == 50); + assertTrue(ImageUtil.getWidth(0.5, 99, 100) == 50); + assertTrue(ImageUtil.getWidth(0.5, 77, 77) == 38); + assertTrue(ImageUtil.getWidth(0.5, 101, 100) == 50); + assertTrue(ImageUtil.getWidth(0.5, 149, 100) == 50); + assertTrue(ImageUtil.getWidth(0.5, 200, 300) == 150); + assertTrue(ImageUtil.getWidth(0.5, 11, 10) == 5); + + assertTrue(ImageUtil.getWidth(2, 1000, 100) == 200); + assertTrue(ImageUtil.getWidth(2, 100, 1000) == 100); + assertTrue(ImageUtil.getWidth(2, 100, 99) == 100); + assertTrue(ImageUtil.getWidth(2, 100, 50) == 100); + assertTrue(ImageUtil.getWidth(2, 100, 49) == 98); + assertTrue(ImageUtil.getWidth(2, 100, 98) == 100); + assertTrue(ImageUtil.getWidth(2, 100, 100) == 100); + assertTrue(ImageUtil.getWidth(2, 99, 100) == 99); + assertTrue(ImageUtil.getWidth(2, 77, 77) == 77); + assertTrue(ImageUtil.getWidth(2, 101, 100) == 101); + assertTrue(ImageUtil.getWidth(2, 149, 100) == 149); + assertTrue(ImageUtil.getWidth(2, 200, 300) == 200); + assertTrue(ImageUtil.getWidth(2, 11, 10) == 11); + } + + @Test + public void testGetHeight() { + assertTrue(ImageUtil.getHeight(1, 1000, 100) == 100); + assertTrue(ImageUtil.getHeight(1, 100, 1000) == 100); + assertTrue(ImageUtil.getHeight(1, 100, 99) == 99); + assertTrue(ImageUtil.getHeight(1, 100, 98) == 98); + assertTrue(ImageUtil.getHeight(1, 100, 100) == 100); + assertTrue(ImageUtil.getHeight(1, 99, 100) == 99); + assertTrue(ImageUtil.getHeight(1, 77, 77) == 77); + assertTrue(ImageUtil.getHeight(1, 101, 100) == 100); + assertTrue(ImageUtil.getHeight(1, 149, 100) == 100); + assertTrue(ImageUtil.getHeight(1, 200, 300) == 200); + assertTrue(ImageUtil.getHeight(1, 11, 10) == 10); + + assertTrue(ImageUtil.getHeight(0.5, 1000, 100) == 100); + assertTrue(ImageUtil.getHeight(0.5, 100, 1000) == 200); + assertTrue(ImageUtil.getHeight(0.5, 100, 99) == 99); + assertTrue(ImageUtil.getHeight(0.5, 100, 98) == 98); + assertTrue(ImageUtil.getHeight(0.5, 100, 100) == 100); + assertTrue(ImageUtil.getHeight(0.5, 99, 100) == 100); + assertTrue(ImageUtil.getHeight(0.5, 77, 77) == 77); + assertTrue(ImageUtil.getHeight(0.5, 101, 100) == 100); + assertTrue(ImageUtil.getHeight(0.5, 149, 100) == 100); + assertTrue(ImageUtil.getHeight(0.5, 200, 300) == 300); + assertTrue(ImageUtil.getHeight(0.5, 11, 10) == 10); + + assertTrue(ImageUtil.getHeight(2, 1000, 100) == 100); + assertTrue(ImageUtil.getHeight(2, 100, 1000) == 50); + assertTrue(ImageUtil.getHeight(2, 100, 99) == 50); + assertTrue(ImageUtil.getHeight(2, 100, 50) == 50); + assertTrue(ImageUtil.getHeight(2, 100, 49) == 49); + assertTrue(ImageUtil.getHeight(2, 100, 98) == 50); + assertTrue(ImageUtil.getHeight(2, 100, 100) == 50); + assertTrue(ImageUtil.getHeight(2, 99, 100) == 49); + assertTrue(ImageUtil.getHeight(2, 77, 77) == 38); + assertTrue(ImageUtil.getHeight(2, 101, 100) == 50); + assertTrue(ImageUtil.getHeight(2, 149, 100) == 74); + assertTrue(ImageUtil.getHeight(2, 200, 300) == 100); + assertTrue(ImageUtil.getHeight(2, 11, 10) == 5); + } +} |