/* * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jalview. If not, see . * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.schemes; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertSame; import jalview.gui.JvOptionPane; import java.awt.Color; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class UserColourSchemeTest { @BeforeClass(alwaysRun = true) public void setUpJvOptionPane() { JvOptionPane.setInteractiveMode(false); JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); } @Test(groups = "Functional") public void testGetColourFromString() { /* * by colour name - if known to AWT, and included in * * @see ColourSchemeProperty.getAWTColorFromName() */ assertSame(Color.RED, UserColourScheme.getColourFromString("red")); assertSame(Color.RED, UserColourScheme.getColourFromString("Red")); assertSame(Color.RED, UserColourScheme.getColourFromString(" RED ")); /* * by RGB hex code */ String hexColour = Integer.toHexString(Color.RED.getRGB() & 0xffffff); assertEquals("ff0000", hexColour); assertEquals(Color.RED, UserColourScheme.getColourFromString(hexColour)); // 'hex' prefixes _not_ wanted here assertNull(UserColourScheme.getColourFromString("0x" + hexColour)); assertNull(UserColourScheme.getColourFromString("#" + hexColour)); // out of range, but Color constructor just or's the rgb value with 0 assertEquals(Color.black, UserColourScheme.getColourFromString("1000000")); /* * by RGB triplet */ Color c = Color.pink; String rgb = String.format("%d,%d,%d", c.getRed(), c.getGreen(), c.getBlue()); assertEquals("255,175,175", rgb); assertEquals(c, UserColourScheme.getColourFromString(rgb)); assertEquals(c, UserColourScheme.getColourFromString("255, 175 , 175")); /* * odds and ends */ assertNull(UserColourScheme.getColourFromString(null)); assertNull(UserColourScheme.getColourFromString("rubbish")); assertEquals(Color.WHITE, UserColourScheme.getColourFromString("-1")); assertNull(UserColourScheme.getColourFromString(String .valueOf(Integer.MAX_VALUE))); assertNull(UserColourScheme.getColourFromString("100,200,300")); assertNull(UserColourScheme.getColourFromString("100,200")); assertNull(UserColourScheme.getColourFromString("100,200,100,200")); } @Test(groups = "Functional") public void testParseAppletParameter() { UserColourScheme cs = new UserColourScheme("white"); cs.parseAppletParameter("D,E=red; K,R,H=0022FF; c=10 , 20,30"); assertEquals(Color.RED, cs.findColour('D')); assertEquals(Color.RED, cs.findColour('d')); assertEquals(Color.RED, cs.findColour('E')); assertEquals(Color.RED, cs.findColour('e')); Color c1 = new Color(0x0022ff); assertEquals(c1, cs.findColour('K')); assertEquals(c1, cs.findColour('R')); assertEquals(c1, cs.findColour('h')); Color c2 = new Color(10, 20, 30); assertEquals(c2, cs.findColour('c')); cs = new UserColourScheme("white"); cs.parseAppletParameter("D,E=red; K,R,H=0022FF; c=10 , 20,30;lowercase=blue;s=pink"); assertEquals(Color.RED, cs.findColour('D')); assertEquals(Color.blue, cs.findColour('d')); assertEquals(Color.RED, cs.findColour('E')); assertEquals(Color.blue, cs.findColour('e')); assertEquals(c1, cs.findColour('K')); assertEquals(c1, cs.findColour('R')); assertEquals(Color.blue, cs.findColour('h')); assertEquals(c2, cs.findColour('c')); // 'lowercase' sets all lower-case not already set to the given colour assertEquals(Color.blue, cs.findColour('k')); assertEquals(Color.blue, cs.findColour('a')); assertEquals(Color.pink, cs.findColour('s')); } }