You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ColourManagerTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.messages;
  23. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  24. import com.dmdirc.interfaces.config.ConfigChangeListener;
  25. import com.dmdirc.logger.ErrorManager;
  26. import com.dmdirc.logger.Logger;
  27. import com.dmdirc.ui.Colour;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.mockito.ArgumentCaptor;
  32. import org.mockito.Captor;
  33. import org.mockito.Mock;
  34. import org.mockito.runners.MockitoJUnitRunner;
  35. import static org.junit.Assert.*;
  36. import static org.mockito.Mockito.*;
  37. @RunWith(MockitoJUnitRunner.class)
  38. public class ColourManagerTest {
  39. @Mock private ErrorManager errorManager;
  40. @Mock private AggregateConfigProvider configManager;
  41. @Captor private ArgumentCaptor<ConfigChangeListener> configListener;
  42. private ColourManager manager;
  43. @Before
  44. public void setup() {
  45. Logger.setErrorManager(errorManager);
  46. manager = new ColourManager(configManager);
  47. verify(configManager).addChangeListener(anyString(), configListener.capture());
  48. }
  49. @Test
  50. public void testGetColourInt() {
  51. int spec = 4;
  52. Colour expResult = Colour.RED;
  53. Colour result = manager.getColourFromIrcCode(spec);
  54. assertEquals(expResult, result);
  55. }
  56. @Test
  57. public void testGetColourOOB() {
  58. Colour result = manager.getColourFromIrcCode(20);
  59. assertEquals(Colour.WHITE, result);
  60. }
  61. @Test
  62. public void testGetColourHexInvalid() {
  63. String spec = "FFZZFF";
  64. Colour result = manager.getColourFromHex(spec);
  65. assertEquals(Colour.WHITE, result);
  66. }
  67. @Test
  68. public void testGetColourHex() {
  69. String spec = "FFFFFF";
  70. Colour expResult = Colour.WHITE;
  71. Colour result = manager.getColourFromHex(spec);
  72. assertEquals(expResult, result);
  73. }
  74. @Test
  75. public void testParseColourNull() {
  76. Colour fallback = Colour.RED;
  77. Colour result = manager.getColourFromString(null, fallback);
  78. assertEquals(fallback, result);
  79. }
  80. @Test
  81. public void testParseColourInvalidNumber() {
  82. Colour fallback = Colour.RED;
  83. Colour result = manager.getColourFromString("zz", fallback);
  84. assertEquals(fallback, result);
  85. }
  86. @Test
  87. public void testParseColourOOBNumber() {
  88. Colour fallback = Colour.RED;
  89. Colour result = manager.getColourFromString("20", fallback);
  90. assertEquals(fallback, result);
  91. }
  92. @Test
  93. public void testParseColourShortNumber() {
  94. Colour fallback = Colour.RED;
  95. Colour result = manager.getColourFromString("1234", fallback);
  96. assertEquals(fallback, result);
  97. }
  98. @Test
  99. public void testColourCache() {
  100. Colour result1 = manager.getColourFromString("ff0f0f", Colour.WHITE);
  101. Colour result2 = manager.getColourFromString("ff0f0f", Colour.WHITE);
  102. Colour result3 = manager.getColourFromHex("ff0f0f");
  103. assertSame(result1, result2);
  104. assertSame(result2, result3);
  105. }
  106. @Test
  107. public void testColourToHex() {
  108. Colour c1 = manager.getColourFromHex("ab3400");
  109. assertEquals("ab3400", ColourManager.getHex(c1).toLowerCase());
  110. }
  111. @Test
  112. public void testCustomColours() {
  113. when(configManager.hasOptionColour("colour", "4")).thenReturn(true);
  114. when(configManager.getOptionColour("colour", "4")).thenReturn(Colour.GREEN);
  115. configListener.getValue().configChanged("colour", "4");
  116. assertEquals("00ff00", ColourManager.getHex(manager.getColourFromIrcCode(4)).toLowerCase());
  117. }
  118. @Test
  119. public void testCustomColours2() {
  120. when(configManager.hasOptionColour("colour", "4")).thenReturn(true);
  121. when(configManager.getOptionColour("colour", "4")).thenReturn(Colour.GREEN);
  122. configListener.getValue().configChanged("colour", "4");
  123. when(configManager.hasOptionColour("colour", "4")).thenReturn(false);
  124. configListener.getValue().configChanged("colour", "4");
  125. assertEquals("ff0000", ColourManager.getHex(manager.getColourFromIrcCode(4)).toLowerCase());
  126. }
  127. }