* Check if injected CSS String is already in the list of injected styles * Change to package for unit testing * Unit test * Add missing importstags/8.15.0
target.addText(css); | target.addText(css); | ||||
target.endTag("css-string"); | target.endTag("css-string"); | ||||
} | } | ||||
@Override | |||||
public boolean equals(Object obj) { | |||||
if (obj == this) { | |||||
return true; | |||||
} else if (obj instanceof InjectedStyleString) { | |||||
InjectedStyleString that = (InjectedStyleString) obj; | |||||
return css.equals(that.css); | |||||
} else { | |||||
return false; | |||||
} | |||||
} | |||||
@Override | |||||
public int hashCode() { | |||||
return css.hashCode(); | |||||
} | |||||
} | } | ||||
private static class InjectedStyleResource implements InjectedStyle { | private static class InjectedStyleResource implements InjectedStyle { | ||||
*/ | */ | ||||
public static class Styles implements Serializable { | public static class Styles implements Serializable { | ||||
private LinkedHashSet<InjectedStyle> injectedStyles = new LinkedHashSet<>(); | |||||
LinkedHashSet<InjectedStyle> injectedStyles = new LinkedHashSet<>(); | |||||
private LinkedHashSet<InjectedStyle> pendingInjections = new LinkedHashSet<>(); | |||||
LinkedHashSet<InjectedStyle> pendingInjections = new LinkedHashSet<>(); | |||||
private final UI ui; | private final UI ui; | ||||
"Cannot inject null CSS string"); | "Cannot inject null CSS string"); | ||||
} | } | ||||
pendingInjections.add(new InjectedStyleString(css)); | |||||
ui.markAsDirty(); | |||||
InjectedStyleString injectedStyleString = new InjectedStyleString( | |||||
css); | |||||
if (!injectedStyles.contains(injectedStyleString) | |||||
&& pendingInjections.add(injectedStyleString)) { | |||||
ui.markAsDirty(); | |||||
} | |||||
} | } | ||||
/** | /** |
package com.vaadin.server; | package com.vaadin.server; | ||||
import static org.junit.Assert.assertEquals; | |||||
import static org.junit.Assert.assertFalse; | import static org.junit.Assert.assertFalse; | ||||
import java.io.Writer; | |||||
import org.easymock.EasyMock; | import org.easymock.EasyMock; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
page.getState(false).hasResizeListeners); | page.getState(false).hasResizeListeners); | ||||
} | } | ||||
@Test | |||||
public void cssStringInjectedTwice() throws PaintException { | |||||
TestPage page = new TestPage(EasyMock.createMock(UI.class), | |||||
EasyMock.createMock(PageState.class)); | |||||
JsonPaintTarget paintTarget = new JsonPaintTarget( | |||||
EasyMock.createMock(LegacyCommunicationManager.class), | |||||
EasyMock.createMock(Writer.class), true); | |||||
page.getStyles().add(".my-style { color: red; }"); | |||||
assertEquals(page.getStyles().pendingInjections.size(), 1); | |||||
page.paintContent(paintTarget); | |||||
assertEquals(page.getStyles().pendingInjections.size(), 0); | |||||
assertEquals(page.getStyles().injectedStyles.size(), 1); | |||||
page.getStyles().add(".my-style { color: red; }"); | |||||
assertEquals(page.getStyles().pendingInjections.size(), 0); | |||||
page.paintContent(paintTarget); | |||||
assertEquals(page.getStyles().pendingInjections.size(), 0); | |||||
assertEquals(page.getStyles().injectedStyles.size(), 1); | |||||
} | |||||
private static class TestPage extends Page { | private static class TestPage extends Page { | ||||
public TestPage(UI uI, PageState state) { | public TestPage(UI uI, PageState state) { |