Skip to content

Commit

Permalink
feat: Improved theme support in compatibility mode (#10181)
Browse files Browse the repository at this point in the history
Throw exception if improved theme support is
used in compatibility mode.

Part of #9983
  • Loading branch information
caalador committed Mar 4, 2021
1 parent 313cd24 commit 51e50ea
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ public static ThemeDefinition findThemeForNavigationTarget(UI ui,
.getAnnotationFor(target, Theme.class);

if (themeAnnotation.isPresent()) {
return new ThemeDefinition(themeAnnotation.get());
final ThemeDefinition themeDefinition = new ThemeDefinition(
themeAnnotation.get());
if (ui.getSession().getConfiguration().isCompatibilityMode()
&& !themeDefinition.getName().isEmpty()) {
throw new IllegalStateException(
"Improved theme support is not available in compatibility mode.");
}
return themeDefinition;
}

if (!AnnotationReader.getAnnotationFor(target, NoTheme.class)
Expand Down
62 changes: 54 additions & 8 deletions flow-server/src/test/java/com/vaadin/flow/theme/ThemeUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.internal.UIInternals;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.Router;
import com.vaadin.flow.server.RouteRegistry;
import com.vaadin.flow.server.VaadinSession;

public class ThemeUtilTest {

Expand All @@ -28,7 +30,7 @@ public String getThemeUrl() {
}

public static class ThemeSingleNavigationTargetSubclass
extends ThemeSingleNavigationTarget {
extends ThemeSingleNavigationTarget {
}

@Route("single")
Expand All @@ -37,8 +39,48 @@ public static class ThemeSingleNavigationTargetSubclass
public static class ThemeSingleNavigationTarget extends Component {
}

@Route("")
@Tag(Tag.DIV)
@Theme(themeFolder = "my-theme")
public static class ImprovedThemeSupport extends Component {
}

@Test
public void navigationTargetWithTheme_subclassGetsTheme() {
UI ui = mockUI(false);

ThemeDefinition theme = ThemeUtil.findThemeForNavigationTarget(ui,
ThemeSingleNavigationTargetSubclass.class, "single");
Assert.assertNotNull(
"Subclass should have a theme when the superclass has", theme);
Assert.assertEquals(
"Subclass should have the same theme as its superclass",
MyTheme.class, theme.getTheme());
}

@Test
public void navigationTargetWithImprovedThemeInCompatibilityMode_throwsException() {
UI ui = mockUI(true);

Assert.assertThrows(
"themeFolder value in compatibilityMode should throw.",
IllegalStateException.class, () -> ThemeUtil
.findThemeForNavigationTarget(ui, ImprovedThemeSupport.class,
""));
}

@Test
public void navigationTargetWithImprovedThemeInNpmMode_getsTheme() {
UI ui = mockUI(false);

ThemeDefinition theme = ThemeUtil
.findThemeForNavigationTarget(ui, ImprovedThemeSupport.class, "");

Assert.assertNotNull(
"Theme should be gotten in npm mode", theme);
}

private UI mockUI(final boolean compatibilityMode) {
RouteRegistry registry = Mockito.mock(RouteRegistry.class);
Router router = new Router(registry);

Expand All @@ -48,12 +90,16 @@ public void navigationTargetWithTheme_subclassGetsTheme() {
UI ui = Mockito.mock(UI.class);
Mockito.when(ui.getInternals()).thenReturn(uiInternals);

ThemeDefinition theme = ThemeUtil.findThemeForNavigationTarget(ui,
ThemeSingleNavigationTargetSubclass.class, "single");
Assert.assertNotNull(
"Subclass should have a theme when the superclass has", theme);
Assert.assertEquals(
"Subclass should have the same theme as its superclass",
MyTheme.class, theme.getTheme());
VaadinSession session = Mockito.mock(VaadinSession.class);
Mockito.when(ui.getSession()).thenReturn(session);

DeploymentConfiguration configuration = Mockito
.mock(DeploymentConfiguration.class);
Mockito.when(session.getConfiguration()).thenReturn(configuration);
Mockito.when(configuration.isCompatibilityMode())
.thenReturn(compatibilityMode);

return ui;
}

}

0 comments on commit 51e50ea

Please sign in to comment.