1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.icon.internal

File DefaultIconManagerTest.java

 

Code metrics

0
73
7
1
181
122
7
0.1
10.43
7
1

Classes

Class Line # Actions
DefaultIconManagerTest 43 73 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.icon.internal;
21   
22    import java.util.List;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.icon.Icon;
28    import org.xwiki.icon.IconRenderer;
29    import org.xwiki.icon.IconSet;
30    import org.xwiki.icon.IconSetManager;
31    import org.xwiki.test.mockito.MockitoComponentMockingRule;
32   
33    import static org.junit.Assert.assertEquals;
34    import static org.junit.Assert.assertTrue;
35    import static org.mockito.Mockito.when;
36   
37    /**
38    * Test class for {@link org.xwiki.icon.internal.DefaultIconManager}.
39    *
40    * @since 6.2M1
41    * @version $Id: 6d4825227616d34c8e45205f891c14dcc85dd004 $
42    */
 
43    public class DefaultIconManagerTest
44    {
45    @Rule
46    public MockitoComponentMockingRule<DefaultIconManager> mocker =
47    new MockitoComponentMockingRule<>(DefaultIconManager.class);
48   
49    private IconSetManager iconSetManager;
50   
51    private IconRenderer iconRenderer;
52   
 
53  6 toggle @Before
54    public void setUp() throws Exception
55    {
56  6 iconSetManager = mocker.getInstance(IconSetManager.class);
57  6 iconRenderer = mocker.getInstance(IconRenderer.class);
58    }
59   
 
60  1 toggle @Test
61    public void render() throws Exception
62    {
63  1 IconSet iconSet = new IconSet("silk");
64  1 iconSet.addIcon("test", new Icon("hello"));
65  1 when(iconSetManager.getCurrentIconSet()).thenReturn(iconSet);
66  1 when(iconRenderer.render("test", iconSet)).thenReturn("rendered icon");
67   
68    // Test
69  1 String result = mocker.getComponentUnderTest().render("test");
70  1 assertEquals("rendered icon", result);
71    }
72   
 
73  1 toggle @Test
74    public void renderWithFallBack() throws Exception
75    {
76  1 IconSet iconSet = new IconSet("silk");
77  1 when(iconSetManager.getCurrentIconSet()).thenReturn(iconSet);
78   
79  1 IconSet defaultIconSet = new IconSet("default");
80  1 when(iconSetManager.getDefaultIconSet()).thenReturn(defaultIconSet);
81  1 when(iconRenderer.render("test", defaultIconSet)).thenReturn("rendered icon");
82   
83    // Test
84  1 String result = mocker.getComponentUnderTest().render("test");
85  1 assertEquals("rendered icon", result);
86    }
87   
 
88  1 toggle @Test
89    public void renderHTML() throws Exception
90    {
91  1 IconSet iconSet = new IconSet("silk");
92  1 iconSet.addIcon("test", new Icon("hello"));
93  1 when(iconSetManager.getCurrentIconSet()).thenReturn(iconSet);
94  1 when(iconRenderer.renderHTML("test", iconSet)).thenReturn("rendered icon");
95   
96    // Test
97  1 String result = mocker.getComponentUnderTest().renderHTML("test");
98  1 assertEquals("rendered icon", result);
99    }
100   
 
101  1 toggle @Test
102    public void renderHTMLWithFallBack() throws Exception
103    {
104  1 IconSet iconSet = new IconSet("silk");
105  1 when(iconSetManager.getCurrentIconSet()).thenReturn(iconSet);
106   
107  1 IconSet defaultIconSet = new IconSet("default");
108  1 when(iconSetManager.getDefaultIconSet()).thenReturn(defaultIconSet);
109  1 when(iconRenderer.renderHTML("test", defaultIconSet)).thenReturn("rendered icon");
110   
111    // Test
112  1 String result = mocker.getComponentUnderTest().renderHTML("test");
113  1 assertEquals("rendered icon", result);
114    }
115   
 
116  1 toggle @Test
117    public void renderWithIconSetName() throws Exception
118    {
119    // Mocks
120  1 IconSet iconSet1 = new IconSet("iconSet1");
121  1 iconSet1.addIcon("icon1", new Icon("icon"));
122  1 when(iconSetManager.getIconSet("iconSet1")).thenReturn(iconSet1);
123  1 when(iconRenderer.render("icon1", iconSet1)).thenReturn("rendered icon 1");
124  1 when(iconRenderer.renderHTML("icon1", iconSet1)).thenReturn("HTML rendered icon 1");
125  1 when(iconRenderer.render("icon2", iconSet1)).thenReturn("");
126  1 when(iconRenderer.renderHTML("icon2", iconSet1)).thenReturn("");
127   
128  1 IconSet defaultIconSet = new IconSet("default");
129  1 defaultIconSet.addIcon("icon2", new Icon("icon"));
130  1 when(iconSetManager.getDefaultIconSet()).thenReturn(defaultIconSet);
131  1 when(iconRenderer.render("icon1", defaultIconSet)).thenReturn("default rendered icon 1");
132  1 when(iconRenderer.render("icon2", defaultIconSet)).thenReturn("default rendered icon 2");
133  1 when(iconRenderer.renderHTML("icon1", defaultIconSet)).thenReturn("HTML default rendered icon 1");
134  1 when(iconRenderer.renderHTML("icon2", defaultIconSet)).thenReturn("HTML default rendered icon 2");
135   
136    // Tests
137  1 assertEquals("rendered icon 1", mocker.getComponentUnderTest().render("icon1", "iconSet1"));
138  1 assertEquals("default rendered icon 2", mocker.getComponentUnderTest().render("icon2", "iconSet1"));
139  1 assertEquals("default rendered icon 1", mocker.getComponentUnderTest().render("icon1", "iconSet2"));
140  1 assertEquals("rendered icon 1", mocker.getComponentUnderTest().render("icon1", "iconSet1", true));
141  1 assertEquals("default rendered icon 2", mocker.getComponentUnderTest().render("icon2", "iconSet1", true));
142  1 assertEquals("default rendered icon 1", mocker.getComponentUnderTest().render("icon1", "iconSet2", true));
143  1 assertEquals("rendered icon 1", mocker.getComponentUnderTest().render("icon1", "iconSet1", false));
144  1 assertEquals("", mocker.getComponentUnderTest().render("icon2", "iconSet1", false));
145  1 assertEquals("", mocker.getComponentUnderTest().render("icon1", "iconSet2", false));
146   
147  1 assertEquals("HTML rendered icon 1", mocker.getComponentUnderTest().renderHTML("icon1", "iconSet1"));
148  1 assertEquals("HTML default rendered icon 2", mocker.getComponentUnderTest().renderHTML("icon2", "iconSet1"));
149  1 assertEquals("HTML default rendered icon 1", mocker.getComponentUnderTest().renderHTML("icon1", "iconSet2"));
150  1 assertEquals("HTML rendered icon 1", mocker.getComponentUnderTest().renderHTML("icon1", "iconSet1", true));
151  1 assertEquals("HTML default rendered icon 2", mocker.getComponentUnderTest().renderHTML("icon2", "iconSet1", true));
152  1 assertEquals("HTML default rendered icon 1", mocker.getComponentUnderTest().renderHTML("icon1", "iconSet2", true));
153  1 assertEquals("HTML rendered icon 1", mocker.getComponentUnderTest().renderHTML("icon1", "iconSet1", false));
154  1 assertEquals("", mocker.getComponentUnderTest().renderHTML("icon2", "iconSet1", false));
155  1 assertEquals("", mocker.getComponentUnderTest().renderHTML("icon1", "iconSet2", false));
156    }
157   
 
158  1 toggle @Test
159    public void getIconNames() throws Exception
160    {
161  1 IconSet iconSet = new IconSet("iconSet1");
162  1 iconSet.addIcon("icon1", new Icon("icon1 value"));
163  1 iconSet.addIcon("icon2", new Icon("icon2 value"));
164   
165    // Mocks
166  1 when(iconSetManager.getCurrentIconSet()).thenReturn(iconSet);
167  1 when(iconSetManager.getIconSet("iconSet1")).thenReturn(iconSet);
168   
169    // Test
170  1 List<String> results = mocker.getComponentUnderTest().getIconNames();
171  1 List<String> results2 = mocker.getComponentUnderTest().getIconNames("iconSet1");
172   
173    // Verify
174  1 assertEquals(2, results.size());
175  1 assertTrue(results.contains("icon1"));
176  1 assertTrue(results.contains("icon2"));
177  1 assertEquals(2, results2.size());
178  1 assertTrue(results2.contains("icon1"));
179  1 assertTrue(results2.contains("icon2"));
180    }
181    }